]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Remove dwarf2_cu::language
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
47 #include "bfd.h"
48 #include "elf-bfd.h"
49 #include "symtab.h"
50 #include "gdbtypes.h"
51 #include "objfiles.h"
52 #include "dwarf2.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91 #include <unordered_set>
92
93 /* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96 static unsigned int dwarf_read_debug = 0;
97
98 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
99
100 #define dwarf_read_debug_printf(fmt, ...) \
101 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
102 ##__VA_ARGS__)
103
104 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
105
106 #define dwarf_read_debug_printf_v(fmt, ...) \
107 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
108 ##__VA_ARGS__)
109
110 /* When non-zero, dump DIEs after they are read in. */
111 static unsigned int dwarf_die_debug = 0;
112
113 /* When non-zero, dump line number entries as they are read in. */
114 unsigned int dwarf_line_debug = 0;
115
116 /* When true, cross-check physname against demangler. */
117 static bool check_physname = false;
118
119 /* When true, do not reject deprecated .gdb_index sections. */
120 static bool use_deprecated_index_sections = false;
121
122 /* This is used to store the data that is always per objfile. */
123 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
124
125 /* These are used to store the dwarf2_per_bfd objects.
126
127 objfiles having the same BFD, which doesn't require relocations, are going to
128 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
129
130 Other objfiles are not going to share a dwarf2_per_bfd with any other
131 objfiles, so they'll have their own version kept in the _objfile_data_key
132 version. */
133 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
134 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
135
136 /* The "aclass" indices for various kinds of computed DWARF symbols. */
137
138 static int dwarf2_locexpr_index;
139 static int dwarf2_loclist_index;
140 static int dwarf2_locexpr_block_index;
141 static int dwarf2_loclist_block_index;
142
143 /* Size of .debug_loclists section header for 32-bit DWARF format. */
144 #define LOCLIST_HEADER_SIZE32 12
145
146 /* Size of .debug_loclists section header for 64-bit DWARF format. */
147 #define LOCLIST_HEADER_SIZE64 20
148
149 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
150 #define RNGLIST_HEADER_SIZE32 12
151
152 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
153 #define RNGLIST_HEADER_SIZE64 20
154
155 /* An index into a (C++) symbol name component in a symbol name as
156 recorded in the mapped_index's symbol table. For each C++ symbol
157 in the symbol table, we record one entry for the start of each
158 component in the symbol in a table of name components, and then
159 sort the table, in order to be able to binary search symbol names,
160 ignoring leading namespaces, both completion and regular look up.
161 For example, for symbol "A::B::C", we'll have an entry that points
162 to "A::B::C", another that points to "B::C", and another for "C".
163 Note that function symbols in GDB index have no parameter
164 information, just the function/method names. You can convert a
165 name_component to a "const char *" using the
166 'mapped_index::symbol_name_at(offset_type)' method. */
167
168 struct name_component
169 {
170 /* Offset in the symbol name where the component starts. Stored as
171 a (32-bit) offset instead of a pointer to save memory and improve
172 locality on 64-bit architectures. */
173 offset_type name_offset;
174
175 /* The symbol's index in the symbol and constant pool tables of a
176 mapped_index. */
177 offset_type idx;
178 };
179
180 /* Base class containing bits shared by both .gdb_index and
181 .debug_name indexes. */
182
183 struct mapped_index_base
184 {
185 mapped_index_base () = default;
186 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
187
188 /* The name_component table (a sorted vector). See name_component's
189 description above. */
190 std::vector<name_component> name_components;
191
192 /* How NAME_COMPONENTS is sorted. */
193 enum case_sensitivity name_components_casing;
194
195 /* Return the number of names in the symbol table. */
196 virtual size_t symbol_name_count () const = 0;
197
198 /* Get the name of the symbol at IDX in the symbol table. */
199 virtual const char *symbol_name_at
200 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
201
202 /* Return whether the name at IDX in the symbol table should be
203 ignored. */
204 virtual bool symbol_name_slot_invalid (offset_type idx) const
205 {
206 return false;
207 }
208
209 /* Build the symbol name component sorted vector, if we haven't
210 yet. */
211 void build_name_components (dwarf2_per_objfile *per_objfile);
212
213 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
214 possible matches for LN_NO_PARAMS in the name component
215 vector. */
216 std::pair<std::vector<name_component>::const_iterator,
217 std::vector<name_component>::const_iterator>
218 find_name_components_bounds (const lookup_name_info &ln_no_params,
219 enum language lang,
220 dwarf2_per_objfile *per_objfile) const;
221
222 /* Prevent deleting/destroying via a base class pointer. */
223 protected:
224 ~mapped_index_base() = default;
225 };
226
227 /* This is a view into the index that converts from bytes to an
228 offset_type, and allows indexing. Unaligned bytes are specifically
229 allowed here, and handled via unpacking. */
230
231 class offset_view
232 {
233 public:
234 offset_view () = default;
235
236 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
237 : m_bytes (bytes)
238 {
239 }
240
241 /* Extract the INDEXth offset_type from the array. */
242 offset_type operator[] (size_t index) const
243 {
244 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
245 return (offset_type) extract_unsigned_integer (bytes,
246 sizeof (offset_type),
247 BFD_ENDIAN_LITTLE);
248 }
249
250 /* Return the number of offset_types in this array. */
251 size_t size () const
252 {
253 return m_bytes.size () / sizeof (offset_type);
254 }
255
256 /* Return true if this view is empty. */
257 bool empty () const
258 {
259 return m_bytes.empty ();
260 }
261
262 private:
263 /* The underlying bytes. */
264 gdb::array_view<const gdb_byte> m_bytes;
265 };
266
267 /* A description of the mapped index. The file format is described in
268 a comment by the code that writes the index. */
269 struct mapped_index final : public mapped_index_base
270 {
271 /* Index data format version. */
272 int version = 0;
273
274 /* The address table data. */
275 gdb::array_view<const gdb_byte> address_table;
276
277 /* The symbol table, implemented as a hash table. */
278 offset_view symbol_table;
279
280 /* A pointer to the constant pool. */
281 gdb::array_view<const gdb_byte> constant_pool;
282
283 /* Return the index into the constant pool of the name of the IDXth
284 symbol in the symbol table. */
285 offset_type symbol_name_index (offset_type idx) const
286 {
287 return symbol_table[2 * idx];
288 }
289
290 /* Return the index into the constant pool of the CU vector of the
291 IDXth symbol in the symbol table. */
292 offset_type symbol_vec_index (offset_type idx) const
293 {
294 return symbol_table[2 * idx + 1];
295 }
296
297 bool symbol_name_slot_invalid (offset_type idx) const override
298 {
299 return (symbol_name_index (idx) == 0
300 && symbol_vec_index (idx) == 0);
301 }
302
303 /* Convenience method to get at the name of the symbol at IDX in the
304 symbol table. */
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
307 {
308 return (const char *) (this->constant_pool.data ()
309 + symbol_name_index (idx));
310 }
311
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size () / 2; }
314 };
315
316 /* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
318 struct mapped_debug_names final : public mapped_index_base
319 {
320 bfd_endian dwarf5_byte_order;
321 bool dwarf5_is_dwarf64;
322 bool augmentation_is_gdb;
323 uint8_t offset_size;
324 uint32_t cu_count = 0;
325 uint32_t tu_count, bucket_count, name_count;
326 const gdb_byte *cu_table_reordered, *tu_table_reordered;
327 const uint32_t *bucket_table_reordered, *hash_table_reordered;
328 const gdb_byte *name_table_string_offs_reordered;
329 const gdb_byte *name_table_entry_offs_reordered;
330 const gdb_byte *entry_pool;
331
332 struct index_val
333 {
334 ULONGEST dwarf_tag;
335 struct attr
336 {
337 /* Attribute name DW_IDX_*. */
338 ULONGEST dw_idx;
339
340 /* Attribute form DW_FORM_*. */
341 ULONGEST form;
342
343 /* Value if FORM is DW_FORM_implicit_const. */
344 LONGEST implicit_const;
345 };
346 std::vector<attr> attr_vec;
347 };
348
349 std::unordered_map<ULONGEST, index_val> abbrev_map;
350
351 const char *namei_to_name
352 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
353
354 /* Implementation of the mapped_index_base virtual interface, for
355 the name_components cache. */
356
357 const char *symbol_name_at
358 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
359 { return namei_to_name (idx, per_objfile); }
360
361 size_t symbol_name_count () const override
362 { return this->name_count; }
363 };
364
365 /* See dwarf2read.h. */
366
367 dwarf2_per_objfile *
368 get_dwarf2_per_objfile (struct objfile *objfile)
369 {
370 return dwarf2_objfile_data_key.get (objfile);
371 }
372
373 /* Default names of the debugging sections. */
374
375 /* Note that if the debugging section has been compressed, it might
376 have a name like .zdebug_info. */
377
378 const struct dwarf2_debug_sections dwarf2_elf_names =
379 {
380 { ".debug_info", ".zdebug_info" },
381 { ".debug_abbrev", ".zdebug_abbrev" },
382 { ".debug_line", ".zdebug_line" },
383 { ".debug_loc", ".zdebug_loc" },
384 { ".debug_loclists", ".zdebug_loclists" },
385 { ".debug_macinfo", ".zdebug_macinfo" },
386 { ".debug_macro", ".zdebug_macro" },
387 { ".debug_str", ".zdebug_str" },
388 { ".debug_str_offsets", ".zdebug_str_offsets" },
389 { ".debug_line_str", ".zdebug_line_str" },
390 { ".debug_ranges", ".zdebug_ranges" },
391 { ".debug_rnglists", ".zdebug_rnglists" },
392 { ".debug_types", ".zdebug_types" },
393 { ".debug_addr", ".zdebug_addr" },
394 { ".debug_frame", ".zdebug_frame" },
395 { ".eh_frame", NULL },
396 { ".gdb_index", ".zgdb_index" },
397 { ".debug_names", ".zdebug_names" },
398 { ".debug_aranges", ".zdebug_aranges" },
399 23
400 };
401
402 /* List of DWO/DWP sections. */
403
404 static const struct dwop_section_names
405 {
406 struct dwarf2_section_names abbrev_dwo;
407 struct dwarf2_section_names info_dwo;
408 struct dwarf2_section_names line_dwo;
409 struct dwarf2_section_names loc_dwo;
410 struct dwarf2_section_names loclists_dwo;
411 struct dwarf2_section_names macinfo_dwo;
412 struct dwarf2_section_names macro_dwo;
413 struct dwarf2_section_names rnglists_dwo;
414 struct dwarf2_section_names str_dwo;
415 struct dwarf2_section_names str_offsets_dwo;
416 struct dwarf2_section_names types_dwo;
417 struct dwarf2_section_names cu_index;
418 struct dwarf2_section_names tu_index;
419 }
420 dwop_section_names =
421 {
422 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
423 { ".debug_info.dwo", ".zdebug_info.dwo" },
424 { ".debug_line.dwo", ".zdebug_line.dwo" },
425 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
426 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
427 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
428 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
429 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
430 { ".debug_str.dwo", ".zdebug_str.dwo" },
431 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
432 { ".debug_types.dwo", ".zdebug_types.dwo" },
433 { ".debug_cu_index", ".zdebug_cu_index" },
434 { ".debug_tu_index", ".zdebug_tu_index" },
435 };
436
437 /* local data types */
438
439 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
440 begin with a header, which contains the following information. */
441 struct loclists_rnglists_header
442 {
443 /* A 4-byte or 12-byte length containing the length of the
444 set of entries for this compilation unit, not including the
445 length field itself. */
446 unsigned int length;
447
448 /* A 2-byte version identifier. */
449 short version;
450
451 /* A 1-byte unsigned integer containing the size in bytes of an address on
452 the target system. */
453 unsigned char addr_size;
454
455 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
456 on the target system. */
457 unsigned char segment_collector_size;
458
459 /* A 4-byte count of the number of offsets that follow the header. */
460 unsigned int offset_entry_count;
461 };
462
463 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
464 This includes type_unit_group and quick_file_names. */
465
466 struct stmt_list_hash
467 {
468 /* The DWO unit this table is from or NULL if there is none. */
469 struct dwo_unit *dwo_unit;
470
471 /* Offset in .debug_line or .debug_line.dwo. */
472 sect_offset line_sect_off;
473 };
474
475 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
476 an object of this type. This contains elements of type unit groups
477 that can be shared across objfiles. The non-shareable parts are in
478 type_unit_group_unshareable. */
479
480 struct type_unit_group : public dwarf2_per_cu_data
481 {
482 /* The TUs that share this DW_AT_stmt_list entry.
483 This is added to while parsing type units to build partial symtabs,
484 and is deleted afterwards and not used again. */
485 std::vector<signatured_type *> *tus = nullptr;
486
487 /* The data used to construct the hash key. */
488 struct stmt_list_hash hash {};
489 };
490
491 /* These sections are what may appear in a (real or virtual) DWO file. */
492
493 struct dwo_sections
494 {
495 struct dwarf2_section_info abbrev;
496 struct dwarf2_section_info line;
497 struct dwarf2_section_info loc;
498 struct dwarf2_section_info loclists;
499 struct dwarf2_section_info macinfo;
500 struct dwarf2_section_info macro;
501 struct dwarf2_section_info rnglists;
502 struct dwarf2_section_info str;
503 struct dwarf2_section_info str_offsets;
504 /* In the case of a virtual DWO file, these two are unused. */
505 struct dwarf2_section_info info;
506 std::vector<dwarf2_section_info> types;
507 };
508
509 /* CUs/TUs in DWP/DWO files. */
510
511 struct dwo_unit
512 {
513 /* Backlink to the containing struct dwo_file. */
514 struct dwo_file *dwo_file;
515
516 /* The "id" that distinguishes this CU/TU.
517 .debug_info calls this "dwo_id", .debug_types calls this "signature".
518 Since signatures came first, we stick with it for consistency. */
519 ULONGEST signature;
520
521 /* The section this CU/TU lives in, in the DWO file. */
522 struct dwarf2_section_info *section;
523
524 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
525 sect_offset sect_off;
526 unsigned int length;
527
528 /* For types, offset in the type's DIE of the type defined by this TU. */
529 cu_offset type_offset_in_tu;
530 };
531
532 /* include/dwarf2.h defines the DWP section codes.
533 It defines a max value but it doesn't define a min value, which we
534 use for error checking, so provide one. */
535
536 enum dwp_v2_section_ids
537 {
538 DW_SECT_MIN = 1
539 };
540
541 /* Data for one DWO file.
542
543 This includes virtual DWO files (a virtual DWO file is a DWO file as it
544 appears in a DWP file). DWP files don't really have DWO files per se -
545 comdat folding of types "loses" the DWO file they came from, and from
546 a high level view DWP files appear to contain a mass of random types.
547 However, to maintain consistency with the non-DWP case we pretend DWP
548 files contain virtual DWO files, and we assign each TU with one virtual
549 DWO file (generally based on the line and abbrev section offsets -
550 a heuristic that seems to work in practice). */
551
552 struct dwo_file
553 {
554 dwo_file () = default;
555 DISABLE_COPY_AND_ASSIGN (dwo_file);
556
557 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
558 For virtual DWO files the name is constructed from the section offsets
559 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
560 from related CU+TUs. */
561 const char *dwo_name = nullptr;
562
563 /* The DW_AT_comp_dir attribute. */
564 const char *comp_dir = nullptr;
565
566 /* The bfd, when the file is open. Otherwise this is NULL.
567 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
568 gdb_bfd_ref_ptr dbfd;
569
570 /* The sections that make up this DWO file.
571 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
572 sections (for lack of a better name). */
573 struct dwo_sections sections {};
574
575 /* The CUs in the file.
576 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
577 an extension to handle LLVM's Link Time Optimization output (where
578 multiple source files may be compiled into a single object/dwo pair). */
579 htab_up cus;
580
581 /* Table of TUs in the file.
582 Each element is a struct dwo_unit. */
583 htab_up tus;
584 };
585
586 /* These sections are what may appear in a DWP file. */
587
588 struct dwp_sections
589 {
590 /* These are used by all DWP versions (1, 2 and 5). */
591 struct dwarf2_section_info str;
592 struct dwarf2_section_info cu_index;
593 struct dwarf2_section_info tu_index;
594
595 /* These are only used by DWP version 2 and version 5 files.
596 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
597 sections are referenced by section number, and are not recorded here.
598 In DWP version 2 or 5 there is at most one copy of all these sections,
599 each section being (effectively) comprised of the concatenation of all of
600 the individual sections that exist in the version 1 format.
601 To keep the code simple we treat each of these concatenated pieces as a
602 section itself (a virtual section?). */
603 struct dwarf2_section_info abbrev;
604 struct dwarf2_section_info info;
605 struct dwarf2_section_info line;
606 struct dwarf2_section_info loc;
607 struct dwarf2_section_info loclists;
608 struct dwarf2_section_info macinfo;
609 struct dwarf2_section_info macro;
610 struct dwarf2_section_info rnglists;
611 struct dwarf2_section_info str_offsets;
612 struct dwarf2_section_info types;
613 };
614
615 /* These sections are what may appear in a virtual DWO file in DWP version 1.
616 A virtual DWO file is a DWO file as it appears in a DWP file. */
617
618 struct virtual_v1_dwo_sections
619 {
620 struct dwarf2_section_info abbrev;
621 struct dwarf2_section_info line;
622 struct dwarf2_section_info loc;
623 struct dwarf2_section_info macinfo;
624 struct dwarf2_section_info macro;
625 struct dwarf2_section_info str_offsets;
626 /* Each DWP hash table entry records one CU or one TU.
627 That is recorded here, and copied to dwo_unit.section. */
628 struct dwarf2_section_info info_or_types;
629 };
630
631 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
632 In version 2, the sections of the DWO files are concatenated together
633 and stored in one section of that name. Thus each ELF section contains
634 several "virtual" sections. */
635
636 struct virtual_v2_or_v5_dwo_sections
637 {
638 bfd_size_type abbrev_offset;
639 bfd_size_type abbrev_size;
640
641 bfd_size_type line_offset;
642 bfd_size_type line_size;
643
644 bfd_size_type loc_offset;
645 bfd_size_type loc_size;
646
647 bfd_size_type loclists_offset;
648 bfd_size_type loclists_size;
649
650 bfd_size_type macinfo_offset;
651 bfd_size_type macinfo_size;
652
653 bfd_size_type macro_offset;
654 bfd_size_type macro_size;
655
656 bfd_size_type rnglists_offset;
657 bfd_size_type rnglists_size;
658
659 bfd_size_type str_offsets_offset;
660 bfd_size_type str_offsets_size;
661
662 /* Each DWP hash table entry records one CU or one TU.
663 That is recorded here, and copied to dwo_unit.section. */
664 bfd_size_type info_or_types_offset;
665 bfd_size_type info_or_types_size;
666 };
667
668 /* Contents of DWP hash tables. */
669
670 struct dwp_hash_table
671 {
672 uint32_t version, nr_columns;
673 uint32_t nr_units, nr_slots;
674 const gdb_byte *hash_table, *unit_table;
675 union
676 {
677 struct
678 {
679 const gdb_byte *indices;
680 } v1;
681 struct
682 {
683 /* This is indexed by column number and gives the id of the section
684 in that column. */
685 #define MAX_NR_V2_DWO_SECTIONS \
686 (1 /* .debug_info or .debug_types */ \
687 + 1 /* .debug_abbrev */ \
688 + 1 /* .debug_line */ \
689 + 1 /* .debug_loc */ \
690 + 1 /* .debug_str_offsets */ \
691 + 1 /* .debug_macro or .debug_macinfo */)
692 int section_ids[MAX_NR_V2_DWO_SECTIONS];
693 const gdb_byte *offsets;
694 const gdb_byte *sizes;
695 } v2;
696 struct
697 {
698 /* This is indexed by column number and gives the id of the section
699 in that column. */
700 #define MAX_NR_V5_DWO_SECTIONS \
701 (1 /* .debug_info */ \
702 + 1 /* .debug_abbrev */ \
703 + 1 /* .debug_line */ \
704 + 1 /* .debug_loclists */ \
705 + 1 /* .debug_str_offsets */ \
706 + 1 /* .debug_macro */ \
707 + 1 /* .debug_rnglists */)
708 int section_ids[MAX_NR_V5_DWO_SECTIONS];
709 const gdb_byte *offsets;
710 const gdb_byte *sizes;
711 } v5;
712 } section_pool;
713 };
714
715 /* Data for one DWP file. */
716
717 struct dwp_file
718 {
719 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
720 : name (name_),
721 dbfd (std::move (abfd))
722 {
723 }
724
725 /* Name of the file. */
726 const char *name;
727
728 /* File format version. */
729 int version = 0;
730
731 /* The bfd. */
732 gdb_bfd_ref_ptr dbfd;
733
734 /* Section info for this file. */
735 struct dwp_sections sections {};
736
737 /* Table of CUs in the file. */
738 const struct dwp_hash_table *cus = nullptr;
739
740 /* Table of TUs in the file. */
741 const struct dwp_hash_table *tus = nullptr;
742
743 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
744 htab_up loaded_cus;
745 htab_up loaded_tus;
746
747 /* Table to map ELF section numbers to their sections.
748 This is only needed for the DWP V1 file format. */
749 unsigned int num_sections = 0;
750 asection **elf_sections = nullptr;
751 };
752
753 /* Struct used to pass misc. parameters to read_die_and_children, et
754 al. which are used for both .debug_info and .debug_types dies.
755 All parameters here are unchanging for the life of the call. This
756 struct exists to abstract away the constant parameters of die reading. */
757
758 struct die_reader_specs
759 {
760 /* The bfd of die_section. */
761 bfd *abfd;
762
763 /* The CU of the DIE we are parsing. */
764 struct dwarf2_cu *cu;
765
766 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
767 struct dwo_file *dwo_file;
768
769 /* The section the die comes from.
770 This is either .debug_info or .debug_types, or the .dwo variants. */
771 struct dwarf2_section_info *die_section;
772
773 /* die_section->buffer. */
774 const gdb_byte *buffer;
775
776 /* The end of the buffer. */
777 const gdb_byte *buffer_end;
778
779 /* The abbreviation table to use when reading the DIEs. */
780 struct abbrev_table *abbrev_table;
781 };
782
783 /* A subclass of die_reader_specs that holds storage and has complex
784 constructor and destructor behavior. */
785
786 class cutu_reader : public die_reader_specs
787 {
788 public:
789
790 cutu_reader (dwarf2_per_cu_data *this_cu,
791 dwarf2_per_objfile *per_objfile,
792 struct abbrev_table *abbrev_table,
793 dwarf2_cu *existing_cu,
794 bool skip_partial);
795
796 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
797 dwarf2_per_objfile *per_objfile,
798 struct dwarf2_cu *parent_cu = nullptr,
799 struct dwo_file *dwo_file = nullptr);
800
801 DISABLE_COPY_AND_ASSIGN (cutu_reader);
802
803 const gdb_byte *info_ptr = nullptr;
804 struct die_info *comp_unit_die = nullptr;
805 bool dummy_p = false;
806
807 /* Release the new CU, putting it on the chain. This cannot be done
808 for dummy CUs. */
809 void keep ();
810
811 private:
812 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
813 dwarf2_per_objfile *per_objfile,
814 dwarf2_cu *existing_cu);
815
816 struct dwarf2_per_cu_data *m_this_cu;
817 std::unique_ptr<dwarf2_cu> m_new_cu;
818
819 /* The ordinary abbreviation table. */
820 abbrev_table_up m_abbrev_table_holder;
821
822 /* The DWO abbreviation table. */
823 abbrev_table_up m_dwo_abbrev_table;
824 };
825
826 /* When we construct a partial symbol table entry we only
827 need this much information. */
828 struct partial_die_info : public allocate_on_obstack
829 {
830 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
831
832 /* Disable assign but still keep copy ctor, which is needed
833 load_partial_dies. */
834 partial_die_info& operator=(const partial_die_info& rhs) = delete;
835 partial_die_info (const partial_die_info &) = default;
836
837 /* Adjust the partial die before generating a symbol for it. This
838 function may set the is_external flag or change the DIE's
839 name. */
840 void fixup (struct dwarf2_cu *cu);
841
842 /* Read a minimal amount of information into the minimal die
843 structure. */
844 const gdb_byte *read (const struct die_reader_specs *reader,
845 const struct abbrev_info &abbrev,
846 const gdb_byte *info_ptr);
847
848 /* Compute the name of this partial DIE. This memoizes the
849 result, so it is safe to call multiple times. */
850 const char *name (dwarf2_cu *cu);
851
852 /* Offset of this DIE. */
853 const sect_offset sect_off;
854
855 /* DWARF-2 tag for this DIE. */
856 const ENUM_BITFIELD(dwarf_tag) tag : 16;
857
858 /* Assorted flags describing the data found in this DIE. */
859 const unsigned int has_children : 1;
860
861 unsigned int is_external : 1;
862 unsigned int is_declaration : 1;
863 unsigned int has_type : 1;
864 unsigned int has_specification : 1;
865 unsigned int has_pc_info : 1;
866 unsigned int may_be_inlined : 1;
867
868 /* This DIE has been marked DW_AT_main_subprogram. */
869 unsigned int main_subprogram : 1;
870
871 /* Flag set if the SCOPE field of this structure has been
872 computed. */
873 unsigned int scope_set : 1;
874
875 /* Flag set if the DIE has a byte_size attribute. */
876 unsigned int has_byte_size : 1;
877
878 /* Flag set if the DIE has a DW_AT_const_value attribute. */
879 unsigned int has_const_value : 1;
880
881 /* Flag set if any of the DIE's children are template arguments. */
882 unsigned int has_template_arguments : 1;
883
884 /* Flag set if fixup has been called on this die. */
885 unsigned int fixup_called : 1;
886
887 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
888 unsigned int is_dwz : 1;
889
890 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
891 unsigned int spec_is_dwz : 1;
892
893 unsigned int canonical_name : 1;
894
895 /* The name of this DIE. Normally the value of DW_AT_name, but
896 sometimes a default name for unnamed DIEs. */
897 const char *raw_name = nullptr;
898
899 /* The linkage name, if present. */
900 const char *linkage_name = nullptr;
901
902 /* The scope to prepend to our children. This is generally
903 allocated on the comp_unit_obstack, so will disappear
904 when this compilation unit leaves the cache. */
905 const char *scope = nullptr;
906
907 /* Some data associated with the partial DIE. The tag determines
908 which field is live. */
909 union
910 {
911 /* The location description associated with this DIE, if any. */
912 struct dwarf_block *locdesc;
913 /* The offset of an import, for DW_TAG_imported_unit. */
914 sect_offset sect_off;
915 } d {};
916
917 /* If HAS_PC_INFO, the PC range associated with this DIE. */
918 CORE_ADDR lowpc = 0;
919 CORE_ADDR highpc = 0;
920
921 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
922 DW_AT_sibling, if any. */
923 /* NOTE: This member isn't strictly necessary, partial_die_info::read
924 could return DW_AT_sibling values to its caller load_partial_dies. */
925 const gdb_byte *sibling = nullptr;
926
927 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
928 DW_AT_specification (or DW_AT_abstract_origin or
929 DW_AT_extension). */
930 sect_offset spec_offset {};
931
932 /* Pointers to this DIE's parent, first child, and next sibling,
933 if any. */
934 struct partial_die_info *die_parent = nullptr;
935 struct partial_die_info *die_child = nullptr;
936 struct partial_die_info *die_sibling = nullptr;
937
938 friend struct partial_die_info *
939 dwarf2_cu::find_partial_die (sect_offset sect_off);
940
941 private:
942 /* Only need to do look up in dwarf2_cu::find_partial_die. */
943 partial_die_info (sect_offset sect_off)
944 : partial_die_info (sect_off, DW_TAG_padding, 0)
945 {
946 }
947
948 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
949 int has_children_)
950 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
951 {
952 is_external = 0;
953 is_declaration = 0;
954 has_type = 0;
955 has_specification = 0;
956 has_pc_info = 0;
957 may_be_inlined = 0;
958 main_subprogram = 0;
959 scope_set = 0;
960 has_byte_size = 0;
961 has_const_value = 0;
962 has_template_arguments = 0;
963 fixup_called = 0;
964 is_dwz = 0;
965 spec_is_dwz = 0;
966 canonical_name = 0;
967 }
968 };
969
970 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
971 but this would require a corresponding change in unpack_field_as_long
972 and friends. */
973 static int bits_per_byte = 8;
974
975 struct variant_part_builder;
976
977 /* When reading a variant, we track a bit more information about the
978 field, and store it in an object of this type. */
979
980 struct variant_field
981 {
982 int first_field = -1;
983 int last_field = -1;
984
985 /* A variant can contain other variant parts. */
986 std::vector<variant_part_builder> variant_parts;
987
988 /* If we see a DW_TAG_variant, then this will be set if this is the
989 default branch. */
990 bool default_branch = false;
991 /* If we see a DW_AT_discr_value, then this will be the discriminant
992 value. */
993 ULONGEST discriminant_value = 0;
994 /* If we see a DW_AT_discr_list, then this is a pointer to the list
995 data. */
996 struct dwarf_block *discr_list_data = nullptr;
997 };
998
999 /* This represents a DW_TAG_variant_part. */
1000
1001 struct variant_part_builder
1002 {
1003 /* The offset of the discriminant field. */
1004 sect_offset discriminant_offset {};
1005
1006 /* Variants that are direct children of this variant part. */
1007 std::vector<variant_field> variants;
1008
1009 /* True if we're currently reading a variant. */
1010 bool processing_variant = false;
1011 };
1012
1013 struct nextfield
1014 {
1015 int accessibility = 0;
1016 int virtuality = 0;
1017 /* Variant parts need to find the discriminant, which is a DIE
1018 reference. We track the section offset of each field to make
1019 this link. */
1020 sect_offset offset;
1021 struct field field {};
1022 };
1023
1024 struct fnfieldlist
1025 {
1026 const char *name = nullptr;
1027 std::vector<struct fn_field> fnfields;
1028 };
1029
1030 /* The routines that read and process dies for a C struct or C++ class
1031 pass lists of data member fields and lists of member function fields
1032 in an instance of a field_info structure, as defined below. */
1033 struct field_info
1034 {
1035 /* List of data member and baseclasses fields. */
1036 std::vector<struct nextfield> fields;
1037 std::vector<struct nextfield> baseclasses;
1038
1039 /* Set if the accessibility of one of the fields is not public. */
1040 bool non_public_fields = false;
1041
1042 /* Member function fieldlist array, contains name of possibly overloaded
1043 member function, number of overloaded member functions and a pointer
1044 to the head of the member function field chain. */
1045 std::vector<struct fnfieldlist> fnfieldlists;
1046
1047 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1048 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1049 std::vector<struct decl_field> typedef_field_list;
1050
1051 /* Nested types defined by this class and the number of elements in this
1052 list. */
1053 std::vector<struct decl_field> nested_types_list;
1054
1055 /* If non-null, this is the variant part we are currently
1056 reading. */
1057 variant_part_builder *current_variant_part = nullptr;
1058 /* This holds all the top-level variant parts attached to the type
1059 we're reading. */
1060 std::vector<variant_part_builder> variant_parts;
1061
1062 /* Return the total number of fields (including baseclasses). */
1063 int nfields () const
1064 {
1065 return fields.size () + baseclasses.size ();
1066 }
1067 };
1068
1069 /* Loaded secondary compilation units are kept in memory until they
1070 have not been referenced for the processing of this many
1071 compilation units. Set this to zero to disable caching. Cache
1072 sizes of up to at least twenty will improve startup time for
1073 typical inter-CU-reference binaries, at an obvious memory cost. */
1074 static int dwarf_max_cache_age = 5;
1075 static void
1076 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1077 struct cmd_list_element *c, const char *value)
1078 {
1079 fprintf_filtered (file, _("The upper bound on the age of cached "
1080 "DWARF compilation units is %s.\n"),
1081 value);
1082 }
1083 \f
1084 /* local function prototypes */
1085
1086 static void dwarf2_find_base_address (struct die_info *die,
1087 struct dwarf2_cu *cu);
1088
1089 static dwarf2_psymtab *create_partial_symtab
1090 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1091 const char *name);
1092
1093 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1094 const gdb_byte *info_ptr,
1095 struct die_info *type_unit_die);
1096
1097 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1098
1099 static void scan_partial_symbols (struct partial_die_info *,
1100 CORE_ADDR *, CORE_ADDR *,
1101 int, struct dwarf2_cu *);
1102
1103 static void add_partial_symbol (struct partial_die_info *,
1104 struct dwarf2_cu *);
1105
1106 static void add_partial_namespace (struct partial_die_info *pdi,
1107 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1108 int set_addrmap, struct dwarf2_cu *cu);
1109
1110 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1111 CORE_ADDR *highpc, int set_addrmap,
1112 struct dwarf2_cu *cu);
1113
1114 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1115 struct dwarf2_cu *cu);
1116
1117 static void add_partial_subprogram (struct partial_die_info *pdi,
1118 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1119 int need_pc, struct dwarf2_cu *cu);
1120
1121 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1122
1123 static struct partial_die_info *load_partial_dies
1124 (const struct die_reader_specs *, const gdb_byte *, int);
1125
1126 /* A pair of partial_die_info and compilation unit. */
1127 struct cu_partial_die_info
1128 {
1129 /* The compilation unit of the partial_die_info. */
1130 struct dwarf2_cu *cu;
1131 /* A partial_die_info. */
1132 struct partial_die_info *pdi;
1133
1134 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1135 : cu (cu),
1136 pdi (pdi)
1137 { /* Nothing. */ }
1138
1139 private:
1140 cu_partial_die_info () = delete;
1141 };
1142
1143 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1144 struct dwarf2_cu *);
1145
1146 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1147 struct attribute *,
1148 const struct attr_abbrev *,
1149 const gdb_byte *);
1150
1151 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1152 struct attribute *attr, dwarf_tag tag);
1153
1154 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1155
1156 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1157 dwarf2_section_info *, sect_offset);
1158
1159 static const char *read_indirect_string
1160 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1161 const struct comp_unit_head *, unsigned int *);
1162
1163 static const char *read_indirect_string_at_offset
1164 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1165
1166 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1167 const gdb_byte *,
1168 unsigned int *);
1169
1170 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1171 ULONGEST str_index);
1172
1173 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1174 ULONGEST str_index);
1175
1176 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1177 struct dwarf2_cu *);
1178
1179 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1180 struct dwarf2_cu *cu);
1181
1182 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1183
1184 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1185 struct dwarf2_cu *cu);
1186
1187 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1188
1189 static struct die_info *die_specification (struct die_info *die,
1190 struct dwarf2_cu **);
1191
1192 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1193 struct dwarf2_cu *cu);
1194
1195 static void dwarf_decode_lines (struct line_header *, const char *,
1196 struct dwarf2_cu *, dwarf2_psymtab *,
1197 CORE_ADDR, int decode_mapping);
1198
1199 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1200 const char *);
1201
1202 static struct symbol *new_symbol (struct die_info *, struct type *,
1203 struct dwarf2_cu *, struct symbol * = NULL);
1204
1205 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1206 struct dwarf2_cu *);
1207
1208 static void dwarf2_const_value_attr (const struct attribute *attr,
1209 struct type *type,
1210 const char *name,
1211 struct obstack *obstack,
1212 struct dwarf2_cu *cu, LONGEST *value,
1213 const gdb_byte **bytes,
1214 struct dwarf2_locexpr_baton **baton);
1215
1216 static struct type *read_subrange_index_type (struct die_info *die,
1217 struct dwarf2_cu *cu);
1218
1219 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1220
1221 static int need_gnat_info (struct dwarf2_cu *);
1222
1223 static struct type *die_descriptive_type (struct die_info *,
1224 struct dwarf2_cu *);
1225
1226 static void set_descriptive_type (struct type *, struct die_info *,
1227 struct dwarf2_cu *);
1228
1229 static struct type *die_containing_type (struct die_info *,
1230 struct dwarf2_cu *);
1231
1232 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1233 struct dwarf2_cu *);
1234
1235 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1236
1237 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1238
1239 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1240
1241 static char *typename_concat (struct obstack *obs, const char *prefix,
1242 const char *suffix, int physname,
1243 struct dwarf2_cu *cu);
1244
1245 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1246
1247 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1248
1249 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1250
1251 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1252
1253 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1254
1255 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1256
1257 /* Return the .debug_loclists section to use for cu. */
1258 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1259
1260 /* Return the .debug_rnglists section to use for cu. */
1261 static struct dwarf2_section_info *cu_debug_rnglists_section
1262 (struct dwarf2_cu *cu, dwarf_tag tag);
1263
1264 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1265 values. Keep the items ordered with increasing constraints compliance. */
1266 enum pc_bounds_kind
1267 {
1268 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1269 PC_BOUNDS_NOT_PRESENT,
1270
1271 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1272 were present but they do not form a valid range of PC addresses. */
1273 PC_BOUNDS_INVALID,
1274
1275 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1276 PC_BOUNDS_RANGES,
1277
1278 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1279 PC_BOUNDS_HIGH_LOW,
1280 };
1281
1282 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1283 CORE_ADDR *, CORE_ADDR *,
1284 struct dwarf2_cu *,
1285 dwarf2_psymtab *);
1286
1287 static void get_scope_pc_bounds (struct die_info *,
1288 CORE_ADDR *, CORE_ADDR *,
1289 struct dwarf2_cu *);
1290
1291 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1292 CORE_ADDR, struct dwarf2_cu *);
1293
1294 static void dwarf2_add_field (struct field_info *, struct die_info *,
1295 struct dwarf2_cu *);
1296
1297 static void dwarf2_attach_fields_to_type (struct field_info *,
1298 struct type *, struct dwarf2_cu *);
1299
1300 static void dwarf2_add_member_fn (struct field_info *,
1301 struct die_info *, struct type *,
1302 struct dwarf2_cu *);
1303
1304 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1305 struct type *,
1306 struct dwarf2_cu *);
1307
1308 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1309
1310 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1311
1312 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1313
1314 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1315
1316 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1317
1318 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1319
1320 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1321
1322 static struct type *read_module_type (struct die_info *die,
1323 struct dwarf2_cu *cu);
1324
1325 static const char *namespace_name (struct die_info *die,
1326 int *is_anonymous, struct dwarf2_cu *);
1327
1328 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1329
1330 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1331 bool * = nullptr);
1332
1333 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1334 struct dwarf2_cu *);
1335
1336 static struct die_info *read_die_and_siblings_1
1337 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1338 struct die_info *);
1339
1340 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1341 const gdb_byte *info_ptr,
1342 const gdb_byte **new_info_ptr,
1343 struct die_info *parent);
1344
1345 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1346 struct die_info **, const gdb_byte *,
1347 int);
1348
1349 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1350 struct die_info **, const gdb_byte *);
1351
1352 static void process_die (struct die_info *, struct dwarf2_cu *);
1353
1354 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1355 struct objfile *);
1356
1357 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1358
1359 static const char *dwarf2_full_name (const char *name,
1360 struct die_info *die,
1361 struct dwarf2_cu *cu);
1362
1363 static const char *dwarf2_physname (const char *name, struct die_info *die,
1364 struct dwarf2_cu *cu);
1365
1366 static struct die_info *dwarf2_extension (struct die_info *die,
1367 struct dwarf2_cu **);
1368
1369 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1370
1371 static void dump_die_for_error (struct die_info *);
1372
1373 static void dump_die_1 (struct ui_file *, int level, int max_level,
1374 struct die_info *);
1375
1376 /*static*/ void dump_die (struct die_info *, int max_level);
1377
1378 static void store_in_ref_table (struct die_info *,
1379 struct dwarf2_cu *);
1380
1381 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1382 const struct attribute *,
1383 struct dwarf2_cu **);
1384
1385 static struct die_info *follow_die_ref (struct die_info *,
1386 const struct attribute *,
1387 struct dwarf2_cu **);
1388
1389 static struct die_info *follow_die_sig (struct die_info *,
1390 const struct attribute *,
1391 struct dwarf2_cu **);
1392
1393 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1394 struct dwarf2_cu *);
1395
1396 static struct type *get_DW_AT_signature_type (struct die_info *,
1397 const struct attribute *,
1398 struct dwarf2_cu *);
1399
1400 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1401 dwarf2_per_objfile *per_objfile);
1402
1403 static void read_signatured_type (signatured_type *sig_type,
1404 dwarf2_per_objfile *per_objfile);
1405
1406 static int attr_to_dynamic_prop (const struct attribute *attr,
1407 struct die_info *die, struct dwarf2_cu *cu,
1408 struct dynamic_prop *prop, struct type *type);
1409
1410 /* memory allocation interface */
1411
1412 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1413
1414 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1415
1416 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1417
1418 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1419 struct dwarf2_loclist_baton *baton,
1420 const struct attribute *attr);
1421
1422 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1423 struct symbol *sym,
1424 struct dwarf2_cu *cu,
1425 int is_block);
1426
1427 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1428 const gdb_byte *info_ptr,
1429 const struct abbrev_info *abbrev);
1430
1431 static hashval_t partial_die_hash (const void *item);
1432
1433 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1434
1435 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1436 (sect_offset sect_off, unsigned int offset_in_dwz,
1437 dwarf2_per_objfile *per_objfile);
1438
1439 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1440 struct die_info *comp_unit_die,
1441 enum language pretend_language);
1442
1443 static struct type *set_die_type (struct die_info *, struct type *,
1444 struct dwarf2_cu *, bool = false);
1445
1446 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1447
1448 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1449 dwarf2_per_objfile *per_objfile,
1450 dwarf2_cu *existing_cu,
1451 bool skip_partial,
1452 enum language pretend_language);
1453
1454 static void process_full_comp_unit (dwarf2_cu *cu,
1455 enum language pretend_language);
1456
1457 static void process_full_type_unit (dwarf2_cu *cu,
1458 enum language pretend_language);
1459
1460 static struct type *get_die_type_at_offset (sect_offset,
1461 dwarf2_per_cu_data *per_cu,
1462 dwarf2_per_objfile *per_objfile);
1463
1464 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1465
1466 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1467 dwarf2_per_objfile *per_objfile,
1468 enum language pretend_language);
1469
1470 static void process_queue (dwarf2_per_objfile *per_objfile);
1471
1472 /* Class, the destructor of which frees all allocated queue entries. This
1473 will only have work to do if an error was thrown while processing the
1474 dwarf. If no error was thrown then the queue entries should have all
1475 been processed, and freed, as we went along. */
1476
1477 class dwarf2_queue_guard
1478 {
1479 public:
1480 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1481 : m_per_objfile (per_objfile)
1482 {
1483 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1484
1485 m_per_objfile->per_bfd->queue.emplace ();
1486 }
1487
1488 /* Free any entries remaining on the queue. There should only be
1489 entries left if we hit an error while processing the dwarf. */
1490 ~dwarf2_queue_guard ()
1491 {
1492 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1493
1494 m_per_objfile->per_bfd->queue.reset ();
1495 }
1496
1497 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1498
1499 private:
1500 dwarf2_per_objfile *m_per_objfile;
1501 };
1502
1503 dwarf2_queue_item::~dwarf2_queue_item ()
1504 {
1505 /* Anything still marked queued is likely to be in an
1506 inconsistent state, so discard it. */
1507 if (per_cu->queued)
1508 {
1509 per_objfile->remove_cu (per_cu);
1510 per_cu->queued = 0;
1511 }
1512 }
1513
1514 /* See dwarf2/read.h. */
1515
1516 void
1517 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1518 {
1519 if (data->is_debug_types)
1520 delete static_cast<signatured_type *> (data);
1521 else
1522 delete data;
1523 }
1524
1525 /* The return type of find_file_and_directory. Note, the enclosed
1526 string pointers are only valid while this object is valid. */
1527
1528 struct file_and_directory
1529 {
1530 /* The filename. This is never NULL. */
1531 const char *name;
1532
1533 /* The compilation directory. NULL if not known. If we needed to
1534 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1535 points directly to the DW_AT_comp_dir string attribute owned by
1536 the obstack that owns the DIE. */
1537 const char *comp_dir;
1538
1539 /* If we needed to build a new string for comp_dir, this is what
1540 owns the storage. */
1541 std::string comp_dir_storage;
1542 };
1543
1544 static file_and_directory find_file_and_directory (struct die_info *die,
1545 struct dwarf2_cu *cu);
1546
1547 static htab_up allocate_signatured_type_table ();
1548
1549 static htab_up allocate_dwo_unit_table ();
1550
1551 static struct dwo_unit *lookup_dwo_unit_in_dwp
1552 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1553 const char *comp_dir, ULONGEST signature, int is_debug_types);
1554
1555 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1556
1557 static struct dwo_unit *lookup_dwo_comp_unit
1558 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1559 ULONGEST signature);
1560
1561 static struct dwo_unit *lookup_dwo_type_unit
1562 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1563
1564 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1565
1566 /* A unique pointer to a dwo_file. */
1567
1568 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1569
1570 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1571
1572 static void check_producer (struct dwarf2_cu *cu);
1573 \f
1574 /* Various complaints about symbol reading that don't abort the process. */
1575
1576 static void
1577 dwarf2_debug_line_missing_file_complaint (void)
1578 {
1579 complaint (_(".debug_line section has line data without a file"));
1580 }
1581
1582 static void
1583 dwarf2_debug_line_missing_end_sequence_complaint (void)
1584 {
1585 complaint (_(".debug_line section has line "
1586 "program sequence without an end"));
1587 }
1588
1589 static void
1590 dwarf2_complex_location_expr_complaint (void)
1591 {
1592 complaint (_("location expression too complex"));
1593 }
1594
1595 static void
1596 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1597 int arg3)
1598 {
1599 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1600 arg1, arg2, arg3);
1601 }
1602
1603 static void
1604 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1605 {
1606 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1607 arg1, arg2);
1608 }
1609
1610 /* Hash function for line_header_hash. */
1611
1612 static hashval_t
1613 line_header_hash (const struct line_header *ofs)
1614 {
1615 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1616 }
1617
1618 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1619
1620 static hashval_t
1621 line_header_hash_voidp (const void *item)
1622 {
1623 const struct line_header *ofs = (const struct line_header *) item;
1624
1625 return line_header_hash (ofs);
1626 }
1627
1628 /* Equality function for line_header_hash. */
1629
1630 static int
1631 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1632 {
1633 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1634 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1635
1636 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1637 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1638 }
1639
1640 \f
1641
1642 /* See declaration. */
1643
1644 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1645 bool can_copy_)
1646 : obfd (obfd),
1647 can_copy (can_copy_)
1648 {
1649 if (names == NULL)
1650 names = &dwarf2_elf_names;
1651
1652 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1653 locate_sections (obfd, sec, *names);
1654 }
1655
1656 dwarf2_per_bfd::~dwarf2_per_bfd ()
1657 {
1658 for (auto &per_cu : all_comp_units)
1659 per_cu->imported_symtabs_free ();
1660
1661 /* Everything else should be on this->obstack. */
1662 }
1663
1664 /* See read.h. */
1665
1666 void
1667 dwarf2_per_objfile::remove_all_cus ()
1668 {
1669 gdb_assert (!this->per_bfd->queue.has_value ());
1670
1671 for (auto pair : m_dwarf2_cus)
1672 delete pair.second;
1673
1674 m_dwarf2_cus.clear ();
1675 }
1676
1677 /* A helper class that calls free_cached_comp_units on
1678 destruction. */
1679
1680 class free_cached_comp_units
1681 {
1682 public:
1683
1684 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1685 : m_per_objfile (per_objfile)
1686 {
1687 }
1688
1689 ~free_cached_comp_units ()
1690 {
1691 m_per_objfile->remove_all_cus ();
1692 }
1693
1694 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1695
1696 private:
1697
1698 dwarf2_per_objfile *m_per_objfile;
1699 };
1700
1701 /* See read.h. */
1702
1703 bool
1704 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1705 {
1706 gdb_assert (per_cu->index < this->m_symtabs.size ());
1707
1708 return this->m_symtabs[per_cu->index] != nullptr;
1709 }
1710
1711 /* See read.h. */
1712
1713 compunit_symtab *
1714 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1715 {
1716 gdb_assert (per_cu->index < this->m_symtabs.size ());
1717
1718 return this->m_symtabs[per_cu->index];
1719 }
1720
1721 /* See read.h. */
1722
1723 void
1724 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1725 compunit_symtab *symtab)
1726 {
1727 gdb_assert (per_cu->index < this->m_symtabs.size ());
1728 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1729
1730 this->m_symtabs[per_cu->index] = symtab;
1731 }
1732
1733 /* Try to locate the sections we need for DWARF 2 debugging
1734 information and return true if we have enough to do something.
1735 NAMES points to the dwarf2 section names, or is NULL if the standard
1736 ELF names are used. CAN_COPY is true for formats where symbol
1737 interposition is possible and so symbol values must follow copy
1738 relocation rules. */
1739
1740 int
1741 dwarf2_has_info (struct objfile *objfile,
1742 const struct dwarf2_debug_sections *names,
1743 bool can_copy)
1744 {
1745 if (objfile->flags & OBJF_READNEVER)
1746 return 0;
1747
1748 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1749
1750 if (per_objfile == NULL)
1751 {
1752 dwarf2_per_bfd *per_bfd;
1753
1754 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1755 BFD doesn't require relocations.
1756
1757 We don't share with objfiles for which -readnow was requested,
1758 because it would complicate things when loading the same BFD with
1759 -readnow and then without -readnow. */
1760 if (!gdb_bfd_requires_relocations (objfile->obfd)
1761 && (objfile->flags & OBJF_READNOW) == 0)
1762 {
1763 /* See if one has been created for this BFD yet. */
1764 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1765
1766 if (per_bfd == nullptr)
1767 {
1768 /* No, create it now. */
1769 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1770 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1771 }
1772 }
1773 else
1774 {
1775 /* No sharing possible, create one specifically for this objfile. */
1776 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1777 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1778 }
1779
1780 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1781 }
1782
1783 return (!per_objfile->per_bfd->info.is_virtual
1784 && per_objfile->per_bfd->info.s.section != NULL
1785 && !per_objfile->per_bfd->abbrev.is_virtual
1786 && per_objfile->per_bfd->abbrev.s.section != NULL);
1787 }
1788
1789 /* See declaration. */
1790
1791 void
1792 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1793 const dwarf2_debug_sections &names)
1794 {
1795 flagword aflag = bfd_section_flags (sectp);
1796
1797 if ((aflag & SEC_HAS_CONTENTS) == 0)
1798 {
1799 }
1800 else if (elf_section_data (sectp)->this_hdr.sh_size
1801 > bfd_get_file_size (abfd))
1802 {
1803 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1804 warning (_("Discarding section %s which has a section size (%s"
1805 ") larger than the file size [in module %s]"),
1806 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1807 bfd_get_filename (abfd));
1808 }
1809 else if (names.info.matches (sectp->name))
1810 {
1811 this->info.s.section = sectp;
1812 this->info.size = bfd_section_size (sectp);
1813 }
1814 else if (names.abbrev.matches (sectp->name))
1815 {
1816 this->abbrev.s.section = sectp;
1817 this->abbrev.size = bfd_section_size (sectp);
1818 }
1819 else if (names.line.matches (sectp->name))
1820 {
1821 this->line.s.section = sectp;
1822 this->line.size = bfd_section_size (sectp);
1823 }
1824 else if (names.loc.matches (sectp->name))
1825 {
1826 this->loc.s.section = sectp;
1827 this->loc.size = bfd_section_size (sectp);
1828 }
1829 else if (names.loclists.matches (sectp->name))
1830 {
1831 this->loclists.s.section = sectp;
1832 this->loclists.size = bfd_section_size (sectp);
1833 }
1834 else if (names.macinfo.matches (sectp->name))
1835 {
1836 this->macinfo.s.section = sectp;
1837 this->macinfo.size = bfd_section_size (sectp);
1838 }
1839 else if (names.macro.matches (sectp->name))
1840 {
1841 this->macro.s.section = sectp;
1842 this->macro.size = bfd_section_size (sectp);
1843 }
1844 else if (names.str.matches (sectp->name))
1845 {
1846 this->str.s.section = sectp;
1847 this->str.size = bfd_section_size (sectp);
1848 }
1849 else if (names.str_offsets.matches (sectp->name))
1850 {
1851 this->str_offsets.s.section = sectp;
1852 this->str_offsets.size = bfd_section_size (sectp);
1853 }
1854 else if (names.line_str.matches (sectp->name))
1855 {
1856 this->line_str.s.section = sectp;
1857 this->line_str.size = bfd_section_size (sectp);
1858 }
1859 else if (names.addr.matches (sectp->name))
1860 {
1861 this->addr.s.section = sectp;
1862 this->addr.size = bfd_section_size (sectp);
1863 }
1864 else if (names.frame.matches (sectp->name))
1865 {
1866 this->frame.s.section = sectp;
1867 this->frame.size = bfd_section_size (sectp);
1868 }
1869 else if (names.eh_frame.matches (sectp->name))
1870 {
1871 this->eh_frame.s.section = sectp;
1872 this->eh_frame.size = bfd_section_size (sectp);
1873 }
1874 else if (names.ranges.matches (sectp->name))
1875 {
1876 this->ranges.s.section = sectp;
1877 this->ranges.size = bfd_section_size (sectp);
1878 }
1879 else if (names.rnglists.matches (sectp->name))
1880 {
1881 this->rnglists.s.section = sectp;
1882 this->rnglists.size = bfd_section_size (sectp);
1883 }
1884 else if (names.types.matches (sectp->name))
1885 {
1886 struct dwarf2_section_info type_section;
1887
1888 memset (&type_section, 0, sizeof (type_section));
1889 type_section.s.section = sectp;
1890 type_section.size = bfd_section_size (sectp);
1891
1892 this->types.push_back (type_section);
1893 }
1894 else if (names.gdb_index.matches (sectp->name))
1895 {
1896 this->gdb_index.s.section = sectp;
1897 this->gdb_index.size = bfd_section_size (sectp);
1898 }
1899 else if (names.debug_names.matches (sectp->name))
1900 {
1901 this->debug_names.s.section = sectp;
1902 this->debug_names.size = bfd_section_size (sectp);
1903 }
1904 else if (names.debug_aranges.matches (sectp->name))
1905 {
1906 this->debug_aranges.s.section = sectp;
1907 this->debug_aranges.size = bfd_section_size (sectp);
1908 }
1909
1910 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1911 && bfd_section_vma (sectp) == 0)
1912 this->has_section_at_zero = true;
1913 }
1914
1915 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1916 SECTION_NAME. */
1917
1918 void
1919 dwarf2_get_section_info (struct objfile *objfile,
1920 enum dwarf2_section_enum sect,
1921 asection **sectp, const gdb_byte **bufp,
1922 bfd_size_type *sizep)
1923 {
1924 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1925 struct dwarf2_section_info *info;
1926
1927 /* We may see an objfile without any DWARF, in which case we just
1928 return nothing. */
1929 if (per_objfile == NULL)
1930 {
1931 *sectp = NULL;
1932 *bufp = NULL;
1933 *sizep = 0;
1934 return;
1935 }
1936 switch (sect)
1937 {
1938 case DWARF2_DEBUG_FRAME:
1939 info = &per_objfile->per_bfd->frame;
1940 break;
1941 case DWARF2_EH_FRAME:
1942 info = &per_objfile->per_bfd->eh_frame;
1943 break;
1944 default:
1945 gdb_assert_not_reached ("unexpected section");
1946 }
1947
1948 info->read (objfile);
1949
1950 *sectp = info->get_bfd_section ();
1951 *bufp = info->buffer;
1952 *sizep = info->size;
1953 }
1954
1955 \f
1956 /* DWARF quick_symbol_functions support. */
1957
1958 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1959 unique line tables, so we maintain a separate table of all .debug_line
1960 derived entries to support the sharing.
1961 All the quick functions need is the list of file names. We discard the
1962 line_header when we're done and don't need to record it here. */
1963 struct quick_file_names
1964 {
1965 /* The data used to construct the hash key. */
1966 struct stmt_list_hash hash;
1967
1968 /* The number of entries in file_names, real_names. */
1969 unsigned int num_file_names;
1970
1971 /* The file names from the line table, after being run through
1972 file_full_name. */
1973 const char **file_names;
1974
1975 /* The file names from the line table after being run through
1976 gdb_realpath. These are computed lazily. */
1977 const char **real_names;
1978 };
1979
1980 /* When using the index (and thus not using psymtabs), each CU has an
1981 object of this type. This is used to hold information needed by
1982 the various "quick" methods. */
1983 struct dwarf2_per_cu_quick_data
1984 {
1985 /* The file table. This can be NULL if there was no file table
1986 or it's currently not read in.
1987 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
1988 struct quick_file_names *file_names;
1989
1990 /* A temporary mark bit used when iterating over all CUs in
1991 expand_symtabs_matching. */
1992 unsigned int mark : 1;
1993
1994 /* True if we've tried to read the file table and found there isn't one.
1995 There will be no point in trying to read it again next time. */
1996 unsigned int no_file_data : 1;
1997 };
1998
1999 /* A subclass of psymbol_functions that arranges to read the DWARF
2000 partial symbols when needed. */
2001 struct lazy_dwarf_reader : public psymbol_functions
2002 {
2003 using psymbol_functions::psymbol_functions;
2004
2005 bool can_lazily_read_symbols () override
2006 {
2007 return true;
2008 }
2009
2010 void read_partial_symbols (struct objfile *objfile) override
2011 {
2012 if (dwarf2_has_info (objfile, nullptr))
2013 dwarf2_build_psymtabs (objfile, this);
2014 }
2015 };
2016
2017 static quick_symbol_functions_up
2018 make_lazy_dwarf_reader ()
2019 {
2020 return quick_symbol_functions_up (new lazy_dwarf_reader);
2021 }
2022
2023 struct dwarf2_base_index_functions : public quick_symbol_functions
2024 {
2025 bool has_symbols (struct objfile *objfile) override;
2026
2027 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2028
2029 void forget_cached_source_info (struct objfile *objfile) override;
2030
2031 enum language lookup_global_symbol_language (struct objfile *objfile,
2032 const char *name,
2033 domain_enum domain,
2034 bool *symbol_found_p) override
2035 {
2036 *symbol_found_p = false;
2037 return language_unknown;
2038 }
2039
2040 void print_stats (struct objfile *objfile, bool print_bcache) override;
2041
2042 void expand_all_symtabs (struct objfile *objfile) override;
2043
2044 struct compunit_symtab *find_pc_sect_compunit_symtab
2045 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2046 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2047
2048 struct compunit_symtab *find_compunit_symtab_by_address
2049 (struct objfile *objfile, CORE_ADDR address) override
2050 {
2051 return nullptr;
2052 }
2053
2054 void map_symbol_filenames (struct objfile *objfile,
2055 gdb::function_view<symbol_filename_ftype> fun,
2056 bool need_fullname) override;
2057 };
2058
2059 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2060 {
2061 void dump (struct objfile *objfile) override;
2062
2063 void expand_matching_symbols
2064 (struct objfile *,
2065 const lookup_name_info &lookup_name,
2066 domain_enum domain,
2067 int global,
2068 symbol_compare_ftype *ordered_compare) override;
2069
2070 bool expand_symtabs_matching
2071 (struct objfile *objfile,
2072 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2073 const lookup_name_info *lookup_name,
2074 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2075 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2076 block_search_flags search_flags,
2077 domain_enum domain,
2078 enum search_domain kind) override;
2079 };
2080
2081 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2082 {
2083 void dump (struct objfile *objfile) override;
2084
2085 void expand_matching_symbols
2086 (struct objfile *,
2087 const lookup_name_info &lookup_name,
2088 domain_enum domain,
2089 int global,
2090 symbol_compare_ftype *ordered_compare) override;
2091
2092 bool expand_symtabs_matching
2093 (struct objfile *objfile,
2094 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2095 const lookup_name_info *lookup_name,
2096 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2097 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2098 block_search_flags search_flags,
2099 domain_enum domain,
2100 enum search_domain kind) override;
2101 };
2102
2103 static quick_symbol_functions_up
2104 make_dwarf_gdb_index ()
2105 {
2106 return quick_symbol_functions_up (new dwarf2_gdb_index);
2107 }
2108
2109 static quick_symbol_functions_up
2110 make_dwarf_debug_names ()
2111 {
2112 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2113 }
2114
2115 /* Utility hash function for a stmt_list_hash. */
2116
2117 static hashval_t
2118 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2119 {
2120 hashval_t v = 0;
2121
2122 if (stmt_list_hash->dwo_unit != NULL)
2123 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2124 v += to_underlying (stmt_list_hash->line_sect_off);
2125 return v;
2126 }
2127
2128 /* Utility equality function for a stmt_list_hash. */
2129
2130 static int
2131 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2132 const struct stmt_list_hash *rhs)
2133 {
2134 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2135 return 0;
2136 if (lhs->dwo_unit != NULL
2137 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2138 return 0;
2139
2140 return lhs->line_sect_off == rhs->line_sect_off;
2141 }
2142
2143 /* Hash function for a quick_file_names. */
2144
2145 static hashval_t
2146 hash_file_name_entry (const void *e)
2147 {
2148 const struct quick_file_names *file_data
2149 = (const struct quick_file_names *) e;
2150
2151 return hash_stmt_list_entry (&file_data->hash);
2152 }
2153
2154 /* Equality function for a quick_file_names. */
2155
2156 static int
2157 eq_file_name_entry (const void *a, const void *b)
2158 {
2159 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2160 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2161
2162 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2163 }
2164
2165 /* Delete function for a quick_file_names. */
2166
2167 static void
2168 delete_file_name_entry (void *e)
2169 {
2170 struct quick_file_names *file_data = (struct quick_file_names *) e;
2171 int i;
2172
2173 for (i = 0; i < file_data->num_file_names; ++i)
2174 {
2175 xfree ((void*) file_data->file_names[i]);
2176 if (file_data->real_names)
2177 xfree ((void*) file_data->real_names[i]);
2178 }
2179
2180 /* The space for the struct itself lives on the obstack, so we don't
2181 free it here. */
2182 }
2183
2184 /* Create a quick_file_names hash table. */
2185
2186 static htab_up
2187 create_quick_file_names_table (unsigned int nr_initial_entries)
2188 {
2189 return htab_up (htab_create_alloc (nr_initial_entries,
2190 hash_file_name_entry, eq_file_name_entry,
2191 delete_file_name_entry, xcalloc, xfree));
2192 }
2193
2194 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2195 function is unrelated to symtabs, symtab would have to be created afterwards.
2196 You should call age_cached_comp_units after processing the CU. */
2197
2198 static dwarf2_cu *
2199 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2200 bool skip_partial)
2201 {
2202 if (per_cu->is_debug_types)
2203 load_full_type_unit (per_cu, per_objfile);
2204 else
2205 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2206 skip_partial, language_minimal);
2207
2208 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2209 if (cu == nullptr)
2210 return nullptr; /* Dummy CU. */
2211
2212 dwarf2_find_base_address (cu->dies, cu);
2213
2214 return cu;
2215 }
2216
2217 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2218
2219 static void
2220 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2221 dwarf2_per_objfile *per_objfile, bool skip_partial)
2222 {
2223 /* Skip type_unit_groups, reading the type units they contain
2224 is handled elsewhere. */
2225 if (per_cu->type_unit_group_p ())
2226 return;
2227
2228 {
2229 /* The destructor of dwarf2_queue_guard frees any entries left on
2230 the queue. After this point we're guaranteed to leave this function
2231 with the dwarf queue empty. */
2232 dwarf2_queue_guard q_guard (per_objfile);
2233
2234 if (!per_objfile->symtab_set_p (per_cu))
2235 {
2236 queue_comp_unit (per_cu, per_objfile, language_minimal);
2237 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2238
2239 /* If we just loaded a CU from a DWO, and we're working with an index
2240 that may badly handle TUs, load all the TUs in that DWO as well.
2241 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2242 if (!per_cu->is_debug_types
2243 && cu != NULL
2244 && cu->dwo_unit != NULL
2245 && per_objfile->per_bfd->index_table != NULL
2246 && per_objfile->per_bfd->index_table->version <= 7
2247 /* DWP files aren't supported yet. */
2248 && get_dwp_file (per_objfile) == NULL)
2249 queue_and_load_all_dwo_tus (cu);
2250 }
2251
2252 process_queue (per_objfile);
2253 }
2254
2255 /* Age the cache, releasing compilation units that have not
2256 been used recently. */
2257 per_objfile->age_comp_units ();
2258 }
2259
2260 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2261 the per-objfile for which this symtab is instantiated.
2262
2263 Returns the resulting symbol table. */
2264
2265 static struct compunit_symtab *
2266 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2267 dwarf2_per_objfile *per_objfile,
2268 bool skip_partial)
2269 {
2270 gdb_assert (per_objfile->per_bfd->using_index);
2271
2272 if (!per_objfile->symtab_set_p (per_cu))
2273 {
2274 free_cached_comp_units freer (per_objfile);
2275 scoped_restore decrementer = increment_reading_symtab ();
2276 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2277 process_cu_includes (per_objfile);
2278 }
2279
2280 return per_objfile->get_symtab (per_cu);
2281 }
2282
2283 /* See read.h. */
2284
2285 dwarf2_per_cu_data_up
2286 dwarf2_per_bfd::allocate_per_cu ()
2287 {
2288 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2289 result->per_bfd = this;
2290 result->index = all_comp_units.size ();
2291 return result;
2292 }
2293
2294 /* See read.h. */
2295
2296 signatured_type_up
2297 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2298 {
2299 signatured_type_up result (new signatured_type (signature));
2300 result->per_bfd = this;
2301 result->index = all_comp_units.size ();
2302 result->is_debug_types = true;
2303 tu_stats.nr_tus++;
2304 return result;
2305 }
2306
2307 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2308 obstack, and constructed with the specified field values. */
2309
2310 static dwarf2_per_cu_data_up
2311 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2312 struct dwarf2_section_info *section,
2313 int is_dwz,
2314 sect_offset sect_off, ULONGEST length)
2315 {
2316 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2317 the_cu->sect_off = sect_off;
2318 the_cu->length = length;
2319 the_cu->section = section;
2320 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2321 struct dwarf2_per_cu_quick_data);
2322 the_cu->is_dwz = is_dwz;
2323 return the_cu;
2324 }
2325
2326 /* A helper for create_cus_from_index that handles a given list of
2327 CUs. */
2328
2329 static void
2330 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2331 const gdb_byte *cu_list, offset_type n_elements,
2332 struct dwarf2_section_info *section,
2333 int is_dwz)
2334 {
2335 for (offset_type i = 0; i < n_elements; i += 2)
2336 {
2337 gdb_static_assert (sizeof (ULONGEST) >= 8);
2338
2339 sect_offset sect_off
2340 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2341 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2342 cu_list += 2 * 8;
2343
2344 dwarf2_per_cu_data_up per_cu
2345 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2346 length);
2347 per_bfd->all_comp_units.push_back (std::move (per_cu));
2348 }
2349 }
2350
2351 /* Read the CU list from the mapped index, and use it to create all
2352 the CU objects for PER_BFD. */
2353
2354 static void
2355 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2356 const gdb_byte *cu_list, offset_type cu_list_elements,
2357 const gdb_byte *dwz_list, offset_type dwz_elements)
2358 {
2359 gdb_assert (per_bfd->all_comp_units.empty ());
2360 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2361
2362 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2363 &per_bfd->info, 0);
2364
2365 if (dwz_elements == 0)
2366 return;
2367
2368 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2369 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2370 &dwz->info, 1);
2371 }
2372
2373 /* Create the signatured type hash table from the index. */
2374
2375 static void
2376 create_signatured_type_table_from_index
2377 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2378 const gdb_byte *bytes, offset_type elements)
2379 {
2380 htab_up sig_types_hash = allocate_signatured_type_table ();
2381
2382 for (offset_type i = 0; i < elements; i += 3)
2383 {
2384 signatured_type_up sig_type;
2385 ULONGEST signature;
2386 void **slot;
2387 cu_offset type_offset_in_tu;
2388
2389 gdb_static_assert (sizeof (ULONGEST) >= 8);
2390 sect_offset sect_off
2391 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2392 type_offset_in_tu
2393 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2394 BFD_ENDIAN_LITTLE);
2395 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2396 bytes += 3 * 8;
2397
2398 sig_type = per_bfd->allocate_signatured_type (signature);
2399 sig_type->type_offset_in_tu = type_offset_in_tu;
2400 sig_type->section = section;
2401 sig_type->sect_off = sect_off;
2402 sig_type->v.quick
2403 = OBSTACK_ZALLOC (&per_bfd->obstack,
2404 struct dwarf2_per_cu_quick_data);
2405
2406 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2407 *slot = sig_type.get ();
2408
2409 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2410 }
2411
2412 per_bfd->signatured_types = std::move (sig_types_hash);
2413 }
2414
2415 /* Create the signatured type hash table from .debug_names. */
2416
2417 static void
2418 create_signatured_type_table_from_debug_names
2419 (dwarf2_per_objfile *per_objfile,
2420 const mapped_debug_names &map,
2421 struct dwarf2_section_info *section,
2422 struct dwarf2_section_info *abbrev_section)
2423 {
2424 struct objfile *objfile = per_objfile->objfile;
2425
2426 section->read (objfile);
2427 abbrev_section->read (objfile);
2428
2429 htab_up sig_types_hash = allocate_signatured_type_table ();
2430
2431 for (uint32_t i = 0; i < map.tu_count; ++i)
2432 {
2433 signatured_type_up sig_type;
2434 void **slot;
2435
2436 sect_offset sect_off
2437 = (sect_offset) (extract_unsigned_integer
2438 (map.tu_table_reordered + i * map.offset_size,
2439 map.offset_size,
2440 map.dwarf5_byte_order));
2441
2442 comp_unit_head cu_header;
2443 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2444 abbrev_section,
2445 section->buffer + to_underlying (sect_off),
2446 rcuh_kind::TYPE);
2447
2448 sig_type = per_objfile->per_bfd->allocate_signatured_type
2449 (cu_header.signature);
2450 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2451 sig_type->section = section;
2452 sig_type->sect_off = sect_off;
2453 sig_type->v.quick
2454 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2455 struct dwarf2_per_cu_quick_data);
2456
2457 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2458 *slot = sig_type.get ();
2459
2460 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2461 }
2462
2463 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2464 }
2465
2466 /* Read the address map data from the mapped index, and use it to
2467 populate the psymtabs_addrmap. */
2468
2469 static void
2470 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2471 struct mapped_index *index)
2472 {
2473 struct objfile *objfile = per_objfile->objfile;
2474 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2475 struct gdbarch *gdbarch = objfile->arch ();
2476 const gdb_byte *iter, *end;
2477 struct addrmap *mutable_map;
2478 CORE_ADDR baseaddr;
2479
2480 auto_obstack temp_obstack;
2481
2482 mutable_map = addrmap_create_mutable (&temp_obstack);
2483
2484 iter = index->address_table.data ();
2485 end = iter + index->address_table.size ();
2486
2487 baseaddr = objfile->text_section_offset ();
2488
2489 while (iter < end)
2490 {
2491 ULONGEST hi, lo, cu_index;
2492 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2493 iter += 8;
2494 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2495 iter += 8;
2496 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2497 iter += 4;
2498
2499 if (lo > hi)
2500 {
2501 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2502 hex_string (lo), hex_string (hi));
2503 continue;
2504 }
2505
2506 if (cu_index >= per_bfd->all_comp_units.size ())
2507 {
2508 complaint (_(".gdb_index address table has invalid CU number %u"),
2509 (unsigned) cu_index);
2510 continue;
2511 }
2512
2513 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2514 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2515 addrmap_set_empty (mutable_map, lo, hi - 1,
2516 per_bfd->get_cu (cu_index));
2517 }
2518
2519 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2520 &per_bfd->obstack);
2521 }
2522
2523 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2524 populate the psymtabs_addrmap. */
2525
2526 static void
2527 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2528 struct dwarf2_section_info *section)
2529 {
2530 struct objfile *objfile = per_objfile->objfile;
2531 bfd *abfd = objfile->obfd;
2532 struct gdbarch *gdbarch = objfile->arch ();
2533 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2534 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2535
2536 auto_obstack temp_obstack;
2537 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2538
2539 std::unordered_map<sect_offset,
2540 dwarf2_per_cu_data *,
2541 gdb::hash_enum<sect_offset>>
2542 debug_info_offset_to_per_cu;
2543 for (const auto &per_cu : per_bfd->all_comp_units)
2544 {
2545 const auto insertpair
2546 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2547 per_cu.get ());
2548 if (!insertpair.second)
2549 {
2550 warning (_("Section .debug_aranges in %s has duplicate "
2551 "debug_info_offset %s, ignoring .debug_aranges."),
2552 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2553 return;
2554 }
2555 }
2556
2557 section->read (objfile);
2558
2559 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2560
2561 const gdb_byte *addr = section->buffer;
2562
2563 while (addr < section->buffer + section->size)
2564 {
2565 const gdb_byte *const entry_addr = addr;
2566 unsigned int bytes_read;
2567
2568 const LONGEST entry_length = read_initial_length (abfd, addr,
2569 &bytes_read);
2570 addr += bytes_read;
2571
2572 const gdb_byte *const entry_end = addr + entry_length;
2573 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2574 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2575 if (addr + entry_length > section->buffer + section->size)
2576 {
2577 warning (_("Section .debug_aranges in %s entry at offset %s "
2578 "length %s exceeds section length %s, "
2579 "ignoring .debug_aranges."),
2580 objfile_name (objfile),
2581 plongest (entry_addr - section->buffer),
2582 plongest (bytes_read + entry_length),
2583 pulongest (section->size));
2584 return;
2585 }
2586
2587 /* The version number. */
2588 const uint16_t version = read_2_bytes (abfd, addr);
2589 addr += 2;
2590 if (version != 2)
2591 {
2592 warning (_("Section .debug_aranges in %s entry at offset %s "
2593 "has unsupported version %d, ignoring .debug_aranges."),
2594 objfile_name (objfile),
2595 plongest (entry_addr - section->buffer), version);
2596 return;
2597 }
2598
2599 const uint64_t debug_info_offset
2600 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2601 addr += offset_size;
2602 const auto per_cu_it
2603 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2604 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2605 {
2606 warning (_("Section .debug_aranges in %s entry at offset %s "
2607 "debug_info_offset %s does not exists, "
2608 "ignoring .debug_aranges."),
2609 objfile_name (objfile),
2610 plongest (entry_addr - section->buffer),
2611 pulongest (debug_info_offset));
2612 return;
2613 }
2614 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2615
2616 const uint8_t address_size = *addr++;
2617 if (address_size < 1 || address_size > 8)
2618 {
2619 warning (_("Section .debug_aranges in %s entry at offset %s "
2620 "address_size %u is invalid, ignoring .debug_aranges."),
2621 objfile_name (objfile),
2622 plongest (entry_addr - section->buffer), address_size);
2623 return;
2624 }
2625
2626 const uint8_t segment_selector_size = *addr++;
2627 if (segment_selector_size != 0)
2628 {
2629 warning (_("Section .debug_aranges in %s entry at offset %s "
2630 "segment_selector_size %u is not supported, "
2631 "ignoring .debug_aranges."),
2632 objfile_name (objfile),
2633 plongest (entry_addr - section->buffer),
2634 segment_selector_size);
2635 return;
2636 }
2637
2638 /* Must pad to an alignment boundary that is twice the address
2639 size. It is undocumented by the DWARF standard but GCC does
2640 use it. */
2641 for (size_t padding = ((-(addr - section->buffer))
2642 & (2 * address_size - 1));
2643 padding > 0; padding--)
2644 if (*addr++ != 0)
2645 {
2646 warning (_("Section .debug_aranges in %s entry at offset %s "
2647 "padding is not zero, ignoring .debug_aranges."),
2648 objfile_name (objfile),
2649 plongest (entry_addr - section->buffer));
2650 return;
2651 }
2652
2653 for (;;)
2654 {
2655 if (addr + 2 * address_size > entry_end)
2656 {
2657 warning (_("Section .debug_aranges in %s entry at offset %s "
2658 "address list is not properly terminated, "
2659 "ignoring .debug_aranges."),
2660 objfile_name (objfile),
2661 plongest (entry_addr - section->buffer));
2662 return;
2663 }
2664 ULONGEST start = extract_unsigned_integer (addr, address_size,
2665 dwarf5_byte_order);
2666 addr += address_size;
2667 ULONGEST length = extract_unsigned_integer (addr, address_size,
2668 dwarf5_byte_order);
2669 addr += address_size;
2670 if (start == 0 && length == 0)
2671 break;
2672 if (start == 0 && !per_bfd->has_section_at_zero)
2673 {
2674 /* Symbol was eliminated due to a COMDAT group. */
2675 continue;
2676 }
2677 ULONGEST end = start + length;
2678 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2679 - baseaddr);
2680 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2681 - baseaddr);
2682 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2683 }
2684 }
2685
2686 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2687 &per_bfd->obstack);
2688 }
2689
2690 /* A helper function that reads the .gdb_index from BUFFER and fills
2691 in MAP. FILENAME is the name of the file containing the data;
2692 it is used for error reporting. DEPRECATED_OK is true if it is
2693 ok to use deprecated sections.
2694
2695 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2696 out parameters that are filled in with information about the CU and
2697 TU lists in the section.
2698
2699 Returns true if all went well, false otherwise. */
2700
2701 static bool
2702 read_gdb_index_from_buffer (const char *filename,
2703 bool deprecated_ok,
2704 gdb::array_view<const gdb_byte> buffer,
2705 struct mapped_index *map,
2706 const gdb_byte **cu_list,
2707 offset_type *cu_list_elements,
2708 const gdb_byte **types_list,
2709 offset_type *types_list_elements)
2710 {
2711 const gdb_byte *addr = &buffer[0];
2712 offset_view metadata (buffer);
2713
2714 /* Version check. */
2715 offset_type version = metadata[0];
2716 /* Versions earlier than 3 emitted every copy of a psymbol. This
2717 causes the index to behave very poorly for certain requests. Version 3
2718 contained incomplete addrmap. So, it seems better to just ignore such
2719 indices. */
2720 if (version < 4)
2721 {
2722 static int warning_printed = 0;
2723 if (!warning_printed)
2724 {
2725 warning (_("Skipping obsolete .gdb_index section in %s."),
2726 filename);
2727 warning_printed = 1;
2728 }
2729 return 0;
2730 }
2731 /* Index version 4 uses a different hash function than index version
2732 5 and later.
2733
2734 Versions earlier than 6 did not emit psymbols for inlined
2735 functions. Using these files will cause GDB not to be able to
2736 set breakpoints on inlined functions by name, so we ignore these
2737 indices unless the user has done
2738 "set use-deprecated-index-sections on". */
2739 if (version < 6 && !deprecated_ok)
2740 {
2741 static int warning_printed = 0;
2742 if (!warning_printed)
2743 {
2744 warning (_("\
2745 Skipping deprecated .gdb_index section in %s.\n\
2746 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2747 to use the section anyway."),
2748 filename);
2749 warning_printed = 1;
2750 }
2751 return 0;
2752 }
2753 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2754 of the TU (for symbols coming from TUs),
2755 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2756 Plus gold-generated indices can have duplicate entries for global symbols,
2757 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2758 These are just performance bugs, and we can't distinguish gdb-generated
2759 indices from gold-generated ones, so issue no warning here. */
2760
2761 /* Indexes with higher version than the one supported by GDB may be no
2762 longer backward compatible. */
2763 if (version > 8)
2764 return 0;
2765
2766 map->version = version;
2767
2768 int i = 1;
2769 *cu_list = addr + metadata[i];
2770 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2771 ++i;
2772
2773 *types_list = addr + metadata[i];
2774 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2775 ++i;
2776
2777 const gdb_byte *address_table = addr + metadata[i];
2778 const gdb_byte *address_table_end = addr + metadata[i + 1];
2779 map->address_table
2780 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2781 ++i;
2782
2783 const gdb_byte *symbol_table = addr + metadata[i];
2784 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2785 map->symbol_table
2786 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2787 symbol_table_end));
2788
2789 ++i;
2790 map->constant_pool = buffer.slice (metadata[i]);
2791
2792 return 1;
2793 }
2794
2795 /* Callback types for dwarf2_read_gdb_index. */
2796
2797 typedef gdb::function_view
2798 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2799 get_gdb_index_contents_ftype;
2800 typedef gdb::function_view
2801 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2802 get_gdb_index_contents_dwz_ftype;
2803
2804 /* Read .gdb_index. If everything went ok, initialize the "quick"
2805 elements of all the CUs and return 1. Otherwise, return 0. */
2806
2807 static int
2808 dwarf2_read_gdb_index
2809 (dwarf2_per_objfile *per_objfile,
2810 get_gdb_index_contents_ftype get_gdb_index_contents,
2811 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2812 {
2813 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2814 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2815 struct dwz_file *dwz;
2816 struct objfile *objfile = per_objfile->objfile;
2817 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2818
2819 gdb::array_view<const gdb_byte> main_index_contents
2820 = get_gdb_index_contents (objfile, per_bfd);
2821
2822 if (main_index_contents.empty ())
2823 return 0;
2824
2825 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2826 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2827 use_deprecated_index_sections,
2828 main_index_contents, map.get (), &cu_list,
2829 &cu_list_elements, &types_list,
2830 &types_list_elements))
2831 return 0;
2832
2833 /* Don't use the index if it's empty. */
2834 if (map->symbol_table.empty ())
2835 return 0;
2836
2837 /* If there is a .dwz file, read it so we can get its CU list as
2838 well. */
2839 dwz = dwarf2_get_dwz_file (per_bfd);
2840 if (dwz != NULL)
2841 {
2842 struct mapped_index dwz_map;
2843 const gdb_byte *dwz_types_ignore;
2844 offset_type dwz_types_elements_ignore;
2845
2846 gdb::array_view<const gdb_byte> dwz_index_content
2847 = get_gdb_index_contents_dwz (objfile, dwz);
2848
2849 if (dwz_index_content.empty ())
2850 return 0;
2851
2852 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2853 1, dwz_index_content, &dwz_map,
2854 &dwz_list, &dwz_list_elements,
2855 &dwz_types_ignore,
2856 &dwz_types_elements_ignore))
2857 {
2858 warning (_("could not read '.gdb_index' section from %s; skipping"),
2859 bfd_get_filename (dwz->dwz_bfd.get ()));
2860 return 0;
2861 }
2862 }
2863
2864 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2865 dwz_list_elements);
2866
2867 if (types_list_elements)
2868 {
2869 /* We can only handle a single .debug_types when we have an
2870 index. */
2871 if (per_bfd->types.size () != 1)
2872 return 0;
2873
2874 dwarf2_section_info *section = &per_bfd->types[0];
2875
2876 create_signatured_type_table_from_index (per_bfd, section, types_list,
2877 types_list_elements);
2878 }
2879
2880 create_addrmap_from_index (per_objfile, map.get ());
2881
2882 per_bfd->index_table = std::move (map);
2883 per_bfd->using_index = 1;
2884 per_bfd->quick_file_names_table =
2885 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2886
2887 return 1;
2888 }
2889
2890 /* die_reader_func for dw2_get_file_names. */
2891
2892 static void
2893 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2894 struct die_info *comp_unit_die)
2895 {
2896 struct dwarf2_cu *cu = reader->cu;
2897 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2898 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2899 struct dwarf2_per_cu_data *lh_cu;
2900 struct attribute *attr;
2901 void **slot;
2902 struct quick_file_names *qfn;
2903
2904 gdb_assert (! this_cu->is_debug_types);
2905
2906 /* Our callers never want to match partial units -- instead they
2907 will match the enclosing full CU. */
2908 if (comp_unit_die->tag == DW_TAG_partial_unit)
2909 {
2910 this_cu->v.quick->no_file_data = 1;
2911 return;
2912 }
2913
2914 lh_cu = this_cu;
2915 slot = NULL;
2916
2917 line_header_up lh;
2918 sect_offset line_offset {};
2919
2920 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2921 if (attr != nullptr && attr->form_is_unsigned ())
2922 {
2923 struct quick_file_names find_entry;
2924
2925 line_offset = (sect_offset) attr->as_unsigned ();
2926
2927 /* We may have already read in this line header (TU line header sharing).
2928 If we have we're done. */
2929 find_entry.hash.dwo_unit = cu->dwo_unit;
2930 find_entry.hash.line_sect_off = line_offset;
2931 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2932 &find_entry, INSERT);
2933 if (*slot != NULL)
2934 {
2935 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
2936 return;
2937 }
2938
2939 lh = dwarf_decode_line_header (line_offset, cu);
2940 }
2941 if (lh == NULL)
2942 {
2943 lh_cu->v.quick->no_file_data = 1;
2944 return;
2945 }
2946
2947 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2948 qfn->hash.dwo_unit = cu->dwo_unit;
2949 qfn->hash.line_sect_off = line_offset;
2950 gdb_assert (slot != NULL);
2951 *slot = qfn;
2952
2953 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
2954
2955 int offset = 0;
2956 if (strcmp (fnd.name, "<unknown>") != 0)
2957 ++offset;
2958
2959 qfn->num_file_names = offset + lh->file_names_size ();
2960 qfn->file_names =
2961 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2962 qfn->num_file_names);
2963 if (offset != 0)
2964 qfn->file_names[0] = xstrdup (fnd.name);
2965 for (int i = 0; i < lh->file_names_size (); ++i)
2966 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
2967 fnd.comp_dir).release ();
2968 qfn->real_names = NULL;
2969
2970 lh_cu->v.quick->file_names = qfn;
2971 }
2972
2973 /* A helper for the "quick" functions which attempts to read the line
2974 table for THIS_CU. */
2975
2976 static struct quick_file_names *
2977 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2978 dwarf2_per_objfile *per_objfile)
2979 {
2980 /* This should never be called for TUs. */
2981 gdb_assert (! this_cu->is_debug_types);
2982 /* Nor type unit groups. */
2983 gdb_assert (! this_cu->type_unit_group_p ());
2984
2985 if (this_cu->v.quick->file_names != NULL)
2986 return this_cu->v.quick->file_names;
2987 /* If we know there is no line data, no point in looking again. */
2988 if (this_cu->v.quick->no_file_data)
2989 return NULL;
2990
2991 cutu_reader reader (this_cu, per_objfile);
2992 if (!reader.dummy_p)
2993 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2994
2995 if (this_cu->v.quick->no_file_data)
2996 return NULL;
2997 return this_cu->v.quick->file_names;
2998 }
2999
3000 /* A helper for the "quick" functions which computes and caches the
3001 real path for a given file name from the line table. */
3002
3003 static const char *
3004 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3005 struct quick_file_names *qfn, int index)
3006 {
3007 if (qfn->real_names == NULL)
3008 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3009 qfn->num_file_names, const char *);
3010
3011 if (qfn->real_names[index] == NULL)
3012 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3013
3014 return qfn->real_names[index];
3015 }
3016
3017 struct symtab *
3018 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3019 {
3020 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3021 dwarf2_per_cu_data *dwarf_cu
3022 = per_objfile->per_bfd->all_comp_units.back ().get ();
3023 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3024
3025 if (cust == NULL)
3026 return NULL;
3027
3028 return compunit_primary_filetab (cust);
3029 }
3030
3031 /* Traversal function for dw2_forget_cached_source_info. */
3032
3033 static int
3034 dw2_free_cached_file_names (void **slot, void *info)
3035 {
3036 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3037
3038 if (file_data->real_names)
3039 {
3040 int i;
3041
3042 for (i = 0; i < file_data->num_file_names; ++i)
3043 {
3044 xfree ((void*) file_data->real_names[i]);
3045 file_data->real_names[i] = NULL;
3046 }
3047 }
3048
3049 return 1;
3050 }
3051
3052 void
3053 dwarf2_base_index_functions::forget_cached_source_info
3054 (struct objfile *objfile)
3055 {
3056 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3057
3058 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3059 dw2_free_cached_file_names, NULL);
3060 }
3061
3062 /* Struct used to manage iterating over all CUs looking for a symbol. */
3063
3064 struct dw2_symtab_iterator
3065 {
3066 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3067 dwarf2_per_objfile *per_objfile;
3068 /* If set, only look for symbols that match that block. Valid values are
3069 GLOBAL_BLOCK and STATIC_BLOCK. */
3070 gdb::optional<block_enum> block_index;
3071 /* The kind of symbol we're looking for. */
3072 domain_enum domain;
3073 /* The list of CUs from the index entry of the symbol,
3074 or NULL if not found. */
3075 offset_view vec;
3076 /* The next element in VEC to look at. */
3077 int next;
3078 /* The number of elements in VEC, or zero if there is no match. */
3079 int length;
3080 /* Have we seen a global version of the symbol?
3081 If so we can ignore all further global instances.
3082 This is to work around gold/15646, inefficient gold-generated
3083 indices. */
3084 int global_seen;
3085 };
3086
3087 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3088
3089 static void
3090 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3091 dwarf2_per_objfile *per_objfile,
3092 gdb::optional<block_enum> block_index,
3093 domain_enum domain, offset_type namei)
3094 {
3095 iter->per_objfile = per_objfile;
3096 iter->block_index = block_index;
3097 iter->domain = domain;
3098 iter->next = 0;
3099 iter->global_seen = 0;
3100 iter->vec = {};
3101 iter->length = 0;
3102
3103 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3104 /* index is NULL if OBJF_READNOW. */
3105 if (index == NULL)
3106 return;
3107
3108 gdb_assert (!index->symbol_name_slot_invalid (namei));
3109 offset_type vec_idx = index->symbol_vec_index (namei);
3110
3111 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3112 iter->length = iter->vec[0];
3113 }
3114
3115 /* Return the next matching CU or NULL if there are no more. */
3116
3117 static struct dwarf2_per_cu_data *
3118 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3119 {
3120 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3121
3122 for ( ; iter->next < iter->length; ++iter->next)
3123 {
3124 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3125 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3126 gdb_index_symbol_kind symbol_kind =
3127 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3128 /* Only check the symbol attributes if they're present.
3129 Indices prior to version 7 don't record them,
3130 and indices >= 7 may elide them for certain symbols
3131 (gold does this). */
3132 int attrs_valid =
3133 (per_objfile->per_bfd->index_table->version >= 7
3134 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3135
3136 /* Don't crash on bad data. */
3137 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3138 {
3139 complaint (_(".gdb_index entry has bad CU index"
3140 " [in module %s]"), objfile_name (per_objfile->objfile));
3141 continue;
3142 }
3143
3144 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3145
3146 /* Skip if already read in. */
3147 if (per_objfile->symtab_set_p (per_cu))
3148 continue;
3149
3150 /* Check static vs global. */
3151 if (attrs_valid)
3152 {
3153 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3154
3155 if (iter->block_index.has_value ())
3156 {
3157 bool want_static = *iter->block_index == STATIC_BLOCK;
3158
3159 if (is_static != want_static)
3160 continue;
3161 }
3162
3163 /* Work around gold/15646. */
3164 if (!is_static
3165 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3166 {
3167 if (iter->global_seen)
3168 continue;
3169
3170 iter->global_seen = 1;
3171 }
3172 }
3173
3174 /* Only check the symbol's kind if it has one. */
3175 if (attrs_valid)
3176 {
3177 switch (iter->domain)
3178 {
3179 case VAR_DOMAIN:
3180 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3181 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3182 /* Some types are also in VAR_DOMAIN. */
3183 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3184 continue;
3185 break;
3186 case STRUCT_DOMAIN:
3187 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3188 continue;
3189 break;
3190 case LABEL_DOMAIN:
3191 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3192 continue;
3193 break;
3194 case MODULE_DOMAIN:
3195 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3196 continue;
3197 break;
3198 default:
3199 break;
3200 }
3201 }
3202
3203 ++iter->next;
3204 return per_cu;
3205 }
3206
3207 return NULL;
3208 }
3209
3210 void
3211 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3212 bool print_bcache)
3213 {
3214 if (print_bcache)
3215 return;
3216
3217 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3218 int total = per_objfile->per_bfd->all_comp_units.size ();
3219 int count = 0;
3220
3221 for (int i = 0; i < total; ++i)
3222 {
3223 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3224
3225 if (!per_objfile->symtab_set_p (per_cu))
3226 ++count;
3227 }
3228 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3229 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3230 }
3231
3232 /* This dumps minimal information about the index.
3233 It is called via "mt print objfiles".
3234 One use is to verify .gdb_index has been loaded by the
3235 gdb.dwarf2/gdb-index.exp testcase. */
3236
3237 void
3238 dwarf2_gdb_index::dump (struct objfile *objfile)
3239 {
3240 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3241
3242 gdb_assert (per_objfile->per_bfd->using_index);
3243 printf_filtered (".gdb_index:");
3244 if (per_objfile->per_bfd->index_table != NULL)
3245 {
3246 printf_filtered (" version %d\n",
3247 per_objfile->per_bfd->index_table->version);
3248 }
3249 else
3250 printf_filtered (" faked for \"readnow\"\n");
3251 printf_filtered ("\n");
3252 }
3253
3254 void
3255 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3256 {
3257 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3258 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3259
3260 for (int i = 0; i < total_units; ++i)
3261 {
3262 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3263
3264 /* We don't want to directly expand a partial CU, because if we
3265 read it with the wrong language, then assertion failures can
3266 be triggered later on. See PR symtab/23010. So, tell
3267 dw2_instantiate_symtab to skip partial CUs -- any important
3268 partial CU will be read via DW_TAG_imported_unit anyway. */
3269 dw2_instantiate_symtab (per_cu, per_objfile, true);
3270 }
3271 }
3272
3273 static bool
3274 dw2_expand_symtabs_matching_symbol
3275 (mapped_index_base &index,
3276 const lookup_name_info &lookup_name_in,
3277 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3278 gdb::function_view<bool (offset_type)> match_callback,
3279 dwarf2_per_objfile *per_objfile);
3280
3281 static bool
3282 dw2_expand_symtabs_matching_one
3283 (dwarf2_per_cu_data *per_cu,
3284 dwarf2_per_objfile *per_objfile,
3285 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3286 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3287
3288 void
3289 dwarf2_gdb_index::expand_matching_symbols
3290 (struct objfile *objfile,
3291 const lookup_name_info &name, domain_enum domain,
3292 int global,
3293 symbol_compare_ftype *ordered_compare)
3294 {
3295 /* Used for Ada. */
3296 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3297
3298 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3299
3300 if (per_objfile->per_bfd->index_table != nullptr)
3301 {
3302 mapped_index &index = *per_objfile->per_bfd->index_table;
3303
3304 const char *match_name = name.ada ().lookup_name ().c_str ();
3305 auto matcher = [&] (const char *symname)
3306 {
3307 if (ordered_compare == nullptr)
3308 return true;
3309 return ordered_compare (symname, match_name) == 0;
3310 };
3311
3312 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3313 [&] (offset_type namei)
3314 {
3315 struct dw2_symtab_iterator iter;
3316 struct dwarf2_per_cu_data *per_cu;
3317
3318 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3319 namei);
3320 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3321 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3322 nullptr);
3323 return true;
3324 }, per_objfile);
3325 }
3326 else
3327 {
3328 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3329 proceed assuming all symtabs have been read in. */
3330 }
3331 }
3332
3333 /* Starting from a search name, return the string that finds the upper
3334 bound of all strings that start with SEARCH_NAME in a sorted name
3335 list. Returns the empty string to indicate that the upper bound is
3336 the end of the list. */
3337
3338 static std::string
3339 make_sort_after_prefix_name (const char *search_name)
3340 {
3341 /* When looking to complete "func", we find the upper bound of all
3342 symbols that start with "func" by looking for where we'd insert
3343 the closest string that would follow "func" in lexicographical
3344 order. Usually, that's "func"-with-last-character-incremented,
3345 i.e. "fund". Mind non-ASCII characters, though. Usually those
3346 will be UTF-8 multi-byte sequences, but we can't be certain.
3347 Especially mind the 0xff character, which is a valid character in
3348 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3349 rule out compilers allowing it in identifiers. Note that
3350 conveniently, strcmp/strcasecmp are specified to compare
3351 characters interpreted as unsigned char. So what we do is treat
3352 the whole string as a base 256 number composed of a sequence of
3353 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3354 to 0, and carries 1 to the following more-significant position.
3355 If the very first character in SEARCH_NAME ends up incremented
3356 and carries/overflows, then the upper bound is the end of the
3357 list. The string after the empty string is also the empty
3358 string.
3359
3360 Some examples of this operation:
3361
3362 SEARCH_NAME => "+1" RESULT
3363
3364 "abc" => "abd"
3365 "ab\xff" => "ac"
3366 "\xff" "a" "\xff" => "\xff" "b"
3367 "\xff" => ""
3368 "\xff\xff" => ""
3369 "" => ""
3370
3371 Then, with these symbols for example:
3372
3373 func
3374 func1
3375 fund
3376
3377 completing "func" looks for symbols between "func" and
3378 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3379 which finds "func" and "func1", but not "fund".
3380
3381 And with:
3382
3383 funcÿ (Latin1 'ÿ' [0xff])
3384 funcÿ1
3385 fund
3386
3387 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3388 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3389
3390 And with:
3391
3392 ÿÿ (Latin1 'ÿ' [0xff])
3393 ÿÿ1
3394
3395 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3396 the end of the list.
3397 */
3398 std::string after = search_name;
3399 while (!after.empty () && (unsigned char) after.back () == 0xff)
3400 after.pop_back ();
3401 if (!after.empty ())
3402 after.back () = (unsigned char) after.back () + 1;
3403 return after;
3404 }
3405
3406 /* See declaration. */
3407
3408 std::pair<std::vector<name_component>::const_iterator,
3409 std::vector<name_component>::const_iterator>
3410 mapped_index_base::find_name_components_bounds
3411 (const lookup_name_info &lookup_name_without_params, language lang,
3412 dwarf2_per_objfile *per_objfile) const
3413 {
3414 auto *name_cmp
3415 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3416
3417 const char *lang_name
3418 = lookup_name_without_params.language_lookup_name (lang);
3419
3420 /* Comparison function object for lower_bound that matches against a
3421 given symbol name. */
3422 auto lookup_compare_lower = [&] (const name_component &elem,
3423 const char *name)
3424 {
3425 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3426 const char *elem_name = elem_qualified + elem.name_offset;
3427 return name_cmp (elem_name, name) < 0;
3428 };
3429
3430 /* Comparison function object for upper_bound that matches against a
3431 given symbol name. */
3432 auto lookup_compare_upper = [&] (const char *name,
3433 const name_component &elem)
3434 {
3435 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3436 const char *elem_name = elem_qualified + elem.name_offset;
3437 return name_cmp (name, elem_name) < 0;
3438 };
3439
3440 auto begin = this->name_components.begin ();
3441 auto end = this->name_components.end ();
3442
3443 /* Find the lower bound. */
3444 auto lower = [&] ()
3445 {
3446 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3447 return begin;
3448 else
3449 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3450 } ();
3451
3452 /* Find the upper bound. */
3453 auto upper = [&] ()
3454 {
3455 if (lookup_name_without_params.completion_mode ())
3456 {
3457 /* In completion mode, we want UPPER to point past all
3458 symbols names that have the same prefix. I.e., with
3459 these symbols, and completing "func":
3460
3461 function << lower bound
3462 function1
3463 other_function << upper bound
3464
3465 We find the upper bound by looking for the insertion
3466 point of "func"-with-last-character-incremented,
3467 i.e. "fund". */
3468 std::string after = make_sort_after_prefix_name (lang_name);
3469 if (after.empty ())
3470 return end;
3471 return std::lower_bound (lower, end, after.c_str (),
3472 lookup_compare_lower);
3473 }
3474 else
3475 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3476 } ();
3477
3478 return {lower, upper};
3479 }
3480
3481 /* See declaration. */
3482
3483 void
3484 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3485 {
3486 if (!this->name_components.empty ())
3487 return;
3488
3489 this->name_components_casing = case_sensitivity;
3490 auto *name_cmp
3491 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3492
3493 /* The code below only knows how to break apart components of C++
3494 symbol names (and other languages that use '::' as
3495 namespace/module separator) and Ada symbol names. */
3496 auto count = this->symbol_name_count ();
3497 for (offset_type idx = 0; idx < count; idx++)
3498 {
3499 if (this->symbol_name_slot_invalid (idx))
3500 continue;
3501
3502 const char *name = this->symbol_name_at (idx, per_objfile);
3503
3504 /* Add each name component to the name component table. */
3505 unsigned int previous_len = 0;
3506
3507 if (strstr (name, "::") != nullptr)
3508 {
3509 for (unsigned int current_len = cp_find_first_component (name);
3510 name[current_len] != '\0';
3511 current_len += cp_find_first_component (name + current_len))
3512 {
3513 gdb_assert (name[current_len] == ':');
3514 this->name_components.push_back ({previous_len, idx});
3515 /* Skip the '::'. */
3516 current_len += 2;
3517 previous_len = current_len;
3518 }
3519 }
3520 else
3521 {
3522 /* Handle the Ada encoded (aka mangled) form here. */
3523 for (const char *iter = strstr (name, "__");
3524 iter != nullptr;
3525 iter = strstr (iter, "__"))
3526 {
3527 this->name_components.push_back ({previous_len, idx});
3528 iter += 2;
3529 previous_len = iter - name;
3530 }
3531 }
3532
3533 this->name_components.push_back ({previous_len, idx});
3534 }
3535
3536 /* Sort name_components elements by name. */
3537 auto name_comp_compare = [&] (const name_component &left,
3538 const name_component &right)
3539 {
3540 const char *left_qualified
3541 = this->symbol_name_at (left.idx, per_objfile);
3542 const char *right_qualified
3543 = this->symbol_name_at (right.idx, per_objfile);
3544
3545 const char *left_name = left_qualified + left.name_offset;
3546 const char *right_name = right_qualified + right.name_offset;
3547
3548 return name_cmp (left_name, right_name) < 0;
3549 };
3550
3551 std::sort (this->name_components.begin (),
3552 this->name_components.end (),
3553 name_comp_compare);
3554 }
3555
3556 /* Helper for dw2_expand_symtabs_matching that works with a
3557 mapped_index_base instead of the containing objfile. This is split
3558 to a separate function in order to be able to unit test the
3559 name_components matching using a mock mapped_index_base. For each
3560 symbol name that matches, calls MATCH_CALLBACK, passing it the
3561 symbol's index in the mapped_index_base symbol table. */
3562
3563 static bool
3564 dw2_expand_symtabs_matching_symbol
3565 (mapped_index_base &index,
3566 const lookup_name_info &lookup_name_in,
3567 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3568 gdb::function_view<bool (offset_type)> match_callback,
3569 dwarf2_per_objfile *per_objfile)
3570 {
3571 lookup_name_info lookup_name_without_params
3572 = lookup_name_in.make_ignore_params ();
3573
3574 /* Build the symbol name component sorted vector, if we haven't
3575 yet. */
3576 index.build_name_components (per_objfile);
3577
3578 /* The same symbol may appear more than once in the range though.
3579 E.g., if we're looking for symbols that complete "w", and we have
3580 a symbol named "w1::w2", we'll find the two name components for
3581 that same symbol in the range. To be sure we only call the
3582 callback once per symbol, we first collect the symbol name
3583 indexes that matched in a temporary vector and ignore
3584 duplicates. */
3585 std::vector<offset_type> matches;
3586
3587 struct name_and_matcher
3588 {
3589 symbol_name_matcher_ftype *matcher;
3590 const char *name;
3591
3592 bool operator== (const name_and_matcher &other) const
3593 {
3594 return matcher == other.matcher && strcmp (name, other.name) == 0;
3595 }
3596 };
3597
3598 /* A vector holding all the different symbol name matchers, for all
3599 languages. */
3600 std::vector<name_and_matcher> matchers;
3601
3602 for (int i = 0; i < nr_languages; i++)
3603 {
3604 enum language lang_e = (enum language) i;
3605
3606 const language_defn *lang = language_def (lang_e);
3607 symbol_name_matcher_ftype *name_matcher
3608 = lang->get_symbol_name_matcher (lookup_name_without_params);
3609
3610 name_and_matcher key {
3611 name_matcher,
3612 lookup_name_without_params.language_lookup_name (lang_e)
3613 };
3614
3615 /* Don't insert the same comparison routine more than once.
3616 Note that we do this linear walk. This is not a problem in
3617 practice because the number of supported languages is
3618 low. */
3619 if (std::find (matchers.begin (), matchers.end (), key)
3620 != matchers.end ())
3621 continue;
3622 matchers.push_back (std::move (key));
3623
3624 auto bounds
3625 = index.find_name_components_bounds (lookup_name_without_params,
3626 lang_e, per_objfile);
3627
3628 /* Now for each symbol name in range, check to see if we have a name
3629 match, and if so, call the MATCH_CALLBACK callback. */
3630
3631 for (; bounds.first != bounds.second; ++bounds.first)
3632 {
3633 const char *qualified
3634 = index.symbol_name_at (bounds.first->idx, per_objfile);
3635
3636 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3637 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3638 continue;
3639
3640 matches.push_back (bounds.first->idx);
3641 }
3642 }
3643
3644 std::sort (matches.begin (), matches.end ());
3645
3646 /* Finally call the callback, once per match. */
3647 ULONGEST prev = -1;
3648 bool result = true;
3649 for (offset_type idx : matches)
3650 {
3651 if (prev != idx)
3652 {
3653 if (!match_callback (idx))
3654 {
3655 result = false;
3656 break;
3657 }
3658 prev = idx;
3659 }
3660 }
3661
3662 /* Above we use a type wider than idx's for 'prev', since 0 and
3663 (offset_type)-1 are both possible values. */
3664 static_assert (sizeof (prev) > sizeof (offset_type), "");
3665
3666 return result;
3667 }
3668
3669 #if GDB_SELF_TEST
3670
3671 namespace selftests { namespace dw2_expand_symtabs_matching {
3672
3673 /* A mock .gdb_index/.debug_names-like name index table, enough to
3674 exercise dw2_expand_symtabs_matching_symbol, which works with the
3675 mapped_index_base interface. Builds an index from the symbol list
3676 passed as parameter to the constructor. */
3677 class mock_mapped_index : public mapped_index_base
3678 {
3679 public:
3680 mock_mapped_index (gdb::array_view<const char *> symbols)
3681 : m_symbol_table (symbols)
3682 {}
3683
3684 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3685
3686 /* Return the number of names in the symbol table. */
3687 size_t symbol_name_count () const override
3688 {
3689 return m_symbol_table.size ();
3690 }
3691
3692 /* Get the name of the symbol at IDX in the symbol table. */
3693 const char *symbol_name_at
3694 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3695 {
3696 return m_symbol_table[idx];
3697 }
3698
3699 private:
3700 gdb::array_view<const char *> m_symbol_table;
3701 };
3702
3703 /* Convenience function that converts a NULL pointer to a "<null>"
3704 string, to pass to print routines. */
3705
3706 static const char *
3707 string_or_null (const char *str)
3708 {
3709 return str != NULL ? str : "<null>";
3710 }
3711
3712 /* Check if a lookup_name_info built from
3713 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3714 index. EXPECTED_LIST is the list of expected matches, in expected
3715 matching order. If no match expected, then an empty list is
3716 specified. Returns true on success. On failure prints a warning
3717 indicating the file:line that failed, and returns false. */
3718
3719 static bool
3720 check_match (const char *file, int line,
3721 mock_mapped_index &mock_index,
3722 const char *name, symbol_name_match_type match_type,
3723 bool completion_mode,
3724 std::initializer_list<const char *> expected_list,
3725 dwarf2_per_objfile *per_objfile)
3726 {
3727 lookup_name_info lookup_name (name, match_type, completion_mode);
3728
3729 bool matched = true;
3730
3731 auto mismatch = [&] (const char *expected_str,
3732 const char *got)
3733 {
3734 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3735 "expected=\"%s\", got=\"%s\"\n"),
3736 file, line,
3737 (match_type == symbol_name_match_type::FULL
3738 ? "FULL" : "WILD"),
3739 name, string_or_null (expected_str), string_or_null (got));
3740 matched = false;
3741 };
3742
3743 auto expected_it = expected_list.begin ();
3744 auto expected_end = expected_list.end ();
3745
3746 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3747 nullptr,
3748 [&] (offset_type idx)
3749 {
3750 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3751 const char *expected_str
3752 = expected_it == expected_end ? NULL : *expected_it++;
3753
3754 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3755 mismatch (expected_str, matched_name);
3756 return true;
3757 }, per_objfile);
3758
3759 const char *expected_str
3760 = expected_it == expected_end ? NULL : *expected_it++;
3761 if (expected_str != NULL)
3762 mismatch (expected_str, NULL);
3763
3764 return matched;
3765 }
3766
3767 /* The symbols added to the mock mapped_index for testing (in
3768 canonical form). */
3769 static const char *test_symbols[] = {
3770 "function",
3771 "std::bar",
3772 "std::zfunction",
3773 "std::zfunction2",
3774 "w1::w2",
3775 "ns::foo<char*>",
3776 "ns::foo<int>",
3777 "ns::foo<long>",
3778 "ns2::tmpl<int>::foo2",
3779 "(anonymous namespace)::A::B::C",
3780
3781 /* These are used to check that the increment-last-char in the
3782 matching algorithm for completion doesn't match "t1_fund" when
3783 completing "t1_func". */
3784 "t1_func",
3785 "t1_func1",
3786 "t1_fund",
3787 "t1_fund1",
3788
3789 /* A UTF-8 name with multi-byte sequences to make sure that
3790 cp-name-parser understands this as a single identifier ("função"
3791 is "function" in PT). */
3792 u8"u8função",
3793
3794 /* \377 (0xff) is Latin1 'ÿ'. */
3795 "yfunc\377",
3796
3797 /* \377 (0xff) is Latin1 'ÿ'. */
3798 "\377",
3799 "\377\377123",
3800
3801 /* A name with all sorts of complications. Starts with "z" to make
3802 it easier for the completion tests below. */
3803 #define Z_SYM_NAME \
3804 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3805 "::tuple<(anonymous namespace)::ui*, " \
3806 "std::default_delete<(anonymous namespace)::ui>, void>"
3807
3808 Z_SYM_NAME
3809 };
3810
3811 /* Returns true if the mapped_index_base::find_name_component_bounds
3812 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3813 in completion mode. */
3814
3815 static bool
3816 check_find_bounds_finds (mapped_index_base &index,
3817 const char *search_name,
3818 gdb::array_view<const char *> expected_syms,
3819 dwarf2_per_objfile *per_objfile)
3820 {
3821 lookup_name_info lookup_name (search_name,
3822 symbol_name_match_type::FULL, true);
3823
3824 auto bounds = index.find_name_components_bounds (lookup_name,
3825 language_cplus,
3826 per_objfile);
3827
3828 size_t distance = std::distance (bounds.first, bounds.second);
3829 if (distance != expected_syms.size ())
3830 return false;
3831
3832 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3833 {
3834 auto nc_elem = bounds.first + exp_elem;
3835 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3836 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3837 return false;
3838 }
3839
3840 return true;
3841 }
3842
3843 /* Test the lower-level mapped_index::find_name_component_bounds
3844 method. */
3845
3846 static void
3847 test_mapped_index_find_name_component_bounds ()
3848 {
3849 mock_mapped_index mock_index (test_symbols);
3850
3851 mock_index.build_name_components (NULL /* per_objfile */);
3852
3853 /* Test the lower-level mapped_index::find_name_component_bounds
3854 method in completion mode. */
3855 {
3856 static const char *expected_syms[] = {
3857 "t1_func",
3858 "t1_func1",
3859 };
3860
3861 SELF_CHECK (check_find_bounds_finds
3862 (mock_index, "t1_func", expected_syms,
3863 NULL /* per_objfile */));
3864 }
3865
3866 /* Check that the increment-last-char in the name matching algorithm
3867 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3868 {
3869 static const char *expected_syms1[] = {
3870 "\377",
3871 "\377\377123",
3872 };
3873 SELF_CHECK (check_find_bounds_finds
3874 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3875
3876 static const char *expected_syms2[] = {
3877 "\377\377123",
3878 };
3879 SELF_CHECK (check_find_bounds_finds
3880 (mock_index, "\377\377", expected_syms2,
3881 NULL /* per_objfile */));
3882 }
3883 }
3884
3885 /* Test dw2_expand_symtabs_matching_symbol. */
3886
3887 static void
3888 test_dw2_expand_symtabs_matching_symbol ()
3889 {
3890 mock_mapped_index mock_index (test_symbols);
3891
3892 /* We let all tests run until the end even if some fails, for debug
3893 convenience. */
3894 bool any_mismatch = false;
3895
3896 /* Create the expected symbols list (an initializer_list). Needed
3897 because lists have commas, and we need to pass them to CHECK,
3898 which is a macro. */
3899 #define EXPECT(...) { __VA_ARGS__ }
3900
3901 /* Wrapper for check_match that passes down the current
3902 __FILE__/__LINE__. */
3903 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3904 any_mismatch |= !check_match (__FILE__, __LINE__, \
3905 mock_index, \
3906 NAME, MATCH_TYPE, COMPLETION_MODE, \
3907 EXPECTED_LIST, NULL)
3908
3909 /* Identity checks. */
3910 for (const char *sym : test_symbols)
3911 {
3912 /* Should be able to match all existing symbols. */
3913 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3914 EXPECT (sym));
3915
3916 /* Should be able to match all existing symbols with
3917 parameters. */
3918 std::string with_params = std::string (sym) + "(int)";
3919 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3920 EXPECT (sym));
3921
3922 /* Should be able to match all existing symbols with
3923 parameters and qualifiers. */
3924 with_params = std::string (sym) + " ( int ) const";
3925 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3926 EXPECT (sym));
3927
3928 /* This should really find sym, but cp-name-parser.y doesn't
3929 know about lvalue/rvalue qualifiers yet. */
3930 with_params = std::string (sym) + " ( int ) &&";
3931 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3932 {});
3933 }
3934
3935 /* Check that the name matching algorithm for completion doesn't get
3936 confused with Latin1 'ÿ' / 0xff. */
3937 {
3938 static const char str[] = "\377";
3939 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3940 EXPECT ("\377", "\377\377123"));
3941 }
3942
3943 /* Check that the increment-last-char in the matching algorithm for
3944 completion doesn't match "t1_fund" when completing "t1_func". */
3945 {
3946 static const char str[] = "t1_func";
3947 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3948 EXPECT ("t1_func", "t1_func1"));
3949 }
3950
3951 /* Check that completion mode works at each prefix of the expected
3952 symbol name. */
3953 {
3954 static const char str[] = "function(int)";
3955 size_t len = strlen (str);
3956 std::string lookup;
3957
3958 for (size_t i = 1; i < len; i++)
3959 {
3960 lookup.assign (str, i);
3961 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3962 EXPECT ("function"));
3963 }
3964 }
3965
3966 /* While "w" is a prefix of both components, the match function
3967 should still only be called once. */
3968 {
3969 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3970 EXPECT ("w1::w2"));
3971 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3972 EXPECT ("w1::w2"));
3973 }
3974
3975 /* Same, with a "complicated" symbol. */
3976 {
3977 static const char str[] = Z_SYM_NAME;
3978 size_t len = strlen (str);
3979 std::string lookup;
3980
3981 for (size_t i = 1; i < len; i++)
3982 {
3983 lookup.assign (str, i);
3984 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3985 EXPECT (Z_SYM_NAME));
3986 }
3987 }
3988
3989 /* In FULL mode, an incomplete symbol doesn't match. */
3990 {
3991 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3992 {});
3993 }
3994
3995 /* A complete symbol with parameters matches any overload, since the
3996 index has no overload info. */
3997 {
3998 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
3999 EXPECT ("std::zfunction", "std::zfunction2"));
4000 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4001 EXPECT ("std::zfunction", "std::zfunction2"));
4002 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4003 EXPECT ("std::zfunction", "std::zfunction2"));
4004 }
4005
4006 /* Check that whitespace is ignored appropriately. A symbol with a
4007 template argument list. */
4008 {
4009 static const char expected[] = "ns::foo<int>";
4010 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4011 EXPECT (expected));
4012 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4013 EXPECT (expected));
4014 }
4015
4016 /* Check that whitespace is ignored appropriately. A symbol with a
4017 template argument list that includes a pointer. */
4018 {
4019 static const char expected[] = "ns::foo<char*>";
4020 /* Try both completion and non-completion modes. */
4021 static const bool completion_mode[2] = {false, true};
4022 for (size_t i = 0; i < 2; i++)
4023 {
4024 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4025 completion_mode[i], EXPECT (expected));
4026 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4027 completion_mode[i], EXPECT (expected));
4028
4029 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4030 completion_mode[i], EXPECT (expected));
4031 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4032 completion_mode[i], EXPECT (expected));
4033 }
4034 }
4035
4036 {
4037 /* Check method qualifiers are ignored. */
4038 static const char expected[] = "ns::foo<char*>";
4039 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4040 symbol_name_match_type::FULL, true, EXPECT (expected));
4041 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4042 symbol_name_match_type::FULL, true, EXPECT (expected));
4043 CHECK_MATCH ("foo < char * > ( int ) const",
4044 symbol_name_match_type::WILD, true, EXPECT (expected));
4045 CHECK_MATCH ("foo < char * > ( int ) &&",
4046 symbol_name_match_type::WILD, true, EXPECT (expected));
4047 }
4048
4049 /* Test lookup names that don't match anything. */
4050 {
4051 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4052 {});
4053
4054 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4055 {});
4056 }
4057
4058 /* Some wild matching tests, exercising "(anonymous namespace)",
4059 which should not be confused with a parameter list. */
4060 {
4061 static const char *syms[] = {
4062 "A::B::C",
4063 "B::C",
4064 "C",
4065 "A :: B :: C ( int )",
4066 "B :: C ( int )",
4067 "C ( int )",
4068 };
4069
4070 for (const char *s : syms)
4071 {
4072 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4073 EXPECT ("(anonymous namespace)::A::B::C"));
4074 }
4075 }
4076
4077 {
4078 static const char expected[] = "ns2::tmpl<int>::foo2";
4079 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4080 EXPECT (expected));
4081 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4082 EXPECT (expected));
4083 }
4084
4085 SELF_CHECK (!any_mismatch);
4086
4087 #undef EXPECT
4088 #undef CHECK_MATCH
4089 }
4090
4091 static void
4092 run_test ()
4093 {
4094 test_mapped_index_find_name_component_bounds ();
4095 test_dw2_expand_symtabs_matching_symbol ();
4096 }
4097
4098 }} // namespace selftests::dw2_expand_symtabs_matching
4099
4100 #endif /* GDB_SELF_TEST */
4101
4102 /* If FILE_MATCHER is NULL or if PER_CU has
4103 dwarf2_per_cu_quick_data::MARK set (see
4104 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4105 EXPANSION_NOTIFY on it. */
4106
4107 static bool
4108 dw2_expand_symtabs_matching_one
4109 (dwarf2_per_cu_data *per_cu,
4110 dwarf2_per_objfile *per_objfile,
4111 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4112 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4113 {
4114 if (file_matcher == NULL || per_cu->v.quick->mark)
4115 {
4116 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4117
4118 compunit_symtab *symtab
4119 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4120 gdb_assert (symtab != nullptr);
4121
4122 if (expansion_notify != NULL && symtab_was_null)
4123 return expansion_notify (symtab);
4124 }
4125 return true;
4126 }
4127
4128 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4129 matched, to expand corresponding CUs that were marked. IDX is the
4130 index of the symbol name that matched. */
4131
4132 static bool
4133 dw2_expand_marked_cus
4134 (dwarf2_per_objfile *per_objfile, offset_type idx,
4135 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4136 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4137 block_search_flags search_flags,
4138 search_domain kind)
4139 {
4140 offset_type vec_len, vec_idx;
4141 bool global_seen = false;
4142 mapped_index &index = *per_objfile->per_bfd->index_table;
4143
4144 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4145 vec_len = vec[0];
4146 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4147 {
4148 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4149 /* This value is only valid for index versions >= 7. */
4150 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4151 gdb_index_symbol_kind symbol_kind =
4152 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4153 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4154 /* Only check the symbol attributes if they're present.
4155 Indices prior to version 7 don't record them,
4156 and indices >= 7 may elide them for certain symbols
4157 (gold does this). */
4158 int attrs_valid =
4159 (index.version >= 7
4160 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4161
4162 /* Work around gold/15646. */
4163 if (attrs_valid
4164 && !is_static
4165 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4166 {
4167 if (global_seen)
4168 continue;
4169
4170 global_seen = true;
4171 }
4172
4173 /* Only check the symbol's kind if it has one. */
4174 if (attrs_valid)
4175 {
4176 if (is_static)
4177 {
4178 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4179 continue;
4180 }
4181 else
4182 {
4183 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4184 continue;
4185 }
4186
4187 switch (kind)
4188 {
4189 case VARIABLES_DOMAIN:
4190 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4191 continue;
4192 break;
4193 case FUNCTIONS_DOMAIN:
4194 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4195 continue;
4196 break;
4197 case TYPES_DOMAIN:
4198 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4199 continue;
4200 break;
4201 case MODULES_DOMAIN:
4202 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4203 continue;
4204 break;
4205 default:
4206 break;
4207 }
4208 }
4209
4210 /* Don't crash on bad data. */
4211 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4212 {
4213 complaint (_(".gdb_index entry has bad CU index"
4214 " [in module %s]"), objfile_name (per_objfile->objfile));
4215 continue;
4216 }
4217
4218 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4219 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4220 expansion_notify))
4221 return false;
4222 }
4223
4224 return true;
4225 }
4226
4227 /* If FILE_MATCHER is non-NULL, set all the
4228 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4229 that match FILE_MATCHER. */
4230
4231 static void
4232 dw_expand_symtabs_matching_file_matcher
4233 (dwarf2_per_objfile *per_objfile,
4234 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4235 {
4236 if (file_matcher == NULL)
4237 return;
4238
4239 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4240 htab_eq_pointer,
4241 NULL, xcalloc, xfree));
4242 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4243 htab_eq_pointer,
4244 NULL, xcalloc, xfree));
4245
4246 /* The rule is CUs specify all the files, including those used by
4247 any TU, so there's no need to scan TUs here. */
4248
4249 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4250 {
4251 QUIT;
4252
4253 if (per_cu->is_debug_types)
4254 continue;
4255 per_cu->v.quick->mark = 0;
4256
4257 /* We only need to look at symtabs not already expanded. */
4258 if (per_objfile->symtab_set_p (per_cu.get ()))
4259 continue;
4260
4261 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4262 per_objfile);
4263 if (file_data == NULL)
4264 continue;
4265
4266 if (htab_find (visited_not_found.get (), file_data) != NULL)
4267 continue;
4268 else if (htab_find (visited_found.get (), file_data) != NULL)
4269 {
4270 per_cu->v.quick->mark = 1;
4271 continue;
4272 }
4273
4274 for (int j = 0; j < file_data->num_file_names; ++j)
4275 {
4276 const char *this_real_name;
4277
4278 if (file_matcher (file_data->file_names[j], false))
4279 {
4280 per_cu->v.quick->mark = 1;
4281 break;
4282 }
4283
4284 /* Before we invoke realpath, which can get expensive when many
4285 files are involved, do a quick comparison of the basenames. */
4286 if (!basenames_may_differ
4287 && !file_matcher (lbasename (file_data->file_names[j]),
4288 true))
4289 continue;
4290
4291 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4292 if (file_matcher (this_real_name, false))
4293 {
4294 per_cu->v.quick->mark = 1;
4295 break;
4296 }
4297 }
4298
4299 void **slot = htab_find_slot (per_cu->v.quick->mark
4300 ? visited_found.get ()
4301 : visited_not_found.get (),
4302 file_data, INSERT);
4303 *slot = file_data;
4304 }
4305 }
4306
4307 bool
4308 dwarf2_gdb_index::expand_symtabs_matching
4309 (struct objfile *objfile,
4310 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4311 const lookup_name_info *lookup_name,
4312 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4313 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4314 block_search_flags search_flags,
4315 domain_enum domain,
4316 enum search_domain kind)
4317 {
4318 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4319
4320 /* index_table is NULL if OBJF_READNOW. */
4321 if (!per_objfile->per_bfd->index_table)
4322 return true;
4323
4324 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4325
4326 if (symbol_matcher == NULL && lookup_name == NULL)
4327 {
4328 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4329 {
4330 QUIT;
4331
4332 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
4333 file_matcher,
4334 expansion_notify))
4335 return false;
4336 }
4337 return true;
4338 }
4339
4340 mapped_index &index = *per_objfile->per_bfd->index_table;
4341
4342 bool result
4343 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4344 symbol_matcher,
4345 [&] (offset_type idx)
4346 {
4347 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4348 expansion_notify, search_flags, kind))
4349 return false;
4350 return true;
4351 }, per_objfile);
4352
4353 return result;
4354 }
4355
4356 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4357 symtab. */
4358
4359 static struct compunit_symtab *
4360 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4361 CORE_ADDR pc)
4362 {
4363 int i;
4364
4365 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4366 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4367 return cust;
4368
4369 if (cust->includes == NULL)
4370 return NULL;
4371
4372 for (i = 0; cust->includes[i]; ++i)
4373 {
4374 struct compunit_symtab *s = cust->includes[i];
4375
4376 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4377 if (s != NULL)
4378 return s;
4379 }
4380
4381 return NULL;
4382 }
4383
4384 struct compunit_symtab *
4385 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4386 (struct objfile *objfile,
4387 struct bound_minimal_symbol msymbol,
4388 CORE_ADDR pc,
4389 struct obj_section *section,
4390 int warn_if_readin)
4391 {
4392 struct dwarf2_per_cu_data *data;
4393 struct compunit_symtab *result;
4394
4395 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4396 if (per_objfile->per_bfd->index_addrmap == nullptr)
4397 return NULL;
4398
4399 CORE_ADDR baseaddr = objfile->text_section_offset ();
4400 data = ((struct dwarf2_per_cu_data *)
4401 addrmap_find (per_objfile->per_bfd->index_addrmap,
4402 pc - baseaddr));
4403 if (!data)
4404 return NULL;
4405
4406 if (warn_if_readin && per_objfile->symtab_set_p (data))
4407 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4408 paddress (objfile->arch (), pc));
4409
4410 result = recursively_find_pc_sect_compunit_symtab
4411 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4412
4413 gdb_assert (result != NULL);
4414 return result;
4415 }
4416
4417 void
4418 dwarf2_base_index_functions::map_symbol_filenames
4419 (struct objfile *objfile,
4420 gdb::function_view<symbol_filename_ftype> fun,
4421 bool need_fullname)
4422 {
4423 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4424
4425 /* Use caches to ensure we only call FUN once for each filename. */
4426 filename_seen_cache filenames_cache;
4427 std::unordered_set<quick_file_names *> qfn_cache;
4428
4429 /* The rule is CUs specify all the files, including those used by any TU,
4430 so there's no need to scan TUs here. We can ignore file names coming
4431 from already-expanded CUs. It is possible that an expanded CU might
4432 reuse the file names data from a currently unexpanded CU, in this
4433 case we don't want to report the files from the unexpanded CU. */
4434
4435 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4436 {
4437 if (per_objfile->symtab_set_p (per_cu.get ()))
4438 {
4439 if (per_cu->v.quick->file_names != nullptr)
4440 qfn_cache.insert (per_cu->v.quick->file_names);
4441 }
4442 }
4443
4444 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4445 {
4446 /* We only need to look at symtabs not already expanded. */
4447 if (per_objfile->symtab_set_p (per_cu.get ()))
4448 continue;
4449
4450 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4451 per_objfile);
4452 if (file_data == nullptr
4453 || qfn_cache.find (file_data) != qfn_cache.end ())
4454 continue;
4455
4456 for (int j = 0; j < file_data->num_file_names; ++j)
4457 {
4458 const char *filename = file_data->file_names[j];
4459 filenames_cache.seen (filename);
4460 }
4461 }
4462
4463 filenames_cache.traverse ([&] (const char *filename)
4464 {
4465 gdb::unique_xmalloc_ptr<char> this_real_name;
4466
4467 if (need_fullname)
4468 this_real_name = gdb_realpath (filename);
4469 fun (filename, this_real_name.get ());
4470 });
4471 }
4472
4473 bool
4474 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4475 {
4476 return true;
4477 }
4478
4479 /* DWARF-5 debug_names reader. */
4480
4481 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4482 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4483
4484 /* A helper function that reads the .debug_names section in SECTION
4485 and fills in MAP. FILENAME is the name of the file containing the
4486 section; it is used for error reporting.
4487
4488 Returns true if all went well, false otherwise. */
4489
4490 static bool
4491 read_debug_names_from_section (struct objfile *objfile,
4492 const char *filename,
4493 struct dwarf2_section_info *section,
4494 mapped_debug_names &map)
4495 {
4496 if (section->empty ())
4497 return false;
4498
4499 /* Older elfutils strip versions could keep the section in the main
4500 executable while splitting it for the separate debug info file. */
4501 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4502 return false;
4503
4504 section->read (objfile);
4505
4506 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4507
4508 const gdb_byte *addr = section->buffer;
4509
4510 bfd *const abfd = section->get_bfd_owner ();
4511
4512 unsigned int bytes_read;
4513 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4514 addr += bytes_read;
4515
4516 map.dwarf5_is_dwarf64 = bytes_read != 4;
4517 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4518 if (bytes_read + length != section->size)
4519 {
4520 /* There may be multiple per-CU indices. */
4521 warning (_("Section .debug_names in %s length %s does not match "
4522 "section length %s, ignoring .debug_names."),
4523 filename, plongest (bytes_read + length),
4524 pulongest (section->size));
4525 return false;
4526 }
4527
4528 /* The version number. */
4529 uint16_t version = read_2_bytes (abfd, addr);
4530 addr += 2;
4531 if (version != 5)
4532 {
4533 warning (_("Section .debug_names in %s has unsupported version %d, "
4534 "ignoring .debug_names."),
4535 filename, version);
4536 return false;
4537 }
4538
4539 /* Padding. */
4540 uint16_t padding = read_2_bytes (abfd, addr);
4541 addr += 2;
4542 if (padding != 0)
4543 {
4544 warning (_("Section .debug_names in %s has unsupported padding %d, "
4545 "ignoring .debug_names."),
4546 filename, padding);
4547 return false;
4548 }
4549
4550 /* comp_unit_count - The number of CUs in the CU list. */
4551 map.cu_count = read_4_bytes (abfd, addr);
4552 addr += 4;
4553
4554 /* local_type_unit_count - The number of TUs in the local TU
4555 list. */
4556 map.tu_count = read_4_bytes (abfd, addr);
4557 addr += 4;
4558
4559 /* foreign_type_unit_count - The number of TUs in the foreign TU
4560 list. */
4561 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4562 addr += 4;
4563 if (foreign_tu_count != 0)
4564 {
4565 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4566 "ignoring .debug_names."),
4567 filename, static_cast<unsigned long> (foreign_tu_count));
4568 return false;
4569 }
4570
4571 /* bucket_count - The number of hash buckets in the hash lookup
4572 table. */
4573 map.bucket_count = read_4_bytes (abfd, addr);
4574 addr += 4;
4575
4576 /* name_count - The number of unique names in the index. */
4577 map.name_count = read_4_bytes (abfd, addr);
4578 addr += 4;
4579
4580 /* abbrev_table_size - The size in bytes of the abbreviations
4581 table. */
4582 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4583 addr += 4;
4584
4585 /* augmentation_string_size - The size in bytes of the augmentation
4586 string. This value is rounded up to a multiple of 4. */
4587 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4588 addr += 4;
4589 map.augmentation_is_gdb = ((augmentation_string_size
4590 == sizeof (dwarf5_augmentation))
4591 && memcmp (addr, dwarf5_augmentation,
4592 sizeof (dwarf5_augmentation)) == 0);
4593 augmentation_string_size += (-augmentation_string_size) & 3;
4594 addr += augmentation_string_size;
4595
4596 /* List of CUs */
4597 map.cu_table_reordered = addr;
4598 addr += map.cu_count * map.offset_size;
4599
4600 /* List of Local TUs */
4601 map.tu_table_reordered = addr;
4602 addr += map.tu_count * map.offset_size;
4603
4604 /* Hash Lookup Table */
4605 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4606 addr += map.bucket_count * 4;
4607 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4608 addr += map.name_count * 4;
4609
4610 /* Name Table */
4611 map.name_table_string_offs_reordered = addr;
4612 addr += map.name_count * map.offset_size;
4613 map.name_table_entry_offs_reordered = addr;
4614 addr += map.name_count * map.offset_size;
4615
4616 const gdb_byte *abbrev_table_start = addr;
4617 for (;;)
4618 {
4619 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4620 addr += bytes_read;
4621 if (index_num == 0)
4622 break;
4623
4624 const auto insertpair
4625 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4626 if (!insertpair.second)
4627 {
4628 warning (_("Section .debug_names in %s has duplicate index %s, "
4629 "ignoring .debug_names."),
4630 filename, pulongest (index_num));
4631 return false;
4632 }
4633 mapped_debug_names::index_val &indexval = insertpair.first->second;
4634 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4635 addr += bytes_read;
4636
4637 for (;;)
4638 {
4639 mapped_debug_names::index_val::attr attr;
4640 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4641 addr += bytes_read;
4642 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4643 addr += bytes_read;
4644 if (attr.form == DW_FORM_implicit_const)
4645 {
4646 attr.implicit_const = read_signed_leb128 (abfd, addr,
4647 &bytes_read);
4648 addr += bytes_read;
4649 }
4650 if (attr.dw_idx == 0 && attr.form == 0)
4651 break;
4652 indexval.attr_vec.push_back (std::move (attr));
4653 }
4654 }
4655 if (addr != abbrev_table_start + abbrev_table_size)
4656 {
4657 warning (_("Section .debug_names in %s has abbreviation_table "
4658 "of size %s vs. written as %u, ignoring .debug_names."),
4659 filename, plongest (addr - abbrev_table_start),
4660 abbrev_table_size);
4661 return false;
4662 }
4663 map.entry_pool = addr;
4664
4665 return true;
4666 }
4667
4668 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4669 list. */
4670
4671 static void
4672 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4673 const mapped_debug_names &map,
4674 dwarf2_section_info &section,
4675 bool is_dwz)
4676 {
4677 if (!map.augmentation_is_gdb)
4678 {
4679 for (uint32_t i = 0; i < map.cu_count; ++i)
4680 {
4681 sect_offset sect_off
4682 = (sect_offset) (extract_unsigned_integer
4683 (map.cu_table_reordered + i * map.offset_size,
4684 map.offset_size,
4685 map.dwarf5_byte_order));
4686 /* We don't know the length of the CU, because the CU list in a
4687 .debug_names index can be incomplete, so we can't use the start
4688 of the next CU as end of this CU. We create the CUs here with
4689 length 0, and in cutu_reader::cutu_reader we'll fill in the
4690 actual length. */
4691 dwarf2_per_cu_data_up per_cu
4692 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4693 sect_off, 0);
4694 per_bfd->all_comp_units.push_back (std::move (per_cu));
4695 }
4696 return;
4697 }
4698
4699 sect_offset sect_off_prev;
4700 for (uint32_t i = 0; i <= map.cu_count; ++i)
4701 {
4702 sect_offset sect_off_next;
4703 if (i < map.cu_count)
4704 {
4705 sect_off_next
4706 = (sect_offset) (extract_unsigned_integer
4707 (map.cu_table_reordered + i * map.offset_size,
4708 map.offset_size,
4709 map.dwarf5_byte_order));
4710 }
4711 else
4712 sect_off_next = (sect_offset) section.size;
4713 if (i >= 1)
4714 {
4715 const ULONGEST length = sect_off_next - sect_off_prev;
4716 dwarf2_per_cu_data_up per_cu
4717 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4718 sect_off_prev, length);
4719 per_bfd->all_comp_units.push_back (std::move (per_cu));
4720 }
4721 sect_off_prev = sect_off_next;
4722 }
4723 }
4724
4725 /* Read the CU list from the mapped index, and use it to create all
4726 the CU objects for this dwarf2_per_objfile. */
4727
4728 static void
4729 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4730 const mapped_debug_names &map,
4731 const mapped_debug_names &dwz_map)
4732 {
4733 gdb_assert (per_bfd->all_comp_units.empty ());
4734 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4735
4736 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4737 false /* is_dwz */);
4738
4739 if (dwz_map.cu_count == 0)
4740 return;
4741
4742 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4743 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4744 true /* is_dwz */);
4745 }
4746
4747 /* Read .debug_names. If everything went ok, initialize the "quick"
4748 elements of all the CUs and return true. Otherwise, return false. */
4749
4750 static bool
4751 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4752 {
4753 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4754 mapped_debug_names dwz_map;
4755 struct objfile *objfile = per_objfile->objfile;
4756 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4757
4758 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4759 &per_bfd->debug_names, *map))
4760 return false;
4761
4762 /* Don't use the index if it's empty. */
4763 if (map->name_count == 0)
4764 return false;
4765
4766 /* If there is a .dwz file, read it so we can get its CU list as
4767 well. */
4768 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4769 if (dwz != NULL)
4770 {
4771 if (!read_debug_names_from_section (objfile,
4772 bfd_get_filename (dwz->dwz_bfd.get ()),
4773 &dwz->debug_names, dwz_map))
4774 {
4775 warning (_("could not read '.debug_names' section from %s; skipping"),
4776 bfd_get_filename (dwz->dwz_bfd.get ()));
4777 return false;
4778 }
4779 }
4780
4781 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4782
4783 if (map->tu_count != 0)
4784 {
4785 /* We can only handle a single .debug_types when we have an
4786 index. */
4787 if (per_bfd->types.size () != 1)
4788 return false;
4789
4790 dwarf2_section_info *section = &per_bfd->types[0];
4791
4792 create_signatured_type_table_from_debug_names
4793 (per_objfile, *map, section, &per_bfd->abbrev);
4794 }
4795
4796 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4797
4798 per_bfd->debug_names_table = std::move (map);
4799 per_bfd->using_index = 1;
4800 per_bfd->quick_file_names_table =
4801 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4802
4803 return true;
4804 }
4805
4806 /* Type used to manage iterating over all CUs looking for a symbol for
4807 .debug_names. */
4808
4809 class dw2_debug_names_iterator
4810 {
4811 public:
4812 dw2_debug_names_iterator (const mapped_debug_names &map,
4813 block_search_flags block_index,
4814 domain_enum domain,
4815 const char *name, dwarf2_per_objfile *per_objfile)
4816 : m_map (map), m_block_index (block_index), m_domain (domain),
4817 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4818 m_per_objfile (per_objfile)
4819 {}
4820
4821 dw2_debug_names_iterator (const mapped_debug_names &map,
4822 search_domain search, uint32_t namei,
4823 dwarf2_per_objfile *per_objfile,
4824 domain_enum domain = UNDEF_DOMAIN)
4825 : m_map (map),
4826 m_domain (domain),
4827 m_search (search),
4828 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4829 m_per_objfile (per_objfile)
4830 {}
4831
4832 dw2_debug_names_iterator (const mapped_debug_names &map,
4833 block_search_flags block_index, domain_enum domain,
4834 uint32_t namei, dwarf2_per_objfile *per_objfile)
4835 : m_map (map), m_block_index (block_index), m_domain (domain),
4836 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4837 m_per_objfile (per_objfile)
4838 {}
4839
4840 /* Return the next matching CU or NULL if there are no more. */
4841 dwarf2_per_cu_data *next ();
4842
4843 private:
4844 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4845 const char *name,
4846 dwarf2_per_objfile *per_objfile);
4847 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4848 uint32_t namei,
4849 dwarf2_per_objfile *per_objfile);
4850
4851 /* The internalized form of .debug_names. */
4852 const mapped_debug_names &m_map;
4853
4854 /* Restrict the search to these blocks. */
4855 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4856 | SEARCH_STATIC_BLOCK);
4857
4858 /* The kind of symbol we're looking for. */
4859 const domain_enum m_domain = UNDEF_DOMAIN;
4860 const search_domain m_search = ALL_DOMAIN;
4861
4862 /* The list of CUs from the index entry of the symbol, or NULL if
4863 not found. */
4864 const gdb_byte *m_addr;
4865
4866 dwarf2_per_objfile *m_per_objfile;
4867 };
4868
4869 const char *
4870 mapped_debug_names::namei_to_name
4871 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4872 {
4873 const ULONGEST namei_string_offs
4874 = extract_unsigned_integer ((name_table_string_offs_reordered
4875 + namei * offset_size),
4876 offset_size,
4877 dwarf5_byte_order);
4878 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4879 }
4880
4881 /* Find a slot in .debug_names for the object named NAME. If NAME is
4882 found, return pointer to its pool data. If NAME cannot be found,
4883 return NULL. */
4884
4885 const gdb_byte *
4886 dw2_debug_names_iterator::find_vec_in_debug_names
4887 (const mapped_debug_names &map, const char *name,
4888 dwarf2_per_objfile *per_objfile)
4889 {
4890 int (*cmp) (const char *, const char *);
4891
4892 gdb::unique_xmalloc_ptr<char> without_params;
4893 if (current_language->la_language == language_cplus
4894 || current_language->la_language == language_fortran
4895 || current_language->la_language == language_d)
4896 {
4897 /* NAME is already canonical. Drop any qualifiers as
4898 .debug_names does not contain any. */
4899
4900 if (strchr (name, '(') != NULL)
4901 {
4902 without_params = cp_remove_params (name);
4903 if (without_params != NULL)
4904 name = without_params.get ();
4905 }
4906 }
4907
4908 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4909
4910 const uint32_t full_hash = dwarf5_djb_hash (name);
4911 uint32_t namei
4912 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4913 (map.bucket_table_reordered
4914 + (full_hash % map.bucket_count)), 4,
4915 map.dwarf5_byte_order);
4916 if (namei == 0)
4917 return NULL;
4918 --namei;
4919 if (namei >= map.name_count)
4920 {
4921 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4922 "[in module %s]"),
4923 namei, map.name_count,
4924 objfile_name (per_objfile->objfile));
4925 return NULL;
4926 }
4927
4928 for (;;)
4929 {
4930 const uint32_t namei_full_hash
4931 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4932 (map.hash_table_reordered + namei), 4,
4933 map.dwarf5_byte_order);
4934 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4935 return NULL;
4936
4937 if (full_hash == namei_full_hash)
4938 {
4939 const char *const namei_string = map.namei_to_name (namei, per_objfile);
4940
4941 #if 0 /* An expensive sanity check. */
4942 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4943 {
4944 complaint (_("Wrong .debug_names hash for string at index %u "
4945 "[in module %s]"),
4946 namei, objfile_name (dwarf2_per_objfile->objfile));
4947 return NULL;
4948 }
4949 #endif
4950
4951 if (cmp (namei_string, name) == 0)
4952 {
4953 const ULONGEST namei_entry_offs
4954 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4955 + namei * map.offset_size),
4956 map.offset_size, map.dwarf5_byte_order);
4957 return map.entry_pool + namei_entry_offs;
4958 }
4959 }
4960
4961 ++namei;
4962 if (namei >= map.name_count)
4963 return NULL;
4964 }
4965 }
4966
4967 const gdb_byte *
4968 dw2_debug_names_iterator::find_vec_in_debug_names
4969 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
4970 {
4971 if (namei >= map.name_count)
4972 {
4973 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4974 "[in module %s]"),
4975 namei, map.name_count,
4976 objfile_name (per_objfile->objfile));
4977 return NULL;
4978 }
4979
4980 const ULONGEST namei_entry_offs
4981 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4982 + namei * map.offset_size),
4983 map.offset_size, map.dwarf5_byte_order);
4984 return map.entry_pool + namei_entry_offs;
4985 }
4986
4987 /* See dw2_debug_names_iterator. */
4988
4989 dwarf2_per_cu_data *
4990 dw2_debug_names_iterator::next ()
4991 {
4992 if (m_addr == NULL)
4993 return NULL;
4994
4995 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4996 struct objfile *objfile = m_per_objfile->objfile;
4997 bfd *const abfd = objfile->obfd;
4998
4999 again:
5000
5001 unsigned int bytes_read;
5002 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5003 m_addr += bytes_read;
5004 if (abbrev == 0)
5005 return NULL;
5006
5007 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5008 if (indexval_it == m_map.abbrev_map.cend ())
5009 {
5010 complaint (_("Wrong .debug_names undefined abbrev code %s "
5011 "[in module %s]"),
5012 pulongest (abbrev), objfile_name (objfile));
5013 return NULL;
5014 }
5015 const mapped_debug_names::index_val &indexval = indexval_it->second;
5016 enum class symbol_linkage {
5017 unknown,
5018 static_,
5019 extern_,
5020 } symbol_linkage_ = symbol_linkage::unknown;
5021 dwarf2_per_cu_data *per_cu = NULL;
5022 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5023 {
5024 ULONGEST ull;
5025 switch (attr.form)
5026 {
5027 case DW_FORM_implicit_const:
5028 ull = attr.implicit_const;
5029 break;
5030 case DW_FORM_flag_present:
5031 ull = 1;
5032 break;
5033 case DW_FORM_udata:
5034 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5035 m_addr += bytes_read;
5036 break;
5037 case DW_FORM_ref4:
5038 ull = read_4_bytes (abfd, m_addr);
5039 m_addr += 4;
5040 break;
5041 case DW_FORM_ref8:
5042 ull = read_8_bytes (abfd, m_addr);
5043 m_addr += 8;
5044 break;
5045 case DW_FORM_ref_sig8:
5046 ull = read_8_bytes (abfd, m_addr);
5047 m_addr += 8;
5048 break;
5049 default:
5050 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5051 dwarf_form_name (attr.form),
5052 objfile_name (objfile));
5053 return NULL;
5054 }
5055 switch (attr.dw_idx)
5056 {
5057 case DW_IDX_compile_unit:
5058 /* Don't crash on bad data. */
5059 if (ull >= per_bfd->all_comp_units.size ())
5060 {
5061 complaint (_(".debug_names entry has bad CU index %s"
5062 " [in module %s]"),
5063 pulongest (ull),
5064 objfile_name (objfile));
5065 continue;
5066 }
5067 per_cu = per_bfd->get_cu (ull);
5068 break;
5069 case DW_IDX_type_unit:
5070 /* Don't crash on bad data. */
5071 if (ull >= per_bfd->tu_stats.nr_tus)
5072 {
5073 complaint (_(".debug_names entry has bad TU index %s"
5074 " [in module %s]"),
5075 pulongest (ull),
5076 objfile_name (objfile));
5077 continue;
5078 }
5079 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5080 break;
5081 case DW_IDX_die_offset:
5082 /* In a per-CU index (as opposed to a per-module index), index
5083 entries without CU attribute implicitly refer to the single CU. */
5084 if (per_cu == NULL)
5085 per_cu = per_bfd->get_cu (0);
5086 break;
5087 case DW_IDX_GNU_internal:
5088 if (!m_map.augmentation_is_gdb)
5089 break;
5090 symbol_linkage_ = symbol_linkage::static_;
5091 break;
5092 case DW_IDX_GNU_external:
5093 if (!m_map.augmentation_is_gdb)
5094 break;
5095 symbol_linkage_ = symbol_linkage::extern_;
5096 break;
5097 }
5098 }
5099
5100 /* Skip if already read in. */
5101 if (m_per_objfile->symtab_set_p (per_cu))
5102 goto again;
5103
5104 /* Check static vs global. */
5105 if (symbol_linkage_ != symbol_linkage::unknown)
5106 {
5107 if (symbol_linkage_ == symbol_linkage::static_)
5108 {
5109 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5110 goto again;
5111 }
5112 else
5113 {
5114 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5115 goto again;
5116 }
5117 }
5118
5119 /* Match dw2_symtab_iter_next, symbol_kind
5120 and debug_names::psymbol_tag. */
5121 switch (m_domain)
5122 {
5123 case VAR_DOMAIN:
5124 switch (indexval.dwarf_tag)
5125 {
5126 case DW_TAG_variable:
5127 case DW_TAG_subprogram:
5128 /* Some types are also in VAR_DOMAIN. */
5129 case DW_TAG_typedef:
5130 case DW_TAG_structure_type:
5131 break;
5132 default:
5133 goto again;
5134 }
5135 break;
5136 case STRUCT_DOMAIN:
5137 switch (indexval.dwarf_tag)
5138 {
5139 case DW_TAG_typedef:
5140 case DW_TAG_structure_type:
5141 break;
5142 default:
5143 goto again;
5144 }
5145 break;
5146 case LABEL_DOMAIN:
5147 switch (indexval.dwarf_tag)
5148 {
5149 case 0:
5150 case DW_TAG_variable:
5151 break;
5152 default:
5153 goto again;
5154 }
5155 break;
5156 case MODULE_DOMAIN:
5157 switch (indexval.dwarf_tag)
5158 {
5159 case DW_TAG_module:
5160 break;
5161 default:
5162 goto again;
5163 }
5164 break;
5165 default:
5166 break;
5167 }
5168
5169 /* Match dw2_expand_symtabs_matching, symbol_kind and
5170 debug_names::psymbol_tag. */
5171 switch (m_search)
5172 {
5173 case VARIABLES_DOMAIN:
5174 switch (indexval.dwarf_tag)
5175 {
5176 case DW_TAG_variable:
5177 break;
5178 default:
5179 goto again;
5180 }
5181 break;
5182 case FUNCTIONS_DOMAIN:
5183 switch (indexval.dwarf_tag)
5184 {
5185 case DW_TAG_subprogram:
5186 break;
5187 default:
5188 goto again;
5189 }
5190 break;
5191 case TYPES_DOMAIN:
5192 switch (indexval.dwarf_tag)
5193 {
5194 case DW_TAG_typedef:
5195 case DW_TAG_structure_type:
5196 break;
5197 default:
5198 goto again;
5199 }
5200 break;
5201 case MODULES_DOMAIN:
5202 switch (indexval.dwarf_tag)
5203 {
5204 case DW_TAG_module:
5205 break;
5206 default:
5207 goto again;
5208 }
5209 default:
5210 break;
5211 }
5212
5213 return per_cu;
5214 }
5215
5216 /* This dumps minimal information about .debug_names. It is called
5217 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5218 uses this to verify that .debug_names has been loaded. */
5219
5220 void
5221 dwarf2_debug_names_index::dump (struct objfile *objfile)
5222 {
5223 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5224
5225 gdb_assert (per_objfile->per_bfd->using_index);
5226 printf_filtered (".debug_names:");
5227 if (per_objfile->per_bfd->debug_names_table)
5228 printf_filtered (" exists\n");
5229 else
5230 printf_filtered (" faked for \"readnow\"\n");
5231 printf_filtered ("\n");
5232 }
5233
5234 void
5235 dwarf2_debug_names_index::expand_matching_symbols
5236 (struct objfile *objfile,
5237 const lookup_name_info &name, domain_enum domain,
5238 int global,
5239 symbol_compare_ftype *ordered_compare)
5240 {
5241 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5242
5243 /* debug_names_table is NULL if OBJF_READNOW. */
5244 if (!per_objfile->per_bfd->debug_names_table)
5245 return;
5246
5247 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5248 const block_search_flags block_flags
5249 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5250
5251 const char *match_name = name.ada ().lookup_name ().c_str ();
5252 auto matcher = [&] (const char *symname)
5253 {
5254 if (ordered_compare == nullptr)
5255 return true;
5256 return ordered_compare (symname, match_name) == 0;
5257 };
5258
5259 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5260 [&] (offset_type namei)
5261 {
5262 /* The name was matched, now expand corresponding CUs that were
5263 marked. */
5264 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5265 per_objfile);
5266
5267 struct dwarf2_per_cu_data *per_cu;
5268 while ((per_cu = iter.next ()) != NULL)
5269 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5270 nullptr);
5271 return true;
5272 }, per_objfile);
5273 }
5274
5275 bool
5276 dwarf2_debug_names_index::expand_symtabs_matching
5277 (struct objfile *objfile,
5278 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5279 const lookup_name_info *lookup_name,
5280 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5281 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5282 block_search_flags search_flags,
5283 domain_enum domain,
5284 enum search_domain kind)
5285 {
5286 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5287
5288 /* debug_names_table is NULL if OBJF_READNOW. */
5289 if (!per_objfile->per_bfd->debug_names_table)
5290 return true;
5291
5292 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5293
5294 if (symbol_matcher == NULL && lookup_name == NULL)
5295 {
5296 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
5297 {
5298 QUIT;
5299
5300 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
5301 file_matcher,
5302 expansion_notify))
5303 return false;
5304 }
5305 return true;
5306 }
5307
5308 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5309
5310 bool result
5311 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5312 symbol_matcher,
5313 [&] (offset_type namei)
5314 {
5315 /* The name was matched, now expand corresponding CUs that were
5316 marked. */
5317 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5318
5319 struct dwarf2_per_cu_data *per_cu;
5320 while ((per_cu = iter.next ()) != NULL)
5321 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5322 file_matcher,
5323 expansion_notify))
5324 return false;
5325 return true;
5326 }, per_objfile);
5327
5328 return result;
5329 }
5330
5331 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5332 to either a dwarf2_per_bfd or dwz_file object. */
5333
5334 template <typename T>
5335 static gdb::array_view<const gdb_byte>
5336 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5337 {
5338 dwarf2_section_info *section = &section_owner->gdb_index;
5339
5340 if (section->empty ())
5341 return {};
5342
5343 /* Older elfutils strip versions could keep the section in the main
5344 executable while splitting it for the separate debug info file. */
5345 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5346 return {};
5347
5348 section->read (obj);
5349
5350 /* dwarf2_section_info::size is a bfd_size_type, while
5351 gdb::array_view works with size_t. On 32-bit hosts, with
5352 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5353 is 32-bit. So we need an explicit narrowing conversion here.
5354 This is fine, because it's impossible to allocate or mmap an
5355 array/buffer larger than what size_t can represent. */
5356 return gdb::make_array_view (section->buffer, section->size);
5357 }
5358
5359 /* Lookup the index cache for the contents of the index associated to
5360 DWARF2_OBJ. */
5361
5362 static gdb::array_view<const gdb_byte>
5363 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5364 {
5365 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5366 if (build_id == nullptr)
5367 return {};
5368
5369 return global_index_cache.lookup_gdb_index (build_id,
5370 &dwarf2_per_bfd->index_cache_res);
5371 }
5372
5373 /* Same as the above, but for DWZ. */
5374
5375 static gdb::array_view<const gdb_byte>
5376 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5377 {
5378 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5379 if (build_id == nullptr)
5380 return {};
5381
5382 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5383 }
5384
5385 /* See dwarf2/public.h. */
5386
5387 void
5388 dwarf2_initialize_objfile (struct objfile *objfile)
5389 {
5390 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5391 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5392
5393 dwarf_read_debug_printf ("called");
5394
5395 /* If we're about to read full symbols, don't bother with the
5396 indices. In this case we also don't care if some other debug
5397 format is making psymtabs, because they are all about to be
5398 expanded anyway. */
5399 if ((objfile->flags & OBJF_READNOW))
5400 {
5401 dwarf_read_debug_printf ("readnow requested");
5402
5403 /* When using READNOW, the using_index flag (set below) indicates that
5404 PER_BFD was already initialized, when we loaded some other objfile. */
5405 if (per_bfd->using_index)
5406 {
5407 dwarf_read_debug_printf ("using_index already set");
5408 per_objfile->resize_symtabs ();
5409 objfile->qf.push_front (make_dwarf_gdb_index ());
5410 return;
5411 }
5412
5413 per_bfd->using_index = 1;
5414 create_all_comp_units (per_objfile);
5415 per_bfd->quick_file_names_table
5416 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5417 per_objfile->resize_symtabs ();
5418
5419 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5420 {
5421 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5422
5423 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5424 struct dwarf2_per_cu_quick_data);
5425 }
5426
5427 /* Arrange for gdb to see the "quick" functions. However, these
5428 functions will be no-ops because we will have expanded all
5429 symtabs. */
5430 objfile->qf.push_front (make_dwarf_gdb_index ());
5431 return;
5432 }
5433
5434 /* Was a debug names index already read when we processed an objfile sharing
5435 PER_BFD? */
5436 if (per_bfd->debug_names_table != nullptr)
5437 {
5438 dwarf_read_debug_printf ("re-using shared debug names table");
5439 per_objfile->resize_symtabs ();
5440 objfile->qf.push_front (make_dwarf_debug_names ());
5441 return;
5442 }
5443
5444 /* Was a GDB index already read when we processed an objfile sharing
5445 PER_BFD? */
5446 if (per_bfd->index_table != nullptr)
5447 {
5448 dwarf_read_debug_printf ("re-using shared index table");
5449 per_objfile->resize_symtabs ();
5450 objfile->qf.push_front (make_dwarf_gdb_index ());
5451 return;
5452 }
5453
5454 /* There might already be partial symtabs built for this BFD. This happens
5455 when loading the same binary twice with the index-cache enabled. If so,
5456 don't try to read an index. The objfile / per_objfile initialization will
5457 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5458 code path. */
5459 if (per_bfd->partial_symtabs != nullptr)
5460 {
5461 dwarf_read_debug_printf ("re-using shared partial symtabs");
5462 objfile->qf.push_front (make_lazy_dwarf_reader ());
5463 return;
5464 }
5465
5466 if (dwarf2_read_debug_names (per_objfile))
5467 {
5468 dwarf_read_debug_printf ("found debug names");
5469 per_objfile->resize_symtabs ();
5470 objfile->qf.push_front (make_dwarf_debug_names ());
5471 return;
5472 }
5473
5474 if (dwarf2_read_gdb_index (per_objfile,
5475 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5476 get_gdb_index_contents_from_section<dwz_file>))
5477 {
5478 dwarf_read_debug_printf ("found gdb index from file");
5479 per_objfile->resize_symtabs ();
5480 objfile->qf.push_front (make_dwarf_gdb_index ());
5481 return;
5482 }
5483
5484 /* ... otherwise, try to find the index in the index cache. */
5485 if (dwarf2_read_gdb_index (per_objfile,
5486 get_gdb_index_contents_from_cache,
5487 get_gdb_index_contents_from_cache_dwz))
5488 {
5489 dwarf_read_debug_printf ("found gdb index from cache");
5490 global_index_cache.hit ();
5491 per_objfile->resize_symtabs ();
5492 objfile->qf.push_front (make_dwarf_gdb_index ());
5493 return;
5494 }
5495
5496 global_index_cache.miss ();
5497 objfile->qf.push_front (make_lazy_dwarf_reader ());
5498 }
5499
5500 \f
5501
5502 /* Build a partial symbol table. */
5503
5504 void
5505 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5506 {
5507 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5508 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5509
5510 if (per_bfd->partial_symtabs != nullptr)
5511 {
5512 /* Partial symbols were already read, so now we can simply
5513 attach them. */
5514 if (psf == nullptr)
5515 {
5516 psf = new psymbol_functions (per_bfd->partial_symtabs);
5517 objfile->qf.emplace_front (psf);
5518 }
5519 else
5520 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5521 per_objfile->resize_symtabs ();
5522 return;
5523 }
5524
5525 if (psf == nullptr)
5526 {
5527 psf = new psymbol_functions;
5528 objfile->qf.emplace_front (psf);
5529 }
5530 const std::shared_ptr<psymtab_storage> &partial_symtabs
5531 = psf->get_partial_symtabs ();
5532
5533 /* Set the local reference to partial symtabs, so that we don't try
5534 to read them again if reading another objfile with the same BFD.
5535 If we can't in fact share, this won't make a difference anyway as
5536 the dwarf2_per_bfd object won't be shared. */
5537 per_bfd->partial_symtabs = partial_symtabs;
5538
5539 try
5540 {
5541 /* This isn't really ideal: all the data we allocate on the
5542 objfile's obstack is still uselessly kept around. However,
5543 freeing it seems unsafe. */
5544 psymtab_discarder psymtabs (partial_symtabs.get ());
5545 dwarf2_build_psymtabs_hard (per_objfile);
5546 psymtabs.keep ();
5547
5548 per_objfile->resize_symtabs ();
5549
5550 /* (maybe) store an index in the cache. */
5551 global_index_cache.store (per_objfile);
5552 }
5553 catch (const gdb_exception_error &except)
5554 {
5555 exception_print (gdb_stderr, except);
5556 }
5557 }
5558
5559 /* Find the base address of the compilation unit for range lists and
5560 location lists. It will normally be specified by DW_AT_low_pc.
5561 In DWARF-3 draft 4, the base address could be overridden by
5562 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5563 compilation units with discontinuous ranges. */
5564
5565 static void
5566 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5567 {
5568 struct attribute *attr;
5569
5570 cu->base_address.reset ();
5571
5572 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5573 if (attr != nullptr)
5574 cu->base_address = attr->as_address ();
5575 else
5576 {
5577 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5578 if (attr != nullptr)
5579 cu->base_address = attr->as_address ();
5580 }
5581 }
5582
5583 /* Helper function that returns the proper abbrev section for
5584 THIS_CU. */
5585
5586 static struct dwarf2_section_info *
5587 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5588 {
5589 struct dwarf2_section_info *abbrev;
5590 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5591
5592 if (this_cu->is_dwz)
5593 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5594 else
5595 abbrev = &per_bfd->abbrev;
5596
5597 return abbrev;
5598 }
5599
5600 /* Fetch the abbreviation table offset from a comp or type unit header. */
5601
5602 static sect_offset
5603 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5604 struct dwarf2_section_info *section,
5605 sect_offset sect_off)
5606 {
5607 bfd *abfd = section->get_bfd_owner ();
5608 const gdb_byte *info_ptr;
5609 unsigned int initial_length_size, offset_size;
5610 uint16_t version;
5611
5612 section->read (per_objfile->objfile);
5613 info_ptr = section->buffer + to_underlying (sect_off);
5614 read_initial_length (abfd, info_ptr, &initial_length_size);
5615 offset_size = initial_length_size == 4 ? 4 : 8;
5616 info_ptr += initial_length_size;
5617
5618 version = read_2_bytes (abfd, info_ptr);
5619 info_ptr += 2;
5620 if (version >= 5)
5621 {
5622 /* Skip unit type and address size. */
5623 info_ptr += 2;
5624 }
5625
5626 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5627 }
5628
5629 /* A partial symtab that is used only for include files. */
5630 struct dwarf2_include_psymtab : public partial_symtab
5631 {
5632 dwarf2_include_psymtab (const char *filename,
5633 psymtab_storage *partial_symtabs,
5634 objfile_per_bfd_storage *objfile_per_bfd)
5635 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5636 {
5637 }
5638
5639 void read_symtab (struct objfile *objfile) override
5640 {
5641 /* It's an include file, no symbols to read for it.
5642 Everything is in the includer symtab. */
5643
5644 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5645 expansion of the includer psymtab. We use the dependencies[0] field to
5646 model the includer. But if we go the regular route of calling
5647 expand_psymtab here, and having expand_psymtab call expand_dependencies
5648 to expand the includer, we'll only use expand_psymtab on the includer
5649 (making it a non-toplevel psymtab), while if we expand the includer via
5650 another path, we'll use read_symtab (making it a toplevel psymtab).
5651 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5652 psymtab, and trigger read_symtab on the includer here directly. */
5653 includer ()->read_symtab (objfile);
5654 }
5655
5656 void expand_psymtab (struct objfile *objfile) override
5657 {
5658 /* This is not called by read_symtab, and should not be called by any
5659 expand_dependencies. */
5660 gdb_assert (false);
5661 }
5662
5663 bool readin_p (struct objfile *objfile) const override
5664 {
5665 return includer ()->readin_p (objfile);
5666 }
5667
5668 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5669 {
5670 return nullptr;
5671 }
5672
5673 private:
5674 partial_symtab *includer () const
5675 {
5676 /* An include psymtab has exactly one dependency: the psymtab that
5677 includes it. */
5678 gdb_assert (this->number_of_dependencies == 1);
5679 return this->dependencies[0];
5680 }
5681 };
5682
5683 /* Allocate a new partial symtab for file named NAME and mark this new
5684 partial symtab as being an include of PST. */
5685
5686 static void
5687 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5688 const char *name,
5689 dwarf2_psymtab *pst,
5690 psymtab_storage *partial_symtabs,
5691 objfile_per_bfd_storage *objfile_per_bfd)
5692 {
5693 dwarf2_include_psymtab *subpst
5694 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5695
5696 if (!IS_ABSOLUTE_PATH (subpst->filename))
5697 subpst->dirname = pst->dirname;
5698
5699 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5700 subpst->dependencies[0] = pst;
5701 subpst->number_of_dependencies = 1;
5702 }
5703
5704 /* Read the Line Number Program data and extract the list of files
5705 included by the source file represented by PST. Build an include
5706 partial symtab for each of these included files. */
5707
5708 static void
5709 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5710 struct die_info *die,
5711 dwarf2_psymtab *pst)
5712 {
5713 line_header_up lh;
5714 struct attribute *attr;
5715
5716 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5717 if (attr != nullptr && attr->form_is_unsigned ())
5718 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5719 if (lh == NULL)
5720 return; /* No linetable, so no includes. */
5721
5722 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5723 that we pass in the raw text_low here; that is ok because we're
5724 only decoding the line table to make include partial symtabs, and
5725 so the addresses aren't really used. */
5726 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5727 pst->raw_text_low (), 1);
5728 }
5729
5730 static hashval_t
5731 hash_signatured_type (const void *item)
5732 {
5733 const struct signatured_type *sig_type
5734 = (const struct signatured_type *) item;
5735
5736 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5737 return sig_type->signature;
5738 }
5739
5740 static int
5741 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5742 {
5743 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5744 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5745
5746 return lhs->signature == rhs->signature;
5747 }
5748
5749 /* Allocate a hash table for signatured types. */
5750
5751 static htab_up
5752 allocate_signatured_type_table ()
5753 {
5754 return htab_up (htab_create_alloc (41,
5755 hash_signatured_type,
5756 eq_signatured_type,
5757 NULL, xcalloc, xfree));
5758 }
5759
5760 /* A helper for create_debug_types_hash_table. Read types from SECTION
5761 and fill them into TYPES_HTAB. It will process only type units,
5762 therefore DW_UT_type. */
5763
5764 static void
5765 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5766 struct dwo_file *dwo_file,
5767 dwarf2_section_info *section, htab_up &types_htab,
5768 rcuh_kind section_kind)
5769 {
5770 struct objfile *objfile = per_objfile->objfile;
5771 struct dwarf2_section_info *abbrev_section;
5772 bfd *abfd;
5773 const gdb_byte *info_ptr, *end_ptr;
5774
5775 abbrev_section = &dwo_file->sections.abbrev;
5776
5777 dwarf_read_debug_printf ("Reading %s for %s",
5778 section->get_name (),
5779 abbrev_section->get_file_name ());
5780
5781 section->read (objfile);
5782 info_ptr = section->buffer;
5783
5784 if (info_ptr == NULL)
5785 return;
5786
5787 /* We can't set abfd until now because the section may be empty or
5788 not present, in which case the bfd is unknown. */
5789 abfd = section->get_bfd_owner ();
5790
5791 /* We don't use cutu_reader here because we don't need to read
5792 any dies: the signature is in the header. */
5793
5794 end_ptr = info_ptr + section->size;
5795 while (info_ptr < end_ptr)
5796 {
5797 signatured_type_up sig_type;
5798 struct dwo_unit *dwo_tu;
5799 void **slot;
5800 const gdb_byte *ptr = info_ptr;
5801 struct comp_unit_head header;
5802 unsigned int length;
5803
5804 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5805
5806 /* Initialize it due to a false compiler warning. */
5807 header.signature = -1;
5808 header.type_cu_offset_in_tu = (cu_offset) -1;
5809
5810 /* We need to read the type's signature in order to build the hash
5811 table, but we don't need anything else just yet. */
5812
5813 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5814 abbrev_section, ptr, section_kind);
5815
5816 length = header.get_length ();
5817
5818 /* Skip dummy type units. */
5819 if (ptr >= info_ptr + length
5820 || peek_abbrev_code (abfd, ptr) == 0
5821 || (header.unit_type != DW_UT_type
5822 && header.unit_type != DW_UT_split_type))
5823 {
5824 info_ptr += length;
5825 continue;
5826 }
5827
5828 if (types_htab == NULL)
5829 types_htab = allocate_dwo_unit_table ();
5830
5831 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5832 dwo_tu->dwo_file = dwo_file;
5833 dwo_tu->signature = header.signature;
5834 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5835 dwo_tu->section = section;
5836 dwo_tu->sect_off = sect_off;
5837 dwo_tu->length = length;
5838
5839 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5840 gdb_assert (slot != NULL);
5841 if (*slot != NULL)
5842 complaint (_("debug type entry at offset %s is duplicate to"
5843 " the entry at offset %s, signature %s"),
5844 sect_offset_str (sect_off),
5845 sect_offset_str (dwo_tu->sect_off),
5846 hex_string (header.signature));
5847 *slot = dwo_tu;
5848
5849 dwarf_read_debug_printf_v (" offset %s, signature %s",
5850 sect_offset_str (sect_off),
5851 hex_string (header.signature));
5852
5853 info_ptr += length;
5854 }
5855 }
5856
5857 /* Create the hash table of all entries in the .debug_types
5858 (or .debug_types.dwo) section(s).
5859 DWO_FILE is a pointer to the DWO file object.
5860
5861 The result is a pointer to the hash table or NULL if there are no types.
5862
5863 Note: This function processes DWO files only, not DWP files. */
5864
5865 static void
5866 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5867 struct dwo_file *dwo_file,
5868 gdb::array_view<dwarf2_section_info> type_sections,
5869 htab_up &types_htab)
5870 {
5871 for (dwarf2_section_info &section : type_sections)
5872 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5873 rcuh_kind::TYPE);
5874 }
5875
5876 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5877 If SLOT is non-NULL, it is the entry to use in the hash table.
5878 Otherwise we find one. */
5879
5880 static struct signatured_type *
5881 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5882 {
5883 if (per_objfile->per_bfd->all_comp_units.size ()
5884 == per_objfile->per_bfd->all_comp_units.capacity ())
5885 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5886
5887 signatured_type_up sig_type_holder
5888 = per_objfile->per_bfd->allocate_signatured_type (sig);
5889 signatured_type *sig_type = sig_type_holder.get ();
5890
5891 per_objfile->resize_symtabs ();
5892
5893 per_objfile->per_bfd->all_comp_units.emplace_back
5894 (sig_type_holder.release ());
5895 if (per_objfile->per_bfd->using_index)
5896 {
5897 sig_type->v.quick =
5898 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
5899 struct dwarf2_per_cu_quick_data);
5900 }
5901
5902 if (slot == NULL)
5903 {
5904 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5905 sig_type, INSERT);
5906 }
5907 gdb_assert (*slot == NULL);
5908 *slot = sig_type;
5909 /* The rest of sig_type must be filled in by the caller. */
5910 return sig_type;
5911 }
5912
5913 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5914 Fill in SIG_ENTRY with DWO_ENTRY. */
5915
5916 static void
5917 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
5918 struct signatured_type *sig_entry,
5919 struct dwo_unit *dwo_entry)
5920 {
5921 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5922
5923 /* Make sure we're not clobbering something we don't expect to. */
5924 gdb_assert (! sig_entry->queued);
5925 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
5926 if (per_bfd->using_index)
5927 {
5928 gdb_assert (sig_entry->v.quick != NULL);
5929 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
5930 }
5931 else
5932 gdb_assert (sig_entry->v.psymtab == NULL);
5933 gdb_assert (sig_entry->signature == dwo_entry->signature);
5934 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5935 gdb_assert (sig_entry->type_unit_group == NULL);
5936 gdb_assert (sig_entry->dwo_unit == NULL);
5937
5938 sig_entry->section = dwo_entry->section;
5939 sig_entry->sect_off = dwo_entry->sect_off;
5940 sig_entry->length = dwo_entry->length;
5941 sig_entry->reading_dwo_directly = 1;
5942 sig_entry->per_bfd = per_bfd;
5943 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5944 sig_entry->dwo_unit = dwo_entry;
5945 }
5946
5947 /* Subroutine of lookup_signatured_type.
5948 If we haven't read the TU yet, create the signatured_type data structure
5949 for a TU to be read in directly from a DWO file, bypassing the stub.
5950 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5951 using .gdb_index, then when reading a CU we want to stay in the DWO file
5952 containing that CU. Otherwise we could end up reading several other DWO
5953 files (due to comdat folding) to process the transitive closure of all the
5954 mentioned TUs, and that can be slow. The current DWO file will have every
5955 type signature that it needs.
5956 We only do this for .gdb_index because in the psymtab case we already have
5957 to read all the DWOs to build the type unit groups. */
5958
5959 static struct signatured_type *
5960 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5961 {
5962 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5963 struct dwo_file *dwo_file;
5964 struct dwo_unit find_dwo_entry, *dwo_entry;
5965 void **slot;
5966
5967 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
5968
5969 /* If TU skeletons have been removed then we may not have read in any
5970 TUs yet. */
5971 if (per_objfile->per_bfd->signatured_types == NULL)
5972 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5973
5974 /* We only ever need to read in one copy of a signatured type.
5975 Use the global signatured_types array to do our own comdat-folding
5976 of types. If this is the first time we're reading this TU, and
5977 the TU has an entry in .gdb_index, replace the recorded data from
5978 .gdb_index with this TU. */
5979
5980 signatured_type find_sig_entry (sig);
5981 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5982 &find_sig_entry, INSERT);
5983 signatured_type *sig_entry = (struct signatured_type *) *slot;
5984
5985 /* We can get here with the TU already read, *or* in the process of being
5986 read. Don't reassign the global entry to point to this DWO if that's
5987 the case. Also note that if the TU is already being read, it may not
5988 have come from a DWO, the program may be a mix of Fission-compiled
5989 code and non-Fission-compiled code. */
5990
5991 /* Have we already tried to read this TU?
5992 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5993 needn't exist in the global table yet). */
5994 if (sig_entry != NULL && sig_entry->tu_read)
5995 return sig_entry;
5996
5997 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5998 dwo_unit of the TU itself. */
5999 dwo_file = cu->dwo_unit->dwo_file;
6000
6001 /* Ok, this is the first time we're reading this TU. */
6002 if (dwo_file->tus == NULL)
6003 return NULL;
6004 find_dwo_entry.signature = sig;
6005 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6006 &find_dwo_entry);
6007 if (dwo_entry == NULL)
6008 return NULL;
6009
6010 /* If the global table doesn't have an entry for this TU, add one. */
6011 if (sig_entry == NULL)
6012 sig_entry = add_type_unit (per_objfile, sig, slot);
6013
6014 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6015 sig_entry->tu_read = 1;
6016 return sig_entry;
6017 }
6018
6019 /* Subroutine of lookup_signatured_type.
6020 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6021 then try the DWP file. If the TU stub (skeleton) has been removed then
6022 it won't be in .gdb_index. */
6023
6024 static struct signatured_type *
6025 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6026 {
6027 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6028 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6029 struct dwo_unit *dwo_entry;
6030 void **slot;
6031
6032 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6033 gdb_assert (dwp_file != NULL);
6034
6035 /* If TU skeletons have been removed then we may not have read in any
6036 TUs yet. */
6037 if (per_objfile->per_bfd->signatured_types == NULL)
6038 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6039
6040 signatured_type find_sig_entry (sig);
6041 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6042 &find_sig_entry, INSERT);
6043 signatured_type *sig_entry = (struct signatured_type *) *slot;
6044
6045 /* Have we already tried to read this TU?
6046 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6047 needn't exist in the global table yet). */
6048 if (sig_entry != NULL)
6049 return sig_entry;
6050
6051 if (dwp_file->tus == NULL)
6052 return NULL;
6053 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6054 1 /* is_debug_types */);
6055 if (dwo_entry == NULL)
6056 return NULL;
6057
6058 sig_entry = add_type_unit (per_objfile, sig, slot);
6059 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6060
6061 return sig_entry;
6062 }
6063
6064 /* Lookup a signature based type for DW_FORM_ref_sig8.
6065 Returns NULL if signature SIG is not present in the table.
6066 It is up to the caller to complain about this. */
6067
6068 static struct signatured_type *
6069 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6070 {
6071 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6072
6073 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6074 {
6075 /* We're in a DWO/DWP file, and we're using .gdb_index.
6076 These cases require special processing. */
6077 if (get_dwp_file (per_objfile) == NULL)
6078 return lookup_dwo_signatured_type (cu, sig);
6079 else
6080 return lookup_dwp_signatured_type (cu, sig);
6081 }
6082 else
6083 {
6084 if (per_objfile->per_bfd->signatured_types == NULL)
6085 return NULL;
6086 signatured_type find_entry (sig);
6087 return ((struct signatured_type *)
6088 htab_find (per_objfile->per_bfd->signatured_types.get (),
6089 &find_entry));
6090 }
6091 }
6092
6093 /* Low level DIE reading support. */
6094
6095 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6096
6097 static void
6098 init_cu_die_reader (struct die_reader_specs *reader,
6099 struct dwarf2_cu *cu,
6100 struct dwarf2_section_info *section,
6101 struct dwo_file *dwo_file,
6102 struct abbrev_table *abbrev_table)
6103 {
6104 gdb_assert (section->readin && section->buffer != NULL);
6105 reader->abfd = section->get_bfd_owner ();
6106 reader->cu = cu;
6107 reader->dwo_file = dwo_file;
6108 reader->die_section = section;
6109 reader->buffer = section->buffer;
6110 reader->buffer_end = section->buffer + section->size;
6111 reader->abbrev_table = abbrev_table;
6112 }
6113
6114 /* Subroutine of cutu_reader to simplify it.
6115 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6116 There's just a lot of work to do, and cutu_reader is big enough
6117 already.
6118
6119 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6120 from it to the DIE in the DWO. If NULL we are skipping the stub.
6121 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6122 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6123 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6124 STUB_COMP_DIR may be non-NULL.
6125 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6126 are filled in with the info of the DIE from the DWO file.
6127 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6128 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6129 kept around for at least as long as *RESULT_READER.
6130
6131 The result is non-zero if a valid (non-dummy) DIE was found. */
6132
6133 static int
6134 read_cutu_die_from_dwo (dwarf2_cu *cu,
6135 struct dwo_unit *dwo_unit,
6136 struct die_info *stub_comp_unit_die,
6137 const char *stub_comp_dir,
6138 struct die_reader_specs *result_reader,
6139 const gdb_byte **result_info_ptr,
6140 struct die_info **result_comp_unit_die,
6141 abbrev_table_up *result_dwo_abbrev_table)
6142 {
6143 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6144 dwarf2_per_cu_data *per_cu = cu->per_cu;
6145 struct objfile *objfile = per_objfile->objfile;
6146 bfd *abfd;
6147 const gdb_byte *begin_info_ptr, *info_ptr;
6148 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6149 int i,num_extra_attrs;
6150 struct dwarf2_section_info *dwo_abbrev_section;
6151 struct die_info *comp_unit_die;
6152
6153 /* At most one of these may be provided. */
6154 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6155
6156 /* These attributes aren't processed until later:
6157 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6158 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6159 referenced later. However, these attributes are found in the stub
6160 which we won't have later. In order to not impose this complication
6161 on the rest of the code, we read them here and copy them to the
6162 DWO CU/TU die. */
6163
6164 stmt_list = NULL;
6165 low_pc = NULL;
6166 high_pc = NULL;
6167 ranges = NULL;
6168 comp_dir = NULL;
6169
6170 if (stub_comp_unit_die != NULL)
6171 {
6172 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6173 DWO file. */
6174 if (!per_cu->is_debug_types)
6175 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6176 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6177 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6178 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6179 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6180
6181 cu->addr_base = stub_comp_unit_die->addr_base ();
6182
6183 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6184 We need the value before we can process DW_AT_ranges values from the
6185 DWO. */
6186 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6187
6188 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6189 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6190 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6191 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6192 section. */
6193 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6194 }
6195 else if (stub_comp_dir != NULL)
6196 {
6197 /* Reconstruct the comp_dir attribute to simplify the code below. */
6198 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6199 comp_dir->name = DW_AT_comp_dir;
6200 comp_dir->form = DW_FORM_string;
6201 comp_dir->set_string_noncanonical (stub_comp_dir);
6202 }
6203
6204 /* Set up for reading the DWO CU/TU. */
6205 cu->dwo_unit = dwo_unit;
6206 dwarf2_section_info *section = dwo_unit->section;
6207 section->read (objfile);
6208 abfd = section->get_bfd_owner ();
6209 begin_info_ptr = info_ptr = (section->buffer
6210 + to_underlying (dwo_unit->sect_off));
6211 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6212
6213 if (per_cu->is_debug_types)
6214 {
6215 signatured_type *sig_type = (struct signatured_type *) per_cu;
6216
6217 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6218 section, dwo_abbrev_section,
6219 info_ptr, rcuh_kind::TYPE);
6220 /* This is not an assert because it can be caused by bad debug info. */
6221 if (sig_type->signature != cu->header.signature)
6222 {
6223 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6224 " TU at offset %s [in module %s]"),
6225 hex_string (sig_type->signature),
6226 hex_string (cu->header.signature),
6227 sect_offset_str (dwo_unit->sect_off),
6228 bfd_get_filename (abfd));
6229 }
6230 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6231 /* For DWOs coming from DWP files, we don't know the CU length
6232 nor the type's offset in the TU until now. */
6233 dwo_unit->length = cu->header.get_length ();
6234 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6235
6236 /* Establish the type offset that can be used to lookup the type.
6237 For DWO files, we don't know it until now. */
6238 sig_type->type_offset_in_section
6239 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6240 }
6241 else
6242 {
6243 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6244 section, dwo_abbrev_section,
6245 info_ptr, rcuh_kind::COMPILE);
6246 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6247 /* For DWOs coming from DWP files, we don't know the CU length
6248 until now. */
6249 dwo_unit->length = cu->header.get_length ();
6250 }
6251
6252 dwo_abbrev_section->read (objfile);
6253 *result_dwo_abbrev_table
6254 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6255 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6256 result_dwo_abbrev_table->get ());
6257
6258 /* Read in the die, but leave space to copy over the attributes
6259 from the stub. This has the benefit of simplifying the rest of
6260 the code - all the work to maintain the illusion of a single
6261 DW_TAG_{compile,type}_unit DIE is done here. */
6262 num_extra_attrs = ((stmt_list != NULL)
6263 + (low_pc != NULL)
6264 + (high_pc != NULL)
6265 + (ranges != NULL)
6266 + (comp_dir != NULL));
6267 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6268 num_extra_attrs);
6269
6270 /* Copy over the attributes from the stub to the DIE we just read in. */
6271 comp_unit_die = *result_comp_unit_die;
6272 i = comp_unit_die->num_attrs;
6273 if (stmt_list != NULL)
6274 comp_unit_die->attrs[i++] = *stmt_list;
6275 if (low_pc != NULL)
6276 comp_unit_die->attrs[i++] = *low_pc;
6277 if (high_pc != NULL)
6278 comp_unit_die->attrs[i++] = *high_pc;
6279 if (ranges != NULL)
6280 comp_unit_die->attrs[i++] = *ranges;
6281 if (comp_dir != NULL)
6282 comp_unit_die->attrs[i++] = *comp_dir;
6283 comp_unit_die->num_attrs += num_extra_attrs;
6284
6285 if (dwarf_die_debug)
6286 {
6287 fprintf_unfiltered (gdb_stdlog,
6288 "Read die from %s@0x%x of %s:\n",
6289 section->get_name (),
6290 (unsigned) (begin_info_ptr - section->buffer),
6291 bfd_get_filename (abfd));
6292 dump_die (comp_unit_die, dwarf_die_debug);
6293 }
6294
6295 /* Skip dummy compilation units. */
6296 if (info_ptr >= begin_info_ptr + dwo_unit->length
6297 || peek_abbrev_code (abfd, info_ptr) == 0)
6298 return 0;
6299
6300 *result_info_ptr = info_ptr;
6301 return 1;
6302 }
6303
6304 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6305 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6306 signature is part of the header. */
6307 static gdb::optional<ULONGEST>
6308 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6309 {
6310 if (cu->header.version >= 5)
6311 return cu->header.signature;
6312 struct attribute *attr;
6313 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6314 if (attr == nullptr || !attr->form_is_unsigned ())
6315 return gdb::optional<ULONGEST> ();
6316 return attr->as_unsigned ();
6317 }
6318
6319 /* Subroutine of cutu_reader to simplify it.
6320 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6321 Returns NULL if the specified DWO unit cannot be found. */
6322
6323 static struct dwo_unit *
6324 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6325 {
6326 dwarf2_per_cu_data *per_cu = cu->per_cu;
6327 struct dwo_unit *dwo_unit;
6328 const char *comp_dir;
6329
6330 gdb_assert (cu != NULL);
6331
6332 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6333 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6334 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6335
6336 if (per_cu->is_debug_types)
6337 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6338 else
6339 {
6340 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6341
6342 if (!signature.has_value ())
6343 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6344 " [in module %s]"),
6345 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6346
6347 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6348 }
6349
6350 return dwo_unit;
6351 }
6352
6353 /* Subroutine of cutu_reader to simplify it.
6354 See it for a description of the parameters.
6355 Read a TU directly from a DWO file, bypassing the stub. */
6356
6357 void
6358 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6359 dwarf2_per_objfile *per_objfile,
6360 dwarf2_cu *existing_cu)
6361 {
6362 struct signatured_type *sig_type;
6363
6364 /* Verify we can do the following downcast, and that we have the
6365 data we need. */
6366 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6367 sig_type = (struct signatured_type *) this_cu;
6368 gdb_assert (sig_type->dwo_unit != NULL);
6369
6370 dwarf2_cu *cu;
6371
6372 if (existing_cu != nullptr)
6373 {
6374 cu = existing_cu;
6375 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6376 /* There's no need to do the rereading_dwo_cu handling that
6377 cutu_reader does since we don't read the stub. */
6378 }
6379 else
6380 {
6381 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6382 in per_objfile yet. */
6383 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6384 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6385 cu = m_new_cu.get ();
6386 }
6387
6388 /* A future optimization, if needed, would be to use an existing
6389 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6390 could share abbrev tables. */
6391
6392 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6393 NULL /* stub_comp_unit_die */,
6394 sig_type->dwo_unit->dwo_file->comp_dir,
6395 this, &info_ptr,
6396 &comp_unit_die,
6397 &m_dwo_abbrev_table) == 0)
6398 {
6399 /* Dummy die. */
6400 dummy_p = true;
6401 }
6402 }
6403
6404 /* Initialize a CU (or TU) and read its DIEs.
6405 If the CU defers to a DWO file, read the DWO file as well.
6406
6407 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6408 Otherwise the table specified in the comp unit header is read in and used.
6409 This is an optimization for when we already have the abbrev table.
6410
6411 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6412 allocated. */
6413
6414 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6415 dwarf2_per_objfile *per_objfile,
6416 struct abbrev_table *abbrev_table,
6417 dwarf2_cu *existing_cu,
6418 bool skip_partial)
6419 : die_reader_specs {},
6420 m_this_cu (this_cu)
6421 {
6422 struct objfile *objfile = per_objfile->objfile;
6423 struct dwarf2_section_info *section = this_cu->section;
6424 bfd *abfd = section->get_bfd_owner ();
6425 const gdb_byte *begin_info_ptr;
6426 struct signatured_type *sig_type = NULL;
6427 struct dwarf2_section_info *abbrev_section;
6428 /* Non-zero if CU currently points to a DWO file and we need to
6429 reread it. When this happens we need to reread the skeleton die
6430 before we can reread the DWO file (this only applies to CUs, not TUs). */
6431 int rereading_dwo_cu = 0;
6432
6433 if (dwarf_die_debug)
6434 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6435 this_cu->is_debug_types ? "type" : "comp",
6436 sect_offset_str (this_cu->sect_off));
6437
6438 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6439 file (instead of going through the stub), short-circuit all of this. */
6440 if (this_cu->reading_dwo_directly)
6441 {
6442 /* Narrow down the scope of possibilities to have to understand. */
6443 gdb_assert (this_cu->is_debug_types);
6444 gdb_assert (abbrev_table == NULL);
6445 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6446 return;
6447 }
6448
6449 /* This is cheap if the section is already read in. */
6450 section->read (objfile);
6451
6452 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6453
6454 abbrev_section = get_abbrev_section_for_cu (this_cu);
6455
6456 dwarf2_cu *cu;
6457
6458 if (existing_cu != nullptr)
6459 {
6460 cu = existing_cu;
6461 /* If this CU is from a DWO file we need to start over, we need to
6462 refetch the attributes from the skeleton CU.
6463 This could be optimized by retrieving those attributes from when we
6464 were here the first time: the previous comp_unit_die was stored in
6465 comp_unit_obstack. But there's no data yet that we need this
6466 optimization. */
6467 if (cu->dwo_unit != NULL)
6468 rereading_dwo_cu = 1;
6469 }
6470 else
6471 {
6472 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6473 in per_objfile yet. */
6474 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6475 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6476 cu = m_new_cu.get ();
6477 }
6478
6479 /* Get the header. */
6480 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6481 {
6482 /* We already have the header, there's no need to read it in again. */
6483 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6484 }
6485 else
6486 {
6487 if (this_cu->is_debug_types)
6488 {
6489 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6490 section, abbrev_section,
6491 info_ptr, rcuh_kind::TYPE);
6492
6493 /* Since per_cu is the first member of struct signatured_type,
6494 we can go from a pointer to one to a pointer to the other. */
6495 sig_type = (struct signatured_type *) this_cu;
6496 gdb_assert (sig_type->signature == cu->header.signature);
6497 gdb_assert (sig_type->type_offset_in_tu
6498 == cu->header.type_cu_offset_in_tu);
6499 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6500
6501 /* LENGTH has not been set yet for type units if we're
6502 using .gdb_index. */
6503 this_cu->length = cu->header.get_length ();
6504
6505 /* Establish the type offset that can be used to lookup the type. */
6506 sig_type->type_offset_in_section =
6507 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6508
6509 this_cu->dwarf_version = cu->header.version;
6510 }
6511 else
6512 {
6513 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6514 section, abbrev_section,
6515 info_ptr,
6516 rcuh_kind::COMPILE);
6517
6518 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6519 if (this_cu->length == 0)
6520 this_cu->length = cu->header.get_length ();
6521 else
6522 gdb_assert (this_cu->length == cu->header.get_length ());
6523 this_cu->dwarf_version = cu->header.version;
6524 }
6525 }
6526
6527 /* Skip dummy compilation units. */
6528 if (info_ptr >= begin_info_ptr + this_cu->length
6529 || peek_abbrev_code (abfd, info_ptr) == 0)
6530 {
6531 dummy_p = true;
6532 return;
6533 }
6534
6535 /* If we don't have them yet, read the abbrevs for this compilation unit.
6536 And if we need to read them now, make sure they're freed when we're
6537 done. */
6538 if (abbrev_table != NULL)
6539 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6540 else
6541 {
6542 abbrev_section->read (objfile);
6543 m_abbrev_table_holder
6544 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6545 abbrev_table = m_abbrev_table_holder.get ();
6546 }
6547
6548 /* Read the top level CU/TU die. */
6549 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6550 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6551
6552 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6553 {
6554 dummy_p = true;
6555 return;
6556 }
6557
6558 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6559 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6560 table from the DWO file and pass the ownership over to us. It will be
6561 referenced from READER, so we must make sure to free it after we're done
6562 with READER.
6563
6564 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6565 DWO CU, that this test will fail (the attribute will not be present). */
6566 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6567 if (dwo_name != nullptr)
6568 {
6569 struct dwo_unit *dwo_unit;
6570 struct die_info *dwo_comp_unit_die;
6571
6572 if (comp_unit_die->has_children)
6573 {
6574 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6575 " has children (offset %s) [in module %s]"),
6576 sect_offset_str (this_cu->sect_off),
6577 bfd_get_filename (abfd));
6578 }
6579 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6580 if (dwo_unit != NULL)
6581 {
6582 if (read_cutu_die_from_dwo (cu, dwo_unit,
6583 comp_unit_die, NULL,
6584 this, &info_ptr,
6585 &dwo_comp_unit_die,
6586 &m_dwo_abbrev_table) == 0)
6587 {
6588 /* Dummy die. */
6589 dummy_p = true;
6590 return;
6591 }
6592 comp_unit_die = dwo_comp_unit_die;
6593 }
6594 else
6595 {
6596 /* Yikes, we couldn't find the rest of the DIE, we only have
6597 the stub. A complaint has already been logged. There's
6598 not much more we can do except pass on the stub DIE to
6599 die_reader_func. We don't want to throw an error on bad
6600 debug info. */
6601 }
6602 }
6603 }
6604
6605 void
6606 cutu_reader::keep ()
6607 {
6608 /* Done, clean up. */
6609 gdb_assert (!dummy_p);
6610 if (m_new_cu != NULL)
6611 {
6612 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6613 now. */
6614 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6615 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6616 }
6617 }
6618
6619 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6620 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6621 assumed to have already done the lookup to find the DWO file).
6622
6623 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6624 THIS_CU->is_debug_types, but nothing else.
6625
6626 We fill in THIS_CU->length.
6627
6628 THIS_CU->cu is always freed when done.
6629 This is done in order to not leave THIS_CU->cu in a state where we have
6630 to care whether it refers to the "main" CU or the DWO CU.
6631
6632 When parent_cu is passed, it is used to provide a default value for
6633 str_offsets_base and addr_base from the parent. */
6634
6635 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6636 dwarf2_per_objfile *per_objfile,
6637 struct dwarf2_cu *parent_cu,
6638 struct dwo_file *dwo_file)
6639 : die_reader_specs {},
6640 m_this_cu (this_cu)
6641 {
6642 struct objfile *objfile = per_objfile->objfile;
6643 struct dwarf2_section_info *section = this_cu->section;
6644 bfd *abfd = section->get_bfd_owner ();
6645 struct dwarf2_section_info *abbrev_section;
6646 const gdb_byte *begin_info_ptr, *info_ptr;
6647
6648 if (dwarf_die_debug)
6649 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6650 this_cu->is_debug_types ? "type" : "comp",
6651 sect_offset_str (this_cu->sect_off));
6652
6653 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6654
6655 abbrev_section = (dwo_file != NULL
6656 ? &dwo_file->sections.abbrev
6657 : get_abbrev_section_for_cu (this_cu));
6658
6659 /* This is cheap if the section is already read in. */
6660 section->read (objfile);
6661
6662 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6663
6664 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6665 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6666 section, abbrev_section, info_ptr,
6667 (this_cu->is_debug_types
6668 ? rcuh_kind::TYPE
6669 : rcuh_kind::COMPILE));
6670
6671 if (parent_cu != nullptr)
6672 {
6673 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6674 m_new_cu->addr_base = parent_cu->addr_base;
6675 }
6676 this_cu->length = m_new_cu->header.get_length ();
6677
6678 /* Skip dummy compilation units. */
6679 if (info_ptr >= begin_info_ptr + this_cu->length
6680 || peek_abbrev_code (abfd, info_ptr) == 0)
6681 {
6682 dummy_p = true;
6683 return;
6684 }
6685
6686 abbrev_section->read (objfile);
6687 m_abbrev_table_holder
6688 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6689
6690 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6691 m_abbrev_table_holder.get ());
6692 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6693 }
6694
6695 \f
6696 /* Type Unit Groups.
6697
6698 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6699 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6700 so that all types coming from the same compilation (.o file) are grouped
6701 together. A future step could be to put the types in the same symtab as
6702 the CU the types ultimately came from. */
6703
6704 static hashval_t
6705 hash_type_unit_group (const void *item)
6706 {
6707 const struct type_unit_group *tu_group
6708 = (const struct type_unit_group *) item;
6709
6710 return hash_stmt_list_entry (&tu_group->hash);
6711 }
6712
6713 static int
6714 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6715 {
6716 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6717 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6718
6719 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6720 }
6721
6722 /* Allocate a hash table for type unit groups. */
6723
6724 static htab_up
6725 allocate_type_unit_groups_table ()
6726 {
6727 return htab_up (htab_create_alloc (3,
6728 hash_type_unit_group,
6729 eq_type_unit_group,
6730 htab_delete_entry<type_unit_group>,
6731 xcalloc, xfree));
6732 }
6733
6734 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6735 partial symtabs. We combine several TUs per psymtab to not let the size
6736 of any one psymtab grow too big. */
6737 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6738 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6739
6740 /* Helper routine for get_type_unit_group.
6741 Create the type_unit_group object used to hold one or more TUs. */
6742
6743 static std::unique_ptr<type_unit_group>
6744 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6745 {
6746 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6747 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6748
6749 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6750 tu_group->per_bfd = per_bfd;
6751
6752 if (per_bfd->using_index)
6753 {
6754 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6755 struct dwarf2_per_cu_quick_data);
6756 }
6757 else
6758 {
6759 unsigned int line_offset = to_underlying (line_offset_struct);
6760 dwarf2_psymtab *pst;
6761 std::string name;
6762
6763 /* Give the symtab a useful name for debug purposes. */
6764 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6765 name = string_printf ("<type_units_%d>",
6766 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6767 else
6768 name = string_printf ("<type_units_at_0x%x>", line_offset);
6769
6770 pst = create_partial_symtab (tu_group.get (), per_objfile,
6771 name.c_str ());
6772 pst->anonymous = true;
6773 }
6774
6775 tu_group->hash.dwo_unit = cu->dwo_unit;
6776 tu_group->hash.line_sect_off = line_offset_struct;
6777
6778 return tu_group;
6779 }
6780
6781 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6782 STMT_LIST is a DW_AT_stmt_list attribute. */
6783
6784 static struct type_unit_group *
6785 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6786 {
6787 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6788 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6789 struct type_unit_group *tu_group;
6790 void **slot;
6791 unsigned int line_offset;
6792 struct type_unit_group type_unit_group_for_lookup;
6793
6794 if (per_objfile->per_bfd->type_unit_groups == NULL)
6795 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6796
6797 /* Do we need to create a new group, or can we use an existing one? */
6798
6799 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6800 {
6801 line_offset = stmt_list->as_unsigned ();
6802 ++tu_stats->nr_symtab_sharers;
6803 }
6804 else
6805 {
6806 /* Ugh, no stmt_list. Rare, but we have to handle it.
6807 We can do various things here like create one group per TU or
6808 spread them over multiple groups to split up the expansion work.
6809 To avoid worst case scenarios (too many groups or too large groups)
6810 we, umm, group them in bunches. */
6811 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6812 | (tu_stats->nr_stmt_less_type_units
6813 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6814 ++tu_stats->nr_stmt_less_type_units;
6815 }
6816
6817 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6818 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6819 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6820 &type_unit_group_for_lookup, INSERT);
6821 if (*slot == nullptr)
6822 {
6823 sect_offset line_offset_struct = (sect_offset) line_offset;
6824 std::unique_ptr<type_unit_group> grp
6825 = create_type_unit_group (cu, line_offset_struct);
6826 *slot = grp.release ();
6827 ++tu_stats->nr_symtabs;
6828 }
6829
6830 tu_group = (struct type_unit_group *) *slot;
6831 gdb_assert (tu_group != nullptr);
6832 return tu_group;
6833 }
6834 \f
6835 /* Partial symbol tables. */
6836
6837 /* Create a psymtab named NAME and assign it to PER_CU.
6838
6839 The caller must fill in the following details:
6840 dirname, textlow, texthigh. */
6841
6842 static dwarf2_psymtab *
6843 create_partial_symtab (dwarf2_per_cu_data *per_cu,
6844 dwarf2_per_objfile *per_objfile,
6845 const char *name)
6846 {
6847 dwarf2_psymtab *pst
6848 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6849 per_objfile->objfile->per_bfd, per_cu);
6850
6851 pst->psymtabs_addrmap_supported = true;
6852
6853 /* This is the glue that links PST into GDB's symbol API. */
6854 per_cu->v.psymtab = pst;
6855
6856 return pst;
6857 }
6858
6859 /* DIE reader function for process_psymtab_comp_unit. */
6860
6861 static void
6862 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6863 const gdb_byte *info_ptr,
6864 struct die_info *comp_unit_die,
6865 enum language pretend_language)
6866 {
6867 struct dwarf2_cu *cu = reader->cu;
6868 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6869 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6870 struct objfile *objfile = per_objfile->objfile;
6871 struct gdbarch *gdbarch = objfile->arch ();
6872 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6873 CORE_ADDR baseaddr;
6874 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6875 dwarf2_psymtab *pst;
6876 enum pc_bounds_kind cu_bounds_kind;
6877 const char *filename;
6878
6879 gdb_assert (! per_cu->is_debug_types);
6880
6881 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
6882
6883 /* Allocate a new partial symbol table structure. */
6884 gdb::unique_xmalloc_ptr<char> debug_filename;
6885 static const char artificial[] = "<artificial>";
6886 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6887 if (filename == NULL)
6888 filename = "";
6889 else if (strcmp (filename, artificial) == 0)
6890 {
6891 debug_filename.reset (concat (artificial, "@",
6892 sect_offset_str (per_cu->sect_off),
6893 (char *) NULL));
6894 filename = debug_filename.get ();
6895 }
6896
6897 pst = create_partial_symtab (per_cu, per_objfile, filename);
6898
6899 /* This must be done before calling dwarf2_build_include_psymtabs. */
6900 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6901
6902 baseaddr = objfile->text_section_offset ();
6903
6904 dwarf2_find_base_address (comp_unit_die, cu);
6905
6906 /* Possibly set the default values of LOWPC and HIGHPC from
6907 `DW_AT_ranges'. */
6908 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6909 &best_highpc, cu, pst);
6910 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6911 {
6912 CORE_ADDR low
6913 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
6914 - baseaddr);
6915 CORE_ADDR high
6916 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
6917 - baseaddr - 1);
6918 /* Store the contiguous range if it is not empty; it can be
6919 empty for CUs with no code. */
6920 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
6921 low, high, pst);
6922 }
6923
6924 /* Check if comp unit has_children.
6925 If so, read the rest of the partial symbols from this comp unit.
6926 If not, there's no more debug_info for this comp unit. */
6927 if (comp_unit_die->has_children)
6928 {
6929 struct partial_die_info *first_die;
6930 CORE_ADDR lowpc, highpc;
6931
6932 lowpc = ((CORE_ADDR) -1);
6933 highpc = ((CORE_ADDR) 0);
6934
6935 first_die = load_partial_dies (reader, info_ptr, 1);
6936
6937 scan_partial_symbols (first_die, &lowpc, &highpc,
6938 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6939
6940 /* If we didn't find a lowpc, set it to highpc to avoid
6941 complaints from `maint check'. */
6942 if (lowpc == ((CORE_ADDR) -1))
6943 lowpc = highpc;
6944
6945 /* If the compilation unit didn't have an explicit address range,
6946 then use the information extracted from its child dies. */
6947 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6948 {
6949 best_lowpc = lowpc;
6950 best_highpc = highpc;
6951 }
6952 }
6953 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
6954 best_lowpc + baseaddr)
6955 - baseaddr);
6956 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
6957 best_highpc + baseaddr)
6958 - baseaddr);
6959
6960 pst->end ();
6961
6962 if (!cu->per_cu->imported_symtabs_empty ())
6963 {
6964 int i;
6965 int len = cu->per_cu->imported_symtabs_size ();
6966
6967 /* Fill in 'dependencies' here; we fill in 'users' in a
6968 post-pass. */
6969 pst->number_of_dependencies = len;
6970 pst->dependencies
6971 = per_bfd->partial_symtabs->allocate_dependencies (len);
6972 for (i = 0; i < len; ++i)
6973 {
6974 pst->dependencies[i]
6975 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
6976 }
6977
6978 cu->per_cu->imported_symtabs_free ();
6979 }
6980
6981 /* Get the list of files included in the current compilation unit,
6982 and build a psymtab for each of them. */
6983 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6984
6985 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
6986 ", %d global, %d static syms",
6987 per_cu->is_debug_types ? "type" : "comp",
6988 sect_offset_str (per_cu->sect_off),
6989 paddress (gdbarch, pst->text_low (objfile)),
6990 paddress (gdbarch, pst->text_high (objfile)),
6991 (int) pst->global_psymbols.size (),
6992 (int) pst->static_psymbols.size ());
6993 }
6994
6995 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6996 Process compilation unit THIS_CU for a psymtab. */
6997
6998 static void
6999 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7000 dwarf2_per_objfile *per_objfile,
7001 bool want_partial_unit,
7002 enum language pretend_language)
7003 {
7004 /* If this compilation unit was already read in, free the
7005 cached copy in order to read it in again. This is
7006 necessary because we skipped some symbols when we first
7007 read in the compilation unit (see load_partial_dies).
7008 This problem could be avoided, but the benefit is unclear. */
7009 per_objfile->remove_cu (this_cu);
7010
7011 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7012
7013 if (reader.comp_unit_die == nullptr)
7014 return;
7015
7016 switch (reader.comp_unit_die->tag)
7017 {
7018 case DW_TAG_compile_unit:
7019 this_cu->unit_type = DW_UT_compile;
7020 break;
7021 case DW_TAG_partial_unit:
7022 this_cu->unit_type = DW_UT_partial;
7023 break;
7024 case DW_TAG_type_unit:
7025 this_cu->unit_type = DW_UT_type;
7026 break;
7027 default:
7028 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7029 dwarf_tag_name (reader.comp_unit_die->tag),
7030 sect_offset_str (reader.cu->per_cu->sect_off),
7031 objfile_name (per_objfile->objfile));
7032 }
7033
7034 if (reader.dummy_p)
7035 {
7036 /* Nothing. */
7037 }
7038 else if (this_cu->is_debug_types)
7039 build_type_psymtabs_reader (&reader, reader.info_ptr,
7040 reader.comp_unit_die);
7041 else if (want_partial_unit
7042 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7043 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7044 reader.comp_unit_die,
7045 pretend_language);
7046
7047 /* Age out any secondary CUs. */
7048 per_objfile->age_comp_units ();
7049 }
7050
7051 /* Reader function for build_type_psymtabs. */
7052
7053 static void
7054 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7055 const gdb_byte *info_ptr,
7056 struct die_info *type_unit_die)
7057 {
7058 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7059 struct dwarf2_cu *cu = reader->cu;
7060 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7061 struct signatured_type *sig_type;
7062 struct type_unit_group *tu_group;
7063 struct attribute *attr;
7064 struct partial_die_info *first_die;
7065 CORE_ADDR lowpc, highpc;
7066 dwarf2_psymtab *pst;
7067
7068 gdb_assert (per_cu->is_debug_types);
7069 sig_type = (struct signatured_type *) per_cu;
7070
7071 if (! type_unit_die->has_children)
7072 return;
7073
7074 attr = type_unit_die->attr (DW_AT_stmt_list);
7075 tu_group = get_type_unit_group (cu, attr);
7076
7077 if (tu_group->tus == nullptr)
7078 tu_group->tus = new std::vector<signatured_type *>;
7079 tu_group->tus->push_back (sig_type);
7080
7081 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7082 pst = create_partial_symtab (per_cu, per_objfile, "");
7083 pst->anonymous = true;
7084
7085 first_die = load_partial_dies (reader, info_ptr, 1);
7086
7087 lowpc = (CORE_ADDR) -1;
7088 highpc = (CORE_ADDR) 0;
7089 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7090
7091 pst->end ();
7092 }
7093
7094 /* Struct used to sort TUs by their abbreviation table offset. */
7095
7096 struct tu_abbrev_offset
7097 {
7098 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7099 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7100 {}
7101
7102 /* This is used when sorting. */
7103 bool operator< (const tu_abbrev_offset &other) const
7104 {
7105 return abbrev_offset < other.abbrev_offset;
7106 }
7107
7108 signatured_type *sig_type;
7109 sect_offset abbrev_offset;
7110 };
7111
7112 /* Efficiently read all the type units.
7113
7114 The efficiency is because we sort TUs by the abbrev table they use and
7115 only read each abbrev table once. In one program there are 200K TUs
7116 sharing 8K abbrev tables.
7117
7118 The main purpose of this function is to support building the
7119 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7120 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7121 can collapse the search space by grouping them by stmt_list.
7122 The savings can be significant, in the same program from above the 200K TUs
7123 share 8K stmt_list tables.
7124
7125 FUNC is expected to call get_type_unit_group, which will create the
7126 struct type_unit_group if necessary and add it to
7127 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7128
7129 static void
7130 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7131 {
7132 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7133 abbrev_table_up abbrev_table;
7134 sect_offset abbrev_offset;
7135
7136 /* It's up to the caller to not call us multiple times. */
7137 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7138
7139 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7140 return;
7141
7142 /* TUs typically share abbrev tables, and there can be way more TUs than
7143 abbrev tables. Sort by abbrev table to reduce the number of times we
7144 read each abbrev table in.
7145 Alternatives are to punt or to maintain a cache of abbrev tables.
7146 This is simpler and efficient enough for now.
7147
7148 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7149 symtab to use). Typically TUs with the same abbrev offset have the same
7150 stmt_list value too so in practice this should work well.
7151
7152 The basic algorithm here is:
7153
7154 sort TUs by abbrev table
7155 for each TU with same abbrev table:
7156 read abbrev table if first user
7157 read TU top level DIE
7158 [IWBN if DWO skeletons had DW_AT_stmt_list]
7159 call FUNC */
7160
7161 dwarf_read_debug_printf ("Building type unit groups ...");
7162
7163 /* Sort in a separate table to maintain the order of all_comp_units
7164 for .gdb_index: TU indices directly index all_type_units. */
7165 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7166 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7167
7168 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7169 {
7170 if (cu->is_debug_types)
7171 {
7172 auto sig_type = static_cast<signatured_type *> (cu.get ());
7173 sorted_by_abbrev.emplace_back
7174 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7175 sig_type->sect_off));
7176 }
7177 }
7178
7179 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7180
7181 abbrev_offset = (sect_offset) ~(unsigned) 0;
7182
7183 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7184 {
7185 /* Switch to the next abbrev table if necessary. */
7186 if (abbrev_table == NULL
7187 || tu.abbrev_offset != abbrev_offset)
7188 {
7189 abbrev_offset = tu.abbrev_offset;
7190 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7191 abbrev_table =
7192 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7193 ++tu_stats->nr_uniq_abbrev_tables;
7194 }
7195
7196 cutu_reader reader (tu.sig_type, per_objfile,
7197 abbrev_table.get (), nullptr, false);
7198 if (!reader.dummy_p)
7199 build_type_psymtabs_reader (&reader, reader.info_ptr,
7200 reader.comp_unit_die);
7201 }
7202 }
7203
7204 /* Print collected type unit statistics. */
7205
7206 static void
7207 print_tu_stats (dwarf2_per_objfile *per_objfile)
7208 {
7209 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7210
7211 dwarf_read_debug_printf ("Type unit statistics:");
7212 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7213 dwarf_read_debug_printf (" %d uniq abbrev tables",
7214 tu_stats->nr_uniq_abbrev_tables);
7215 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7216 tu_stats->nr_symtabs);
7217 dwarf_read_debug_printf (" %d symtab sharers",
7218 tu_stats->nr_symtab_sharers);
7219 dwarf_read_debug_printf (" %d type units without a stmt_list",
7220 tu_stats->nr_stmt_less_type_units);
7221 dwarf_read_debug_printf (" %d all_type_units reallocs",
7222 tu_stats->nr_all_type_units_reallocs);
7223 }
7224
7225 /* Traversal function for build_type_psymtabs. */
7226
7227 static int
7228 build_type_psymtab_dependencies (void **slot, void *info)
7229 {
7230 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7231 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7232 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7233 dwarf2_psymtab *pst = tu_group->v.psymtab;
7234 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7235 int i;
7236
7237 gdb_assert (len > 0);
7238 gdb_assert (tu_group->type_unit_group_p ());
7239
7240 pst->number_of_dependencies = len;
7241 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7242 for (i = 0; i < len; ++i)
7243 {
7244 struct signatured_type *iter = tu_group->tus->at (i);
7245 gdb_assert (iter->is_debug_types);
7246 pst->dependencies[i] = iter->v.psymtab;
7247 iter->type_unit_group = tu_group;
7248 }
7249
7250 delete tu_group->tus;
7251 tu_group->tus = nullptr;
7252
7253 return 1;
7254 }
7255
7256 /* Traversal function for process_skeletonless_type_unit.
7257 Read a TU in a DWO file and build partial symbols for it. */
7258
7259 static int
7260 process_skeletonless_type_unit (void **slot, void *info)
7261 {
7262 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7263 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7264
7265 /* If this TU doesn't exist in the global table, add it and read it in. */
7266
7267 if (per_objfile->per_bfd->signatured_types == NULL)
7268 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7269
7270 signatured_type find_entry (dwo_unit->signature);
7271 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7272 &find_entry, INSERT);
7273 /* If we've already seen this type there's nothing to do. What's happening
7274 is we're doing our own version of comdat-folding here. */
7275 if (*slot != NULL)
7276 return 1;
7277
7278 /* This does the job that create_all_comp_units would have done for
7279 this TU. */
7280 signatured_type *entry
7281 = add_type_unit (per_objfile, dwo_unit->signature, slot);
7282 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7283 *slot = entry;
7284
7285 /* This does the job that build_type_psymtabs would have done. */
7286 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7287 if (!reader.dummy_p)
7288 build_type_psymtabs_reader (&reader, reader.info_ptr,
7289 reader.comp_unit_die);
7290
7291 return 1;
7292 }
7293
7294 /* Traversal function for process_skeletonless_type_units. */
7295
7296 static int
7297 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7298 {
7299 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7300
7301 if (dwo_file->tus != NULL)
7302 htab_traverse_noresize (dwo_file->tus.get (),
7303 process_skeletonless_type_unit, info);
7304
7305 return 1;
7306 }
7307
7308 /* Scan all TUs of DWO files, verifying we've processed them.
7309 This is needed in case a TU was emitted without its skeleton.
7310 Note: This can't be done until we know what all the DWO files are. */
7311
7312 static void
7313 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7314 {
7315 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7316 if (get_dwp_file (per_objfile) == NULL
7317 && per_objfile->per_bfd->dwo_files != NULL)
7318 {
7319 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7320 process_dwo_file_for_skeletonless_type_units,
7321 per_objfile);
7322 }
7323 }
7324
7325 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7326
7327 static void
7328 set_partial_user (dwarf2_per_objfile *per_objfile)
7329 {
7330 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7331 {
7332 dwarf2_psymtab *pst = per_cu->v.psymtab;
7333
7334 if (pst == NULL)
7335 continue;
7336
7337 for (int j = 0; j < pst->number_of_dependencies; ++j)
7338 {
7339 /* Set the 'user' field only if it is not already set. */
7340 if (pst->dependencies[j]->user == NULL)
7341 pst->dependencies[j]->user = pst;
7342 }
7343 }
7344 }
7345
7346 /* Build the partial symbol table by doing a quick pass through the
7347 .debug_info and .debug_abbrev sections. */
7348
7349 static void
7350 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7351 {
7352 struct objfile *objfile = per_objfile->objfile;
7353 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7354
7355 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7356 objfile_name (objfile));
7357
7358 scoped_restore restore_reading_psyms
7359 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7360
7361 per_bfd->info.read (objfile);
7362
7363 /* Any cached compilation units will be linked by the per-objfile
7364 read_in_chain. Make sure to free them when we're done. */
7365 free_cached_comp_units freer (per_objfile);
7366
7367 create_all_comp_units (per_objfile);
7368 build_type_psymtabs (per_objfile);
7369
7370 /* Create a temporary address map on a temporary obstack. We later
7371 copy this to the final obstack. */
7372 auto_obstack temp_obstack;
7373
7374 scoped_restore save_psymtabs_addrmap
7375 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7376 addrmap_create_mutable (&temp_obstack));
7377
7378 for (const auto &per_cu : per_bfd->all_comp_units)
7379 {
7380 if (per_cu->v.psymtab != NULL)
7381 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7382 continue;
7383 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7384 language_minimal);
7385 }
7386
7387 /* This has to wait until we read the CUs, we need the list of DWOs. */
7388 process_skeletonless_type_units (per_objfile);
7389
7390 /* Now that all TUs have been processed we can fill in the dependencies. */
7391 if (per_bfd->type_unit_groups != NULL)
7392 {
7393 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7394 build_type_psymtab_dependencies, per_objfile);
7395 }
7396
7397 if (dwarf_read_debug > 0)
7398 print_tu_stats (per_objfile);
7399
7400 set_partial_user (per_objfile);
7401
7402 per_bfd->partial_symtabs->psymtabs_addrmap
7403 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7404 per_bfd->partial_symtabs->obstack ());
7405 /* At this point we want to keep the address map. */
7406 save_psymtabs_addrmap.release ();
7407
7408 dwarf_read_debug_printf ("Done building psymtabs of %s",
7409 objfile_name (objfile));
7410 }
7411
7412 /* Load the partial DIEs for a secondary CU into memory.
7413 This is also used when rereading a primary CU with load_all_dies. */
7414
7415 static void
7416 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7417 dwarf2_per_objfile *per_objfile,
7418 dwarf2_cu *existing_cu)
7419 {
7420 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7421
7422 if (!reader.dummy_p)
7423 {
7424 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7425 language_minimal);
7426
7427 /* Check if comp unit has_children.
7428 If so, read the rest of the partial symbols from this comp unit.
7429 If not, there's no more debug_info for this comp unit. */
7430 if (reader.comp_unit_die->has_children)
7431 load_partial_dies (&reader, reader.info_ptr, 0);
7432
7433 reader.keep ();
7434 }
7435 }
7436
7437 static void
7438 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7439 struct dwarf2_section_info *section,
7440 struct dwarf2_section_info *abbrev_section,
7441 unsigned int is_dwz,
7442 htab_up &types_htab,
7443 rcuh_kind section_kind)
7444 {
7445 const gdb_byte *info_ptr;
7446 struct objfile *objfile = per_objfile->objfile;
7447
7448 dwarf_read_debug_printf ("Reading %s for %s",
7449 section->get_name (),
7450 section->get_file_name ());
7451
7452 section->read (objfile);
7453
7454 info_ptr = section->buffer;
7455
7456 while (info_ptr < section->buffer + section->size)
7457 {
7458 dwarf2_per_cu_data_up this_cu;
7459
7460 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7461
7462 comp_unit_head cu_header;
7463 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7464 abbrev_section, info_ptr,
7465 section_kind);
7466
7467 /* Save the compilation unit for later lookup. */
7468 if (cu_header.unit_type != DW_UT_type)
7469 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7470 else
7471 {
7472 if (types_htab == nullptr)
7473 types_htab = allocate_signatured_type_table ();
7474
7475 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7476 (cu_header.signature);
7477 signatured_type *sig_ptr = sig_type.get ();
7478 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7479 this_cu.reset (sig_type.release ());
7480
7481 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7482 gdb_assert (slot != nullptr);
7483 if (*slot != nullptr)
7484 complaint (_("debug type entry at offset %s is duplicate to"
7485 " the entry at offset %s, signature %s"),
7486 sect_offset_str (sect_off),
7487 sect_offset_str (sig_ptr->sect_off),
7488 hex_string (sig_ptr->signature));
7489 *slot = sig_ptr;
7490 }
7491 this_cu->sect_off = sect_off;
7492 this_cu->length = cu_header.length + cu_header.initial_length_size;
7493 this_cu->is_dwz = is_dwz;
7494 this_cu->section = section;
7495
7496 info_ptr = info_ptr + this_cu->length;
7497 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7498 }
7499 }
7500
7501 /* Create a list of all compilation units in OBJFILE.
7502 This is only done for -readnow and building partial symtabs. */
7503
7504 static void
7505 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7506 {
7507 htab_up types_htab;
7508
7509 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7510 &per_objfile->per_bfd->abbrev, 0,
7511 types_htab, rcuh_kind::COMPILE);
7512 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7513 read_comp_units_from_section (per_objfile, &section,
7514 &per_objfile->per_bfd->abbrev, 0,
7515 types_htab, rcuh_kind::TYPE);
7516
7517 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7518 if (dwz != NULL)
7519 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7520 types_htab, rcuh_kind::COMPILE);
7521
7522 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7523 }
7524
7525 /* Process all loaded DIEs for compilation unit CU, starting at
7526 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7527 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7528 DW_AT_ranges). See the comments of add_partial_subprogram on how
7529 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7530
7531 static void
7532 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7533 CORE_ADDR *highpc, int set_addrmap,
7534 struct dwarf2_cu *cu)
7535 {
7536 struct partial_die_info *pdi;
7537
7538 /* Now, march along the PDI's, descending into ones which have
7539 interesting children but skipping the children of the other ones,
7540 until we reach the end of the compilation unit. */
7541
7542 pdi = first_die;
7543
7544 while (pdi != NULL)
7545 {
7546 pdi->fixup (cu);
7547
7548 /* Anonymous namespaces or modules have no name but have interesting
7549 children, so we need to look at them. Ditto for anonymous
7550 enums. */
7551
7552 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7553 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7554 || pdi->tag == DW_TAG_imported_unit
7555 || pdi->tag == DW_TAG_inlined_subroutine)
7556 {
7557 switch (pdi->tag)
7558 {
7559 case DW_TAG_subprogram:
7560 case DW_TAG_inlined_subroutine:
7561 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7562 if (cu->per_cu->lang == language_cplus)
7563 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7564 set_addrmap, cu);
7565 break;
7566 case DW_TAG_constant:
7567 case DW_TAG_variable:
7568 case DW_TAG_typedef:
7569 case DW_TAG_union_type:
7570 if (!pdi->is_declaration
7571 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7572 {
7573 add_partial_symbol (pdi, cu);
7574 }
7575 break;
7576 case DW_TAG_class_type:
7577 case DW_TAG_interface_type:
7578 case DW_TAG_structure_type:
7579 if (!pdi->is_declaration)
7580 {
7581 add_partial_symbol (pdi, cu);
7582 }
7583 if ((cu->per_cu->lang == language_rust
7584 || cu->per_cu->lang == language_cplus)
7585 && pdi->has_children)
7586 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7587 set_addrmap, cu);
7588 break;
7589 case DW_TAG_enumeration_type:
7590 if (!pdi->is_declaration)
7591 add_partial_enumeration (pdi, cu);
7592 break;
7593 case DW_TAG_base_type:
7594 case DW_TAG_subrange_type:
7595 /* File scope base type definitions are added to the partial
7596 symbol table. */
7597 add_partial_symbol (pdi, cu);
7598 break;
7599 case DW_TAG_namespace:
7600 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7601 break;
7602 case DW_TAG_module:
7603 if (!pdi->is_declaration)
7604 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7605 break;
7606 case DW_TAG_imported_unit:
7607 {
7608 struct dwarf2_per_cu_data *per_cu;
7609
7610 /* For now we don't handle imported units in type units. */
7611 if (cu->per_cu->is_debug_types)
7612 {
7613 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7614 " supported in type units [in module %s]"),
7615 objfile_name (cu->per_objfile->objfile));
7616 }
7617
7618 per_cu = dwarf2_find_containing_comp_unit
7619 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
7620
7621 /* Go read the partial unit, if needed. */
7622 if (per_cu->v.psymtab == NULL)
7623 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7624 cu->per_cu->lang);
7625
7626 cu->per_cu->imported_symtabs_push (per_cu);
7627 }
7628 break;
7629 case DW_TAG_imported_declaration:
7630 add_partial_symbol (pdi, cu);
7631 break;
7632 default:
7633 break;
7634 }
7635 }
7636
7637 /* If the die has a sibling, skip to the sibling. */
7638
7639 pdi = pdi->die_sibling;
7640 }
7641 }
7642
7643 /* Functions used to compute the fully scoped name of a partial DIE.
7644
7645 Normally, this is simple. For C++, the parent DIE's fully scoped
7646 name is concatenated with "::" and the partial DIE's name.
7647 Enumerators are an exception; they use the scope of their parent
7648 enumeration type, i.e. the name of the enumeration type is not
7649 prepended to the enumerator.
7650
7651 There are two complexities. One is DW_AT_specification; in this
7652 case "parent" means the parent of the target of the specification,
7653 instead of the direct parent of the DIE. The other is compilers
7654 which do not emit DW_TAG_namespace; in this case we try to guess
7655 the fully qualified name of structure types from their members'
7656 linkage names. This must be done using the DIE's children rather
7657 than the children of any DW_AT_specification target. We only need
7658 to do this for structures at the top level, i.e. if the target of
7659 any DW_AT_specification (if any; otherwise the DIE itself) does not
7660 have a parent. */
7661
7662 /* Compute the scope prefix associated with PDI's parent, in
7663 compilation unit CU. The result will be allocated on CU's
7664 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7665 field. NULL is returned if no prefix is necessary. */
7666 static const char *
7667 partial_die_parent_scope (struct partial_die_info *pdi,
7668 struct dwarf2_cu *cu)
7669 {
7670 const char *grandparent_scope;
7671 struct partial_die_info *parent, *real_pdi;
7672
7673 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7674 then this means the parent of the specification DIE. */
7675
7676 real_pdi = pdi;
7677 while (real_pdi->has_specification)
7678 {
7679 auto res = find_partial_die (real_pdi->spec_offset,
7680 real_pdi->spec_is_dwz, cu);
7681 real_pdi = res.pdi;
7682 cu = res.cu;
7683 }
7684
7685 parent = real_pdi->die_parent;
7686 if (parent == NULL)
7687 return NULL;
7688
7689 if (parent->scope_set)
7690 return parent->scope;
7691
7692 parent->fixup (cu);
7693
7694 grandparent_scope = partial_die_parent_scope (parent, cu);
7695
7696 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7697 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7698 Work around this problem here. */
7699 if (cu->per_cu->lang == language_cplus
7700 && parent->tag == DW_TAG_namespace
7701 && strcmp (parent->name (cu), "::") == 0
7702 && grandparent_scope == NULL)
7703 {
7704 parent->scope = NULL;
7705 parent->scope_set = 1;
7706 return NULL;
7707 }
7708
7709 /* Nested subroutines in Fortran get a prefix. */
7710 if (pdi->tag == DW_TAG_enumerator)
7711 /* Enumerators should not get the name of the enumeration as a prefix. */
7712 parent->scope = grandparent_scope;
7713 else if (parent->tag == DW_TAG_namespace
7714 || parent->tag == DW_TAG_module
7715 || parent->tag == DW_TAG_structure_type
7716 || parent->tag == DW_TAG_class_type
7717 || parent->tag == DW_TAG_interface_type
7718 || parent->tag == DW_TAG_union_type
7719 || parent->tag == DW_TAG_enumeration_type
7720 || (cu->per_cu->lang == language_fortran
7721 && parent->tag == DW_TAG_subprogram
7722 && pdi->tag == DW_TAG_subprogram))
7723 {
7724 if (grandparent_scope == NULL)
7725 parent->scope = parent->name (cu);
7726 else
7727 parent->scope = typename_concat (&cu->comp_unit_obstack,
7728 grandparent_scope,
7729 parent->name (cu), 0, cu);
7730 }
7731 else
7732 {
7733 /* FIXME drow/2004-04-01: What should we be doing with
7734 function-local names? For partial symbols, we should probably be
7735 ignoring them. */
7736 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7737 dwarf_tag_name (parent->tag),
7738 sect_offset_str (pdi->sect_off));
7739 parent->scope = grandparent_scope;
7740 }
7741
7742 parent->scope_set = 1;
7743 return parent->scope;
7744 }
7745
7746 /* Return the fully scoped name associated with PDI, from compilation unit
7747 CU. The result will be allocated with malloc. */
7748
7749 static gdb::unique_xmalloc_ptr<char>
7750 partial_die_full_name (struct partial_die_info *pdi,
7751 struct dwarf2_cu *cu)
7752 {
7753 const char *parent_scope;
7754
7755 /* If this is a template instantiation, we can not work out the
7756 template arguments from partial DIEs. So, unfortunately, we have
7757 to go through the full DIEs. At least any work we do building
7758 types here will be reused if full symbols are loaded later. */
7759 if (pdi->has_template_arguments)
7760 {
7761 pdi->fixup (cu);
7762
7763 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
7764 {
7765 struct die_info *die;
7766 struct attribute attr;
7767 struct dwarf2_cu *ref_cu = cu;
7768
7769 /* DW_FORM_ref_addr is using section offset. */
7770 attr.name = (enum dwarf_attribute) 0;
7771 attr.form = DW_FORM_ref_addr;
7772 attr.u.unsnd = to_underlying (pdi->sect_off);
7773 die = follow_die_ref (NULL, &attr, &ref_cu);
7774
7775 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7776 }
7777 }
7778
7779 parent_scope = partial_die_parent_scope (pdi, cu);
7780 if (parent_scope == NULL)
7781 return NULL;
7782 else
7783 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7784 pdi->name (cu),
7785 0, cu));
7786 }
7787
7788 static void
7789 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7790 {
7791 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7792 struct objfile *objfile = per_objfile->objfile;
7793 struct gdbarch *gdbarch = objfile->arch ();
7794 CORE_ADDR addr = 0;
7795 const char *actual_name = NULL;
7796 CORE_ADDR baseaddr;
7797
7798 baseaddr = objfile->text_section_offset ();
7799
7800 gdb::unique_xmalloc_ptr<char> built_actual_name
7801 = partial_die_full_name (pdi, cu);
7802 if (built_actual_name != NULL)
7803 actual_name = built_actual_name.get ();
7804
7805 if (actual_name == NULL)
7806 actual_name = pdi->name (cu);
7807
7808 partial_symbol psymbol;
7809 memset (&psymbol, 0, sizeof (psymbol));
7810 psymbol.ginfo.set_language (cu->per_cu->lang,
7811 &objfile->objfile_obstack);
7812 psymbol.ginfo.set_section_index (-1);
7813
7814 /* The code below indicates that the psymbol should be installed by
7815 setting this. */
7816 gdb::optional<psymbol_placement> where;
7817
7818 switch (pdi->tag)
7819 {
7820 case DW_TAG_inlined_subroutine:
7821 case DW_TAG_subprogram:
7822 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7823 - baseaddr);
7824 if (pdi->is_external
7825 || cu->per_cu->lang == language_ada
7826 || (cu->per_cu->lang == language_fortran
7827 && pdi->die_parent != NULL
7828 && pdi->die_parent->tag == DW_TAG_subprogram))
7829 {
7830 /* Normally, only "external" DIEs are part of the global scope.
7831 But in Ada and Fortran, we want to be able to access nested
7832 procedures globally. So all Ada and Fortran subprograms are
7833 stored in the global scope. */
7834 where = psymbol_placement::GLOBAL;
7835 }
7836 else
7837 where = psymbol_placement::STATIC;
7838
7839 psymbol.domain = VAR_DOMAIN;
7840 psymbol.aclass = LOC_BLOCK;
7841 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7842 psymbol.ginfo.value.address = addr;
7843
7844 if (pdi->main_subprogram && actual_name != NULL)
7845 set_objfile_main_name (objfile, actual_name, cu->per_cu->lang);
7846 break;
7847 case DW_TAG_constant:
7848 psymbol.domain = VAR_DOMAIN;
7849 psymbol.aclass = LOC_STATIC;
7850 where = (pdi->is_external
7851 ? psymbol_placement::GLOBAL
7852 : psymbol_placement::STATIC);
7853 break;
7854 case DW_TAG_variable:
7855 if (pdi->d.locdesc)
7856 addr = decode_locdesc (pdi->d.locdesc, cu);
7857
7858 if (pdi->d.locdesc
7859 && addr == 0
7860 && !per_objfile->per_bfd->has_section_at_zero)
7861 {
7862 /* A global or static variable may also have been stripped
7863 out by the linker if unused, in which case its address
7864 will be nullified; do not add such variables into partial
7865 symbol table then. */
7866 }
7867 else if (pdi->is_external)
7868 {
7869 /* Global Variable.
7870 Don't enter into the minimal symbol tables as there is
7871 a minimal symbol table entry from the ELF symbols already.
7872 Enter into partial symbol table if it has a location
7873 descriptor or a type.
7874 If the location descriptor is missing, new_symbol will create
7875 a LOC_UNRESOLVED symbol, the address of the variable will then
7876 be determined from the minimal symbol table whenever the variable
7877 is referenced.
7878 The address for the partial symbol table entry is not
7879 used by GDB, but it comes in handy for debugging partial symbol
7880 table building. */
7881
7882 if (pdi->d.locdesc || pdi->has_type)
7883 {
7884 psymbol.domain = VAR_DOMAIN;
7885 psymbol.aclass = LOC_STATIC;
7886 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7887 psymbol.ginfo.value.address = addr;
7888 where = psymbol_placement::GLOBAL;
7889 }
7890 }
7891 else
7892 {
7893 int has_loc = pdi->d.locdesc != NULL;
7894
7895 /* Static Variable. Skip symbols whose value we cannot know (those
7896 without location descriptors or constant values). */
7897 if (!has_loc && !pdi->has_const_value)
7898 return;
7899
7900 psymbol.domain = VAR_DOMAIN;
7901 psymbol.aclass = LOC_STATIC;
7902 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7903 if (has_loc)
7904 psymbol.ginfo.value.address = addr;
7905 where = psymbol_placement::STATIC;
7906 }
7907 break;
7908 case DW_TAG_array_type:
7909 case DW_TAG_typedef:
7910 case DW_TAG_base_type:
7911 case DW_TAG_subrange_type:
7912 psymbol.domain = VAR_DOMAIN;
7913 psymbol.aclass = LOC_TYPEDEF;
7914 where = psymbol_placement::STATIC;
7915 break;
7916 case DW_TAG_imported_declaration:
7917 case DW_TAG_namespace:
7918 psymbol.domain = VAR_DOMAIN;
7919 psymbol.aclass = LOC_TYPEDEF;
7920 where = psymbol_placement::GLOBAL;
7921 break;
7922 case DW_TAG_module:
7923 /* With Fortran 77 there might be a "BLOCK DATA" module
7924 available without any name. If so, we skip the module as it
7925 doesn't bring any value. */
7926 if (actual_name != nullptr)
7927 {
7928 psymbol.domain = MODULE_DOMAIN;
7929 psymbol.aclass = LOC_TYPEDEF;
7930 where = psymbol_placement::GLOBAL;
7931 }
7932 break;
7933 case DW_TAG_class_type:
7934 case DW_TAG_interface_type:
7935 case DW_TAG_structure_type:
7936 case DW_TAG_union_type:
7937 case DW_TAG_enumeration_type:
7938 /* Skip external references. The DWARF standard says in the section
7939 about "Structure, Union, and Class Type Entries": "An incomplete
7940 structure, union or class type is represented by a structure,
7941 union or class entry that does not have a byte size attribute
7942 and that has a DW_AT_declaration attribute." */
7943 if (!pdi->has_byte_size && pdi->is_declaration)
7944 return;
7945
7946 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7947 static vs. global. */
7948 psymbol.domain = STRUCT_DOMAIN;
7949 psymbol.aclass = LOC_TYPEDEF;
7950 where = (cu->per_cu->lang == language_cplus
7951 ? psymbol_placement::GLOBAL
7952 : psymbol_placement::STATIC);
7953 break;
7954 case DW_TAG_enumerator:
7955 psymbol.domain = VAR_DOMAIN;
7956 psymbol.aclass = LOC_CONST;
7957 where = (cu->per_cu->lang == language_cplus
7958 ? psymbol_placement::GLOBAL
7959 : psymbol_placement::STATIC);
7960 break;
7961 default:
7962 break;
7963 }
7964
7965 if (where.has_value ())
7966 {
7967 if (built_actual_name != nullptr)
7968 actual_name = objfile->intern (actual_name);
7969 if (pdi->linkage_name == nullptr
7970 || cu->per_cu->lang == language_ada)
7971 psymbol.ginfo.set_linkage_name (actual_name);
7972 else
7973 {
7974 psymbol.ginfo.set_demangled_name (actual_name,
7975 &objfile->objfile_obstack);
7976 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
7977 }
7978 cu->per_cu->v.psymtab->add_psymbol
7979 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
7980 objfile);
7981 }
7982 }
7983
7984 /* Read a partial die corresponding to a namespace; also, add a symbol
7985 corresponding to that namespace to the symbol table. NAMESPACE is
7986 the name of the enclosing namespace. */
7987
7988 static void
7989 add_partial_namespace (struct partial_die_info *pdi,
7990 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7991 int set_addrmap, struct dwarf2_cu *cu)
7992 {
7993 /* Add a symbol for the namespace. */
7994
7995 add_partial_symbol (pdi, cu);
7996
7997 /* Now scan partial symbols in that namespace. */
7998
7999 if (pdi->has_children)
8000 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8001 }
8002
8003 /* Read a partial die corresponding to a Fortran module. */
8004
8005 static void
8006 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8007 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8008 {
8009 /* Add a symbol for the namespace. */
8010
8011 add_partial_symbol (pdi, cu);
8012
8013 /* Now scan partial symbols in that module. */
8014
8015 if (pdi->has_children)
8016 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8017 }
8018
8019 /* Read a partial die corresponding to a subprogram or an inlined
8020 subprogram and create a partial symbol for that subprogram.
8021 When the CU language allows it, this routine also defines a partial
8022 symbol for each nested subprogram that this subprogram contains.
8023 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8024 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8025
8026 PDI may also be a lexical block, in which case we simply search
8027 recursively for subprograms defined inside that lexical block.
8028 Again, this is only performed when the CU language allows this
8029 type of definitions. */
8030
8031 static void
8032 add_partial_subprogram (struct partial_die_info *pdi,
8033 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8034 int set_addrmap, struct dwarf2_cu *cu)
8035 {
8036 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8037 {
8038 if (pdi->has_pc_info)
8039 {
8040 if (pdi->lowpc < *lowpc)
8041 *lowpc = pdi->lowpc;
8042 if (pdi->highpc > *highpc)
8043 *highpc = pdi->highpc;
8044 if (set_addrmap)
8045 {
8046 struct objfile *objfile = cu->per_objfile->objfile;
8047 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8048 struct gdbarch *gdbarch = objfile->arch ();
8049 CORE_ADDR baseaddr;
8050 CORE_ADDR this_highpc;
8051 CORE_ADDR this_lowpc;
8052
8053 baseaddr = objfile->text_section_offset ();
8054 this_lowpc
8055 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8056 pdi->lowpc + baseaddr)
8057 - baseaddr);
8058 this_highpc
8059 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8060 pdi->highpc + baseaddr)
8061 - baseaddr);
8062 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8063 this_lowpc, this_highpc - 1,
8064 cu->per_cu->v.psymtab);
8065 }
8066 }
8067
8068 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8069 {
8070 if (!pdi->is_declaration)
8071 /* Ignore subprogram DIEs that do not have a name, they are
8072 illegal. Do not emit a complaint at this point, we will
8073 do so when we convert this psymtab into a symtab. */
8074 if (pdi->name (cu))
8075 add_partial_symbol (pdi, cu);
8076 }
8077 }
8078
8079 if (! pdi->has_children)
8080 return;
8081
8082 if (cu->per_cu->lang == language_ada
8083 || cu->per_cu->lang == language_fortran)
8084 {
8085 pdi = pdi->die_child;
8086 while (pdi != NULL)
8087 {
8088 pdi->fixup (cu);
8089 if (pdi->tag == DW_TAG_subprogram
8090 || pdi->tag == DW_TAG_inlined_subroutine
8091 || pdi->tag == DW_TAG_lexical_block)
8092 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8093 pdi = pdi->die_sibling;
8094 }
8095 }
8096 }
8097
8098 /* Read a partial die corresponding to an enumeration type. */
8099
8100 static void
8101 add_partial_enumeration (struct partial_die_info *enum_pdi,
8102 struct dwarf2_cu *cu)
8103 {
8104 struct partial_die_info *pdi;
8105
8106 if (enum_pdi->name (cu) != NULL)
8107 add_partial_symbol (enum_pdi, cu);
8108
8109 pdi = enum_pdi->die_child;
8110 while (pdi)
8111 {
8112 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8113 complaint (_("malformed enumerator DIE ignored"));
8114 else
8115 add_partial_symbol (pdi, cu);
8116 pdi = pdi->die_sibling;
8117 }
8118 }
8119
8120 /* Return the initial uleb128 in the die at INFO_PTR. */
8121
8122 static unsigned int
8123 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8124 {
8125 unsigned int bytes_read;
8126
8127 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8128 }
8129
8130 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8131 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8132
8133 Return the corresponding abbrev, or NULL if the number is zero (indicating
8134 an empty DIE). In either case *BYTES_READ will be set to the length of
8135 the initial number. */
8136
8137 static const struct abbrev_info *
8138 peek_die_abbrev (const die_reader_specs &reader,
8139 const gdb_byte *info_ptr, unsigned int *bytes_read)
8140 {
8141 dwarf2_cu *cu = reader.cu;
8142 bfd *abfd = reader.abfd;
8143 unsigned int abbrev_number
8144 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8145
8146 if (abbrev_number == 0)
8147 return NULL;
8148
8149 const abbrev_info *abbrev
8150 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8151 if (!abbrev)
8152 {
8153 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8154 " at offset %s [in module %s]"),
8155 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8156 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8157 }
8158
8159 return abbrev;
8160 }
8161
8162 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8163 Returns a pointer to the end of a series of DIEs, terminated by an empty
8164 DIE. Any children of the skipped DIEs will also be skipped. */
8165
8166 static const gdb_byte *
8167 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8168 {
8169 while (1)
8170 {
8171 unsigned int bytes_read;
8172 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8173 &bytes_read);
8174
8175 if (abbrev == NULL)
8176 return info_ptr + bytes_read;
8177 else
8178 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8179 }
8180 }
8181
8182 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8183 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8184 abbrev corresponding to that skipped uleb128 should be passed in
8185 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8186 children. */
8187
8188 static const gdb_byte *
8189 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8190 const struct abbrev_info *abbrev)
8191 {
8192 unsigned int bytes_read;
8193 struct attribute attr;
8194 bfd *abfd = reader->abfd;
8195 struct dwarf2_cu *cu = reader->cu;
8196 const gdb_byte *buffer = reader->buffer;
8197 const gdb_byte *buffer_end = reader->buffer_end;
8198 unsigned int form, i;
8199
8200 for (i = 0; i < abbrev->num_attrs; i++)
8201 {
8202 /* The only abbrev we care about is DW_AT_sibling. */
8203 if (abbrev->attrs[i].name == DW_AT_sibling)
8204 {
8205 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8206 if (attr.form == DW_FORM_ref_addr)
8207 complaint (_("ignoring absolute DW_AT_sibling"));
8208 else
8209 {
8210 sect_offset off = attr.get_ref_die_offset ();
8211 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8212
8213 if (sibling_ptr < info_ptr)
8214 complaint (_("DW_AT_sibling points backwards"));
8215 else if (sibling_ptr > reader->buffer_end)
8216 reader->die_section->overflow_complaint ();
8217 else
8218 return sibling_ptr;
8219 }
8220 }
8221
8222 /* If it isn't DW_AT_sibling, skip this attribute. */
8223 form = abbrev->attrs[i].form;
8224 skip_attribute:
8225 switch (form)
8226 {
8227 case DW_FORM_ref_addr:
8228 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8229 and later it is offset sized. */
8230 if (cu->header.version == 2)
8231 info_ptr += cu->header.addr_size;
8232 else
8233 info_ptr += cu->header.offset_size;
8234 break;
8235 case DW_FORM_GNU_ref_alt:
8236 info_ptr += cu->header.offset_size;
8237 break;
8238 case DW_FORM_addr:
8239 info_ptr += cu->header.addr_size;
8240 break;
8241 case DW_FORM_data1:
8242 case DW_FORM_ref1:
8243 case DW_FORM_flag:
8244 case DW_FORM_strx1:
8245 info_ptr += 1;
8246 break;
8247 case DW_FORM_flag_present:
8248 case DW_FORM_implicit_const:
8249 break;
8250 case DW_FORM_data2:
8251 case DW_FORM_ref2:
8252 case DW_FORM_strx2:
8253 info_ptr += 2;
8254 break;
8255 case DW_FORM_strx3:
8256 info_ptr += 3;
8257 break;
8258 case DW_FORM_data4:
8259 case DW_FORM_ref4:
8260 case DW_FORM_strx4:
8261 info_ptr += 4;
8262 break;
8263 case DW_FORM_data8:
8264 case DW_FORM_ref8:
8265 case DW_FORM_ref_sig8:
8266 info_ptr += 8;
8267 break;
8268 case DW_FORM_data16:
8269 info_ptr += 16;
8270 break;
8271 case DW_FORM_string:
8272 read_direct_string (abfd, info_ptr, &bytes_read);
8273 info_ptr += bytes_read;
8274 break;
8275 case DW_FORM_sec_offset:
8276 case DW_FORM_strp:
8277 case DW_FORM_GNU_strp_alt:
8278 info_ptr += cu->header.offset_size;
8279 break;
8280 case DW_FORM_exprloc:
8281 case DW_FORM_block:
8282 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8283 info_ptr += bytes_read;
8284 break;
8285 case DW_FORM_block1:
8286 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8287 break;
8288 case DW_FORM_block2:
8289 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8290 break;
8291 case DW_FORM_block4:
8292 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8293 break;
8294 case DW_FORM_addrx:
8295 case DW_FORM_strx:
8296 case DW_FORM_sdata:
8297 case DW_FORM_udata:
8298 case DW_FORM_ref_udata:
8299 case DW_FORM_GNU_addr_index:
8300 case DW_FORM_GNU_str_index:
8301 case DW_FORM_rnglistx:
8302 case DW_FORM_loclistx:
8303 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8304 break;
8305 case DW_FORM_indirect:
8306 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8307 info_ptr += bytes_read;
8308 /* We need to continue parsing from here, so just go back to
8309 the top. */
8310 goto skip_attribute;
8311
8312 default:
8313 error (_("Dwarf Error: Cannot handle %s "
8314 "in DWARF reader [in module %s]"),
8315 dwarf_form_name (form),
8316 bfd_get_filename (abfd));
8317 }
8318 }
8319
8320 if (abbrev->has_children)
8321 return skip_children (reader, info_ptr);
8322 else
8323 return info_ptr;
8324 }
8325
8326 /* Locate ORIG_PDI's sibling.
8327 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8328
8329 static const gdb_byte *
8330 locate_pdi_sibling (const struct die_reader_specs *reader,
8331 struct partial_die_info *orig_pdi,
8332 const gdb_byte *info_ptr)
8333 {
8334 /* Do we know the sibling already? */
8335
8336 if (orig_pdi->sibling)
8337 return orig_pdi->sibling;
8338
8339 /* Are there any children to deal with? */
8340
8341 if (!orig_pdi->has_children)
8342 return info_ptr;
8343
8344 /* Skip the children the long way. */
8345
8346 return skip_children (reader, info_ptr);
8347 }
8348
8349 /* Expand this partial symbol table into a full symbol table. SELF is
8350 not NULL. */
8351
8352 void
8353 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8354 {
8355 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8356
8357 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8358
8359 /* If this psymtab is constructed from a debug-only objfile, the
8360 has_section_at_zero flag will not necessarily be correct. We
8361 can get the correct value for this flag by looking at the data
8362 associated with the (presumably stripped) associated objfile. */
8363 if (objfile->separate_debug_objfile_backlink)
8364 {
8365 dwarf2_per_objfile *per_objfile_backlink
8366 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8367
8368 per_objfile->per_bfd->has_section_at_zero
8369 = per_objfile_backlink->per_bfd->has_section_at_zero;
8370 }
8371
8372 expand_psymtab (objfile);
8373
8374 process_cu_includes (per_objfile);
8375 }
8376 \f
8377 /* Reading in full CUs. */
8378
8379 /* Add PER_CU to the queue. */
8380
8381 static void
8382 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8383 dwarf2_per_objfile *per_objfile,
8384 enum language pretend_language)
8385 {
8386 per_cu->queued = 1;
8387
8388 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8389 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8390 }
8391
8392 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8393
8394 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8395 dependency.
8396
8397 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8398 DIEs, false otherwise.
8399
8400 Explanation: there is an invariant that if a CU is queued for expansion
8401 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8402 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8403 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8404 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8405 invariant is respected.
8406
8407 The caller is therefore not required to load the CU's DIEs (we return false)
8408 if:
8409
8410 - the CU is already expanded, and therefore does not get enqueued
8411 - the CU gets enqueued for expansion, but its DIEs are already loaded
8412
8413 Note that the caller should not use this function's return value as an
8414 indicator of whether the CU's DIEs are loaded right now, it should check
8415 that by calling `dwarf2_per_objfile::get_cu` instead. */
8416
8417 static int
8418 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8419 dwarf2_per_cu_data *per_cu,
8420 dwarf2_per_objfile *per_objfile,
8421 enum language pretend_language)
8422 {
8423 /* We may arrive here during partial symbol reading, if we need full
8424 DIEs to process an unusual case (e.g. template arguments). Do
8425 not queue PER_CU, just tell our caller to load its DIEs. */
8426 if (per_cu->per_bfd->reading_partial_symbols)
8427 {
8428 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8429
8430 if (cu == NULL || cu->dies == NULL)
8431 return 1;
8432 return 0;
8433 }
8434
8435 /* Mark the dependence relation so that we don't flush PER_CU
8436 too early. */
8437 if (dependent_cu != NULL)
8438 dependent_cu->add_dependence (per_cu);
8439
8440 /* If it's already on the queue, we have nothing to do. */
8441 if (per_cu->queued)
8442 {
8443 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8444 loaded. */
8445 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8446
8447 /* If the CU is queued for expansion, it should not already be
8448 expanded. */
8449 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8450
8451 /* The DIEs are already loaded, the caller doesn't need to do it. */
8452 return 0;
8453 }
8454
8455 bool queued = false;
8456 if (!per_objfile->symtab_set_p (per_cu))
8457 {
8458 /* Add it to the queue. */
8459 queue_comp_unit (per_cu, per_objfile, pretend_language);
8460 queued = true;
8461 }
8462
8463 /* If the compilation unit is already loaded, just mark it as
8464 used. */
8465 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8466 if (cu != nullptr)
8467 cu->last_used = 0;
8468
8469 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8470 and the DIEs are not already loaded. */
8471 return queued && cu == nullptr;
8472 }
8473
8474 /* Process the queue. */
8475
8476 static void
8477 process_queue (dwarf2_per_objfile *per_objfile)
8478 {
8479 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8480 objfile_name (per_objfile->objfile));
8481
8482 /* The queue starts out with one item, but following a DIE reference
8483 may load a new CU, adding it to the end of the queue. */
8484 while (!per_objfile->per_bfd->queue->empty ())
8485 {
8486 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8487 dwarf2_per_cu_data *per_cu = item.per_cu;
8488
8489 if (!per_objfile->symtab_set_p (per_cu))
8490 {
8491 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8492
8493 /* Skip dummy CUs. */
8494 if (cu != nullptr)
8495 {
8496 unsigned int debug_print_threshold;
8497 char buf[100];
8498
8499 if (per_cu->is_debug_types)
8500 {
8501 struct signatured_type *sig_type =
8502 (struct signatured_type *) per_cu;
8503
8504 sprintf (buf, "TU %s at offset %s",
8505 hex_string (sig_type->signature),
8506 sect_offset_str (per_cu->sect_off));
8507 /* There can be 100s of TUs.
8508 Only print them in verbose mode. */
8509 debug_print_threshold = 2;
8510 }
8511 else
8512 {
8513 sprintf (buf, "CU at offset %s",
8514 sect_offset_str (per_cu->sect_off));
8515 debug_print_threshold = 1;
8516 }
8517
8518 if (dwarf_read_debug >= debug_print_threshold)
8519 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8520
8521 if (per_cu->is_debug_types)
8522 process_full_type_unit (cu, item.pretend_language);
8523 else
8524 process_full_comp_unit (cu, item.pretend_language);
8525
8526 if (dwarf_read_debug >= debug_print_threshold)
8527 dwarf_read_debug_printf ("Done expanding %s", buf);
8528 }
8529 }
8530
8531 per_cu->queued = 0;
8532 per_objfile->per_bfd->queue->pop ();
8533 }
8534
8535 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8536 objfile_name (per_objfile->objfile));
8537 }
8538
8539 /* Read in full symbols for PST, and anything it depends on. */
8540
8541 void
8542 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8543 {
8544 gdb_assert (!readin_p (objfile));
8545
8546 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8547 free_cached_comp_units freer (per_objfile);
8548 expand_dependencies (objfile);
8549
8550 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8551 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8552 }
8553
8554 /* See psympriv.h. */
8555
8556 bool
8557 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8558 {
8559 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8560 return per_objfile->symtab_set_p (per_cu_data);
8561 }
8562
8563 /* See psympriv.h. */
8564
8565 compunit_symtab *
8566 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8567 {
8568 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8569 return per_objfile->get_symtab (per_cu_data);
8570 }
8571
8572 /* Trivial hash function for die_info: the hash value of a DIE
8573 is its offset in .debug_info for this objfile. */
8574
8575 static hashval_t
8576 die_hash (const void *item)
8577 {
8578 const struct die_info *die = (const struct die_info *) item;
8579
8580 return to_underlying (die->sect_off);
8581 }
8582
8583 /* Trivial comparison function for die_info structures: two DIEs
8584 are equal if they have the same offset. */
8585
8586 static int
8587 die_eq (const void *item_lhs, const void *item_rhs)
8588 {
8589 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8590 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8591
8592 return die_lhs->sect_off == die_rhs->sect_off;
8593 }
8594
8595 /* Load the DIEs associated with PER_CU into memory.
8596
8597 In some cases, the caller, while reading partial symbols, will need to load
8598 the full symbols for the CU for some reason. It will already have a
8599 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8600 rather than creating a new one. */
8601
8602 static void
8603 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8604 dwarf2_per_objfile *per_objfile,
8605 dwarf2_cu *existing_cu,
8606 bool skip_partial,
8607 enum language pretend_language)
8608 {
8609 gdb_assert (! this_cu->is_debug_types);
8610
8611 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8612 if (reader.dummy_p)
8613 return;
8614
8615 struct dwarf2_cu *cu = reader.cu;
8616 const gdb_byte *info_ptr = reader.info_ptr;
8617
8618 gdb_assert (cu->die_hash == NULL);
8619 cu->die_hash =
8620 htab_create_alloc_ex (cu->header.length / 12,
8621 die_hash,
8622 die_eq,
8623 NULL,
8624 &cu->comp_unit_obstack,
8625 hashtab_obstack_allocate,
8626 dummy_obstack_deallocate);
8627
8628 if (reader.comp_unit_die->has_children)
8629 reader.comp_unit_die->child
8630 = read_die_and_siblings (&reader, reader.info_ptr,
8631 &info_ptr, reader.comp_unit_die);
8632 cu->dies = reader.comp_unit_die;
8633 /* comp_unit_die is not stored in die_hash, no need. */
8634
8635 /* We try not to read any attributes in this function, because not
8636 all CUs needed for references have been loaded yet, and symbol
8637 table processing isn't initialized. But we have to set the CU language,
8638 or we won't be able to build types correctly.
8639 Similarly, if we do not read the producer, we can not apply
8640 producer-specific interpretation. */
8641 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8642
8643 reader.keep ();
8644 }
8645
8646 /* Add a DIE to the delayed physname list. */
8647
8648 static void
8649 add_to_method_list (struct type *type, int fnfield_index, int index,
8650 const char *name, struct die_info *die,
8651 struct dwarf2_cu *cu)
8652 {
8653 struct delayed_method_info mi;
8654 mi.type = type;
8655 mi.fnfield_index = fnfield_index;
8656 mi.index = index;
8657 mi.name = name;
8658 mi.die = die;
8659 cu->method_list.push_back (mi);
8660 }
8661
8662 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8663 "const" / "volatile". If so, decrements LEN by the length of the
8664 modifier and return true. Otherwise return false. */
8665
8666 template<size_t N>
8667 static bool
8668 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8669 {
8670 size_t mod_len = sizeof (mod) - 1;
8671 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8672 {
8673 len -= mod_len;
8674 return true;
8675 }
8676 return false;
8677 }
8678
8679 /* Compute the physnames of any methods on the CU's method list.
8680
8681 The computation of method physnames is delayed in order to avoid the
8682 (bad) condition that one of the method's formal parameters is of an as yet
8683 incomplete type. */
8684
8685 static void
8686 compute_delayed_physnames (struct dwarf2_cu *cu)
8687 {
8688 /* Only C++ delays computing physnames. */
8689 if (cu->method_list.empty ())
8690 return;
8691 gdb_assert (cu->per_cu->lang == language_cplus);
8692
8693 for (const delayed_method_info &mi : cu->method_list)
8694 {
8695 const char *physname;
8696 struct fn_fieldlist *fn_flp
8697 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8698 physname = dwarf2_physname (mi.name, mi.die, cu);
8699 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8700 = physname ? physname : "";
8701
8702 /* Since there's no tag to indicate whether a method is a
8703 const/volatile overload, extract that information out of the
8704 demangled name. */
8705 if (physname != NULL)
8706 {
8707 size_t len = strlen (physname);
8708
8709 while (1)
8710 {
8711 if (physname[len] == ')') /* shortcut */
8712 break;
8713 else if (check_modifier (physname, len, " const"))
8714 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8715 else if (check_modifier (physname, len, " volatile"))
8716 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8717 else
8718 break;
8719 }
8720 }
8721 }
8722
8723 /* The list is no longer needed. */
8724 cu->method_list.clear ();
8725 }
8726
8727 /* Go objects should be embedded in a DW_TAG_module DIE,
8728 and it's not clear if/how imported objects will appear.
8729 To keep Go support simple until that's worked out,
8730 go back through what we've read and create something usable.
8731 We could do this while processing each DIE, and feels kinda cleaner,
8732 but that way is more invasive.
8733 This is to, for example, allow the user to type "p var" or "b main"
8734 without having to specify the package name, and allow lookups
8735 of module.object to work in contexts that use the expression
8736 parser. */
8737
8738 static void
8739 fixup_go_packaging (struct dwarf2_cu *cu)
8740 {
8741 gdb::unique_xmalloc_ptr<char> package_name;
8742 struct pending *list;
8743 int i;
8744
8745 for (list = *cu->get_builder ()->get_global_symbols ();
8746 list != NULL;
8747 list = list->next)
8748 {
8749 for (i = 0; i < list->nsyms; ++i)
8750 {
8751 struct symbol *sym = list->symbol[i];
8752
8753 if (sym->language () == language_go
8754 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8755 {
8756 gdb::unique_xmalloc_ptr<char> this_package_name
8757 (go_symbol_package_name (sym));
8758
8759 if (this_package_name == NULL)
8760 continue;
8761 if (package_name == NULL)
8762 package_name = std::move (this_package_name);
8763 else
8764 {
8765 struct objfile *objfile = cu->per_objfile->objfile;
8766 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8767 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8768 (symbol_symtab (sym) != NULL
8769 ? symtab_to_filename_for_display
8770 (symbol_symtab (sym))
8771 : objfile_name (objfile)),
8772 this_package_name.get (), package_name.get ());
8773 }
8774 }
8775 }
8776 }
8777
8778 if (package_name != NULL)
8779 {
8780 struct objfile *objfile = cu->per_objfile->objfile;
8781 const char *saved_package_name = objfile->intern (package_name.get ());
8782 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8783 saved_package_name);
8784 struct symbol *sym;
8785
8786 sym = new (&objfile->objfile_obstack) symbol;
8787 sym->set_language (language_go, &objfile->objfile_obstack);
8788 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8789 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8790 e.g., "main" finds the "main" module and not C's main(). */
8791 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8792 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8793 SYMBOL_TYPE (sym) = type;
8794
8795 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
8796 }
8797 }
8798
8799 /* Allocate a fully-qualified name consisting of the two parts on the
8800 obstack. */
8801
8802 static const char *
8803 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8804 {
8805 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8806 }
8807
8808 /* A helper that allocates a variant part to attach to a Rust enum
8809 type. OBSTACK is where the results should be allocated. TYPE is
8810 the type we're processing. DISCRIMINANT_INDEX is the index of the
8811 discriminant. It must be the index of one of the fields of TYPE,
8812 or -1 to mean there is no discriminant (univariant enum).
8813 DEFAULT_INDEX is the index of the default field; or -1 if there is
8814 no default. RANGES is indexed by "effective" field number (the
8815 field index, but omitting the discriminant and default fields) and
8816 must hold the discriminant values used by the variants. Note that
8817 RANGES must have a lifetime at least as long as OBSTACK -- either
8818 already allocated on it, or static. */
8819
8820 static void
8821 alloc_rust_variant (struct obstack *obstack, struct type *type,
8822 int discriminant_index, int default_index,
8823 gdb::array_view<discriminant_range> ranges)
8824 {
8825 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8826 gdb_assert (discriminant_index == -1
8827 || (discriminant_index >= 0
8828 && discriminant_index < type->num_fields ()));
8829 gdb_assert (default_index == -1
8830 || (default_index >= 0 && default_index < type->num_fields ()));
8831
8832 /* We have one variant for each non-discriminant field. */
8833 int n_variants = type->num_fields ();
8834 if (discriminant_index != -1)
8835 --n_variants;
8836
8837 variant *variants = new (obstack) variant[n_variants];
8838 int var_idx = 0;
8839 int range_idx = 0;
8840 for (int i = 0; i < type->num_fields (); ++i)
8841 {
8842 if (i == discriminant_index)
8843 continue;
8844
8845 variants[var_idx].first_field = i;
8846 variants[var_idx].last_field = i + 1;
8847
8848 /* The default field does not need a range, but other fields do.
8849 We skipped the discriminant above. */
8850 if (i != default_index)
8851 {
8852 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8853 ++range_idx;
8854 }
8855
8856 ++var_idx;
8857 }
8858
8859 gdb_assert (range_idx == ranges.size ());
8860 gdb_assert (var_idx == n_variants);
8861
8862 variant_part *part = new (obstack) variant_part;
8863 part->discriminant_index = discriminant_index;
8864 /* If there is no discriminant, then whether it is signed is of no
8865 consequence. */
8866 part->is_unsigned
8867 = (discriminant_index == -1
8868 ? false
8869 : type->field (discriminant_index).type ()->is_unsigned ());
8870 part->variants = gdb::array_view<variant> (variants, n_variants);
8871
8872 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
8873 gdb::array_view<variant_part> *prop_value
8874 = new (storage) gdb::array_view<variant_part> (part, 1);
8875
8876 struct dynamic_prop prop;
8877 prop.set_variant_parts (prop_value);
8878
8879 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
8880 }
8881
8882 /* Some versions of rustc emitted enums in an unusual way.
8883
8884 Ordinary enums were emitted as unions. The first element of each
8885 structure in the union was named "RUST$ENUM$DISR". This element
8886 held the discriminant.
8887
8888 These versions of Rust also implemented the "non-zero"
8889 optimization. When the enum had two values, and one is empty and
8890 the other holds a pointer that cannot be zero, the pointer is used
8891 as the discriminant, with a zero value meaning the empty variant.
8892 Here, the union's first member is of the form
8893 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
8894 where the fieldnos are the indices of the fields that should be
8895 traversed in order to find the field (which may be several fields deep)
8896 and the variantname is the name of the variant of the case when the
8897 field is zero.
8898
8899 This function recognizes whether TYPE is of one of these forms,
8900 and, if so, smashes it to be a variant type. */
8901
8902 static void
8903 quirk_rust_enum (struct type *type, struct objfile *objfile)
8904 {
8905 gdb_assert (type->code () == TYPE_CODE_UNION);
8906
8907 /* We don't need to deal with empty enums. */
8908 if (type->num_fields () == 0)
8909 return;
8910
8911 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
8912 if (type->num_fields () == 1
8913 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
8914 {
8915 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
8916
8917 /* Decode the field name to find the offset of the
8918 discriminant. */
8919 ULONGEST bit_offset = 0;
8920 struct type *field_type = type->field (0).type ();
8921 while (name[0] >= '0' && name[0] <= '9')
8922 {
8923 char *tail;
8924 unsigned long index = strtoul (name, &tail, 10);
8925 name = tail;
8926 if (*name != '$'
8927 || index >= field_type->num_fields ()
8928 || (TYPE_FIELD_LOC_KIND (field_type, index)
8929 != FIELD_LOC_KIND_BITPOS))
8930 {
8931 complaint (_("Could not parse Rust enum encoding string \"%s\""
8932 "[in module %s]"),
8933 TYPE_FIELD_NAME (type, 0),
8934 objfile_name (objfile));
8935 return;
8936 }
8937 ++name;
8938
8939 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
8940 field_type = field_type->field (index).type ();
8941 }
8942
8943 /* Smash this type to be a structure type. We have to do this
8944 because the type has already been recorded. */
8945 type->set_code (TYPE_CODE_STRUCT);
8946 type->set_num_fields (3);
8947 /* Save the field we care about. */
8948 struct field saved_field = type->field (0);
8949 type->set_fields
8950 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
8951
8952 /* Put the discriminant at index 0. */
8953 type->field (0).set_type (field_type);
8954 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8955 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
8956 SET_FIELD_BITPOS (type->field (0), bit_offset);
8957
8958 /* The order of fields doesn't really matter, so put the real
8959 field at index 1 and the data-less field at index 2. */
8960 type->field (1) = saved_field;
8961 TYPE_FIELD_NAME (type, 1)
8962 = rust_last_path_segment (type->field (1).type ()->name ());
8963 type->field (1).type ()->set_name
8964 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8965 TYPE_FIELD_NAME (type, 1)));
8966
8967 const char *dataless_name
8968 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8969 name);
8970 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8971 dataless_name);
8972 type->field (2).set_type (dataless_type);
8973 /* NAME points into the original discriminant name, which
8974 already has the correct lifetime. */
8975 TYPE_FIELD_NAME (type, 2) = name;
8976 SET_FIELD_BITPOS (type->field (2), 0);
8977
8978 /* Indicate that this is a variant type. */
8979 static discriminant_range ranges[1] = { { 0, 0 } };
8980 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
8981 }
8982 /* A union with a single anonymous field is probably an old-style
8983 univariant enum. */
8984 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
8985 {
8986 /* Smash this type to be a structure type. We have to do this
8987 because the type has already been recorded. */
8988 type->set_code (TYPE_CODE_STRUCT);
8989
8990 struct type *field_type = type->field (0).type ();
8991 const char *variant_name
8992 = rust_last_path_segment (field_type->name ());
8993 TYPE_FIELD_NAME (type, 0) = variant_name;
8994 field_type->set_name
8995 (rust_fully_qualify (&objfile->objfile_obstack,
8996 type->name (), variant_name));
8997
8998 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
8999 }
9000 else
9001 {
9002 struct type *disr_type = nullptr;
9003 for (int i = 0; i < type->num_fields (); ++i)
9004 {
9005 disr_type = type->field (i).type ();
9006
9007 if (disr_type->code () != TYPE_CODE_STRUCT)
9008 {
9009 /* All fields of a true enum will be structs. */
9010 return;
9011 }
9012 else if (disr_type->num_fields () == 0)
9013 {
9014 /* Could be data-less variant, so keep going. */
9015 disr_type = nullptr;
9016 }
9017 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9018 "RUST$ENUM$DISR") != 0)
9019 {
9020 /* Not a Rust enum. */
9021 return;
9022 }
9023 else
9024 {
9025 /* Found one. */
9026 break;
9027 }
9028 }
9029
9030 /* If we got here without a discriminant, then it's probably
9031 just a union. */
9032 if (disr_type == nullptr)
9033 return;
9034
9035 /* Smash this type to be a structure type. We have to do this
9036 because the type has already been recorded. */
9037 type->set_code (TYPE_CODE_STRUCT);
9038
9039 /* Make space for the discriminant field. */
9040 struct field *disr_field = &disr_type->field (0);
9041 field *new_fields
9042 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9043 * sizeof (struct field)));
9044 memcpy (new_fields + 1, type->fields (),
9045 type->num_fields () * sizeof (struct field));
9046 type->set_fields (new_fields);
9047 type->set_num_fields (type->num_fields () + 1);
9048
9049 /* Install the discriminant at index 0 in the union. */
9050 type->field (0) = *disr_field;
9051 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9052 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9053
9054 /* We need a way to find the correct discriminant given a
9055 variant name. For convenience we build a map here. */
9056 struct type *enum_type = disr_field->type ();
9057 std::unordered_map<std::string, ULONGEST> discriminant_map;
9058 for (int i = 0; i < enum_type->num_fields (); ++i)
9059 {
9060 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9061 {
9062 const char *name
9063 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9064 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9065 }
9066 }
9067
9068 int n_fields = type->num_fields ();
9069 /* We don't need a range entry for the discriminant, but we do
9070 need one for every other field, as there is no default
9071 variant. */
9072 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9073 discriminant_range,
9074 n_fields - 1);
9075 /* Skip the discriminant here. */
9076 for (int i = 1; i < n_fields; ++i)
9077 {
9078 /* Find the final word in the name of this variant's type.
9079 That name can be used to look up the correct
9080 discriminant. */
9081 const char *variant_name
9082 = rust_last_path_segment (type->field (i).type ()->name ());
9083
9084 auto iter = discriminant_map.find (variant_name);
9085 if (iter != discriminant_map.end ())
9086 {
9087 ranges[i - 1].low = iter->second;
9088 ranges[i - 1].high = iter->second;
9089 }
9090
9091 /* In Rust, each element should have the size of the
9092 enclosing enum. */
9093 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9094
9095 /* Remove the discriminant field, if it exists. */
9096 struct type *sub_type = type->field (i).type ();
9097 if (sub_type->num_fields () > 0)
9098 {
9099 sub_type->set_num_fields (sub_type->num_fields () - 1);
9100 sub_type->set_fields (sub_type->fields () + 1);
9101 }
9102 TYPE_FIELD_NAME (type, i) = variant_name;
9103 sub_type->set_name
9104 (rust_fully_qualify (&objfile->objfile_obstack,
9105 type->name (), variant_name));
9106 }
9107
9108 /* Indicate that this is a variant type. */
9109 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9110 gdb::array_view<discriminant_range> (ranges,
9111 n_fields - 1));
9112 }
9113 }
9114
9115 /* Rewrite some Rust unions to be structures with variants parts. */
9116
9117 static void
9118 rust_union_quirks (struct dwarf2_cu *cu)
9119 {
9120 gdb_assert (cu->per_cu->lang == language_rust);
9121 for (type *type_ : cu->rust_unions)
9122 quirk_rust_enum (type_, cu->per_objfile->objfile);
9123 /* We don't need this any more. */
9124 cu->rust_unions.clear ();
9125 }
9126
9127 /* See read.h. */
9128
9129 type_unit_group_unshareable *
9130 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9131 {
9132 auto iter = this->m_type_units.find (tu_group);
9133 if (iter != this->m_type_units.end ())
9134 return iter->second.get ();
9135
9136 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9137 type_unit_group_unshareable *result = uniq.get ();
9138 this->m_type_units[tu_group] = std::move (uniq);
9139 return result;
9140 }
9141
9142 struct type *
9143 dwarf2_per_objfile::get_type_for_signatured_type
9144 (signatured_type *sig_type) const
9145 {
9146 auto iter = this->m_type_map.find (sig_type);
9147 if (iter == this->m_type_map.end ())
9148 return nullptr;
9149
9150 return iter->second;
9151 }
9152
9153 void dwarf2_per_objfile::set_type_for_signatured_type
9154 (signatured_type *sig_type, struct type *type)
9155 {
9156 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9157
9158 this->m_type_map[sig_type] = type;
9159 }
9160
9161 /* A helper function for computing the list of all symbol tables
9162 included by PER_CU. */
9163
9164 static void
9165 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9166 htab_t all_children, htab_t all_type_symtabs,
9167 dwarf2_per_cu_data *per_cu,
9168 dwarf2_per_objfile *per_objfile,
9169 struct compunit_symtab *immediate_parent)
9170 {
9171 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9172 if (*slot != NULL)
9173 {
9174 /* This inclusion and its children have been processed. */
9175 return;
9176 }
9177
9178 *slot = per_cu;
9179
9180 /* Only add a CU if it has a symbol table. */
9181 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9182 if (cust != NULL)
9183 {
9184 /* If this is a type unit only add its symbol table if we haven't
9185 seen it yet (type unit per_cu's can share symtabs). */
9186 if (per_cu->is_debug_types)
9187 {
9188 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9189 if (*slot == NULL)
9190 {
9191 *slot = cust;
9192 result->push_back (cust);
9193 if (cust->user == NULL)
9194 cust->user = immediate_parent;
9195 }
9196 }
9197 else
9198 {
9199 result->push_back (cust);
9200 if (cust->user == NULL)
9201 cust->user = immediate_parent;
9202 }
9203 }
9204
9205 if (!per_cu->imported_symtabs_empty ())
9206 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9207 {
9208 recursively_compute_inclusions (result, all_children,
9209 all_type_symtabs, ptr, per_objfile,
9210 cust);
9211 }
9212 }
9213
9214 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9215 PER_CU. */
9216
9217 static void
9218 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9219 dwarf2_per_objfile *per_objfile)
9220 {
9221 gdb_assert (! per_cu->is_debug_types);
9222
9223 if (!per_cu->imported_symtabs_empty ())
9224 {
9225 int len;
9226 std::vector<compunit_symtab *> result_symtabs;
9227 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9228
9229 /* If we don't have a symtab, we can just skip this case. */
9230 if (cust == NULL)
9231 return;
9232
9233 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9234 htab_eq_pointer,
9235 NULL, xcalloc, xfree));
9236 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9237 htab_eq_pointer,
9238 NULL, xcalloc, xfree));
9239
9240 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9241 {
9242 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9243 all_type_symtabs.get (), ptr,
9244 per_objfile, cust);
9245 }
9246
9247 /* Now we have a transitive closure of all the included symtabs. */
9248 len = result_symtabs.size ();
9249 cust->includes
9250 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9251 struct compunit_symtab *, len + 1);
9252 memcpy (cust->includes, result_symtabs.data (),
9253 len * sizeof (compunit_symtab *));
9254 cust->includes[len] = NULL;
9255 }
9256 }
9257
9258 /* Compute the 'includes' field for the symtabs of all the CUs we just
9259 read. */
9260
9261 static void
9262 process_cu_includes (dwarf2_per_objfile *per_objfile)
9263 {
9264 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9265 {
9266 if (! iter->is_debug_types)
9267 compute_compunit_symtab_includes (iter, per_objfile);
9268 }
9269
9270 per_objfile->per_bfd->just_read_cus.clear ();
9271 }
9272
9273 /* Generate full symbol information for CU, whose DIEs have
9274 already been loaded into memory. */
9275
9276 static void
9277 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9278 {
9279 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9280 struct objfile *objfile = per_objfile->objfile;
9281 struct gdbarch *gdbarch = objfile->arch ();
9282 CORE_ADDR lowpc, highpc;
9283 struct compunit_symtab *cust;
9284 CORE_ADDR baseaddr;
9285 struct block *static_block;
9286 CORE_ADDR addr;
9287
9288 baseaddr = objfile->text_section_offset ();
9289
9290 /* Clear the list here in case something was left over. */
9291 cu->method_list.clear ();
9292
9293 dwarf2_find_base_address (cu->dies, cu);
9294
9295 /* Before we start reading the top-level DIE, ensure it has a valid tag
9296 type. */
9297 switch (cu->dies->tag)
9298 {
9299 case DW_TAG_compile_unit:
9300 case DW_TAG_partial_unit:
9301 case DW_TAG_type_unit:
9302 break;
9303 default:
9304 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9305 dwarf_tag_name (cu->dies->tag),
9306 sect_offset_str (cu->per_cu->sect_off),
9307 objfile_name (per_objfile->objfile));
9308 }
9309
9310 /* Do line number decoding in read_file_scope () */
9311 process_die (cu->dies, cu);
9312
9313 /* For now fudge the Go package. */
9314 if (cu->per_cu->lang == language_go)
9315 fixup_go_packaging (cu);
9316
9317 /* Now that we have processed all the DIEs in the CU, all the types
9318 should be complete, and it should now be safe to compute all of the
9319 physnames. */
9320 compute_delayed_physnames (cu);
9321
9322 if (cu->per_cu->lang == language_rust)
9323 rust_union_quirks (cu);
9324
9325 /* Some compilers don't define a DW_AT_high_pc attribute for the
9326 compilation unit. If the DW_AT_high_pc is missing, synthesize
9327 it, by scanning the DIE's below the compilation unit. */
9328 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9329
9330 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9331 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9332
9333 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9334 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9335 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9336 addrmap to help ensure it has an accurate map of pc values belonging to
9337 this comp unit. */
9338 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9339
9340 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9341 SECT_OFF_TEXT (objfile),
9342 0);
9343
9344 if (cust != NULL)
9345 {
9346 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9347
9348 /* Set symtab language to language from DW_AT_language. If the
9349 compilation is from a C file generated by language preprocessors, do
9350 not set the language if it was already deduced by start_subfile. */
9351 if (!(cu->per_cu->lang == language_c
9352 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9353 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
9354
9355 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9356 produce DW_AT_location with location lists but it can be possibly
9357 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9358 there were bugs in prologue debug info, fixed later in GCC-4.5
9359 by "unwind info for epilogues" patch (which is not directly related).
9360
9361 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9362 needed, it would be wrong due to missing DW_AT_producer there.
9363
9364 Still one can confuse GDB by using non-standard GCC compilation
9365 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9366 */
9367 if (cu->has_loclist && gcc_4_minor >= 5)
9368 cust->locations_valid = 1;
9369
9370 if (gcc_4_minor >= 5)
9371 cust->epilogue_unwind_valid = 1;
9372
9373 cust->call_site_htab = cu->call_site_htab;
9374 }
9375
9376 per_objfile->set_symtab (cu->per_cu, cust);
9377
9378 /* Push it for inclusion processing later. */
9379 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9380
9381 /* Not needed any more. */
9382 cu->reset_builder ();
9383 }
9384
9385 /* Generate full symbol information for type unit CU, whose DIEs have
9386 already been loaded into memory. */
9387
9388 static void
9389 process_full_type_unit (dwarf2_cu *cu,
9390 enum language pretend_language)
9391 {
9392 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9393 struct objfile *objfile = per_objfile->objfile;
9394 struct compunit_symtab *cust;
9395 struct signatured_type *sig_type;
9396
9397 gdb_assert (cu->per_cu->is_debug_types);
9398 sig_type = (struct signatured_type *) cu->per_cu;
9399
9400 /* Clear the list here in case something was left over. */
9401 cu->method_list.clear ();
9402
9403 /* The symbol tables are set up in read_type_unit_scope. */
9404 process_die (cu->dies, cu);
9405
9406 /* For now fudge the Go package. */
9407 if (cu->per_cu->lang == language_go)
9408 fixup_go_packaging (cu);
9409
9410 /* Now that we have processed all the DIEs in the CU, all the types
9411 should be complete, and it should now be safe to compute all of the
9412 physnames. */
9413 compute_delayed_physnames (cu);
9414
9415 if (cu->per_cu->lang == language_rust)
9416 rust_union_quirks (cu);
9417
9418 /* TUs share symbol tables.
9419 If this is the first TU to use this symtab, complete the construction
9420 of it with end_expandable_symtab. Otherwise, complete the addition of
9421 this TU's symbols to the existing symtab. */
9422 type_unit_group_unshareable *tug_unshare =
9423 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9424 if (tug_unshare->compunit_symtab == NULL)
9425 {
9426 buildsym_compunit *builder = cu->get_builder ();
9427 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9428 tug_unshare->compunit_symtab = cust;
9429
9430 if (cust != NULL)
9431 {
9432 /* Set symtab language to language from DW_AT_language. If the
9433 compilation is from a C file generated by language preprocessors,
9434 do not set the language if it was already deduced by
9435 start_subfile. */
9436 if (!(cu->per_cu->lang == language_c
9437 && COMPUNIT_FILETABS (cust)->language != language_c))
9438 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
9439 }
9440 }
9441 else
9442 {
9443 cu->get_builder ()->augment_type_symtab ();
9444 cust = tug_unshare->compunit_symtab;
9445 }
9446
9447 per_objfile->set_symtab (cu->per_cu, cust);
9448
9449 /* Not needed any more. */
9450 cu->reset_builder ();
9451 }
9452
9453 /* Process an imported unit DIE. */
9454
9455 static void
9456 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9457 {
9458 struct attribute *attr;
9459
9460 /* For now we don't handle imported units in type units. */
9461 if (cu->per_cu->is_debug_types)
9462 {
9463 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9464 " supported in type units [in module %s]"),
9465 objfile_name (cu->per_objfile->objfile));
9466 }
9467
9468 attr = dwarf2_attr (die, DW_AT_import, cu);
9469 if (attr != NULL)
9470 {
9471 sect_offset sect_off = attr->get_ref_die_offset ();
9472 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9473 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9474 dwarf2_per_cu_data *per_cu
9475 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9476
9477 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9478 into another compilation unit, at root level. Regard this as a hint,
9479 and ignore it. */
9480 if (die->parent && die->parent->parent == NULL
9481 && per_cu->unit_type == DW_UT_compile
9482 && per_cu->lang == language_cplus)
9483 return;
9484
9485 /* If necessary, add it to the queue and load its DIEs. */
9486 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
9487 cu->per_cu->lang))
9488 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9489 false, cu->per_cu->lang);
9490
9491 cu->per_cu->imported_symtabs_push (per_cu);
9492 }
9493 }
9494
9495 /* RAII object that represents a process_die scope: i.e.,
9496 starts/finishes processing a DIE. */
9497 class process_die_scope
9498 {
9499 public:
9500 process_die_scope (die_info *die, dwarf2_cu *cu)
9501 : m_die (die), m_cu (cu)
9502 {
9503 /* We should only be processing DIEs not already in process. */
9504 gdb_assert (!m_die->in_process);
9505 m_die->in_process = true;
9506 }
9507
9508 ~process_die_scope ()
9509 {
9510 m_die->in_process = false;
9511
9512 /* If we're done processing the DIE for the CU that owns the line
9513 header, we don't need the line header anymore. */
9514 if (m_cu->line_header_die_owner == m_die)
9515 {
9516 delete m_cu->line_header;
9517 m_cu->line_header = NULL;
9518 m_cu->line_header_die_owner = NULL;
9519 }
9520 }
9521
9522 private:
9523 die_info *m_die;
9524 dwarf2_cu *m_cu;
9525 };
9526
9527 /* Process a die and its children. */
9528
9529 static void
9530 process_die (struct die_info *die, struct dwarf2_cu *cu)
9531 {
9532 process_die_scope scope (die, cu);
9533
9534 switch (die->tag)
9535 {
9536 case DW_TAG_padding:
9537 break;
9538 case DW_TAG_compile_unit:
9539 case DW_TAG_partial_unit:
9540 read_file_scope (die, cu);
9541 break;
9542 case DW_TAG_type_unit:
9543 read_type_unit_scope (die, cu);
9544 break;
9545 case DW_TAG_subprogram:
9546 /* Nested subprograms in Fortran get a prefix. */
9547 if (cu->per_cu->lang == language_fortran
9548 && die->parent != NULL
9549 && die->parent->tag == DW_TAG_subprogram)
9550 cu->processing_has_namespace_info = true;
9551 /* Fall through. */
9552 case DW_TAG_inlined_subroutine:
9553 read_func_scope (die, cu);
9554 break;
9555 case DW_TAG_lexical_block:
9556 case DW_TAG_try_block:
9557 case DW_TAG_catch_block:
9558 read_lexical_block_scope (die, cu);
9559 break;
9560 case DW_TAG_call_site:
9561 case DW_TAG_GNU_call_site:
9562 read_call_site_scope (die, cu);
9563 break;
9564 case DW_TAG_class_type:
9565 case DW_TAG_interface_type:
9566 case DW_TAG_structure_type:
9567 case DW_TAG_union_type:
9568 process_structure_scope (die, cu);
9569 break;
9570 case DW_TAG_enumeration_type:
9571 process_enumeration_scope (die, cu);
9572 break;
9573
9574 /* These dies have a type, but processing them does not create
9575 a symbol or recurse to process the children. Therefore we can
9576 read them on-demand through read_type_die. */
9577 case DW_TAG_subroutine_type:
9578 case DW_TAG_set_type:
9579 case DW_TAG_pointer_type:
9580 case DW_TAG_ptr_to_member_type:
9581 case DW_TAG_reference_type:
9582 case DW_TAG_rvalue_reference_type:
9583 case DW_TAG_string_type:
9584 break;
9585
9586 case DW_TAG_array_type:
9587 /* We only need to handle this case for Ada -- in other
9588 languages, it's normal for the compiler to emit a typedef
9589 instead. */
9590 if (cu->per_cu->lang != language_ada)
9591 break;
9592 /* FALLTHROUGH */
9593 case DW_TAG_base_type:
9594 case DW_TAG_subrange_type:
9595 case DW_TAG_typedef:
9596 /* Add a typedef symbol for the type definition, if it has a
9597 DW_AT_name. */
9598 new_symbol (die, read_type_die (die, cu), cu);
9599 break;
9600 case DW_TAG_common_block:
9601 read_common_block (die, cu);
9602 break;
9603 case DW_TAG_common_inclusion:
9604 break;
9605 case DW_TAG_namespace:
9606 cu->processing_has_namespace_info = true;
9607 read_namespace (die, cu);
9608 break;
9609 case DW_TAG_module:
9610 cu->processing_has_namespace_info = true;
9611 read_module (die, cu);
9612 break;
9613 case DW_TAG_imported_declaration:
9614 cu->processing_has_namespace_info = true;
9615 if (read_namespace_alias (die, cu))
9616 break;
9617 /* The declaration is not a global namespace alias. */
9618 /* Fall through. */
9619 case DW_TAG_imported_module:
9620 cu->processing_has_namespace_info = true;
9621 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9622 || cu->per_cu->lang != language_fortran))
9623 complaint (_("Tag '%s' has unexpected children"),
9624 dwarf_tag_name (die->tag));
9625 read_import_statement (die, cu);
9626 break;
9627
9628 case DW_TAG_imported_unit:
9629 process_imported_unit_die (die, cu);
9630 break;
9631
9632 case DW_TAG_variable:
9633 read_variable (die, cu);
9634 break;
9635
9636 default:
9637 new_symbol (die, NULL, cu);
9638 break;
9639 }
9640 }
9641 \f
9642 /* DWARF name computation. */
9643
9644 /* A helper function for dwarf2_compute_name which determines whether DIE
9645 needs to have the name of the scope prepended to the name listed in the
9646 die. */
9647
9648 static int
9649 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9650 {
9651 struct attribute *attr;
9652
9653 switch (die->tag)
9654 {
9655 case DW_TAG_namespace:
9656 case DW_TAG_typedef:
9657 case DW_TAG_class_type:
9658 case DW_TAG_interface_type:
9659 case DW_TAG_structure_type:
9660 case DW_TAG_union_type:
9661 case DW_TAG_enumeration_type:
9662 case DW_TAG_enumerator:
9663 case DW_TAG_subprogram:
9664 case DW_TAG_inlined_subroutine:
9665 case DW_TAG_member:
9666 case DW_TAG_imported_declaration:
9667 return 1;
9668
9669 case DW_TAG_variable:
9670 case DW_TAG_constant:
9671 /* We only need to prefix "globally" visible variables. These include
9672 any variable marked with DW_AT_external or any variable that
9673 lives in a namespace. [Variables in anonymous namespaces
9674 require prefixing, but they are not DW_AT_external.] */
9675
9676 if (dwarf2_attr (die, DW_AT_specification, cu))
9677 {
9678 struct dwarf2_cu *spec_cu = cu;
9679
9680 return die_needs_namespace (die_specification (die, &spec_cu),
9681 spec_cu);
9682 }
9683
9684 attr = dwarf2_attr (die, DW_AT_external, cu);
9685 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9686 && die->parent->tag != DW_TAG_module)
9687 return 0;
9688 /* A variable in a lexical block of some kind does not need a
9689 namespace, even though in C++ such variables may be external
9690 and have a mangled name. */
9691 if (die->parent->tag == DW_TAG_lexical_block
9692 || die->parent->tag == DW_TAG_try_block
9693 || die->parent->tag == DW_TAG_catch_block
9694 || die->parent->tag == DW_TAG_subprogram)
9695 return 0;
9696 return 1;
9697
9698 default:
9699 return 0;
9700 }
9701 }
9702
9703 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9704 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9705 defined for the given DIE. */
9706
9707 static struct attribute *
9708 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9709 {
9710 struct attribute *attr;
9711
9712 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9713 if (attr == NULL)
9714 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9715
9716 return attr;
9717 }
9718
9719 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9720 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9721 defined for the given DIE. */
9722
9723 static const char *
9724 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9725 {
9726 const char *linkage_name;
9727
9728 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9729 if (linkage_name == NULL)
9730 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9731
9732 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9733 See https://github.com/rust-lang/rust/issues/32925. */
9734 if (cu->per_cu->lang == language_rust && linkage_name != NULL
9735 && strchr (linkage_name, '{') != NULL)
9736 linkage_name = NULL;
9737
9738 return linkage_name;
9739 }
9740
9741 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9742 compute the physname for the object, which include a method's:
9743 - formal parameters (C++),
9744 - receiver type (Go),
9745
9746 The term "physname" is a bit confusing.
9747 For C++, for example, it is the demangled name.
9748 For Go, for example, it's the mangled name.
9749
9750 For Ada, return the DIE's linkage name rather than the fully qualified
9751 name. PHYSNAME is ignored..
9752
9753 The result is allocated on the objfile->per_bfd's obstack and
9754 canonicalized. */
9755
9756 static const char *
9757 dwarf2_compute_name (const char *name,
9758 struct die_info *die, struct dwarf2_cu *cu,
9759 int physname)
9760 {
9761 struct objfile *objfile = cu->per_objfile->objfile;
9762
9763 if (name == NULL)
9764 name = dwarf2_name (die, cu);
9765
9766 enum language lang = cu->per_cu->lang;
9767
9768 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9769 but otherwise compute it by typename_concat inside GDB.
9770 FIXME: Actually this is not really true, or at least not always true.
9771 It's all very confusing. compute_and_set_names doesn't try to demangle
9772 Fortran names because there is no mangling standard. So new_symbol
9773 will set the demangled name to the result of dwarf2_full_name, and it is
9774 the demangled name that GDB uses if it exists. */
9775 if (lang == language_ada
9776 || (lang == language_fortran && physname))
9777 {
9778 /* For Ada unit, we prefer the linkage name over the name, as
9779 the former contains the exported name, which the user expects
9780 to be able to reference. Ideally, we want the user to be able
9781 to reference this entity using either natural or linkage name,
9782 but we haven't started looking at this enhancement yet. */
9783 const char *linkage_name = dw2_linkage_name (die, cu);
9784
9785 if (linkage_name != NULL)
9786 return linkage_name;
9787 }
9788
9789 /* These are the only languages we know how to qualify names in. */
9790 if (name != NULL
9791 && (lang == language_cplus
9792 || lang == language_fortran || lang == language_d
9793 || lang == language_rust))
9794 {
9795 if (die_needs_namespace (die, cu))
9796 {
9797 const char *prefix;
9798 const char *canonical_name = NULL;
9799
9800 string_file buf;
9801
9802 prefix = determine_prefix (die, cu);
9803 if (*prefix != '\0')
9804 {
9805 gdb::unique_xmalloc_ptr<char> prefixed_name
9806 (typename_concat (NULL, prefix, name, physname, cu));
9807
9808 buf.puts (prefixed_name.get ());
9809 }
9810 else
9811 buf.puts (name);
9812
9813 /* Template parameters may be specified in the DIE's DW_AT_name, or
9814 as children with DW_TAG_template_type_param or
9815 DW_TAG_value_type_param. If the latter, add them to the name
9816 here. If the name already has template parameters, then
9817 skip this step; some versions of GCC emit both, and
9818 it is more efficient to use the pre-computed name.
9819
9820 Something to keep in mind about this process: it is very
9821 unlikely, or in some cases downright impossible, to produce
9822 something that will match the mangled name of a function.
9823 If the definition of the function has the same debug info,
9824 we should be able to match up with it anyway. But fallbacks
9825 using the minimal symbol, for instance to find a method
9826 implemented in a stripped copy of libstdc++, will not work.
9827 If we do not have debug info for the definition, we will have to
9828 match them up some other way.
9829
9830 When we do name matching there is a related problem with function
9831 templates; two instantiated function templates are allowed to
9832 differ only by their return types, which we do not add here. */
9833
9834 if (lang == language_cplus && strchr (name, '<') == NULL)
9835 {
9836 struct attribute *attr;
9837 struct die_info *child;
9838 int first = 1;
9839
9840 die->building_fullname = 1;
9841
9842 for (child = die->child; child != NULL; child = child->sibling)
9843 {
9844 struct type *type;
9845 LONGEST value;
9846 const gdb_byte *bytes;
9847 struct dwarf2_locexpr_baton *baton;
9848 struct value *v;
9849
9850 if (child->tag != DW_TAG_template_type_param
9851 && child->tag != DW_TAG_template_value_param)
9852 continue;
9853
9854 if (first)
9855 {
9856 buf.puts ("<");
9857 first = 0;
9858 }
9859 else
9860 buf.puts (", ");
9861
9862 attr = dwarf2_attr (child, DW_AT_type, cu);
9863 if (attr == NULL)
9864 {
9865 complaint (_("template parameter missing DW_AT_type"));
9866 buf.puts ("UNKNOWN_TYPE");
9867 continue;
9868 }
9869 type = die_type (child, cu);
9870
9871 if (child->tag == DW_TAG_template_type_param)
9872 {
9873 cu->language_defn->print_type (type, "", &buf, -1, 0,
9874 &type_print_raw_options);
9875 continue;
9876 }
9877
9878 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9879 if (attr == NULL)
9880 {
9881 complaint (_("template parameter missing "
9882 "DW_AT_const_value"));
9883 buf.puts ("UNKNOWN_VALUE");
9884 continue;
9885 }
9886
9887 dwarf2_const_value_attr (attr, type, name,
9888 &cu->comp_unit_obstack, cu,
9889 &value, &bytes, &baton);
9890
9891 if (type->has_no_signedness ())
9892 /* GDB prints characters as NUMBER 'CHAR'. If that's
9893 changed, this can use value_print instead. */
9894 cu->language_defn->printchar (value, type, &buf);
9895 else
9896 {
9897 struct value_print_options opts;
9898
9899 if (baton != NULL)
9900 v = dwarf2_evaluate_loc_desc (type, NULL,
9901 baton->data,
9902 baton->size,
9903 baton->per_cu,
9904 baton->per_objfile);
9905 else if (bytes != NULL)
9906 {
9907 v = allocate_value (type);
9908 memcpy (value_contents_writeable (v), bytes,
9909 TYPE_LENGTH (type));
9910 }
9911 else
9912 v = value_from_longest (type, value);
9913
9914 /* Specify decimal so that we do not depend on
9915 the radix. */
9916 get_formatted_print_options (&opts, 'd');
9917 opts.raw = 1;
9918 value_print (v, &buf, &opts);
9919 release_value (v);
9920 }
9921 }
9922
9923 die->building_fullname = 0;
9924
9925 if (!first)
9926 {
9927 /* Close the argument list, with a space if necessary
9928 (nested templates). */
9929 if (!buf.empty () && buf.string ().back () == '>')
9930 buf.puts (" >");
9931 else
9932 buf.puts (">");
9933 }
9934 }
9935
9936 /* For C++ methods, append formal parameter type
9937 information, if PHYSNAME. */
9938
9939 if (physname && die->tag == DW_TAG_subprogram
9940 && lang == language_cplus)
9941 {
9942 struct type *type = read_type_die (die, cu);
9943
9944 c_type_print_args (type, &buf, 1, lang,
9945 &type_print_raw_options);
9946
9947 if (lang == language_cplus)
9948 {
9949 /* Assume that an artificial first parameter is
9950 "this", but do not crash if it is not. RealView
9951 marks unnamed (and thus unused) parameters as
9952 artificial; there is no way to differentiate
9953 the two cases. */
9954 if (type->num_fields () > 0
9955 && TYPE_FIELD_ARTIFICIAL (type, 0)
9956 && type->field (0).type ()->code () == TYPE_CODE_PTR
9957 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
9958 buf.puts (" const");
9959 }
9960 }
9961
9962 const std::string &intermediate_name = buf.string ();
9963
9964 if (lang == language_cplus)
9965 canonical_name
9966 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9967 objfile);
9968
9969 /* If we only computed INTERMEDIATE_NAME, or if
9970 INTERMEDIATE_NAME is already canonical, then we need to
9971 intern it. */
9972 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9973 name = objfile->intern (intermediate_name);
9974 else
9975 name = canonical_name;
9976 }
9977 }
9978
9979 return name;
9980 }
9981
9982 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9983 If scope qualifiers are appropriate they will be added. The result
9984 will be allocated on the storage_obstack, or NULL if the DIE does
9985 not have a name. NAME may either be from a previous call to
9986 dwarf2_name or NULL.
9987
9988 The output string will be canonicalized (if C++). */
9989
9990 static const char *
9991 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9992 {
9993 return dwarf2_compute_name (name, die, cu, 0);
9994 }
9995
9996 /* Construct a physname for the given DIE in CU. NAME may either be
9997 from a previous call to dwarf2_name or NULL. The result will be
9998 allocated on the objfile_objstack or NULL if the DIE does not have a
9999 name.
10000
10001 The output string will be canonicalized (if C++). */
10002
10003 static const char *
10004 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10005 {
10006 struct objfile *objfile = cu->per_objfile->objfile;
10007 const char *retval, *mangled = NULL, *canon = NULL;
10008 int need_copy = 1;
10009
10010 /* In this case dwarf2_compute_name is just a shortcut not building anything
10011 on its own. */
10012 if (!die_needs_namespace (die, cu))
10013 return dwarf2_compute_name (name, die, cu, 1);
10014
10015 if (cu->per_cu->lang != language_rust)
10016 mangled = dw2_linkage_name (die, cu);
10017
10018 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10019 has computed. */
10020 gdb::unique_xmalloc_ptr<char> demangled;
10021 if (mangled != NULL)
10022 {
10023 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
10024 {
10025 /* Do nothing (do not demangle the symbol name). */
10026 }
10027 else
10028 {
10029 /* Use DMGL_RET_DROP for C++ template functions to suppress
10030 their return type. It is easier for GDB users to search
10031 for such functions as `name(params)' than `long name(params)'.
10032 In such case the minimal symbol names do not match the full
10033 symbol names but for template functions there is never a need
10034 to look up their definition from their declaration so
10035 the only disadvantage remains the minimal symbol variant
10036 `long name(params)' does not have the proper inferior type. */
10037 demangled.reset (gdb_demangle (mangled,
10038 (DMGL_PARAMS | DMGL_ANSI
10039 | DMGL_RET_DROP)));
10040 }
10041 if (demangled)
10042 canon = demangled.get ();
10043 else
10044 {
10045 canon = mangled;
10046 need_copy = 0;
10047 }
10048 }
10049
10050 if (canon == NULL || check_physname)
10051 {
10052 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10053
10054 if (canon != NULL && strcmp (physname, canon) != 0)
10055 {
10056 /* It may not mean a bug in GDB. The compiler could also
10057 compute DW_AT_linkage_name incorrectly. But in such case
10058 GDB would need to be bug-to-bug compatible. */
10059
10060 complaint (_("Computed physname <%s> does not match demangled <%s> "
10061 "(from linkage <%s>) - DIE at %s [in module %s]"),
10062 physname, canon, mangled, sect_offset_str (die->sect_off),
10063 objfile_name (objfile));
10064
10065 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10066 is available here - over computed PHYSNAME. It is safer
10067 against both buggy GDB and buggy compilers. */
10068
10069 retval = canon;
10070 }
10071 else
10072 {
10073 retval = physname;
10074 need_copy = 0;
10075 }
10076 }
10077 else
10078 retval = canon;
10079
10080 if (need_copy)
10081 retval = objfile->intern (retval);
10082
10083 return retval;
10084 }
10085
10086 /* Inspect DIE in CU for a namespace alias. If one exists, record
10087 a new symbol for it.
10088
10089 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10090
10091 static int
10092 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10093 {
10094 struct attribute *attr;
10095
10096 /* If the die does not have a name, this is not a namespace
10097 alias. */
10098 attr = dwarf2_attr (die, DW_AT_name, cu);
10099 if (attr != NULL)
10100 {
10101 int num;
10102 struct die_info *d = die;
10103 struct dwarf2_cu *imported_cu = cu;
10104
10105 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10106 keep inspecting DIEs until we hit the underlying import. */
10107 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10108 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10109 {
10110 attr = dwarf2_attr (d, DW_AT_import, cu);
10111 if (attr == NULL)
10112 break;
10113
10114 d = follow_die_ref (d, attr, &imported_cu);
10115 if (d->tag != DW_TAG_imported_declaration)
10116 break;
10117 }
10118
10119 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10120 {
10121 complaint (_("DIE at %s has too many recursively imported "
10122 "declarations"), sect_offset_str (d->sect_off));
10123 return 0;
10124 }
10125
10126 if (attr != NULL)
10127 {
10128 struct type *type;
10129 sect_offset sect_off = attr->get_ref_die_offset ();
10130
10131 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10132 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10133 {
10134 /* This declaration is a global namespace alias. Add
10135 a symbol for it whose type is the aliased namespace. */
10136 new_symbol (die, type, cu);
10137 return 1;
10138 }
10139 }
10140 }
10141
10142 return 0;
10143 }
10144
10145 /* Return the using directives repository (global or local?) to use in the
10146 current context for CU.
10147
10148 For Ada, imported declarations can materialize renamings, which *may* be
10149 global. However it is impossible (for now?) in DWARF to distinguish
10150 "external" imported declarations and "static" ones. As all imported
10151 declarations seem to be static in all other languages, make them all CU-wide
10152 global only in Ada. */
10153
10154 static struct using_direct **
10155 using_directives (struct dwarf2_cu *cu)
10156 {
10157 if (cu->per_cu->lang == language_ada
10158 && cu->get_builder ()->outermost_context_p ())
10159 return cu->get_builder ()->get_global_using_directives ();
10160 else
10161 return cu->get_builder ()->get_local_using_directives ();
10162 }
10163
10164 /* Read the import statement specified by the given die and record it. */
10165
10166 static void
10167 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10168 {
10169 struct objfile *objfile = cu->per_objfile->objfile;
10170 struct attribute *import_attr;
10171 struct die_info *imported_die, *child_die;
10172 struct dwarf2_cu *imported_cu;
10173 const char *imported_name;
10174 const char *imported_name_prefix;
10175 const char *canonical_name;
10176 const char *import_alias;
10177 const char *imported_declaration = NULL;
10178 const char *import_prefix;
10179 std::vector<const char *> excludes;
10180
10181 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10182 if (import_attr == NULL)
10183 {
10184 complaint (_("Tag '%s' has no DW_AT_import"),
10185 dwarf_tag_name (die->tag));
10186 return;
10187 }
10188
10189 imported_cu = cu;
10190 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10191 imported_name = dwarf2_name (imported_die, imported_cu);
10192 if (imported_name == NULL)
10193 {
10194 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10195
10196 The import in the following code:
10197 namespace A
10198 {
10199 typedef int B;
10200 }
10201
10202 int main ()
10203 {
10204 using A::B;
10205 B b;
10206 return b;
10207 }
10208
10209 ...
10210 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10211 <52> DW_AT_decl_file : 1
10212 <53> DW_AT_decl_line : 6
10213 <54> DW_AT_import : <0x75>
10214 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10215 <59> DW_AT_name : B
10216 <5b> DW_AT_decl_file : 1
10217 <5c> DW_AT_decl_line : 2
10218 <5d> DW_AT_type : <0x6e>
10219 ...
10220 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10221 <76> DW_AT_byte_size : 4
10222 <77> DW_AT_encoding : 5 (signed)
10223
10224 imports the wrong die ( 0x75 instead of 0x58 ).
10225 This case will be ignored until the gcc bug is fixed. */
10226 return;
10227 }
10228
10229 /* Figure out the local name after import. */
10230 import_alias = dwarf2_name (die, cu);
10231
10232 /* Figure out where the statement is being imported to. */
10233 import_prefix = determine_prefix (die, cu);
10234
10235 /* Figure out what the scope of the imported die is and prepend it
10236 to the name of the imported die. */
10237 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10238
10239 if (imported_die->tag != DW_TAG_namespace
10240 && imported_die->tag != DW_TAG_module)
10241 {
10242 imported_declaration = imported_name;
10243 canonical_name = imported_name_prefix;
10244 }
10245 else if (strlen (imported_name_prefix) > 0)
10246 canonical_name = obconcat (&objfile->objfile_obstack,
10247 imported_name_prefix,
10248 (cu->per_cu->lang == language_d
10249 ? "."
10250 : "::"),
10251 imported_name, (char *) NULL);
10252 else
10253 canonical_name = imported_name;
10254
10255 if (die->tag == DW_TAG_imported_module
10256 && cu->per_cu->lang == language_fortran)
10257 for (child_die = die->child; child_die && child_die->tag;
10258 child_die = child_die->sibling)
10259 {
10260 /* DWARF-4: A Fortran use statement with a “rename list” may be
10261 represented by an imported module entry with an import attribute
10262 referring to the module and owned entries corresponding to those
10263 entities that are renamed as part of being imported. */
10264
10265 if (child_die->tag != DW_TAG_imported_declaration)
10266 {
10267 complaint (_("child DW_TAG_imported_declaration expected "
10268 "- DIE at %s [in module %s]"),
10269 sect_offset_str (child_die->sect_off),
10270 objfile_name (objfile));
10271 continue;
10272 }
10273
10274 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10275 if (import_attr == NULL)
10276 {
10277 complaint (_("Tag '%s' has no DW_AT_import"),
10278 dwarf_tag_name (child_die->tag));
10279 continue;
10280 }
10281
10282 imported_cu = cu;
10283 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10284 &imported_cu);
10285 imported_name = dwarf2_name (imported_die, imported_cu);
10286 if (imported_name == NULL)
10287 {
10288 complaint (_("child DW_TAG_imported_declaration has unknown "
10289 "imported name - DIE at %s [in module %s]"),
10290 sect_offset_str (child_die->sect_off),
10291 objfile_name (objfile));
10292 continue;
10293 }
10294
10295 excludes.push_back (imported_name);
10296
10297 process_die (child_die, cu);
10298 }
10299
10300 add_using_directive (using_directives (cu),
10301 import_prefix,
10302 canonical_name,
10303 import_alias,
10304 imported_declaration,
10305 excludes,
10306 0,
10307 &objfile->objfile_obstack);
10308 }
10309
10310 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10311 types, but gives them a size of zero. Starting with version 14,
10312 ICC is compatible with GCC. */
10313
10314 static bool
10315 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10316 {
10317 if (!cu->checked_producer)
10318 check_producer (cu);
10319
10320 return cu->producer_is_icc_lt_14;
10321 }
10322
10323 /* ICC generates a DW_AT_type for C void functions. This was observed on
10324 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10325 which says that void functions should not have a DW_AT_type. */
10326
10327 static bool
10328 producer_is_icc (struct dwarf2_cu *cu)
10329 {
10330 if (!cu->checked_producer)
10331 check_producer (cu);
10332
10333 return cu->producer_is_icc;
10334 }
10335
10336 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10337 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10338 this, it was first present in GCC release 4.3.0. */
10339
10340 static bool
10341 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10342 {
10343 if (!cu->checked_producer)
10344 check_producer (cu);
10345
10346 return cu->producer_is_gcc_lt_4_3;
10347 }
10348
10349 static file_and_directory
10350 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10351 {
10352 file_and_directory res;
10353
10354 /* Find the filename. Do not use dwarf2_name here, since the filename
10355 is not a source language identifier. */
10356 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10357 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10358
10359 if (res.comp_dir == NULL
10360 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10361 && IS_ABSOLUTE_PATH (res.name))
10362 {
10363 res.comp_dir_storage = ldirname (res.name);
10364 if (!res.comp_dir_storage.empty ())
10365 res.comp_dir = res.comp_dir_storage.c_str ();
10366 }
10367 if (res.comp_dir != NULL)
10368 {
10369 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10370 directory, get rid of it. */
10371 const char *cp = strchr (res.comp_dir, ':');
10372
10373 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10374 res.comp_dir = cp + 1;
10375 }
10376
10377 if (res.name == NULL)
10378 res.name = "<unknown>";
10379
10380 return res;
10381 }
10382
10383 /* Handle DW_AT_stmt_list for a compilation unit.
10384 DIE is the DW_TAG_compile_unit die for CU.
10385 COMP_DIR is the compilation directory. LOWPC is passed to
10386 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10387
10388 static void
10389 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10390 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10391 {
10392 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10393 struct attribute *attr;
10394 struct line_header line_header_local;
10395 hashval_t line_header_local_hash;
10396 void **slot;
10397 int decode_mapping;
10398
10399 gdb_assert (! cu->per_cu->is_debug_types);
10400
10401 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10402 if (attr == NULL || !attr->form_is_unsigned ())
10403 return;
10404
10405 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10406
10407 /* The line header hash table is only created if needed (it exists to
10408 prevent redundant reading of the line table for partial_units).
10409 If we're given a partial_unit, we'll need it. If we're given a
10410 compile_unit, then use the line header hash table if it's already
10411 created, but don't create one just yet. */
10412
10413 if (per_objfile->line_header_hash == NULL
10414 && die->tag == DW_TAG_partial_unit)
10415 {
10416 per_objfile->line_header_hash
10417 .reset (htab_create_alloc (127, line_header_hash_voidp,
10418 line_header_eq_voidp,
10419 htab_delete_entry<line_header>,
10420 xcalloc, xfree));
10421 }
10422
10423 line_header_local.sect_off = line_offset;
10424 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10425 line_header_local_hash = line_header_hash (&line_header_local);
10426 if (per_objfile->line_header_hash != NULL)
10427 {
10428 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10429 &line_header_local,
10430 line_header_local_hash, NO_INSERT);
10431
10432 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10433 is not present in *SLOT (since if there is something in *SLOT then
10434 it will be for a partial_unit). */
10435 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10436 {
10437 gdb_assert (*slot != NULL);
10438 cu->line_header = (struct line_header *) *slot;
10439 return;
10440 }
10441 }
10442
10443 /* dwarf_decode_line_header does not yet provide sufficient information.
10444 We always have to call also dwarf_decode_lines for it. */
10445 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10446 if (lh == NULL)
10447 return;
10448
10449 cu->line_header = lh.release ();
10450 cu->line_header_die_owner = die;
10451
10452 if (per_objfile->line_header_hash == NULL)
10453 slot = NULL;
10454 else
10455 {
10456 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10457 &line_header_local,
10458 line_header_local_hash, INSERT);
10459 gdb_assert (slot != NULL);
10460 }
10461 if (slot != NULL && *slot == NULL)
10462 {
10463 /* This newly decoded line number information unit will be owned
10464 by line_header_hash hash table. */
10465 *slot = cu->line_header;
10466 cu->line_header_die_owner = NULL;
10467 }
10468 else
10469 {
10470 /* We cannot free any current entry in (*slot) as that struct line_header
10471 may be already used by multiple CUs. Create only temporary decoded
10472 line_header for this CU - it may happen at most once for each line
10473 number information unit. And if we're not using line_header_hash
10474 then this is what we want as well. */
10475 gdb_assert (die->tag != DW_TAG_partial_unit);
10476 }
10477 decode_mapping = (die->tag != DW_TAG_partial_unit);
10478 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10479 decode_mapping);
10480
10481 }
10482
10483 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10484
10485 static void
10486 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10487 {
10488 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10489 struct objfile *objfile = per_objfile->objfile;
10490 struct gdbarch *gdbarch = objfile->arch ();
10491 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10492 CORE_ADDR highpc = ((CORE_ADDR) 0);
10493 struct attribute *attr;
10494 struct die_info *child_die;
10495 CORE_ADDR baseaddr;
10496
10497 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
10498 baseaddr = objfile->text_section_offset ();
10499
10500 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10501
10502 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10503 from finish_block. */
10504 if (lowpc == ((CORE_ADDR) -1))
10505 lowpc = highpc;
10506 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10507
10508 file_and_directory fnd = find_file_and_directory (die, cu);
10509
10510 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10511
10512 gdb_assert (per_objfile->sym_cu == nullptr);
10513 scoped_restore restore_sym_cu
10514 = make_scoped_restore (&per_objfile->sym_cu, cu);
10515
10516 /* Decode line number information if present. We do this before
10517 processing child DIEs, so that the line header table is available
10518 for DW_AT_decl_file. */
10519 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10520
10521 /* Process all dies in compilation unit. */
10522 if (die->child != NULL)
10523 {
10524 child_die = die->child;
10525 while (child_die && child_die->tag)
10526 {
10527 process_die (child_die, cu);
10528 child_die = child_die->sibling;
10529 }
10530 }
10531 per_objfile->sym_cu = nullptr;
10532
10533 /* Decode macro information, if present. Dwarf 2 macro information
10534 refers to information in the line number info statement program
10535 header, so we can only read it if we've read the header
10536 successfully. */
10537 attr = dwarf2_attr (die, DW_AT_macros, cu);
10538 if (attr == NULL)
10539 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10540 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10541 {
10542 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10543 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10544
10545 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10546 }
10547 else
10548 {
10549 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10550 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10551 {
10552 unsigned int macro_offset = attr->as_unsigned ();
10553
10554 dwarf_decode_macros (cu, macro_offset, 0);
10555 }
10556 }
10557 }
10558
10559 void
10560 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10561 {
10562 struct type_unit_group *tu_group;
10563 int first_time;
10564 struct attribute *attr;
10565 unsigned int i;
10566 struct signatured_type *sig_type;
10567
10568 gdb_assert (per_cu->is_debug_types);
10569 sig_type = (struct signatured_type *) per_cu;
10570
10571 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10572
10573 /* If we're using .gdb_index (includes -readnow) then
10574 per_cu->type_unit_group may not have been set up yet. */
10575 if (sig_type->type_unit_group == NULL)
10576 sig_type->type_unit_group = get_type_unit_group (this, attr);
10577 tu_group = sig_type->type_unit_group;
10578
10579 /* If we've already processed this stmt_list there's no real need to
10580 do it again, we could fake it and just recreate the part we need
10581 (file name,index -> symtab mapping). If data shows this optimization
10582 is useful we can do it then. */
10583 type_unit_group_unshareable *tug_unshare
10584 = per_objfile->get_type_unit_group_unshareable (tu_group);
10585 first_time = tug_unshare->compunit_symtab == NULL;
10586
10587 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10588 debug info. */
10589 line_header_up lh;
10590 if (attr != NULL && attr->form_is_unsigned ())
10591 {
10592 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10593 lh = dwarf_decode_line_header (line_offset, this);
10594 }
10595 if (lh == NULL)
10596 {
10597 if (first_time)
10598 start_symtab ("", NULL, 0);
10599 else
10600 {
10601 gdb_assert (tug_unshare->symtabs == NULL);
10602 gdb_assert (m_builder == nullptr);
10603 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10604 m_builder.reset (new struct buildsym_compunit
10605 (COMPUNIT_OBJFILE (cust), "",
10606 COMPUNIT_DIRNAME (cust),
10607 compunit_language (cust),
10608 0, cust));
10609 list_in_scope = get_builder ()->get_file_symbols ();
10610 }
10611 return;
10612 }
10613
10614 line_header = lh.release ();
10615 line_header_die_owner = die;
10616
10617 if (first_time)
10618 {
10619 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10620
10621 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10622 still initializing it, and our caller (a few levels up)
10623 process_full_type_unit still needs to know if this is the first
10624 time. */
10625
10626 tug_unshare->symtabs
10627 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10628 struct symtab *, line_header->file_names_size ());
10629
10630 auto &file_names = line_header->file_names ();
10631 for (i = 0; i < file_names.size (); ++i)
10632 {
10633 file_entry &fe = file_names[i];
10634 dwarf2_start_subfile (this, fe.name,
10635 fe.include_dir (line_header));
10636 buildsym_compunit *b = get_builder ();
10637 if (b->get_current_subfile ()->symtab == NULL)
10638 {
10639 /* NOTE: start_subfile will recognize when it's been
10640 passed a file it has already seen. So we can't
10641 assume there's a simple mapping from
10642 cu->line_header->file_names to subfiles, plus
10643 cu->line_header->file_names may contain dups. */
10644 b->get_current_subfile ()->symtab
10645 = allocate_symtab (cust, b->get_current_subfile ()->name);
10646 }
10647
10648 fe.symtab = b->get_current_subfile ()->symtab;
10649 tug_unshare->symtabs[i] = fe.symtab;
10650 }
10651 }
10652 else
10653 {
10654 gdb_assert (m_builder == nullptr);
10655 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10656 m_builder.reset (new struct buildsym_compunit
10657 (COMPUNIT_OBJFILE (cust), "",
10658 COMPUNIT_DIRNAME (cust),
10659 compunit_language (cust),
10660 0, cust));
10661 list_in_scope = get_builder ()->get_file_symbols ();
10662
10663 auto &file_names = line_header->file_names ();
10664 for (i = 0; i < file_names.size (); ++i)
10665 {
10666 file_entry &fe = file_names[i];
10667 fe.symtab = tug_unshare->symtabs[i];
10668 }
10669 }
10670
10671 /* The main symtab is allocated last. Type units don't have DW_AT_name
10672 so they don't have a "real" (so to speak) symtab anyway.
10673 There is later code that will assign the main symtab to all symbols
10674 that don't have one. We need to handle the case of a symbol with a
10675 missing symtab (DW_AT_decl_file) anyway. */
10676 }
10677
10678 /* Process DW_TAG_type_unit.
10679 For TUs we want to skip the first top level sibling if it's not the
10680 actual type being defined by this TU. In this case the first top
10681 level sibling is there to provide context only. */
10682
10683 static void
10684 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10685 {
10686 struct die_info *child_die;
10687
10688 prepare_one_comp_unit (cu, die, language_minimal);
10689
10690 /* Initialize (or reinitialize) the machinery for building symtabs.
10691 We do this before processing child DIEs, so that the line header table
10692 is available for DW_AT_decl_file. */
10693 cu->setup_type_unit_groups (die);
10694
10695 if (die->child != NULL)
10696 {
10697 child_die = die->child;
10698 while (child_die && child_die->tag)
10699 {
10700 process_die (child_die, cu);
10701 child_die = child_die->sibling;
10702 }
10703 }
10704 }
10705 \f
10706 /* DWO/DWP files.
10707
10708 http://gcc.gnu.org/wiki/DebugFission
10709 http://gcc.gnu.org/wiki/DebugFissionDWP
10710
10711 To simplify handling of both DWO files ("object" files with the DWARF info)
10712 and DWP files (a file with the DWOs packaged up into one file), we treat
10713 DWP files as having a collection of virtual DWO files. */
10714
10715 static hashval_t
10716 hash_dwo_file (const void *item)
10717 {
10718 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10719 hashval_t hash;
10720
10721 hash = htab_hash_string (dwo_file->dwo_name);
10722 if (dwo_file->comp_dir != NULL)
10723 hash += htab_hash_string (dwo_file->comp_dir);
10724 return hash;
10725 }
10726
10727 static int
10728 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10729 {
10730 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10731 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10732
10733 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10734 return 0;
10735 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10736 return lhs->comp_dir == rhs->comp_dir;
10737 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10738 }
10739
10740 /* Allocate a hash table for DWO files. */
10741
10742 static htab_up
10743 allocate_dwo_file_hash_table ()
10744 {
10745 return htab_up (htab_create_alloc (41,
10746 hash_dwo_file,
10747 eq_dwo_file,
10748 htab_delete_entry<dwo_file>,
10749 xcalloc, xfree));
10750 }
10751
10752 /* Lookup DWO file DWO_NAME. */
10753
10754 static void **
10755 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
10756 const char *dwo_name,
10757 const char *comp_dir)
10758 {
10759 struct dwo_file find_entry;
10760 void **slot;
10761
10762 if (per_objfile->per_bfd->dwo_files == NULL)
10763 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
10764
10765 find_entry.dwo_name = dwo_name;
10766 find_entry.comp_dir = comp_dir;
10767 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
10768 INSERT);
10769
10770 return slot;
10771 }
10772
10773 static hashval_t
10774 hash_dwo_unit (const void *item)
10775 {
10776 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10777
10778 /* This drops the top 32 bits of the id, but is ok for a hash. */
10779 return dwo_unit->signature;
10780 }
10781
10782 static int
10783 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10784 {
10785 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10786 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10787
10788 /* The signature is assumed to be unique within the DWO file.
10789 So while object file CU dwo_id's always have the value zero,
10790 that's OK, assuming each object file DWO file has only one CU,
10791 and that's the rule for now. */
10792 return lhs->signature == rhs->signature;
10793 }
10794
10795 /* Allocate a hash table for DWO CUs,TUs.
10796 There is one of these tables for each of CUs,TUs for each DWO file. */
10797
10798 static htab_up
10799 allocate_dwo_unit_table ()
10800 {
10801 /* Start out with a pretty small number.
10802 Generally DWO files contain only one CU and maybe some TUs. */
10803 return htab_up (htab_create_alloc (3,
10804 hash_dwo_unit,
10805 eq_dwo_unit,
10806 NULL, xcalloc, xfree));
10807 }
10808
10809 /* die_reader_func for create_dwo_cu. */
10810
10811 static void
10812 create_dwo_cu_reader (const struct die_reader_specs *reader,
10813 const gdb_byte *info_ptr,
10814 struct die_info *comp_unit_die,
10815 struct dwo_file *dwo_file,
10816 struct dwo_unit *dwo_unit)
10817 {
10818 struct dwarf2_cu *cu = reader->cu;
10819 sect_offset sect_off = cu->per_cu->sect_off;
10820 struct dwarf2_section_info *section = cu->per_cu->section;
10821
10822 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10823 if (!signature.has_value ())
10824 {
10825 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10826 " its dwo_id [in module %s]"),
10827 sect_offset_str (sect_off), dwo_file->dwo_name);
10828 return;
10829 }
10830
10831 dwo_unit->dwo_file = dwo_file;
10832 dwo_unit->signature = *signature;
10833 dwo_unit->section = section;
10834 dwo_unit->sect_off = sect_off;
10835 dwo_unit->length = cu->per_cu->length;
10836
10837 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10838 sect_offset_str (sect_off),
10839 hex_string (dwo_unit->signature));
10840 }
10841
10842 /* Create the dwo_units for the CUs in a DWO_FILE.
10843 Note: This function processes DWO files only, not DWP files. */
10844
10845 static void
10846 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10847 dwarf2_cu *cu, struct dwo_file &dwo_file,
10848 dwarf2_section_info &section, htab_up &cus_htab)
10849 {
10850 struct objfile *objfile = per_objfile->objfile;
10851 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10852 const gdb_byte *info_ptr, *end_ptr;
10853
10854 section.read (objfile);
10855 info_ptr = section.buffer;
10856
10857 if (info_ptr == NULL)
10858 return;
10859
10860 dwarf_read_debug_printf ("Reading %s for %s:",
10861 section.get_name (),
10862 section.get_file_name ());
10863
10864 end_ptr = info_ptr + section.size;
10865 while (info_ptr < end_ptr)
10866 {
10867 struct dwarf2_per_cu_data per_cu;
10868 struct dwo_unit read_unit {};
10869 struct dwo_unit *dwo_unit;
10870 void **slot;
10871 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10872
10873 per_cu.per_bfd = per_bfd;
10874 per_cu.is_debug_types = 0;
10875 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10876 per_cu.section = &section;
10877
10878 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
10879 if (!reader.dummy_p)
10880 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
10881 &dwo_file, &read_unit);
10882 info_ptr += per_cu.length;
10883
10884 // If the unit could not be parsed, skip it.
10885 if (read_unit.dwo_file == NULL)
10886 continue;
10887
10888 if (cus_htab == NULL)
10889 cus_htab = allocate_dwo_unit_table ();
10890
10891 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
10892 struct dwo_unit);
10893 *dwo_unit = read_unit;
10894 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
10895 gdb_assert (slot != NULL);
10896 if (*slot != NULL)
10897 {
10898 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10899 sect_offset dup_sect_off = dup_cu->sect_off;
10900
10901 complaint (_("debug cu entry at offset %s is duplicate to"
10902 " the entry at offset %s, signature %s"),
10903 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
10904 hex_string (dwo_unit->signature));
10905 }
10906 *slot = (void *)dwo_unit;
10907 }
10908 }
10909
10910 /* DWP file .debug_{cu,tu}_index section format:
10911 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10912 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10913
10914 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10915 officially standard DWP format was published with DWARF v5 and is called
10916 Version 5. There are no versions 3 or 4.
10917
10918 DWP Version 1:
10919
10920 Both index sections have the same format, and serve to map a 64-bit
10921 signature to a set of section numbers. Each section begins with a header,
10922 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10923 indexes, and a pool of 32-bit section numbers. The index sections will be
10924 aligned at 8-byte boundaries in the file.
10925
10926 The index section header consists of:
10927
10928 V, 32 bit version number
10929 -, 32 bits unused
10930 N, 32 bit number of compilation units or type units in the index
10931 M, 32 bit number of slots in the hash table
10932
10933 Numbers are recorded using the byte order of the application binary.
10934
10935 The hash table begins at offset 16 in the section, and consists of an array
10936 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10937 order of the application binary). Unused slots in the hash table are 0.
10938 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10939
10940 The parallel table begins immediately after the hash table
10941 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10942 array of 32-bit indexes (using the byte order of the application binary),
10943 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10944 table contains a 32-bit index into the pool of section numbers. For unused
10945 hash table slots, the corresponding entry in the parallel table will be 0.
10946
10947 The pool of section numbers begins immediately following the hash table
10948 (at offset 16 + 12 * M from the beginning of the section). The pool of
10949 section numbers consists of an array of 32-bit words (using the byte order
10950 of the application binary). Each item in the array is indexed starting
10951 from 0. The hash table entry provides the index of the first section
10952 number in the set. Additional section numbers in the set follow, and the
10953 set is terminated by a 0 entry (section number 0 is not used in ELF).
10954
10955 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10956 section must be the first entry in the set, and the .debug_abbrev.dwo must
10957 be the second entry. Other members of the set may follow in any order.
10958
10959 ---
10960
10961 DWP Versions 2 and 5:
10962
10963 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10964 and the entries in the index tables are now offsets into these sections.
10965 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10966 section.
10967
10968 Index Section Contents:
10969 Header
10970 Hash Table of Signatures dwp_hash_table.hash_table
10971 Parallel Table of Indices dwp_hash_table.unit_table
10972 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10973 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10974
10975 The index section header consists of:
10976
10977 V, 32 bit version number
10978 L, 32 bit number of columns in the table of section offsets
10979 N, 32 bit number of compilation units or type units in the index
10980 M, 32 bit number of slots in the hash table
10981
10982 Numbers are recorded using the byte order of the application binary.
10983
10984 The hash table has the same format as version 1.
10985 The parallel table of indices has the same format as version 1,
10986 except that the entries are origin-1 indices into the table of sections
10987 offsets and the table of section sizes.
10988
10989 The table of offsets begins immediately following the parallel table
10990 (at offset 16 + 12 * M from the beginning of the section). The table is
10991 a two-dimensional array of 32-bit words (using the byte order of the
10992 application binary), with L columns and N+1 rows, in row-major order.
10993 Each row in the array is indexed starting from 0. The first row provides
10994 a key to the remaining rows: each column in this row provides an identifier
10995 for a debug section, and the offsets in the same column of subsequent rows
10996 refer to that section. The section identifiers for Version 2 are:
10997
10998 DW_SECT_INFO 1 .debug_info.dwo
10999 DW_SECT_TYPES 2 .debug_types.dwo
11000 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11001 DW_SECT_LINE 4 .debug_line.dwo
11002 DW_SECT_LOC 5 .debug_loc.dwo
11003 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11004 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11005 DW_SECT_MACRO 8 .debug_macro.dwo
11006
11007 The section identifiers for Version 5 are:
11008
11009 DW_SECT_INFO_V5 1 .debug_info.dwo
11010 DW_SECT_RESERVED_V5 2 --
11011 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11012 DW_SECT_LINE_V5 4 .debug_line.dwo
11013 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11014 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11015 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11016 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11017
11018 The offsets provided by the CU and TU index sections are the base offsets
11019 for the contributions made by each CU or TU to the corresponding section
11020 in the package file. Each CU and TU header contains an abbrev_offset
11021 field, used to find the abbreviations table for that CU or TU within the
11022 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11023 be interpreted as relative to the base offset given in the index section.
11024 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11025 should be interpreted as relative to the base offset for .debug_line.dwo,
11026 and offsets into other debug sections obtained from DWARF attributes should
11027 also be interpreted as relative to the corresponding base offset.
11028
11029 The table of sizes begins immediately following the table of offsets.
11030 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11031 with L columns and N rows, in row-major order. Each row in the array is
11032 indexed starting from 1 (row 0 is shared by the two tables).
11033
11034 ---
11035
11036 Hash table lookup is handled the same in version 1 and 2:
11037
11038 We assume that N and M will not exceed 2^32 - 1.
11039 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11040
11041 Given a 64-bit compilation unit signature or a type signature S, an entry
11042 in the hash table is located as follows:
11043
11044 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11045 the low-order k bits all set to 1.
11046
11047 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11048
11049 3) If the hash table entry at index H matches the signature, use that
11050 entry. If the hash table entry at index H is unused (all zeroes),
11051 terminate the search: the signature is not present in the table.
11052
11053 4) Let H = (H + H') modulo M. Repeat at Step 3.
11054
11055 Because M > N and H' and M are relatively prime, the search is guaranteed
11056 to stop at an unused slot or find the match. */
11057
11058 /* Create a hash table to map DWO IDs to their CU/TU entry in
11059 .debug_{info,types}.dwo in DWP_FILE.
11060 Returns NULL if there isn't one.
11061 Note: This function processes DWP files only, not DWO files. */
11062
11063 static struct dwp_hash_table *
11064 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11065 struct dwp_file *dwp_file, int is_debug_types)
11066 {
11067 struct objfile *objfile = per_objfile->objfile;
11068 bfd *dbfd = dwp_file->dbfd.get ();
11069 const gdb_byte *index_ptr, *index_end;
11070 struct dwarf2_section_info *index;
11071 uint32_t version, nr_columns, nr_units, nr_slots;
11072 struct dwp_hash_table *htab;
11073
11074 if (is_debug_types)
11075 index = &dwp_file->sections.tu_index;
11076 else
11077 index = &dwp_file->sections.cu_index;
11078
11079 if (index->empty ())
11080 return NULL;
11081 index->read (objfile);
11082
11083 index_ptr = index->buffer;
11084 index_end = index_ptr + index->size;
11085
11086 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11087 For now it's safe to just read 4 bytes (particularly as it's difficult to
11088 tell if you're dealing with Version 5 before you've read the version). */
11089 version = read_4_bytes (dbfd, index_ptr);
11090 index_ptr += 4;
11091 if (version == 2 || version == 5)
11092 nr_columns = read_4_bytes (dbfd, index_ptr);
11093 else
11094 nr_columns = 0;
11095 index_ptr += 4;
11096 nr_units = read_4_bytes (dbfd, index_ptr);
11097 index_ptr += 4;
11098 nr_slots = read_4_bytes (dbfd, index_ptr);
11099 index_ptr += 4;
11100
11101 if (version != 1 && version != 2 && version != 5)
11102 {
11103 error (_("Dwarf Error: unsupported DWP file version (%s)"
11104 " [in module %s]"),
11105 pulongest (version), dwp_file->name);
11106 }
11107 if (nr_slots != (nr_slots & -nr_slots))
11108 {
11109 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11110 " is not power of 2 [in module %s]"),
11111 pulongest (nr_slots), dwp_file->name);
11112 }
11113
11114 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11115 htab->version = version;
11116 htab->nr_columns = nr_columns;
11117 htab->nr_units = nr_units;
11118 htab->nr_slots = nr_slots;
11119 htab->hash_table = index_ptr;
11120 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11121
11122 /* Exit early if the table is empty. */
11123 if (nr_slots == 0 || nr_units == 0
11124 || (version == 2 && nr_columns == 0)
11125 || (version == 5 && nr_columns == 0))
11126 {
11127 /* All must be zero. */
11128 if (nr_slots != 0 || nr_units != 0
11129 || (version == 2 && nr_columns != 0)
11130 || (version == 5 && nr_columns != 0))
11131 {
11132 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11133 " all zero [in modules %s]"),
11134 dwp_file->name);
11135 }
11136 return htab;
11137 }
11138
11139 if (version == 1)
11140 {
11141 htab->section_pool.v1.indices =
11142 htab->unit_table + sizeof (uint32_t) * nr_slots;
11143 /* It's harder to decide whether the section is too small in v1.
11144 V1 is deprecated anyway so we punt. */
11145 }
11146 else if (version == 2)
11147 {
11148 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11149 int *ids = htab->section_pool.v2.section_ids;
11150 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11151 /* Reverse map for error checking. */
11152 int ids_seen[DW_SECT_MAX + 1];
11153 int i;
11154
11155 if (nr_columns < 2)
11156 {
11157 error (_("Dwarf Error: bad DWP hash table, too few columns"
11158 " in section table [in module %s]"),
11159 dwp_file->name);
11160 }
11161 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11162 {
11163 error (_("Dwarf Error: bad DWP hash table, too many columns"
11164 " in section table [in module %s]"),
11165 dwp_file->name);
11166 }
11167 memset (ids, 255, sizeof_ids);
11168 memset (ids_seen, 255, sizeof (ids_seen));
11169 for (i = 0; i < nr_columns; ++i)
11170 {
11171 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11172
11173 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11174 {
11175 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11176 " in section table [in module %s]"),
11177 id, dwp_file->name);
11178 }
11179 if (ids_seen[id] != -1)
11180 {
11181 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11182 " id %d in section table [in module %s]"),
11183 id, dwp_file->name);
11184 }
11185 ids_seen[id] = i;
11186 ids[i] = id;
11187 }
11188 /* Must have exactly one info or types section. */
11189 if (((ids_seen[DW_SECT_INFO] != -1)
11190 + (ids_seen[DW_SECT_TYPES] != -1))
11191 != 1)
11192 {
11193 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11194 " DWO info/types section [in module %s]"),
11195 dwp_file->name);
11196 }
11197 /* Must have an abbrev section. */
11198 if (ids_seen[DW_SECT_ABBREV] == -1)
11199 {
11200 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11201 " section [in module %s]"),
11202 dwp_file->name);
11203 }
11204 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11205 htab->section_pool.v2.sizes =
11206 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11207 * nr_units * nr_columns);
11208 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11209 * nr_units * nr_columns))
11210 > index_end)
11211 {
11212 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11213 " [in module %s]"),
11214 dwp_file->name);
11215 }
11216 }
11217 else /* version == 5 */
11218 {
11219 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11220 int *ids = htab->section_pool.v5.section_ids;
11221 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11222 /* Reverse map for error checking. */
11223 int ids_seen[DW_SECT_MAX_V5 + 1];
11224
11225 if (nr_columns < 2)
11226 {
11227 error (_("Dwarf Error: bad DWP hash table, too few columns"
11228 " in section table [in module %s]"),
11229 dwp_file->name);
11230 }
11231 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11232 {
11233 error (_("Dwarf Error: bad DWP hash table, too many columns"
11234 " in section table [in module %s]"),
11235 dwp_file->name);
11236 }
11237 memset (ids, 255, sizeof_ids);
11238 memset (ids_seen, 255, sizeof (ids_seen));
11239 for (int i = 0; i < nr_columns; ++i)
11240 {
11241 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11242
11243 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11244 {
11245 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11246 " in section table [in module %s]"),
11247 id, dwp_file->name);
11248 }
11249 if (ids_seen[id] != -1)
11250 {
11251 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11252 " id %d in section table [in module %s]"),
11253 id, dwp_file->name);
11254 }
11255 ids_seen[id] = i;
11256 ids[i] = id;
11257 }
11258 /* Must have seen an info section. */
11259 if (ids_seen[DW_SECT_INFO_V5] == -1)
11260 {
11261 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11262 " DWO info/types section [in module %s]"),
11263 dwp_file->name);
11264 }
11265 /* Must have an abbrev section. */
11266 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11267 {
11268 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11269 " section [in module %s]"),
11270 dwp_file->name);
11271 }
11272 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11273 htab->section_pool.v5.sizes
11274 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11275 * nr_units * nr_columns);
11276 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11277 * nr_units * nr_columns))
11278 > index_end)
11279 {
11280 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11281 " [in module %s]"),
11282 dwp_file->name);
11283 }
11284 }
11285
11286 return htab;
11287 }
11288
11289 /* Update SECTIONS with the data from SECTP.
11290
11291 This function is like the other "locate" section routines, but in
11292 this context the sections to read comes from the DWP V1 hash table,
11293 not the full ELF section table.
11294
11295 The result is non-zero for success, or zero if an error was found. */
11296
11297 static int
11298 locate_v1_virtual_dwo_sections (asection *sectp,
11299 struct virtual_v1_dwo_sections *sections)
11300 {
11301 const struct dwop_section_names *names = &dwop_section_names;
11302
11303 if (names->abbrev_dwo.matches (sectp->name))
11304 {
11305 /* There can be only one. */
11306 if (sections->abbrev.s.section != NULL)
11307 return 0;
11308 sections->abbrev.s.section = sectp;
11309 sections->abbrev.size = bfd_section_size (sectp);
11310 }
11311 else if (names->info_dwo.matches (sectp->name)
11312 || names->types_dwo.matches (sectp->name))
11313 {
11314 /* There can be only one. */
11315 if (sections->info_or_types.s.section != NULL)
11316 return 0;
11317 sections->info_or_types.s.section = sectp;
11318 sections->info_or_types.size = bfd_section_size (sectp);
11319 }
11320 else if (names->line_dwo.matches (sectp->name))
11321 {
11322 /* There can be only one. */
11323 if (sections->line.s.section != NULL)
11324 return 0;
11325 sections->line.s.section = sectp;
11326 sections->line.size = bfd_section_size (sectp);
11327 }
11328 else if (names->loc_dwo.matches (sectp->name))
11329 {
11330 /* There can be only one. */
11331 if (sections->loc.s.section != NULL)
11332 return 0;
11333 sections->loc.s.section = sectp;
11334 sections->loc.size = bfd_section_size (sectp);
11335 }
11336 else if (names->macinfo_dwo.matches (sectp->name))
11337 {
11338 /* There can be only one. */
11339 if (sections->macinfo.s.section != NULL)
11340 return 0;
11341 sections->macinfo.s.section = sectp;
11342 sections->macinfo.size = bfd_section_size (sectp);
11343 }
11344 else if (names->macro_dwo.matches (sectp->name))
11345 {
11346 /* There can be only one. */
11347 if (sections->macro.s.section != NULL)
11348 return 0;
11349 sections->macro.s.section = sectp;
11350 sections->macro.size = bfd_section_size (sectp);
11351 }
11352 else if (names->str_offsets_dwo.matches (sectp->name))
11353 {
11354 /* There can be only one. */
11355 if (sections->str_offsets.s.section != NULL)
11356 return 0;
11357 sections->str_offsets.s.section = sectp;
11358 sections->str_offsets.size = bfd_section_size (sectp);
11359 }
11360 else
11361 {
11362 /* No other kind of section is valid. */
11363 return 0;
11364 }
11365
11366 return 1;
11367 }
11368
11369 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11370 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11371 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11372 This is for DWP version 1 files. */
11373
11374 static struct dwo_unit *
11375 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11376 struct dwp_file *dwp_file,
11377 uint32_t unit_index,
11378 const char *comp_dir,
11379 ULONGEST signature, int is_debug_types)
11380 {
11381 const struct dwp_hash_table *dwp_htab =
11382 is_debug_types ? dwp_file->tus : dwp_file->cus;
11383 bfd *dbfd = dwp_file->dbfd.get ();
11384 const char *kind = is_debug_types ? "TU" : "CU";
11385 struct dwo_file *dwo_file;
11386 struct dwo_unit *dwo_unit;
11387 struct virtual_v1_dwo_sections sections;
11388 void **dwo_file_slot;
11389 int i;
11390
11391 gdb_assert (dwp_file->version == 1);
11392
11393 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11394 kind, pulongest (unit_index), hex_string (signature),
11395 dwp_file->name);
11396
11397 /* Fetch the sections of this DWO unit.
11398 Put a limit on the number of sections we look for so that bad data
11399 doesn't cause us to loop forever. */
11400
11401 #define MAX_NR_V1_DWO_SECTIONS \
11402 (1 /* .debug_info or .debug_types */ \
11403 + 1 /* .debug_abbrev */ \
11404 + 1 /* .debug_line */ \
11405 + 1 /* .debug_loc */ \
11406 + 1 /* .debug_str_offsets */ \
11407 + 1 /* .debug_macro or .debug_macinfo */ \
11408 + 1 /* trailing zero */)
11409
11410 memset (&sections, 0, sizeof (sections));
11411
11412 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11413 {
11414 asection *sectp;
11415 uint32_t section_nr =
11416 read_4_bytes (dbfd,
11417 dwp_htab->section_pool.v1.indices
11418 + (unit_index + i) * sizeof (uint32_t));
11419
11420 if (section_nr == 0)
11421 break;
11422 if (section_nr >= dwp_file->num_sections)
11423 {
11424 error (_("Dwarf Error: bad DWP hash table, section number too large"
11425 " [in module %s]"),
11426 dwp_file->name);
11427 }
11428
11429 sectp = dwp_file->elf_sections[section_nr];
11430 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11431 {
11432 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11433 " [in module %s]"),
11434 dwp_file->name);
11435 }
11436 }
11437
11438 if (i < 2
11439 || sections.info_or_types.empty ()
11440 || sections.abbrev.empty ())
11441 {
11442 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11443 " [in module %s]"),
11444 dwp_file->name);
11445 }
11446 if (i == MAX_NR_V1_DWO_SECTIONS)
11447 {
11448 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11449 " [in module %s]"),
11450 dwp_file->name);
11451 }
11452
11453 /* It's easier for the rest of the code if we fake a struct dwo_file and
11454 have dwo_unit "live" in that. At least for now.
11455
11456 The DWP file can be made up of a random collection of CUs and TUs.
11457 However, for each CU + set of TUs that came from the same original DWO
11458 file, we can combine them back into a virtual DWO file to save space
11459 (fewer struct dwo_file objects to allocate). Remember that for really
11460 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11461
11462 std::string virtual_dwo_name =
11463 string_printf ("virtual-dwo/%d-%d-%d-%d",
11464 sections.abbrev.get_id (),
11465 sections.line.get_id (),
11466 sections.loc.get_id (),
11467 sections.str_offsets.get_id ());
11468 /* Can we use an existing virtual DWO file? */
11469 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11470 comp_dir);
11471 /* Create one if necessary. */
11472 if (*dwo_file_slot == NULL)
11473 {
11474 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11475 virtual_dwo_name.c_str ());
11476
11477 dwo_file = new struct dwo_file;
11478 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11479 dwo_file->comp_dir = comp_dir;
11480 dwo_file->sections.abbrev = sections.abbrev;
11481 dwo_file->sections.line = sections.line;
11482 dwo_file->sections.loc = sections.loc;
11483 dwo_file->sections.macinfo = sections.macinfo;
11484 dwo_file->sections.macro = sections.macro;
11485 dwo_file->sections.str_offsets = sections.str_offsets;
11486 /* The "str" section is global to the entire DWP file. */
11487 dwo_file->sections.str = dwp_file->sections.str;
11488 /* The info or types section is assigned below to dwo_unit,
11489 there's no need to record it in dwo_file.
11490 Also, we can't simply record type sections in dwo_file because
11491 we record a pointer into the vector in dwo_unit. As we collect more
11492 types we'll grow the vector and eventually have to reallocate space
11493 for it, invalidating all copies of pointers into the previous
11494 contents. */
11495 *dwo_file_slot = dwo_file;
11496 }
11497 else
11498 {
11499 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11500 virtual_dwo_name.c_str ());
11501
11502 dwo_file = (struct dwo_file *) *dwo_file_slot;
11503 }
11504
11505 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11506 dwo_unit->dwo_file = dwo_file;
11507 dwo_unit->signature = signature;
11508 dwo_unit->section =
11509 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11510 *dwo_unit->section = sections.info_or_types;
11511 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11512
11513 return dwo_unit;
11514 }
11515
11516 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11517 simplify them. Given a pointer to the containing section SECTION, and
11518 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11519 virtual section of just that piece. */
11520
11521 static struct dwarf2_section_info
11522 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11523 struct dwarf2_section_info *section,
11524 bfd_size_type offset, bfd_size_type size)
11525 {
11526 struct dwarf2_section_info result;
11527 asection *sectp;
11528
11529 gdb_assert (section != NULL);
11530 gdb_assert (!section->is_virtual);
11531
11532 memset (&result, 0, sizeof (result));
11533 result.s.containing_section = section;
11534 result.is_virtual = true;
11535
11536 if (size == 0)
11537 return result;
11538
11539 sectp = section->get_bfd_section ();
11540
11541 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11542 bounds of the real section. This is a pretty-rare event, so just
11543 flag an error (easier) instead of a warning and trying to cope. */
11544 if (sectp == NULL
11545 || offset + size > bfd_section_size (sectp))
11546 {
11547 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11548 " in section %s [in module %s]"),
11549 sectp ? bfd_section_name (sectp) : "<unknown>",
11550 objfile_name (per_objfile->objfile));
11551 }
11552
11553 result.virtual_offset = offset;
11554 result.size = size;
11555 return result;
11556 }
11557
11558 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11559 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11560 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11561 This is for DWP version 2 files. */
11562
11563 static struct dwo_unit *
11564 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11565 struct dwp_file *dwp_file,
11566 uint32_t unit_index,
11567 const char *comp_dir,
11568 ULONGEST signature, int is_debug_types)
11569 {
11570 const struct dwp_hash_table *dwp_htab =
11571 is_debug_types ? dwp_file->tus : dwp_file->cus;
11572 bfd *dbfd = dwp_file->dbfd.get ();
11573 const char *kind = is_debug_types ? "TU" : "CU";
11574 struct dwo_file *dwo_file;
11575 struct dwo_unit *dwo_unit;
11576 struct virtual_v2_or_v5_dwo_sections sections;
11577 void **dwo_file_slot;
11578 int i;
11579
11580 gdb_assert (dwp_file->version == 2);
11581
11582 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11583 kind, pulongest (unit_index), hex_string (signature),
11584 dwp_file->name);
11585
11586 /* Fetch the section offsets of this DWO unit. */
11587
11588 memset (&sections, 0, sizeof (sections));
11589
11590 for (i = 0; i < dwp_htab->nr_columns; ++i)
11591 {
11592 uint32_t offset = read_4_bytes (dbfd,
11593 dwp_htab->section_pool.v2.offsets
11594 + (((unit_index - 1) * dwp_htab->nr_columns
11595 + i)
11596 * sizeof (uint32_t)));
11597 uint32_t size = read_4_bytes (dbfd,
11598 dwp_htab->section_pool.v2.sizes
11599 + (((unit_index - 1) * dwp_htab->nr_columns
11600 + i)
11601 * sizeof (uint32_t)));
11602
11603 switch (dwp_htab->section_pool.v2.section_ids[i])
11604 {
11605 case DW_SECT_INFO:
11606 case DW_SECT_TYPES:
11607 sections.info_or_types_offset = offset;
11608 sections.info_or_types_size = size;
11609 break;
11610 case DW_SECT_ABBREV:
11611 sections.abbrev_offset = offset;
11612 sections.abbrev_size = size;
11613 break;
11614 case DW_SECT_LINE:
11615 sections.line_offset = offset;
11616 sections.line_size = size;
11617 break;
11618 case DW_SECT_LOC:
11619 sections.loc_offset = offset;
11620 sections.loc_size = size;
11621 break;
11622 case DW_SECT_STR_OFFSETS:
11623 sections.str_offsets_offset = offset;
11624 sections.str_offsets_size = size;
11625 break;
11626 case DW_SECT_MACINFO:
11627 sections.macinfo_offset = offset;
11628 sections.macinfo_size = size;
11629 break;
11630 case DW_SECT_MACRO:
11631 sections.macro_offset = offset;
11632 sections.macro_size = size;
11633 break;
11634 }
11635 }
11636
11637 /* It's easier for the rest of the code if we fake a struct dwo_file and
11638 have dwo_unit "live" in that. At least for now.
11639
11640 The DWP file can be made up of a random collection of CUs and TUs.
11641 However, for each CU + set of TUs that came from the same original DWO
11642 file, we can combine them back into a virtual DWO file to save space
11643 (fewer struct dwo_file objects to allocate). Remember that for really
11644 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11645
11646 std::string virtual_dwo_name =
11647 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11648 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11649 (long) (sections.line_size ? sections.line_offset : 0),
11650 (long) (sections.loc_size ? sections.loc_offset : 0),
11651 (long) (sections.str_offsets_size
11652 ? sections.str_offsets_offset : 0));
11653 /* Can we use an existing virtual DWO file? */
11654 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11655 comp_dir);
11656 /* Create one if necessary. */
11657 if (*dwo_file_slot == NULL)
11658 {
11659 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11660 virtual_dwo_name.c_str ());
11661
11662 dwo_file = new struct dwo_file;
11663 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11664 dwo_file->comp_dir = comp_dir;
11665 dwo_file->sections.abbrev =
11666 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11667 sections.abbrev_offset,
11668 sections.abbrev_size);
11669 dwo_file->sections.line =
11670 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11671 sections.line_offset,
11672 sections.line_size);
11673 dwo_file->sections.loc =
11674 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11675 sections.loc_offset, sections.loc_size);
11676 dwo_file->sections.macinfo =
11677 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11678 sections.macinfo_offset,
11679 sections.macinfo_size);
11680 dwo_file->sections.macro =
11681 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11682 sections.macro_offset,
11683 sections.macro_size);
11684 dwo_file->sections.str_offsets =
11685 create_dwp_v2_or_v5_section (per_objfile,
11686 &dwp_file->sections.str_offsets,
11687 sections.str_offsets_offset,
11688 sections.str_offsets_size);
11689 /* The "str" section is global to the entire DWP file. */
11690 dwo_file->sections.str = dwp_file->sections.str;
11691 /* The info or types section is assigned below to dwo_unit,
11692 there's no need to record it in dwo_file.
11693 Also, we can't simply record type sections in dwo_file because
11694 we record a pointer into the vector in dwo_unit. As we collect more
11695 types we'll grow the vector and eventually have to reallocate space
11696 for it, invalidating all copies of pointers into the previous
11697 contents. */
11698 *dwo_file_slot = dwo_file;
11699 }
11700 else
11701 {
11702 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11703 virtual_dwo_name.c_str ());
11704
11705 dwo_file = (struct dwo_file *) *dwo_file_slot;
11706 }
11707
11708 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11709 dwo_unit->dwo_file = dwo_file;
11710 dwo_unit->signature = signature;
11711 dwo_unit->section =
11712 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11713 *dwo_unit->section = create_dwp_v2_or_v5_section
11714 (per_objfile,
11715 is_debug_types
11716 ? &dwp_file->sections.types
11717 : &dwp_file->sections.info,
11718 sections.info_or_types_offset,
11719 sections.info_or_types_size);
11720 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11721
11722 return dwo_unit;
11723 }
11724
11725 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11726 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11727 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11728 This is for DWP version 5 files. */
11729
11730 static struct dwo_unit *
11731 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11732 struct dwp_file *dwp_file,
11733 uint32_t unit_index,
11734 const char *comp_dir,
11735 ULONGEST signature, int is_debug_types)
11736 {
11737 const struct dwp_hash_table *dwp_htab
11738 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11739 bfd *dbfd = dwp_file->dbfd.get ();
11740 const char *kind = is_debug_types ? "TU" : "CU";
11741 struct dwo_file *dwo_file;
11742 struct dwo_unit *dwo_unit;
11743 struct virtual_v2_or_v5_dwo_sections sections {};
11744 void **dwo_file_slot;
11745
11746 gdb_assert (dwp_file->version == 5);
11747
11748 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11749 kind, pulongest (unit_index), hex_string (signature),
11750 dwp_file->name);
11751
11752 /* Fetch the section offsets of this DWO unit. */
11753
11754 /* memset (&sections, 0, sizeof (sections)); */
11755
11756 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11757 {
11758 uint32_t offset = read_4_bytes (dbfd,
11759 dwp_htab->section_pool.v5.offsets
11760 + (((unit_index - 1)
11761 * dwp_htab->nr_columns
11762 + i)
11763 * sizeof (uint32_t)));
11764 uint32_t size = read_4_bytes (dbfd,
11765 dwp_htab->section_pool.v5.sizes
11766 + (((unit_index - 1) * dwp_htab->nr_columns
11767 + i)
11768 * sizeof (uint32_t)));
11769
11770 switch (dwp_htab->section_pool.v5.section_ids[i])
11771 {
11772 case DW_SECT_ABBREV_V5:
11773 sections.abbrev_offset = offset;
11774 sections.abbrev_size = size;
11775 break;
11776 case DW_SECT_INFO_V5:
11777 sections.info_or_types_offset = offset;
11778 sections.info_or_types_size = size;
11779 break;
11780 case DW_SECT_LINE_V5:
11781 sections.line_offset = offset;
11782 sections.line_size = size;
11783 break;
11784 case DW_SECT_LOCLISTS_V5:
11785 sections.loclists_offset = offset;
11786 sections.loclists_size = size;
11787 break;
11788 case DW_SECT_MACRO_V5:
11789 sections.macro_offset = offset;
11790 sections.macro_size = size;
11791 break;
11792 case DW_SECT_RNGLISTS_V5:
11793 sections.rnglists_offset = offset;
11794 sections.rnglists_size = size;
11795 break;
11796 case DW_SECT_STR_OFFSETS_V5:
11797 sections.str_offsets_offset = offset;
11798 sections.str_offsets_size = size;
11799 break;
11800 case DW_SECT_RESERVED_V5:
11801 default:
11802 break;
11803 }
11804 }
11805
11806 /* It's easier for the rest of the code if we fake a struct dwo_file and
11807 have dwo_unit "live" in that. At least for now.
11808
11809 The DWP file can be made up of a random collection of CUs and TUs.
11810 However, for each CU + set of TUs that came from the same original DWO
11811 file, we can combine them back into a virtual DWO file to save space
11812 (fewer struct dwo_file objects to allocate). Remember that for really
11813 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11814
11815 std::string virtual_dwo_name =
11816 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
11817 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11818 (long) (sections.line_size ? sections.line_offset : 0),
11819 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11820 (long) (sections.str_offsets_size
11821 ? sections.str_offsets_offset : 0),
11822 (long) (sections.macro_size ? sections.macro_offset : 0),
11823 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11824 /* Can we use an existing virtual DWO file? */
11825 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11826 virtual_dwo_name.c_str (),
11827 comp_dir);
11828 /* Create one if necessary. */
11829 if (*dwo_file_slot == NULL)
11830 {
11831 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11832 virtual_dwo_name.c_str ());
11833
11834 dwo_file = new struct dwo_file;
11835 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11836 dwo_file->comp_dir = comp_dir;
11837 dwo_file->sections.abbrev =
11838 create_dwp_v2_or_v5_section (per_objfile,
11839 &dwp_file->sections.abbrev,
11840 sections.abbrev_offset,
11841 sections.abbrev_size);
11842 dwo_file->sections.line =
11843 create_dwp_v2_or_v5_section (per_objfile,
11844 &dwp_file->sections.line,
11845 sections.line_offset, sections.line_size);
11846 dwo_file->sections.macro =
11847 create_dwp_v2_or_v5_section (per_objfile,
11848 &dwp_file->sections.macro,
11849 sections.macro_offset,
11850 sections.macro_size);
11851 dwo_file->sections.loclists =
11852 create_dwp_v2_or_v5_section (per_objfile,
11853 &dwp_file->sections.loclists,
11854 sections.loclists_offset,
11855 sections.loclists_size);
11856 dwo_file->sections.rnglists =
11857 create_dwp_v2_or_v5_section (per_objfile,
11858 &dwp_file->sections.rnglists,
11859 sections.rnglists_offset,
11860 sections.rnglists_size);
11861 dwo_file->sections.str_offsets =
11862 create_dwp_v2_or_v5_section (per_objfile,
11863 &dwp_file->sections.str_offsets,
11864 sections.str_offsets_offset,
11865 sections.str_offsets_size);
11866 /* The "str" section is global to the entire DWP file. */
11867 dwo_file->sections.str = dwp_file->sections.str;
11868 /* The info or types section is assigned below to dwo_unit,
11869 there's no need to record it in dwo_file.
11870 Also, we can't simply record type sections in dwo_file because
11871 we record a pointer into the vector in dwo_unit. As we collect more
11872 types we'll grow the vector and eventually have to reallocate space
11873 for it, invalidating all copies of pointers into the previous
11874 contents. */
11875 *dwo_file_slot = dwo_file;
11876 }
11877 else
11878 {
11879 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11880 virtual_dwo_name.c_str ());
11881
11882 dwo_file = (struct dwo_file *) *dwo_file_slot;
11883 }
11884
11885 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11886 dwo_unit->dwo_file = dwo_file;
11887 dwo_unit->signature = signature;
11888 dwo_unit->section
11889 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11890 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
11891 &dwp_file->sections.info,
11892 sections.info_or_types_offset,
11893 sections.info_or_types_size);
11894 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11895
11896 return dwo_unit;
11897 }
11898
11899 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11900 Returns NULL if the signature isn't found. */
11901
11902 static struct dwo_unit *
11903 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
11904 struct dwp_file *dwp_file, const char *comp_dir,
11905 ULONGEST signature, int is_debug_types)
11906 {
11907 const struct dwp_hash_table *dwp_htab =
11908 is_debug_types ? dwp_file->tus : dwp_file->cus;
11909 bfd *dbfd = dwp_file->dbfd.get ();
11910 uint32_t mask = dwp_htab->nr_slots - 1;
11911 uint32_t hash = signature & mask;
11912 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11913 unsigned int i;
11914 void **slot;
11915 struct dwo_unit find_dwo_cu;
11916
11917 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11918 find_dwo_cu.signature = signature;
11919 slot = htab_find_slot (is_debug_types
11920 ? dwp_file->loaded_tus.get ()
11921 : dwp_file->loaded_cus.get (),
11922 &find_dwo_cu, INSERT);
11923
11924 if (*slot != NULL)
11925 return (struct dwo_unit *) *slot;
11926
11927 /* Use a for loop so that we don't loop forever on bad debug info. */
11928 for (i = 0; i < dwp_htab->nr_slots; ++i)
11929 {
11930 ULONGEST signature_in_table;
11931
11932 signature_in_table =
11933 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11934 if (signature_in_table == signature)
11935 {
11936 uint32_t unit_index =
11937 read_4_bytes (dbfd,
11938 dwp_htab->unit_table + hash * sizeof (uint32_t));
11939
11940 if (dwp_file->version == 1)
11941 {
11942 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11943 unit_index, comp_dir,
11944 signature, is_debug_types);
11945 }
11946 else if (dwp_file->version == 2)
11947 {
11948 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11949 unit_index, comp_dir,
11950 signature, is_debug_types);
11951 }
11952 else /* version == 5 */
11953 {
11954 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11955 unit_index, comp_dir,
11956 signature, is_debug_types);
11957 }
11958 return (struct dwo_unit *) *slot;
11959 }
11960 if (signature_in_table == 0)
11961 return NULL;
11962 hash = (hash + hash2) & mask;
11963 }
11964
11965 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11966 " [in module %s]"),
11967 dwp_file->name);
11968 }
11969
11970 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11971 Open the file specified by FILE_NAME and hand it off to BFD for
11972 preliminary analysis. Return a newly initialized bfd *, which
11973 includes a canonicalized copy of FILE_NAME.
11974 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11975 SEARCH_CWD is true if the current directory is to be searched.
11976 It will be searched before debug-file-directory.
11977 If successful, the file is added to the bfd include table of the
11978 objfile's bfd (see gdb_bfd_record_inclusion).
11979 If unable to find/open the file, return NULL.
11980 NOTE: This function is derived from symfile_bfd_open. */
11981
11982 static gdb_bfd_ref_ptr
11983 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
11984 const char *file_name, int is_dwp, int search_cwd)
11985 {
11986 int desc;
11987 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11988 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11989 to debug_file_directory. */
11990 const char *search_path;
11991 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11992
11993 gdb::unique_xmalloc_ptr<char> search_path_holder;
11994 if (search_cwd)
11995 {
11996 if (*debug_file_directory != '\0')
11997 {
11998 search_path_holder.reset (concat (".", dirname_separator_string,
11999 debug_file_directory,
12000 (char *) NULL));
12001 search_path = search_path_holder.get ();
12002 }
12003 else
12004 search_path = ".";
12005 }
12006 else
12007 search_path = debug_file_directory;
12008
12009 /* Add the path for the executable binary to the list of search paths. */
12010 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12011 search_path_holder.reset (concat (objfile_dir.c_str (),
12012 dirname_separator_string,
12013 search_path, nullptr));
12014 search_path = search_path_holder.get ();
12015
12016 openp_flags flags = OPF_RETURN_REALPATH;
12017 if (is_dwp)
12018 flags |= OPF_SEARCH_IN_PATH;
12019
12020 gdb::unique_xmalloc_ptr<char> absolute_name;
12021 desc = openp (search_path, flags, file_name,
12022 O_RDONLY | O_BINARY, &absolute_name);
12023 if (desc < 0)
12024 return NULL;
12025
12026 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12027 gnutarget, desc));
12028 if (sym_bfd == NULL)
12029 return NULL;
12030 bfd_set_cacheable (sym_bfd.get (), 1);
12031
12032 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12033 return NULL;
12034
12035 /* Success. Record the bfd as having been included by the objfile's bfd.
12036 This is important because things like demangled_names_hash lives in the
12037 objfile's per_bfd space and may have references to things like symbol
12038 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12039 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12040
12041 return sym_bfd;
12042 }
12043
12044 /* Try to open DWO file FILE_NAME.
12045 COMP_DIR is the DW_AT_comp_dir attribute.
12046 The result is the bfd handle of the file.
12047 If there is a problem finding or opening the file, return NULL.
12048 Upon success, the canonicalized path of the file is stored in the bfd,
12049 same as symfile_bfd_open. */
12050
12051 static gdb_bfd_ref_ptr
12052 open_dwo_file (dwarf2_per_objfile *per_objfile,
12053 const char *file_name, const char *comp_dir)
12054 {
12055 if (IS_ABSOLUTE_PATH (file_name))
12056 return try_open_dwop_file (per_objfile, file_name,
12057 0 /*is_dwp*/, 0 /*search_cwd*/);
12058
12059 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12060
12061 if (comp_dir != NULL)
12062 {
12063 gdb::unique_xmalloc_ptr<char> path_to_try
12064 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12065
12066 /* NOTE: If comp_dir is a relative path, this will also try the
12067 search path, which seems useful. */
12068 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12069 0 /*is_dwp*/,
12070 1 /*search_cwd*/));
12071 if (abfd != NULL)
12072 return abfd;
12073 }
12074
12075 /* That didn't work, try debug-file-directory, which, despite its name,
12076 is a list of paths. */
12077
12078 if (*debug_file_directory == '\0')
12079 return NULL;
12080
12081 return try_open_dwop_file (per_objfile, file_name,
12082 0 /*is_dwp*/, 1 /*search_cwd*/);
12083 }
12084
12085 /* This function is mapped across the sections and remembers the offset and
12086 size of each of the DWO debugging sections we are interested in. */
12087
12088 static void
12089 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12090 dwo_sections *dwo_sections)
12091 {
12092 const struct dwop_section_names *names = &dwop_section_names;
12093
12094 if (names->abbrev_dwo.matches (sectp->name))
12095 {
12096 dwo_sections->abbrev.s.section = sectp;
12097 dwo_sections->abbrev.size = bfd_section_size (sectp);
12098 }
12099 else if (names->info_dwo.matches (sectp->name))
12100 {
12101 dwo_sections->info.s.section = sectp;
12102 dwo_sections->info.size = bfd_section_size (sectp);
12103 }
12104 else if (names->line_dwo.matches (sectp->name))
12105 {
12106 dwo_sections->line.s.section = sectp;
12107 dwo_sections->line.size = bfd_section_size (sectp);
12108 }
12109 else if (names->loc_dwo.matches (sectp->name))
12110 {
12111 dwo_sections->loc.s.section = sectp;
12112 dwo_sections->loc.size = bfd_section_size (sectp);
12113 }
12114 else if (names->loclists_dwo.matches (sectp->name))
12115 {
12116 dwo_sections->loclists.s.section = sectp;
12117 dwo_sections->loclists.size = bfd_section_size (sectp);
12118 }
12119 else if (names->macinfo_dwo.matches (sectp->name))
12120 {
12121 dwo_sections->macinfo.s.section = sectp;
12122 dwo_sections->macinfo.size = bfd_section_size (sectp);
12123 }
12124 else if (names->macro_dwo.matches (sectp->name))
12125 {
12126 dwo_sections->macro.s.section = sectp;
12127 dwo_sections->macro.size = bfd_section_size (sectp);
12128 }
12129 else if (names->rnglists_dwo.matches (sectp->name))
12130 {
12131 dwo_sections->rnglists.s.section = sectp;
12132 dwo_sections->rnglists.size = bfd_section_size (sectp);
12133 }
12134 else if (names->str_dwo.matches (sectp->name))
12135 {
12136 dwo_sections->str.s.section = sectp;
12137 dwo_sections->str.size = bfd_section_size (sectp);
12138 }
12139 else if (names->str_offsets_dwo.matches (sectp->name))
12140 {
12141 dwo_sections->str_offsets.s.section = sectp;
12142 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12143 }
12144 else if (names->types_dwo.matches (sectp->name))
12145 {
12146 struct dwarf2_section_info type_section;
12147
12148 memset (&type_section, 0, sizeof (type_section));
12149 type_section.s.section = sectp;
12150 type_section.size = bfd_section_size (sectp);
12151 dwo_sections->types.push_back (type_section);
12152 }
12153 }
12154
12155 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12156 by PER_CU. This is for the non-DWP case.
12157 The result is NULL if DWO_NAME can't be found. */
12158
12159 static struct dwo_file *
12160 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12161 const char *comp_dir)
12162 {
12163 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12164
12165 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12166 if (dbfd == NULL)
12167 {
12168 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12169
12170 return NULL;
12171 }
12172
12173 dwo_file_up dwo_file (new struct dwo_file);
12174 dwo_file->dwo_name = dwo_name;
12175 dwo_file->comp_dir = comp_dir;
12176 dwo_file->dbfd = std::move (dbfd);
12177
12178 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12179 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12180 &dwo_file->sections);
12181
12182 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12183 dwo_file->cus);
12184
12185 if (cu->per_cu->dwarf_version < 5)
12186 {
12187 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12188 dwo_file->sections.types, dwo_file->tus);
12189 }
12190 else
12191 {
12192 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12193 &dwo_file->sections.info, dwo_file->tus,
12194 rcuh_kind::COMPILE);
12195 }
12196
12197 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12198
12199 return dwo_file.release ();
12200 }
12201
12202 /* This function is mapped across the sections and remembers the offset and
12203 size of each of the DWP debugging sections common to version 1 and 2 that
12204 we are interested in. */
12205
12206 static void
12207 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12208 dwp_file *dwp_file)
12209 {
12210 const struct dwop_section_names *names = &dwop_section_names;
12211 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12212
12213 /* Record the ELF section number for later lookup: this is what the
12214 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12215 gdb_assert (elf_section_nr < dwp_file->num_sections);
12216 dwp_file->elf_sections[elf_section_nr] = sectp;
12217
12218 /* Look for specific sections that we need. */
12219 if (names->str_dwo.matches (sectp->name))
12220 {
12221 dwp_file->sections.str.s.section = sectp;
12222 dwp_file->sections.str.size = bfd_section_size (sectp);
12223 }
12224 else if (names->cu_index.matches (sectp->name))
12225 {
12226 dwp_file->sections.cu_index.s.section = sectp;
12227 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12228 }
12229 else if (names->tu_index.matches (sectp->name))
12230 {
12231 dwp_file->sections.tu_index.s.section = sectp;
12232 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12233 }
12234 }
12235
12236 /* This function is mapped across the sections and remembers the offset and
12237 size of each of the DWP version 2 debugging sections that we are interested
12238 in. This is split into a separate function because we don't know if we
12239 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12240
12241 static void
12242 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12243 {
12244 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12245 const struct dwop_section_names *names = &dwop_section_names;
12246 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12247
12248 /* Record the ELF section number for later lookup: this is what the
12249 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12250 gdb_assert (elf_section_nr < dwp_file->num_sections);
12251 dwp_file->elf_sections[elf_section_nr] = sectp;
12252
12253 /* Look for specific sections that we need. */
12254 if (names->abbrev_dwo.matches (sectp->name))
12255 {
12256 dwp_file->sections.abbrev.s.section = sectp;
12257 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12258 }
12259 else if (names->info_dwo.matches (sectp->name))
12260 {
12261 dwp_file->sections.info.s.section = sectp;
12262 dwp_file->sections.info.size = bfd_section_size (sectp);
12263 }
12264 else if (names->line_dwo.matches (sectp->name))
12265 {
12266 dwp_file->sections.line.s.section = sectp;
12267 dwp_file->sections.line.size = bfd_section_size (sectp);
12268 }
12269 else if (names->loc_dwo.matches (sectp->name))
12270 {
12271 dwp_file->sections.loc.s.section = sectp;
12272 dwp_file->sections.loc.size = bfd_section_size (sectp);
12273 }
12274 else if (names->macinfo_dwo.matches (sectp->name))
12275 {
12276 dwp_file->sections.macinfo.s.section = sectp;
12277 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12278 }
12279 else if (names->macro_dwo.matches (sectp->name))
12280 {
12281 dwp_file->sections.macro.s.section = sectp;
12282 dwp_file->sections.macro.size = bfd_section_size (sectp);
12283 }
12284 else if (names->str_offsets_dwo.matches (sectp->name))
12285 {
12286 dwp_file->sections.str_offsets.s.section = sectp;
12287 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12288 }
12289 else if (names->types_dwo.matches (sectp->name))
12290 {
12291 dwp_file->sections.types.s.section = sectp;
12292 dwp_file->sections.types.size = bfd_section_size (sectp);
12293 }
12294 }
12295
12296 /* This function is mapped across the sections and remembers the offset and
12297 size of each of the DWP version 5 debugging sections that we are interested
12298 in. This is split into a separate function because we don't know if we
12299 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12300
12301 static void
12302 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12303 {
12304 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12305 const struct dwop_section_names *names = &dwop_section_names;
12306 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12307
12308 /* Record the ELF section number for later lookup: this is what the
12309 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12310 gdb_assert (elf_section_nr < dwp_file->num_sections);
12311 dwp_file->elf_sections[elf_section_nr] = sectp;
12312
12313 /* Look for specific sections that we need. */
12314 if (names->abbrev_dwo.matches (sectp->name))
12315 {
12316 dwp_file->sections.abbrev.s.section = sectp;
12317 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12318 }
12319 else if (names->info_dwo.matches (sectp->name))
12320 {
12321 dwp_file->sections.info.s.section = sectp;
12322 dwp_file->sections.info.size = bfd_section_size (sectp);
12323 }
12324 else if (names->line_dwo.matches (sectp->name))
12325 {
12326 dwp_file->sections.line.s.section = sectp;
12327 dwp_file->sections.line.size = bfd_section_size (sectp);
12328 }
12329 else if (names->loclists_dwo.matches (sectp->name))
12330 {
12331 dwp_file->sections.loclists.s.section = sectp;
12332 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12333 }
12334 else if (names->macro_dwo.matches (sectp->name))
12335 {
12336 dwp_file->sections.macro.s.section = sectp;
12337 dwp_file->sections.macro.size = bfd_section_size (sectp);
12338 }
12339 else if (names->rnglists_dwo.matches (sectp->name))
12340 {
12341 dwp_file->sections.rnglists.s.section = sectp;
12342 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12343 }
12344 else if (names->str_offsets_dwo.matches (sectp->name))
12345 {
12346 dwp_file->sections.str_offsets.s.section = sectp;
12347 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12348 }
12349 }
12350
12351 /* Hash function for dwp_file loaded CUs/TUs. */
12352
12353 static hashval_t
12354 hash_dwp_loaded_cutus (const void *item)
12355 {
12356 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12357
12358 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12359 return dwo_unit->signature;
12360 }
12361
12362 /* Equality function for dwp_file loaded CUs/TUs. */
12363
12364 static int
12365 eq_dwp_loaded_cutus (const void *a, const void *b)
12366 {
12367 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12368 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12369
12370 return dua->signature == dub->signature;
12371 }
12372
12373 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12374
12375 static htab_up
12376 allocate_dwp_loaded_cutus_table ()
12377 {
12378 return htab_up (htab_create_alloc (3,
12379 hash_dwp_loaded_cutus,
12380 eq_dwp_loaded_cutus,
12381 NULL, xcalloc, xfree));
12382 }
12383
12384 /* Try to open DWP file FILE_NAME.
12385 The result is the bfd handle of the file.
12386 If there is a problem finding or opening the file, return NULL.
12387 Upon success, the canonicalized path of the file is stored in the bfd,
12388 same as symfile_bfd_open. */
12389
12390 static gdb_bfd_ref_ptr
12391 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12392 {
12393 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12394 1 /*is_dwp*/,
12395 1 /*search_cwd*/));
12396 if (abfd != NULL)
12397 return abfd;
12398
12399 /* Work around upstream bug 15652.
12400 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12401 [Whether that's a "bug" is debatable, but it is getting in our way.]
12402 We have no real idea where the dwp file is, because gdb's realpath-ing
12403 of the executable's path may have discarded the needed info.
12404 [IWBN if the dwp file name was recorded in the executable, akin to
12405 .gnu_debuglink, but that doesn't exist yet.]
12406 Strip the directory from FILE_NAME and search again. */
12407 if (*debug_file_directory != '\0')
12408 {
12409 /* Don't implicitly search the current directory here.
12410 If the user wants to search "." to handle this case,
12411 it must be added to debug-file-directory. */
12412 return try_open_dwop_file (per_objfile, lbasename (file_name),
12413 1 /*is_dwp*/,
12414 0 /*search_cwd*/);
12415 }
12416
12417 return NULL;
12418 }
12419
12420 /* Initialize the use of the DWP file for the current objfile.
12421 By convention the name of the DWP file is ${objfile}.dwp.
12422 The result is NULL if it can't be found. */
12423
12424 static std::unique_ptr<struct dwp_file>
12425 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12426 {
12427 struct objfile *objfile = per_objfile->objfile;
12428
12429 /* Try to find first .dwp for the binary file before any symbolic links
12430 resolving. */
12431
12432 /* If the objfile is a debug file, find the name of the real binary
12433 file and get the name of dwp file from there. */
12434 std::string dwp_name;
12435 if (objfile->separate_debug_objfile_backlink != NULL)
12436 {
12437 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12438 const char *backlink_basename = lbasename (backlink->original_name);
12439
12440 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12441 }
12442 else
12443 dwp_name = objfile->original_name;
12444
12445 dwp_name += ".dwp";
12446
12447 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12448 if (dbfd == NULL
12449 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12450 {
12451 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12452 dwp_name = objfile_name (objfile);
12453 dwp_name += ".dwp";
12454 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12455 }
12456
12457 if (dbfd == NULL)
12458 {
12459 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12460
12461 return std::unique_ptr<dwp_file> ();
12462 }
12463
12464 const char *name = bfd_get_filename (dbfd.get ());
12465 std::unique_ptr<struct dwp_file> dwp_file
12466 (new struct dwp_file (name, std::move (dbfd)));
12467
12468 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12469 dwp_file->elf_sections =
12470 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12471 dwp_file->num_sections, asection *);
12472
12473 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12474 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12475 dwp_file.get ());
12476
12477 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12478
12479 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12480
12481 /* The DWP file version is stored in the hash table. Oh well. */
12482 if (dwp_file->cus && dwp_file->tus
12483 && dwp_file->cus->version != dwp_file->tus->version)
12484 {
12485 /* Technically speaking, we should try to limp along, but this is
12486 pretty bizarre. We use pulongest here because that's the established
12487 portability solution (e.g, we cannot use %u for uint32_t). */
12488 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12489 " TU version %s [in DWP file %s]"),
12490 pulongest (dwp_file->cus->version),
12491 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12492 }
12493
12494 if (dwp_file->cus)
12495 dwp_file->version = dwp_file->cus->version;
12496 else if (dwp_file->tus)
12497 dwp_file->version = dwp_file->tus->version;
12498 else
12499 dwp_file->version = 2;
12500
12501 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12502 {
12503 if (dwp_file->version == 2)
12504 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12505 dwp_file.get ());
12506 else
12507 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12508 dwp_file.get ());
12509 }
12510
12511 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12512 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12513
12514 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12515 dwarf_read_debug_printf (" %s CUs, %s TUs",
12516 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12517 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12518
12519 return dwp_file;
12520 }
12521
12522 /* Wrapper around open_and_init_dwp_file, only open it once. */
12523
12524 static struct dwp_file *
12525 get_dwp_file (dwarf2_per_objfile *per_objfile)
12526 {
12527 if (!per_objfile->per_bfd->dwp_checked)
12528 {
12529 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12530 per_objfile->per_bfd->dwp_checked = 1;
12531 }
12532 return per_objfile->per_bfd->dwp_file.get ();
12533 }
12534
12535 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12536 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12537 or in the DWP file for the objfile, referenced by THIS_UNIT.
12538 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12539 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12540
12541 This is called, for example, when wanting to read a variable with a
12542 complex location. Therefore we don't want to do file i/o for every call.
12543 Therefore we don't want to look for a DWO file on every call.
12544 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12545 then we check if we've already seen DWO_NAME, and only THEN do we check
12546 for a DWO file.
12547
12548 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12549 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12550
12551 static struct dwo_unit *
12552 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12553 ULONGEST signature, int is_debug_types)
12554 {
12555 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12556 struct objfile *objfile = per_objfile->objfile;
12557 const char *kind = is_debug_types ? "TU" : "CU";
12558 void **dwo_file_slot;
12559 struct dwo_file *dwo_file;
12560 struct dwp_file *dwp_file;
12561
12562 /* First see if there's a DWP file.
12563 If we have a DWP file but didn't find the DWO inside it, don't
12564 look for the original DWO file. It makes gdb behave differently
12565 depending on whether one is debugging in the build tree. */
12566
12567 dwp_file = get_dwp_file (per_objfile);
12568 if (dwp_file != NULL)
12569 {
12570 const struct dwp_hash_table *dwp_htab =
12571 is_debug_types ? dwp_file->tus : dwp_file->cus;
12572
12573 if (dwp_htab != NULL)
12574 {
12575 struct dwo_unit *dwo_cutu =
12576 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12577 is_debug_types);
12578
12579 if (dwo_cutu != NULL)
12580 {
12581 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12582 kind, hex_string (signature),
12583 host_address_to_string (dwo_cutu));
12584
12585 return dwo_cutu;
12586 }
12587 }
12588 }
12589 else
12590 {
12591 /* No DWP file, look for the DWO file. */
12592
12593 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12594 if (*dwo_file_slot == NULL)
12595 {
12596 /* Read in the file and build a table of the CUs/TUs it contains. */
12597 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12598 }
12599 /* NOTE: This will be NULL if unable to open the file. */
12600 dwo_file = (struct dwo_file *) *dwo_file_slot;
12601
12602 if (dwo_file != NULL)
12603 {
12604 struct dwo_unit *dwo_cutu = NULL;
12605
12606 if (is_debug_types && dwo_file->tus)
12607 {
12608 struct dwo_unit find_dwo_cutu;
12609
12610 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12611 find_dwo_cutu.signature = signature;
12612 dwo_cutu
12613 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12614 &find_dwo_cutu);
12615 }
12616 else if (!is_debug_types && dwo_file->cus)
12617 {
12618 struct dwo_unit find_dwo_cutu;
12619
12620 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12621 find_dwo_cutu.signature = signature;
12622 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12623 &find_dwo_cutu);
12624 }
12625
12626 if (dwo_cutu != NULL)
12627 {
12628 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12629 kind, dwo_name, hex_string (signature),
12630 host_address_to_string (dwo_cutu));
12631
12632 return dwo_cutu;
12633 }
12634 }
12635 }
12636
12637 /* We didn't find it. This could mean a dwo_id mismatch, or
12638 someone deleted the DWO/DWP file, or the search path isn't set up
12639 correctly to find the file. */
12640
12641 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12642 kind, dwo_name, hex_string (signature));
12643
12644 /* This is a warning and not a complaint because it can be caused by
12645 pilot error (e.g., user accidentally deleting the DWO). */
12646 {
12647 /* Print the name of the DWP file if we looked there, helps the user
12648 better diagnose the problem. */
12649 std::string dwp_text;
12650
12651 if (dwp_file != NULL)
12652 dwp_text = string_printf (" [in DWP file %s]",
12653 lbasename (dwp_file->name));
12654
12655 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12656 " [in module %s]"),
12657 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12658 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12659 }
12660 return NULL;
12661 }
12662
12663 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12664 See lookup_dwo_cutu_unit for details. */
12665
12666 static struct dwo_unit *
12667 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12668 ULONGEST signature)
12669 {
12670 gdb_assert (!cu->per_cu->is_debug_types);
12671
12672 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12673 }
12674
12675 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12676 See lookup_dwo_cutu_unit for details. */
12677
12678 static struct dwo_unit *
12679 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12680 {
12681 gdb_assert (cu->per_cu->is_debug_types);
12682
12683 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12684
12685 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12686 }
12687
12688 /* Traversal function for queue_and_load_all_dwo_tus. */
12689
12690 static int
12691 queue_and_load_dwo_tu (void **slot, void *info)
12692 {
12693 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12694 dwarf2_cu *cu = (dwarf2_cu *) info;
12695 ULONGEST signature = dwo_unit->signature;
12696 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12697
12698 if (sig_type != NULL)
12699 {
12700 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12701 a real dependency of PER_CU on SIG_TYPE. That is detected later
12702 while processing PER_CU. */
12703 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12704 cu->per_cu->lang))
12705 load_full_type_unit (sig_type, cu->per_objfile);
12706 cu->per_cu->imported_symtabs_push (sig_type);
12707 }
12708
12709 return 1;
12710 }
12711
12712 /* Queue all TUs contained in the DWO of CU to be read in.
12713 The DWO may have the only definition of the type, though it may not be
12714 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12715 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12716
12717 static void
12718 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12719 {
12720 struct dwo_unit *dwo_unit;
12721 struct dwo_file *dwo_file;
12722
12723 gdb_assert (cu != nullptr);
12724 gdb_assert (!cu->per_cu->is_debug_types);
12725 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12726
12727 dwo_unit = cu->dwo_unit;
12728 gdb_assert (dwo_unit != NULL);
12729
12730 dwo_file = dwo_unit->dwo_file;
12731 if (dwo_file->tus != NULL)
12732 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12733 }
12734
12735 /* Read in various DIEs. */
12736
12737 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12738 Inherit only the children of the DW_AT_abstract_origin DIE not being
12739 already referenced by DW_AT_abstract_origin from the children of the
12740 current DIE. */
12741
12742 static void
12743 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12744 {
12745 struct die_info *child_die;
12746 sect_offset *offsetp;
12747 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12748 struct die_info *origin_die;
12749 /* Iterator of the ORIGIN_DIE children. */
12750 struct die_info *origin_child_die;
12751 struct attribute *attr;
12752 struct dwarf2_cu *origin_cu;
12753 struct pending **origin_previous_list_in_scope;
12754
12755 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12756 if (!attr)
12757 return;
12758
12759 /* Note that following die references may follow to a die in a
12760 different cu. */
12761
12762 origin_cu = cu;
12763 origin_die = follow_die_ref (die, attr, &origin_cu);
12764
12765 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12766 symbols in. */
12767 origin_previous_list_in_scope = origin_cu->list_in_scope;
12768 origin_cu->list_in_scope = cu->list_in_scope;
12769
12770 if (die->tag != origin_die->tag
12771 && !(die->tag == DW_TAG_inlined_subroutine
12772 && origin_die->tag == DW_TAG_subprogram))
12773 complaint (_("DIE %s and its abstract origin %s have different tags"),
12774 sect_offset_str (die->sect_off),
12775 sect_offset_str (origin_die->sect_off));
12776
12777 /* Find if the concrete and abstract trees are structurally the
12778 same. This is a shallow traversal and it is not bullet-proof;
12779 the compiler can trick the debugger into believing that the trees
12780 are isomorphic, whereas they actually are not. However, the
12781 likelyhood of this happening is pretty low, and a full-fledged
12782 check would be an overkill. */
12783 bool are_isomorphic = true;
12784 die_info *concrete_child = die->child;
12785 die_info *abstract_child = origin_die->child;
12786 while (concrete_child != nullptr || abstract_child != nullptr)
12787 {
12788 if (concrete_child == nullptr
12789 || abstract_child == nullptr
12790 || concrete_child->tag != abstract_child->tag)
12791 {
12792 are_isomorphic = false;
12793 break;
12794 }
12795
12796 concrete_child = concrete_child->sibling;
12797 abstract_child = abstract_child->sibling;
12798 }
12799
12800 /* Walk the origin's children in parallel to the concrete children.
12801 This helps match an origin child in case the debug info misses
12802 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12803 origin tree may not have the same tree structure as the concrete
12804 DIE, though. */
12805 die_info *corresponding_abstract_child
12806 = are_isomorphic ? origin_die->child : nullptr;
12807
12808 std::vector<sect_offset> offsets;
12809
12810 for (child_die = die->child;
12811 child_die && child_die->tag;
12812 child_die = child_die->sibling)
12813 {
12814 struct die_info *child_origin_die;
12815 struct dwarf2_cu *child_origin_cu;
12816
12817 /* We are trying to process concrete instance entries:
12818 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12819 it's not relevant to our analysis here. i.e. detecting DIEs that are
12820 present in the abstract instance but not referenced in the concrete
12821 one. */
12822 if (child_die->tag == DW_TAG_call_site
12823 || child_die->tag == DW_TAG_GNU_call_site)
12824 {
12825 if (are_isomorphic)
12826 corresponding_abstract_child
12827 = corresponding_abstract_child->sibling;
12828 continue;
12829 }
12830
12831 /* For each CHILD_DIE, find the corresponding child of
12832 ORIGIN_DIE. If there is more than one layer of
12833 DW_AT_abstract_origin, follow them all; there shouldn't be,
12834 but GCC versions at least through 4.4 generate this (GCC PR
12835 40573). */
12836 child_origin_die = child_die;
12837 child_origin_cu = cu;
12838 while (1)
12839 {
12840 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12841 child_origin_cu);
12842 if (attr == NULL)
12843 break;
12844 child_origin_die = follow_die_ref (child_origin_die, attr,
12845 &child_origin_cu);
12846 }
12847
12848 /* If missing DW_AT_abstract_origin, try the corresponding child
12849 of the origin. Clang emits such lexical scopes. */
12850 if (child_origin_die == child_die
12851 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12852 && are_isomorphic
12853 && child_die->tag == DW_TAG_lexical_block)
12854 child_origin_die = corresponding_abstract_child;
12855
12856 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12857 counterpart may exist. */
12858 if (child_origin_die != child_die)
12859 {
12860 if (child_die->tag != child_origin_die->tag
12861 && !(child_die->tag == DW_TAG_inlined_subroutine
12862 && child_origin_die->tag == DW_TAG_subprogram))
12863 complaint (_("Child DIE %s and its abstract origin %s have "
12864 "different tags"),
12865 sect_offset_str (child_die->sect_off),
12866 sect_offset_str (child_origin_die->sect_off));
12867 if (child_origin_die->parent != origin_die)
12868 complaint (_("Child DIE %s and its abstract origin %s have "
12869 "different parents"),
12870 sect_offset_str (child_die->sect_off),
12871 sect_offset_str (child_origin_die->sect_off));
12872 else
12873 offsets.push_back (child_origin_die->sect_off);
12874 }
12875
12876 if (are_isomorphic)
12877 corresponding_abstract_child = corresponding_abstract_child->sibling;
12878 }
12879 std::sort (offsets.begin (), offsets.end ());
12880 sect_offset *offsets_end = offsets.data () + offsets.size ();
12881 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12882 if (offsetp[-1] == *offsetp)
12883 complaint (_("Multiple children of DIE %s refer "
12884 "to DIE %s as their abstract origin"),
12885 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12886
12887 offsetp = offsets.data ();
12888 origin_child_die = origin_die->child;
12889 while (origin_child_die && origin_child_die->tag)
12890 {
12891 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12892 while (offsetp < offsets_end
12893 && *offsetp < origin_child_die->sect_off)
12894 offsetp++;
12895 if (offsetp >= offsets_end
12896 || *offsetp > origin_child_die->sect_off)
12897 {
12898 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12899 Check whether we're already processing ORIGIN_CHILD_DIE.
12900 This can happen with mutually referenced abstract_origins.
12901 PR 16581. */
12902 if (!origin_child_die->in_process)
12903 process_die (origin_child_die, origin_cu);
12904 }
12905 origin_child_die = origin_child_die->sibling;
12906 }
12907 origin_cu->list_in_scope = origin_previous_list_in_scope;
12908
12909 if (cu != origin_cu)
12910 compute_delayed_physnames (origin_cu);
12911 }
12912
12913 static void
12914 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12915 {
12916 struct objfile *objfile = cu->per_objfile->objfile;
12917 struct gdbarch *gdbarch = objfile->arch ();
12918 struct context_stack *newobj;
12919 CORE_ADDR lowpc;
12920 CORE_ADDR highpc;
12921 struct die_info *child_die;
12922 struct attribute *attr, *call_line, *call_file;
12923 const char *name;
12924 CORE_ADDR baseaddr;
12925 struct block *block;
12926 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12927 std::vector<struct symbol *> template_args;
12928 struct template_symbol *templ_func = NULL;
12929
12930 if (inlined_func)
12931 {
12932 /* If we do not have call site information, we can't show the
12933 caller of this inlined function. That's too confusing, so
12934 only use the scope for local variables. */
12935 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12936 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12937 if (call_line == NULL || call_file == NULL)
12938 {
12939 read_lexical_block_scope (die, cu);
12940 return;
12941 }
12942 }
12943
12944 baseaddr = objfile->text_section_offset ();
12945
12946 name = dwarf2_name (die, cu);
12947
12948 /* Ignore functions with missing or empty names. These are actually
12949 illegal according to the DWARF standard. */
12950 if (name == NULL)
12951 {
12952 complaint (_("missing name for subprogram DIE at %s"),
12953 sect_offset_str (die->sect_off));
12954 return;
12955 }
12956
12957 /* Ignore functions with missing or invalid low and high pc attributes. */
12958 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12959 <= PC_BOUNDS_INVALID)
12960 {
12961 attr = dwarf2_attr (die, DW_AT_external, cu);
12962 if (attr == nullptr || !attr->as_boolean ())
12963 complaint (_("cannot get low and high bounds "
12964 "for subprogram DIE at %s"),
12965 sect_offset_str (die->sect_off));
12966 return;
12967 }
12968
12969 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12970 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12971
12972 /* If we have any template arguments, then we must allocate a
12973 different sort of symbol. */
12974 for (child_die = die->child; child_die; child_die = child_die->sibling)
12975 {
12976 if (child_die->tag == DW_TAG_template_type_param
12977 || child_die->tag == DW_TAG_template_value_param)
12978 {
12979 templ_func = new (&objfile->objfile_obstack) template_symbol;
12980 templ_func->subclass = SYMBOL_TEMPLATE;
12981 break;
12982 }
12983 }
12984
12985 gdb_assert (cu->get_builder () != nullptr);
12986 newobj = cu->get_builder ()->push_context (0, lowpc);
12987 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12988 (struct symbol *) templ_func);
12989
12990 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12991 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12992 cu->per_cu->lang);
12993
12994 /* If there is a location expression for DW_AT_frame_base, record
12995 it. */
12996 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12997 if (attr != nullptr)
12998 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12999
13000 /* If there is a location for the static link, record it. */
13001 newobj->static_link = NULL;
13002 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13003 if (attr != nullptr)
13004 {
13005 newobj->static_link
13006 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13007 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13008 cu->addr_type ());
13009 }
13010
13011 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13012
13013 if (die->child != NULL)
13014 {
13015 child_die = die->child;
13016 while (child_die && child_die->tag)
13017 {
13018 if (child_die->tag == DW_TAG_template_type_param
13019 || child_die->tag == DW_TAG_template_value_param)
13020 {
13021 struct symbol *arg = new_symbol (child_die, NULL, cu);
13022
13023 if (arg != NULL)
13024 template_args.push_back (arg);
13025 }
13026 else
13027 process_die (child_die, cu);
13028 child_die = child_die->sibling;
13029 }
13030 }
13031
13032 inherit_abstract_dies (die, cu);
13033
13034 /* If we have a DW_AT_specification, we might need to import using
13035 directives from the context of the specification DIE. See the
13036 comment in determine_prefix. */
13037 if (cu->per_cu->lang == language_cplus
13038 && dwarf2_attr (die, DW_AT_specification, cu))
13039 {
13040 struct dwarf2_cu *spec_cu = cu;
13041 struct die_info *spec_die = die_specification (die, &spec_cu);
13042
13043 while (spec_die)
13044 {
13045 child_die = spec_die->child;
13046 while (child_die && child_die->tag)
13047 {
13048 if (child_die->tag == DW_TAG_imported_module)
13049 process_die (child_die, spec_cu);
13050 child_die = child_die->sibling;
13051 }
13052
13053 /* In some cases, GCC generates specification DIEs that
13054 themselves contain DW_AT_specification attributes. */
13055 spec_die = die_specification (spec_die, &spec_cu);
13056 }
13057 }
13058
13059 struct context_stack cstk = cu->get_builder ()->pop_context ();
13060 /* Make a block for the local symbols within. */
13061 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13062 cstk.static_link, lowpc, highpc);
13063
13064 /* For C++, set the block's scope. */
13065 if ((cu->per_cu->lang == language_cplus
13066 || cu->per_cu->lang == language_fortran
13067 || cu->per_cu->lang == language_d
13068 || cu->per_cu->lang == language_rust)
13069 && cu->processing_has_namespace_info)
13070 block_set_scope (block, determine_prefix (die, cu),
13071 &objfile->objfile_obstack);
13072
13073 /* If we have address ranges, record them. */
13074 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13075
13076 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13077
13078 /* Attach template arguments to function. */
13079 if (!template_args.empty ())
13080 {
13081 gdb_assert (templ_func != NULL);
13082
13083 templ_func->n_template_arguments = template_args.size ();
13084 templ_func->template_arguments
13085 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13086 templ_func->n_template_arguments);
13087 memcpy (templ_func->template_arguments,
13088 template_args.data (),
13089 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13090
13091 /* Make sure that the symtab is set on the new symbols. Even
13092 though they don't appear in this symtab directly, other parts
13093 of gdb assume that symbols do, and this is reasonably
13094 true. */
13095 for (symbol *sym : template_args)
13096 symbol_set_symtab (sym, symbol_symtab (templ_func));
13097 }
13098
13099 /* In C++, we can have functions nested inside functions (e.g., when
13100 a function declares a class that has methods). This means that
13101 when we finish processing a function scope, we may need to go
13102 back to building a containing block's symbol lists. */
13103 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13104 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13105
13106 /* If we've finished processing a top-level function, subsequent
13107 symbols go in the file symbol list. */
13108 if (cu->get_builder ()->outermost_context_p ())
13109 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13110 }
13111
13112 /* Process all the DIES contained within a lexical block scope. Start
13113 a new scope, process the dies, and then close the scope. */
13114
13115 static void
13116 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13117 {
13118 struct objfile *objfile = cu->per_objfile->objfile;
13119 struct gdbarch *gdbarch = objfile->arch ();
13120 CORE_ADDR lowpc, highpc;
13121 struct die_info *child_die;
13122 CORE_ADDR baseaddr;
13123
13124 baseaddr = objfile->text_section_offset ();
13125
13126 /* Ignore blocks with missing or invalid low and high pc attributes. */
13127 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13128 as multiple lexical blocks? Handling children in a sane way would
13129 be nasty. Might be easier to properly extend generic blocks to
13130 describe ranges. */
13131 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13132 {
13133 case PC_BOUNDS_NOT_PRESENT:
13134 /* DW_TAG_lexical_block has no attributes, process its children as if
13135 there was no wrapping by that DW_TAG_lexical_block.
13136 GCC does no longer produces such DWARF since GCC r224161. */
13137 for (child_die = die->child;
13138 child_die != NULL && child_die->tag;
13139 child_die = child_die->sibling)
13140 {
13141 /* We might already be processing this DIE. This can happen
13142 in an unusual circumstance -- where a subroutine A
13143 appears lexically in another subroutine B, but A actually
13144 inlines B. The recursion is broken here, rather than in
13145 inherit_abstract_dies, because it seems better to simply
13146 drop concrete children here. */
13147 if (!child_die->in_process)
13148 process_die (child_die, cu);
13149 }
13150 return;
13151 case PC_BOUNDS_INVALID:
13152 return;
13153 }
13154 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13155 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13156
13157 cu->get_builder ()->push_context (0, lowpc);
13158 if (die->child != NULL)
13159 {
13160 child_die = die->child;
13161 while (child_die && child_die->tag)
13162 {
13163 process_die (child_die, cu);
13164 child_die = child_die->sibling;
13165 }
13166 }
13167 inherit_abstract_dies (die, cu);
13168 struct context_stack cstk = cu->get_builder ()->pop_context ();
13169
13170 if (*cu->get_builder ()->get_local_symbols () != NULL
13171 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13172 {
13173 struct block *block
13174 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13175 cstk.start_addr, highpc);
13176
13177 /* Note that recording ranges after traversing children, as we
13178 do here, means that recording a parent's ranges entails
13179 walking across all its children's ranges as they appear in
13180 the address map, which is quadratic behavior.
13181
13182 It would be nicer to record the parent's ranges before
13183 traversing its children, simply overriding whatever you find
13184 there. But since we don't even decide whether to create a
13185 block until after we've traversed its children, that's hard
13186 to do. */
13187 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13188 }
13189 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13190 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13191 }
13192
13193 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13194
13195 static void
13196 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13197 {
13198 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13199 struct objfile *objfile = per_objfile->objfile;
13200 struct gdbarch *gdbarch = objfile->arch ();
13201 CORE_ADDR pc, baseaddr;
13202 struct attribute *attr;
13203 struct call_site *call_site, call_site_local;
13204 void **slot;
13205 int nparams;
13206 struct die_info *child_die;
13207
13208 baseaddr = objfile->text_section_offset ();
13209
13210 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13211 if (attr == NULL)
13212 {
13213 /* This was a pre-DWARF-5 GNU extension alias
13214 for DW_AT_call_return_pc. */
13215 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13216 }
13217 if (!attr)
13218 {
13219 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13220 "DIE %s [in module %s]"),
13221 sect_offset_str (die->sect_off), objfile_name (objfile));
13222 return;
13223 }
13224 pc = attr->as_address () + baseaddr;
13225 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13226
13227 if (cu->call_site_htab == NULL)
13228 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13229 NULL, &objfile->objfile_obstack,
13230 hashtab_obstack_allocate, NULL);
13231 call_site_local.pc = pc;
13232 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13233 if (*slot != NULL)
13234 {
13235 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13236 "DIE %s [in module %s]"),
13237 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13238 objfile_name (objfile));
13239 return;
13240 }
13241
13242 /* Count parameters at the caller. */
13243
13244 nparams = 0;
13245 for (child_die = die->child; child_die && child_die->tag;
13246 child_die = child_die->sibling)
13247 {
13248 if (child_die->tag != DW_TAG_call_site_parameter
13249 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13250 {
13251 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13252 "DW_TAG_call_site child DIE %s [in module %s]"),
13253 child_die->tag, sect_offset_str (child_die->sect_off),
13254 objfile_name (objfile));
13255 continue;
13256 }
13257
13258 nparams++;
13259 }
13260
13261 call_site
13262 = ((struct call_site *)
13263 obstack_alloc (&objfile->objfile_obstack,
13264 sizeof (*call_site)
13265 + (sizeof (*call_site->parameter) * (nparams - 1))));
13266 *slot = call_site;
13267 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13268 call_site->pc = pc;
13269
13270 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13271 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13272 {
13273 struct die_info *func_die;
13274
13275 /* Skip also over DW_TAG_inlined_subroutine. */
13276 for (func_die = die->parent;
13277 func_die && func_die->tag != DW_TAG_subprogram
13278 && func_die->tag != DW_TAG_subroutine_type;
13279 func_die = func_die->parent);
13280
13281 /* DW_AT_call_all_calls is a superset
13282 of DW_AT_call_all_tail_calls. */
13283 if (func_die
13284 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13285 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13286 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13287 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13288 {
13289 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13290 not complete. But keep CALL_SITE for look ups via call_site_htab,
13291 both the initial caller containing the real return address PC and
13292 the final callee containing the current PC of a chain of tail
13293 calls do not need to have the tail call list complete. But any
13294 function candidate for a virtual tail call frame searched via
13295 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13296 determined unambiguously. */
13297 }
13298 else
13299 {
13300 struct type *func_type = NULL;
13301
13302 if (func_die)
13303 func_type = get_die_type (func_die, cu);
13304 if (func_type != NULL)
13305 {
13306 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13307
13308 /* Enlist this call site to the function. */
13309 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13310 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13311 }
13312 else
13313 complaint (_("Cannot find function owning DW_TAG_call_site "
13314 "DIE %s [in module %s]"),
13315 sect_offset_str (die->sect_off), objfile_name (objfile));
13316 }
13317 }
13318
13319 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13320 if (attr == NULL)
13321 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13322 if (attr == NULL)
13323 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13324 if (attr == NULL)
13325 {
13326 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13327 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13328 }
13329 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13330 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13331 /* Keep NULL DWARF_BLOCK. */;
13332 else if (attr->form_is_block ())
13333 {
13334 struct dwarf2_locexpr_baton *dlbaton;
13335 struct dwarf_block *block = attr->as_block ();
13336
13337 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13338 dlbaton->data = block->data;
13339 dlbaton->size = block->size;
13340 dlbaton->per_objfile = per_objfile;
13341 dlbaton->per_cu = cu->per_cu;
13342
13343 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13344 }
13345 else if (attr->form_is_ref ())
13346 {
13347 struct dwarf2_cu *target_cu = cu;
13348 struct die_info *target_die;
13349
13350 target_die = follow_die_ref (die, attr, &target_cu);
13351 gdb_assert (target_cu->per_objfile->objfile == objfile);
13352 if (die_is_declaration (target_die, target_cu))
13353 {
13354 const char *target_physname;
13355
13356 /* Prefer the mangled name; otherwise compute the demangled one. */
13357 target_physname = dw2_linkage_name (target_die, target_cu);
13358 if (target_physname == NULL)
13359 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13360 if (target_physname == NULL)
13361 complaint (_("DW_AT_call_target target DIE has invalid "
13362 "physname, for referencing DIE %s [in module %s]"),
13363 sect_offset_str (die->sect_off), objfile_name (objfile));
13364 else
13365 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13366 }
13367 else
13368 {
13369 CORE_ADDR lowpc;
13370
13371 /* DW_AT_entry_pc should be preferred. */
13372 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13373 <= PC_BOUNDS_INVALID)
13374 complaint (_("DW_AT_call_target target DIE has invalid "
13375 "low pc, for referencing DIE %s [in module %s]"),
13376 sect_offset_str (die->sect_off), objfile_name (objfile));
13377 else
13378 {
13379 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13380 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13381 }
13382 }
13383 }
13384 else
13385 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13386 "block nor reference, for DIE %s [in module %s]"),
13387 sect_offset_str (die->sect_off), objfile_name (objfile));
13388
13389 call_site->per_cu = cu->per_cu;
13390 call_site->per_objfile = per_objfile;
13391
13392 for (child_die = die->child;
13393 child_die && child_die->tag;
13394 child_die = child_die->sibling)
13395 {
13396 struct call_site_parameter *parameter;
13397 struct attribute *loc, *origin;
13398
13399 if (child_die->tag != DW_TAG_call_site_parameter
13400 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13401 {
13402 /* Already printed the complaint above. */
13403 continue;
13404 }
13405
13406 gdb_assert (call_site->parameter_count < nparams);
13407 parameter = &call_site->parameter[call_site->parameter_count];
13408
13409 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13410 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13411 register is contained in DW_AT_call_value. */
13412
13413 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13414 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13415 if (origin == NULL)
13416 {
13417 /* This was a pre-DWARF-5 GNU extension alias
13418 for DW_AT_call_parameter. */
13419 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13420 }
13421 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13422 {
13423 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13424
13425 sect_offset sect_off = origin->get_ref_die_offset ();
13426 if (!cu->header.offset_in_cu_p (sect_off))
13427 {
13428 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13429 binding can be done only inside one CU. Such referenced DIE
13430 therefore cannot be even moved to DW_TAG_partial_unit. */
13431 complaint (_("DW_AT_call_parameter offset is not in CU for "
13432 "DW_TAG_call_site child DIE %s [in module %s]"),
13433 sect_offset_str (child_die->sect_off),
13434 objfile_name (objfile));
13435 continue;
13436 }
13437 parameter->u.param_cu_off
13438 = (cu_offset) (sect_off - cu->header.sect_off);
13439 }
13440 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13441 {
13442 complaint (_("No DW_FORM_block* DW_AT_location for "
13443 "DW_TAG_call_site child DIE %s [in module %s]"),
13444 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13445 continue;
13446 }
13447 else
13448 {
13449 struct dwarf_block *block = loc->as_block ();
13450
13451 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13452 (block->data, &block->data[block->size]);
13453 if (parameter->u.dwarf_reg != -1)
13454 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13455 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13456 &block->data[block->size],
13457 &parameter->u.fb_offset))
13458 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13459 else
13460 {
13461 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13462 "for DW_FORM_block* DW_AT_location is supported for "
13463 "DW_TAG_call_site child DIE %s "
13464 "[in module %s]"),
13465 sect_offset_str (child_die->sect_off),
13466 objfile_name (objfile));
13467 continue;
13468 }
13469 }
13470
13471 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13472 if (attr == NULL)
13473 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13474 if (attr == NULL || !attr->form_is_block ())
13475 {
13476 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13477 "DW_TAG_call_site child DIE %s [in module %s]"),
13478 sect_offset_str (child_die->sect_off),
13479 objfile_name (objfile));
13480 continue;
13481 }
13482
13483 struct dwarf_block *block = attr->as_block ();
13484 parameter->value = block->data;
13485 parameter->value_size = block->size;
13486
13487 /* Parameters are not pre-cleared by memset above. */
13488 parameter->data_value = NULL;
13489 parameter->data_value_size = 0;
13490 call_site->parameter_count++;
13491
13492 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13493 if (attr == NULL)
13494 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13495 if (attr != nullptr)
13496 {
13497 if (!attr->form_is_block ())
13498 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13499 "DW_TAG_call_site child DIE %s [in module %s]"),
13500 sect_offset_str (child_die->sect_off),
13501 objfile_name (objfile));
13502 else
13503 {
13504 block = attr->as_block ();
13505 parameter->data_value = block->data;
13506 parameter->data_value_size = block->size;
13507 }
13508 }
13509 }
13510 }
13511
13512 /* Helper function for read_variable. If DIE represents a virtual
13513 table, then return the type of the concrete object that is
13514 associated with the virtual table. Otherwise, return NULL. */
13515
13516 static struct type *
13517 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13518 {
13519 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13520 if (attr == NULL)
13521 return NULL;
13522
13523 /* Find the type DIE. */
13524 struct die_info *type_die = NULL;
13525 struct dwarf2_cu *type_cu = cu;
13526
13527 if (attr->form_is_ref ())
13528 type_die = follow_die_ref (die, attr, &type_cu);
13529 if (type_die == NULL)
13530 return NULL;
13531
13532 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13533 return NULL;
13534 return die_containing_type (type_die, type_cu);
13535 }
13536
13537 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13538
13539 static void
13540 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13541 {
13542 struct rust_vtable_symbol *storage = NULL;
13543
13544 if (cu->per_cu->lang == language_rust)
13545 {
13546 struct type *containing_type = rust_containing_type (die, cu);
13547
13548 if (containing_type != NULL)
13549 {
13550 struct objfile *objfile = cu->per_objfile->objfile;
13551
13552 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13553 storage->concrete_type = containing_type;
13554 storage->subclass = SYMBOL_RUST_VTABLE;
13555 }
13556 }
13557
13558 struct symbol *res = new_symbol (die, NULL, cu, storage);
13559 struct attribute *abstract_origin
13560 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13561 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13562 if (res == NULL && loc && abstract_origin)
13563 {
13564 /* We have a variable without a name, but with a location and an abstract
13565 origin. This may be a concrete instance of an abstract variable
13566 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13567 later. */
13568 struct dwarf2_cu *origin_cu = cu;
13569 struct die_info *origin_die
13570 = follow_die_ref (die, abstract_origin, &origin_cu);
13571 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13572 per_objfile->per_bfd->abstract_to_concrete
13573 [origin_die->sect_off].push_back (die->sect_off);
13574 }
13575 }
13576
13577 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13578 reading .debug_rnglists.
13579 Callback's type should be:
13580 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13581 Return true if the attributes are present and valid, otherwise,
13582 return false. */
13583
13584 template <typename Callback>
13585 static bool
13586 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13587 dwarf_tag tag, Callback &&callback)
13588 {
13589 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13590 struct objfile *objfile = per_objfile->objfile;
13591 bfd *obfd = objfile->obfd;
13592 /* Base address selection entry. */
13593 gdb::optional<CORE_ADDR> base;
13594 const gdb_byte *buffer;
13595 CORE_ADDR baseaddr;
13596 bool overflow = false;
13597 ULONGEST addr_index;
13598 struct dwarf2_section_info *rnglists_section;
13599
13600 base = cu->base_address;
13601 rnglists_section = cu_debug_rnglists_section (cu, tag);
13602 rnglists_section->read (objfile);
13603
13604 if (offset >= rnglists_section->size)
13605 {
13606 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13607 offset);
13608 return false;
13609 }
13610 buffer = rnglists_section->buffer + offset;
13611
13612 baseaddr = objfile->text_section_offset ();
13613
13614 while (1)
13615 {
13616 /* Initialize it due to a false compiler warning. */
13617 CORE_ADDR range_beginning = 0, range_end = 0;
13618 const gdb_byte *buf_end = (rnglists_section->buffer
13619 + rnglists_section->size);
13620 unsigned int bytes_read;
13621
13622 if (buffer == buf_end)
13623 {
13624 overflow = true;
13625 break;
13626 }
13627 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13628 switch (rlet)
13629 {
13630 case DW_RLE_end_of_list:
13631 break;
13632 case DW_RLE_base_address:
13633 if (buffer + cu->header.addr_size > buf_end)
13634 {
13635 overflow = true;
13636 break;
13637 }
13638 base = cu->header.read_address (obfd, buffer, &bytes_read);
13639 buffer += bytes_read;
13640 break;
13641 case DW_RLE_base_addressx:
13642 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13643 buffer += bytes_read;
13644 base = read_addr_index (cu, addr_index);
13645 break;
13646 case DW_RLE_start_length:
13647 if (buffer + cu->header.addr_size > buf_end)
13648 {
13649 overflow = true;
13650 break;
13651 }
13652 range_beginning = cu->header.read_address (obfd, buffer,
13653 &bytes_read);
13654 buffer += bytes_read;
13655 range_end = (range_beginning
13656 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13657 buffer += bytes_read;
13658 if (buffer > buf_end)
13659 {
13660 overflow = true;
13661 break;
13662 }
13663 break;
13664 case DW_RLE_startx_length:
13665 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13666 buffer += bytes_read;
13667 range_beginning = read_addr_index (cu, addr_index);
13668 if (buffer > buf_end)
13669 {
13670 overflow = true;
13671 break;
13672 }
13673 range_end = (range_beginning
13674 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13675 buffer += bytes_read;
13676 break;
13677 case DW_RLE_offset_pair:
13678 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13679 buffer += bytes_read;
13680 if (buffer > buf_end)
13681 {
13682 overflow = true;
13683 break;
13684 }
13685 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13686 buffer += bytes_read;
13687 if (buffer > buf_end)
13688 {
13689 overflow = true;
13690 break;
13691 }
13692 break;
13693 case DW_RLE_start_end:
13694 if (buffer + 2 * cu->header.addr_size > buf_end)
13695 {
13696 overflow = true;
13697 break;
13698 }
13699 range_beginning = cu->header.read_address (obfd, buffer,
13700 &bytes_read);
13701 buffer += bytes_read;
13702 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13703 buffer += bytes_read;
13704 break;
13705 case DW_RLE_startx_endx:
13706 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13707 buffer += bytes_read;
13708 range_beginning = read_addr_index (cu, addr_index);
13709 if (buffer > buf_end)
13710 {
13711 overflow = true;
13712 break;
13713 }
13714 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13715 buffer += bytes_read;
13716 range_end = read_addr_index (cu, addr_index);
13717 break;
13718 default:
13719 complaint (_("Invalid .debug_rnglists data (no base address)"));
13720 return false;
13721 }
13722 if (rlet == DW_RLE_end_of_list || overflow)
13723 break;
13724 if (rlet == DW_RLE_base_address)
13725 continue;
13726
13727 if (range_beginning > range_end)
13728 {
13729 /* Inverted range entries are invalid. */
13730 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13731 return false;
13732 }
13733
13734 /* Empty range entries have no effect. */
13735 if (range_beginning == range_end)
13736 continue;
13737
13738 /* Only DW_RLE_offset_pair needs the base address added. */
13739 if (rlet == DW_RLE_offset_pair)
13740 {
13741 if (!base.has_value ())
13742 {
13743 /* We have no valid base address for the DW_RLE_offset_pair. */
13744 complaint (_("Invalid .debug_rnglists data (no base address for "
13745 "DW_RLE_offset_pair)"));
13746 return false;
13747 }
13748
13749 range_beginning += *base;
13750 range_end += *base;
13751 }
13752
13753 /* A not-uncommon case of bad debug info.
13754 Don't pollute the addrmap with bad data. */
13755 if (range_beginning + baseaddr == 0
13756 && !per_objfile->per_bfd->has_section_at_zero)
13757 {
13758 complaint (_(".debug_rnglists entry has start address of zero"
13759 " [in module %s]"), objfile_name (objfile));
13760 continue;
13761 }
13762
13763 callback (range_beginning, range_end);
13764 }
13765
13766 if (overflow)
13767 {
13768 complaint (_("Offset %d is not terminated "
13769 "for DW_AT_ranges attribute"),
13770 offset);
13771 return false;
13772 }
13773
13774 return true;
13775 }
13776
13777 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13778 Callback's type should be:
13779 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13780 Return 1 if the attributes are present and valid, otherwise, return 0. */
13781
13782 template <typename Callback>
13783 static int
13784 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
13785 Callback &&callback)
13786 {
13787 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13788 struct objfile *objfile = per_objfile->objfile;
13789 struct comp_unit_head *cu_header = &cu->header;
13790 bfd *obfd = objfile->obfd;
13791 unsigned int addr_size = cu_header->addr_size;
13792 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13793 /* Base address selection entry. */
13794 gdb::optional<CORE_ADDR> base;
13795 unsigned int dummy;
13796 const gdb_byte *buffer;
13797 CORE_ADDR baseaddr;
13798
13799 if (cu_header->version >= 5)
13800 return dwarf2_rnglists_process (offset, cu, tag, callback);
13801
13802 base = cu->base_address;
13803
13804 per_objfile->per_bfd->ranges.read (objfile);
13805 if (offset >= per_objfile->per_bfd->ranges.size)
13806 {
13807 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13808 offset);
13809 return 0;
13810 }
13811 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13812
13813 baseaddr = objfile->text_section_offset ();
13814
13815 while (1)
13816 {
13817 CORE_ADDR range_beginning, range_end;
13818
13819 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13820 buffer += addr_size;
13821 range_end = cu->header.read_address (obfd, buffer, &dummy);
13822 buffer += addr_size;
13823 offset += 2 * addr_size;
13824
13825 /* An end of list marker is a pair of zero addresses. */
13826 if (range_beginning == 0 && range_end == 0)
13827 /* Found the end of list entry. */
13828 break;
13829
13830 /* Each base address selection entry is a pair of 2 values.
13831 The first is the largest possible address, the second is
13832 the base address. Check for a base address here. */
13833 if ((range_beginning & mask) == mask)
13834 {
13835 /* If we found the largest possible address, then we already
13836 have the base address in range_end. */
13837 base = range_end;
13838 continue;
13839 }
13840
13841 if (!base.has_value ())
13842 {
13843 /* We have no valid base address for the ranges
13844 data. */
13845 complaint (_("Invalid .debug_ranges data (no base address)"));
13846 return 0;
13847 }
13848
13849 if (range_beginning > range_end)
13850 {
13851 /* Inverted range entries are invalid. */
13852 complaint (_("Invalid .debug_ranges data (inverted range)"));
13853 return 0;
13854 }
13855
13856 /* Empty range entries have no effect. */
13857 if (range_beginning == range_end)
13858 continue;
13859
13860 range_beginning += *base;
13861 range_end += *base;
13862
13863 /* A not-uncommon case of bad debug info.
13864 Don't pollute the addrmap with bad data. */
13865 if (range_beginning + baseaddr == 0
13866 && !per_objfile->per_bfd->has_section_at_zero)
13867 {
13868 complaint (_(".debug_ranges entry has start address of zero"
13869 " [in module %s]"), objfile_name (objfile));
13870 continue;
13871 }
13872
13873 callback (range_beginning, range_end);
13874 }
13875
13876 return 1;
13877 }
13878
13879 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13880 Return 1 if the attributes are present and valid, otherwise, return 0.
13881 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
13882
13883 static int
13884 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13885 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13886 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
13887 {
13888 struct objfile *objfile = cu->per_objfile->objfile;
13889 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
13890 struct gdbarch *gdbarch = objfile->arch ();
13891 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13892 int low_set = 0;
13893 CORE_ADDR low = 0;
13894 CORE_ADDR high = 0;
13895 int retval;
13896
13897 retval = dwarf2_ranges_process (offset, cu, tag,
13898 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13899 {
13900 if (ranges_pst != NULL)
13901 {
13902 CORE_ADDR lowpc;
13903 CORE_ADDR highpc;
13904
13905 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13906 range_beginning + baseaddr)
13907 - baseaddr);
13908 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13909 range_end + baseaddr)
13910 - baseaddr);
13911 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
13912 lowpc, highpc - 1, ranges_pst);
13913 }
13914
13915 /* FIXME: This is recording everything as a low-high
13916 segment of consecutive addresses. We should have a
13917 data structure for discontiguous block ranges
13918 instead. */
13919 if (! low_set)
13920 {
13921 low = range_beginning;
13922 high = range_end;
13923 low_set = 1;
13924 }
13925 else
13926 {
13927 if (range_beginning < low)
13928 low = range_beginning;
13929 if (range_end > high)
13930 high = range_end;
13931 }
13932 });
13933 if (!retval)
13934 return 0;
13935
13936 if (! low_set)
13937 /* If the first entry is an end-of-list marker, the range
13938 describes an empty scope, i.e. no instructions. */
13939 return 0;
13940
13941 if (low_return)
13942 *low_return = low;
13943 if (high_return)
13944 *high_return = high;
13945 return 1;
13946 }
13947
13948 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13949 definition for the return value. *LOWPC and *HIGHPC are set iff
13950 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13951
13952 static enum pc_bounds_kind
13953 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13954 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13955 dwarf2_psymtab *pst)
13956 {
13957 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13958 struct attribute *attr;
13959 struct attribute *attr_high;
13960 CORE_ADDR low = 0;
13961 CORE_ADDR high = 0;
13962 enum pc_bounds_kind ret;
13963
13964 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13965 if (attr_high)
13966 {
13967 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13968 if (attr != nullptr)
13969 {
13970 low = attr->as_address ();
13971 high = attr_high->as_address ();
13972 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13973 high += low;
13974 }
13975 else
13976 /* Found high w/o low attribute. */
13977 return PC_BOUNDS_INVALID;
13978
13979 /* Found consecutive range of addresses. */
13980 ret = PC_BOUNDS_HIGH_LOW;
13981 }
13982 else
13983 {
13984 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13985 if (attr != nullptr && attr->form_is_unsigned ())
13986 {
13987 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13988 on DWARF version). */
13989 ULONGEST ranges_offset = attr->as_unsigned ();
13990
13991 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13992 this value. */
13993 if (die->tag != DW_TAG_compile_unit)
13994 ranges_offset += cu->gnu_ranges_base;
13995
13996 /* Value of the DW_AT_ranges attribute is the offset in the
13997 .debug_ranges section. */
13998 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
13999 die->tag))
14000 return PC_BOUNDS_INVALID;
14001 /* Found discontinuous range of addresses. */
14002 ret = PC_BOUNDS_RANGES;
14003 }
14004 else
14005 return PC_BOUNDS_NOT_PRESENT;
14006 }
14007
14008 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14009 if (high <= low)
14010 return PC_BOUNDS_INVALID;
14011
14012 /* When using the GNU linker, .gnu.linkonce. sections are used to
14013 eliminate duplicate copies of functions and vtables and such.
14014 The linker will arbitrarily choose one and discard the others.
14015 The AT_*_pc values for such functions refer to local labels in
14016 these sections. If the section from that file was discarded, the
14017 labels are not in the output, so the relocs get a value of 0.
14018 If this is a discarded function, mark the pc bounds as invalid,
14019 so that GDB will ignore it. */
14020 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14021 return PC_BOUNDS_INVALID;
14022
14023 *lowpc = low;
14024 if (highpc)
14025 *highpc = high;
14026 return ret;
14027 }
14028
14029 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14030 its low and high PC addresses. Do nothing if these addresses could not
14031 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14032 and HIGHPC to the high address if greater than HIGHPC. */
14033
14034 static void
14035 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14036 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14037 struct dwarf2_cu *cu)
14038 {
14039 CORE_ADDR low, high;
14040 struct die_info *child = die->child;
14041
14042 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14043 {
14044 *lowpc = std::min (*lowpc, low);
14045 *highpc = std::max (*highpc, high);
14046 }
14047
14048 /* If the language does not allow nested subprograms (either inside
14049 subprograms or lexical blocks), we're done. */
14050 if (cu->per_cu->lang != language_ada)
14051 return;
14052
14053 /* Check all the children of the given DIE. If it contains nested
14054 subprograms, then check their pc bounds. Likewise, we need to
14055 check lexical blocks as well, as they may also contain subprogram
14056 definitions. */
14057 while (child && child->tag)
14058 {
14059 if (child->tag == DW_TAG_subprogram
14060 || child->tag == DW_TAG_lexical_block)
14061 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14062 child = child->sibling;
14063 }
14064 }
14065
14066 /* Get the low and high pc's represented by the scope DIE, and store
14067 them in *LOWPC and *HIGHPC. If the correct values can't be
14068 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14069
14070 static void
14071 get_scope_pc_bounds (struct die_info *die,
14072 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14073 struct dwarf2_cu *cu)
14074 {
14075 CORE_ADDR best_low = (CORE_ADDR) -1;
14076 CORE_ADDR best_high = (CORE_ADDR) 0;
14077 CORE_ADDR current_low, current_high;
14078
14079 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14080 >= PC_BOUNDS_RANGES)
14081 {
14082 best_low = current_low;
14083 best_high = current_high;
14084 }
14085 else
14086 {
14087 struct die_info *child = die->child;
14088
14089 while (child && child->tag)
14090 {
14091 switch (child->tag) {
14092 case DW_TAG_subprogram:
14093 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14094 break;
14095 case DW_TAG_namespace:
14096 case DW_TAG_module:
14097 /* FIXME: carlton/2004-01-16: Should we do this for
14098 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14099 that current GCC's always emit the DIEs corresponding
14100 to definitions of methods of classes as children of a
14101 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14102 the DIEs giving the declarations, which could be
14103 anywhere). But I don't see any reason why the
14104 standards says that they have to be there. */
14105 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14106
14107 if (current_low != ((CORE_ADDR) -1))
14108 {
14109 best_low = std::min (best_low, current_low);
14110 best_high = std::max (best_high, current_high);
14111 }
14112 break;
14113 default:
14114 /* Ignore. */
14115 break;
14116 }
14117
14118 child = child->sibling;
14119 }
14120 }
14121
14122 *lowpc = best_low;
14123 *highpc = best_high;
14124 }
14125
14126 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14127 in DIE. */
14128
14129 static void
14130 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14131 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14132 {
14133 struct objfile *objfile = cu->per_objfile->objfile;
14134 struct gdbarch *gdbarch = objfile->arch ();
14135 struct attribute *attr;
14136 struct attribute *attr_high;
14137
14138 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14139 if (attr_high)
14140 {
14141 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14142 if (attr != nullptr)
14143 {
14144 CORE_ADDR low = attr->as_address ();
14145 CORE_ADDR high = attr_high->as_address ();
14146
14147 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14148 high += low;
14149
14150 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14151 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14152 cu->get_builder ()->record_block_range (block, low, high - 1);
14153 }
14154 }
14155
14156 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14157 if (attr != nullptr && attr->form_is_unsigned ())
14158 {
14159 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14160 on DWARF version). */
14161 ULONGEST ranges_offset = attr->as_unsigned ();
14162
14163 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14164 this value. */
14165 if (die->tag != DW_TAG_compile_unit)
14166 ranges_offset += cu->gnu_ranges_base;
14167
14168 std::vector<blockrange> blockvec;
14169 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14170 [&] (CORE_ADDR start, CORE_ADDR end)
14171 {
14172 start += baseaddr;
14173 end += baseaddr;
14174 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14175 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14176 cu->get_builder ()->record_block_range (block, start, end - 1);
14177 blockvec.emplace_back (start, end);
14178 });
14179
14180 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14181 }
14182 }
14183
14184 /* Check whether the producer field indicates either of GCC < 4.6, or the
14185 Intel C/C++ compiler, and cache the result in CU. */
14186
14187 static void
14188 check_producer (struct dwarf2_cu *cu)
14189 {
14190 int major, minor;
14191
14192 if (cu->producer == NULL)
14193 {
14194 /* For unknown compilers expect their behavior is DWARF version
14195 compliant.
14196
14197 GCC started to support .debug_types sections by -gdwarf-4 since
14198 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14199 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14200 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14201 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14202 }
14203 else if (producer_is_gcc (cu->producer, &major, &minor))
14204 {
14205 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14206 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14207 }
14208 else if (producer_is_icc (cu->producer, &major, &minor))
14209 {
14210 cu->producer_is_icc = true;
14211 cu->producer_is_icc_lt_14 = major < 14;
14212 }
14213 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14214 cu->producer_is_codewarrior = true;
14215 else
14216 {
14217 /* For other non-GCC compilers, expect their behavior is DWARF version
14218 compliant. */
14219 }
14220
14221 cu->checked_producer = true;
14222 }
14223
14224 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14225 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14226 during 4.6.0 experimental. */
14227
14228 static bool
14229 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14230 {
14231 if (!cu->checked_producer)
14232 check_producer (cu);
14233
14234 return cu->producer_is_gxx_lt_4_6;
14235 }
14236
14237
14238 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14239 with incorrect is_stmt attributes. */
14240
14241 static bool
14242 producer_is_codewarrior (struct dwarf2_cu *cu)
14243 {
14244 if (!cu->checked_producer)
14245 check_producer (cu);
14246
14247 return cu->producer_is_codewarrior;
14248 }
14249
14250 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14251 If that attribute is not available, return the appropriate
14252 default. */
14253
14254 static enum dwarf_access_attribute
14255 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14256 {
14257 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14258 if (attr != nullptr)
14259 {
14260 LONGEST value = attr->constant_value (-1);
14261 if (value == DW_ACCESS_public
14262 || value == DW_ACCESS_protected
14263 || value == DW_ACCESS_private)
14264 return (dwarf_access_attribute) value;
14265 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14266 plongest (value));
14267 }
14268
14269 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14270 {
14271 /* The default DWARF 2 accessibility for members is public, the default
14272 accessibility for inheritance is private. */
14273
14274 if (die->tag != DW_TAG_inheritance)
14275 return DW_ACCESS_public;
14276 else
14277 return DW_ACCESS_private;
14278 }
14279 else
14280 {
14281 /* DWARF 3+ defines the default accessibility a different way. The same
14282 rules apply now for DW_TAG_inheritance as for the members and it only
14283 depends on the container kind. */
14284
14285 if (die->parent->tag == DW_TAG_class_type)
14286 return DW_ACCESS_private;
14287 else
14288 return DW_ACCESS_public;
14289 }
14290 }
14291
14292 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14293 offset. If the attribute was not found return 0, otherwise return
14294 1. If it was found but could not properly be handled, set *OFFSET
14295 to 0. */
14296
14297 static int
14298 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14299 LONGEST *offset)
14300 {
14301 struct attribute *attr;
14302
14303 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14304 if (attr != NULL)
14305 {
14306 *offset = 0;
14307
14308 /* Note that we do not check for a section offset first here.
14309 This is because DW_AT_data_member_location is new in DWARF 4,
14310 so if we see it, we can assume that a constant form is really
14311 a constant and not a section offset. */
14312 if (attr->form_is_constant ())
14313 *offset = attr->constant_value (0);
14314 else if (attr->form_is_section_offset ())
14315 dwarf2_complex_location_expr_complaint ();
14316 else if (attr->form_is_block ())
14317 *offset = decode_locdesc (attr->as_block (), cu);
14318 else
14319 dwarf2_complex_location_expr_complaint ();
14320
14321 return 1;
14322 }
14323
14324 return 0;
14325 }
14326
14327 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14328
14329 static void
14330 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14331 struct field *field)
14332 {
14333 struct attribute *attr;
14334
14335 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14336 if (attr != NULL)
14337 {
14338 if (attr->form_is_constant ())
14339 {
14340 LONGEST offset = attr->constant_value (0);
14341 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14342 }
14343 else if (attr->form_is_section_offset ())
14344 dwarf2_complex_location_expr_complaint ();
14345 else if (attr->form_is_block ())
14346 {
14347 bool handled;
14348 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14349 if (handled)
14350 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14351 else
14352 {
14353 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14354 struct objfile *objfile = per_objfile->objfile;
14355 struct dwarf2_locexpr_baton *dlbaton
14356 = XOBNEW (&objfile->objfile_obstack,
14357 struct dwarf2_locexpr_baton);
14358 dlbaton->data = attr->as_block ()->data;
14359 dlbaton->size = attr->as_block ()->size;
14360 /* When using this baton, we want to compute the address
14361 of the field, not the value. This is why
14362 is_reference is set to false here. */
14363 dlbaton->is_reference = false;
14364 dlbaton->per_objfile = per_objfile;
14365 dlbaton->per_cu = cu->per_cu;
14366
14367 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14368 }
14369 }
14370 else
14371 dwarf2_complex_location_expr_complaint ();
14372 }
14373 }
14374
14375 /* Add an aggregate field to the field list. */
14376
14377 static void
14378 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14379 struct dwarf2_cu *cu)
14380 {
14381 struct objfile *objfile = cu->per_objfile->objfile;
14382 struct gdbarch *gdbarch = objfile->arch ();
14383 struct nextfield *new_field;
14384 struct attribute *attr;
14385 struct field *fp;
14386 const char *fieldname = "";
14387
14388 if (die->tag == DW_TAG_inheritance)
14389 {
14390 fip->baseclasses.emplace_back ();
14391 new_field = &fip->baseclasses.back ();
14392 }
14393 else
14394 {
14395 fip->fields.emplace_back ();
14396 new_field = &fip->fields.back ();
14397 }
14398
14399 new_field->offset = die->sect_off;
14400
14401 new_field->accessibility = dwarf2_access_attribute (die, cu);
14402 if (new_field->accessibility != DW_ACCESS_public)
14403 fip->non_public_fields = true;
14404
14405 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14406 if (attr != nullptr)
14407 new_field->virtuality = attr->as_virtuality ();
14408 else
14409 new_field->virtuality = DW_VIRTUALITY_none;
14410
14411 fp = &new_field->field;
14412
14413 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14414 {
14415 /* Data member other than a C++ static data member. */
14416
14417 /* Get type of field. */
14418 fp->set_type (die_type (die, cu));
14419
14420 SET_FIELD_BITPOS (*fp, 0);
14421
14422 /* Get bit size of field (zero if none). */
14423 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14424 if (attr != nullptr)
14425 {
14426 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14427 }
14428 else
14429 {
14430 FIELD_BITSIZE (*fp) = 0;
14431 }
14432
14433 /* Get bit offset of field. */
14434 handle_data_member_location (die, cu, fp);
14435 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14436 if (attr != nullptr && attr->form_is_constant ())
14437 {
14438 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14439 {
14440 /* For big endian bits, the DW_AT_bit_offset gives the
14441 additional bit offset from the MSB of the containing
14442 anonymous object to the MSB of the field. We don't
14443 have to do anything special since we don't need to
14444 know the size of the anonymous object. */
14445 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14446 + attr->constant_value (0)));
14447 }
14448 else
14449 {
14450 /* For little endian bits, compute the bit offset to the
14451 MSB of the anonymous object, subtract off the number of
14452 bits from the MSB of the field to the MSB of the
14453 object, and then subtract off the number of bits of
14454 the field itself. The result is the bit offset of
14455 the LSB of the field. */
14456 int anonymous_size;
14457 int bit_offset = attr->constant_value (0);
14458
14459 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14460 if (attr != nullptr && attr->form_is_constant ())
14461 {
14462 /* The size of the anonymous object containing
14463 the bit field is explicit, so use the
14464 indicated size (in bytes). */
14465 anonymous_size = attr->constant_value (0);
14466 }
14467 else
14468 {
14469 /* The size of the anonymous object containing
14470 the bit field must be inferred from the type
14471 attribute of the data member containing the
14472 bit field. */
14473 anonymous_size = TYPE_LENGTH (fp->type ());
14474 }
14475 SET_FIELD_BITPOS (*fp,
14476 (FIELD_BITPOS (*fp)
14477 + anonymous_size * bits_per_byte
14478 - bit_offset - FIELD_BITSIZE (*fp)));
14479 }
14480 }
14481 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14482 if (attr != NULL)
14483 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14484 + attr->constant_value (0)));
14485
14486 /* Get name of field. */
14487 fieldname = dwarf2_name (die, cu);
14488 if (fieldname == NULL)
14489 fieldname = "";
14490
14491 /* The name is already allocated along with this objfile, so we don't
14492 need to duplicate it for the type. */
14493 fp->name = fieldname;
14494
14495 /* Change accessibility for artificial fields (e.g. virtual table
14496 pointer or virtual base class pointer) to private. */
14497 if (dwarf2_attr (die, DW_AT_artificial, cu))
14498 {
14499 FIELD_ARTIFICIAL (*fp) = 1;
14500 new_field->accessibility = DW_ACCESS_private;
14501 fip->non_public_fields = true;
14502 }
14503 }
14504 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14505 {
14506 /* C++ static member. */
14507
14508 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14509 is a declaration, but all versions of G++ as of this writing
14510 (so through at least 3.2.1) incorrectly generate
14511 DW_TAG_variable tags. */
14512
14513 const char *physname;
14514
14515 /* Get name of field. */
14516 fieldname = dwarf2_name (die, cu);
14517 if (fieldname == NULL)
14518 return;
14519
14520 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14521 if (attr
14522 /* Only create a symbol if this is an external value.
14523 new_symbol checks this and puts the value in the global symbol
14524 table, which we want. If it is not external, new_symbol
14525 will try to put the value in cu->list_in_scope which is wrong. */
14526 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14527 {
14528 /* A static const member, not much different than an enum as far as
14529 we're concerned, except that we can support more types. */
14530 new_symbol (die, NULL, cu);
14531 }
14532
14533 /* Get physical name. */
14534 physname = dwarf2_physname (fieldname, die, cu);
14535
14536 /* The name is already allocated along with this objfile, so we don't
14537 need to duplicate it for the type. */
14538 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14539 fp->set_type (die_type (die, cu));
14540 FIELD_NAME (*fp) = fieldname;
14541 }
14542 else if (die->tag == DW_TAG_inheritance)
14543 {
14544 /* C++ base class field. */
14545 handle_data_member_location (die, cu, fp);
14546 FIELD_BITSIZE (*fp) = 0;
14547 fp->set_type (die_type (die, cu));
14548 FIELD_NAME (*fp) = fp->type ()->name ();
14549 }
14550 else
14551 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14552 }
14553
14554 /* Can the type given by DIE define another type? */
14555
14556 static bool
14557 type_can_define_types (const struct die_info *die)
14558 {
14559 switch (die->tag)
14560 {
14561 case DW_TAG_typedef:
14562 case DW_TAG_class_type:
14563 case DW_TAG_structure_type:
14564 case DW_TAG_union_type:
14565 case DW_TAG_enumeration_type:
14566 return true;
14567
14568 default:
14569 return false;
14570 }
14571 }
14572
14573 /* Add a type definition defined in the scope of the FIP's class. */
14574
14575 static void
14576 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14577 struct dwarf2_cu *cu)
14578 {
14579 struct decl_field fp;
14580 memset (&fp, 0, sizeof (fp));
14581
14582 gdb_assert (type_can_define_types (die));
14583
14584 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14585 fp.name = dwarf2_name (die, cu);
14586 fp.type = read_type_die (die, cu);
14587
14588 /* Save accessibility. */
14589 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14590 switch (accessibility)
14591 {
14592 case DW_ACCESS_public:
14593 /* The assumed value if neither private nor protected. */
14594 break;
14595 case DW_ACCESS_private:
14596 fp.is_private = 1;
14597 break;
14598 case DW_ACCESS_protected:
14599 fp.is_protected = 1;
14600 break;
14601 }
14602
14603 if (die->tag == DW_TAG_typedef)
14604 fip->typedef_field_list.push_back (fp);
14605 else
14606 fip->nested_types_list.push_back (fp);
14607 }
14608
14609 /* A convenience typedef that's used when finding the discriminant
14610 field for a variant part. */
14611 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14612 offset_map_type;
14613
14614 /* Compute the discriminant range for a given variant. OBSTACK is
14615 where the results will be stored. VARIANT is the variant to
14616 process. IS_UNSIGNED indicates whether the discriminant is signed
14617 or unsigned. */
14618
14619 static const gdb::array_view<discriminant_range>
14620 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14621 bool is_unsigned)
14622 {
14623 std::vector<discriminant_range> ranges;
14624
14625 if (variant.default_branch)
14626 return {};
14627
14628 if (variant.discr_list_data == nullptr)
14629 {
14630 discriminant_range r
14631 = {variant.discriminant_value, variant.discriminant_value};
14632 ranges.push_back (r);
14633 }
14634 else
14635 {
14636 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14637 variant.discr_list_data->size);
14638 while (!data.empty ())
14639 {
14640 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14641 {
14642 complaint (_("invalid discriminant marker: %d"), data[0]);
14643 break;
14644 }
14645 bool is_range = data[0] == DW_DSC_range;
14646 data = data.slice (1);
14647
14648 ULONGEST low, high;
14649 unsigned int bytes_read;
14650
14651 if (data.empty ())
14652 {
14653 complaint (_("DW_AT_discr_list missing low value"));
14654 break;
14655 }
14656 if (is_unsigned)
14657 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14658 else
14659 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14660 &bytes_read);
14661 data = data.slice (bytes_read);
14662
14663 if (is_range)
14664 {
14665 if (data.empty ())
14666 {
14667 complaint (_("DW_AT_discr_list missing high value"));
14668 break;
14669 }
14670 if (is_unsigned)
14671 high = read_unsigned_leb128 (nullptr, data.data (),
14672 &bytes_read);
14673 else
14674 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14675 &bytes_read);
14676 data = data.slice (bytes_read);
14677 }
14678 else
14679 high = low;
14680
14681 ranges.push_back ({ low, high });
14682 }
14683 }
14684
14685 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14686 ranges.size ());
14687 std::copy (ranges.begin (), ranges.end (), result);
14688 return gdb::array_view<discriminant_range> (result, ranges.size ());
14689 }
14690
14691 static const gdb::array_view<variant_part> create_variant_parts
14692 (struct obstack *obstack,
14693 const offset_map_type &offset_map,
14694 struct field_info *fi,
14695 const std::vector<variant_part_builder> &variant_parts);
14696
14697 /* Fill in a "struct variant" for a given variant field. RESULT is
14698 the variant to fill in. OBSTACK is where any needed allocations
14699 will be done. OFFSET_MAP holds the mapping from section offsets to
14700 fields for the type. FI describes the fields of the type we're
14701 processing. FIELD is the variant field we're converting. */
14702
14703 static void
14704 create_one_variant (variant &result, struct obstack *obstack,
14705 const offset_map_type &offset_map,
14706 struct field_info *fi, const variant_field &field)
14707 {
14708 result.discriminants = convert_variant_range (obstack, field, false);
14709 result.first_field = field.first_field + fi->baseclasses.size ();
14710 result.last_field = field.last_field + fi->baseclasses.size ();
14711 result.parts = create_variant_parts (obstack, offset_map, fi,
14712 field.variant_parts);
14713 }
14714
14715 /* Fill in a "struct variant_part" for a given variant part. RESULT
14716 is the variant part to fill in. OBSTACK is where any needed
14717 allocations will be done. OFFSET_MAP holds the mapping from
14718 section offsets to fields for the type. FI describes the fields of
14719 the type we're processing. BUILDER is the variant part to be
14720 converted. */
14721
14722 static void
14723 create_one_variant_part (variant_part &result,
14724 struct obstack *obstack,
14725 const offset_map_type &offset_map,
14726 struct field_info *fi,
14727 const variant_part_builder &builder)
14728 {
14729 auto iter = offset_map.find (builder.discriminant_offset);
14730 if (iter == offset_map.end ())
14731 {
14732 result.discriminant_index = -1;
14733 /* Doesn't matter. */
14734 result.is_unsigned = false;
14735 }
14736 else
14737 {
14738 result.discriminant_index = iter->second;
14739 result.is_unsigned
14740 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
14741 }
14742
14743 size_t n = builder.variants.size ();
14744 variant *output = new (obstack) variant[n];
14745 for (size_t i = 0; i < n; ++i)
14746 create_one_variant (output[i], obstack, offset_map, fi,
14747 builder.variants[i]);
14748
14749 result.variants = gdb::array_view<variant> (output, n);
14750 }
14751
14752 /* Create a vector of variant parts that can be attached to a type.
14753 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14754 holds the mapping from section offsets to fields for the type. FI
14755 describes the fields of the type we're processing. VARIANT_PARTS
14756 is the vector to convert. */
14757
14758 static const gdb::array_view<variant_part>
14759 create_variant_parts (struct obstack *obstack,
14760 const offset_map_type &offset_map,
14761 struct field_info *fi,
14762 const std::vector<variant_part_builder> &variant_parts)
14763 {
14764 if (variant_parts.empty ())
14765 return {};
14766
14767 size_t n = variant_parts.size ();
14768 variant_part *result = new (obstack) variant_part[n];
14769 for (size_t i = 0; i < n; ++i)
14770 create_one_variant_part (result[i], obstack, offset_map, fi,
14771 variant_parts[i]);
14772
14773 return gdb::array_view<variant_part> (result, n);
14774 }
14775
14776 /* Compute the variant part vector for FIP, attaching it to TYPE when
14777 done. */
14778
14779 static void
14780 add_variant_property (struct field_info *fip, struct type *type,
14781 struct dwarf2_cu *cu)
14782 {
14783 /* Map section offsets of fields to their field index. Note the
14784 field index here does not take the number of baseclasses into
14785 account. */
14786 offset_map_type offset_map;
14787 for (int i = 0; i < fip->fields.size (); ++i)
14788 offset_map[fip->fields[i].offset] = i;
14789
14790 struct objfile *objfile = cu->per_objfile->objfile;
14791 gdb::array_view<variant_part> parts
14792 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14793 fip->variant_parts);
14794
14795 struct dynamic_prop prop;
14796 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14797 obstack_copy (&objfile->objfile_obstack, &parts,
14798 sizeof (parts)));
14799
14800 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14801 }
14802
14803 /* Create the vector of fields, and attach it to the type. */
14804
14805 static void
14806 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14807 struct dwarf2_cu *cu)
14808 {
14809 int nfields = fip->nfields ();
14810
14811 /* Record the field count, allocate space for the array of fields,
14812 and create blank accessibility bitfields if necessary. */
14813 type->set_num_fields (nfields);
14814 type->set_fields
14815 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14816
14817 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
14818 {
14819 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14820
14821 TYPE_FIELD_PRIVATE_BITS (type) =
14822 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14823 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14824
14825 TYPE_FIELD_PROTECTED_BITS (type) =
14826 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14827 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14828
14829 TYPE_FIELD_IGNORE_BITS (type) =
14830 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14831 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14832 }
14833
14834 /* If the type has baseclasses, allocate and clear a bit vector for
14835 TYPE_FIELD_VIRTUAL_BITS. */
14836 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
14837 {
14838 int num_bytes = B_BYTES (fip->baseclasses.size ());
14839 unsigned char *pointer;
14840
14841 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14842 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14843 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14844 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14845 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14846 }
14847
14848 if (!fip->variant_parts.empty ())
14849 add_variant_property (fip, type, cu);
14850
14851 /* Copy the saved-up fields into the field vector. */
14852 for (int i = 0; i < nfields; ++i)
14853 {
14854 struct nextfield &field
14855 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14856 : fip->fields[i - fip->baseclasses.size ()]);
14857
14858 type->field (i) = field.field;
14859 switch (field.accessibility)
14860 {
14861 case DW_ACCESS_private:
14862 if (cu->per_cu->lang != language_ada)
14863 SET_TYPE_FIELD_PRIVATE (type, i);
14864 break;
14865
14866 case DW_ACCESS_protected:
14867 if (cu->per_cu->lang != language_ada)
14868 SET_TYPE_FIELD_PROTECTED (type, i);
14869 break;
14870
14871 case DW_ACCESS_public:
14872 break;
14873
14874 default:
14875 /* Unknown accessibility. Complain and treat it as public. */
14876 {
14877 complaint (_("unsupported accessibility %d"),
14878 field.accessibility);
14879 }
14880 break;
14881 }
14882 if (i < fip->baseclasses.size ())
14883 {
14884 switch (field.virtuality)
14885 {
14886 case DW_VIRTUALITY_virtual:
14887 case DW_VIRTUALITY_pure_virtual:
14888 if (cu->per_cu->lang == language_ada)
14889 error (_("unexpected virtuality in component of Ada type"));
14890 SET_TYPE_FIELD_VIRTUAL (type, i);
14891 break;
14892 }
14893 }
14894 }
14895 }
14896
14897 /* Return true if this member function is a constructor, false
14898 otherwise. */
14899
14900 static int
14901 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14902 {
14903 const char *fieldname;
14904 const char *type_name;
14905 int len;
14906
14907 if (die->parent == NULL)
14908 return 0;
14909
14910 if (die->parent->tag != DW_TAG_structure_type
14911 && die->parent->tag != DW_TAG_union_type
14912 && die->parent->tag != DW_TAG_class_type)
14913 return 0;
14914
14915 fieldname = dwarf2_name (die, cu);
14916 type_name = dwarf2_name (die->parent, cu);
14917 if (fieldname == NULL || type_name == NULL)
14918 return 0;
14919
14920 len = strlen (fieldname);
14921 return (strncmp (fieldname, type_name, len) == 0
14922 && (type_name[len] == '\0' || type_name[len] == '<'));
14923 }
14924
14925 /* Add a member function to the proper fieldlist. */
14926
14927 static void
14928 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14929 struct type *type, struct dwarf2_cu *cu)
14930 {
14931 struct objfile *objfile = cu->per_objfile->objfile;
14932 struct attribute *attr;
14933 int i;
14934 struct fnfieldlist *flp = nullptr;
14935 struct fn_field *fnp;
14936 const char *fieldname;
14937 struct type *this_type;
14938
14939 if (cu->per_cu->lang == language_ada)
14940 error (_("unexpected member function in Ada type"));
14941
14942 /* Get name of member function. */
14943 fieldname = dwarf2_name (die, cu);
14944 if (fieldname == NULL)
14945 return;
14946
14947 /* Look up member function name in fieldlist. */
14948 for (i = 0; i < fip->fnfieldlists.size (); i++)
14949 {
14950 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14951 {
14952 flp = &fip->fnfieldlists[i];
14953 break;
14954 }
14955 }
14956
14957 /* Create a new fnfieldlist if necessary. */
14958 if (flp == nullptr)
14959 {
14960 fip->fnfieldlists.emplace_back ();
14961 flp = &fip->fnfieldlists.back ();
14962 flp->name = fieldname;
14963 i = fip->fnfieldlists.size () - 1;
14964 }
14965
14966 /* Create a new member function field and add it to the vector of
14967 fnfieldlists. */
14968 flp->fnfields.emplace_back ();
14969 fnp = &flp->fnfields.back ();
14970
14971 /* Delay processing of the physname until later. */
14972 if (cu->per_cu->lang == language_cplus)
14973 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14974 die, cu);
14975 else
14976 {
14977 const char *physname = dwarf2_physname (fieldname, die, cu);
14978 fnp->physname = physname ? physname : "";
14979 }
14980
14981 fnp->type = alloc_type (objfile);
14982 this_type = read_type_die (die, cu);
14983 if (this_type && this_type->code () == TYPE_CODE_FUNC)
14984 {
14985 int nparams = this_type->num_fields ();
14986
14987 /* TYPE is the domain of this method, and THIS_TYPE is the type
14988 of the method itself (TYPE_CODE_METHOD). */
14989 smash_to_method_type (fnp->type, type,
14990 TYPE_TARGET_TYPE (this_type),
14991 this_type->fields (),
14992 this_type->num_fields (),
14993 this_type->has_varargs ());
14994
14995 /* Handle static member functions.
14996 Dwarf2 has no clean way to discern C++ static and non-static
14997 member functions. G++ helps GDB by marking the first
14998 parameter for non-static member functions (which is the this
14999 pointer) as artificial. We obtain this information from
15000 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15001 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15002 fnp->voffset = VOFFSET_STATIC;
15003 }
15004 else
15005 complaint (_("member function type missing for '%s'"),
15006 dwarf2_full_name (fieldname, die, cu));
15007
15008 /* Get fcontext from DW_AT_containing_type if present. */
15009 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15010 fnp->fcontext = die_containing_type (die, cu);
15011
15012 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15013 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15014
15015 /* Get accessibility. */
15016 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15017 switch (accessibility)
15018 {
15019 case DW_ACCESS_private:
15020 fnp->is_private = 1;
15021 break;
15022 case DW_ACCESS_protected:
15023 fnp->is_protected = 1;
15024 break;
15025 }
15026
15027 /* Check for artificial methods. */
15028 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15029 if (attr && attr->as_boolean ())
15030 fnp->is_artificial = 1;
15031
15032 /* Check for defaulted methods. */
15033 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15034 if (attr != nullptr)
15035 fnp->defaulted = attr->defaulted ();
15036
15037 /* Check for deleted methods. */
15038 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15039 if (attr != nullptr && attr->as_boolean ())
15040 fnp->is_deleted = 1;
15041
15042 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15043
15044 /* Get index in virtual function table if it is a virtual member
15045 function. For older versions of GCC, this is an offset in the
15046 appropriate virtual table, as specified by DW_AT_containing_type.
15047 For everyone else, it is an expression to be evaluated relative
15048 to the object address. */
15049
15050 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15051 if (attr != nullptr)
15052 {
15053 if (attr->form_is_block () && attr->as_block ()->size > 0)
15054 {
15055 struct dwarf_block *block = attr->as_block ();
15056
15057 if (block->data[0] == DW_OP_constu)
15058 {
15059 /* Old-style GCC. */
15060 fnp->voffset = decode_locdesc (block, cu) + 2;
15061 }
15062 else if (block->data[0] == DW_OP_deref
15063 || (block->size > 1
15064 && block->data[0] == DW_OP_deref_size
15065 && block->data[1] == cu->header.addr_size))
15066 {
15067 fnp->voffset = decode_locdesc (block, cu);
15068 if ((fnp->voffset % cu->header.addr_size) != 0)
15069 dwarf2_complex_location_expr_complaint ();
15070 else
15071 fnp->voffset /= cu->header.addr_size;
15072 fnp->voffset += 2;
15073 }
15074 else
15075 dwarf2_complex_location_expr_complaint ();
15076
15077 if (!fnp->fcontext)
15078 {
15079 /* If there is no `this' field and no DW_AT_containing_type,
15080 we cannot actually find a base class context for the
15081 vtable! */
15082 if (this_type->num_fields () == 0
15083 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15084 {
15085 complaint (_("cannot determine context for virtual member "
15086 "function \"%s\" (offset %s)"),
15087 fieldname, sect_offset_str (die->sect_off));
15088 }
15089 else
15090 {
15091 fnp->fcontext
15092 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15093 }
15094 }
15095 }
15096 else if (attr->form_is_section_offset ())
15097 {
15098 dwarf2_complex_location_expr_complaint ();
15099 }
15100 else
15101 {
15102 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15103 fieldname);
15104 }
15105 }
15106 else
15107 {
15108 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15109 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15110 {
15111 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15112 complaint (_("Member function \"%s\" (offset %s) is virtual "
15113 "but the vtable offset is not specified"),
15114 fieldname, sect_offset_str (die->sect_off));
15115 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15116 TYPE_CPLUS_DYNAMIC (type) = 1;
15117 }
15118 }
15119 }
15120
15121 /* Create the vector of member function fields, and attach it to the type. */
15122
15123 static void
15124 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15125 struct dwarf2_cu *cu)
15126 {
15127 if (cu->per_cu->lang == language_ada)
15128 error (_("unexpected member functions in Ada type"));
15129
15130 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15131 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15132 TYPE_ALLOC (type,
15133 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15134
15135 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15136 {
15137 struct fnfieldlist &nf = fip->fnfieldlists[i];
15138 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15139
15140 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15141 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15142 fn_flp->fn_fields = (struct fn_field *)
15143 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15144
15145 for (int k = 0; k < nf.fnfields.size (); ++k)
15146 fn_flp->fn_fields[k] = nf.fnfields[k];
15147 }
15148
15149 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15150 }
15151
15152 /* Returns non-zero if NAME is the name of a vtable member in CU's
15153 language, zero otherwise. */
15154 static int
15155 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15156 {
15157 static const char vptr[] = "_vptr";
15158
15159 /* Look for the C++ form of the vtable. */
15160 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15161 return 1;
15162
15163 return 0;
15164 }
15165
15166 /* GCC outputs unnamed structures that are really pointers to member
15167 functions, with the ABI-specified layout. If TYPE describes
15168 such a structure, smash it into a member function type.
15169
15170 GCC shouldn't do this; it should just output pointer to member DIEs.
15171 This is GCC PR debug/28767. */
15172
15173 static void
15174 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15175 {
15176 struct type *pfn_type, *self_type, *new_type;
15177
15178 /* Check for a structure with no name and two children. */
15179 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15180 return;
15181
15182 /* Check for __pfn and __delta members. */
15183 if (TYPE_FIELD_NAME (type, 0) == NULL
15184 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15185 || TYPE_FIELD_NAME (type, 1) == NULL
15186 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15187 return;
15188
15189 /* Find the type of the method. */
15190 pfn_type = type->field (0).type ();
15191 if (pfn_type == NULL
15192 || pfn_type->code () != TYPE_CODE_PTR
15193 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15194 return;
15195
15196 /* Look for the "this" argument. */
15197 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15198 if (pfn_type->num_fields () == 0
15199 /* || pfn_type->field (0).type () == NULL */
15200 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15201 return;
15202
15203 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15204 new_type = alloc_type (objfile);
15205 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15206 pfn_type->fields (), pfn_type->num_fields (),
15207 pfn_type->has_varargs ());
15208 smash_to_methodptr_type (type, new_type);
15209 }
15210
15211 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15212 requires rewriting, then copy it and return the updated copy.
15213 Otherwise return nullptr. */
15214
15215 static struct type *
15216 rewrite_array_type (struct type *type)
15217 {
15218 if (type->code () != TYPE_CODE_ARRAY)
15219 return nullptr;
15220
15221 struct type *index_type = type->index_type ();
15222 range_bounds *current_bounds = index_type->bounds ();
15223
15224 /* Handle multi-dimensional arrays. */
15225 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15226 if (new_target == nullptr)
15227 {
15228 /* Maybe we don't need to rewrite this array. */
15229 if (current_bounds->low.kind () == PROP_CONST
15230 && current_bounds->high.kind () == PROP_CONST)
15231 return nullptr;
15232 }
15233
15234 /* Either the target type was rewritten, or the bounds have to be
15235 updated. Either way we want to copy the type and update
15236 everything. */
15237 struct type *copy = copy_type (type);
15238 int nfields = copy->num_fields ();
15239 field *new_fields
15240 = ((struct field *) TYPE_ZALLOC (copy,
15241 nfields * sizeof (struct field)));
15242 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15243 copy->set_fields (new_fields);
15244 if (new_target != nullptr)
15245 TYPE_TARGET_TYPE (copy) = new_target;
15246
15247 struct type *index_copy = copy_type (index_type);
15248 range_bounds *bounds
15249 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15250 sizeof (range_bounds));
15251 *bounds = *current_bounds;
15252 bounds->low.set_const_val (1);
15253 bounds->high.set_const_val (0);
15254 index_copy->set_bounds (bounds);
15255 copy->set_index_type (index_copy);
15256
15257 return copy;
15258 }
15259
15260 /* While some versions of GCC will generate complicated DWARF for an
15261 array (see quirk_ada_thick_pointer), more recent versions were
15262 modified to emit an explicit thick pointer structure. However, in
15263 this case, the array still has DWARF expressions for its ranges,
15264 and these must be ignored. */
15265
15266 static void
15267 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15268 struct type *type)
15269 {
15270 gdb_assert (cu->per_cu->lang == language_ada);
15271
15272 /* Check for a structure with two children. */
15273 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15274 return;
15275
15276 /* Check for P_ARRAY and P_BOUNDS members. */
15277 if (TYPE_FIELD_NAME (type, 0) == NULL
15278 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15279 || TYPE_FIELD_NAME (type, 1) == NULL
15280 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15281 return;
15282
15283 /* Make sure we're looking at a pointer to an array. */
15284 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15285 return;
15286
15287 /* The Ada code already knows how to handle these types, so all that
15288 we need to do is turn the bounds into static bounds. However, we
15289 don't want to rewrite existing array or index types in-place,
15290 because those may be referenced in other contexts where this
15291 rewriting is undesirable. */
15292 struct type *new_ary_type
15293 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15294 if (new_ary_type != nullptr)
15295 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15296 }
15297
15298 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15299 appropriate error checking and issuing complaints if there is a
15300 problem. */
15301
15302 static ULONGEST
15303 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15304 {
15305 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15306
15307 if (attr == nullptr)
15308 return 0;
15309
15310 if (!attr->form_is_constant ())
15311 {
15312 complaint (_("DW_AT_alignment must have constant form"
15313 " - DIE at %s [in module %s]"),
15314 sect_offset_str (die->sect_off),
15315 objfile_name (cu->per_objfile->objfile));
15316 return 0;
15317 }
15318
15319 LONGEST val = attr->constant_value (0);
15320 if (val < 0)
15321 {
15322 complaint (_("DW_AT_alignment value must not be negative"
15323 " - DIE at %s [in module %s]"),
15324 sect_offset_str (die->sect_off),
15325 objfile_name (cu->per_objfile->objfile));
15326 return 0;
15327 }
15328 ULONGEST align = val;
15329
15330 if (align == 0)
15331 {
15332 complaint (_("DW_AT_alignment value must not be zero"
15333 " - DIE at %s [in module %s]"),
15334 sect_offset_str (die->sect_off),
15335 objfile_name (cu->per_objfile->objfile));
15336 return 0;
15337 }
15338 if ((align & (align - 1)) != 0)
15339 {
15340 complaint (_("DW_AT_alignment value must be a power of 2"
15341 " - DIE at %s [in module %s]"),
15342 sect_offset_str (die->sect_off),
15343 objfile_name (cu->per_objfile->objfile));
15344 return 0;
15345 }
15346
15347 return align;
15348 }
15349
15350 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15351 the alignment for TYPE. */
15352
15353 static void
15354 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15355 struct type *type)
15356 {
15357 if (!set_type_align (type, get_alignment (cu, die)))
15358 complaint (_("DW_AT_alignment value too large"
15359 " - DIE at %s [in module %s]"),
15360 sect_offset_str (die->sect_off),
15361 objfile_name (cu->per_objfile->objfile));
15362 }
15363
15364 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15365 constant for a type, according to DWARF5 spec, Table 5.5. */
15366
15367 static bool
15368 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15369 {
15370 switch (value)
15371 {
15372 case DW_CC_normal:
15373 case DW_CC_pass_by_reference:
15374 case DW_CC_pass_by_value:
15375 return true;
15376
15377 default:
15378 complaint (_("unrecognized DW_AT_calling_convention value "
15379 "(%s) for a type"), pulongest (value));
15380 return false;
15381 }
15382 }
15383
15384 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15385 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15386 also according to GNU-specific values (see include/dwarf2.h). */
15387
15388 static bool
15389 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15390 {
15391 switch (value)
15392 {
15393 case DW_CC_normal:
15394 case DW_CC_program:
15395 case DW_CC_nocall:
15396 return true;
15397
15398 case DW_CC_GNU_renesas_sh:
15399 case DW_CC_GNU_borland_fastcall_i386:
15400 case DW_CC_GDB_IBM_OpenCL:
15401 return true;
15402
15403 default:
15404 complaint (_("unrecognized DW_AT_calling_convention value "
15405 "(%s) for a subroutine"), pulongest (value));
15406 return false;
15407 }
15408 }
15409
15410 /* Called when we find the DIE that starts a structure or union scope
15411 (definition) to create a type for the structure or union. Fill in
15412 the type's name and general properties; the members will not be
15413 processed until process_structure_scope. A symbol table entry for
15414 the type will also not be done until process_structure_scope (assuming
15415 the type has a name).
15416
15417 NOTE: we need to call these functions regardless of whether or not the
15418 DIE has a DW_AT_name attribute, since it might be an anonymous
15419 structure or union. This gets the type entered into our set of
15420 user defined types. */
15421
15422 static struct type *
15423 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15424 {
15425 struct objfile *objfile = cu->per_objfile->objfile;
15426 struct type *type;
15427 struct attribute *attr;
15428 const char *name;
15429
15430 /* If the definition of this type lives in .debug_types, read that type.
15431 Don't follow DW_AT_specification though, that will take us back up
15432 the chain and we want to go down. */
15433 attr = die->attr (DW_AT_signature);
15434 if (attr != nullptr)
15435 {
15436 type = get_DW_AT_signature_type (die, attr, cu);
15437
15438 /* The type's CU may not be the same as CU.
15439 Ensure TYPE is recorded with CU in die_type_hash. */
15440 return set_die_type (die, type, cu);
15441 }
15442
15443 type = alloc_type (objfile);
15444 INIT_CPLUS_SPECIFIC (type);
15445
15446 name = dwarf2_name (die, cu);
15447 if (name != NULL)
15448 {
15449 if (cu->per_cu->lang == language_cplus
15450 || cu->per_cu->lang == language_d
15451 || cu->per_cu->lang == language_rust)
15452 {
15453 const char *full_name = dwarf2_full_name (name, die, cu);
15454
15455 /* dwarf2_full_name might have already finished building the DIE's
15456 type. If so, there is no need to continue. */
15457 if (get_die_type (die, cu) != NULL)
15458 return get_die_type (die, cu);
15459
15460 type->set_name (full_name);
15461 }
15462 else
15463 {
15464 /* The name is already allocated along with this objfile, so
15465 we don't need to duplicate it for the type. */
15466 type->set_name (name);
15467 }
15468 }
15469
15470 if (die->tag == DW_TAG_structure_type)
15471 {
15472 type->set_code (TYPE_CODE_STRUCT);
15473 }
15474 else if (die->tag == DW_TAG_union_type)
15475 {
15476 type->set_code (TYPE_CODE_UNION);
15477 }
15478 else
15479 {
15480 type->set_code (TYPE_CODE_STRUCT);
15481 }
15482
15483 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
15484 type->set_is_declared_class (true);
15485
15486 /* Store the calling convention in the type if it's available in
15487 the die. Otherwise the calling convention remains set to
15488 the default value DW_CC_normal. */
15489 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15490 if (attr != nullptr
15491 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15492 {
15493 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15494 TYPE_CPLUS_CALLING_CONVENTION (type)
15495 = (enum dwarf_calling_convention) (attr->constant_value (0));
15496 }
15497
15498 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15499 if (attr != nullptr)
15500 {
15501 if (attr->form_is_constant ())
15502 TYPE_LENGTH (type) = attr->constant_value (0);
15503 else
15504 {
15505 struct dynamic_prop prop;
15506 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15507 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15508 TYPE_LENGTH (type) = 0;
15509 }
15510 }
15511 else
15512 {
15513 TYPE_LENGTH (type) = 0;
15514 }
15515
15516 maybe_set_alignment (cu, die, type);
15517
15518 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15519 {
15520 /* ICC<14 does not output the required DW_AT_declaration on
15521 incomplete types, but gives them a size of zero. */
15522 type->set_is_stub (true);
15523 }
15524 else
15525 type->set_stub_is_supported (true);
15526
15527 if (die_is_declaration (die, cu))
15528 type->set_is_stub (true);
15529 else if (attr == NULL && die->child == NULL
15530 && producer_is_realview (cu->producer))
15531 /* RealView does not output the required DW_AT_declaration
15532 on incomplete types. */
15533 type->set_is_stub (true);
15534
15535 /* We need to add the type field to the die immediately so we don't
15536 infinitely recurse when dealing with pointers to the structure
15537 type within the structure itself. */
15538 set_die_type (die, type, cu);
15539
15540 /* set_die_type should be already done. */
15541 set_descriptive_type (type, die, cu);
15542
15543 return type;
15544 }
15545
15546 static void handle_struct_member_die
15547 (struct die_info *child_die,
15548 struct type *type,
15549 struct field_info *fi,
15550 std::vector<struct symbol *> *template_args,
15551 struct dwarf2_cu *cu);
15552
15553 /* A helper for handle_struct_member_die that handles
15554 DW_TAG_variant_part. */
15555
15556 static void
15557 handle_variant_part (struct die_info *die, struct type *type,
15558 struct field_info *fi,
15559 std::vector<struct symbol *> *template_args,
15560 struct dwarf2_cu *cu)
15561 {
15562 variant_part_builder *new_part;
15563 if (fi->current_variant_part == nullptr)
15564 {
15565 fi->variant_parts.emplace_back ();
15566 new_part = &fi->variant_parts.back ();
15567 }
15568 else if (!fi->current_variant_part->processing_variant)
15569 {
15570 complaint (_("nested DW_TAG_variant_part seen "
15571 "- DIE at %s [in module %s]"),
15572 sect_offset_str (die->sect_off),
15573 objfile_name (cu->per_objfile->objfile));
15574 return;
15575 }
15576 else
15577 {
15578 variant_field &current = fi->current_variant_part->variants.back ();
15579 current.variant_parts.emplace_back ();
15580 new_part = &current.variant_parts.back ();
15581 }
15582
15583 /* When we recurse, we want callees to add to this new variant
15584 part. */
15585 scoped_restore save_current_variant_part
15586 = make_scoped_restore (&fi->current_variant_part, new_part);
15587
15588 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15589 if (discr == NULL)
15590 {
15591 /* It's a univariant form, an extension we support. */
15592 }
15593 else if (discr->form_is_ref ())
15594 {
15595 struct dwarf2_cu *target_cu = cu;
15596 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15597
15598 new_part->discriminant_offset = target_die->sect_off;
15599 }
15600 else
15601 {
15602 complaint (_("DW_AT_discr does not have DIE reference form"
15603 " - DIE at %s [in module %s]"),
15604 sect_offset_str (die->sect_off),
15605 objfile_name (cu->per_objfile->objfile));
15606 }
15607
15608 for (die_info *child_die = die->child;
15609 child_die != NULL;
15610 child_die = child_die->sibling)
15611 handle_struct_member_die (child_die, type, fi, template_args, cu);
15612 }
15613
15614 /* A helper for handle_struct_member_die that handles
15615 DW_TAG_variant. */
15616
15617 static void
15618 handle_variant (struct die_info *die, struct type *type,
15619 struct field_info *fi,
15620 std::vector<struct symbol *> *template_args,
15621 struct dwarf2_cu *cu)
15622 {
15623 if (fi->current_variant_part == nullptr)
15624 {
15625 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15626 "- DIE at %s [in module %s]"),
15627 sect_offset_str (die->sect_off),
15628 objfile_name (cu->per_objfile->objfile));
15629 return;
15630 }
15631 if (fi->current_variant_part->processing_variant)
15632 {
15633 complaint (_("nested DW_TAG_variant seen "
15634 "- DIE at %s [in module %s]"),
15635 sect_offset_str (die->sect_off),
15636 objfile_name (cu->per_objfile->objfile));
15637 return;
15638 }
15639
15640 scoped_restore save_processing_variant
15641 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15642 true);
15643
15644 fi->current_variant_part->variants.emplace_back ();
15645 variant_field &variant = fi->current_variant_part->variants.back ();
15646 variant.first_field = fi->fields.size ();
15647
15648 /* In a variant we want to get the discriminant and also add a
15649 field for our sole member child. */
15650 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15651 if (discr == nullptr || !discr->form_is_constant ())
15652 {
15653 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15654 if (discr == nullptr || discr->as_block ()->size == 0)
15655 variant.default_branch = true;
15656 else
15657 variant.discr_list_data = discr->as_block ();
15658 }
15659 else
15660 variant.discriminant_value = discr->constant_value (0);
15661
15662 for (die_info *variant_child = die->child;
15663 variant_child != NULL;
15664 variant_child = variant_child->sibling)
15665 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15666
15667 variant.last_field = fi->fields.size ();
15668 }
15669
15670 /* A helper for process_structure_scope that handles a single member
15671 DIE. */
15672
15673 static void
15674 handle_struct_member_die (struct die_info *child_die, struct type *type,
15675 struct field_info *fi,
15676 std::vector<struct symbol *> *template_args,
15677 struct dwarf2_cu *cu)
15678 {
15679 if (child_die->tag == DW_TAG_member
15680 || child_die->tag == DW_TAG_variable)
15681 {
15682 /* NOTE: carlton/2002-11-05: A C++ static data member
15683 should be a DW_TAG_member that is a declaration, but
15684 all versions of G++ as of this writing (so through at
15685 least 3.2.1) incorrectly generate DW_TAG_variable
15686 tags for them instead. */
15687 dwarf2_add_field (fi, child_die, cu);
15688 }
15689 else if (child_die->tag == DW_TAG_subprogram)
15690 {
15691 /* Rust doesn't have member functions in the C++ sense.
15692 However, it does emit ordinary functions as children
15693 of a struct DIE. */
15694 if (cu->per_cu->lang == language_rust)
15695 read_func_scope (child_die, cu);
15696 else
15697 {
15698 /* C++ member function. */
15699 dwarf2_add_member_fn (fi, child_die, type, cu);
15700 }
15701 }
15702 else if (child_die->tag == DW_TAG_inheritance)
15703 {
15704 /* C++ base class field. */
15705 dwarf2_add_field (fi, child_die, cu);
15706 }
15707 else if (type_can_define_types (child_die))
15708 dwarf2_add_type_defn (fi, child_die, cu);
15709 else if (child_die->tag == DW_TAG_template_type_param
15710 || child_die->tag == DW_TAG_template_value_param)
15711 {
15712 struct symbol *arg = new_symbol (child_die, NULL, cu);
15713
15714 if (arg != NULL)
15715 template_args->push_back (arg);
15716 }
15717 else if (child_die->tag == DW_TAG_variant_part)
15718 handle_variant_part (child_die, type, fi, template_args, cu);
15719 else if (child_die->tag == DW_TAG_variant)
15720 handle_variant (child_die, type, fi, template_args, cu);
15721 }
15722
15723 /* Finish creating a structure or union type, including filling in
15724 its members and creating a symbol for it. */
15725
15726 static void
15727 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15728 {
15729 struct objfile *objfile = cu->per_objfile->objfile;
15730 struct die_info *child_die;
15731 struct type *type;
15732
15733 type = get_die_type (die, cu);
15734 if (type == NULL)
15735 type = read_structure_type (die, cu);
15736
15737 bool has_template_parameters = false;
15738 if (die->child != NULL && ! die_is_declaration (die, cu))
15739 {
15740 struct field_info fi;
15741 std::vector<struct symbol *> template_args;
15742
15743 child_die = die->child;
15744
15745 while (child_die && child_die->tag)
15746 {
15747 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15748 child_die = child_die->sibling;
15749 }
15750
15751 /* Attach template arguments to type. */
15752 if (!template_args.empty ())
15753 {
15754 has_template_parameters = true;
15755 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15756 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15757 TYPE_TEMPLATE_ARGUMENTS (type)
15758 = XOBNEWVEC (&objfile->objfile_obstack,
15759 struct symbol *,
15760 TYPE_N_TEMPLATE_ARGUMENTS (type));
15761 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15762 template_args.data (),
15763 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15764 * sizeof (struct symbol *)));
15765 }
15766
15767 /* Attach fields and member functions to the type. */
15768 if (fi.nfields () > 0)
15769 dwarf2_attach_fields_to_type (&fi, type, cu);
15770 if (!fi.fnfieldlists.empty ())
15771 {
15772 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15773
15774 /* Get the type which refers to the base class (possibly this
15775 class itself) which contains the vtable pointer for the current
15776 class from the DW_AT_containing_type attribute. This use of
15777 DW_AT_containing_type is a GNU extension. */
15778
15779 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15780 {
15781 struct type *t = die_containing_type (die, cu);
15782
15783 set_type_vptr_basetype (type, t);
15784 if (type == t)
15785 {
15786 int i;
15787
15788 /* Our own class provides vtbl ptr. */
15789 for (i = t->num_fields () - 1;
15790 i >= TYPE_N_BASECLASSES (t);
15791 --i)
15792 {
15793 const char *fieldname = TYPE_FIELD_NAME (t, i);
15794
15795 if (is_vtable_name (fieldname, cu))
15796 {
15797 set_type_vptr_fieldno (type, i);
15798 break;
15799 }
15800 }
15801
15802 /* Complain if virtual function table field not found. */
15803 if (i < TYPE_N_BASECLASSES (t))
15804 complaint (_("virtual function table pointer "
15805 "not found when defining class '%s'"),
15806 type->name () ? type->name () : "");
15807 }
15808 else
15809 {
15810 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15811 }
15812 }
15813 else if (cu->producer
15814 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15815 {
15816 /* The IBM XLC compiler does not provide direct indication
15817 of the containing type, but the vtable pointer is
15818 always named __vfp. */
15819
15820 int i;
15821
15822 for (i = type->num_fields () - 1;
15823 i >= TYPE_N_BASECLASSES (type);
15824 --i)
15825 {
15826 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15827 {
15828 set_type_vptr_fieldno (type, i);
15829 set_type_vptr_basetype (type, type);
15830 break;
15831 }
15832 }
15833 }
15834 }
15835
15836 /* Copy fi.typedef_field_list linked list elements content into the
15837 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15838 if (!fi.typedef_field_list.empty ())
15839 {
15840 int count = fi.typedef_field_list.size ();
15841
15842 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15843 TYPE_TYPEDEF_FIELD_ARRAY (type)
15844 = ((struct decl_field *)
15845 TYPE_ALLOC (type,
15846 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15847 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15848
15849 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15850 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15851 }
15852
15853 /* Copy fi.nested_types_list linked list elements content into the
15854 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15855 if (!fi.nested_types_list.empty ()
15856 && cu->per_cu->lang != language_ada)
15857 {
15858 int count = fi.nested_types_list.size ();
15859
15860 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15861 TYPE_NESTED_TYPES_ARRAY (type)
15862 = ((struct decl_field *)
15863 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15864 TYPE_NESTED_TYPES_COUNT (type) = count;
15865
15866 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15867 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15868 }
15869 }
15870
15871 quirk_gcc_member_function_pointer (type, objfile);
15872 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
15873 cu->rust_unions.push_back (type);
15874 else if (cu->per_cu->lang == language_ada)
15875 quirk_ada_thick_pointer_struct (die, cu, type);
15876
15877 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15878 snapshots) has been known to create a die giving a declaration
15879 for a class that has, as a child, a die giving a definition for a
15880 nested class. So we have to process our children even if the
15881 current die is a declaration. Normally, of course, a declaration
15882 won't have any children at all. */
15883
15884 child_die = die->child;
15885
15886 while (child_die != NULL && child_die->tag)
15887 {
15888 if (child_die->tag == DW_TAG_member
15889 || child_die->tag == DW_TAG_variable
15890 || child_die->tag == DW_TAG_inheritance
15891 || child_die->tag == DW_TAG_template_value_param
15892 || child_die->tag == DW_TAG_template_type_param)
15893 {
15894 /* Do nothing. */
15895 }
15896 else
15897 process_die (child_die, cu);
15898
15899 child_die = child_die->sibling;
15900 }
15901
15902 /* Do not consider external references. According to the DWARF standard,
15903 these DIEs are identified by the fact that they have no byte_size
15904 attribute, and a declaration attribute. */
15905 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15906 || !die_is_declaration (die, cu)
15907 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15908 {
15909 struct symbol *sym = new_symbol (die, type, cu);
15910
15911 if (has_template_parameters)
15912 {
15913 struct symtab *symtab;
15914 if (sym != nullptr)
15915 symtab = symbol_symtab (sym);
15916 else if (cu->line_header != nullptr)
15917 {
15918 /* Any related symtab will do. */
15919 symtab
15920 = cu->line_header->file_names ()[0].symtab;
15921 }
15922 else
15923 {
15924 symtab = nullptr;
15925 complaint (_("could not find suitable "
15926 "symtab for template parameter"
15927 " - DIE at %s [in module %s]"),
15928 sect_offset_str (die->sect_off),
15929 objfile_name (objfile));
15930 }
15931
15932 if (symtab != nullptr)
15933 {
15934 /* Make sure that the symtab is set on the new symbols.
15935 Even though they don't appear in this symtab directly,
15936 other parts of gdb assume that symbols do, and this is
15937 reasonably true. */
15938 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15939 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15940 }
15941 }
15942 }
15943 }
15944
15945 /* Assuming DIE is an enumeration type, and TYPE is its associated
15946 type, update TYPE using some information only available in DIE's
15947 children. In particular, the fields are computed. */
15948
15949 static void
15950 update_enumeration_type_from_children (struct die_info *die,
15951 struct type *type,
15952 struct dwarf2_cu *cu)
15953 {
15954 struct die_info *child_die;
15955 int unsigned_enum = 1;
15956 int flag_enum = 1;
15957
15958 auto_obstack obstack;
15959 std::vector<struct field> fields;
15960
15961 for (child_die = die->child;
15962 child_die != NULL && child_die->tag;
15963 child_die = child_die->sibling)
15964 {
15965 struct attribute *attr;
15966 LONGEST value;
15967 const gdb_byte *bytes;
15968 struct dwarf2_locexpr_baton *baton;
15969 const char *name;
15970
15971 if (child_die->tag != DW_TAG_enumerator)
15972 continue;
15973
15974 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15975 if (attr == NULL)
15976 continue;
15977
15978 name = dwarf2_name (child_die, cu);
15979 if (name == NULL)
15980 name = "<anonymous enumerator>";
15981
15982 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15983 &value, &bytes, &baton);
15984 if (value < 0)
15985 {
15986 unsigned_enum = 0;
15987 flag_enum = 0;
15988 }
15989 else
15990 {
15991 if (count_one_bits_ll (value) >= 2)
15992 flag_enum = 0;
15993 }
15994
15995 fields.emplace_back ();
15996 struct field &field = fields.back ();
15997 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15998 SET_FIELD_ENUMVAL (field, value);
15999 }
16000
16001 if (!fields.empty ())
16002 {
16003 type->set_num_fields (fields.size ());
16004 type->set_fields
16005 ((struct field *)
16006 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16007 memcpy (type->fields (), fields.data (),
16008 sizeof (struct field) * fields.size ());
16009 }
16010
16011 if (unsigned_enum)
16012 type->set_is_unsigned (true);
16013
16014 if (flag_enum)
16015 type->set_is_flag_enum (true);
16016 }
16017
16018 /* Given a DW_AT_enumeration_type die, set its type. We do not
16019 complete the type's fields yet, or create any symbols. */
16020
16021 static struct type *
16022 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16023 {
16024 struct objfile *objfile = cu->per_objfile->objfile;
16025 struct type *type;
16026 struct attribute *attr;
16027 const char *name;
16028
16029 /* If the definition of this type lives in .debug_types, read that type.
16030 Don't follow DW_AT_specification though, that will take us back up
16031 the chain and we want to go down. */
16032 attr = die->attr (DW_AT_signature);
16033 if (attr != nullptr)
16034 {
16035 type = get_DW_AT_signature_type (die, attr, cu);
16036
16037 /* The type's CU may not be the same as CU.
16038 Ensure TYPE is recorded with CU in die_type_hash. */
16039 return set_die_type (die, type, cu);
16040 }
16041
16042 type = alloc_type (objfile);
16043
16044 type->set_code (TYPE_CODE_ENUM);
16045 name = dwarf2_full_name (NULL, die, cu);
16046 if (name != NULL)
16047 type->set_name (name);
16048
16049 attr = dwarf2_attr (die, DW_AT_type, cu);
16050 if (attr != NULL)
16051 {
16052 struct type *underlying_type = die_type (die, cu);
16053
16054 TYPE_TARGET_TYPE (type) = underlying_type;
16055 }
16056
16057 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16058 if (attr != nullptr)
16059 {
16060 TYPE_LENGTH (type) = attr->constant_value (0);
16061 }
16062 else
16063 {
16064 TYPE_LENGTH (type) = 0;
16065 }
16066
16067 maybe_set_alignment (cu, die, type);
16068
16069 /* The enumeration DIE can be incomplete. In Ada, any type can be
16070 declared as private in the package spec, and then defined only
16071 inside the package body. Such types are known as Taft Amendment
16072 Types. When another package uses such a type, an incomplete DIE
16073 may be generated by the compiler. */
16074 if (die_is_declaration (die, cu))
16075 type->set_is_stub (true);
16076
16077 /* If this type has an underlying type that is not a stub, then we
16078 may use its attributes. We always use the "unsigned" attribute
16079 in this situation, because ordinarily we guess whether the type
16080 is unsigned -- but the guess can be wrong and the underlying type
16081 can tell us the reality. However, we defer to a local size
16082 attribute if one exists, because this lets the compiler override
16083 the underlying type if needed. */
16084 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16085 {
16086 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16087 underlying_type = check_typedef (underlying_type);
16088
16089 type->set_is_unsigned (underlying_type->is_unsigned ());
16090
16091 if (TYPE_LENGTH (type) == 0)
16092 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16093
16094 if (TYPE_RAW_ALIGN (type) == 0
16095 && TYPE_RAW_ALIGN (underlying_type) != 0)
16096 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16097 }
16098
16099 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16100
16101 set_die_type (die, type, cu);
16102
16103 /* Finish the creation of this type by using the enum's children.
16104 Note that, as usual, this must come after set_die_type to avoid
16105 infinite recursion when trying to compute the names of the
16106 enumerators. */
16107 update_enumeration_type_from_children (die, type, cu);
16108
16109 return type;
16110 }
16111
16112 /* Given a pointer to a die which begins an enumeration, process all
16113 the dies that define the members of the enumeration, and create the
16114 symbol for the enumeration type.
16115
16116 NOTE: We reverse the order of the element list. */
16117
16118 static void
16119 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16120 {
16121 struct type *this_type;
16122
16123 this_type = get_die_type (die, cu);
16124 if (this_type == NULL)
16125 this_type = read_enumeration_type (die, cu);
16126
16127 if (die->child != NULL)
16128 {
16129 struct die_info *child_die;
16130 const char *name;
16131
16132 child_die = die->child;
16133 while (child_die && child_die->tag)
16134 {
16135 if (child_die->tag != DW_TAG_enumerator)
16136 {
16137 process_die (child_die, cu);
16138 }
16139 else
16140 {
16141 name = dwarf2_name (child_die, cu);
16142 if (name)
16143 new_symbol (child_die, this_type, cu);
16144 }
16145
16146 child_die = child_die->sibling;
16147 }
16148 }
16149
16150 /* If we are reading an enum from a .debug_types unit, and the enum
16151 is a declaration, and the enum is not the signatured type in the
16152 unit, then we do not want to add a symbol for it. Adding a
16153 symbol would in some cases obscure the true definition of the
16154 enum, giving users an incomplete type when the definition is
16155 actually available. Note that we do not want to do this for all
16156 enums which are just declarations, because C++0x allows forward
16157 enum declarations. */
16158 if (cu->per_cu->is_debug_types
16159 && die_is_declaration (die, cu))
16160 {
16161 struct signatured_type *sig_type;
16162
16163 sig_type = (struct signatured_type *) cu->per_cu;
16164 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16165 if (sig_type->type_offset_in_section != die->sect_off)
16166 return;
16167 }
16168
16169 new_symbol (die, this_type, cu);
16170 }
16171
16172 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16173 expression for an index type and finds the corresponding field
16174 offset in the hidden "P_BOUNDS" structure. Returns true on success
16175 and updates *FIELD, false if it fails to recognize an
16176 expression. */
16177
16178 static bool
16179 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16180 int *bounds_offset, struct field *field,
16181 struct dwarf2_cu *cu)
16182 {
16183 struct attribute *attr = dwarf2_attr (die, name, cu);
16184 if (attr == nullptr || !attr->form_is_block ())
16185 return false;
16186
16187 const struct dwarf_block *block = attr->as_block ();
16188 const gdb_byte *start = block->data;
16189 const gdb_byte *end = block->data + block->size;
16190
16191 /* The expression to recognize generally looks like:
16192
16193 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16194 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16195
16196 However, the second "plus_uconst" may be missing:
16197
16198 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16199 DW_OP_deref_size: 4)
16200
16201 This happens when the field is at the start of the structure.
16202
16203 Also, the final deref may not be sized:
16204
16205 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16206 DW_OP_deref)
16207
16208 This happens when the size of the index type happens to be the
16209 same as the architecture's word size. This can occur with or
16210 without the second plus_uconst. */
16211
16212 if (end - start < 2)
16213 return false;
16214 if (*start++ != DW_OP_push_object_address)
16215 return false;
16216 if (*start++ != DW_OP_plus_uconst)
16217 return false;
16218
16219 uint64_t this_bound_off;
16220 start = gdb_read_uleb128 (start, end, &this_bound_off);
16221 if (start == nullptr || (int) this_bound_off != this_bound_off)
16222 return false;
16223 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16224 is consistent among all bounds. */
16225 if (*bounds_offset == -1)
16226 *bounds_offset = this_bound_off;
16227 else if (*bounds_offset != this_bound_off)
16228 return false;
16229
16230 if (start == end || *start++ != DW_OP_deref)
16231 return false;
16232
16233 int offset = 0;
16234 if (start ==end)
16235 return false;
16236 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16237 {
16238 /* This means an offset of 0. */
16239 }
16240 else if (*start++ != DW_OP_plus_uconst)
16241 return false;
16242 else
16243 {
16244 /* The size is the parameter to DW_OP_plus_uconst. */
16245 uint64_t val;
16246 start = gdb_read_uleb128 (start, end, &val);
16247 if (start == nullptr)
16248 return false;
16249 if ((int) val != val)
16250 return false;
16251 offset = val;
16252 }
16253
16254 if (start == end)
16255 return false;
16256
16257 uint64_t size;
16258 if (*start == DW_OP_deref_size)
16259 {
16260 start = gdb_read_uleb128 (start + 1, end, &size);
16261 if (start == nullptr)
16262 return false;
16263 }
16264 else if (*start == DW_OP_deref)
16265 {
16266 size = cu->header.addr_size;
16267 ++start;
16268 }
16269 else
16270 return false;
16271
16272 SET_FIELD_BITPOS (*field, 8 * offset);
16273 if (size != TYPE_LENGTH (field->type ()))
16274 FIELD_BITSIZE (*field) = 8 * size;
16275
16276 return true;
16277 }
16278
16279 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16280 some kinds of Ada arrays:
16281
16282 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16283 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16284 <11e0> DW_AT_data_location: 2 byte block: 97 6
16285 (DW_OP_push_object_address; DW_OP_deref)
16286 <11e3> DW_AT_type : <0x1173>
16287 <11e7> DW_AT_sibling : <0x1201>
16288 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16289 <11ec> DW_AT_type : <0x1206>
16290 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16291 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16292 DW_OP_deref_size: 4)
16293 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16294 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16295 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16296
16297 This actually represents a "thick pointer", which is a structure
16298 with two elements: one that is a pointer to the array data, and one
16299 that is a pointer to another structure; this second structure holds
16300 the array bounds.
16301
16302 This returns a new type on success, or nullptr if this didn't
16303 recognize the type. */
16304
16305 static struct type *
16306 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16307 struct type *type)
16308 {
16309 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16310 /* So far we've only seen this with block form. */
16311 if (attr == nullptr || !attr->form_is_block ())
16312 return nullptr;
16313
16314 /* Note that this will fail if the structure layout is changed by
16315 the compiler. However, we have no good way to recognize some
16316 other layout, because we don't know what expression the compiler
16317 might choose to emit should this happen. */
16318 struct dwarf_block *blk = attr->as_block ();
16319 if (blk->size != 2
16320 || blk->data[0] != DW_OP_push_object_address
16321 || blk->data[1] != DW_OP_deref)
16322 return nullptr;
16323
16324 int bounds_offset = -1;
16325 int max_align = -1;
16326 std::vector<struct field> range_fields;
16327 for (struct die_info *child_die = die->child;
16328 child_die;
16329 child_die = child_die->sibling)
16330 {
16331 if (child_die->tag == DW_TAG_subrange_type)
16332 {
16333 struct type *underlying = read_subrange_index_type (child_die, cu);
16334
16335 int this_align = type_align (underlying);
16336 if (this_align > max_align)
16337 max_align = this_align;
16338
16339 range_fields.emplace_back ();
16340 range_fields.emplace_back ();
16341
16342 struct field &lower = range_fields[range_fields.size () - 2];
16343 struct field &upper = range_fields[range_fields.size () - 1];
16344
16345 lower.set_type (underlying);
16346 FIELD_ARTIFICIAL (lower) = 1;
16347
16348 upper.set_type (underlying);
16349 FIELD_ARTIFICIAL (upper) = 1;
16350
16351 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16352 &bounds_offset, &lower, cu)
16353 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16354 &bounds_offset, &upper, cu))
16355 return nullptr;
16356 }
16357 }
16358
16359 /* This shouldn't really happen, but double-check that we found
16360 where the bounds are stored. */
16361 if (bounds_offset == -1)
16362 return nullptr;
16363
16364 struct objfile *objfile = cu->per_objfile->objfile;
16365 for (int i = 0; i < range_fields.size (); i += 2)
16366 {
16367 char name[20];
16368
16369 /* Set the name of each field in the bounds. */
16370 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16371 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16372 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16373 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16374 }
16375
16376 struct type *bounds = alloc_type (objfile);
16377 bounds->set_code (TYPE_CODE_STRUCT);
16378
16379 bounds->set_num_fields (range_fields.size ());
16380 bounds->set_fields
16381 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16382 * sizeof (struct field))));
16383 memcpy (bounds->fields (), range_fields.data (),
16384 bounds->num_fields () * sizeof (struct field));
16385
16386 int last_fieldno = range_fields.size () - 1;
16387 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16388 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16389 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16390
16391 /* Rewrite the existing array type in place. Specifically, we
16392 remove any dynamic properties we might have read, and we replace
16393 the index types. */
16394 struct type *iter = type;
16395 for (int i = 0; i < range_fields.size (); i += 2)
16396 {
16397 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16398 iter->main_type->dyn_prop_list = nullptr;
16399 iter->set_index_type
16400 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16401 iter = TYPE_TARGET_TYPE (iter);
16402 }
16403
16404 struct type *result = alloc_type (objfile);
16405 result->set_code (TYPE_CODE_STRUCT);
16406
16407 result->set_num_fields (2);
16408 result->set_fields
16409 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16410 * sizeof (struct field))));
16411
16412 /* The names are chosen to coincide with what the compiler does with
16413 -fgnat-encodings=all, which the Ada code in gdb already
16414 understands. */
16415 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16416 result->field (0).set_type (lookup_pointer_type (type));
16417
16418 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16419 result->field (1).set_type (lookup_pointer_type (bounds));
16420 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16421
16422 result->set_name (type->name ());
16423 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16424 + TYPE_LENGTH (result->field (1).type ()));
16425
16426 return result;
16427 }
16428
16429 /* Extract all information from a DW_TAG_array_type DIE and put it in
16430 the DIE's type field. For now, this only handles one dimensional
16431 arrays. */
16432
16433 static struct type *
16434 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16435 {
16436 struct objfile *objfile = cu->per_objfile->objfile;
16437 struct die_info *child_die;
16438 struct type *type;
16439 struct type *element_type, *range_type, *index_type;
16440 struct attribute *attr;
16441 const char *name;
16442 struct dynamic_prop *byte_stride_prop = NULL;
16443 unsigned int bit_stride = 0;
16444
16445 element_type = die_type (die, cu);
16446
16447 /* The die_type call above may have already set the type for this DIE. */
16448 type = get_die_type (die, cu);
16449 if (type)
16450 return type;
16451
16452 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16453 if (attr != NULL)
16454 {
16455 int stride_ok;
16456 struct type *prop_type = cu->addr_sized_int_type (false);
16457
16458 byte_stride_prop
16459 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16460 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16461 prop_type);
16462 if (!stride_ok)
16463 {
16464 complaint (_("unable to read array DW_AT_byte_stride "
16465 " - DIE at %s [in module %s]"),
16466 sect_offset_str (die->sect_off),
16467 objfile_name (cu->per_objfile->objfile));
16468 /* Ignore this attribute. We will likely not be able to print
16469 arrays of this type correctly, but there is little we can do
16470 to help if we cannot read the attribute's value. */
16471 byte_stride_prop = NULL;
16472 }
16473 }
16474
16475 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16476 if (attr != NULL)
16477 bit_stride = attr->constant_value (0);
16478
16479 /* Irix 6.2 native cc creates array types without children for
16480 arrays with unspecified length. */
16481 if (die->child == NULL)
16482 {
16483 index_type = objfile_type (objfile)->builtin_int;
16484 range_type = create_static_range_type (NULL, index_type, 0, -1);
16485 type = create_array_type_with_stride (NULL, element_type, range_type,
16486 byte_stride_prop, bit_stride);
16487 return set_die_type (die, type, cu);
16488 }
16489
16490 std::vector<struct type *> range_types;
16491 child_die = die->child;
16492 while (child_die && child_die->tag)
16493 {
16494 if (child_die->tag == DW_TAG_subrange_type)
16495 {
16496 struct type *child_type = read_type_die (child_die, cu);
16497
16498 if (child_type != NULL)
16499 {
16500 /* The range type was succesfully read. Save it for the
16501 array type creation. */
16502 range_types.push_back (child_type);
16503 }
16504 }
16505 child_die = child_die->sibling;
16506 }
16507
16508 if (range_types.empty ())
16509 {
16510 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16511 sect_offset_str (die->sect_off),
16512 objfile_name (cu->per_objfile->objfile));
16513 return NULL;
16514 }
16515
16516 /* Dwarf2 dimensions are output from left to right, create the
16517 necessary array types in backwards order. */
16518
16519 type = element_type;
16520
16521 if (read_array_order (die, cu) == DW_ORD_col_major)
16522 {
16523 int i = 0;
16524
16525 while (i < range_types.size ())
16526 {
16527 type = create_array_type_with_stride (NULL, type, range_types[i++],
16528 byte_stride_prop, bit_stride);
16529 bit_stride = 0;
16530 byte_stride_prop = nullptr;
16531 }
16532 }
16533 else
16534 {
16535 size_t ndim = range_types.size ();
16536 while (ndim-- > 0)
16537 {
16538 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16539 byte_stride_prop, bit_stride);
16540 bit_stride = 0;
16541 byte_stride_prop = nullptr;
16542 }
16543 }
16544
16545 gdb_assert (type != element_type);
16546
16547 /* Understand Dwarf2 support for vector types (like they occur on
16548 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16549 array type. This is not part of the Dwarf2/3 standard yet, but a
16550 custom vendor extension. The main difference between a regular
16551 array and the vector variant is that vectors are passed by value
16552 to functions. */
16553 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16554 if (attr != nullptr)
16555 make_vector_type (type);
16556
16557 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16558 implementation may choose to implement triple vectors using this
16559 attribute. */
16560 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16561 if (attr != nullptr && attr->form_is_unsigned ())
16562 {
16563 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16564 TYPE_LENGTH (type) = attr->as_unsigned ();
16565 else
16566 complaint (_("DW_AT_byte_size for array type smaller "
16567 "than the total size of elements"));
16568 }
16569
16570 name = dwarf2_name (die, cu);
16571 if (name)
16572 type->set_name (name);
16573
16574 maybe_set_alignment (cu, die, type);
16575
16576 struct type *replacement_type = nullptr;
16577 if (cu->per_cu->lang == language_ada)
16578 {
16579 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16580 if (replacement_type != nullptr)
16581 type = replacement_type;
16582 }
16583
16584 /* Install the type in the die. */
16585 set_die_type (die, type, cu, replacement_type != nullptr);
16586
16587 /* set_die_type should be already done. */
16588 set_descriptive_type (type, die, cu);
16589
16590 return type;
16591 }
16592
16593 static enum dwarf_array_dim_ordering
16594 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16595 {
16596 struct attribute *attr;
16597
16598 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16599
16600 if (attr != nullptr)
16601 {
16602 LONGEST val = attr->constant_value (-1);
16603 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16604 return (enum dwarf_array_dim_ordering) val;
16605 }
16606
16607 /* GNU F77 is a special case, as at 08/2004 array type info is the
16608 opposite order to the dwarf2 specification, but data is still
16609 laid out as per normal fortran.
16610
16611 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16612 version checking. */
16613
16614 if (cu->per_cu->lang == language_fortran
16615 && cu->producer && strstr (cu->producer, "GNU F77"))
16616 {
16617 return DW_ORD_row_major;
16618 }
16619
16620 switch (cu->language_defn->array_ordering ())
16621 {
16622 case array_column_major:
16623 return DW_ORD_col_major;
16624 case array_row_major:
16625 default:
16626 return DW_ORD_row_major;
16627 };
16628 }
16629
16630 /* Extract all information from a DW_TAG_set_type DIE and put it in
16631 the DIE's type field. */
16632
16633 static struct type *
16634 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16635 {
16636 struct type *domain_type, *set_type;
16637 struct attribute *attr;
16638
16639 domain_type = die_type (die, cu);
16640
16641 /* The die_type call above may have already set the type for this DIE. */
16642 set_type = get_die_type (die, cu);
16643 if (set_type)
16644 return set_type;
16645
16646 set_type = create_set_type (NULL, domain_type);
16647
16648 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16649 if (attr != nullptr && attr->form_is_unsigned ())
16650 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16651
16652 maybe_set_alignment (cu, die, set_type);
16653
16654 return set_die_type (die, set_type, cu);
16655 }
16656
16657 /* A helper for read_common_block that creates a locexpr baton.
16658 SYM is the symbol which we are marking as computed.
16659 COMMON_DIE is the DIE for the common block.
16660 COMMON_LOC is the location expression attribute for the common
16661 block itself.
16662 MEMBER_LOC is the location expression attribute for the particular
16663 member of the common block that we are processing.
16664 CU is the CU from which the above come. */
16665
16666 static void
16667 mark_common_block_symbol_computed (struct symbol *sym,
16668 struct die_info *common_die,
16669 struct attribute *common_loc,
16670 struct attribute *member_loc,
16671 struct dwarf2_cu *cu)
16672 {
16673 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16674 struct objfile *objfile = per_objfile->objfile;
16675 struct dwarf2_locexpr_baton *baton;
16676 gdb_byte *ptr;
16677 unsigned int cu_off;
16678 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16679 LONGEST offset = 0;
16680
16681 gdb_assert (common_loc && member_loc);
16682 gdb_assert (common_loc->form_is_block ());
16683 gdb_assert (member_loc->form_is_block ()
16684 || member_loc->form_is_constant ());
16685
16686 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16687 baton->per_objfile = per_objfile;
16688 baton->per_cu = cu->per_cu;
16689 gdb_assert (baton->per_cu);
16690
16691 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16692
16693 if (member_loc->form_is_constant ())
16694 {
16695 offset = member_loc->constant_value (0);
16696 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16697 }
16698 else
16699 baton->size += member_loc->as_block ()->size;
16700
16701 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16702 baton->data = ptr;
16703
16704 *ptr++ = DW_OP_call4;
16705 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16706 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16707 ptr += 4;
16708
16709 if (member_loc->form_is_constant ())
16710 {
16711 *ptr++ = DW_OP_addr;
16712 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16713 ptr += cu->header.addr_size;
16714 }
16715 else
16716 {
16717 /* We have to copy the data here, because DW_OP_call4 will only
16718 use a DW_AT_location attribute. */
16719 struct dwarf_block *block = member_loc->as_block ();
16720 memcpy (ptr, block->data, block->size);
16721 ptr += block->size;
16722 }
16723
16724 *ptr++ = DW_OP_plus;
16725 gdb_assert (ptr - baton->data == baton->size);
16726
16727 SYMBOL_LOCATION_BATON (sym) = baton;
16728 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16729 }
16730
16731 /* Create appropriate locally-scoped variables for all the
16732 DW_TAG_common_block entries. Also create a struct common_block
16733 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16734 is used to separate the common blocks name namespace from regular
16735 variable names. */
16736
16737 static void
16738 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16739 {
16740 struct attribute *attr;
16741
16742 attr = dwarf2_attr (die, DW_AT_location, cu);
16743 if (attr != nullptr)
16744 {
16745 /* Support the .debug_loc offsets. */
16746 if (attr->form_is_block ())
16747 {
16748 /* Ok. */
16749 }
16750 else if (attr->form_is_section_offset ())
16751 {
16752 dwarf2_complex_location_expr_complaint ();
16753 attr = NULL;
16754 }
16755 else
16756 {
16757 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16758 "common block member");
16759 attr = NULL;
16760 }
16761 }
16762
16763 if (die->child != NULL)
16764 {
16765 struct objfile *objfile = cu->per_objfile->objfile;
16766 struct die_info *child_die;
16767 size_t n_entries = 0, size;
16768 struct common_block *common_block;
16769 struct symbol *sym;
16770
16771 for (child_die = die->child;
16772 child_die && child_die->tag;
16773 child_die = child_die->sibling)
16774 ++n_entries;
16775
16776 size = (sizeof (struct common_block)
16777 + (n_entries - 1) * sizeof (struct symbol *));
16778 common_block
16779 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16780 size);
16781 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16782 common_block->n_entries = 0;
16783
16784 for (child_die = die->child;
16785 child_die && child_die->tag;
16786 child_die = child_die->sibling)
16787 {
16788 /* Create the symbol in the DW_TAG_common_block block in the current
16789 symbol scope. */
16790 sym = new_symbol (child_die, NULL, cu);
16791 if (sym != NULL)
16792 {
16793 struct attribute *member_loc;
16794
16795 common_block->contents[common_block->n_entries++] = sym;
16796
16797 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16798 cu);
16799 if (member_loc)
16800 {
16801 /* GDB has handled this for a long time, but it is
16802 not specified by DWARF. It seems to have been
16803 emitted by gfortran at least as recently as:
16804 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16805 complaint (_("Variable in common block has "
16806 "DW_AT_data_member_location "
16807 "- DIE at %s [in module %s]"),
16808 sect_offset_str (child_die->sect_off),
16809 objfile_name (objfile));
16810
16811 if (member_loc->form_is_section_offset ())
16812 dwarf2_complex_location_expr_complaint ();
16813 else if (member_loc->form_is_constant ()
16814 || member_loc->form_is_block ())
16815 {
16816 if (attr != nullptr)
16817 mark_common_block_symbol_computed (sym, die, attr,
16818 member_loc, cu);
16819 }
16820 else
16821 dwarf2_complex_location_expr_complaint ();
16822 }
16823 }
16824 }
16825
16826 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16827 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16828 }
16829 }
16830
16831 /* Create a type for a C++ namespace. */
16832
16833 static struct type *
16834 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16835 {
16836 struct objfile *objfile = cu->per_objfile->objfile;
16837 const char *previous_prefix, *name;
16838 int is_anonymous;
16839 struct type *type;
16840
16841 /* For extensions, reuse the type of the original namespace. */
16842 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16843 {
16844 struct die_info *ext_die;
16845 struct dwarf2_cu *ext_cu = cu;
16846
16847 ext_die = dwarf2_extension (die, &ext_cu);
16848 type = read_type_die (ext_die, ext_cu);
16849
16850 /* EXT_CU may not be the same as CU.
16851 Ensure TYPE is recorded with CU in die_type_hash. */
16852 return set_die_type (die, type, cu);
16853 }
16854
16855 name = namespace_name (die, &is_anonymous, cu);
16856
16857 /* Now build the name of the current namespace. */
16858
16859 previous_prefix = determine_prefix (die, cu);
16860 if (previous_prefix[0] != '\0')
16861 name = typename_concat (&objfile->objfile_obstack,
16862 previous_prefix, name, 0, cu);
16863
16864 /* Create the type. */
16865 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16866
16867 return set_die_type (die, type, cu);
16868 }
16869
16870 /* Read a namespace scope. */
16871
16872 static void
16873 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16874 {
16875 struct objfile *objfile = cu->per_objfile->objfile;
16876 int is_anonymous;
16877
16878 /* Add a symbol associated to this if we haven't seen the namespace
16879 before. Also, add a using directive if it's an anonymous
16880 namespace. */
16881
16882 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16883 {
16884 struct type *type;
16885
16886 type = read_type_die (die, cu);
16887 new_symbol (die, type, cu);
16888
16889 namespace_name (die, &is_anonymous, cu);
16890 if (is_anonymous)
16891 {
16892 const char *previous_prefix = determine_prefix (die, cu);
16893
16894 std::vector<const char *> excludes;
16895 add_using_directive (using_directives (cu),
16896 previous_prefix, type->name (), NULL,
16897 NULL, excludes, 0, &objfile->objfile_obstack);
16898 }
16899 }
16900
16901 if (die->child != NULL)
16902 {
16903 struct die_info *child_die = die->child;
16904
16905 while (child_die && child_die->tag)
16906 {
16907 process_die (child_die, cu);
16908 child_die = child_die->sibling;
16909 }
16910 }
16911 }
16912
16913 /* Read a Fortran module as type. This DIE can be only a declaration used for
16914 imported module. Still we need that type as local Fortran "use ... only"
16915 declaration imports depend on the created type in determine_prefix. */
16916
16917 static struct type *
16918 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16919 {
16920 struct objfile *objfile = cu->per_objfile->objfile;
16921 const char *module_name;
16922 struct type *type;
16923
16924 module_name = dwarf2_name (die, cu);
16925 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16926
16927 return set_die_type (die, type, cu);
16928 }
16929
16930 /* Read a Fortran module. */
16931
16932 static void
16933 read_module (struct die_info *die, struct dwarf2_cu *cu)
16934 {
16935 struct die_info *child_die = die->child;
16936 struct type *type;
16937
16938 type = read_type_die (die, cu);
16939 new_symbol (die, type, cu);
16940
16941 while (child_die && child_die->tag)
16942 {
16943 process_die (child_die, cu);
16944 child_die = child_die->sibling;
16945 }
16946 }
16947
16948 /* Return the name of the namespace represented by DIE. Set
16949 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16950 namespace. */
16951
16952 static const char *
16953 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16954 {
16955 struct die_info *current_die;
16956 const char *name = NULL;
16957
16958 /* Loop through the extensions until we find a name. */
16959
16960 for (current_die = die;
16961 current_die != NULL;
16962 current_die = dwarf2_extension (die, &cu))
16963 {
16964 /* We don't use dwarf2_name here so that we can detect the absence
16965 of a name -> anonymous namespace. */
16966 name = dwarf2_string_attr (die, DW_AT_name, cu);
16967
16968 if (name != NULL)
16969 break;
16970 }
16971
16972 /* Is it an anonymous namespace? */
16973
16974 *is_anonymous = (name == NULL);
16975 if (*is_anonymous)
16976 name = CP_ANONYMOUS_NAMESPACE_STR;
16977
16978 return name;
16979 }
16980
16981 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16982 the user defined type vector. */
16983
16984 static struct type *
16985 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16986 {
16987 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16988 struct comp_unit_head *cu_header = &cu->header;
16989 struct type *type;
16990 struct attribute *attr_byte_size;
16991 struct attribute *attr_address_class;
16992 int byte_size, addr_class;
16993 struct type *target_type;
16994
16995 target_type = die_type (die, cu);
16996
16997 /* The die_type call above may have already set the type for this DIE. */
16998 type = get_die_type (die, cu);
16999 if (type)
17000 return type;
17001
17002 type = lookup_pointer_type (target_type);
17003
17004 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17005 if (attr_byte_size)
17006 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17007 else
17008 byte_size = cu_header->addr_size;
17009
17010 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17011 if (attr_address_class)
17012 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17013 else
17014 addr_class = DW_ADDR_none;
17015
17016 ULONGEST alignment = get_alignment (cu, die);
17017
17018 /* If the pointer size, alignment, or address class is different
17019 than the default, create a type variant marked as such and set
17020 the length accordingly. */
17021 if (TYPE_LENGTH (type) != byte_size
17022 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17023 && alignment != TYPE_RAW_ALIGN (type))
17024 || addr_class != DW_ADDR_none)
17025 {
17026 if (gdbarch_address_class_type_flags_p (gdbarch))
17027 {
17028 type_instance_flags type_flags
17029 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17030 addr_class);
17031 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17032 == 0);
17033 type = make_type_with_address_space (type, type_flags);
17034 }
17035 else if (TYPE_LENGTH (type) != byte_size)
17036 {
17037 complaint (_("invalid pointer size %d"), byte_size);
17038 }
17039 else if (TYPE_RAW_ALIGN (type) != alignment)
17040 {
17041 complaint (_("Invalid DW_AT_alignment"
17042 " - DIE at %s [in module %s]"),
17043 sect_offset_str (die->sect_off),
17044 objfile_name (cu->per_objfile->objfile));
17045 }
17046 else
17047 {
17048 /* Should we also complain about unhandled address classes? */
17049 }
17050 }
17051
17052 TYPE_LENGTH (type) = byte_size;
17053 set_type_align (type, alignment);
17054 return set_die_type (die, type, cu);
17055 }
17056
17057 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17058 the user defined type vector. */
17059
17060 static struct type *
17061 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17062 {
17063 struct type *type;
17064 struct type *to_type;
17065 struct type *domain;
17066
17067 to_type = die_type (die, cu);
17068 domain = die_containing_type (die, cu);
17069
17070 /* The calls above may have already set the type for this DIE. */
17071 type = get_die_type (die, cu);
17072 if (type)
17073 return type;
17074
17075 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17076 type = lookup_methodptr_type (to_type);
17077 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17078 {
17079 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17080
17081 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17082 to_type->fields (), to_type->num_fields (),
17083 to_type->has_varargs ());
17084 type = lookup_methodptr_type (new_type);
17085 }
17086 else
17087 type = lookup_memberptr_type (to_type, domain);
17088
17089 return set_die_type (die, type, cu);
17090 }
17091
17092 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17093 the user defined type vector. */
17094
17095 static struct type *
17096 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17097 enum type_code refcode)
17098 {
17099 struct comp_unit_head *cu_header = &cu->header;
17100 struct type *type, *target_type;
17101 struct attribute *attr;
17102
17103 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17104
17105 target_type = die_type (die, cu);
17106
17107 /* The die_type call above may have already set the type for this DIE. */
17108 type = get_die_type (die, cu);
17109 if (type)
17110 return type;
17111
17112 type = lookup_reference_type (target_type, refcode);
17113 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17114 if (attr != nullptr)
17115 {
17116 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17117 }
17118 else
17119 {
17120 TYPE_LENGTH (type) = cu_header->addr_size;
17121 }
17122 maybe_set_alignment (cu, die, type);
17123 return set_die_type (die, type, cu);
17124 }
17125
17126 /* Add the given cv-qualifiers to the element type of the array. GCC
17127 outputs DWARF type qualifiers that apply to an array, not the
17128 element type. But GDB relies on the array element type to carry
17129 the cv-qualifiers. This mimics section 6.7.3 of the C99
17130 specification. */
17131
17132 static struct type *
17133 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17134 struct type *base_type, int cnst, int voltl)
17135 {
17136 struct type *el_type, *inner_array;
17137
17138 base_type = copy_type (base_type);
17139 inner_array = base_type;
17140
17141 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17142 {
17143 TYPE_TARGET_TYPE (inner_array) =
17144 copy_type (TYPE_TARGET_TYPE (inner_array));
17145 inner_array = TYPE_TARGET_TYPE (inner_array);
17146 }
17147
17148 el_type = TYPE_TARGET_TYPE (inner_array);
17149 cnst |= TYPE_CONST (el_type);
17150 voltl |= TYPE_VOLATILE (el_type);
17151 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17152
17153 return set_die_type (die, base_type, cu);
17154 }
17155
17156 static struct type *
17157 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17158 {
17159 struct type *base_type, *cv_type;
17160
17161 base_type = die_type (die, cu);
17162
17163 /* The die_type call above may have already set the type for this DIE. */
17164 cv_type = get_die_type (die, cu);
17165 if (cv_type)
17166 return cv_type;
17167
17168 /* In case the const qualifier is applied to an array type, the element type
17169 is so qualified, not the array type (section 6.7.3 of C99). */
17170 if (base_type->code () == TYPE_CODE_ARRAY)
17171 return add_array_cv_type (die, cu, base_type, 1, 0);
17172
17173 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17174 return set_die_type (die, cv_type, cu);
17175 }
17176
17177 static struct type *
17178 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17179 {
17180 struct type *base_type, *cv_type;
17181
17182 base_type = die_type (die, cu);
17183
17184 /* The die_type call above may have already set the type for this DIE. */
17185 cv_type = get_die_type (die, cu);
17186 if (cv_type)
17187 return cv_type;
17188
17189 /* In case the volatile qualifier is applied to an array type, the
17190 element type is so qualified, not the array type (section 6.7.3
17191 of C99). */
17192 if (base_type->code () == TYPE_CODE_ARRAY)
17193 return add_array_cv_type (die, cu, base_type, 0, 1);
17194
17195 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17196 return set_die_type (die, cv_type, cu);
17197 }
17198
17199 /* Handle DW_TAG_restrict_type. */
17200
17201 static struct type *
17202 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17203 {
17204 struct type *base_type, *cv_type;
17205
17206 base_type = die_type (die, cu);
17207
17208 /* The die_type call above may have already set the type for this DIE. */
17209 cv_type = get_die_type (die, cu);
17210 if (cv_type)
17211 return cv_type;
17212
17213 cv_type = make_restrict_type (base_type);
17214 return set_die_type (die, cv_type, cu);
17215 }
17216
17217 /* Handle DW_TAG_atomic_type. */
17218
17219 static struct type *
17220 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17221 {
17222 struct type *base_type, *cv_type;
17223
17224 base_type = die_type (die, cu);
17225
17226 /* The die_type call above may have already set the type for this DIE. */
17227 cv_type = get_die_type (die, cu);
17228 if (cv_type)
17229 return cv_type;
17230
17231 cv_type = make_atomic_type (base_type);
17232 return set_die_type (die, cv_type, cu);
17233 }
17234
17235 /* Extract all information from a DW_TAG_string_type DIE and add to
17236 the user defined type vector. It isn't really a user defined type,
17237 but it behaves like one, with other DIE's using an AT_user_def_type
17238 attribute to reference it. */
17239
17240 static struct type *
17241 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17242 {
17243 struct objfile *objfile = cu->per_objfile->objfile;
17244 struct gdbarch *gdbarch = objfile->arch ();
17245 struct type *type, *range_type, *index_type, *char_type;
17246 struct attribute *attr;
17247 struct dynamic_prop prop;
17248 bool length_is_constant = true;
17249 LONGEST length;
17250
17251 /* There are a couple of places where bit sizes might be made use of
17252 when parsing a DW_TAG_string_type, however, no producer that we know
17253 of make use of these. Handling bit sizes that are a multiple of the
17254 byte size is easy enough, but what about other bit sizes? Lets deal
17255 with that problem when we have to. Warn about these attributes being
17256 unsupported, then parse the type and ignore them like we always
17257 have. */
17258 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17259 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17260 {
17261 static bool warning_printed = false;
17262 if (!warning_printed)
17263 {
17264 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17265 "currently supported on DW_TAG_string_type."));
17266 warning_printed = true;
17267 }
17268 }
17269
17270 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17271 if (attr != nullptr && !attr->form_is_constant ())
17272 {
17273 /* The string length describes the location at which the length of
17274 the string can be found. The size of the length field can be
17275 specified with one of the attributes below. */
17276 struct type *prop_type;
17277 struct attribute *len
17278 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17279 if (len == nullptr)
17280 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17281 if (len != nullptr && len->form_is_constant ())
17282 {
17283 /* Pass 0 as the default as we know this attribute is constant
17284 and the default value will not be returned. */
17285 LONGEST sz = len->constant_value (0);
17286 prop_type = cu->per_objfile->int_type (sz, true);
17287 }
17288 else
17289 {
17290 /* If the size is not specified then we assume it is the size of
17291 an address on this target. */
17292 prop_type = cu->addr_sized_int_type (true);
17293 }
17294
17295 /* Convert the attribute into a dynamic property. */
17296 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17297 length = 1;
17298 else
17299 length_is_constant = false;
17300 }
17301 else if (attr != nullptr)
17302 {
17303 /* This DW_AT_string_length just contains the length with no
17304 indirection. There's no need to create a dynamic property in this
17305 case. Pass 0 for the default value as we know it will not be
17306 returned in this case. */
17307 length = attr->constant_value (0);
17308 }
17309 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17310 {
17311 /* We don't currently support non-constant byte sizes for strings. */
17312 length = attr->constant_value (1);
17313 }
17314 else
17315 {
17316 /* Use 1 as a fallback length if we have nothing else. */
17317 length = 1;
17318 }
17319
17320 index_type = objfile_type (objfile)->builtin_int;
17321 if (length_is_constant)
17322 range_type = create_static_range_type (NULL, index_type, 1, length);
17323 else
17324 {
17325 struct dynamic_prop low_bound;
17326
17327 low_bound.set_const_val (1);
17328 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17329 }
17330 char_type = language_string_char_type (cu->language_defn, gdbarch);
17331 type = create_string_type (NULL, char_type, range_type);
17332
17333 return set_die_type (die, type, cu);
17334 }
17335
17336 /* Assuming that DIE corresponds to a function, returns nonzero
17337 if the function is prototyped. */
17338
17339 static int
17340 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17341 {
17342 struct attribute *attr;
17343
17344 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17345 if (attr && attr->as_boolean ())
17346 return 1;
17347
17348 /* The DWARF standard implies that the DW_AT_prototyped attribute
17349 is only meaningful for C, but the concept also extends to other
17350 languages that allow unprototyped functions (Eg: Objective C).
17351 For all other languages, assume that functions are always
17352 prototyped. */
17353 if (cu->per_cu->lang != language_c
17354 && cu->per_cu->lang != language_objc
17355 && cu->per_cu->lang != language_opencl)
17356 return 1;
17357
17358 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17359 prototyped and unprototyped functions; default to prototyped,
17360 since that is more common in modern code (and RealView warns
17361 about unprototyped functions). */
17362 if (producer_is_realview (cu->producer))
17363 return 1;
17364
17365 return 0;
17366 }
17367
17368 /* Handle DIES due to C code like:
17369
17370 struct foo
17371 {
17372 int (*funcp)(int a, long l);
17373 int b;
17374 };
17375
17376 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17377
17378 static struct type *
17379 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17380 {
17381 struct objfile *objfile = cu->per_objfile->objfile;
17382 struct type *type; /* Type that this function returns. */
17383 struct type *ftype; /* Function that returns above type. */
17384 struct attribute *attr;
17385
17386 type = die_type (die, cu);
17387
17388 /* The die_type call above may have already set the type for this DIE. */
17389 ftype = get_die_type (die, cu);
17390 if (ftype)
17391 return ftype;
17392
17393 ftype = lookup_function_type (type);
17394
17395 if (prototyped_function_p (die, cu))
17396 ftype->set_is_prototyped (true);
17397
17398 /* Store the calling convention in the type if it's available in
17399 the subroutine die. Otherwise set the calling convention to
17400 the default value DW_CC_normal. */
17401 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17402 if (attr != nullptr
17403 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17404 TYPE_CALLING_CONVENTION (ftype)
17405 = (enum dwarf_calling_convention) attr->constant_value (0);
17406 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17407 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17408 else
17409 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17410
17411 /* Record whether the function returns normally to its caller or not
17412 if the DWARF producer set that information. */
17413 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17414 if (attr && attr->as_boolean ())
17415 TYPE_NO_RETURN (ftype) = 1;
17416
17417 /* We need to add the subroutine type to the die immediately so
17418 we don't infinitely recurse when dealing with parameters
17419 declared as the same subroutine type. */
17420 set_die_type (die, ftype, cu);
17421
17422 if (die->child != NULL)
17423 {
17424 struct type *void_type = objfile_type (objfile)->builtin_void;
17425 struct die_info *child_die;
17426 int nparams, iparams;
17427
17428 /* Count the number of parameters.
17429 FIXME: GDB currently ignores vararg functions, but knows about
17430 vararg member functions. */
17431 nparams = 0;
17432 child_die = die->child;
17433 while (child_die && child_die->tag)
17434 {
17435 if (child_die->tag == DW_TAG_formal_parameter)
17436 nparams++;
17437 else if (child_die->tag == DW_TAG_unspecified_parameters)
17438 ftype->set_has_varargs (true);
17439
17440 child_die = child_die->sibling;
17441 }
17442
17443 /* Allocate storage for parameters and fill them in. */
17444 ftype->set_num_fields (nparams);
17445 ftype->set_fields
17446 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17447
17448 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17449 even if we error out during the parameters reading below. */
17450 for (iparams = 0; iparams < nparams; iparams++)
17451 ftype->field (iparams).set_type (void_type);
17452
17453 iparams = 0;
17454 child_die = die->child;
17455 while (child_die && child_die->tag)
17456 {
17457 if (child_die->tag == DW_TAG_formal_parameter)
17458 {
17459 struct type *arg_type;
17460
17461 /* DWARF version 2 has no clean way to discern C++
17462 static and non-static member functions. G++ helps
17463 GDB by marking the first parameter for non-static
17464 member functions (which is the this pointer) as
17465 artificial. We pass this information to
17466 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17467
17468 DWARF version 3 added DW_AT_object_pointer, which GCC
17469 4.5 does not yet generate. */
17470 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17471 if (attr != nullptr)
17472 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17473 else
17474 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17475 arg_type = die_type (child_die, cu);
17476
17477 /* RealView does not mark THIS as const, which the testsuite
17478 expects. GCC marks THIS as const in method definitions,
17479 but not in the class specifications (GCC PR 43053). */
17480 if (cu->per_cu->lang == language_cplus
17481 && !TYPE_CONST (arg_type)
17482 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17483 {
17484 int is_this = 0;
17485 struct dwarf2_cu *arg_cu = cu;
17486 const char *name = dwarf2_name (child_die, cu);
17487
17488 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17489 if (attr != nullptr)
17490 {
17491 /* If the compiler emits this, use it. */
17492 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17493 is_this = 1;
17494 }
17495 else if (name && strcmp (name, "this") == 0)
17496 /* Function definitions will have the argument names. */
17497 is_this = 1;
17498 else if (name == NULL && iparams == 0)
17499 /* Declarations may not have the names, so like
17500 elsewhere in GDB, assume an artificial first
17501 argument is "this". */
17502 is_this = 1;
17503
17504 if (is_this)
17505 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17506 arg_type, 0);
17507 }
17508
17509 ftype->field (iparams).set_type (arg_type);
17510 iparams++;
17511 }
17512 child_die = child_die->sibling;
17513 }
17514 }
17515
17516 return ftype;
17517 }
17518
17519 static struct type *
17520 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17521 {
17522 struct objfile *objfile = cu->per_objfile->objfile;
17523 const char *name = NULL;
17524 struct type *this_type, *target_type;
17525
17526 name = dwarf2_full_name (NULL, die, cu);
17527 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17528 this_type->set_target_is_stub (true);
17529 set_die_type (die, this_type, cu);
17530 target_type = die_type (die, cu);
17531 if (target_type != this_type)
17532 TYPE_TARGET_TYPE (this_type) = target_type;
17533 else
17534 {
17535 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17536 spec and cause infinite loops in GDB. */
17537 complaint (_("Self-referential DW_TAG_typedef "
17538 "- DIE at %s [in module %s]"),
17539 sect_offset_str (die->sect_off), objfile_name (objfile));
17540 TYPE_TARGET_TYPE (this_type) = NULL;
17541 }
17542 if (name == NULL)
17543 {
17544 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17545 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17546 Handle these by just returning the target type, rather than
17547 constructing an anonymous typedef type and trying to handle this
17548 elsewhere. */
17549 set_die_type (die, target_type, cu);
17550 return target_type;
17551 }
17552 return this_type;
17553 }
17554
17555 /* Helper for get_dwarf2_rational_constant that computes the value of
17556 a given gmp_mpz given an attribute. */
17557
17558 static void
17559 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17560 {
17561 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17562 location expression that pushes an implicit value. */
17563 if (attr->form == DW_FORM_exprloc)
17564 {
17565 dwarf_block *blk = attr->as_block ();
17566 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17567 {
17568 uint64_t len;
17569 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17570 blk->data + blk->size,
17571 &len);
17572 if (ptr - blk->data + len <= blk->size)
17573 {
17574 mpz_import (value->val, len,
17575 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17576 1, 0, 0, ptr);
17577 return;
17578 }
17579 }
17580
17581 /* On failure set it to 1. */
17582 *value = gdb_mpz (1);
17583 }
17584 else if (attr->form_is_block ())
17585 {
17586 dwarf_block *blk = attr->as_block ();
17587 mpz_import (value->val, blk->size,
17588 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17589 1, 0, 0, blk->data);
17590 }
17591 else
17592 *value = gdb_mpz (attr->constant_value (1));
17593 }
17594
17595 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17596 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17597
17598 If the numerator and/or numerator attribute is missing,
17599 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17600 untouched. */
17601
17602 static void
17603 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17604 gdb_mpz *numerator, gdb_mpz *denominator)
17605 {
17606 struct attribute *num_attr, *denom_attr;
17607
17608 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17609 if (num_attr == nullptr)
17610 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17611 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17612
17613 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17614 if (denom_attr == nullptr)
17615 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17616 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17617
17618 if (num_attr == nullptr || denom_attr == nullptr)
17619 return;
17620
17621 get_mpz (cu, numerator, num_attr);
17622 get_mpz (cu, denominator, denom_attr);
17623 }
17624
17625 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17626 rational constant, rather than a signed one.
17627
17628 If the rational constant has a negative value, a complaint
17629 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17630
17631 static void
17632 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17633 struct dwarf2_cu *cu,
17634 gdb_mpz *numerator,
17635 gdb_mpz *denominator)
17636 {
17637 gdb_mpz num (1);
17638 gdb_mpz denom (1);
17639
17640 get_dwarf2_rational_constant (die, cu, &num, &denom);
17641 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17642 {
17643 mpz_neg (num.val, num.val);
17644 mpz_neg (denom.val, denom.val);
17645 }
17646 else if (mpz_sgn (num.val) == -1)
17647 {
17648 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17649 " in DIE at %s"),
17650 sect_offset_str (die->sect_off));
17651 return;
17652 }
17653 else if (mpz_sgn (denom.val) == -1)
17654 {
17655 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17656 " in DIE at %s"),
17657 sect_offset_str (die->sect_off));
17658 return;
17659 }
17660
17661 *numerator = std::move (num);
17662 *denominator = std::move (denom);
17663 }
17664
17665 /* Assuming that ENCODING is a string whose contents starting at the
17666 K'th character is "_nn" where "nn" is a decimal number, scan that
17667 number and set RESULT to the value. K is updated to point to the
17668 character immediately following the number.
17669
17670 If the string does not conform to the format described above, false
17671 is returned, and K may or may not be changed. */
17672
17673 static bool
17674 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17675 {
17676 /* The next character should be an underscore ('_') followed
17677 by a digit. */
17678 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17679 return false;
17680
17681 /* Skip the underscore. */
17682 k++;
17683 int start = k;
17684
17685 /* Determine the number of digits for our number. */
17686 while (isdigit (encoding[k]))
17687 k++;
17688 if (k == start)
17689 return false;
17690
17691 std::string copy (&encoding[start], k - start);
17692 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17693 return false;
17694
17695 return true;
17696 }
17697
17698 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17699 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17700 DENOM, update OFFSET, and return true on success. Return false on
17701 failure. */
17702
17703 static bool
17704 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17705 gdb_mpz *num, gdb_mpz *denom)
17706 {
17707 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17708 return false;
17709 return ada_get_gnat_encoded_number (encoding, offset, denom);
17710 }
17711
17712 /* Assuming DIE corresponds to a fixed point type, finish the creation
17713 of the corresponding TYPE by setting its type-specific data. CU is
17714 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17715 encodings. It is nullptr if the GNAT encoding should be
17716 ignored. */
17717
17718 static void
17719 finish_fixed_point_type (struct type *type, const char *suffix,
17720 struct die_info *die, struct dwarf2_cu *cu)
17721 {
17722 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17723 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17724
17725 /* If GNAT encodings are preferred, don't examine the
17726 attributes. */
17727 struct attribute *attr = nullptr;
17728 if (suffix == nullptr)
17729 {
17730 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17731 if (attr == nullptr)
17732 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17733 if (attr == nullptr)
17734 attr = dwarf2_attr (die, DW_AT_small, cu);
17735 }
17736
17737 /* Numerator and denominator of our fixed-point type's scaling factor.
17738 The default is a scaling factor of 1, which we use as a fallback
17739 when we are not able to decode it (problem with the debugging info,
17740 unsupported forms, bug in GDB, etc...). Using that as the default
17741 allows us to at least print the unscaled value, which might still
17742 be useful to a user. */
17743 gdb_mpz scale_num (1);
17744 gdb_mpz scale_denom (1);
17745
17746 if (attr == nullptr)
17747 {
17748 int offset = 0;
17749 if (suffix != nullptr
17750 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17751 &scale_denom)
17752 /* The number might be encoded as _nn_dd_nn_dd, where the
17753 second ratio is the 'small value. In this situation, we
17754 want the second value. */
17755 && (suffix[offset] != '_'
17756 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17757 &scale_denom)))
17758 {
17759 /* Found it. */
17760 }
17761 else
17762 {
17763 /* Scaling factor not found. Assume a scaling factor of 1,
17764 and hope for the best. At least the user will be able to
17765 see the encoded value. */
17766 scale_num = 1;
17767 scale_denom = 1;
17768 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17769 sect_offset_str (die->sect_off));
17770 }
17771 }
17772 else if (attr->name == DW_AT_binary_scale)
17773 {
17774 LONGEST scale_exp = attr->constant_value (0);
17775 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17776
17777 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
17778 }
17779 else if (attr->name == DW_AT_decimal_scale)
17780 {
17781 LONGEST scale_exp = attr->constant_value (0);
17782 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17783
17784 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
17785 }
17786 else if (attr->name == DW_AT_small)
17787 {
17788 struct die_info *scale_die;
17789 struct dwarf2_cu *scale_cu = cu;
17790
17791 scale_die = follow_die_ref (die, attr, &scale_cu);
17792 if (scale_die->tag == DW_TAG_constant)
17793 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17794 &scale_num, &scale_denom);
17795 else
17796 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17797 " (DIE at %s)"),
17798 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17799 }
17800 else
17801 {
17802 complaint (_("unsupported scale attribute %s for fixed-point type"
17803 " (DIE at %s)"),
17804 dwarf_attr_name (attr->name),
17805 sect_offset_str (die->sect_off));
17806 }
17807
17808 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
17809 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17810 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
17811 mpq_canonicalize (scaling_factor.val);
17812 }
17813
17814 /* The gnat-encoding suffix for fixed point. */
17815
17816 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
17817
17818 /* If NAME encodes an Ada fixed-point type, return a pointer to the
17819 "XF" suffix of the name. The text after this is what encodes the
17820 'small and 'delta information. Otherwise, return nullptr. */
17821
17822 static const char *
17823 gnat_encoded_fixed_point_type_info (const char *name)
17824 {
17825 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17826 }
17827
17828 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17829 (which may be different from NAME) to the architecture back-end to allow
17830 it to guess the correct format if necessary. */
17831
17832 static struct type *
17833 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17834 const char *name_hint, enum bfd_endian byte_order)
17835 {
17836 struct gdbarch *gdbarch = objfile->arch ();
17837 const struct floatformat **format;
17838 struct type *type;
17839
17840 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17841 if (format)
17842 type = init_float_type (objfile, bits, name, format, byte_order);
17843 else
17844 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17845
17846 return type;
17847 }
17848
17849 /* Allocate an integer type of size BITS and name NAME. */
17850
17851 static struct type *
17852 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17853 int bits, int unsigned_p, const char *name)
17854 {
17855 struct type *type;
17856
17857 /* Versions of Intel's C Compiler generate an integer type called "void"
17858 instead of using DW_TAG_unspecified_type. This has been seen on
17859 at least versions 14, 17, and 18. */
17860 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17861 && strcmp (name, "void") == 0)
17862 type = objfile_type (objfile)->builtin_void;
17863 else
17864 type = init_integer_type (objfile, bits, unsigned_p, name);
17865
17866 return type;
17867 }
17868
17869 /* Return true if DIE has a DW_AT_small attribute whose value is
17870 a constant rational, where both the numerator and denominator
17871 are equal to zero.
17872
17873 CU is the DIE's Compilation Unit. */
17874
17875 static bool
17876 has_zero_over_zero_small_attribute (struct die_info *die,
17877 struct dwarf2_cu *cu)
17878 {
17879 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17880 if (attr == nullptr)
17881 return false;
17882
17883 struct dwarf2_cu *scale_cu = cu;
17884 struct die_info *scale_die
17885 = follow_die_ref (die, attr, &scale_cu);
17886
17887 if (scale_die->tag != DW_TAG_constant)
17888 return false;
17889
17890 gdb_mpz num (1), denom (1);
17891 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
17892 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
17893 }
17894
17895 /* Initialise and return a floating point type of size BITS suitable for
17896 use as a component of a complex number. The NAME_HINT is passed through
17897 when initialising the floating point type and is the name of the complex
17898 type.
17899
17900 As DWARF doesn't currently provide an explicit name for the components
17901 of a complex number, but it can be helpful to have these components
17902 named, we try to select a suitable name based on the size of the
17903 component. */
17904 static struct type *
17905 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17906 struct objfile *objfile,
17907 int bits, const char *name_hint,
17908 enum bfd_endian byte_order)
17909 {
17910 gdbarch *gdbarch = objfile->arch ();
17911 struct type *tt = nullptr;
17912
17913 /* Try to find a suitable floating point builtin type of size BITS.
17914 We're going to use the name of this type as the name for the complex
17915 target type that we are about to create. */
17916 switch (cu->per_cu->lang)
17917 {
17918 case language_fortran:
17919 switch (bits)
17920 {
17921 case 32:
17922 tt = builtin_f_type (gdbarch)->builtin_real;
17923 break;
17924 case 64:
17925 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17926 break;
17927 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17928 case 128:
17929 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17930 break;
17931 }
17932 break;
17933 default:
17934 switch (bits)
17935 {
17936 case 32:
17937 tt = builtin_type (gdbarch)->builtin_float;
17938 break;
17939 case 64:
17940 tt = builtin_type (gdbarch)->builtin_double;
17941 break;
17942 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17943 case 128:
17944 tt = builtin_type (gdbarch)->builtin_long_double;
17945 break;
17946 }
17947 break;
17948 }
17949
17950 /* If the type we found doesn't match the size we were looking for, then
17951 pretend we didn't find a type at all, the complex target type we
17952 create will then be nameless. */
17953 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17954 tt = nullptr;
17955
17956 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17957 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17958 }
17959
17960 /* Find a representation of a given base type and install
17961 it in the TYPE field of the die. */
17962
17963 static struct type *
17964 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17965 {
17966 struct objfile *objfile = cu->per_objfile->objfile;
17967 struct type *type;
17968 struct attribute *attr;
17969 int encoding = 0, bits = 0;
17970 const char *name;
17971 gdbarch *arch;
17972
17973 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17974 if (attr != nullptr && attr->form_is_constant ())
17975 encoding = attr->constant_value (0);
17976 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17977 if (attr != nullptr)
17978 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
17979 name = dwarf2_name (die, cu);
17980 if (!name)
17981 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17982
17983 arch = objfile->arch ();
17984 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17985
17986 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17987 if (attr != nullptr && attr->form_is_constant ())
17988 {
17989 int endianity = attr->constant_value (0);
17990
17991 switch (endianity)
17992 {
17993 case DW_END_big:
17994 byte_order = BFD_ENDIAN_BIG;
17995 break;
17996 case DW_END_little:
17997 byte_order = BFD_ENDIAN_LITTLE;
17998 break;
17999 default:
18000 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18001 break;
18002 }
18003 }
18004
18005 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18006 && cu->per_cu->lang == language_ada
18007 && has_zero_over_zero_small_attribute (die, cu))
18008 {
18009 /* brobecker/2018-02-24: This is a fixed point type for which
18010 the scaling factor is represented as fraction whose value
18011 does not make sense (zero divided by zero), so we should
18012 normally never see these. However, there is a small category
18013 of fixed point types for which GNAT is unable to provide
18014 the scaling factor via the standard DWARF mechanisms, and
18015 for which the info is provided via the GNAT encodings instead.
18016 This is likely what this DIE is about. */
18017 encoding = (encoding == DW_ATE_signed_fixed
18018 ? DW_ATE_signed
18019 : DW_ATE_unsigned);
18020 }
18021
18022 /* With GNAT encodings, fixed-point information will be encoded in
18023 the type name. Note that this can also occur with the above
18024 zero-over-zero case, which is why this is a separate "if" rather
18025 than an "else if". */
18026 const char *gnat_encoding_suffix = nullptr;
18027 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18028 && cu->per_cu->lang == language_ada
18029 && name != nullptr)
18030 {
18031 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18032 if (gnat_encoding_suffix != nullptr)
18033 {
18034 gdb_assert (startswith (gnat_encoding_suffix,
18035 GNAT_FIXED_POINT_SUFFIX));
18036 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18037 name, gnat_encoding_suffix - name);
18038 /* Use -1 here so that SUFFIX points at the "_" after the
18039 "XF". */
18040 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18041
18042 encoding = (encoding == DW_ATE_signed
18043 ? DW_ATE_signed_fixed
18044 : DW_ATE_unsigned_fixed);
18045 }
18046 }
18047
18048 switch (encoding)
18049 {
18050 case DW_ATE_address:
18051 /* Turn DW_ATE_address into a void * pointer. */
18052 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18053 type = init_pointer_type (objfile, bits, name, type);
18054 break;
18055 case DW_ATE_boolean:
18056 type = init_boolean_type (objfile, bits, 1, name);
18057 break;
18058 case DW_ATE_complex_float:
18059 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18060 byte_order);
18061 if (type->code () == TYPE_CODE_ERROR)
18062 {
18063 if (name == nullptr)
18064 {
18065 struct obstack *obstack
18066 = &cu->per_objfile->objfile->objfile_obstack;
18067 name = obconcat (obstack, "_Complex ", type->name (),
18068 nullptr);
18069 }
18070 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18071 }
18072 else
18073 type = init_complex_type (name, type);
18074 break;
18075 case DW_ATE_decimal_float:
18076 type = init_decfloat_type (objfile, bits, name);
18077 break;
18078 case DW_ATE_float:
18079 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18080 break;
18081 case DW_ATE_signed:
18082 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18083 break;
18084 case DW_ATE_unsigned:
18085 if (cu->per_cu->lang == language_fortran
18086 && name
18087 && startswith (name, "character("))
18088 type = init_character_type (objfile, bits, 1, name);
18089 else
18090 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18091 break;
18092 case DW_ATE_signed_char:
18093 if (cu->per_cu->lang == language_ada
18094 || cu->per_cu->lang == language_m2
18095 || cu->per_cu->lang == language_pascal
18096 || cu->per_cu->lang == language_fortran)
18097 type = init_character_type (objfile, bits, 0, name);
18098 else
18099 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18100 break;
18101 case DW_ATE_unsigned_char:
18102 if (cu->per_cu->lang == language_ada
18103 || cu->per_cu->lang == language_m2
18104 || cu->per_cu->lang == language_pascal
18105 || cu->per_cu->lang == language_fortran
18106 || cu->per_cu->lang == language_rust)
18107 type = init_character_type (objfile, bits, 1, name);
18108 else
18109 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18110 break;
18111 case DW_ATE_UTF:
18112 {
18113 if (bits == 16)
18114 type = builtin_type (arch)->builtin_char16;
18115 else if (bits == 32)
18116 type = builtin_type (arch)->builtin_char32;
18117 else
18118 {
18119 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18120 bits);
18121 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18122 }
18123 return set_die_type (die, type, cu);
18124 }
18125 break;
18126 case DW_ATE_signed_fixed:
18127 type = init_fixed_point_type (objfile, bits, 0, name);
18128 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18129 break;
18130 case DW_ATE_unsigned_fixed:
18131 type = init_fixed_point_type (objfile, bits, 1, name);
18132 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18133 break;
18134
18135 default:
18136 complaint (_("unsupported DW_AT_encoding: '%s'"),
18137 dwarf_type_encoding_name (encoding));
18138 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18139 break;
18140 }
18141
18142 if (name && strcmp (name, "char") == 0)
18143 type->set_has_no_signedness (true);
18144
18145 maybe_set_alignment (cu, die, type);
18146
18147 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18148
18149 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18150 {
18151 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18152 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18153 {
18154 unsigned real_bit_size = attr->as_unsigned ();
18155 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18156 /* Only use the attributes if they make sense together. */
18157 if (attr == nullptr
18158 || (attr->as_unsigned () + real_bit_size
18159 <= 8 * TYPE_LENGTH (type)))
18160 {
18161 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18162 = real_bit_size;
18163 if (attr != nullptr)
18164 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18165 = attr->as_unsigned ();
18166 }
18167 }
18168 }
18169
18170 return set_die_type (die, type, cu);
18171 }
18172
18173 /* A helper function that returns the name of DIE, if it refers to a
18174 variable declaration. */
18175
18176 static const char *
18177 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18178 {
18179 if (die->tag != DW_TAG_variable)
18180 return nullptr;
18181
18182 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18183 if (attr == nullptr || !attr->as_boolean ())
18184 return nullptr;
18185
18186 attr = dwarf2_attr (die, DW_AT_name, cu);
18187 if (attr == nullptr)
18188 return nullptr;
18189 return attr->as_string ();
18190 }
18191
18192 /* Parse dwarf attribute if it's a block, reference or constant and put the
18193 resulting value of the attribute into struct bound_prop.
18194 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18195
18196 static int
18197 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18198 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18199 struct type *default_type)
18200 {
18201 struct dwarf2_property_baton *baton;
18202 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18203 struct objfile *objfile = per_objfile->objfile;
18204 struct obstack *obstack = &objfile->objfile_obstack;
18205
18206 gdb_assert (default_type != NULL);
18207
18208 if (attr == NULL || prop == NULL)
18209 return 0;
18210
18211 if (attr->form_is_block ())
18212 {
18213 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18214 baton->property_type = default_type;
18215 baton->locexpr.per_cu = cu->per_cu;
18216 baton->locexpr.per_objfile = per_objfile;
18217
18218 struct dwarf_block *block = attr->as_block ();
18219 baton->locexpr.size = block->size;
18220 baton->locexpr.data = block->data;
18221 switch (attr->name)
18222 {
18223 case DW_AT_string_length:
18224 baton->locexpr.is_reference = true;
18225 break;
18226 default:
18227 baton->locexpr.is_reference = false;
18228 break;
18229 }
18230
18231 prop->set_locexpr (baton);
18232 gdb_assert (prop->baton () != NULL);
18233 }
18234 else if (attr->form_is_ref ())
18235 {
18236 struct dwarf2_cu *target_cu = cu;
18237 struct die_info *target_die;
18238 struct attribute *target_attr;
18239
18240 target_die = follow_die_ref (die, attr, &target_cu);
18241 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18242 if (target_attr == NULL)
18243 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18244 target_cu);
18245 if (target_attr == NULL)
18246 {
18247 const char *name = var_decl_name (target_die, target_cu);
18248 if (name != nullptr)
18249 {
18250 prop->set_variable_name (name);
18251 return 1;
18252 }
18253 return 0;
18254 }
18255
18256 switch (target_attr->name)
18257 {
18258 case DW_AT_location:
18259 if (target_attr->form_is_section_offset ())
18260 {
18261 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18262 baton->property_type = die_type (target_die, target_cu);
18263 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18264 prop->set_loclist (baton);
18265 gdb_assert (prop->baton () != NULL);
18266 }
18267 else if (target_attr->form_is_block ())
18268 {
18269 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18270 baton->property_type = die_type (target_die, target_cu);
18271 baton->locexpr.per_cu = cu->per_cu;
18272 baton->locexpr.per_objfile = per_objfile;
18273 struct dwarf_block *block = target_attr->as_block ();
18274 baton->locexpr.size = block->size;
18275 baton->locexpr.data = block->data;
18276 baton->locexpr.is_reference = true;
18277 prop->set_locexpr (baton);
18278 gdb_assert (prop->baton () != NULL);
18279 }
18280 else
18281 {
18282 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18283 "dynamic property");
18284 return 0;
18285 }
18286 break;
18287 case DW_AT_data_member_location:
18288 {
18289 LONGEST offset;
18290
18291 if (!handle_data_member_location (target_die, target_cu,
18292 &offset))
18293 return 0;
18294
18295 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18296 baton->property_type = read_type_die (target_die->parent,
18297 target_cu);
18298 baton->offset_info.offset = offset;
18299 baton->offset_info.type = die_type (target_die, target_cu);
18300 prop->set_addr_offset (baton);
18301 break;
18302 }
18303 }
18304 }
18305 else if (attr->form_is_constant ())
18306 prop->set_const_val (attr->constant_value (0));
18307 else
18308 {
18309 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18310 dwarf2_name (die, cu));
18311 return 0;
18312 }
18313
18314 return 1;
18315 }
18316
18317 /* See read.h. */
18318
18319 struct type *
18320 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18321 {
18322 struct type *int_type;
18323
18324 /* Helper macro to examine the various builtin types. */
18325 #define TRY_TYPE(F) \
18326 int_type = (unsigned_p \
18327 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18328 : objfile_type (objfile)->builtin_ ## F); \
18329 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18330 return int_type
18331
18332 TRY_TYPE (char);
18333 TRY_TYPE (short);
18334 TRY_TYPE (int);
18335 TRY_TYPE (long);
18336 TRY_TYPE (long_long);
18337
18338 #undef TRY_TYPE
18339
18340 gdb_assert_not_reached ("unable to find suitable integer type");
18341 }
18342
18343 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18344 present (which is valid) then compute the default type based on the
18345 compilation units address size. */
18346
18347 static struct type *
18348 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18349 {
18350 struct type *index_type = die_type (die, cu);
18351
18352 /* Dwarf-2 specifications explicitly allows to create subrange types
18353 without specifying a base type.
18354 In that case, the base type must be set to the type of
18355 the lower bound, upper bound or count, in that order, if any of these
18356 three attributes references an object that has a type.
18357 If no base type is found, the Dwarf-2 specifications say that
18358 a signed integer type of size equal to the size of an address should
18359 be used.
18360 For the following C code: `extern char gdb_int [];'
18361 GCC produces an empty range DIE.
18362 FIXME: muller/2010-05-28: Possible references to object for low bound,
18363 high bound or count are not yet handled by this code. */
18364 if (index_type->code () == TYPE_CODE_VOID)
18365 index_type = cu->addr_sized_int_type (false);
18366
18367 return index_type;
18368 }
18369
18370 /* Read the given DW_AT_subrange DIE. */
18371
18372 static struct type *
18373 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18374 {
18375 struct type *base_type, *orig_base_type;
18376 struct type *range_type;
18377 struct attribute *attr;
18378 struct dynamic_prop low, high;
18379 int low_default_is_valid;
18380 int high_bound_is_count = 0;
18381 const char *name;
18382 ULONGEST negative_mask;
18383
18384 orig_base_type = read_subrange_index_type (die, cu);
18385
18386 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18387 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18388 creating the range type, but we use the result of check_typedef
18389 when examining properties of the type. */
18390 base_type = check_typedef (orig_base_type);
18391
18392 /* The die_type call above may have already set the type for this DIE. */
18393 range_type = get_die_type (die, cu);
18394 if (range_type)
18395 return range_type;
18396
18397 high.set_const_val (0);
18398
18399 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18400 omitting DW_AT_lower_bound. */
18401 switch (cu->per_cu->lang)
18402 {
18403 case language_c:
18404 case language_cplus:
18405 low.set_const_val (0);
18406 low_default_is_valid = 1;
18407 break;
18408 case language_fortran:
18409 low.set_const_val (1);
18410 low_default_is_valid = 1;
18411 break;
18412 case language_d:
18413 case language_objc:
18414 case language_rust:
18415 low.set_const_val (0);
18416 low_default_is_valid = (cu->header.version >= 4);
18417 break;
18418 case language_ada:
18419 case language_m2:
18420 case language_pascal:
18421 low.set_const_val (1);
18422 low_default_is_valid = (cu->header.version >= 4);
18423 break;
18424 default:
18425 low.set_const_val (0);
18426 low_default_is_valid = 0;
18427 break;
18428 }
18429
18430 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18431 if (attr != nullptr)
18432 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18433 else if (!low_default_is_valid)
18434 complaint (_("Missing DW_AT_lower_bound "
18435 "- DIE at %s [in module %s]"),
18436 sect_offset_str (die->sect_off),
18437 objfile_name (cu->per_objfile->objfile));
18438
18439 struct attribute *attr_ub, *attr_count;
18440 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18441 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18442 {
18443 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18444 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18445 {
18446 /* If bounds are constant do the final calculation here. */
18447 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18448 high.set_const_val (low.const_val () + high.const_val () - 1);
18449 else
18450 high_bound_is_count = 1;
18451 }
18452 else
18453 {
18454 if (attr_ub != NULL)
18455 complaint (_("Unresolved DW_AT_upper_bound "
18456 "- DIE at %s [in module %s]"),
18457 sect_offset_str (die->sect_off),
18458 objfile_name (cu->per_objfile->objfile));
18459 if (attr_count != NULL)
18460 complaint (_("Unresolved DW_AT_count "
18461 "- DIE at %s [in module %s]"),
18462 sect_offset_str (die->sect_off),
18463 objfile_name (cu->per_objfile->objfile));
18464 }
18465 }
18466
18467 LONGEST bias = 0;
18468 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18469 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18470 bias = bias_attr->constant_value (0);
18471
18472 /* Normally, the DWARF producers are expected to use a signed
18473 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18474 But this is unfortunately not always the case, as witnessed
18475 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18476 is used instead. To work around that ambiguity, we treat
18477 the bounds as signed, and thus sign-extend their values, when
18478 the base type is signed. */
18479 negative_mask =
18480 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18481 if (low.kind () == PROP_CONST
18482 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18483 low.set_const_val (low.const_val () | negative_mask);
18484 if (high.kind () == PROP_CONST
18485 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18486 high.set_const_val (high.const_val () | negative_mask);
18487
18488 /* Check for bit and byte strides. */
18489 struct dynamic_prop byte_stride_prop;
18490 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18491 if (attr_byte_stride != nullptr)
18492 {
18493 struct type *prop_type = cu->addr_sized_int_type (false);
18494 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18495 prop_type);
18496 }
18497
18498 struct dynamic_prop bit_stride_prop;
18499 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18500 if (attr_bit_stride != nullptr)
18501 {
18502 /* It only makes sense to have either a bit or byte stride. */
18503 if (attr_byte_stride != nullptr)
18504 {
18505 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18506 "- DIE at %s [in module %s]"),
18507 sect_offset_str (die->sect_off),
18508 objfile_name (cu->per_objfile->objfile));
18509 attr_bit_stride = nullptr;
18510 }
18511 else
18512 {
18513 struct type *prop_type = cu->addr_sized_int_type (false);
18514 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18515 prop_type);
18516 }
18517 }
18518
18519 if (attr_byte_stride != nullptr
18520 || attr_bit_stride != nullptr)
18521 {
18522 bool byte_stride_p = (attr_byte_stride != nullptr);
18523 struct dynamic_prop *stride
18524 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18525
18526 range_type
18527 = create_range_type_with_stride (NULL, orig_base_type, &low,
18528 &high, bias, stride, byte_stride_p);
18529 }
18530 else
18531 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18532
18533 if (high_bound_is_count)
18534 range_type->bounds ()->flag_upper_bound_is_count = 1;
18535
18536 /* Ada expects an empty array on no boundary attributes. */
18537 if (attr == NULL && cu->per_cu->lang != language_ada)
18538 range_type->bounds ()->high.set_undefined ();
18539
18540 name = dwarf2_name (die, cu);
18541 if (name)
18542 range_type->set_name (name);
18543
18544 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18545 if (attr != nullptr)
18546 TYPE_LENGTH (range_type) = attr->constant_value (0);
18547
18548 maybe_set_alignment (cu, die, range_type);
18549
18550 set_die_type (die, range_type, cu);
18551
18552 /* set_die_type should be already done. */
18553 set_descriptive_type (range_type, die, cu);
18554
18555 return range_type;
18556 }
18557
18558 static struct type *
18559 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18560 {
18561 struct type *type;
18562
18563 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18564 type->set_name (dwarf2_name (die, cu));
18565
18566 /* In Ada, an unspecified type is typically used when the description
18567 of the type is deferred to a different unit. When encountering
18568 such a type, we treat it as a stub, and try to resolve it later on,
18569 when needed. */
18570 if (cu->per_cu->lang == language_ada)
18571 type->set_is_stub (true);
18572
18573 return set_die_type (die, type, cu);
18574 }
18575
18576 /* Read a single die and all its descendents. Set the die's sibling
18577 field to NULL; set other fields in the die correctly, and set all
18578 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18579 location of the info_ptr after reading all of those dies. PARENT
18580 is the parent of the die in question. */
18581
18582 static struct die_info *
18583 read_die_and_children (const struct die_reader_specs *reader,
18584 const gdb_byte *info_ptr,
18585 const gdb_byte **new_info_ptr,
18586 struct die_info *parent)
18587 {
18588 struct die_info *die;
18589 const gdb_byte *cur_ptr;
18590
18591 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18592 if (die == NULL)
18593 {
18594 *new_info_ptr = cur_ptr;
18595 return NULL;
18596 }
18597 store_in_ref_table (die, reader->cu);
18598
18599 if (die->has_children)
18600 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18601 else
18602 {
18603 die->child = NULL;
18604 *new_info_ptr = cur_ptr;
18605 }
18606
18607 die->sibling = NULL;
18608 die->parent = parent;
18609 return die;
18610 }
18611
18612 /* Read a die, all of its descendents, and all of its siblings; set
18613 all of the fields of all of the dies correctly. Arguments are as
18614 in read_die_and_children. */
18615
18616 static struct die_info *
18617 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18618 const gdb_byte *info_ptr,
18619 const gdb_byte **new_info_ptr,
18620 struct die_info *parent)
18621 {
18622 struct die_info *first_die, *last_sibling;
18623 const gdb_byte *cur_ptr;
18624
18625 cur_ptr = info_ptr;
18626 first_die = last_sibling = NULL;
18627
18628 while (1)
18629 {
18630 struct die_info *die
18631 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18632
18633 if (die == NULL)
18634 {
18635 *new_info_ptr = cur_ptr;
18636 return first_die;
18637 }
18638
18639 if (!first_die)
18640 first_die = die;
18641 else
18642 last_sibling->sibling = die;
18643
18644 last_sibling = die;
18645 }
18646 }
18647
18648 /* Read a die, all of its descendents, and all of its siblings; set
18649 all of the fields of all of the dies correctly. Arguments are as
18650 in read_die_and_children.
18651 This the main entry point for reading a DIE and all its children. */
18652
18653 static struct die_info *
18654 read_die_and_siblings (const struct die_reader_specs *reader,
18655 const gdb_byte *info_ptr,
18656 const gdb_byte **new_info_ptr,
18657 struct die_info *parent)
18658 {
18659 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18660 new_info_ptr, parent);
18661
18662 if (dwarf_die_debug)
18663 {
18664 fprintf_unfiltered (gdb_stdlog,
18665 "Read die from %s@0x%x of %s:\n",
18666 reader->die_section->get_name (),
18667 (unsigned) (info_ptr - reader->die_section->buffer),
18668 bfd_get_filename (reader->abfd));
18669 dump_die (die, dwarf_die_debug);
18670 }
18671
18672 return die;
18673 }
18674
18675 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18676 attributes.
18677 The caller is responsible for filling in the extra attributes
18678 and updating (*DIEP)->num_attrs.
18679 Set DIEP to point to a newly allocated die with its information,
18680 except for its child, sibling, and parent fields. */
18681
18682 static const gdb_byte *
18683 read_full_die_1 (const struct die_reader_specs *reader,
18684 struct die_info **diep, const gdb_byte *info_ptr,
18685 int num_extra_attrs)
18686 {
18687 unsigned int abbrev_number, bytes_read, i;
18688 const struct abbrev_info *abbrev;
18689 struct die_info *die;
18690 struct dwarf2_cu *cu = reader->cu;
18691 bfd *abfd = reader->abfd;
18692
18693 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18694 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18695 info_ptr += bytes_read;
18696 if (!abbrev_number)
18697 {
18698 *diep = NULL;
18699 return info_ptr;
18700 }
18701
18702 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18703 if (!abbrev)
18704 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18705 abbrev_number,
18706 bfd_get_filename (abfd));
18707
18708 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18709 die->sect_off = sect_off;
18710 die->tag = abbrev->tag;
18711 die->abbrev = abbrev_number;
18712 die->has_children = abbrev->has_children;
18713
18714 /* Make the result usable.
18715 The caller needs to update num_attrs after adding the extra
18716 attributes. */
18717 die->num_attrs = abbrev->num_attrs;
18718
18719 bool any_need_reprocess = false;
18720 for (i = 0; i < abbrev->num_attrs; ++i)
18721 {
18722 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18723 info_ptr);
18724 if (die->attrs[i].requires_reprocessing_p ())
18725 any_need_reprocess = true;
18726 }
18727
18728 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18729 if (attr != nullptr && attr->form_is_unsigned ())
18730 cu->str_offsets_base = attr->as_unsigned ();
18731
18732 attr = die->attr (DW_AT_loclists_base);
18733 if (attr != nullptr)
18734 cu->loclist_base = attr->as_unsigned ();
18735
18736 auto maybe_addr_base = die->addr_base ();
18737 if (maybe_addr_base.has_value ())
18738 cu->addr_base = *maybe_addr_base;
18739
18740 attr = die->attr (DW_AT_rnglists_base);
18741 if (attr != nullptr)
18742 cu->rnglists_base = attr->as_unsigned ();
18743
18744 if (any_need_reprocess)
18745 {
18746 for (i = 0; i < abbrev->num_attrs; ++i)
18747 {
18748 if (die->attrs[i].requires_reprocessing_p ())
18749 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18750 }
18751 }
18752 *diep = die;
18753 return info_ptr;
18754 }
18755
18756 /* Read a die and all its attributes.
18757 Set DIEP to point to a newly allocated die with its information,
18758 except for its child, sibling, and parent fields. */
18759
18760 static const gdb_byte *
18761 read_full_die (const struct die_reader_specs *reader,
18762 struct die_info **diep, const gdb_byte *info_ptr)
18763 {
18764 const gdb_byte *result;
18765
18766 result = read_full_die_1 (reader, diep, info_ptr, 0);
18767
18768 if (dwarf_die_debug)
18769 {
18770 fprintf_unfiltered (gdb_stdlog,
18771 "Read die from %s@0x%x of %s:\n",
18772 reader->die_section->get_name (),
18773 (unsigned) (info_ptr - reader->die_section->buffer),
18774 bfd_get_filename (reader->abfd));
18775 dump_die (*diep, dwarf_die_debug);
18776 }
18777
18778 return result;
18779 }
18780 \f
18781
18782 /* Returns nonzero if TAG represents a type that we might generate a partial
18783 symbol for. */
18784
18785 static int
18786 is_type_tag_for_partial (int tag, enum language lang)
18787 {
18788 switch (tag)
18789 {
18790 #if 0
18791 /* Some types that would be reasonable to generate partial symbols for,
18792 that we don't at present. Note that normally this does not
18793 matter, mainly because C compilers don't give names to these
18794 types, but instead emit DW_TAG_typedef. */
18795 case DW_TAG_file_type:
18796 case DW_TAG_ptr_to_member_type:
18797 case DW_TAG_set_type:
18798 case DW_TAG_string_type:
18799 case DW_TAG_subroutine_type:
18800 #endif
18801
18802 /* GNAT may emit an array with a name, but no typedef, so we
18803 need to make a symbol in this case. */
18804 case DW_TAG_array_type:
18805 return lang == language_ada;
18806
18807 case DW_TAG_base_type:
18808 case DW_TAG_class_type:
18809 case DW_TAG_interface_type:
18810 case DW_TAG_enumeration_type:
18811 case DW_TAG_structure_type:
18812 case DW_TAG_subrange_type:
18813 case DW_TAG_typedef:
18814 case DW_TAG_union_type:
18815 return 1;
18816 default:
18817 return 0;
18818 }
18819 }
18820
18821 /* Load all DIEs that are interesting for partial symbols into memory. */
18822
18823 static struct partial_die_info *
18824 load_partial_dies (const struct die_reader_specs *reader,
18825 const gdb_byte *info_ptr, int building_psymtab)
18826 {
18827 struct dwarf2_cu *cu = reader->cu;
18828 struct objfile *objfile = cu->per_objfile->objfile;
18829 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18830 unsigned int bytes_read;
18831 unsigned int load_all = 0;
18832 int nesting_level = 1;
18833
18834 parent_die = NULL;
18835 last_die = NULL;
18836
18837 gdb_assert (cu->per_cu != NULL);
18838 if (cu->load_all_dies)
18839 load_all = 1;
18840
18841 cu->partial_dies
18842 = htab_create_alloc_ex (cu->header.length / 12,
18843 partial_die_hash,
18844 partial_die_eq,
18845 NULL,
18846 &cu->comp_unit_obstack,
18847 hashtab_obstack_allocate,
18848 dummy_obstack_deallocate);
18849
18850 while (1)
18851 {
18852 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18853 &bytes_read);
18854
18855 /* A NULL abbrev means the end of a series of children. */
18856 if (abbrev == NULL)
18857 {
18858 if (--nesting_level == 0)
18859 return first_die;
18860
18861 info_ptr += bytes_read;
18862 last_die = parent_die;
18863 parent_die = parent_die->die_parent;
18864 continue;
18865 }
18866
18867 /* Check for template arguments. We never save these; if
18868 they're seen, we just mark the parent, and go on our way. */
18869 if (parent_die != NULL
18870 && cu->per_cu->lang == language_cplus
18871 && (abbrev->tag == DW_TAG_template_type_param
18872 || abbrev->tag == DW_TAG_template_value_param))
18873 {
18874 parent_die->has_template_arguments = 1;
18875
18876 if (!load_all)
18877 {
18878 /* We don't need a partial DIE for the template argument. */
18879 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18880 continue;
18881 }
18882 }
18883
18884 /* We only recurse into c++ subprograms looking for template arguments.
18885 Skip their other children. */
18886 if (!load_all
18887 && cu->per_cu->lang == language_cplus
18888 && parent_die != NULL
18889 && parent_die->tag == DW_TAG_subprogram
18890 && abbrev->tag != DW_TAG_inlined_subroutine)
18891 {
18892 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18893 continue;
18894 }
18895
18896 /* Check whether this DIE is interesting enough to save. Normally
18897 we would not be interested in members here, but there may be
18898 later variables referencing them via DW_AT_specification (for
18899 static members). */
18900 if (!load_all
18901 && !is_type_tag_for_partial (abbrev->tag, cu->per_cu->lang)
18902 && abbrev->tag != DW_TAG_constant
18903 && abbrev->tag != DW_TAG_enumerator
18904 && abbrev->tag != DW_TAG_subprogram
18905 && abbrev->tag != DW_TAG_inlined_subroutine
18906 && abbrev->tag != DW_TAG_lexical_block
18907 && abbrev->tag != DW_TAG_variable
18908 && abbrev->tag != DW_TAG_namespace
18909 && abbrev->tag != DW_TAG_module
18910 && abbrev->tag != DW_TAG_member
18911 && abbrev->tag != DW_TAG_imported_unit
18912 && abbrev->tag != DW_TAG_imported_declaration)
18913 {
18914 /* Otherwise we skip to the next sibling, if any. */
18915 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18916 continue;
18917 }
18918
18919 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18920 abbrev);
18921
18922 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18923
18924 /* This two-pass algorithm for processing partial symbols has a
18925 high cost in cache pressure. Thus, handle some simple cases
18926 here which cover the majority of C partial symbols. DIEs
18927 which neither have specification tags in them, nor could have
18928 specification tags elsewhere pointing at them, can simply be
18929 processed and discarded.
18930
18931 This segment is also optional; scan_partial_symbols and
18932 add_partial_symbol will handle these DIEs if we chain
18933 them in normally. When compilers which do not emit large
18934 quantities of duplicate debug information are more common,
18935 this code can probably be removed. */
18936
18937 /* Any complete simple types at the top level (pretty much all
18938 of them, for a language without namespaces), can be processed
18939 directly. */
18940 if (parent_die == NULL
18941 && pdi.has_specification == 0
18942 && pdi.is_declaration == 0
18943 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18944 || pdi.tag == DW_TAG_base_type
18945 || pdi.tag == DW_TAG_array_type
18946 || pdi.tag == DW_TAG_subrange_type))
18947 {
18948 if (building_psymtab && pdi.raw_name != NULL)
18949 add_partial_symbol (&pdi, cu);
18950
18951 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18952 continue;
18953 }
18954
18955 /* The exception for DW_TAG_typedef with has_children above is
18956 a workaround of GCC PR debug/47510. In the case of this complaint
18957 type_name_or_error will error on such types later.
18958
18959 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18960 it could not find the child DIEs referenced later, this is checked
18961 above. In correct DWARF DW_TAG_typedef should have no children. */
18962
18963 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18964 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18965 "- DIE at %s [in module %s]"),
18966 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18967
18968 /* If we're at the second level, and we're an enumerator, and
18969 our parent has no specification (meaning possibly lives in a
18970 namespace elsewhere), then we can add the partial symbol now
18971 instead of queueing it. */
18972 if (pdi.tag == DW_TAG_enumerator
18973 && parent_die != NULL
18974 && parent_die->die_parent == NULL
18975 && parent_die->tag == DW_TAG_enumeration_type
18976 && parent_die->has_specification == 0)
18977 {
18978 if (pdi.raw_name == NULL)
18979 complaint (_("malformed enumerator DIE ignored"));
18980 else if (building_psymtab)
18981 add_partial_symbol (&pdi, cu);
18982
18983 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18984 continue;
18985 }
18986
18987 struct partial_die_info *part_die
18988 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18989
18990 /* We'll save this DIE so link it in. */
18991 part_die->die_parent = parent_die;
18992 part_die->die_sibling = NULL;
18993 part_die->die_child = NULL;
18994
18995 if (last_die && last_die == parent_die)
18996 last_die->die_child = part_die;
18997 else if (last_die)
18998 last_die->die_sibling = part_die;
18999
19000 last_die = part_die;
19001
19002 if (first_die == NULL)
19003 first_die = part_die;
19004
19005 /* Maybe add the DIE to the hash table. Not all DIEs that we
19006 find interesting need to be in the hash table, because we
19007 also have the parent/sibling/child chains; only those that we
19008 might refer to by offset later during partial symbol reading.
19009
19010 For now this means things that might have be the target of a
19011 DW_AT_specification, DW_AT_abstract_origin, or
19012 DW_AT_extension. DW_AT_extension will refer only to
19013 namespaces; DW_AT_abstract_origin refers to functions (and
19014 many things under the function DIE, but we do not recurse
19015 into function DIEs during partial symbol reading) and
19016 possibly variables as well; DW_AT_specification refers to
19017 declarations. Declarations ought to have the DW_AT_declaration
19018 flag. It happens that GCC forgets to put it in sometimes, but
19019 only for functions, not for types.
19020
19021 Adding more things than necessary to the hash table is harmless
19022 except for the performance cost. Adding too few will result in
19023 wasted time in find_partial_die, when we reread the compilation
19024 unit with load_all_dies set. */
19025
19026 if (load_all
19027 || abbrev->tag == DW_TAG_constant
19028 || abbrev->tag == DW_TAG_subprogram
19029 || abbrev->tag == DW_TAG_variable
19030 || abbrev->tag == DW_TAG_namespace
19031 || part_die->is_declaration)
19032 {
19033 void **slot;
19034
19035 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19036 to_underlying (part_die->sect_off),
19037 INSERT);
19038 *slot = part_die;
19039 }
19040
19041 /* For some DIEs we want to follow their children (if any). For C
19042 we have no reason to follow the children of structures; for other
19043 languages we have to, so that we can get at method physnames
19044 to infer fully qualified class names, for DW_AT_specification,
19045 and for C++ template arguments. For C++, we also look one level
19046 inside functions to find template arguments (if the name of the
19047 function does not already contain the template arguments).
19048
19049 For Ada and Fortran, we need to scan the children of subprograms
19050 and lexical blocks as well because these languages allow the
19051 definition of nested entities that could be interesting for the
19052 debugger, such as nested subprograms for instance. */
19053 if (last_die->has_children
19054 && (load_all
19055 || last_die->tag == DW_TAG_namespace
19056 || last_die->tag == DW_TAG_module
19057 || last_die->tag == DW_TAG_enumeration_type
19058 || (cu->per_cu->lang == language_cplus
19059 && last_die->tag == DW_TAG_subprogram
19060 && (last_die->raw_name == NULL
19061 || strchr (last_die->raw_name, '<') == NULL))
19062 || (cu->per_cu->lang != language_c
19063 && (last_die->tag == DW_TAG_class_type
19064 || last_die->tag == DW_TAG_interface_type
19065 || last_die->tag == DW_TAG_structure_type
19066 || last_die->tag == DW_TAG_union_type))
19067 || ((cu->per_cu->lang == language_ada
19068 || cu->per_cu->lang == language_fortran)
19069 && (last_die->tag == DW_TAG_subprogram
19070 || last_die->tag == DW_TAG_lexical_block))))
19071 {
19072 nesting_level++;
19073 parent_die = last_die;
19074 continue;
19075 }
19076
19077 /* Otherwise we skip to the next sibling, if any. */
19078 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19079
19080 /* Back to the top, do it again. */
19081 }
19082 }
19083
19084 partial_die_info::partial_die_info (sect_offset sect_off_,
19085 const struct abbrev_info *abbrev)
19086 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19087 {
19088 }
19089
19090 /* See class definition. */
19091
19092 const char *
19093 partial_die_info::name (dwarf2_cu *cu)
19094 {
19095 if (!canonical_name && raw_name != nullptr)
19096 {
19097 struct objfile *objfile = cu->per_objfile->objfile;
19098 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19099 canonical_name = 1;
19100 }
19101
19102 return raw_name;
19103 }
19104
19105 /* Read a minimal amount of information into the minimal die structure.
19106 INFO_PTR should point just after the initial uleb128 of a DIE. */
19107
19108 const gdb_byte *
19109 partial_die_info::read (const struct die_reader_specs *reader,
19110 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19111 {
19112 struct dwarf2_cu *cu = reader->cu;
19113 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19114 unsigned int i;
19115 int has_low_pc_attr = 0;
19116 int has_high_pc_attr = 0;
19117 int high_pc_relative = 0;
19118
19119 for (i = 0; i < abbrev.num_attrs; ++i)
19120 {
19121 attribute attr;
19122 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19123 /* String and address offsets that need to do the reprocessing have
19124 already been read at this point, so there is no need to wait until
19125 the loop terminates to do the reprocessing. */
19126 if (attr.requires_reprocessing_p ())
19127 read_attribute_reprocess (reader, &attr, tag);
19128 /* Store the data if it is of an attribute we want to keep in a
19129 partial symbol table. */
19130 switch (attr.name)
19131 {
19132 case DW_AT_name:
19133 switch (tag)
19134 {
19135 case DW_TAG_compile_unit:
19136 case DW_TAG_partial_unit:
19137 case DW_TAG_type_unit:
19138 /* Compilation units have a DW_AT_name that is a filename, not
19139 a source language identifier. */
19140 case DW_TAG_enumeration_type:
19141 case DW_TAG_enumerator:
19142 /* These tags always have simple identifiers already; no need
19143 to canonicalize them. */
19144 canonical_name = 1;
19145 raw_name = attr.as_string ();
19146 break;
19147 default:
19148 canonical_name = 0;
19149 raw_name = attr.as_string ();
19150 break;
19151 }
19152 break;
19153 case DW_AT_linkage_name:
19154 case DW_AT_MIPS_linkage_name:
19155 /* Note that both forms of linkage name might appear. We
19156 assume they will be the same, and we only store the last
19157 one we see. */
19158 linkage_name = attr.as_string ();
19159 break;
19160 case DW_AT_low_pc:
19161 has_low_pc_attr = 1;
19162 lowpc = attr.as_address ();
19163 break;
19164 case DW_AT_high_pc:
19165 has_high_pc_attr = 1;
19166 highpc = attr.as_address ();
19167 if (cu->header.version >= 4 && attr.form_is_constant ())
19168 high_pc_relative = 1;
19169 break;
19170 case DW_AT_location:
19171 /* Support the .debug_loc offsets. */
19172 if (attr.form_is_block ())
19173 {
19174 d.locdesc = attr.as_block ();
19175 }
19176 else if (attr.form_is_section_offset ())
19177 {
19178 dwarf2_complex_location_expr_complaint ();
19179 }
19180 else
19181 {
19182 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19183 "partial symbol information");
19184 }
19185 break;
19186 case DW_AT_external:
19187 is_external = attr.as_boolean ();
19188 break;
19189 case DW_AT_declaration:
19190 is_declaration = attr.as_boolean ();
19191 break;
19192 case DW_AT_type:
19193 has_type = 1;
19194 break;
19195 case DW_AT_abstract_origin:
19196 case DW_AT_specification:
19197 case DW_AT_extension:
19198 has_specification = 1;
19199 spec_offset = attr.get_ref_die_offset ();
19200 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19201 || cu->per_cu->is_dwz);
19202 break;
19203 case DW_AT_sibling:
19204 /* Ignore absolute siblings, they might point outside of
19205 the current compile unit. */
19206 if (attr.form == DW_FORM_ref_addr)
19207 complaint (_("ignoring absolute DW_AT_sibling"));
19208 else
19209 {
19210 const gdb_byte *buffer = reader->buffer;
19211 sect_offset off = attr.get_ref_die_offset ();
19212 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19213
19214 if (sibling_ptr < info_ptr)
19215 complaint (_("DW_AT_sibling points backwards"));
19216 else if (sibling_ptr > reader->buffer_end)
19217 reader->die_section->overflow_complaint ();
19218 else
19219 sibling = sibling_ptr;
19220 }
19221 break;
19222 case DW_AT_byte_size:
19223 has_byte_size = 1;
19224 break;
19225 case DW_AT_const_value:
19226 has_const_value = 1;
19227 break;
19228 case DW_AT_calling_convention:
19229 /* DWARF doesn't provide a way to identify a program's source-level
19230 entry point. DW_AT_calling_convention attributes are only meant
19231 to describe functions' calling conventions.
19232
19233 However, because it's a necessary piece of information in
19234 Fortran, and before DWARF 4 DW_CC_program was the only
19235 piece of debugging information whose definition refers to
19236 a 'main program' at all, several compilers marked Fortran
19237 main programs with DW_CC_program --- even when those
19238 functions use the standard calling conventions.
19239
19240 Although DWARF now specifies a way to provide this
19241 information, we support this practice for backward
19242 compatibility. */
19243 if (attr.constant_value (0) == DW_CC_program
19244 && cu->per_cu->lang == language_fortran)
19245 main_subprogram = 1;
19246 break;
19247 case DW_AT_inline:
19248 {
19249 LONGEST value = attr.constant_value (-1);
19250 if (value == DW_INL_inlined
19251 || value == DW_INL_declared_inlined)
19252 may_be_inlined = 1;
19253 }
19254 break;
19255
19256 case DW_AT_import:
19257 if (tag == DW_TAG_imported_unit)
19258 {
19259 d.sect_off = attr.get_ref_die_offset ();
19260 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19261 || cu->per_cu->is_dwz);
19262 }
19263 break;
19264
19265 case DW_AT_main_subprogram:
19266 main_subprogram = attr.as_boolean ();
19267 break;
19268
19269 case DW_AT_ranges:
19270 {
19271 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19272 on DWARF version). */
19273 ULONGEST ranges_offset = attr.as_unsigned ();
19274
19275 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19276 this value. */
19277 if (tag != DW_TAG_compile_unit)
19278 ranges_offset += cu->gnu_ranges_base;
19279
19280 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19281 nullptr, tag))
19282 has_pc_info = 1;
19283 }
19284 break;
19285
19286 default:
19287 break;
19288 }
19289 }
19290
19291 /* For Ada, if both the name and the linkage name appear, we prefer
19292 the latter. This lets "catch exception" work better, regardless
19293 of the order in which the name and linkage name were emitted.
19294 Really, though, this is just a workaround for the fact that gdb
19295 doesn't store both the name and the linkage name. */
19296 if (cu->per_cu->lang == language_ada && linkage_name != nullptr)
19297 raw_name = linkage_name;
19298
19299 if (high_pc_relative)
19300 highpc += lowpc;
19301
19302 if (has_low_pc_attr && has_high_pc_attr)
19303 {
19304 /* When using the GNU linker, .gnu.linkonce. sections are used to
19305 eliminate duplicate copies of functions and vtables and such.
19306 The linker will arbitrarily choose one and discard the others.
19307 The AT_*_pc values for such functions refer to local labels in
19308 these sections. If the section from that file was discarded, the
19309 labels are not in the output, so the relocs get a value of 0.
19310 If this is a discarded function, mark the pc bounds as invalid,
19311 so that GDB will ignore it. */
19312 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19313 {
19314 struct objfile *objfile = per_objfile->objfile;
19315 struct gdbarch *gdbarch = objfile->arch ();
19316
19317 complaint (_("DW_AT_low_pc %s is zero "
19318 "for DIE at %s [in module %s]"),
19319 paddress (gdbarch, lowpc),
19320 sect_offset_str (sect_off),
19321 objfile_name (objfile));
19322 }
19323 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19324 else if (lowpc >= highpc)
19325 {
19326 struct objfile *objfile = per_objfile->objfile;
19327 struct gdbarch *gdbarch = objfile->arch ();
19328
19329 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19330 "for DIE at %s [in module %s]"),
19331 paddress (gdbarch, lowpc),
19332 paddress (gdbarch, highpc),
19333 sect_offset_str (sect_off),
19334 objfile_name (objfile));
19335 }
19336 else
19337 has_pc_info = 1;
19338 }
19339
19340 return info_ptr;
19341 }
19342
19343 /* Find a cached partial DIE at OFFSET in CU. */
19344
19345 struct partial_die_info *
19346 dwarf2_cu::find_partial_die (sect_offset sect_off)
19347 {
19348 struct partial_die_info *lookup_die = NULL;
19349 struct partial_die_info part_die (sect_off);
19350
19351 lookup_die = ((struct partial_die_info *)
19352 htab_find_with_hash (partial_dies, &part_die,
19353 to_underlying (sect_off)));
19354
19355 return lookup_die;
19356 }
19357
19358 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19359 except in the case of .debug_types DIEs which do not reference
19360 outside their CU (they do however referencing other types via
19361 DW_FORM_ref_sig8). */
19362
19363 static const struct cu_partial_die_info
19364 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19365 {
19366 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19367 struct objfile *objfile = per_objfile->objfile;
19368 struct partial_die_info *pd = NULL;
19369
19370 if (offset_in_dwz == cu->per_cu->is_dwz
19371 && cu->header.offset_in_cu_p (sect_off))
19372 {
19373 pd = cu->find_partial_die (sect_off);
19374 if (pd != NULL)
19375 return { cu, pd };
19376 /* We missed recording what we needed.
19377 Load all dies and try again. */
19378 }
19379 else
19380 {
19381 /* TUs don't reference other CUs/TUs (except via type signatures). */
19382 if (cu->per_cu->is_debug_types)
19383 {
19384 error (_("Dwarf Error: Type Unit at offset %s contains"
19385 " external reference to offset %s [in module %s].\n"),
19386 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19387 bfd_get_filename (objfile->obfd));
19388 }
19389 dwarf2_per_cu_data *per_cu
19390 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19391 per_objfile);
19392
19393 cu = per_objfile->get_cu (per_cu);
19394 if (cu == NULL || cu->partial_dies == NULL)
19395 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19396
19397 cu = per_objfile->get_cu (per_cu);
19398
19399 cu->last_used = 0;
19400 pd = cu->find_partial_die (sect_off);
19401 }
19402
19403 /* If we didn't find it, and not all dies have been loaded,
19404 load them all and try again. */
19405
19406 if (pd == NULL && cu->load_all_dies == 0)
19407 {
19408 cu->load_all_dies = 1;
19409
19410 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19411 THIS_CU->cu may already be in use. So we can't just free it and
19412 replace its DIEs with the ones we read in. Instead, we leave those
19413 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19414 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19415 set. */
19416 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19417
19418 pd = cu->find_partial_die (sect_off);
19419 }
19420
19421 if (pd == NULL)
19422 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
19423 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19424 return { cu, pd };
19425 }
19426
19427 /* See if we can figure out if the class lives in a namespace. We do
19428 this by looking for a member function; its demangled name will
19429 contain namespace info, if there is any. */
19430
19431 static void
19432 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19433 struct dwarf2_cu *cu)
19434 {
19435 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19436 what template types look like, because the demangler
19437 frequently doesn't give the same name as the debug info. We
19438 could fix this by only using the demangled name to get the
19439 prefix (but see comment in read_structure_type). */
19440
19441 struct partial_die_info *real_pdi;
19442 struct partial_die_info *child_pdi;
19443
19444 /* If this DIE (this DIE's specification, if any) has a parent, then
19445 we should not do this. We'll prepend the parent's fully qualified
19446 name when we create the partial symbol. */
19447
19448 real_pdi = struct_pdi;
19449 while (real_pdi->has_specification)
19450 {
19451 auto res = find_partial_die (real_pdi->spec_offset,
19452 real_pdi->spec_is_dwz, cu);
19453 real_pdi = res.pdi;
19454 cu = res.cu;
19455 }
19456
19457 if (real_pdi->die_parent != NULL)
19458 return;
19459
19460 for (child_pdi = struct_pdi->die_child;
19461 child_pdi != NULL;
19462 child_pdi = child_pdi->die_sibling)
19463 {
19464 if (child_pdi->tag == DW_TAG_subprogram
19465 && child_pdi->linkage_name != NULL)
19466 {
19467 gdb::unique_xmalloc_ptr<char> actual_class_name
19468 (cu->language_defn->class_name_from_physname
19469 (child_pdi->linkage_name));
19470 if (actual_class_name != NULL)
19471 {
19472 struct objfile *objfile = cu->per_objfile->objfile;
19473 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19474 struct_pdi->canonical_name = 1;
19475 }
19476 break;
19477 }
19478 }
19479 }
19480
19481 /* Return true if a DIE with TAG may have the DW_AT_const_value
19482 attribute. */
19483
19484 static bool
19485 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19486 {
19487 switch (tag)
19488 {
19489 case DW_TAG_constant:
19490 case DW_TAG_enumerator:
19491 case DW_TAG_formal_parameter:
19492 case DW_TAG_template_value_param:
19493 case DW_TAG_variable:
19494 return true;
19495 }
19496
19497 return false;
19498 }
19499
19500 void
19501 partial_die_info::fixup (struct dwarf2_cu *cu)
19502 {
19503 /* Once we've fixed up a die, there's no point in doing so again.
19504 This also avoids a memory leak if we were to call
19505 guess_partial_die_structure_name multiple times. */
19506 if (fixup_called)
19507 return;
19508
19509 /* If we found a reference attribute and the DIE has no name, try
19510 to find a name in the referred to DIE. */
19511
19512 if (raw_name == NULL && has_specification)
19513 {
19514 struct partial_die_info *spec_die;
19515
19516 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19517 spec_die = res.pdi;
19518 cu = res.cu;
19519
19520 spec_die->fixup (cu);
19521
19522 if (spec_die->raw_name)
19523 {
19524 raw_name = spec_die->raw_name;
19525 canonical_name = spec_die->canonical_name;
19526
19527 /* Copy DW_AT_external attribute if it is set. */
19528 if (spec_die->is_external)
19529 is_external = spec_die->is_external;
19530 }
19531 }
19532
19533 if (!has_const_value && has_specification
19534 && can_have_DW_AT_const_value_p (tag))
19535 {
19536 struct partial_die_info *spec_die;
19537
19538 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19539 spec_die = res.pdi;
19540 cu = res.cu;
19541
19542 spec_die->fixup (cu);
19543
19544 if (spec_die->has_const_value)
19545 {
19546 /* Copy DW_AT_const_value attribute if it is set. */
19547 has_const_value = spec_die->has_const_value;
19548 }
19549 }
19550
19551 /* Set default names for some unnamed DIEs. */
19552
19553 if (raw_name == NULL && tag == DW_TAG_namespace)
19554 {
19555 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19556 canonical_name = 1;
19557 }
19558
19559 /* If there is no parent die to provide a namespace, and there are
19560 children, see if we can determine the namespace from their linkage
19561 name. */
19562 if (cu->per_cu->lang == language_cplus
19563 && !cu->per_objfile->per_bfd->types.empty ()
19564 && die_parent == NULL
19565 && has_children
19566 && (tag == DW_TAG_class_type
19567 || tag == DW_TAG_structure_type
19568 || tag == DW_TAG_union_type))
19569 guess_partial_die_structure_name (this, cu);
19570
19571 /* GCC might emit a nameless struct or union that has a linkage
19572 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19573 if (raw_name == NULL
19574 && (tag == DW_TAG_class_type
19575 || tag == DW_TAG_interface_type
19576 || tag == DW_TAG_structure_type
19577 || tag == DW_TAG_union_type)
19578 && linkage_name != NULL)
19579 {
19580 gdb::unique_xmalloc_ptr<char> demangled
19581 (gdb_demangle (linkage_name, DMGL_TYPES));
19582 if (demangled != nullptr)
19583 {
19584 const char *base;
19585
19586 /* Strip any leading namespaces/classes, keep only the base name.
19587 DW_AT_name for named DIEs does not contain the prefixes. */
19588 base = strrchr (demangled.get (), ':');
19589 if (base && base > demangled.get () && base[-1] == ':')
19590 base++;
19591 else
19592 base = demangled.get ();
19593
19594 struct objfile *objfile = cu->per_objfile->objfile;
19595 raw_name = objfile->intern (base);
19596 canonical_name = 1;
19597 }
19598 }
19599
19600 fixup_called = 1;
19601 }
19602
19603 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19604 contents from the given SECTION in the HEADER.
19605
19606 HEADER_OFFSET is the offset of the header in the section. */
19607 static void
19608 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19609 struct dwarf2_section_info *section,
19610 sect_offset header_offset)
19611 {
19612 unsigned int bytes_read;
19613 bfd *abfd = section->get_bfd_owner ();
19614 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19615
19616 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19617 info_ptr += bytes_read;
19618
19619 header->version = read_2_bytes (abfd, info_ptr);
19620 info_ptr += 2;
19621
19622 header->addr_size = read_1_byte (abfd, info_ptr);
19623 info_ptr += 1;
19624
19625 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19626 info_ptr += 1;
19627
19628 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19629 }
19630
19631 /* Return the DW_AT_loclists_base value for the CU. */
19632 static ULONGEST
19633 lookup_loclist_base (struct dwarf2_cu *cu)
19634 {
19635 /* For the .dwo unit, the loclist_base points to the first offset following
19636 the header. The header consists of the following entities-
19637 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19638 bit format)
19639 2. version (2 bytes)
19640 3. address size (1 byte)
19641 4. segment selector size (1 byte)
19642 5. offset entry count (4 bytes)
19643 These sizes are derived as per the DWARFv5 standard. */
19644 if (cu->dwo_unit != nullptr)
19645 {
19646 if (cu->header.initial_length_size == 4)
19647 return LOCLIST_HEADER_SIZE32;
19648 return LOCLIST_HEADER_SIZE64;
19649 }
19650 return cu->loclist_base;
19651 }
19652
19653 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19654 array of offsets in the .debug_loclists section. */
19655
19656 static sect_offset
19657 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19658 {
19659 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19660 struct objfile *objfile = per_objfile->objfile;
19661 bfd *abfd = objfile->obfd;
19662 ULONGEST loclist_header_size =
19663 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19664 : LOCLIST_HEADER_SIZE64);
19665 ULONGEST loclist_base = lookup_loclist_base (cu);
19666
19667 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19668 ULONGEST start_offset =
19669 loclist_base + loclist_index * cu->header.offset_size;
19670
19671 /* Get loclists section. */
19672 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19673
19674 /* Read the loclists section content. */
19675 section->read (objfile);
19676 if (section->buffer == NULL)
19677 error (_("DW_FORM_loclistx used without .debug_loclists "
19678 "section [in module %s]"), objfile_name (objfile));
19679
19680 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19681 so if loclist_base is smaller than the header size, we have a problem. */
19682 if (loclist_base < loclist_header_size)
19683 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19684 objfile_name (objfile));
19685
19686 /* Read the header of the loclists contribution. */
19687 struct loclists_rnglists_header header;
19688 read_loclists_rnglists_header (&header, section,
19689 (sect_offset) (loclist_base - loclist_header_size));
19690
19691 /* Verify the loclist index is valid. */
19692 if (loclist_index >= header.offset_entry_count)
19693 error (_("DW_FORM_loclistx pointing outside of "
19694 ".debug_loclists offset array [in module %s]"),
19695 objfile_name (objfile));
19696
19697 /* Validate that reading won't go beyond the end of the section. */
19698 if (start_offset + cu->header.offset_size > section->size)
19699 error (_("Reading DW_FORM_loclistx index beyond end of"
19700 ".debug_loclists section [in module %s]"),
19701 objfile_name (objfile));
19702
19703 const gdb_byte *info_ptr = section->buffer + start_offset;
19704
19705 if (cu->header.offset_size == 4)
19706 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19707 else
19708 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19709 }
19710
19711 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19712 array of offsets in the .debug_rnglists section. */
19713
19714 static sect_offset
19715 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19716 dwarf_tag tag)
19717 {
19718 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19719 struct objfile *objfile = dwarf2_per_objfile->objfile;
19720 bfd *abfd = objfile->obfd;
19721 ULONGEST rnglist_header_size =
19722 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19723 : RNGLIST_HEADER_SIZE64);
19724
19725 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19726 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19727 doesn't apply. */
19728 ULONGEST rnglist_base =
19729 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19730
19731 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19732 ULONGEST start_offset =
19733 rnglist_base + rnglist_index * cu->header.offset_size;
19734
19735 /* Get rnglists section. */
19736 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19737
19738 /* Read the rnglists section content. */
19739 section->read (objfile);
19740 if (section->buffer == nullptr)
19741 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19742 "[in module %s]"),
19743 objfile_name (objfile));
19744
19745 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19746 so if rnglist_base is smaller than the header size, we have a problem. */
19747 if (rnglist_base < rnglist_header_size)
19748 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19749 objfile_name (objfile));
19750
19751 /* Read the header of the rnglists contribution. */
19752 struct loclists_rnglists_header header;
19753 read_loclists_rnglists_header (&header, section,
19754 (sect_offset) (rnglist_base - rnglist_header_size));
19755
19756 /* Verify the rnglist index is valid. */
19757 if (rnglist_index >= header.offset_entry_count)
19758 error (_("DW_FORM_rnglistx index pointing outside of "
19759 ".debug_rnglists offset array [in module %s]"),
19760 objfile_name (objfile));
19761
19762 /* Validate that reading won't go beyond the end of the section. */
19763 if (start_offset + cu->header.offset_size > section->size)
19764 error (_("Reading DW_FORM_rnglistx index beyond end of"
19765 ".debug_rnglists section [in module %s]"),
19766 objfile_name (objfile));
19767
19768 const gdb_byte *info_ptr = section->buffer + start_offset;
19769
19770 if (cu->header.offset_size == 4)
19771 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19772 else
19773 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19774 }
19775
19776 /* Process the attributes that had to be skipped in the first round. These
19777 attributes are the ones that need str_offsets_base or addr_base attributes.
19778 They could not have been processed in the first round, because at the time
19779 the values of str_offsets_base or addr_base may not have been known. */
19780 static void
19781 read_attribute_reprocess (const struct die_reader_specs *reader,
19782 struct attribute *attr, dwarf_tag tag)
19783 {
19784 struct dwarf2_cu *cu = reader->cu;
19785 switch (attr->form)
19786 {
19787 case DW_FORM_addrx:
19788 case DW_FORM_GNU_addr_index:
19789 attr->set_address (read_addr_index (cu,
19790 attr->as_unsigned_reprocess ()));
19791 break;
19792 case DW_FORM_loclistx:
19793 {
19794 sect_offset loclists_sect_off
19795 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19796
19797 attr->set_unsigned (to_underlying (loclists_sect_off));
19798 }
19799 break;
19800 case DW_FORM_rnglistx:
19801 {
19802 sect_offset rnglists_sect_off
19803 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19804
19805 attr->set_unsigned (to_underlying (rnglists_sect_off));
19806 }
19807 break;
19808 case DW_FORM_strx:
19809 case DW_FORM_strx1:
19810 case DW_FORM_strx2:
19811 case DW_FORM_strx3:
19812 case DW_FORM_strx4:
19813 case DW_FORM_GNU_str_index:
19814 {
19815 unsigned int str_index = attr->as_unsigned_reprocess ();
19816 gdb_assert (!attr->canonical_string_p ());
19817 if (reader->dwo_file != NULL)
19818 attr->set_string_noncanonical (read_dwo_str_index (reader,
19819 str_index));
19820 else
19821 attr->set_string_noncanonical (read_stub_str_index (cu,
19822 str_index));
19823 break;
19824 }
19825 default:
19826 gdb_assert_not_reached (_("Unexpected DWARF form."));
19827 }
19828 }
19829
19830 /* Read an attribute value described by an attribute form. */
19831
19832 static const gdb_byte *
19833 read_attribute_value (const struct die_reader_specs *reader,
19834 struct attribute *attr, unsigned form,
19835 LONGEST implicit_const, const gdb_byte *info_ptr)
19836 {
19837 struct dwarf2_cu *cu = reader->cu;
19838 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19839 struct objfile *objfile = per_objfile->objfile;
19840 bfd *abfd = reader->abfd;
19841 struct comp_unit_head *cu_header = &cu->header;
19842 unsigned int bytes_read;
19843 struct dwarf_block *blk;
19844
19845 attr->form = (enum dwarf_form) form;
19846 switch (form)
19847 {
19848 case DW_FORM_ref_addr:
19849 if (cu_header->version == 2)
19850 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
19851 &bytes_read));
19852 else
19853 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19854 &bytes_read));
19855 info_ptr += bytes_read;
19856 break;
19857 case DW_FORM_GNU_ref_alt:
19858 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19859 &bytes_read));
19860 info_ptr += bytes_read;
19861 break;
19862 case DW_FORM_addr:
19863 {
19864 struct gdbarch *gdbarch = objfile->arch ();
19865 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
19866 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19867 attr->set_address (addr);
19868 info_ptr += bytes_read;
19869 }
19870 break;
19871 case DW_FORM_block2:
19872 blk = dwarf_alloc_block (cu);
19873 blk->size = read_2_bytes (abfd, info_ptr);
19874 info_ptr += 2;
19875 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19876 info_ptr += blk->size;
19877 attr->set_block (blk);
19878 break;
19879 case DW_FORM_block4:
19880 blk = dwarf_alloc_block (cu);
19881 blk->size = read_4_bytes (abfd, info_ptr);
19882 info_ptr += 4;
19883 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19884 info_ptr += blk->size;
19885 attr->set_block (blk);
19886 break;
19887 case DW_FORM_data2:
19888 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
19889 info_ptr += 2;
19890 break;
19891 case DW_FORM_data4:
19892 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
19893 info_ptr += 4;
19894 break;
19895 case DW_FORM_data8:
19896 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
19897 info_ptr += 8;
19898 break;
19899 case DW_FORM_data16:
19900 blk = dwarf_alloc_block (cu);
19901 blk->size = 16;
19902 blk->data = read_n_bytes (abfd, info_ptr, 16);
19903 info_ptr += 16;
19904 attr->set_block (blk);
19905 break;
19906 case DW_FORM_sec_offset:
19907 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19908 &bytes_read));
19909 info_ptr += bytes_read;
19910 break;
19911 case DW_FORM_loclistx:
19912 {
19913 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19914 &bytes_read));
19915 info_ptr += bytes_read;
19916 }
19917 break;
19918 case DW_FORM_string:
19919 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19920 &bytes_read));
19921 info_ptr += bytes_read;
19922 break;
19923 case DW_FORM_strp:
19924 if (!cu->per_cu->is_dwz)
19925 {
19926 attr->set_string_noncanonical
19927 (read_indirect_string (per_objfile,
19928 abfd, info_ptr, cu_header,
19929 &bytes_read));
19930 info_ptr += bytes_read;
19931 break;
19932 }
19933 /* FALLTHROUGH */
19934 case DW_FORM_line_strp:
19935 if (!cu->per_cu->is_dwz)
19936 {
19937 attr->set_string_noncanonical
19938 (per_objfile->read_line_string (info_ptr, cu_header,
19939 &bytes_read));
19940 info_ptr += bytes_read;
19941 break;
19942 }
19943 /* FALLTHROUGH */
19944 case DW_FORM_GNU_strp_alt:
19945 {
19946 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19947 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19948 &bytes_read);
19949
19950 attr->set_string_noncanonical
19951 (dwz->read_string (objfile, str_offset));
19952 info_ptr += bytes_read;
19953 }
19954 break;
19955 case DW_FORM_exprloc:
19956 case DW_FORM_block:
19957 blk = dwarf_alloc_block (cu);
19958 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19959 info_ptr += bytes_read;
19960 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19961 info_ptr += blk->size;
19962 attr->set_block (blk);
19963 break;
19964 case DW_FORM_block1:
19965 blk = dwarf_alloc_block (cu);
19966 blk->size = read_1_byte (abfd, info_ptr);
19967 info_ptr += 1;
19968 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19969 info_ptr += blk->size;
19970 attr->set_block (blk);
19971 break;
19972 case DW_FORM_data1:
19973 case DW_FORM_flag:
19974 attr->set_unsigned (read_1_byte (abfd, info_ptr));
19975 info_ptr += 1;
19976 break;
19977 case DW_FORM_flag_present:
19978 attr->set_unsigned (1);
19979 break;
19980 case DW_FORM_sdata:
19981 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
19982 info_ptr += bytes_read;
19983 break;
19984 case DW_FORM_rnglistx:
19985 {
19986 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19987 &bytes_read));
19988 info_ptr += bytes_read;
19989 }
19990 break;
19991 case DW_FORM_udata:
19992 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19993 info_ptr += bytes_read;
19994 break;
19995 case DW_FORM_ref1:
19996 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19997 + read_1_byte (abfd, info_ptr)));
19998 info_ptr += 1;
19999 break;
20000 case DW_FORM_ref2:
20001 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20002 + read_2_bytes (abfd, info_ptr)));
20003 info_ptr += 2;
20004 break;
20005 case DW_FORM_ref4:
20006 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20007 + read_4_bytes (abfd, info_ptr)));
20008 info_ptr += 4;
20009 break;
20010 case DW_FORM_ref8:
20011 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20012 + read_8_bytes (abfd, info_ptr)));
20013 info_ptr += 8;
20014 break;
20015 case DW_FORM_ref_sig8:
20016 attr->set_signature (read_8_bytes (abfd, info_ptr));
20017 info_ptr += 8;
20018 break;
20019 case DW_FORM_ref_udata:
20020 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20021 + read_unsigned_leb128 (abfd, info_ptr,
20022 &bytes_read)));
20023 info_ptr += bytes_read;
20024 break;
20025 case DW_FORM_indirect:
20026 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20027 info_ptr += bytes_read;
20028 if (form == DW_FORM_implicit_const)
20029 {
20030 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20031 info_ptr += bytes_read;
20032 }
20033 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20034 info_ptr);
20035 break;
20036 case DW_FORM_implicit_const:
20037 attr->set_signed (implicit_const);
20038 break;
20039 case DW_FORM_addrx:
20040 case DW_FORM_GNU_addr_index:
20041 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20042 &bytes_read));
20043 info_ptr += bytes_read;
20044 break;
20045 case DW_FORM_strx:
20046 case DW_FORM_strx1:
20047 case DW_FORM_strx2:
20048 case DW_FORM_strx3:
20049 case DW_FORM_strx4:
20050 case DW_FORM_GNU_str_index:
20051 {
20052 ULONGEST str_index;
20053 if (form == DW_FORM_strx1)
20054 {
20055 str_index = read_1_byte (abfd, info_ptr);
20056 info_ptr += 1;
20057 }
20058 else if (form == DW_FORM_strx2)
20059 {
20060 str_index = read_2_bytes (abfd, info_ptr);
20061 info_ptr += 2;
20062 }
20063 else if (form == DW_FORM_strx3)
20064 {
20065 str_index = read_3_bytes (abfd, info_ptr);
20066 info_ptr += 3;
20067 }
20068 else if (form == DW_FORM_strx4)
20069 {
20070 str_index = read_4_bytes (abfd, info_ptr);
20071 info_ptr += 4;
20072 }
20073 else
20074 {
20075 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20076 info_ptr += bytes_read;
20077 }
20078 attr->set_unsigned_reprocess (str_index);
20079 }
20080 break;
20081 default:
20082 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20083 dwarf_form_name (form),
20084 bfd_get_filename (abfd));
20085 }
20086
20087 /* Super hack. */
20088 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20089 attr->form = DW_FORM_GNU_ref_alt;
20090
20091 /* We have seen instances where the compiler tried to emit a byte
20092 size attribute of -1 which ended up being encoded as an unsigned
20093 0xffffffff. Although 0xffffffff is technically a valid size value,
20094 an object of this size seems pretty unlikely so we can relatively
20095 safely treat these cases as if the size attribute was invalid and
20096 treat them as zero by default. */
20097 if (attr->name == DW_AT_byte_size
20098 && form == DW_FORM_data4
20099 && attr->as_unsigned () >= 0xffffffff)
20100 {
20101 complaint
20102 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20103 hex_string (attr->as_unsigned ()));
20104 attr->set_unsigned (0);
20105 }
20106
20107 return info_ptr;
20108 }
20109
20110 /* Read an attribute described by an abbreviated attribute. */
20111
20112 static const gdb_byte *
20113 read_attribute (const struct die_reader_specs *reader,
20114 struct attribute *attr, const struct attr_abbrev *abbrev,
20115 const gdb_byte *info_ptr)
20116 {
20117 attr->name = abbrev->name;
20118 attr->string_is_canonical = 0;
20119 attr->requires_reprocessing = 0;
20120 return read_attribute_value (reader, attr, abbrev->form,
20121 abbrev->implicit_const, info_ptr);
20122 }
20123
20124 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20125
20126 static const char *
20127 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20128 LONGEST str_offset)
20129 {
20130 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20131 str_offset, "DW_FORM_strp");
20132 }
20133
20134 /* Return pointer to string at .debug_str offset as read from BUF.
20135 BUF is assumed to be in a compilation unit described by CU_HEADER.
20136 Return *BYTES_READ_PTR count of bytes read from BUF. */
20137
20138 static const char *
20139 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20140 const gdb_byte *buf,
20141 const struct comp_unit_head *cu_header,
20142 unsigned int *bytes_read_ptr)
20143 {
20144 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20145
20146 return read_indirect_string_at_offset (per_objfile, str_offset);
20147 }
20148
20149 /* See read.h. */
20150
20151 const char *
20152 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20153 const struct comp_unit_head *cu_header,
20154 unsigned int *bytes_read_ptr)
20155 {
20156 bfd *abfd = objfile->obfd;
20157 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20158
20159 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20160 }
20161
20162 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20163 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20164 ADDR_SIZE is the size of addresses from the CU header. */
20165
20166 static CORE_ADDR
20167 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20168 gdb::optional<ULONGEST> addr_base, int addr_size)
20169 {
20170 struct objfile *objfile = per_objfile->objfile;
20171 bfd *abfd = objfile->obfd;
20172 const gdb_byte *info_ptr;
20173 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20174
20175 per_objfile->per_bfd->addr.read (objfile);
20176 if (per_objfile->per_bfd->addr.buffer == NULL)
20177 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20178 objfile_name (objfile));
20179 if (addr_base_or_zero + addr_index * addr_size
20180 >= per_objfile->per_bfd->addr.size)
20181 error (_("DW_FORM_addr_index pointing outside of "
20182 ".debug_addr section [in module %s]"),
20183 objfile_name (objfile));
20184 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20185 + addr_index * addr_size);
20186 if (addr_size == 4)
20187 return bfd_get_32 (abfd, info_ptr);
20188 else
20189 return bfd_get_64 (abfd, info_ptr);
20190 }
20191
20192 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20193
20194 static CORE_ADDR
20195 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20196 {
20197 return read_addr_index_1 (cu->per_objfile, addr_index,
20198 cu->addr_base, cu->header.addr_size);
20199 }
20200
20201 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20202
20203 static CORE_ADDR
20204 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20205 unsigned int *bytes_read)
20206 {
20207 bfd *abfd = cu->per_objfile->objfile->obfd;
20208 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20209
20210 return read_addr_index (cu, addr_index);
20211 }
20212
20213 /* See read.h. */
20214
20215 CORE_ADDR
20216 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20217 dwarf2_per_objfile *per_objfile,
20218 unsigned int addr_index)
20219 {
20220 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20221 gdb::optional<ULONGEST> addr_base;
20222 int addr_size;
20223
20224 /* We need addr_base and addr_size.
20225 If we don't have PER_CU->cu, we have to get it.
20226 Nasty, but the alternative is storing the needed info in PER_CU,
20227 which at this point doesn't seem justified: it's not clear how frequently
20228 it would get used and it would increase the size of every PER_CU.
20229 Entry points like dwarf2_per_cu_addr_size do a similar thing
20230 so we're not in uncharted territory here.
20231 Alas we need to be a bit more complicated as addr_base is contained
20232 in the DIE.
20233
20234 We don't need to read the entire CU(/TU).
20235 We just need the header and top level die.
20236
20237 IWBN to use the aging mechanism to let us lazily later discard the CU.
20238 For now we skip this optimization. */
20239
20240 if (cu != NULL)
20241 {
20242 addr_base = cu->addr_base;
20243 addr_size = cu->header.addr_size;
20244 }
20245 else
20246 {
20247 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20248 addr_base = reader.cu->addr_base;
20249 addr_size = reader.cu->header.addr_size;
20250 }
20251
20252 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20253 }
20254
20255 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20256 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20257 DWO file. */
20258
20259 static const char *
20260 read_str_index (struct dwarf2_cu *cu,
20261 struct dwarf2_section_info *str_section,
20262 struct dwarf2_section_info *str_offsets_section,
20263 ULONGEST str_offsets_base, ULONGEST str_index)
20264 {
20265 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20266 struct objfile *objfile = per_objfile->objfile;
20267 const char *objf_name = objfile_name (objfile);
20268 bfd *abfd = objfile->obfd;
20269 const gdb_byte *info_ptr;
20270 ULONGEST str_offset;
20271 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20272
20273 str_section->read (objfile);
20274 str_offsets_section->read (objfile);
20275 if (str_section->buffer == NULL)
20276 error (_("%s used without %s section"
20277 " in CU at offset %s [in module %s]"),
20278 form_name, str_section->get_name (),
20279 sect_offset_str (cu->header.sect_off), objf_name);
20280 if (str_offsets_section->buffer == NULL)
20281 error (_("%s used without %s section"
20282 " in CU at offset %s [in module %s]"),
20283 form_name, str_section->get_name (),
20284 sect_offset_str (cu->header.sect_off), objf_name);
20285 info_ptr = (str_offsets_section->buffer
20286 + str_offsets_base
20287 + str_index * cu->header.offset_size);
20288 if (cu->header.offset_size == 4)
20289 str_offset = bfd_get_32 (abfd, info_ptr);
20290 else
20291 str_offset = bfd_get_64 (abfd, info_ptr);
20292 if (str_offset >= str_section->size)
20293 error (_("Offset from %s pointing outside of"
20294 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20295 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20296 return (const char *) (str_section->buffer + str_offset);
20297 }
20298
20299 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20300
20301 static const char *
20302 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20303 {
20304 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20305 ? reader->cu->header.addr_size : 0;
20306 return read_str_index (reader->cu,
20307 &reader->dwo_file->sections.str,
20308 &reader->dwo_file->sections.str_offsets,
20309 str_offsets_base, str_index);
20310 }
20311
20312 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20313
20314 static const char *
20315 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20316 {
20317 struct objfile *objfile = cu->per_objfile->objfile;
20318 const char *objf_name = objfile_name (objfile);
20319 static const char form_name[] = "DW_FORM_GNU_str_index";
20320 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20321
20322 if (!cu->str_offsets_base.has_value ())
20323 error (_("%s used in Fission stub without %s"
20324 " in CU at offset 0x%lx [in module %s]"),
20325 form_name, str_offsets_attr_name,
20326 (long) cu->header.offset_size, objf_name);
20327
20328 return read_str_index (cu,
20329 &cu->per_objfile->per_bfd->str,
20330 &cu->per_objfile->per_bfd->str_offsets,
20331 *cu->str_offsets_base, str_index);
20332 }
20333
20334 /* Return the length of an LEB128 number in BUF. */
20335
20336 static int
20337 leb128_size (const gdb_byte *buf)
20338 {
20339 const gdb_byte *begin = buf;
20340 gdb_byte byte;
20341
20342 while (1)
20343 {
20344 byte = *buf++;
20345 if ((byte & 128) == 0)
20346 return buf - begin;
20347 }
20348 }
20349
20350 static enum language
20351 dwarf_lang_to_enum_language (unsigned int lang)
20352 {
20353 enum language language;
20354
20355 switch (lang)
20356 {
20357 case DW_LANG_C89:
20358 case DW_LANG_C99:
20359 case DW_LANG_C11:
20360 case DW_LANG_C:
20361 case DW_LANG_UPC:
20362 language = language_c;
20363 break;
20364 case DW_LANG_Java:
20365 case DW_LANG_C_plus_plus:
20366 case DW_LANG_C_plus_plus_11:
20367 case DW_LANG_C_plus_plus_14:
20368 language = language_cplus;
20369 break;
20370 case DW_LANG_D:
20371 language = language_d;
20372 break;
20373 case DW_LANG_Fortran77:
20374 case DW_LANG_Fortran90:
20375 case DW_LANG_Fortran95:
20376 case DW_LANG_Fortran03:
20377 case DW_LANG_Fortran08:
20378 language = language_fortran;
20379 break;
20380 case DW_LANG_Go:
20381 language = language_go;
20382 break;
20383 case DW_LANG_Mips_Assembler:
20384 language = language_asm;
20385 break;
20386 case DW_LANG_Ada83:
20387 case DW_LANG_Ada95:
20388 language = language_ada;
20389 break;
20390 case DW_LANG_Modula2:
20391 language = language_m2;
20392 break;
20393 case DW_LANG_Pascal83:
20394 language = language_pascal;
20395 break;
20396 case DW_LANG_ObjC:
20397 language = language_objc;
20398 break;
20399 case DW_LANG_Rust:
20400 case DW_LANG_Rust_old:
20401 language = language_rust;
20402 break;
20403 case DW_LANG_OpenCL:
20404 language = language_opencl;
20405 break;
20406 case DW_LANG_Cobol74:
20407 case DW_LANG_Cobol85:
20408 default:
20409 language = language_minimal;
20410 break;
20411 }
20412
20413 return language;
20414 }
20415
20416 /* Return the named attribute or NULL if not there. */
20417
20418 static struct attribute *
20419 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20420 {
20421 for (;;)
20422 {
20423 unsigned int i;
20424 struct attribute *spec = NULL;
20425
20426 for (i = 0; i < die->num_attrs; ++i)
20427 {
20428 if (die->attrs[i].name == name)
20429 return &die->attrs[i];
20430 if (die->attrs[i].name == DW_AT_specification
20431 || die->attrs[i].name == DW_AT_abstract_origin)
20432 spec = &die->attrs[i];
20433 }
20434
20435 if (!spec)
20436 break;
20437
20438 die = follow_die_ref (die, spec, &cu);
20439 }
20440
20441 return NULL;
20442 }
20443
20444 /* Return the string associated with a string-typed attribute, or NULL if it
20445 is either not found or is of an incorrect type. */
20446
20447 static const char *
20448 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20449 {
20450 struct attribute *attr;
20451 const char *str = NULL;
20452
20453 attr = dwarf2_attr (die, name, cu);
20454
20455 if (attr != NULL)
20456 {
20457 str = attr->as_string ();
20458 if (str == nullptr)
20459 complaint (_("string type expected for attribute %s for "
20460 "DIE at %s in module %s"),
20461 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20462 objfile_name (cu->per_objfile->objfile));
20463 }
20464
20465 return str;
20466 }
20467
20468 /* Return the dwo name or NULL if not present. If present, it is in either
20469 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20470 static const char *
20471 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20472 {
20473 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20474 if (dwo_name == nullptr)
20475 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20476 return dwo_name;
20477 }
20478
20479 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20480 and holds a non-zero value. This function should only be used for
20481 DW_FORM_flag or DW_FORM_flag_present attributes. */
20482
20483 static int
20484 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20485 {
20486 struct attribute *attr = dwarf2_attr (die, name, cu);
20487
20488 return attr != nullptr && attr->as_boolean ();
20489 }
20490
20491 static int
20492 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20493 {
20494 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20495 which value is non-zero. However, we have to be careful with
20496 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20497 (via dwarf2_flag_true_p) follows this attribute. So we may
20498 end up accidently finding a declaration attribute that belongs
20499 to a different DIE referenced by the specification attribute,
20500 even though the given DIE does not have a declaration attribute. */
20501 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20502 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20503 }
20504
20505 /* Return the die giving the specification for DIE, if there is
20506 one. *SPEC_CU is the CU containing DIE on input, and the CU
20507 containing the return value on output. If there is no
20508 specification, but there is an abstract origin, that is
20509 returned. */
20510
20511 static struct die_info *
20512 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20513 {
20514 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20515 *spec_cu);
20516
20517 if (spec_attr == NULL)
20518 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20519
20520 if (spec_attr == NULL)
20521 return NULL;
20522 else
20523 return follow_die_ref (die, spec_attr, spec_cu);
20524 }
20525
20526 /* A convenience function to find the proper .debug_line section for a CU. */
20527
20528 static struct dwarf2_section_info *
20529 get_debug_line_section (struct dwarf2_cu *cu)
20530 {
20531 struct dwarf2_section_info *section;
20532 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20533
20534 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20535 DWO file. */
20536 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20537 section = &cu->dwo_unit->dwo_file->sections.line;
20538 else if (cu->per_cu->is_dwz)
20539 {
20540 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20541
20542 section = &dwz->line;
20543 }
20544 else
20545 section = &per_objfile->per_bfd->line;
20546
20547 return section;
20548 }
20549
20550 /* Read the statement program header starting at OFFSET in
20551 .debug_line, or .debug_line.dwo. Return a pointer
20552 to a struct line_header, allocated using xmalloc.
20553 Returns NULL if there is a problem reading the header, e.g., if it
20554 has a version we don't understand.
20555
20556 NOTE: the strings in the include directory and file name tables of
20557 the returned object point into the dwarf line section buffer,
20558 and must not be freed. */
20559
20560 static line_header_up
20561 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20562 {
20563 struct dwarf2_section_info *section;
20564 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20565
20566 section = get_debug_line_section (cu);
20567 section->read (per_objfile->objfile);
20568 if (section->buffer == NULL)
20569 {
20570 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20571 complaint (_("missing .debug_line.dwo section"));
20572 else
20573 complaint (_("missing .debug_line section"));
20574 return 0;
20575 }
20576
20577 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20578 per_objfile, section, &cu->header);
20579 }
20580
20581 /* Subroutine of dwarf_decode_lines to simplify it.
20582 Return the file name of the psymtab for the given file_entry.
20583 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20584 If space for the result is malloc'd, *NAME_HOLDER will be set.
20585 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20586
20587 static const char *
20588 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20589 const dwarf2_psymtab *pst,
20590 const char *comp_dir,
20591 gdb::unique_xmalloc_ptr<char> *name_holder)
20592 {
20593 const char *include_name = fe.name;
20594 const char *include_name_to_compare = include_name;
20595 const char *pst_filename;
20596 int file_is_pst;
20597
20598 const char *dir_name = fe.include_dir (lh);
20599
20600 gdb::unique_xmalloc_ptr<char> hold_compare;
20601 if (!IS_ABSOLUTE_PATH (include_name)
20602 && (dir_name != NULL || comp_dir != NULL))
20603 {
20604 /* Avoid creating a duplicate psymtab for PST.
20605 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20606 Before we do the comparison, however, we need to account
20607 for DIR_NAME and COMP_DIR.
20608 First prepend dir_name (if non-NULL). If we still don't
20609 have an absolute path prepend comp_dir (if non-NULL).
20610 However, the directory we record in the include-file's
20611 psymtab does not contain COMP_DIR (to match the
20612 corresponding symtab(s)).
20613
20614 Example:
20615
20616 bash$ cd /tmp
20617 bash$ gcc -g ./hello.c
20618 include_name = "hello.c"
20619 dir_name = "."
20620 DW_AT_comp_dir = comp_dir = "/tmp"
20621 DW_AT_name = "./hello.c"
20622
20623 */
20624
20625 if (dir_name != NULL)
20626 {
20627 name_holder->reset (concat (dir_name, SLASH_STRING,
20628 include_name, (char *) NULL));
20629 include_name = name_holder->get ();
20630 include_name_to_compare = include_name;
20631 }
20632 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20633 {
20634 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20635 include_name, (char *) NULL));
20636 include_name_to_compare = hold_compare.get ();
20637 }
20638 }
20639
20640 pst_filename = pst->filename;
20641 gdb::unique_xmalloc_ptr<char> copied_name;
20642 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20643 {
20644 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20645 pst_filename, (char *) NULL));
20646 pst_filename = copied_name.get ();
20647 }
20648
20649 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20650
20651 if (file_is_pst)
20652 return NULL;
20653 return include_name;
20654 }
20655
20656 /* State machine to track the state of the line number program. */
20657
20658 class lnp_state_machine
20659 {
20660 public:
20661 /* Initialize a machine state for the start of a line number
20662 program. */
20663 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20664 bool record_lines_p);
20665
20666 file_entry *current_file ()
20667 {
20668 /* lh->file_names is 0-based, but the file name numbers in the
20669 statement program are 1-based. */
20670 return m_line_header->file_name_at (m_file);
20671 }
20672
20673 /* Record the line in the state machine. END_SEQUENCE is true if
20674 we're processing the end of a sequence. */
20675 void record_line (bool end_sequence);
20676
20677 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20678 nop-out rest of the lines in this sequence. */
20679 void check_line_address (struct dwarf2_cu *cu,
20680 const gdb_byte *line_ptr,
20681 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20682
20683 void handle_set_discriminator (unsigned int discriminator)
20684 {
20685 m_discriminator = discriminator;
20686 m_line_has_non_zero_discriminator |= discriminator != 0;
20687 }
20688
20689 /* Handle DW_LNE_set_address. */
20690 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20691 {
20692 m_op_index = 0;
20693 address += baseaddr;
20694 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20695 }
20696
20697 /* Handle DW_LNS_advance_pc. */
20698 void handle_advance_pc (CORE_ADDR adjust);
20699
20700 /* Handle a special opcode. */
20701 void handle_special_opcode (unsigned char op_code);
20702
20703 /* Handle DW_LNS_advance_line. */
20704 void handle_advance_line (int line_delta)
20705 {
20706 advance_line (line_delta);
20707 }
20708
20709 /* Handle DW_LNS_set_file. */
20710 void handle_set_file (file_name_index file);
20711
20712 /* Handle DW_LNS_negate_stmt. */
20713 void handle_negate_stmt ()
20714 {
20715 m_is_stmt = !m_is_stmt;
20716 }
20717
20718 /* Handle DW_LNS_const_add_pc. */
20719 void handle_const_add_pc ();
20720
20721 /* Handle DW_LNS_fixed_advance_pc. */
20722 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20723 {
20724 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20725 m_op_index = 0;
20726 }
20727
20728 /* Handle DW_LNS_copy. */
20729 void handle_copy ()
20730 {
20731 record_line (false);
20732 m_discriminator = 0;
20733 }
20734
20735 /* Handle DW_LNE_end_sequence. */
20736 void handle_end_sequence ()
20737 {
20738 m_currently_recording_lines = true;
20739 }
20740
20741 private:
20742 /* Advance the line by LINE_DELTA. */
20743 void advance_line (int line_delta)
20744 {
20745 m_line += line_delta;
20746
20747 if (line_delta != 0)
20748 m_line_has_non_zero_discriminator = m_discriminator != 0;
20749 }
20750
20751 struct dwarf2_cu *m_cu;
20752
20753 gdbarch *m_gdbarch;
20754
20755 /* True if we're recording lines.
20756 Otherwise we're building partial symtabs and are just interested in
20757 finding include files mentioned by the line number program. */
20758 bool m_record_lines_p;
20759
20760 /* The line number header. */
20761 line_header *m_line_header;
20762
20763 /* These are part of the standard DWARF line number state machine,
20764 and initialized according to the DWARF spec. */
20765
20766 unsigned char m_op_index = 0;
20767 /* The line table index of the current file. */
20768 file_name_index m_file = 1;
20769 unsigned int m_line = 1;
20770
20771 /* These are initialized in the constructor. */
20772
20773 CORE_ADDR m_address;
20774 bool m_is_stmt;
20775 unsigned int m_discriminator;
20776
20777 /* Additional bits of state we need to track. */
20778
20779 /* The last file that we called dwarf2_start_subfile for.
20780 This is only used for TLLs. */
20781 unsigned int m_last_file = 0;
20782 /* The last file a line number was recorded for. */
20783 struct subfile *m_last_subfile = NULL;
20784
20785 /* The address of the last line entry. */
20786 CORE_ADDR m_last_address;
20787
20788 /* Set to true when a previous line at the same address (using
20789 m_last_address) had m_is_stmt true. This is reset to false when a
20790 line entry at a new address (m_address different to m_last_address) is
20791 processed. */
20792 bool m_stmt_at_address = false;
20793
20794 /* When true, record the lines we decode. */
20795 bool m_currently_recording_lines = false;
20796
20797 /* The last line number that was recorded, used to coalesce
20798 consecutive entries for the same line. This can happen, for
20799 example, when discriminators are present. PR 17276. */
20800 unsigned int m_last_line = 0;
20801 bool m_line_has_non_zero_discriminator = false;
20802 };
20803
20804 void
20805 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20806 {
20807 CORE_ADDR addr_adj = (((m_op_index + adjust)
20808 / m_line_header->maximum_ops_per_instruction)
20809 * m_line_header->minimum_instruction_length);
20810 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20811 m_op_index = ((m_op_index + adjust)
20812 % m_line_header->maximum_ops_per_instruction);
20813 }
20814
20815 void
20816 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20817 {
20818 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20819 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20820 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20821 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20822 / m_line_header->maximum_ops_per_instruction)
20823 * m_line_header->minimum_instruction_length);
20824 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20825 m_op_index = ((m_op_index + adj_opcode_d)
20826 % m_line_header->maximum_ops_per_instruction);
20827
20828 int line_delta = m_line_header->line_base + adj_opcode_r;
20829 advance_line (line_delta);
20830 record_line (false);
20831 m_discriminator = 0;
20832 }
20833
20834 void
20835 lnp_state_machine::handle_set_file (file_name_index file)
20836 {
20837 m_file = file;
20838
20839 const file_entry *fe = current_file ();
20840 if (fe == NULL)
20841 dwarf2_debug_line_missing_file_complaint ();
20842 else if (m_record_lines_p)
20843 {
20844 const char *dir = fe->include_dir (m_line_header);
20845
20846 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20847 m_line_has_non_zero_discriminator = m_discriminator != 0;
20848 dwarf2_start_subfile (m_cu, fe->name, dir);
20849 }
20850 }
20851
20852 void
20853 lnp_state_machine::handle_const_add_pc ()
20854 {
20855 CORE_ADDR adjust
20856 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20857
20858 CORE_ADDR addr_adj
20859 = (((m_op_index + adjust)
20860 / m_line_header->maximum_ops_per_instruction)
20861 * m_line_header->minimum_instruction_length);
20862
20863 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20864 m_op_index = ((m_op_index + adjust)
20865 % m_line_header->maximum_ops_per_instruction);
20866 }
20867
20868 /* Return non-zero if we should add LINE to the line number table.
20869 LINE is the line to add, LAST_LINE is the last line that was added,
20870 LAST_SUBFILE is the subfile for LAST_LINE.
20871 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20872 had a non-zero discriminator.
20873
20874 We have to be careful in the presence of discriminators.
20875 E.g., for this line:
20876
20877 for (i = 0; i < 100000; i++);
20878
20879 clang can emit four line number entries for that one line,
20880 each with a different discriminator.
20881 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20882
20883 However, we want gdb to coalesce all four entries into one.
20884 Otherwise the user could stepi into the middle of the line and
20885 gdb would get confused about whether the pc really was in the
20886 middle of the line.
20887
20888 Things are further complicated by the fact that two consecutive
20889 line number entries for the same line is a heuristic used by gcc
20890 to denote the end of the prologue. So we can't just discard duplicate
20891 entries, we have to be selective about it. The heuristic we use is
20892 that we only collapse consecutive entries for the same line if at least
20893 one of those entries has a non-zero discriminator. PR 17276.
20894
20895 Note: Addresses in the line number state machine can never go backwards
20896 within one sequence, thus this coalescing is ok. */
20897
20898 static int
20899 dwarf_record_line_p (struct dwarf2_cu *cu,
20900 unsigned int line, unsigned int last_line,
20901 int line_has_non_zero_discriminator,
20902 struct subfile *last_subfile)
20903 {
20904 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20905 return 1;
20906 if (line != last_line)
20907 return 1;
20908 /* Same line for the same file that we've seen already.
20909 As a last check, for pr 17276, only record the line if the line
20910 has never had a non-zero discriminator. */
20911 if (!line_has_non_zero_discriminator)
20912 return 1;
20913 return 0;
20914 }
20915
20916 /* Use the CU's builder to record line number LINE beginning at
20917 address ADDRESS in the line table of subfile SUBFILE. */
20918
20919 static void
20920 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20921 unsigned int line, CORE_ADDR address, bool is_stmt,
20922 struct dwarf2_cu *cu)
20923 {
20924 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20925
20926 if (dwarf_line_debug)
20927 {
20928 fprintf_unfiltered (gdb_stdlog,
20929 "Recording line %u, file %s, address %s\n",
20930 line, lbasename (subfile->name),
20931 paddress (gdbarch, address));
20932 }
20933
20934 if (cu != nullptr)
20935 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20936 }
20937
20938 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20939 Mark the end of a set of line number records.
20940 The arguments are the same as for dwarf_record_line_1.
20941 If SUBFILE is NULL the request is ignored. */
20942
20943 static void
20944 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20945 CORE_ADDR address, struct dwarf2_cu *cu)
20946 {
20947 if (subfile == NULL)
20948 return;
20949
20950 if (dwarf_line_debug)
20951 {
20952 fprintf_unfiltered (gdb_stdlog,
20953 "Finishing current line, file %s, address %s\n",
20954 lbasename (subfile->name),
20955 paddress (gdbarch, address));
20956 }
20957
20958 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20959 }
20960
20961 void
20962 lnp_state_machine::record_line (bool end_sequence)
20963 {
20964 if (dwarf_line_debug)
20965 {
20966 fprintf_unfiltered (gdb_stdlog,
20967 "Processing actual line %u: file %u,"
20968 " address %s, is_stmt %u, discrim %u%s\n",
20969 m_line, m_file,
20970 paddress (m_gdbarch, m_address),
20971 m_is_stmt, m_discriminator,
20972 (end_sequence ? "\t(end sequence)" : ""));
20973 }
20974
20975 file_entry *fe = current_file ();
20976
20977 if (fe == NULL)
20978 dwarf2_debug_line_missing_file_complaint ();
20979 /* For now we ignore lines not starting on an instruction boundary.
20980 But not when processing end_sequence for compatibility with the
20981 previous version of the code. */
20982 else if (m_op_index == 0 || end_sequence)
20983 {
20984 fe->included_p = 1;
20985 if (m_record_lines_p)
20986 {
20987 /* When we switch files we insert an end maker in the first file,
20988 switch to the second file and add a new line entry. The
20989 problem is that the end marker inserted in the first file will
20990 discard any previous line entries at the same address. If the
20991 line entries in the first file are marked as is-stmt, while
20992 the new line in the second file is non-stmt, then this means
20993 the end marker will discard is-stmt lines so we can have a
20994 non-stmt line. This means that there are less addresses at
20995 which the user can insert a breakpoint.
20996
20997 To improve this we track the last address in m_last_address,
20998 and whether we have seen an is-stmt at this address. Then
20999 when switching files, if we have seen a stmt at the current
21000 address, and we are switching to create a non-stmt line, then
21001 discard the new line. */
21002 bool file_changed
21003 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21004 bool ignore_this_line
21005 = ((file_changed && !end_sequence && m_last_address == m_address
21006 && !m_is_stmt && m_stmt_at_address)
21007 || (!end_sequence && m_line == 0));
21008
21009 if ((file_changed && !ignore_this_line) || end_sequence)
21010 {
21011 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21012 m_currently_recording_lines ? m_cu : nullptr);
21013 }
21014
21015 if (!end_sequence && !ignore_this_line)
21016 {
21017 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21018
21019 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21020 m_line_has_non_zero_discriminator,
21021 m_last_subfile))
21022 {
21023 buildsym_compunit *builder = m_cu->get_builder ();
21024 dwarf_record_line_1 (m_gdbarch,
21025 builder->get_current_subfile (),
21026 m_line, m_address, is_stmt,
21027 m_currently_recording_lines ? m_cu : nullptr);
21028 }
21029 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21030 m_last_line = m_line;
21031 }
21032 }
21033 }
21034
21035 /* Track whether we have seen any m_is_stmt true at m_address in case we
21036 have multiple line table entries all at m_address. */
21037 if (m_last_address != m_address)
21038 {
21039 m_stmt_at_address = false;
21040 m_last_address = m_address;
21041 }
21042 m_stmt_at_address |= m_is_stmt;
21043 }
21044
21045 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21046 line_header *lh, bool record_lines_p)
21047 {
21048 m_cu = cu;
21049 m_gdbarch = arch;
21050 m_record_lines_p = record_lines_p;
21051 m_line_header = lh;
21052
21053 m_currently_recording_lines = true;
21054
21055 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21056 was a line entry for it so that the backend has a chance to adjust it
21057 and also record it in case it needs it. This is currently used by MIPS
21058 code, cf. `mips_adjust_dwarf2_line'. */
21059 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21060 m_is_stmt = lh->default_is_stmt;
21061 m_discriminator = 0;
21062
21063 m_last_address = m_address;
21064 m_stmt_at_address = false;
21065 }
21066
21067 void
21068 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21069 const gdb_byte *line_ptr,
21070 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21071 {
21072 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21073 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21074 located at 0x0. In this case, additionally check that if
21075 ADDRESS < UNRELOCATED_LOWPC. */
21076
21077 if ((address == 0 && address < unrelocated_lowpc)
21078 || address == (CORE_ADDR) -1)
21079 {
21080 /* This line table is for a function which has been
21081 GCd by the linker. Ignore it. PR gdb/12528 */
21082
21083 struct objfile *objfile = cu->per_objfile->objfile;
21084 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21085
21086 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21087 line_offset, objfile_name (objfile));
21088 m_currently_recording_lines = false;
21089 /* Note: m_currently_recording_lines is left as false until we see
21090 DW_LNE_end_sequence. */
21091 }
21092 }
21093
21094 /* Subroutine of dwarf_decode_lines to simplify it.
21095 Process the line number information in LH.
21096 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21097 program in order to set included_p for every referenced header. */
21098
21099 static void
21100 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21101 const int decode_for_pst_p, CORE_ADDR lowpc)
21102 {
21103 const gdb_byte *line_ptr, *extended_end;
21104 const gdb_byte *line_end;
21105 unsigned int bytes_read, extended_len;
21106 unsigned char op_code, extended_op;
21107 CORE_ADDR baseaddr;
21108 struct objfile *objfile = cu->per_objfile->objfile;
21109 bfd *abfd = objfile->obfd;
21110 struct gdbarch *gdbarch = objfile->arch ();
21111 /* True if we're recording line info (as opposed to building partial
21112 symtabs and just interested in finding include files mentioned by
21113 the line number program). */
21114 bool record_lines_p = !decode_for_pst_p;
21115
21116 baseaddr = objfile->text_section_offset ();
21117
21118 line_ptr = lh->statement_program_start;
21119 line_end = lh->statement_program_end;
21120
21121 /* Read the statement sequences until there's nothing left. */
21122 while (line_ptr < line_end)
21123 {
21124 /* The DWARF line number program state machine. Reset the state
21125 machine at the start of each sequence. */
21126 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21127 bool end_sequence = false;
21128
21129 if (record_lines_p)
21130 {
21131 /* Start a subfile for the current file of the state
21132 machine. */
21133 const file_entry *fe = state_machine.current_file ();
21134
21135 if (fe != NULL)
21136 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21137 }
21138
21139 /* Decode the table. */
21140 while (line_ptr < line_end && !end_sequence)
21141 {
21142 op_code = read_1_byte (abfd, line_ptr);
21143 line_ptr += 1;
21144
21145 if (op_code >= lh->opcode_base)
21146 {
21147 /* Special opcode. */
21148 state_machine.handle_special_opcode (op_code);
21149 }
21150 else switch (op_code)
21151 {
21152 case DW_LNS_extended_op:
21153 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21154 &bytes_read);
21155 line_ptr += bytes_read;
21156 extended_end = line_ptr + extended_len;
21157 extended_op = read_1_byte (abfd, line_ptr);
21158 line_ptr += 1;
21159 if (DW_LNE_lo_user <= extended_op
21160 && extended_op <= DW_LNE_hi_user)
21161 {
21162 /* Vendor extension, ignore. */
21163 line_ptr = extended_end;
21164 break;
21165 }
21166 switch (extended_op)
21167 {
21168 case DW_LNE_end_sequence:
21169 state_machine.handle_end_sequence ();
21170 end_sequence = true;
21171 break;
21172 case DW_LNE_set_address:
21173 {
21174 CORE_ADDR address
21175 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21176 line_ptr += bytes_read;
21177
21178 state_machine.check_line_address (cu, line_ptr,
21179 lowpc - baseaddr, address);
21180 state_machine.handle_set_address (baseaddr, address);
21181 }
21182 break;
21183 case DW_LNE_define_file:
21184 {
21185 const char *cur_file;
21186 unsigned int mod_time, length;
21187 dir_index dindex;
21188
21189 cur_file = read_direct_string (abfd, line_ptr,
21190 &bytes_read);
21191 line_ptr += bytes_read;
21192 dindex = (dir_index)
21193 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21194 line_ptr += bytes_read;
21195 mod_time =
21196 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21197 line_ptr += bytes_read;
21198 length =
21199 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21200 line_ptr += bytes_read;
21201 lh->add_file_name (cur_file, dindex, mod_time, length);
21202 }
21203 break;
21204 case DW_LNE_set_discriminator:
21205 {
21206 /* The discriminator is not interesting to the
21207 debugger; just ignore it. We still need to
21208 check its value though:
21209 if there are consecutive entries for the same
21210 (non-prologue) line we want to coalesce them.
21211 PR 17276. */
21212 unsigned int discr
21213 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21214 line_ptr += bytes_read;
21215
21216 state_machine.handle_set_discriminator (discr);
21217 }
21218 break;
21219 default:
21220 complaint (_("mangled .debug_line section"));
21221 return;
21222 }
21223 /* Make sure that we parsed the extended op correctly. If e.g.
21224 we expected a different address size than the producer used,
21225 we may have read the wrong number of bytes. */
21226 if (line_ptr != extended_end)
21227 {
21228 complaint (_("mangled .debug_line section"));
21229 return;
21230 }
21231 break;
21232 case DW_LNS_copy:
21233 state_machine.handle_copy ();
21234 break;
21235 case DW_LNS_advance_pc:
21236 {
21237 CORE_ADDR adjust
21238 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21239 line_ptr += bytes_read;
21240
21241 state_machine.handle_advance_pc (adjust);
21242 }
21243 break;
21244 case DW_LNS_advance_line:
21245 {
21246 int line_delta
21247 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21248 line_ptr += bytes_read;
21249
21250 state_machine.handle_advance_line (line_delta);
21251 }
21252 break;
21253 case DW_LNS_set_file:
21254 {
21255 file_name_index file
21256 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21257 &bytes_read);
21258 line_ptr += bytes_read;
21259
21260 state_machine.handle_set_file (file);
21261 }
21262 break;
21263 case DW_LNS_set_column:
21264 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21265 line_ptr += bytes_read;
21266 break;
21267 case DW_LNS_negate_stmt:
21268 state_machine.handle_negate_stmt ();
21269 break;
21270 case DW_LNS_set_basic_block:
21271 break;
21272 /* Add to the address register of the state machine the
21273 address increment value corresponding to special opcode
21274 255. I.e., this value is scaled by the minimum
21275 instruction length since special opcode 255 would have
21276 scaled the increment. */
21277 case DW_LNS_const_add_pc:
21278 state_machine.handle_const_add_pc ();
21279 break;
21280 case DW_LNS_fixed_advance_pc:
21281 {
21282 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21283 line_ptr += 2;
21284
21285 state_machine.handle_fixed_advance_pc (addr_adj);
21286 }
21287 break;
21288 default:
21289 {
21290 /* Unknown standard opcode, ignore it. */
21291 int i;
21292
21293 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21294 {
21295 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21296 line_ptr += bytes_read;
21297 }
21298 }
21299 }
21300 }
21301
21302 if (!end_sequence)
21303 dwarf2_debug_line_missing_end_sequence_complaint ();
21304
21305 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21306 in which case we still finish recording the last line). */
21307 state_machine.record_line (true);
21308 }
21309 }
21310
21311 /* Decode the Line Number Program (LNP) for the given line_header
21312 structure and CU. The actual information extracted and the type
21313 of structures created from the LNP depends on the value of PST.
21314
21315 1. If PST is NULL, then this procedure uses the data from the program
21316 to create all necessary symbol tables, and their linetables.
21317
21318 2. If PST is not NULL, this procedure reads the program to determine
21319 the list of files included by the unit represented by PST, and
21320 builds all the associated partial symbol tables.
21321
21322 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21323 It is used for relative paths in the line table.
21324 NOTE: When processing partial symtabs (pst != NULL),
21325 comp_dir == pst->dirname.
21326
21327 NOTE: It is important that psymtabs have the same file name (via strcmp)
21328 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21329 symtab we don't use it in the name of the psymtabs we create.
21330 E.g. expand_line_sal requires this when finding psymtabs to expand.
21331 A good testcase for this is mb-inline.exp.
21332
21333 LOWPC is the lowest address in CU (or 0 if not known).
21334
21335 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21336 for its PC<->lines mapping information. Otherwise only the filename
21337 table is read in. */
21338
21339 static void
21340 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21341 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21342 CORE_ADDR lowpc, int decode_mapping)
21343 {
21344 struct objfile *objfile = cu->per_objfile->objfile;
21345 const int decode_for_pst_p = (pst != NULL);
21346
21347 if (decode_mapping)
21348 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21349
21350 if (decode_for_pst_p)
21351 {
21352 /* Now that we're done scanning the Line Header Program, we can
21353 create the psymtab of each included file. */
21354 for (auto &file_entry : lh->file_names ())
21355 if (file_entry.included_p == 1)
21356 {
21357 gdb::unique_xmalloc_ptr<char> name_holder;
21358 const char *include_name =
21359 psymtab_include_file_name (lh, file_entry, pst,
21360 comp_dir, &name_holder);
21361 if (include_name != NULL)
21362 dwarf2_create_include_psymtab
21363 (cu->per_objfile->per_bfd, include_name, pst,
21364 cu->per_objfile->per_bfd->partial_symtabs.get (),
21365 objfile->per_bfd);
21366 }
21367 }
21368 else
21369 {
21370 /* Make sure a symtab is created for every file, even files
21371 which contain only variables (i.e. no code with associated
21372 line numbers). */
21373 buildsym_compunit *builder = cu->get_builder ();
21374 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21375
21376 for (auto &fe : lh->file_names ())
21377 {
21378 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21379 if (builder->get_current_subfile ()->symtab == NULL)
21380 {
21381 builder->get_current_subfile ()->symtab
21382 = allocate_symtab (cust,
21383 builder->get_current_subfile ()->name);
21384 }
21385 fe.symtab = builder->get_current_subfile ()->symtab;
21386 }
21387 }
21388 }
21389
21390 /* Start a subfile for DWARF. FILENAME is the name of the file and
21391 DIRNAME the name of the source directory which contains FILENAME
21392 or NULL if not known.
21393 This routine tries to keep line numbers from identical absolute and
21394 relative file names in a common subfile.
21395
21396 Using the `list' example from the GDB testsuite, which resides in
21397 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21398 of /srcdir/list0.c yields the following debugging information for list0.c:
21399
21400 DW_AT_name: /srcdir/list0.c
21401 DW_AT_comp_dir: /compdir
21402 files.files[0].name: list0.h
21403 files.files[0].dir: /srcdir
21404 files.files[1].name: list0.c
21405 files.files[1].dir: /srcdir
21406
21407 The line number information for list0.c has to end up in a single
21408 subfile, so that `break /srcdir/list0.c:1' works as expected.
21409 start_subfile will ensure that this happens provided that we pass the
21410 concatenation of files.files[1].dir and files.files[1].name as the
21411 subfile's name. */
21412
21413 static void
21414 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21415 const char *dirname)
21416 {
21417 gdb::unique_xmalloc_ptr<char> copy;
21418
21419 /* In order not to lose the line information directory,
21420 we concatenate it to the filename when it makes sense.
21421 Note that the Dwarf3 standard says (speaking of filenames in line
21422 information): ``The directory index is ignored for file names
21423 that represent full path names''. Thus ignoring dirname in the
21424 `else' branch below isn't an issue. */
21425
21426 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21427 {
21428 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21429 filename = copy.get ();
21430 }
21431
21432 cu->get_builder ()->start_subfile (filename);
21433 }
21434
21435 static void
21436 var_decode_location (struct attribute *attr, struct symbol *sym,
21437 struct dwarf2_cu *cu)
21438 {
21439 struct objfile *objfile = cu->per_objfile->objfile;
21440 struct comp_unit_head *cu_header = &cu->header;
21441
21442 /* NOTE drow/2003-01-30: There used to be a comment and some special
21443 code here to turn a symbol with DW_AT_external and a
21444 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21445 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21446 with some versions of binutils) where shared libraries could have
21447 relocations against symbols in their debug information - the
21448 minimal symbol would have the right address, but the debug info
21449 would not. It's no longer necessary, because we will explicitly
21450 apply relocations when we read in the debug information now. */
21451
21452 /* A DW_AT_location attribute with no contents indicates that a
21453 variable has been optimized away. */
21454 if (attr->form_is_block () && attr->as_block ()->size == 0)
21455 {
21456 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21457 return;
21458 }
21459
21460 /* Handle one degenerate form of location expression specially, to
21461 preserve GDB's previous behavior when section offsets are
21462 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21463 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21464
21465 if (attr->form_is_block ())
21466 {
21467 struct dwarf_block *block = attr->as_block ();
21468
21469 if ((block->data[0] == DW_OP_addr
21470 && block->size == 1 + cu_header->addr_size)
21471 || ((block->data[0] == DW_OP_GNU_addr_index
21472 || block->data[0] == DW_OP_addrx)
21473 && (block->size
21474 == 1 + leb128_size (&block->data[1]))))
21475 {
21476 unsigned int dummy;
21477
21478 if (block->data[0] == DW_OP_addr)
21479 SET_SYMBOL_VALUE_ADDRESS
21480 (sym, cu->header.read_address (objfile->obfd,
21481 block->data + 1,
21482 &dummy));
21483 else
21484 SET_SYMBOL_VALUE_ADDRESS
21485 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21486 &dummy));
21487 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21488 fixup_symbol_section (sym, objfile);
21489 SET_SYMBOL_VALUE_ADDRESS
21490 (sym,
21491 SYMBOL_VALUE_ADDRESS (sym)
21492 + objfile->section_offsets[sym->section_index ()]);
21493 return;
21494 }
21495 }
21496
21497 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21498 expression evaluator, and use LOC_COMPUTED only when necessary
21499 (i.e. when the value of a register or memory location is
21500 referenced, or a thread-local block, etc.). Then again, it might
21501 not be worthwhile. I'm assuming that it isn't unless performance
21502 or memory numbers show me otherwise. */
21503
21504 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21505
21506 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21507 cu->has_loclist = true;
21508 }
21509
21510 /* Given a pointer to a DWARF information entry, figure out if we need
21511 to make a symbol table entry for it, and if so, create a new entry
21512 and return a pointer to it.
21513 If TYPE is NULL, determine symbol type from the die, otherwise
21514 used the passed type.
21515 If SPACE is not NULL, use it to hold the new symbol. If it is
21516 NULL, allocate a new symbol on the objfile's obstack. */
21517
21518 static struct symbol *
21519 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21520 struct symbol *space)
21521 {
21522 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21523 struct objfile *objfile = per_objfile->objfile;
21524 struct gdbarch *gdbarch = objfile->arch ();
21525 struct symbol *sym = NULL;
21526 const char *name;
21527 struct attribute *attr = NULL;
21528 struct attribute *attr2 = NULL;
21529 CORE_ADDR baseaddr;
21530 struct pending **list_to_add = NULL;
21531
21532 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21533
21534 baseaddr = objfile->text_section_offset ();
21535
21536 name = dwarf2_name (die, cu);
21537 if (name)
21538 {
21539 int suppress_add = 0;
21540
21541 if (space)
21542 sym = space;
21543 else
21544 sym = new (&objfile->objfile_obstack) symbol;
21545 OBJSTAT (objfile, n_syms++);
21546
21547 /* Cache this symbol's name and the name's demangled form (if any). */
21548 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
21549 /* Fortran does not have mangling standard and the mangling does differ
21550 between gfortran, iFort etc. */
21551 const char *physname
21552 = (cu->per_cu->lang == language_fortran
21553 ? dwarf2_full_name (name, die, cu)
21554 : dwarf2_physname (name, die, cu));
21555 const char *linkagename = dw2_linkage_name (die, cu);
21556
21557 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
21558 sym->set_linkage_name (physname);
21559 else
21560 {
21561 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21562 sym->set_linkage_name (linkagename);
21563 }
21564
21565 /* Default assumptions.
21566 Use the passed type or decode it from the die. */
21567 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21568 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21569 if (type != NULL)
21570 SYMBOL_TYPE (sym) = type;
21571 else
21572 SYMBOL_TYPE (sym) = die_type (die, cu);
21573 attr = dwarf2_attr (die,
21574 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21575 cu);
21576 if (attr != nullptr)
21577 SYMBOL_LINE (sym) = attr->constant_value (0);
21578
21579 attr = dwarf2_attr (die,
21580 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21581 cu);
21582 if (attr != nullptr && attr->is_nonnegative ())
21583 {
21584 file_name_index file_index
21585 = (file_name_index) attr->as_nonnegative ();
21586 struct file_entry *fe;
21587
21588 if (cu->line_header != NULL)
21589 fe = cu->line_header->file_name_at (file_index);
21590 else
21591 fe = NULL;
21592
21593 if (fe == NULL)
21594 complaint (_("file index out of range"));
21595 else
21596 symbol_set_symtab (sym, fe->symtab);
21597 }
21598
21599 switch (die->tag)
21600 {
21601 case DW_TAG_label:
21602 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21603 if (attr != nullptr)
21604 {
21605 CORE_ADDR addr;
21606
21607 addr = attr->as_address ();
21608 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21609 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21610 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21611 }
21612 else
21613 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21614 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21615 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21616 add_symbol_to_list (sym, cu->list_in_scope);
21617 break;
21618 case DW_TAG_subprogram:
21619 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21620 finish_block. */
21621 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21622 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21623 if ((attr2 != nullptr && attr2->as_boolean ())
21624 || cu->per_cu->lang == language_ada
21625 || cu->per_cu->lang == language_fortran)
21626 {
21627 /* Subprograms marked external are stored as a global symbol.
21628 Ada and Fortran subprograms, whether marked external or
21629 not, are always stored as a global symbol, because we want
21630 to be able to access them globally. For instance, we want
21631 to be able to break on a nested subprogram without having
21632 to specify the context. */
21633 list_to_add = cu->get_builder ()->get_global_symbols ();
21634 }
21635 else
21636 {
21637 list_to_add = cu->list_in_scope;
21638 }
21639 break;
21640 case DW_TAG_inlined_subroutine:
21641 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21642 finish_block. */
21643 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21644 SYMBOL_INLINED (sym) = 1;
21645 list_to_add = cu->list_in_scope;
21646 break;
21647 case DW_TAG_template_value_param:
21648 suppress_add = 1;
21649 /* Fall through. */
21650 case DW_TAG_constant:
21651 case DW_TAG_variable:
21652 case DW_TAG_member:
21653 /* Compilation with minimal debug info may result in
21654 variables with missing type entries. Change the
21655 misleading `void' type to something sensible. */
21656 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21657 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21658
21659 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21660 /* In the case of DW_TAG_member, we should only be called for
21661 static const members. */
21662 if (die->tag == DW_TAG_member)
21663 {
21664 /* dwarf2_add_field uses die_is_declaration,
21665 so we do the same. */
21666 gdb_assert (die_is_declaration (die, cu));
21667 gdb_assert (attr);
21668 }
21669 if (attr != nullptr)
21670 {
21671 dwarf2_const_value (attr, sym, cu);
21672 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21673 if (!suppress_add)
21674 {
21675 if (attr2 != nullptr && attr2->as_boolean ())
21676 list_to_add = cu->get_builder ()->get_global_symbols ();
21677 else
21678 list_to_add = cu->list_in_scope;
21679 }
21680 break;
21681 }
21682 attr = dwarf2_attr (die, DW_AT_location, cu);
21683 if (attr != nullptr)
21684 {
21685 var_decode_location (attr, sym, cu);
21686 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21687
21688 /* Fortran explicitly imports any global symbols to the local
21689 scope by DW_TAG_common_block. */
21690 if (cu->per_cu->lang == language_fortran && die->parent
21691 && die->parent->tag == DW_TAG_common_block)
21692 attr2 = NULL;
21693
21694 if (SYMBOL_CLASS (sym) == LOC_STATIC
21695 && SYMBOL_VALUE_ADDRESS (sym) == 0
21696 && !per_objfile->per_bfd->has_section_at_zero)
21697 {
21698 /* When a static variable is eliminated by the linker,
21699 the corresponding debug information is not stripped
21700 out, but the variable address is set to null;
21701 do not add such variables into symbol table. */
21702 }
21703 else if (attr2 != nullptr && attr2->as_boolean ())
21704 {
21705 if (SYMBOL_CLASS (sym) == LOC_STATIC
21706 && (objfile->flags & OBJF_MAINLINE) == 0
21707 && per_objfile->per_bfd->can_copy)
21708 {
21709 /* A global static variable might be subject to
21710 copy relocation. We first check for a local
21711 minsym, though, because maybe the symbol was
21712 marked hidden, in which case this would not
21713 apply. */
21714 bound_minimal_symbol found
21715 = (lookup_minimal_symbol_linkage
21716 (sym->linkage_name (), objfile));
21717 if (found.minsym != nullptr)
21718 sym->maybe_copied = 1;
21719 }
21720
21721 /* A variable with DW_AT_external is never static,
21722 but it may be block-scoped. */
21723 list_to_add
21724 = ((cu->list_in_scope
21725 == cu->get_builder ()->get_file_symbols ())
21726 ? cu->get_builder ()->get_global_symbols ()
21727 : cu->list_in_scope);
21728 }
21729 else
21730 list_to_add = cu->list_in_scope;
21731 }
21732 else
21733 {
21734 /* We do not know the address of this symbol.
21735 If it is an external symbol and we have type information
21736 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21737 The address of the variable will then be determined from
21738 the minimal symbol table whenever the variable is
21739 referenced. */
21740 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21741
21742 /* Fortran explicitly imports any global symbols to the local
21743 scope by DW_TAG_common_block. */
21744 if (cu->per_cu->lang == language_fortran && die->parent
21745 && die->parent->tag == DW_TAG_common_block)
21746 {
21747 /* SYMBOL_CLASS doesn't matter here because
21748 read_common_block is going to reset it. */
21749 if (!suppress_add)
21750 list_to_add = cu->list_in_scope;
21751 }
21752 else if (attr2 != nullptr && attr2->as_boolean ()
21753 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21754 {
21755 /* A variable with DW_AT_external is never static, but it
21756 may be block-scoped. */
21757 list_to_add
21758 = ((cu->list_in_scope
21759 == cu->get_builder ()->get_file_symbols ())
21760 ? cu->get_builder ()->get_global_symbols ()
21761 : cu->list_in_scope);
21762
21763 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21764 }
21765 else if (!die_is_declaration (die, cu))
21766 {
21767 /* Use the default LOC_OPTIMIZED_OUT class. */
21768 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21769 if (!suppress_add)
21770 list_to_add = cu->list_in_scope;
21771 }
21772 }
21773 break;
21774 case DW_TAG_formal_parameter:
21775 {
21776 /* If we are inside a function, mark this as an argument. If
21777 not, we might be looking at an argument to an inlined function
21778 when we do not have enough information to show inlined frames;
21779 pretend it's a local variable in that case so that the user can
21780 still see it. */
21781 struct context_stack *curr
21782 = cu->get_builder ()->get_current_context_stack ();
21783 if (curr != nullptr && curr->name != nullptr)
21784 SYMBOL_IS_ARGUMENT (sym) = 1;
21785 attr = dwarf2_attr (die, DW_AT_location, cu);
21786 if (attr != nullptr)
21787 {
21788 var_decode_location (attr, sym, cu);
21789 }
21790 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21791 if (attr != nullptr)
21792 {
21793 dwarf2_const_value (attr, sym, cu);
21794 }
21795
21796 list_to_add = cu->list_in_scope;
21797 }
21798 break;
21799 case DW_TAG_unspecified_parameters:
21800 /* From varargs functions; gdb doesn't seem to have any
21801 interest in this information, so just ignore it for now.
21802 (FIXME?) */
21803 break;
21804 case DW_TAG_template_type_param:
21805 suppress_add = 1;
21806 /* Fall through. */
21807 case DW_TAG_class_type:
21808 case DW_TAG_interface_type:
21809 case DW_TAG_structure_type:
21810 case DW_TAG_union_type:
21811 case DW_TAG_set_type:
21812 case DW_TAG_enumeration_type:
21813 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21814 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21815
21816 {
21817 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21818 really ever be static objects: otherwise, if you try
21819 to, say, break of a class's method and you're in a file
21820 which doesn't mention that class, it won't work unless
21821 the check for all static symbols in lookup_symbol_aux
21822 saves you. See the OtherFileClass tests in
21823 gdb.c++/namespace.exp. */
21824
21825 if (!suppress_add)
21826 {
21827 buildsym_compunit *builder = cu->get_builder ();
21828 list_to_add
21829 = (cu->list_in_scope == builder->get_file_symbols ()
21830 && cu->per_cu->lang == language_cplus
21831 ? builder->get_global_symbols ()
21832 : cu->list_in_scope);
21833
21834 /* The semantics of C++ state that "struct foo {
21835 ... }" also defines a typedef for "foo". */
21836 if (cu->per_cu->lang == language_cplus
21837 || cu->per_cu->lang == language_ada
21838 || cu->per_cu->lang == language_d
21839 || cu->per_cu->lang == language_rust)
21840 {
21841 /* The symbol's name is already allocated along
21842 with this objfile, so we don't need to
21843 duplicate it for the type. */
21844 if (SYMBOL_TYPE (sym)->name () == 0)
21845 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21846 }
21847 }
21848 }
21849 break;
21850 case DW_TAG_typedef:
21851 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21852 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21853 list_to_add = cu->list_in_scope;
21854 break;
21855 case DW_TAG_array_type:
21856 case DW_TAG_base_type:
21857 case DW_TAG_subrange_type:
21858 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21859 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21860 list_to_add = cu->list_in_scope;
21861 break;
21862 case DW_TAG_enumerator:
21863 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21864 if (attr != nullptr)
21865 {
21866 dwarf2_const_value (attr, sym, cu);
21867 }
21868 {
21869 /* NOTE: carlton/2003-11-10: See comment above in the
21870 DW_TAG_class_type, etc. block. */
21871
21872 list_to_add
21873 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21874 && cu->per_cu->lang == language_cplus
21875 ? cu->get_builder ()->get_global_symbols ()
21876 : cu->list_in_scope);
21877 }
21878 break;
21879 case DW_TAG_imported_declaration:
21880 case DW_TAG_namespace:
21881 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21882 list_to_add = cu->get_builder ()->get_global_symbols ();
21883 break;
21884 case DW_TAG_module:
21885 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21886 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21887 list_to_add = cu->get_builder ()->get_global_symbols ();
21888 break;
21889 case DW_TAG_common_block:
21890 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21891 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21892 add_symbol_to_list (sym, cu->list_in_scope);
21893 break;
21894 default:
21895 /* Not a tag we recognize. Hopefully we aren't processing
21896 trash data, but since we must specifically ignore things
21897 we don't recognize, there is nothing else we should do at
21898 this point. */
21899 complaint (_("unsupported tag: '%s'"),
21900 dwarf_tag_name (die->tag));
21901 break;
21902 }
21903
21904 if (suppress_add)
21905 {
21906 sym->hash_next = objfile->template_symbols;
21907 objfile->template_symbols = sym;
21908 list_to_add = NULL;
21909 }
21910
21911 if (list_to_add != NULL)
21912 add_symbol_to_list (sym, list_to_add);
21913
21914 /* For the benefit of old versions of GCC, check for anonymous
21915 namespaces based on the demangled name. */
21916 if (!cu->processing_has_namespace_info
21917 && cu->per_cu->lang == language_cplus)
21918 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21919 }
21920 return (sym);
21921 }
21922
21923 /* Given an attr with a DW_FORM_dataN value in host byte order,
21924 zero-extend it as appropriate for the symbol's type. The DWARF
21925 standard (v4) is not entirely clear about the meaning of using
21926 DW_FORM_dataN for a constant with a signed type, where the type is
21927 wider than the data. The conclusion of a discussion on the DWARF
21928 list was that this is unspecified. We choose to always zero-extend
21929 because that is the interpretation long in use by GCC. */
21930
21931 static gdb_byte *
21932 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21933 struct dwarf2_cu *cu, LONGEST *value, int bits)
21934 {
21935 struct objfile *objfile = cu->per_objfile->objfile;
21936 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21937 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21938 LONGEST l = attr->constant_value (0);
21939
21940 if (bits < sizeof (*value) * 8)
21941 {
21942 l &= ((LONGEST) 1 << bits) - 1;
21943 *value = l;
21944 }
21945 else if (bits == sizeof (*value) * 8)
21946 *value = l;
21947 else
21948 {
21949 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21950 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21951 return bytes;
21952 }
21953
21954 return NULL;
21955 }
21956
21957 /* Read a constant value from an attribute. Either set *VALUE, or if
21958 the value does not fit in *VALUE, set *BYTES - either already
21959 allocated on the objfile obstack, or newly allocated on OBSTACK,
21960 or, set *BATON, if we translated the constant to a location
21961 expression. */
21962
21963 static void
21964 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21965 const char *name, struct obstack *obstack,
21966 struct dwarf2_cu *cu,
21967 LONGEST *value, const gdb_byte **bytes,
21968 struct dwarf2_locexpr_baton **baton)
21969 {
21970 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21971 struct objfile *objfile = per_objfile->objfile;
21972 struct comp_unit_head *cu_header = &cu->header;
21973 struct dwarf_block *blk;
21974 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21975 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21976
21977 *value = 0;
21978 *bytes = NULL;
21979 *baton = NULL;
21980
21981 switch (attr->form)
21982 {
21983 case DW_FORM_addr:
21984 case DW_FORM_addrx:
21985 case DW_FORM_GNU_addr_index:
21986 {
21987 gdb_byte *data;
21988
21989 if (TYPE_LENGTH (type) != cu_header->addr_size)
21990 dwarf2_const_value_length_mismatch_complaint (name,
21991 cu_header->addr_size,
21992 TYPE_LENGTH (type));
21993 /* Symbols of this form are reasonably rare, so we just
21994 piggyback on the existing location code rather than writing
21995 a new implementation of symbol_computed_ops. */
21996 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21997 (*baton)->per_objfile = per_objfile;
21998 (*baton)->per_cu = cu->per_cu;
21999 gdb_assert ((*baton)->per_cu);
22000
22001 (*baton)->size = 2 + cu_header->addr_size;
22002 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22003 (*baton)->data = data;
22004
22005 data[0] = DW_OP_addr;
22006 store_unsigned_integer (&data[1], cu_header->addr_size,
22007 byte_order, attr->as_address ());
22008 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22009 }
22010 break;
22011 case DW_FORM_string:
22012 case DW_FORM_strp:
22013 case DW_FORM_strx:
22014 case DW_FORM_GNU_str_index:
22015 case DW_FORM_GNU_strp_alt:
22016 /* The string is already allocated on the objfile obstack, point
22017 directly to it. */
22018 *bytes = (const gdb_byte *) attr->as_string ();
22019 break;
22020 case DW_FORM_block1:
22021 case DW_FORM_block2:
22022 case DW_FORM_block4:
22023 case DW_FORM_block:
22024 case DW_FORM_exprloc:
22025 case DW_FORM_data16:
22026 blk = attr->as_block ();
22027 if (TYPE_LENGTH (type) != blk->size)
22028 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22029 TYPE_LENGTH (type));
22030 *bytes = blk->data;
22031 break;
22032
22033 /* The DW_AT_const_value attributes are supposed to carry the
22034 symbol's value "represented as it would be on the target
22035 architecture." By the time we get here, it's already been
22036 converted to host endianness, so we just need to sign- or
22037 zero-extend it as appropriate. */
22038 case DW_FORM_data1:
22039 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22040 break;
22041 case DW_FORM_data2:
22042 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22043 break;
22044 case DW_FORM_data4:
22045 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22046 break;
22047 case DW_FORM_data8:
22048 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22049 break;
22050
22051 case DW_FORM_sdata:
22052 case DW_FORM_implicit_const:
22053 *value = attr->as_signed ();
22054 break;
22055
22056 case DW_FORM_udata:
22057 *value = attr->as_unsigned ();
22058 break;
22059
22060 default:
22061 complaint (_("unsupported const value attribute form: '%s'"),
22062 dwarf_form_name (attr->form));
22063 *value = 0;
22064 break;
22065 }
22066 }
22067
22068
22069 /* Copy constant value from an attribute to a symbol. */
22070
22071 static void
22072 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22073 struct dwarf2_cu *cu)
22074 {
22075 struct objfile *objfile = cu->per_objfile->objfile;
22076 LONGEST value;
22077 const gdb_byte *bytes;
22078 struct dwarf2_locexpr_baton *baton;
22079
22080 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22081 sym->print_name (),
22082 &objfile->objfile_obstack, cu,
22083 &value, &bytes, &baton);
22084
22085 if (baton != NULL)
22086 {
22087 SYMBOL_LOCATION_BATON (sym) = baton;
22088 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22089 }
22090 else if (bytes != NULL)
22091 {
22092 SYMBOL_VALUE_BYTES (sym) = bytes;
22093 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22094 }
22095 else
22096 {
22097 SYMBOL_VALUE (sym) = value;
22098 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22099 }
22100 }
22101
22102 /* Return the type of the die in question using its DW_AT_type attribute. */
22103
22104 static struct type *
22105 die_type (struct die_info *die, struct dwarf2_cu *cu)
22106 {
22107 struct attribute *type_attr;
22108
22109 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22110 if (!type_attr)
22111 {
22112 struct objfile *objfile = cu->per_objfile->objfile;
22113 /* A missing DW_AT_type represents a void type. */
22114 return objfile_type (objfile)->builtin_void;
22115 }
22116
22117 return lookup_die_type (die, type_attr, cu);
22118 }
22119
22120 /* True iff CU's producer generates GNAT Ada auxiliary information
22121 that allows to find parallel types through that information instead
22122 of having to do expensive parallel lookups by type name. */
22123
22124 static int
22125 need_gnat_info (struct dwarf2_cu *cu)
22126 {
22127 /* Assume that the Ada compiler was GNAT, which always produces
22128 the auxiliary information. */
22129 return (cu->per_cu->lang == language_ada);
22130 }
22131
22132 /* Return the auxiliary type of the die in question using its
22133 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22134 attribute is not present. */
22135
22136 static struct type *
22137 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22138 {
22139 struct attribute *type_attr;
22140
22141 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22142 if (!type_attr)
22143 return NULL;
22144
22145 return lookup_die_type (die, type_attr, cu);
22146 }
22147
22148 /* If DIE has a descriptive_type attribute, then set the TYPE's
22149 descriptive type accordingly. */
22150
22151 static void
22152 set_descriptive_type (struct type *type, struct die_info *die,
22153 struct dwarf2_cu *cu)
22154 {
22155 struct type *descriptive_type = die_descriptive_type (die, cu);
22156
22157 if (descriptive_type)
22158 {
22159 ALLOCATE_GNAT_AUX_TYPE (type);
22160 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22161 }
22162 }
22163
22164 /* Return the containing type of the die in question using its
22165 DW_AT_containing_type attribute. */
22166
22167 static struct type *
22168 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22169 {
22170 struct attribute *type_attr;
22171 struct objfile *objfile = cu->per_objfile->objfile;
22172
22173 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22174 if (!type_attr)
22175 error (_("Dwarf Error: Problem turning containing type into gdb type "
22176 "[in module %s]"), objfile_name (objfile));
22177
22178 return lookup_die_type (die, type_attr, cu);
22179 }
22180
22181 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22182
22183 static struct type *
22184 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22185 {
22186 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22187 struct objfile *objfile = per_objfile->objfile;
22188 char *saved;
22189
22190 std::string message
22191 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22192 objfile_name (objfile),
22193 sect_offset_str (cu->header.sect_off),
22194 sect_offset_str (die->sect_off));
22195 saved = obstack_strdup (&objfile->objfile_obstack, message);
22196
22197 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22198 }
22199
22200 /* Look up the type of DIE in CU using its type attribute ATTR.
22201 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22202 DW_AT_containing_type.
22203 If there is no type substitute an error marker. */
22204
22205 static struct type *
22206 lookup_die_type (struct die_info *die, const struct attribute *attr,
22207 struct dwarf2_cu *cu)
22208 {
22209 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22210 struct objfile *objfile = per_objfile->objfile;
22211 struct type *this_type;
22212
22213 gdb_assert (attr->name == DW_AT_type
22214 || attr->name == DW_AT_GNAT_descriptive_type
22215 || attr->name == DW_AT_containing_type);
22216
22217 /* First see if we have it cached. */
22218
22219 if (attr->form == DW_FORM_GNU_ref_alt)
22220 {
22221 struct dwarf2_per_cu_data *per_cu;
22222 sect_offset sect_off = attr->get_ref_die_offset ();
22223
22224 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22225 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22226 }
22227 else if (attr->form_is_ref ())
22228 {
22229 sect_offset sect_off = attr->get_ref_die_offset ();
22230
22231 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22232 }
22233 else if (attr->form == DW_FORM_ref_sig8)
22234 {
22235 ULONGEST signature = attr->as_signature ();
22236
22237 return get_signatured_type (die, signature, cu);
22238 }
22239 else
22240 {
22241 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22242 " at %s [in module %s]"),
22243 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22244 objfile_name (objfile));
22245 return build_error_marker_type (cu, die);
22246 }
22247
22248 /* If not cached we need to read it in. */
22249
22250 if (this_type == NULL)
22251 {
22252 struct die_info *type_die = NULL;
22253 struct dwarf2_cu *type_cu = cu;
22254
22255 if (attr->form_is_ref ())
22256 type_die = follow_die_ref (die, attr, &type_cu);
22257 if (type_die == NULL)
22258 return build_error_marker_type (cu, die);
22259 /* If we find the type now, it's probably because the type came
22260 from an inter-CU reference and the type's CU got expanded before
22261 ours. */
22262 this_type = read_type_die (type_die, type_cu);
22263 }
22264
22265 /* If we still don't have a type use an error marker. */
22266
22267 if (this_type == NULL)
22268 return build_error_marker_type (cu, die);
22269
22270 return this_type;
22271 }
22272
22273 /* Return the type in DIE, CU.
22274 Returns NULL for invalid types.
22275
22276 This first does a lookup in die_type_hash,
22277 and only reads the die in if necessary.
22278
22279 NOTE: This can be called when reading in partial or full symbols. */
22280
22281 static struct type *
22282 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22283 {
22284 struct type *this_type;
22285
22286 this_type = get_die_type (die, cu);
22287 if (this_type)
22288 return this_type;
22289
22290 return read_type_die_1 (die, cu);
22291 }
22292
22293 /* Read the type in DIE, CU.
22294 Returns NULL for invalid types. */
22295
22296 static struct type *
22297 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22298 {
22299 struct type *this_type = NULL;
22300
22301 switch (die->tag)
22302 {
22303 case DW_TAG_class_type:
22304 case DW_TAG_interface_type:
22305 case DW_TAG_structure_type:
22306 case DW_TAG_union_type:
22307 this_type = read_structure_type (die, cu);
22308 break;
22309 case DW_TAG_enumeration_type:
22310 this_type = read_enumeration_type (die, cu);
22311 break;
22312 case DW_TAG_subprogram:
22313 case DW_TAG_subroutine_type:
22314 case DW_TAG_inlined_subroutine:
22315 this_type = read_subroutine_type (die, cu);
22316 break;
22317 case DW_TAG_array_type:
22318 this_type = read_array_type (die, cu);
22319 break;
22320 case DW_TAG_set_type:
22321 this_type = read_set_type (die, cu);
22322 break;
22323 case DW_TAG_pointer_type:
22324 this_type = read_tag_pointer_type (die, cu);
22325 break;
22326 case DW_TAG_ptr_to_member_type:
22327 this_type = read_tag_ptr_to_member_type (die, cu);
22328 break;
22329 case DW_TAG_reference_type:
22330 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22331 break;
22332 case DW_TAG_rvalue_reference_type:
22333 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22334 break;
22335 case DW_TAG_const_type:
22336 this_type = read_tag_const_type (die, cu);
22337 break;
22338 case DW_TAG_volatile_type:
22339 this_type = read_tag_volatile_type (die, cu);
22340 break;
22341 case DW_TAG_restrict_type:
22342 this_type = read_tag_restrict_type (die, cu);
22343 break;
22344 case DW_TAG_string_type:
22345 this_type = read_tag_string_type (die, cu);
22346 break;
22347 case DW_TAG_typedef:
22348 this_type = read_typedef (die, cu);
22349 break;
22350 case DW_TAG_subrange_type:
22351 this_type = read_subrange_type (die, cu);
22352 break;
22353 case DW_TAG_base_type:
22354 this_type = read_base_type (die, cu);
22355 break;
22356 case DW_TAG_unspecified_type:
22357 this_type = read_unspecified_type (die, cu);
22358 break;
22359 case DW_TAG_namespace:
22360 this_type = read_namespace_type (die, cu);
22361 break;
22362 case DW_TAG_module:
22363 this_type = read_module_type (die, cu);
22364 break;
22365 case DW_TAG_atomic_type:
22366 this_type = read_tag_atomic_type (die, cu);
22367 break;
22368 default:
22369 complaint (_("unexpected tag in read_type_die: '%s'"),
22370 dwarf_tag_name (die->tag));
22371 break;
22372 }
22373
22374 return this_type;
22375 }
22376
22377 /* See if we can figure out if the class lives in a namespace. We do
22378 this by looking for a member function; its demangled name will
22379 contain namespace info, if there is any.
22380 Return the computed name or NULL.
22381 Space for the result is allocated on the objfile's obstack.
22382 This is the full-die version of guess_partial_die_structure_name.
22383 In this case we know DIE has no useful parent. */
22384
22385 static const char *
22386 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22387 {
22388 struct die_info *spec_die;
22389 struct dwarf2_cu *spec_cu;
22390 struct die_info *child;
22391 struct objfile *objfile = cu->per_objfile->objfile;
22392
22393 spec_cu = cu;
22394 spec_die = die_specification (die, &spec_cu);
22395 if (spec_die != NULL)
22396 {
22397 die = spec_die;
22398 cu = spec_cu;
22399 }
22400
22401 for (child = die->child;
22402 child != NULL;
22403 child = child->sibling)
22404 {
22405 if (child->tag == DW_TAG_subprogram)
22406 {
22407 const char *linkage_name = dw2_linkage_name (child, cu);
22408
22409 if (linkage_name != NULL)
22410 {
22411 gdb::unique_xmalloc_ptr<char> actual_name
22412 (cu->language_defn->class_name_from_physname (linkage_name));
22413 const char *name = NULL;
22414
22415 if (actual_name != NULL)
22416 {
22417 const char *die_name = dwarf2_name (die, cu);
22418
22419 if (die_name != NULL
22420 && strcmp (die_name, actual_name.get ()) != 0)
22421 {
22422 /* Strip off the class name from the full name.
22423 We want the prefix. */
22424 int die_name_len = strlen (die_name);
22425 int actual_name_len = strlen (actual_name.get ());
22426 const char *ptr = actual_name.get ();
22427
22428 /* Test for '::' as a sanity check. */
22429 if (actual_name_len > die_name_len + 2
22430 && ptr[actual_name_len - die_name_len - 1] == ':')
22431 name = obstack_strndup (
22432 &objfile->per_bfd->storage_obstack,
22433 ptr, actual_name_len - die_name_len - 2);
22434 }
22435 }
22436 return name;
22437 }
22438 }
22439 }
22440
22441 return NULL;
22442 }
22443
22444 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22445 prefix part in such case. See
22446 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22447
22448 static const char *
22449 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22450 {
22451 struct attribute *attr;
22452 const char *base;
22453
22454 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22455 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22456 return NULL;
22457
22458 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22459 return NULL;
22460
22461 attr = dw2_linkage_name_attr (die, cu);
22462 const char *attr_name = attr->as_string ();
22463 if (attr == NULL || attr_name == NULL)
22464 return NULL;
22465
22466 /* dwarf2_name had to be already called. */
22467 gdb_assert (attr->canonical_string_p ());
22468
22469 /* Strip the base name, keep any leading namespaces/classes. */
22470 base = strrchr (attr_name, ':');
22471 if (base == NULL || base == attr_name || base[-1] != ':')
22472 return "";
22473
22474 struct objfile *objfile = cu->per_objfile->objfile;
22475 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22476 attr_name,
22477 &base[-1] - attr_name);
22478 }
22479
22480 /* Return the name of the namespace/class that DIE is defined within,
22481 or "" if we can't tell. The caller should not xfree the result.
22482
22483 For example, if we're within the method foo() in the following
22484 code:
22485
22486 namespace N {
22487 class C {
22488 void foo () {
22489 }
22490 };
22491 }
22492
22493 then determine_prefix on foo's die will return "N::C". */
22494
22495 static const char *
22496 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22497 {
22498 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22499 struct die_info *parent, *spec_die;
22500 struct dwarf2_cu *spec_cu;
22501 struct type *parent_type;
22502 const char *retval;
22503
22504 if (cu->per_cu->lang != language_cplus
22505 && cu->per_cu->lang != language_fortran
22506 && cu->per_cu->lang != language_d
22507 && cu->per_cu->lang != language_rust)
22508 return "";
22509
22510 retval = anonymous_struct_prefix (die, cu);
22511 if (retval)
22512 return retval;
22513
22514 /* We have to be careful in the presence of DW_AT_specification.
22515 For example, with GCC 3.4, given the code
22516
22517 namespace N {
22518 void foo() {
22519 // Definition of N::foo.
22520 }
22521 }
22522
22523 then we'll have a tree of DIEs like this:
22524
22525 1: DW_TAG_compile_unit
22526 2: DW_TAG_namespace // N
22527 3: DW_TAG_subprogram // declaration of N::foo
22528 4: DW_TAG_subprogram // definition of N::foo
22529 DW_AT_specification // refers to die #3
22530
22531 Thus, when processing die #4, we have to pretend that we're in
22532 the context of its DW_AT_specification, namely the contex of die
22533 #3. */
22534 spec_cu = cu;
22535 spec_die = die_specification (die, &spec_cu);
22536 if (spec_die == NULL)
22537 parent = die->parent;
22538 else
22539 {
22540 parent = spec_die->parent;
22541 cu = spec_cu;
22542 }
22543
22544 if (parent == NULL)
22545 return "";
22546 else if (parent->building_fullname)
22547 {
22548 const char *name;
22549 const char *parent_name;
22550
22551 /* It has been seen on RealView 2.2 built binaries,
22552 DW_TAG_template_type_param types actually _defined_ as
22553 children of the parent class:
22554
22555 enum E {};
22556 template class <class Enum> Class{};
22557 Class<enum E> class_e;
22558
22559 1: DW_TAG_class_type (Class)
22560 2: DW_TAG_enumeration_type (E)
22561 3: DW_TAG_enumerator (enum1:0)
22562 3: DW_TAG_enumerator (enum2:1)
22563 ...
22564 2: DW_TAG_template_type_param
22565 DW_AT_type DW_FORM_ref_udata (E)
22566
22567 Besides being broken debug info, it can put GDB into an
22568 infinite loop. Consider:
22569
22570 When we're building the full name for Class<E>, we'll start
22571 at Class, and go look over its template type parameters,
22572 finding E. We'll then try to build the full name of E, and
22573 reach here. We're now trying to build the full name of E,
22574 and look over the parent DIE for containing scope. In the
22575 broken case, if we followed the parent DIE of E, we'd again
22576 find Class, and once again go look at its template type
22577 arguments, etc., etc. Simply don't consider such parent die
22578 as source-level parent of this die (it can't be, the language
22579 doesn't allow it), and break the loop here. */
22580 name = dwarf2_name (die, cu);
22581 parent_name = dwarf2_name (parent, cu);
22582 complaint (_("template param type '%s' defined within parent '%s'"),
22583 name ? name : "<unknown>",
22584 parent_name ? parent_name : "<unknown>");
22585 return "";
22586 }
22587 else
22588 switch (parent->tag)
22589 {
22590 case DW_TAG_namespace:
22591 parent_type = read_type_die (parent, cu);
22592 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22593 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22594 Work around this problem here. */
22595 if (cu->per_cu->lang == language_cplus
22596 && strcmp (parent_type->name (), "::") == 0)
22597 return "";
22598 /* We give a name to even anonymous namespaces. */
22599 return parent_type->name ();
22600 case DW_TAG_class_type:
22601 case DW_TAG_interface_type:
22602 case DW_TAG_structure_type:
22603 case DW_TAG_union_type:
22604 case DW_TAG_module:
22605 parent_type = read_type_die (parent, cu);
22606 if (parent_type->name () != NULL)
22607 return parent_type->name ();
22608 else
22609 /* An anonymous structure is only allowed non-static data
22610 members; no typedefs, no member functions, et cetera.
22611 So it does not need a prefix. */
22612 return "";
22613 case DW_TAG_compile_unit:
22614 case DW_TAG_partial_unit:
22615 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22616 if (cu->per_cu->lang == language_cplus
22617 && !per_objfile->per_bfd->types.empty ()
22618 && die->child != NULL
22619 && (die->tag == DW_TAG_class_type
22620 || die->tag == DW_TAG_structure_type
22621 || die->tag == DW_TAG_union_type))
22622 {
22623 const char *name = guess_full_die_structure_name (die, cu);
22624 if (name != NULL)
22625 return name;
22626 }
22627 return "";
22628 case DW_TAG_subprogram:
22629 /* Nested subroutines in Fortran get a prefix with the name
22630 of the parent's subroutine. */
22631 if (cu->per_cu->lang == language_fortran)
22632 {
22633 if ((die->tag == DW_TAG_subprogram)
22634 && (dwarf2_name (parent, cu) != NULL))
22635 return dwarf2_name (parent, cu);
22636 }
22637 return determine_prefix (parent, cu);
22638 case DW_TAG_enumeration_type:
22639 parent_type = read_type_die (parent, cu);
22640 if (parent_type->is_declared_class ())
22641 {
22642 if (parent_type->name () != NULL)
22643 return parent_type->name ();
22644 return "";
22645 }
22646 /* Fall through. */
22647 default:
22648 return determine_prefix (parent, cu);
22649 }
22650 }
22651
22652 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22653 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22654 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22655 an obconcat, otherwise allocate storage for the result. The CU argument is
22656 used to determine the language and hence, the appropriate separator. */
22657
22658 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22659
22660 static char *
22661 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22662 int physname, struct dwarf2_cu *cu)
22663 {
22664 const char *lead = "";
22665 const char *sep;
22666
22667 if (suffix == NULL || suffix[0] == '\0'
22668 || prefix == NULL || prefix[0] == '\0')
22669 sep = "";
22670 else if (cu->per_cu->lang == language_d)
22671 {
22672 /* For D, the 'main' function could be defined in any module, but it
22673 should never be prefixed. */
22674 if (strcmp (suffix, "D main") == 0)
22675 {
22676 prefix = "";
22677 sep = "";
22678 }
22679 else
22680 sep = ".";
22681 }
22682 else if (cu->per_cu->lang == language_fortran && physname)
22683 {
22684 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22685 DW_AT_MIPS_linkage_name is preferred and used instead. */
22686
22687 lead = "__";
22688 sep = "_MOD_";
22689 }
22690 else
22691 sep = "::";
22692
22693 if (prefix == NULL)
22694 prefix = "";
22695 if (suffix == NULL)
22696 suffix = "";
22697
22698 if (obs == NULL)
22699 {
22700 char *retval
22701 = ((char *)
22702 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22703
22704 strcpy (retval, lead);
22705 strcat (retval, prefix);
22706 strcat (retval, sep);
22707 strcat (retval, suffix);
22708 return retval;
22709 }
22710 else
22711 {
22712 /* We have an obstack. */
22713 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22714 }
22715 }
22716
22717 /* Get name of a die, return NULL if not found. */
22718
22719 static const char *
22720 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22721 struct objfile *objfile)
22722 {
22723 if (name && cu->per_cu->lang == language_cplus)
22724 {
22725 gdb::unique_xmalloc_ptr<char> canon_name
22726 = cp_canonicalize_string (name);
22727
22728 if (canon_name != nullptr)
22729 name = objfile->intern (canon_name.get ());
22730 }
22731
22732 return name;
22733 }
22734
22735 /* Get name of a die, return NULL if not found.
22736 Anonymous namespaces are converted to their magic string. */
22737
22738 static const char *
22739 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22740 {
22741 struct attribute *attr;
22742 struct objfile *objfile = cu->per_objfile->objfile;
22743
22744 attr = dwarf2_attr (die, DW_AT_name, cu);
22745 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22746 if (attr_name == nullptr
22747 && die->tag != DW_TAG_namespace
22748 && die->tag != DW_TAG_class_type
22749 && die->tag != DW_TAG_interface_type
22750 && die->tag != DW_TAG_structure_type
22751 && die->tag != DW_TAG_union_type)
22752 return NULL;
22753
22754 switch (die->tag)
22755 {
22756 case DW_TAG_compile_unit:
22757 case DW_TAG_partial_unit:
22758 /* Compilation units have a DW_AT_name that is a filename, not
22759 a source language identifier. */
22760 case DW_TAG_enumeration_type:
22761 case DW_TAG_enumerator:
22762 /* These tags always have simple identifiers already; no need
22763 to canonicalize them. */
22764 return attr_name;
22765
22766 case DW_TAG_namespace:
22767 if (attr_name != nullptr)
22768 return attr_name;
22769 return CP_ANONYMOUS_NAMESPACE_STR;
22770
22771 case DW_TAG_class_type:
22772 case DW_TAG_interface_type:
22773 case DW_TAG_structure_type:
22774 case DW_TAG_union_type:
22775 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22776 structures or unions. These were of the form "._%d" in GCC 4.1,
22777 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22778 and GCC 4.4. We work around this problem by ignoring these. */
22779 if (attr_name != nullptr
22780 && (startswith (attr_name, "._")
22781 || startswith (attr_name, "<anonymous")))
22782 return NULL;
22783
22784 /* GCC might emit a nameless typedef that has a linkage name. See
22785 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22786 if (!attr || attr_name == NULL)
22787 {
22788 attr = dw2_linkage_name_attr (die, cu);
22789 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22790 if (attr == NULL || attr_name == NULL)
22791 return NULL;
22792
22793 /* Avoid demangling attr_name the second time on a second
22794 call for the same DIE. */
22795 if (!attr->canonical_string_p ())
22796 {
22797 gdb::unique_xmalloc_ptr<char> demangled
22798 (gdb_demangle (attr_name, DMGL_TYPES));
22799 if (demangled == nullptr)
22800 return nullptr;
22801
22802 attr->set_string_canonical (objfile->intern (demangled.get ()));
22803 attr_name = attr->as_string ();
22804 }
22805
22806 /* Strip any leading namespaces/classes, keep only the
22807 base name. DW_AT_name for named DIEs does not
22808 contain the prefixes. */
22809 const char *base = strrchr (attr_name, ':');
22810 if (base && base > attr_name && base[-1] == ':')
22811 return &base[1];
22812 else
22813 return attr_name;
22814 }
22815 break;
22816
22817 default:
22818 break;
22819 }
22820
22821 if (!attr->canonical_string_p ())
22822 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22823 objfile));
22824 return attr->as_string ();
22825 }
22826
22827 /* Return the die that this die in an extension of, or NULL if there
22828 is none. *EXT_CU is the CU containing DIE on input, and the CU
22829 containing the return value on output. */
22830
22831 static struct die_info *
22832 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22833 {
22834 struct attribute *attr;
22835
22836 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22837 if (attr == NULL)
22838 return NULL;
22839
22840 return follow_die_ref (die, attr, ext_cu);
22841 }
22842
22843 static void
22844 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22845 {
22846 unsigned int i;
22847
22848 print_spaces (indent, f);
22849 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22850 dwarf_tag_name (die->tag), die->abbrev,
22851 sect_offset_str (die->sect_off));
22852
22853 if (die->parent != NULL)
22854 {
22855 print_spaces (indent, f);
22856 fprintf_unfiltered (f, " parent at offset: %s\n",
22857 sect_offset_str (die->parent->sect_off));
22858 }
22859
22860 print_spaces (indent, f);
22861 fprintf_unfiltered (f, " has children: %s\n",
22862 dwarf_bool_name (die->child != NULL));
22863
22864 print_spaces (indent, f);
22865 fprintf_unfiltered (f, " attributes:\n");
22866
22867 for (i = 0; i < die->num_attrs; ++i)
22868 {
22869 print_spaces (indent, f);
22870 fprintf_unfiltered (f, " %s (%s) ",
22871 dwarf_attr_name (die->attrs[i].name),
22872 dwarf_form_name (die->attrs[i].form));
22873
22874 switch (die->attrs[i].form)
22875 {
22876 case DW_FORM_addr:
22877 case DW_FORM_addrx:
22878 case DW_FORM_GNU_addr_index:
22879 fprintf_unfiltered (f, "address: ");
22880 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
22881 break;
22882 case DW_FORM_block2:
22883 case DW_FORM_block4:
22884 case DW_FORM_block:
22885 case DW_FORM_block1:
22886 fprintf_unfiltered (f, "block: size %s",
22887 pulongest (die->attrs[i].as_block ()->size));
22888 break;
22889 case DW_FORM_exprloc:
22890 fprintf_unfiltered (f, "expression: size %s",
22891 pulongest (die->attrs[i].as_block ()->size));
22892 break;
22893 case DW_FORM_data16:
22894 fprintf_unfiltered (f, "constant of 16 bytes");
22895 break;
22896 case DW_FORM_ref_addr:
22897 fprintf_unfiltered (f, "ref address: ");
22898 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
22899 break;
22900 case DW_FORM_GNU_ref_alt:
22901 fprintf_unfiltered (f, "alt ref address: ");
22902 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
22903 break;
22904 case DW_FORM_ref1:
22905 case DW_FORM_ref2:
22906 case DW_FORM_ref4:
22907 case DW_FORM_ref8:
22908 case DW_FORM_ref_udata:
22909 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22910 (long) (die->attrs[i].as_unsigned ()));
22911 break;
22912 case DW_FORM_data1:
22913 case DW_FORM_data2:
22914 case DW_FORM_data4:
22915 case DW_FORM_data8:
22916 case DW_FORM_udata:
22917 fprintf_unfiltered (f, "constant: %s",
22918 pulongest (die->attrs[i].as_unsigned ()));
22919 break;
22920 case DW_FORM_sec_offset:
22921 fprintf_unfiltered (f, "section offset: %s",
22922 pulongest (die->attrs[i].as_unsigned ()));
22923 break;
22924 case DW_FORM_ref_sig8:
22925 fprintf_unfiltered (f, "signature: %s",
22926 hex_string (die->attrs[i].as_signature ()));
22927 break;
22928 case DW_FORM_string:
22929 case DW_FORM_strp:
22930 case DW_FORM_line_strp:
22931 case DW_FORM_strx:
22932 case DW_FORM_GNU_str_index:
22933 case DW_FORM_GNU_strp_alt:
22934 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22935 die->attrs[i].as_string ()
22936 ? die->attrs[i].as_string () : "",
22937 die->attrs[i].canonical_string_p () ? "is" : "not");
22938 break;
22939 case DW_FORM_flag:
22940 if (die->attrs[i].as_boolean ())
22941 fprintf_unfiltered (f, "flag: TRUE");
22942 else
22943 fprintf_unfiltered (f, "flag: FALSE");
22944 break;
22945 case DW_FORM_flag_present:
22946 fprintf_unfiltered (f, "flag: TRUE");
22947 break;
22948 case DW_FORM_indirect:
22949 /* The reader will have reduced the indirect form to
22950 the "base form" so this form should not occur. */
22951 fprintf_unfiltered (f,
22952 "unexpected attribute form: DW_FORM_indirect");
22953 break;
22954 case DW_FORM_sdata:
22955 case DW_FORM_implicit_const:
22956 fprintf_unfiltered (f, "constant: %s",
22957 plongest (die->attrs[i].as_signed ()));
22958 break;
22959 default:
22960 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22961 die->attrs[i].form);
22962 break;
22963 }
22964 fprintf_unfiltered (f, "\n");
22965 }
22966 }
22967
22968 static void
22969 dump_die_for_error (struct die_info *die)
22970 {
22971 dump_die_shallow (gdb_stderr, 0, die);
22972 }
22973
22974 static void
22975 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22976 {
22977 int indent = level * 4;
22978
22979 gdb_assert (die != NULL);
22980
22981 if (level >= max_level)
22982 return;
22983
22984 dump_die_shallow (f, indent, die);
22985
22986 if (die->child != NULL)
22987 {
22988 print_spaces (indent, f);
22989 fprintf_unfiltered (f, " Children:");
22990 if (level + 1 < max_level)
22991 {
22992 fprintf_unfiltered (f, "\n");
22993 dump_die_1 (f, level + 1, max_level, die->child);
22994 }
22995 else
22996 {
22997 fprintf_unfiltered (f,
22998 " [not printed, max nesting level reached]\n");
22999 }
23000 }
23001
23002 if (die->sibling != NULL && level > 0)
23003 {
23004 dump_die_1 (f, level, max_level, die->sibling);
23005 }
23006 }
23007
23008 /* This is called from the pdie macro in gdbinit.in.
23009 It's not static so gcc will keep a copy callable from gdb. */
23010
23011 void
23012 dump_die (struct die_info *die, int max_level)
23013 {
23014 dump_die_1 (gdb_stdlog, 0, max_level, die);
23015 }
23016
23017 static void
23018 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23019 {
23020 void **slot;
23021
23022 slot = htab_find_slot_with_hash (cu->die_hash, die,
23023 to_underlying (die->sect_off),
23024 INSERT);
23025
23026 *slot = die;
23027 }
23028
23029 /* Follow reference or signature attribute ATTR of SRC_DIE.
23030 On entry *REF_CU is the CU of SRC_DIE.
23031 On exit *REF_CU is the CU of the result. */
23032
23033 static struct die_info *
23034 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23035 struct dwarf2_cu **ref_cu)
23036 {
23037 struct die_info *die;
23038
23039 if (attr->form_is_ref ())
23040 die = follow_die_ref (src_die, attr, ref_cu);
23041 else if (attr->form == DW_FORM_ref_sig8)
23042 die = follow_die_sig (src_die, attr, ref_cu);
23043 else
23044 {
23045 dump_die_for_error (src_die);
23046 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23047 objfile_name ((*ref_cu)->per_objfile->objfile));
23048 }
23049
23050 return die;
23051 }
23052
23053 /* Follow reference OFFSET.
23054 On entry *REF_CU is the CU of the source die referencing OFFSET.
23055 On exit *REF_CU is the CU of the result.
23056 Returns NULL if OFFSET is invalid. */
23057
23058 static struct die_info *
23059 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23060 struct dwarf2_cu **ref_cu)
23061 {
23062 struct die_info temp_die;
23063 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23064 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23065
23066 gdb_assert (cu->per_cu != NULL);
23067
23068 target_cu = cu;
23069
23070 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23071 "source CU contains target offset: %d",
23072 sect_offset_str (cu->per_cu->sect_off),
23073 sect_offset_str (sect_off),
23074 cu->header.offset_in_cu_p (sect_off));
23075
23076 if (cu->per_cu->is_debug_types)
23077 {
23078 /* .debug_types CUs cannot reference anything outside their CU.
23079 If they need to, they have to reference a signatured type via
23080 DW_FORM_ref_sig8. */
23081 if (!cu->header.offset_in_cu_p (sect_off))
23082 return NULL;
23083 }
23084 else if (offset_in_dwz != cu->per_cu->is_dwz
23085 || !cu->header.offset_in_cu_p (sect_off))
23086 {
23087 struct dwarf2_per_cu_data *per_cu;
23088
23089 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23090 per_objfile);
23091
23092 dwarf_read_debug_printf_v ("target CU offset: %s, "
23093 "target CU DIEs loaded: %d",
23094 sect_offset_str (per_cu->sect_off),
23095 per_objfile->get_cu (per_cu) != nullptr);
23096
23097 /* If necessary, add it to the queue and load its DIEs.
23098
23099 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23100 it doesn't mean they are currently loaded. Since we require them
23101 to be loaded, we must check for ourselves. */
23102 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
23103 || per_objfile->get_cu (per_cu) == nullptr)
23104 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23105 false, cu->per_cu->lang);
23106
23107 target_cu = per_objfile->get_cu (per_cu);
23108 gdb_assert (target_cu != nullptr);
23109 }
23110 else if (cu->dies == NULL)
23111 {
23112 /* We're loading full DIEs during partial symbol reading. */
23113 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23114 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23115 language_minimal);
23116 }
23117
23118 *ref_cu = target_cu;
23119 temp_die.sect_off = sect_off;
23120
23121 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23122 &temp_die,
23123 to_underlying (sect_off));
23124 }
23125
23126 /* Follow reference attribute ATTR of SRC_DIE.
23127 On entry *REF_CU is the CU of SRC_DIE.
23128 On exit *REF_CU is the CU of the result. */
23129
23130 static struct die_info *
23131 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23132 struct dwarf2_cu **ref_cu)
23133 {
23134 sect_offset sect_off = attr->get_ref_die_offset ();
23135 struct dwarf2_cu *cu = *ref_cu;
23136 struct die_info *die;
23137
23138 die = follow_die_offset (sect_off,
23139 (attr->form == DW_FORM_GNU_ref_alt
23140 || cu->per_cu->is_dwz),
23141 ref_cu);
23142 if (!die)
23143 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23144 "at %s [in module %s]"),
23145 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23146 objfile_name (cu->per_objfile->objfile));
23147
23148 return die;
23149 }
23150
23151 /* See read.h. */
23152
23153 struct dwarf2_locexpr_baton
23154 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23155 dwarf2_per_cu_data *per_cu,
23156 dwarf2_per_objfile *per_objfile,
23157 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23158 bool resolve_abstract_p)
23159 {
23160 struct die_info *die;
23161 struct attribute *attr;
23162 struct dwarf2_locexpr_baton retval;
23163 struct objfile *objfile = per_objfile->objfile;
23164
23165 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23166 if (cu == nullptr)
23167 cu = load_cu (per_cu, per_objfile, false);
23168
23169 if (cu == nullptr)
23170 {
23171 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23172 Instead just throw an error, not much else we can do. */
23173 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23174 sect_offset_str (sect_off), objfile_name (objfile));
23175 }
23176
23177 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23178 if (!die)
23179 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23180 sect_offset_str (sect_off), objfile_name (objfile));
23181
23182 attr = dwarf2_attr (die, DW_AT_location, cu);
23183 if (!attr && resolve_abstract_p
23184 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23185 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23186 {
23187 CORE_ADDR pc = get_frame_pc ();
23188 CORE_ADDR baseaddr = objfile->text_section_offset ();
23189 struct gdbarch *gdbarch = objfile->arch ();
23190
23191 for (const auto &cand_off
23192 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23193 {
23194 struct dwarf2_cu *cand_cu = cu;
23195 struct die_info *cand
23196 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23197 if (!cand
23198 || !cand->parent
23199 || cand->parent->tag != DW_TAG_subprogram)
23200 continue;
23201
23202 CORE_ADDR pc_low, pc_high;
23203 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23204 if (pc_low == ((CORE_ADDR) -1))
23205 continue;
23206 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23207 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23208 if (!(pc_low <= pc && pc < pc_high))
23209 continue;
23210
23211 die = cand;
23212 attr = dwarf2_attr (die, DW_AT_location, cu);
23213 break;
23214 }
23215 }
23216
23217 if (!attr)
23218 {
23219 /* DWARF: "If there is no such attribute, then there is no effect.".
23220 DATA is ignored if SIZE is 0. */
23221
23222 retval.data = NULL;
23223 retval.size = 0;
23224 }
23225 else if (attr->form_is_section_offset ())
23226 {
23227 struct dwarf2_loclist_baton loclist_baton;
23228 CORE_ADDR pc = get_frame_pc ();
23229 size_t size;
23230
23231 fill_in_loclist_baton (cu, &loclist_baton, attr);
23232
23233 retval.data = dwarf2_find_location_expression (&loclist_baton,
23234 &size, pc);
23235 retval.size = size;
23236 }
23237 else
23238 {
23239 if (!attr->form_is_block ())
23240 error (_("Dwarf Error: DIE at %s referenced in module %s "
23241 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23242 sect_offset_str (sect_off), objfile_name (objfile));
23243
23244 struct dwarf_block *block = attr->as_block ();
23245 retval.data = block->data;
23246 retval.size = block->size;
23247 }
23248 retval.per_objfile = per_objfile;
23249 retval.per_cu = cu->per_cu;
23250
23251 per_objfile->age_comp_units ();
23252
23253 return retval;
23254 }
23255
23256 /* See read.h. */
23257
23258 struct dwarf2_locexpr_baton
23259 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23260 dwarf2_per_cu_data *per_cu,
23261 dwarf2_per_objfile *per_objfile,
23262 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23263 {
23264 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23265
23266 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23267 get_frame_pc);
23268 }
23269
23270 /* Write a constant of a given type as target-ordered bytes into
23271 OBSTACK. */
23272
23273 static const gdb_byte *
23274 write_constant_as_bytes (struct obstack *obstack,
23275 enum bfd_endian byte_order,
23276 struct type *type,
23277 ULONGEST value,
23278 LONGEST *len)
23279 {
23280 gdb_byte *result;
23281
23282 *len = TYPE_LENGTH (type);
23283 result = (gdb_byte *) obstack_alloc (obstack, *len);
23284 store_unsigned_integer (result, *len, byte_order, value);
23285
23286 return result;
23287 }
23288
23289 /* See read.h. */
23290
23291 const gdb_byte *
23292 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23293 dwarf2_per_cu_data *per_cu,
23294 dwarf2_per_objfile *per_objfile,
23295 obstack *obstack,
23296 LONGEST *len)
23297 {
23298 struct die_info *die;
23299 struct attribute *attr;
23300 const gdb_byte *result = NULL;
23301 struct type *type;
23302 LONGEST value;
23303 enum bfd_endian byte_order;
23304 struct objfile *objfile = per_objfile->objfile;
23305
23306 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23307 if (cu == nullptr)
23308 cu = load_cu (per_cu, per_objfile, false);
23309
23310 if (cu == nullptr)
23311 {
23312 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23313 Instead just throw an error, not much else we can do. */
23314 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23315 sect_offset_str (sect_off), objfile_name (objfile));
23316 }
23317
23318 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23319 if (!die)
23320 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23321 sect_offset_str (sect_off), objfile_name (objfile));
23322
23323 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23324 if (attr == NULL)
23325 return NULL;
23326
23327 byte_order = (bfd_big_endian (objfile->obfd)
23328 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23329
23330 switch (attr->form)
23331 {
23332 case DW_FORM_addr:
23333 case DW_FORM_addrx:
23334 case DW_FORM_GNU_addr_index:
23335 {
23336 gdb_byte *tem;
23337
23338 *len = cu->header.addr_size;
23339 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23340 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23341 result = tem;
23342 }
23343 break;
23344 case DW_FORM_string:
23345 case DW_FORM_strp:
23346 case DW_FORM_strx:
23347 case DW_FORM_GNU_str_index:
23348 case DW_FORM_GNU_strp_alt:
23349 /* The string is already allocated on the objfile obstack, point
23350 directly to it. */
23351 {
23352 const char *attr_name = attr->as_string ();
23353 result = (const gdb_byte *) attr_name;
23354 *len = strlen (attr_name);
23355 }
23356 break;
23357 case DW_FORM_block1:
23358 case DW_FORM_block2:
23359 case DW_FORM_block4:
23360 case DW_FORM_block:
23361 case DW_FORM_exprloc:
23362 case DW_FORM_data16:
23363 {
23364 struct dwarf_block *block = attr->as_block ();
23365 result = block->data;
23366 *len = block->size;
23367 }
23368 break;
23369
23370 /* The DW_AT_const_value attributes are supposed to carry the
23371 symbol's value "represented as it would be on the target
23372 architecture." By the time we get here, it's already been
23373 converted to host endianness, so we just need to sign- or
23374 zero-extend it as appropriate. */
23375 case DW_FORM_data1:
23376 type = die_type (die, cu);
23377 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23378 if (result == NULL)
23379 result = write_constant_as_bytes (obstack, byte_order,
23380 type, value, len);
23381 break;
23382 case DW_FORM_data2:
23383 type = die_type (die, cu);
23384 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23385 if (result == NULL)
23386 result = write_constant_as_bytes (obstack, byte_order,
23387 type, value, len);
23388 break;
23389 case DW_FORM_data4:
23390 type = die_type (die, cu);
23391 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23392 if (result == NULL)
23393 result = write_constant_as_bytes (obstack, byte_order,
23394 type, value, len);
23395 break;
23396 case DW_FORM_data8:
23397 type = die_type (die, cu);
23398 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23399 if (result == NULL)
23400 result = write_constant_as_bytes (obstack, byte_order,
23401 type, value, len);
23402 break;
23403
23404 case DW_FORM_sdata:
23405 case DW_FORM_implicit_const:
23406 type = die_type (die, cu);
23407 result = write_constant_as_bytes (obstack, byte_order,
23408 type, attr->as_signed (), len);
23409 break;
23410
23411 case DW_FORM_udata:
23412 type = die_type (die, cu);
23413 result = write_constant_as_bytes (obstack, byte_order,
23414 type, attr->as_unsigned (), len);
23415 break;
23416
23417 default:
23418 complaint (_("unsupported const value attribute form: '%s'"),
23419 dwarf_form_name (attr->form));
23420 break;
23421 }
23422
23423 return result;
23424 }
23425
23426 /* See read.h. */
23427
23428 struct type *
23429 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23430 dwarf2_per_cu_data *per_cu,
23431 dwarf2_per_objfile *per_objfile,
23432 const char **var_name)
23433 {
23434 struct die_info *die;
23435
23436 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23437 if (cu == nullptr)
23438 cu = load_cu (per_cu, per_objfile, false);
23439
23440 if (cu == nullptr)
23441 return nullptr;
23442
23443 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23444 if (!die)
23445 return NULL;
23446
23447 if (var_name != nullptr)
23448 *var_name = var_decl_name (die, cu);
23449 return die_type (die, cu);
23450 }
23451
23452 /* See read.h. */
23453
23454 struct type *
23455 dwarf2_get_die_type (cu_offset die_offset,
23456 dwarf2_per_cu_data *per_cu,
23457 dwarf2_per_objfile *per_objfile)
23458 {
23459 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23460 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23461 }
23462
23463 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23464 On entry *REF_CU is the CU of SRC_DIE.
23465 On exit *REF_CU is the CU of the result.
23466 Returns NULL if the referenced DIE isn't found. */
23467
23468 static struct die_info *
23469 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23470 struct dwarf2_cu **ref_cu)
23471 {
23472 struct die_info temp_die;
23473 struct dwarf2_cu *sig_cu;
23474 struct die_info *die;
23475 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23476
23477
23478 /* While it might be nice to assert sig_type->type == NULL here,
23479 we can get here for DW_AT_imported_declaration where we need
23480 the DIE not the type. */
23481
23482 /* If necessary, add it to the queue and load its DIEs.
23483
23484 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23485 it doesn't mean they are currently loaded. Since we require them
23486 to be loaded, we must check for ourselves. */
23487 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23488 language_minimal)
23489 || per_objfile->get_cu (sig_type) == nullptr)
23490 read_signatured_type (sig_type, per_objfile);
23491
23492 sig_cu = per_objfile->get_cu (sig_type);
23493 gdb_assert (sig_cu != NULL);
23494 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23495 temp_die.sect_off = sig_type->type_offset_in_section;
23496 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23497 to_underlying (temp_die.sect_off));
23498 if (die)
23499 {
23500 /* For .gdb_index version 7 keep track of included TUs.
23501 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23502 if (per_objfile->per_bfd->index_table != NULL
23503 && per_objfile->per_bfd->index_table->version <= 7)
23504 {
23505 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23506 }
23507
23508 *ref_cu = sig_cu;
23509 return die;
23510 }
23511
23512 return NULL;
23513 }
23514
23515 /* Follow signatured type referenced by ATTR in SRC_DIE.
23516 On entry *REF_CU is the CU of SRC_DIE.
23517 On exit *REF_CU is the CU of the result.
23518 The result is the DIE of the type.
23519 If the referenced type cannot be found an error is thrown. */
23520
23521 static struct die_info *
23522 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23523 struct dwarf2_cu **ref_cu)
23524 {
23525 ULONGEST signature = attr->as_signature ();
23526 struct signatured_type *sig_type;
23527 struct die_info *die;
23528
23529 gdb_assert (attr->form == DW_FORM_ref_sig8);
23530
23531 sig_type = lookup_signatured_type (*ref_cu, signature);
23532 /* sig_type will be NULL if the signatured type is missing from
23533 the debug info. */
23534 if (sig_type == NULL)
23535 {
23536 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23537 " from DIE at %s [in module %s]"),
23538 hex_string (signature), sect_offset_str (src_die->sect_off),
23539 objfile_name ((*ref_cu)->per_objfile->objfile));
23540 }
23541
23542 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23543 if (die == NULL)
23544 {
23545 dump_die_for_error (src_die);
23546 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23547 " from DIE at %s [in module %s]"),
23548 hex_string (signature), sect_offset_str (src_die->sect_off),
23549 objfile_name ((*ref_cu)->per_objfile->objfile));
23550 }
23551
23552 return die;
23553 }
23554
23555 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23556 reading in and processing the type unit if necessary. */
23557
23558 static struct type *
23559 get_signatured_type (struct die_info *die, ULONGEST signature,
23560 struct dwarf2_cu *cu)
23561 {
23562 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23563 struct signatured_type *sig_type;
23564 struct dwarf2_cu *type_cu;
23565 struct die_info *type_die;
23566 struct type *type;
23567
23568 sig_type = lookup_signatured_type (cu, signature);
23569 /* sig_type will be NULL if the signatured type is missing from
23570 the debug info. */
23571 if (sig_type == NULL)
23572 {
23573 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23574 " from DIE at %s [in module %s]"),
23575 hex_string (signature), sect_offset_str (die->sect_off),
23576 objfile_name (per_objfile->objfile));
23577 return build_error_marker_type (cu, die);
23578 }
23579
23580 /* If we already know the type we're done. */
23581 type = per_objfile->get_type_for_signatured_type (sig_type);
23582 if (type != nullptr)
23583 return type;
23584
23585 type_cu = cu;
23586 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23587 if (type_die != NULL)
23588 {
23589 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23590 is created. This is important, for example, because for c++ classes
23591 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23592 type = read_type_die (type_die, type_cu);
23593 if (type == NULL)
23594 {
23595 complaint (_("Dwarf Error: Cannot build signatured type %s"
23596 " referenced from DIE at %s [in module %s]"),
23597 hex_string (signature), sect_offset_str (die->sect_off),
23598 objfile_name (per_objfile->objfile));
23599 type = build_error_marker_type (cu, die);
23600 }
23601 }
23602 else
23603 {
23604 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23605 " from DIE at %s [in module %s]"),
23606 hex_string (signature), sect_offset_str (die->sect_off),
23607 objfile_name (per_objfile->objfile));
23608 type = build_error_marker_type (cu, die);
23609 }
23610
23611 per_objfile->set_type_for_signatured_type (sig_type, type);
23612
23613 return type;
23614 }
23615
23616 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23617 reading in and processing the type unit if necessary. */
23618
23619 static struct type *
23620 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23621 struct dwarf2_cu *cu) /* ARI: editCase function */
23622 {
23623 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23624 if (attr->form_is_ref ())
23625 {
23626 struct dwarf2_cu *type_cu = cu;
23627 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23628
23629 return read_type_die (type_die, type_cu);
23630 }
23631 else if (attr->form == DW_FORM_ref_sig8)
23632 {
23633 return get_signatured_type (die, attr->as_signature (), cu);
23634 }
23635 else
23636 {
23637 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23638
23639 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23640 " at %s [in module %s]"),
23641 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23642 objfile_name (per_objfile->objfile));
23643 return build_error_marker_type (cu, die);
23644 }
23645 }
23646
23647 /* Load the DIEs associated with type unit PER_CU into memory. */
23648
23649 static void
23650 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23651 dwarf2_per_objfile *per_objfile)
23652 {
23653 struct signatured_type *sig_type;
23654
23655 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23656 gdb_assert (! per_cu->type_unit_group_p ());
23657
23658 /* We have the per_cu, but we need the signatured_type.
23659 Fortunately this is an easy translation. */
23660 gdb_assert (per_cu->is_debug_types);
23661 sig_type = (struct signatured_type *) per_cu;
23662
23663 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23664
23665 read_signatured_type (sig_type, per_objfile);
23666
23667 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23668 }
23669
23670 /* Read in a signatured type and build its CU and DIEs.
23671 If the type is a stub for the real type in a DWO file,
23672 read in the real type from the DWO file as well. */
23673
23674 static void
23675 read_signatured_type (signatured_type *sig_type,
23676 dwarf2_per_objfile *per_objfile)
23677 {
23678 gdb_assert (sig_type->is_debug_types);
23679 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23680
23681 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23682
23683 if (!reader.dummy_p)
23684 {
23685 struct dwarf2_cu *cu = reader.cu;
23686 const gdb_byte *info_ptr = reader.info_ptr;
23687
23688 gdb_assert (cu->die_hash == NULL);
23689 cu->die_hash =
23690 htab_create_alloc_ex (cu->header.length / 12,
23691 die_hash,
23692 die_eq,
23693 NULL,
23694 &cu->comp_unit_obstack,
23695 hashtab_obstack_allocate,
23696 dummy_obstack_deallocate);
23697
23698 if (reader.comp_unit_die->has_children)
23699 reader.comp_unit_die->child
23700 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23701 reader.comp_unit_die);
23702 cu->dies = reader.comp_unit_die;
23703 /* comp_unit_die is not stored in die_hash, no need. */
23704
23705 /* We try not to read any attributes in this function, because
23706 not all CUs needed for references have been loaded yet, and
23707 symbol table processing isn't initialized. But we have to
23708 set the CU language, or we won't be able to build types
23709 correctly. Similarly, if we do not read the producer, we can
23710 not apply producer-specific interpretation. */
23711 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23712
23713 reader.keep ();
23714 }
23715
23716 sig_type->tu_read = 1;
23717 }
23718
23719 /* Decode simple location descriptions.
23720 Given a pointer to a dwarf block that defines a location, compute
23721 the location and return the value. If COMPUTED is non-null, it is
23722 set to true to indicate that decoding was successful, and false
23723 otherwise. If COMPUTED is null, then this function may emit a
23724 complaint. */
23725
23726 static CORE_ADDR
23727 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23728 {
23729 struct objfile *objfile = cu->per_objfile->objfile;
23730 size_t i;
23731 size_t size = blk->size;
23732 const gdb_byte *data = blk->data;
23733 CORE_ADDR stack[64];
23734 int stacki;
23735 unsigned int bytes_read, unsnd;
23736 gdb_byte op;
23737
23738 if (computed != nullptr)
23739 *computed = false;
23740
23741 i = 0;
23742 stacki = 0;
23743 stack[stacki] = 0;
23744 stack[++stacki] = 0;
23745
23746 while (i < size)
23747 {
23748 op = data[i++];
23749 switch (op)
23750 {
23751 case DW_OP_lit0:
23752 case DW_OP_lit1:
23753 case DW_OP_lit2:
23754 case DW_OP_lit3:
23755 case DW_OP_lit4:
23756 case DW_OP_lit5:
23757 case DW_OP_lit6:
23758 case DW_OP_lit7:
23759 case DW_OP_lit8:
23760 case DW_OP_lit9:
23761 case DW_OP_lit10:
23762 case DW_OP_lit11:
23763 case DW_OP_lit12:
23764 case DW_OP_lit13:
23765 case DW_OP_lit14:
23766 case DW_OP_lit15:
23767 case DW_OP_lit16:
23768 case DW_OP_lit17:
23769 case DW_OP_lit18:
23770 case DW_OP_lit19:
23771 case DW_OP_lit20:
23772 case DW_OP_lit21:
23773 case DW_OP_lit22:
23774 case DW_OP_lit23:
23775 case DW_OP_lit24:
23776 case DW_OP_lit25:
23777 case DW_OP_lit26:
23778 case DW_OP_lit27:
23779 case DW_OP_lit28:
23780 case DW_OP_lit29:
23781 case DW_OP_lit30:
23782 case DW_OP_lit31:
23783 stack[++stacki] = op - DW_OP_lit0;
23784 break;
23785
23786 case DW_OP_reg0:
23787 case DW_OP_reg1:
23788 case DW_OP_reg2:
23789 case DW_OP_reg3:
23790 case DW_OP_reg4:
23791 case DW_OP_reg5:
23792 case DW_OP_reg6:
23793 case DW_OP_reg7:
23794 case DW_OP_reg8:
23795 case DW_OP_reg9:
23796 case DW_OP_reg10:
23797 case DW_OP_reg11:
23798 case DW_OP_reg12:
23799 case DW_OP_reg13:
23800 case DW_OP_reg14:
23801 case DW_OP_reg15:
23802 case DW_OP_reg16:
23803 case DW_OP_reg17:
23804 case DW_OP_reg18:
23805 case DW_OP_reg19:
23806 case DW_OP_reg20:
23807 case DW_OP_reg21:
23808 case DW_OP_reg22:
23809 case DW_OP_reg23:
23810 case DW_OP_reg24:
23811 case DW_OP_reg25:
23812 case DW_OP_reg26:
23813 case DW_OP_reg27:
23814 case DW_OP_reg28:
23815 case DW_OP_reg29:
23816 case DW_OP_reg30:
23817 case DW_OP_reg31:
23818 stack[++stacki] = op - DW_OP_reg0;
23819 if (i < size)
23820 {
23821 if (computed == nullptr)
23822 dwarf2_complex_location_expr_complaint ();
23823 else
23824 return 0;
23825 }
23826 break;
23827
23828 case DW_OP_regx:
23829 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23830 i += bytes_read;
23831 stack[++stacki] = unsnd;
23832 if (i < size)
23833 {
23834 if (computed == nullptr)
23835 dwarf2_complex_location_expr_complaint ();
23836 else
23837 return 0;
23838 }
23839 break;
23840
23841 case DW_OP_addr:
23842 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23843 &bytes_read);
23844 i += bytes_read;
23845 break;
23846
23847 case DW_OP_const1u:
23848 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23849 i += 1;
23850 break;
23851
23852 case DW_OP_const1s:
23853 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23854 i += 1;
23855 break;
23856
23857 case DW_OP_const2u:
23858 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23859 i += 2;
23860 break;
23861
23862 case DW_OP_const2s:
23863 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23864 i += 2;
23865 break;
23866
23867 case DW_OP_const4u:
23868 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23869 i += 4;
23870 break;
23871
23872 case DW_OP_const4s:
23873 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23874 i += 4;
23875 break;
23876
23877 case DW_OP_const8u:
23878 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23879 i += 8;
23880 break;
23881
23882 case DW_OP_constu:
23883 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23884 &bytes_read);
23885 i += bytes_read;
23886 break;
23887
23888 case DW_OP_consts:
23889 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23890 i += bytes_read;
23891 break;
23892
23893 case DW_OP_dup:
23894 stack[stacki + 1] = stack[stacki];
23895 stacki++;
23896 break;
23897
23898 case DW_OP_plus:
23899 stack[stacki - 1] += stack[stacki];
23900 stacki--;
23901 break;
23902
23903 case DW_OP_plus_uconst:
23904 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23905 &bytes_read);
23906 i += bytes_read;
23907 break;
23908
23909 case DW_OP_minus:
23910 stack[stacki - 1] -= stack[stacki];
23911 stacki--;
23912 break;
23913
23914 case DW_OP_deref:
23915 /* If we're not the last op, then we definitely can't encode
23916 this using GDB's address_class enum. This is valid for partial
23917 global symbols, although the variable's address will be bogus
23918 in the psymtab. */
23919 if (i < size)
23920 {
23921 if (computed == nullptr)
23922 dwarf2_complex_location_expr_complaint ();
23923 else
23924 return 0;
23925 }
23926 break;
23927
23928 case DW_OP_GNU_push_tls_address:
23929 case DW_OP_form_tls_address:
23930 /* The top of the stack has the offset from the beginning
23931 of the thread control block at which the variable is located. */
23932 /* Nothing should follow this operator, so the top of stack would
23933 be returned. */
23934 /* This is valid for partial global symbols, but the variable's
23935 address will be bogus in the psymtab. Make it always at least
23936 non-zero to not look as a variable garbage collected by linker
23937 which have DW_OP_addr 0. */
23938 if (i < size)
23939 {
23940 if (computed == nullptr)
23941 dwarf2_complex_location_expr_complaint ();
23942 else
23943 return 0;
23944 }
23945 stack[stacki]++;
23946 break;
23947
23948 case DW_OP_GNU_uninit:
23949 if (computed != nullptr)
23950 return 0;
23951 break;
23952
23953 case DW_OP_addrx:
23954 case DW_OP_GNU_addr_index:
23955 case DW_OP_GNU_const_index:
23956 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23957 &bytes_read);
23958 i += bytes_read;
23959 break;
23960
23961 default:
23962 if (computed == nullptr)
23963 {
23964 const char *name = get_DW_OP_name (op);
23965
23966 if (name)
23967 complaint (_("unsupported stack op: '%s'"),
23968 name);
23969 else
23970 complaint (_("unsupported stack op: '%02x'"),
23971 op);
23972 }
23973
23974 return (stack[stacki]);
23975 }
23976
23977 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23978 outside of the allocated space. Also enforce minimum>0. */
23979 if (stacki >= ARRAY_SIZE (stack) - 1)
23980 {
23981 if (computed == nullptr)
23982 complaint (_("location description stack overflow"));
23983 return 0;
23984 }
23985
23986 if (stacki <= 0)
23987 {
23988 if (computed == nullptr)
23989 complaint (_("location description stack underflow"));
23990 return 0;
23991 }
23992 }
23993
23994 if (computed != nullptr)
23995 *computed = true;
23996 return (stack[stacki]);
23997 }
23998
23999 /* memory allocation interface */
24000
24001 static struct dwarf_block *
24002 dwarf_alloc_block (struct dwarf2_cu *cu)
24003 {
24004 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24005 }
24006
24007 static struct die_info *
24008 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24009 {
24010 struct die_info *die;
24011 size_t size = sizeof (struct die_info);
24012
24013 if (num_attrs > 1)
24014 size += (num_attrs - 1) * sizeof (struct attribute);
24015
24016 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24017 memset (die, 0, sizeof (struct die_info));
24018 return (die);
24019 }
24020
24021 \f
24022
24023 /* Macro support. */
24024
24025 /* An overload of dwarf_decode_macros that finds the correct section
24026 and ensures it is read in before calling the other overload. */
24027
24028 static void
24029 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24030 int section_is_gnu)
24031 {
24032 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24033 struct objfile *objfile = per_objfile->objfile;
24034 const struct line_header *lh = cu->line_header;
24035 unsigned int offset_size = cu->header.offset_size;
24036 struct dwarf2_section_info *section;
24037 const char *section_name;
24038
24039 if (cu->dwo_unit != nullptr)
24040 {
24041 if (section_is_gnu)
24042 {
24043 section = &cu->dwo_unit->dwo_file->sections.macro;
24044 section_name = ".debug_macro.dwo";
24045 }
24046 else
24047 {
24048 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24049 section_name = ".debug_macinfo.dwo";
24050 }
24051 }
24052 else
24053 {
24054 if (section_is_gnu)
24055 {
24056 section = &per_objfile->per_bfd->macro;
24057 section_name = ".debug_macro";
24058 }
24059 else
24060 {
24061 section = &per_objfile->per_bfd->macinfo;
24062 section_name = ".debug_macinfo";
24063 }
24064 }
24065
24066 section->read (objfile);
24067 if (section->buffer == nullptr)
24068 {
24069 complaint (_("missing %s section"), section_name);
24070 return;
24071 }
24072
24073 buildsym_compunit *builder = cu->get_builder ();
24074
24075 struct dwarf2_section_info *str_offsets_section;
24076 struct dwarf2_section_info *str_section;
24077 ULONGEST str_offsets_base;
24078
24079 if (cu->dwo_unit != nullptr)
24080 {
24081 str_offsets_section = &cu->dwo_unit->dwo_file
24082 ->sections.str_offsets;
24083 str_section = &cu->dwo_unit->dwo_file->sections.str;
24084 str_offsets_base = cu->header.addr_size;
24085 }
24086 else
24087 {
24088 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24089 str_section = &per_objfile->per_bfd->str;
24090 str_offsets_base = *cu->str_offsets_base;
24091 }
24092
24093 dwarf_decode_macros (per_objfile, builder, section, lh,
24094 offset_size, offset, str_section, str_offsets_section,
24095 str_offsets_base, section_is_gnu);
24096 }
24097
24098 /* Return the .debug_loc section to use for CU.
24099 For DWO files use .debug_loc.dwo. */
24100
24101 static struct dwarf2_section_info *
24102 cu_debug_loc_section (struct dwarf2_cu *cu)
24103 {
24104 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24105
24106 if (cu->dwo_unit)
24107 {
24108 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24109
24110 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24111 }
24112 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24113 : &per_objfile->per_bfd->loc);
24114 }
24115
24116 /* Return the .debug_rnglists section to use for CU. */
24117 static struct dwarf2_section_info *
24118 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24119 {
24120 if (cu->header.version < 5)
24121 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24122 cu->header.version);
24123 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24124
24125 /* Make sure we read the .debug_rnglists section from the file that
24126 contains the DW_AT_ranges attribute we are reading. Normally that
24127 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24128 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24129 program. */
24130 if (cu->dwo_unit != nullptr
24131 && tag != DW_TAG_compile_unit
24132 && tag != DW_TAG_skeleton_unit)
24133 {
24134 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24135
24136 if (sections->rnglists.size > 0)
24137 return &sections->rnglists;
24138 else
24139 error (_(".debug_rnglists section is missing from .dwo file."));
24140 }
24141 return &dwarf2_per_objfile->per_bfd->rnglists;
24142 }
24143
24144 /* A helper function that fills in a dwarf2_loclist_baton. */
24145
24146 static void
24147 fill_in_loclist_baton (struct dwarf2_cu *cu,
24148 struct dwarf2_loclist_baton *baton,
24149 const struct attribute *attr)
24150 {
24151 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24152 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24153
24154 section->read (per_objfile->objfile);
24155
24156 baton->per_objfile = per_objfile;
24157 baton->per_cu = cu->per_cu;
24158 gdb_assert (baton->per_cu);
24159 /* We don't know how long the location list is, but make sure we
24160 don't run off the edge of the section. */
24161 baton->size = section->size - attr->as_unsigned ();
24162 baton->data = section->buffer + attr->as_unsigned ();
24163 if (cu->base_address.has_value ())
24164 baton->base_address = *cu->base_address;
24165 else
24166 baton->base_address = 0;
24167 baton->from_dwo = cu->dwo_unit != NULL;
24168 }
24169
24170 static void
24171 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24172 struct dwarf2_cu *cu, int is_block)
24173 {
24174 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24175 struct objfile *objfile = per_objfile->objfile;
24176 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24177
24178 if (attr->form_is_section_offset ()
24179 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24180 the section. If so, fall through to the complaint in the
24181 other branch. */
24182 && attr->as_unsigned () < section->get_size (objfile))
24183 {
24184 struct dwarf2_loclist_baton *baton;
24185
24186 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24187
24188 fill_in_loclist_baton (cu, baton, attr);
24189
24190 if (!cu->base_address.has_value ())
24191 complaint (_("Location list used without "
24192 "specifying the CU base address."));
24193
24194 SYMBOL_ACLASS_INDEX (sym) = (is_block
24195 ? dwarf2_loclist_block_index
24196 : dwarf2_loclist_index);
24197 SYMBOL_LOCATION_BATON (sym) = baton;
24198 }
24199 else
24200 {
24201 struct dwarf2_locexpr_baton *baton;
24202
24203 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24204 baton->per_objfile = per_objfile;
24205 baton->per_cu = cu->per_cu;
24206 gdb_assert (baton->per_cu);
24207
24208 if (attr->form_is_block ())
24209 {
24210 /* Note that we're just copying the block's data pointer
24211 here, not the actual data. We're still pointing into the
24212 info_buffer for SYM's objfile; right now we never release
24213 that buffer, but when we do clean up properly this may
24214 need to change. */
24215 struct dwarf_block *block = attr->as_block ();
24216 baton->size = block->size;
24217 baton->data = block->data;
24218 }
24219 else
24220 {
24221 dwarf2_invalid_attrib_class_complaint ("location description",
24222 sym->natural_name ());
24223 baton->size = 0;
24224 }
24225
24226 SYMBOL_ACLASS_INDEX (sym) = (is_block
24227 ? dwarf2_locexpr_block_index
24228 : dwarf2_locexpr_index);
24229 SYMBOL_LOCATION_BATON (sym) = baton;
24230 }
24231 }
24232
24233 /* See read.h. */
24234
24235 const comp_unit_head *
24236 dwarf2_per_cu_data::get_header () const
24237 {
24238 if (!m_header_read_in)
24239 {
24240 const gdb_byte *info_ptr
24241 = this->section->buffer + to_underlying (this->sect_off);
24242
24243 memset (&m_header, 0, sizeof (m_header));
24244
24245 read_comp_unit_head (&m_header, info_ptr, this->section,
24246 rcuh_kind::COMPILE);
24247
24248 m_header_read_in = true;
24249 }
24250
24251 return &m_header;
24252 }
24253
24254 /* See read.h. */
24255
24256 int
24257 dwarf2_per_cu_data::addr_size () const
24258 {
24259 return this->get_header ()->addr_size;
24260 }
24261
24262 /* See read.h. */
24263
24264 int
24265 dwarf2_per_cu_data::offset_size () const
24266 {
24267 return this->get_header ()->offset_size;
24268 }
24269
24270 /* See read.h. */
24271
24272 int
24273 dwarf2_per_cu_data::ref_addr_size () const
24274 {
24275 const comp_unit_head *header = this->get_header ();
24276
24277 if (header->version == 2)
24278 return header->addr_size;
24279 else
24280 return header->offset_size;
24281 }
24282
24283 /* A helper function for dwarf2_find_containing_comp_unit that returns
24284 the index of the result, and that searches a vector. It will
24285 return a result even if the offset in question does not actually
24286 occur in any CU. This is separate so that it can be unit
24287 tested. */
24288
24289 static int
24290 dwarf2_find_containing_comp_unit
24291 (sect_offset sect_off,
24292 unsigned int offset_in_dwz,
24293 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
24294 {
24295 int low, high;
24296
24297 low = 0;
24298 high = all_comp_units.size () - 1;
24299 while (high > low)
24300 {
24301 struct dwarf2_per_cu_data *mid_cu;
24302 int mid = low + (high - low) / 2;
24303
24304 mid_cu = all_comp_units[mid].get ();
24305 if (mid_cu->is_dwz > offset_in_dwz
24306 || (mid_cu->is_dwz == offset_in_dwz
24307 && mid_cu->sect_off + mid_cu->length > sect_off))
24308 high = mid;
24309 else
24310 low = mid + 1;
24311 }
24312 gdb_assert (low == high);
24313 return low;
24314 }
24315
24316 /* Locate the .debug_info compilation unit from CU's objfile which contains
24317 the DIE at OFFSET. Raises an error on failure. */
24318
24319 static struct dwarf2_per_cu_data *
24320 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24321 unsigned int offset_in_dwz,
24322 dwarf2_per_objfile *per_objfile)
24323 {
24324 int low = dwarf2_find_containing_comp_unit
24325 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24326 dwarf2_per_cu_data *this_cu
24327 = per_objfile->per_bfd->all_comp_units[low].get ();
24328
24329 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24330 {
24331 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24332 error (_("Dwarf Error: could not find partial DIE containing "
24333 "offset %s [in module %s]"),
24334 sect_offset_str (sect_off),
24335 bfd_get_filename (per_objfile->objfile->obfd));
24336
24337 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24338 <= sect_off);
24339 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
24340 }
24341 else
24342 {
24343 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24344 && sect_off >= this_cu->sect_off + this_cu->length)
24345 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24346 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24347 return this_cu;
24348 }
24349 }
24350
24351 #if GDB_SELF_TEST
24352
24353 namespace selftests {
24354 namespace find_containing_comp_unit {
24355
24356 static void
24357 run_test ()
24358 {
24359 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
24360 dwarf2_per_cu_data *one_ptr = one.get ();
24361 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
24362 dwarf2_per_cu_data *two_ptr = two.get ();
24363 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
24364 dwarf2_per_cu_data *three_ptr = three.get ();
24365 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
24366 dwarf2_per_cu_data *four_ptr = four.get ();
24367
24368 one->length = 5;
24369 two->sect_off = sect_offset (one->length);
24370 two->length = 7;
24371
24372 three->length = 5;
24373 three->is_dwz = 1;
24374 four->sect_off = sect_offset (three->length);
24375 four->length = 7;
24376 four->is_dwz = 1;
24377
24378 std::vector<dwarf2_per_cu_data_up> units;
24379 units.push_back (std::move (one));
24380 units.push_back (std::move (two));
24381 units.push_back (std::move (three));
24382 units.push_back (std::move (four));
24383
24384 int result;
24385
24386 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24387 SELF_CHECK (units[result].get () == one_ptr);
24388 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24389 SELF_CHECK (units[result].get () == one_ptr);
24390 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24391 SELF_CHECK (units[result].get () == two_ptr);
24392
24393 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24394 SELF_CHECK (units[result].get () == three_ptr);
24395 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24396 SELF_CHECK (units[result].get () == three_ptr);
24397 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24398 SELF_CHECK (units[result].get () == four_ptr);
24399 }
24400
24401 }
24402 }
24403
24404 #endif /* GDB_SELF_TEST */
24405
24406 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24407
24408 static void
24409 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24410 enum language pretend_language)
24411 {
24412 struct attribute *attr;
24413
24414 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24415
24416 /* Set the language we're debugging. */
24417 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24418 if (cu->producer != nullptr
24419 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
24420 {
24421 /* The XLCL doesn't generate DW_LANG_OpenCL because this
24422 attribute is not standardised yet. As a workaround for the
24423 language detection we fall back to the DW_AT_producer
24424 string. */
24425 cu->per_cu->lang = language_opencl;
24426 }
24427 else if (cu->producer != nullptr
24428 && strstr (cu->producer, "GNU Go ") != NULL)
24429 {
24430 /* Similar hack for Go. */
24431 cu->per_cu->lang = language_go;
24432 }
24433 else if (attr != nullptr)
24434 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
24435 else
24436 cu->per_cu->lang = pretend_language;
24437 cu->language_defn = language_def (cu->per_cu->lang);
24438 }
24439
24440 /* See read.h. */
24441
24442 dwarf2_cu *
24443 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24444 {
24445 auto it = m_dwarf2_cus.find (per_cu);
24446 if (it == m_dwarf2_cus.end ())
24447 return nullptr;
24448
24449 return it->second;
24450 }
24451
24452 /* See read.h. */
24453
24454 void
24455 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24456 {
24457 gdb_assert (this->get_cu (per_cu) == nullptr);
24458
24459 m_dwarf2_cus[per_cu] = cu;
24460 }
24461
24462 /* See read.h. */
24463
24464 void
24465 dwarf2_per_objfile::age_comp_units ()
24466 {
24467 dwarf_read_debug_printf_v ("running");
24468
24469 /* This is not expected to be called in the middle of CU expansion. There is
24470 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24471 loaded in memory. Calling age_comp_units while the queue is in use could
24472 make us free the DIEs for a CU that is in the queue and therefore break
24473 that invariant. */
24474 gdb_assert (!this->per_bfd->queue.has_value ());
24475
24476 /* Start by clearing all marks. */
24477 for (auto pair : m_dwarf2_cus)
24478 pair.second->clear_mark ();
24479
24480 /* Traverse all CUs, mark them and their dependencies if used recently
24481 enough. */
24482 for (auto pair : m_dwarf2_cus)
24483 {
24484 dwarf2_cu *cu = pair.second;
24485
24486 cu->last_used++;
24487 if (cu->last_used <= dwarf_max_cache_age)
24488 cu->mark ();
24489 }
24490
24491 /* Delete all CUs still not marked. */
24492 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24493 {
24494 dwarf2_cu *cu = it->second;
24495
24496 if (!cu->is_marked ())
24497 {
24498 dwarf_read_debug_printf_v ("deleting old CU %s",
24499 sect_offset_str (cu->per_cu->sect_off));
24500 delete cu;
24501 it = m_dwarf2_cus.erase (it);
24502 }
24503 else
24504 it++;
24505 }
24506 }
24507
24508 /* See read.h. */
24509
24510 void
24511 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24512 {
24513 auto it = m_dwarf2_cus.find (per_cu);
24514 if (it == m_dwarf2_cus.end ())
24515 return;
24516
24517 delete it->second;
24518
24519 m_dwarf2_cus.erase (it);
24520 }
24521
24522 dwarf2_per_objfile::~dwarf2_per_objfile ()
24523 {
24524 remove_all_cus ();
24525 }
24526
24527 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24528 We store these in a hash table separate from the DIEs, and preserve them
24529 when the DIEs are flushed out of cache.
24530
24531 The CU "per_cu" pointer is needed because offset alone is not enough to
24532 uniquely identify the type. A file may have multiple .debug_types sections,
24533 or the type may come from a DWO file. Furthermore, while it's more logical
24534 to use per_cu->section+offset, with Fission the section with the data is in
24535 the DWO file but we don't know that section at the point we need it.
24536 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24537 because we can enter the lookup routine, get_die_type_at_offset, from
24538 outside this file, and thus won't necessarily have PER_CU->cu.
24539 Fortunately, PER_CU is stable for the life of the objfile. */
24540
24541 struct dwarf2_per_cu_offset_and_type
24542 {
24543 const struct dwarf2_per_cu_data *per_cu;
24544 sect_offset sect_off;
24545 struct type *type;
24546 };
24547
24548 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24549
24550 static hashval_t
24551 per_cu_offset_and_type_hash (const void *item)
24552 {
24553 const struct dwarf2_per_cu_offset_and_type *ofs
24554 = (const struct dwarf2_per_cu_offset_and_type *) item;
24555
24556 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24557 }
24558
24559 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24560
24561 static int
24562 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24563 {
24564 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24565 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24566 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24567 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24568
24569 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24570 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24571 }
24572
24573 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24574 table if necessary. For convenience, return TYPE.
24575
24576 The DIEs reading must have careful ordering to:
24577 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24578 reading current DIE.
24579 * Not trying to dereference contents of still incompletely read in types
24580 while reading in other DIEs.
24581 * Enable referencing still incompletely read in types just by a pointer to
24582 the type without accessing its fields.
24583
24584 Therefore caller should follow these rules:
24585 * Try to fetch any prerequisite types we may need to build this DIE type
24586 before building the type and calling set_die_type.
24587 * After building type call set_die_type for current DIE as soon as
24588 possible before fetching more types to complete the current type.
24589 * Make the type as complete as possible before fetching more types. */
24590
24591 static struct type *
24592 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24593 bool skip_data_location)
24594 {
24595 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24596 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24597 struct objfile *objfile = per_objfile->objfile;
24598 struct attribute *attr;
24599 struct dynamic_prop prop;
24600
24601 /* For Ada types, make sure that the gnat-specific data is always
24602 initialized (if not already set). There are a few types where
24603 we should not be doing so, because the type-specific area is
24604 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24605 where the type-specific area is used to store the floatformat).
24606 But this is not a problem, because the gnat-specific information
24607 is actually not needed for these types. */
24608 if (need_gnat_info (cu)
24609 && type->code () != TYPE_CODE_FUNC
24610 && type->code () != TYPE_CODE_FLT
24611 && type->code () != TYPE_CODE_METHODPTR
24612 && type->code () != TYPE_CODE_MEMBERPTR
24613 && type->code () != TYPE_CODE_METHOD
24614 && type->code () != TYPE_CODE_FIXED_POINT
24615 && !HAVE_GNAT_AUX_INFO (type))
24616 INIT_GNAT_SPECIFIC (type);
24617
24618 /* Read DW_AT_allocated and set in type. */
24619 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24620 if (attr != NULL)
24621 {
24622 struct type *prop_type = cu->addr_sized_int_type (false);
24623 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24624 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24625 }
24626
24627 /* Read DW_AT_associated and set in type. */
24628 attr = dwarf2_attr (die, DW_AT_associated, cu);
24629 if (attr != NULL)
24630 {
24631 struct type *prop_type = cu->addr_sized_int_type (false);
24632 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24633 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24634 }
24635
24636 /* Read DW_AT_data_location and set in type. */
24637 if (!skip_data_location)
24638 {
24639 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24640 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24641 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24642 }
24643
24644 if (per_objfile->die_type_hash == NULL)
24645 per_objfile->die_type_hash
24646 = htab_up (htab_create_alloc (127,
24647 per_cu_offset_and_type_hash,
24648 per_cu_offset_and_type_eq,
24649 NULL, xcalloc, xfree));
24650
24651 ofs.per_cu = cu->per_cu;
24652 ofs.sect_off = die->sect_off;
24653 ofs.type = type;
24654 slot = (struct dwarf2_per_cu_offset_and_type **)
24655 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24656 if (*slot)
24657 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24658 sect_offset_str (die->sect_off));
24659 *slot = XOBNEW (&objfile->objfile_obstack,
24660 struct dwarf2_per_cu_offset_and_type);
24661 **slot = ofs;
24662 return type;
24663 }
24664
24665 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24666 or return NULL if the die does not have a saved type. */
24667
24668 static struct type *
24669 get_die_type_at_offset (sect_offset sect_off,
24670 dwarf2_per_cu_data *per_cu,
24671 dwarf2_per_objfile *per_objfile)
24672 {
24673 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24674
24675 if (per_objfile->die_type_hash == NULL)
24676 return NULL;
24677
24678 ofs.per_cu = per_cu;
24679 ofs.sect_off = sect_off;
24680 slot = ((struct dwarf2_per_cu_offset_and_type *)
24681 htab_find (per_objfile->die_type_hash.get (), &ofs));
24682 if (slot)
24683 return slot->type;
24684 else
24685 return NULL;
24686 }
24687
24688 /* Look up the type for DIE in CU in die_type_hash,
24689 or return NULL if DIE does not have a saved type. */
24690
24691 static struct type *
24692 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24693 {
24694 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24695 }
24696
24697 /* Trivial hash function for partial_die_info: the hash value of a DIE
24698 is its offset in .debug_info for this objfile. */
24699
24700 static hashval_t
24701 partial_die_hash (const void *item)
24702 {
24703 const struct partial_die_info *part_die
24704 = (const struct partial_die_info *) item;
24705
24706 return to_underlying (part_die->sect_off);
24707 }
24708
24709 /* Trivial comparison function for partial_die_info structures: two DIEs
24710 are equal if they have the same offset. */
24711
24712 static int
24713 partial_die_eq (const void *item_lhs, const void *item_rhs)
24714 {
24715 const struct partial_die_info *part_die_lhs
24716 = (const struct partial_die_info *) item_lhs;
24717 const struct partial_die_info *part_die_rhs
24718 = (const struct partial_die_info *) item_rhs;
24719
24720 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24721 }
24722
24723 struct cmd_list_element *set_dwarf_cmdlist;
24724 struct cmd_list_element *show_dwarf_cmdlist;
24725
24726 static void
24727 show_check_physname (struct ui_file *file, int from_tty,
24728 struct cmd_list_element *c, const char *value)
24729 {
24730 fprintf_filtered (file,
24731 _("Whether to check \"physname\" is %s.\n"),
24732 value);
24733 }
24734
24735 void _initialize_dwarf2_read ();
24736 void
24737 _initialize_dwarf2_read ()
24738 {
24739 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24740 Set DWARF specific variables.\n\
24741 Configure DWARF variables such as the cache size."),
24742 &set_dwarf_cmdlist,
24743 0/*allow-unknown*/, &maintenance_set_cmdlist);
24744
24745 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24746 Show DWARF specific variables.\n\
24747 Show DWARF variables such as the cache size."),
24748 &show_dwarf_cmdlist,
24749 0/*allow-unknown*/, &maintenance_show_cmdlist);
24750
24751 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24752 &dwarf_max_cache_age, _("\
24753 Set the upper bound on the age of cached DWARF compilation units."), _("\
24754 Show the upper bound on the age of cached DWARF compilation units."), _("\
24755 A higher limit means that cached compilation units will be stored\n\
24756 in memory longer, and more total memory will be used. Zero disables\n\
24757 caching, which can slow down startup."),
24758 NULL,
24759 show_dwarf_max_cache_age,
24760 &set_dwarf_cmdlist,
24761 &show_dwarf_cmdlist);
24762
24763 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24764 Set debugging of the DWARF reader."), _("\
24765 Show debugging of the DWARF reader."), _("\
24766 When enabled (non-zero), debugging messages are printed during DWARF\n\
24767 reading and symtab expansion. A value of 1 (one) provides basic\n\
24768 information. A value greater than 1 provides more verbose information."),
24769 NULL,
24770 NULL,
24771 &setdebuglist, &showdebuglist);
24772
24773 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24774 Set debugging of the DWARF DIE reader."), _("\
24775 Show debugging of the DWARF DIE reader."), _("\
24776 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24777 The value is the maximum depth to print."),
24778 NULL,
24779 NULL,
24780 &setdebuglist, &showdebuglist);
24781
24782 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24783 Set debugging of the dwarf line reader."), _("\
24784 Show debugging of the dwarf line reader."), _("\
24785 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24786 A value of 1 (one) provides basic information.\n\
24787 A value greater than 1 provides more verbose information."),
24788 NULL,
24789 NULL,
24790 &setdebuglist, &showdebuglist);
24791
24792 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24793 Set cross-checking of \"physname\" code against demangler."), _("\
24794 Show cross-checking of \"physname\" code against demangler."), _("\
24795 When enabled, GDB's internal \"physname\" code is checked against\n\
24796 the demangler."),
24797 NULL, show_check_physname,
24798 &setdebuglist, &showdebuglist);
24799
24800 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24801 no_class, &use_deprecated_index_sections, _("\
24802 Set whether to use deprecated gdb_index sections."), _("\
24803 Show whether to use deprecated gdb_index sections."), _("\
24804 When enabled, deprecated .gdb_index sections are used anyway.\n\
24805 Normally they are ignored either because of a missing feature or\n\
24806 performance issue.\n\
24807 Warning: This option must be enabled before gdb reads the file."),
24808 NULL,
24809 NULL,
24810 &setlist, &showlist);
24811
24812 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24813 &dwarf2_locexpr_funcs);
24814 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24815 &dwarf2_loclist_funcs);
24816
24817 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24818 &dwarf2_block_frame_base_locexpr_funcs);
24819 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24820 &dwarf2_block_frame_base_loclist_funcs);
24821
24822 #if GDB_SELF_TEST
24823 selftests::register_test ("dw2_expand_symtabs_matching",
24824 selftests::dw2_expand_symtabs_matching::run_test);
24825 selftests::register_test ("dwarf2_find_containing_comp_unit",
24826 selftests::find_containing_comp_unit::run_test);
24827 #endif
24828 }