]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
[gdb/symtab] Fix infinite recursion in dwarf2_cu::get_builder(), again
[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 void set_cu_language (unsigned int, struct dwarf2_cu *);
1177
1178 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1179 struct dwarf2_cu *);
1180
1181 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1182 struct dwarf2_cu *cu);
1183
1184 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1185
1186 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1187 struct dwarf2_cu *cu);
1188
1189 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1190
1191 static struct die_info *die_specification (struct die_info *die,
1192 struct dwarf2_cu **);
1193
1194 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1195 struct dwarf2_cu *cu);
1196
1197 static void dwarf_decode_lines (struct line_header *, const char *,
1198 struct dwarf2_cu *, dwarf2_psymtab *,
1199 CORE_ADDR, int decode_mapping);
1200
1201 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1202 const char *);
1203
1204 static struct symbol *new_symbol (struct die_info *, struct type *,
1205 struct dwarf2_cu *, struct symbol * = NULL);
1206
1207 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1208 struct dwarf2_cu *);
1209
1210 static void dwarf2_const_value_attr (const struct attribute *attr,
1211 struct type *type,
1212 const char *name,
1213 struct obstack *obstack,
1214 struct dwarf2_cu *cu, LONGEST *value,
1215 const gdb_byte **bytes,
1216 struct dwarf2_locexpr_baton **baton);
1217
1218 static struct type *read_subrange_index_type (struct die_info *die,
1219 struct dwarf2_cu *cu);
1220
1221 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1222
1223 static int need_gnat_info (struct dwarf2_cu *);
1224
1225 static struct type *die_descriptive_type (struct die_info *,
1226 struct dwarf2_cu *);
1227
1228 static void set_descriptive_type (struct type *, struct die_info *,
1229 struct dwarf2_cu *);
1230
1231 static struct type *die_containing_type (struct die_info *,
1232 struct dwarf2_cu *);
1233
1234 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1235 struct dwarf2_cu *);
1236
1237 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1238
1239 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1240
1241 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1242
1243 static char *typename_concat (struct obstack *obs, const char *prefix,
1244 const char *suffix, int physname,
1245 struct dwarf2_cu *cu);
1246
1247 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1248
1249 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1250
1251 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1252
1253 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1254
1255 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1256
1257 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1258
1259 /* Return the .debug_loclists section to use for cu. */
1260 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1261
1262 /* Return the .debug_rnglists section to use for cu. */
1263 static struct dwarf2_section_info *cu_debug_rnglists_section
1264 (struct dwarf2_cu *cu, dwarf_tag tag);
1265
1266 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1267 values. Keep the items ordered with increasing constraints compliance. */
1268 enum pc_bounds_kind
1269 {
1270 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1271 PC_BOUNDS_NOT_PRESENT,
1272
1273 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1274 were present but they do not form a valid range of PC addresses. */
1275 PC_BOUNDS_INVALID,
1276
1277 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1278 PC_BOUNDS_RANGES,
1279
1280 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1281 PC_BOUNDS_HIGH_LOW,
1282 };
1283
1284 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1285 CORE_ADDR *, CORE_ADDR *,
1286 struct dwarf2_cu *,
1287 dwarf2_psymtab *);
1288
1289 static void get_scope_pc_bounds (struct die_info *,
1290 CORE_ADDR *, CORE_ADDR *,
1291 struct dwarf2_cu *);
1292
1293 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1294 CORE_ADDR, struct dwarf2_cu *);
1295
1296 static void dwarf2_add_field (struct field_info *, struct die_info *,
1297 struct dwarf2_cu *);
1298
1299 static void dwarf2_attach_fields_to_type (struct field_info *,
1300 struct type *, struct dwarf2_cu *);
1301
1302 static void dwarf2_add_member_fn (struct field_info *,
1303 struct die_info *, struct type *,
1304 struct dwarf2_cu *);
1305
1306 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1307 struct type *,
1308 struct dwarf2_cu *);
1309
1310 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1311
1312 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1313
1314 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1315
1316 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1317
1318 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1319
1320 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1321
1322 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1323
1324 static struct type *read_module_type (struct die_info *die,
1325 struct dwarf2_cu *cu);
1326
1327 static const char *namespace_name (struct die_info *die,
1328 int *is_anonymous, struct dwarf2_cu *);
1329
1330 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1331
1332 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1333 bool * = nullptr);
1334
1335 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1336 struct dwarf2_cu *);
1337
1338 static struct die_info *read_die_and_siblings_1
1339 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1340 struct die_info *);
1341
1342 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1343 const gdb_byte *info_ptr,
1344 const gdb_byte **new_info_ptr,
1345 struct die_info *parent);
1346
1347 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1348 struct die_info **, const gdb_byte *,
1349 int);
1350
1351 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1352 struct die_info **, const gdb_byte *);
1353
1354 static void process_die (struct die_info *, struct dwarf2_cu *);
1355
1356 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1357 struct objfile *);
1358
1359 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1360
1361 static const char *dwarf2_full_name (const char *name,
1362 struct die_info *die,
1363 struct dwarf2_cu *cu);
1364
1365 static const char *dwarf2_physname (const char *name, struct die_info *die,
1366 struct dwarf2_cu *cu);
1367
1368 static struct die_info *dwarf2_extension (struct die_info *die,
1369 struct dwarf2_cu **);
1370
1371 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1372
1373 static void dump_die_for_error (struct die_info *);
1374
1375 static void dump_die_1 (struct ui_file *, int level, int max_level,
1376 struct die_info *);
1377
1378 /*static*/ void dump_die (struct die_info *, int max_level);
1379
1380 static void store_in_ref_table (struct die_info *,
1381 struct dwarf2_cu *);
1382
1383 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1384 const struct attribute *,
1385 struct dwarf2_cu **);
1386
1387 static struct die_info *follow_die_ref (struct die_info *,
1388 const struct attribute *,
1389 struct dwarf2_cu **);
1390
1391 static struct die_info *follow_die_sig (struct die_info *,
1392 const struct attribute *,
1393 struct dwarf2_cu **);
1394
1395 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1396 struct dwarf2_cu *);
1397
1398 static struct type *get_DW_AT_signature_type (struct die_info *,
1399 const struct attribute *,
1400 struct dwarf2_cu *);
1401
1402 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1403 dwarf2_per_objfile *per_objfile);
1404
1405 static void read_signatured_type (signatured_type *sig_type,
1406 dwarf2_per_objfile *per_objfile);
1407
1408 static int attr_to_dynamic_prop (const struct attribute *attr,
1409 struct die_info *die, struct dwarf2_cu *cu,
1410 struct dynamic_prop *prop, struct type *type);
1411
1412 /* memory allocation interface */
1413
1414 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1415
1416 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1417
1418 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1419
1420 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1421 struct dwarf2_loclist_baton *baton,
1422 const struct attribute *attr);
1423
1424 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1425 struct symbol *sym,
1426 struct dwarf2_cu *cu,
1427 int is_block);
1428
1429 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1430 const gdb_byte *info_ptr,
1431 const struct abbrev_info *abbrev);
1432
1433 static hashval_t partial_die_hash (const void *item);
1434
1435 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1436
1437 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1438 (sect_offset sect_off, unsigned int offset_in_dwz,
1439 dwarf2_per_objfile *per_objfile);
1440
1441 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1442 struct die_info *comp_unit_die,
1443 enum language pretend_language);
1444
1445 static struct type *set_die_type (struct die_info *, struct type *,
1446 struct dwarf2_cu *, bool = false);
1447
1448 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1449
1450 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1451 dwarf2_per_objfile *per_objfile,
1452 dwarf2_cu *existing_cu,
1453 bool skip_partial,
1454 enum language pretend_language);
1455
1456 static void process_full_comp_unit (dwarf2_cu *cu,
1457 enum language pretend_language);
1458
1459 static void process_full_type_unit (dwarf2_cu *cu,
1460 enum language pretend_language);
1461
1462 static struct type *get_die_type_at_offset (sect_offset,
1463 dwarf2_per_cu_data *per_cu,
1464 dwarf2_per_objfile *per_objfile);
1465
1466 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1467
1468 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1469 dwarf2_per_objfile *per_objfile,
1470 enum language pretend_language);
1471
1472 static void process_queue (dwarf2_per_objfile *per_objfile);
1473
1474 /* Class, the destructor of which frees all allocated queue entries. This
1475 will only have work to do if an error was thrown while processing the
1476 dwarf. If no error was thrown then the queue entries should have all
1477 been processed, and freed, as we went along. */
1478
1479 class dwarf2_queue_guard
1480 {
1481 public:
1482 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1483 : m_per_objfile (per_objfile)
1484 {
1485 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1486
1487 m_per_objfile->per_bfd->queue.emplace ();
1488 }
1489
1490 /* Free any entries remaining on the queue. There should only be
1491 entries left if we hit an error while processing the dwarf. */
1492 ~dwarf2_queue_guard ()
1493 {
1494 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1495
1496 m_per_objfile->per_bfd->queue.reset ();
1497 }
1498
1499 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1500
1501 private:
1502 dwarf2_per_objfile *m_per_objfile;
1503 };
1504
1505 dwarf2_queue_item::~dwarf2_queue_item ()
1506 {
1507 /* Anything still marked queued is likely to be in an
1508 inconsistent state, so discard it. */
1509 if (per_cu->queued)
1510 {
1511 per_objfile->remove_cu (per_cu);
1512 per_cu->queued = 0;
1513 }
1514 }
1515
1516 /* See dwarf2/read.h. */
1517
1518 void
1519 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1520 {
1521 if (data->is_debug_types)
1522 delete static_cast<signatured_type *> (data);
1523 else
1524 delete data;
1525 }
1526
1527 /* The return type of find_file_and_directory. Note, the enclosed
1528 string pointers are only valid while this object is valid. */
1529
1530 struct file_and_directory
1531 {
1532 /* The filename. This is never NULL. */
1533 const char *name;
1534
1535 /* The compilation directory. NULL if not known. If we needed to
1536 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1537 points directly to the DW_AT_comp_dir string attribute owned by
1538 the obstack that owns the DIE. */
1539 const char *comp_dir;
1540
1541 /* If we needed to build a new string for comp_dir, this is what
1542 owns the storage. */
1543 std::string comp_dir_storage;
1544 };
1545
1546 static file_and_directory find_file_and_directory (struct die_info *die,
1547 struct dwarf2_cu *cu);
1548
1549 static htab_up allocate_signatured_type_table ();
1550
1551 static htab_up allocate_dwo_unit_table ();
1552
1553 static struct dwo_unit *lookup_dwo_unit_in_dwp
1554 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1555 const char *comp_dir, ULONGEST signature, int is_debug_types);
1556
1557 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1558
1559 static struct dwo_unit *lookup_dwo_comp_unit
1560 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1561 ULONGEST signature);
1562
1563 static struct dwo_unit *lookup_dwo_type_unit
1564 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1565
1566 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1567
1568 /* A unique pointer to a dwo_file. */
1569
1570 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1571
1572 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1573
1574 static void check_producer (struct dwarf2_cu *cu);
1575 \f
1576 /* Various complaints about symbol reading that don't abort the process. */
1577
1578 static void
1579 dwarf2_debug_line_missing_file_complaint (void)
1580 {
1581 complaint (_(".debug_line section has line data without a file"));
1582 }
1583
1584 static void
1585 dwarf2_debug_line_missing_end_sequence_complaint (void)
1586 {
1587 complaint (_(".debug_line section has line "
1588 "program sequence without an end"));
1589 }
1590
1591 static void
1592 dwarf2_complex_location_expr_complaint (void)
1593 {
1594 complaint (_("location expression too complex"));
1595 }
1596
1597 static void
1598 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1599 int arg3)
1600 {
1601 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1602 arg1, arg2, arg3);
1603 }
1604
1605 static void
1606 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1607 {
1608 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1609 arg1, arg2);
1610 }
1611
1612 /* Hash function for line_header_hash. */
1613
1614 static hashval_t
1615 line_header_hash (const struct line_header *ofs)
1616 {
1617 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1618 }
1619
1620 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1621
1622 static hashval_t
1623 line_header_hash_voidp (const void *item)
1624 {
1625 const struct line_header *ofs = (const struct line_header *) item;
1626
1627 return line_header_hash (ofs);
1628 }
1629
1630 /* Equality function for line_header_hash. */
1631
1632 static int
1633 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1634 {
1635 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1636 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1637
1638 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1639 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1640 }
1641
1642 \f
1643
1644 /* See declaration. */
1645
1646 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1647 bool can_copy_)
1648 : obfd (obfd),
1649 can_copy (can_copy_)
1650 {
1651 if (names == NULL)
1652 names = &dwarf2_elf_names;
1653
1654 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1655 locate_sections (obfd, sec, *names);
1656 }
1657
1658 dwarf2_per_bfd::~dwarf2_per_bfd ()
1659 {
1660 for (auto &per_cu : all_comp_units)
1661 per_cu->imported_symtabs_free ();
1662
1663 /* Everything else should be on this->obstack. */
1664 }
1665
1666 /* See read.h. */
1667
1668 void
1669 dwarf2_per_objfile::remove_all_cus ()
1670 {
1671 gdb_assert (!this->per_bfd->queue.has_value ());
1672
1673 for (auto pair : m_dwarf2_cus)
1674 delete pair.second;
1675
1676 m_dwarf2_cus.clear ();
1677 }
1678
1679 /* A helper class that calls free_cached_comp_units on
1680 destruction. */
1681
1682 class free_cached_comp_units
1683 {
1684 public:
1685
1686 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1687 : m_per_objfile (per_objfile)
1688 {
1689 }
1690
1691 ~free_cached_comp_units ()
1692 {
1693 m_per_objfile->remove_all_cus ();
1694 }
1695
1696 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1697
1698 private:
1699
1700 dwarf2_per_objfile *m_per_objfile;
1701 };
1702
1703 /* See read.h. */
1704
1705 bool
1706 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1707 {
1708 gdb_assert (per_cu->index < this->m_symtabs.size ());
1709
1710 return this->m_symtabs[per_cu->index] != nullptr;
1711 }
1712
1713 /* See read.h. */
1714
1715 compunit_symtab *
1716 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1717 {
1718 gdb_assert (per_cu->index < this->m_symtabs.size ());
1719
1720 return this->m_symtabs[per_cu->index];
1721 }
1722
1723 /* See read.h. */
1724
1725 void
1726 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1727 compunit_symtab *symtab)
1728 {
1729 gdb_assert (per_cu->index < this->m_symtabs.size ());
1730 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1731
1732 this->m_symtabs[per_cu->index] = symtab;
1733 }
1734
1735 /* Try to locate the sections we need for DWARF 2 debugging
1736 information and return true if we have enough to do something.
1737 NAMES points to the dwarf2 section names, or is NULL if the standard
1738 ELF names are used. CAN_COPY is true for formats where symbol
1739 interposition is possible and so symbol values must follow copy
1740 relocation rules. */
1741
1742 int
1743 dwarf2_has_info (struct objfile *objfile,
1744 const struct dwarf2_debug_sections *names,
1745 bool can_copy)
1746 {
1747 if (objfile->flags & OBJF_READNEVER)
1748 return 0;
1749
1750 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1751
1752 if (per_objfile == NULL)
1753 {
1754 dwarf2_per_bfd *per_bfd;
1755
1756 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1757 BFD doesn't require relocations.
1758
1759 We don't share with objfiles for which -readnow was requested,
1760 because it would complicate things when loading the same BFD with
1761 -readnow and then without -readnow. */
1762 if (!gdb_bfd_requires_relocations (objfile->obfd)
1763 && (objfile->flags & OBJF_READNOW) == 0)
1764 {
1765 /* See if one has been created for this BFD yet. */
1766 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1767
1768 if (per_bfd == nullptr)
1769 {
1770 /* No, create it now. */
1771 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1772 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1773 }
1774 }
1775 else
1776 {
1777 /* No sharing possible, create one specifically for this objfile. */
1778 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1779 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1780 }
1781
1782 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1783 }
1784
1785 return (!per_objfile->per_bfd->info.is_virtual
1786 && per_objfile->per_bfd->info.s.section != NULL
1787 && !per_objfile->per_bfd->abbrev.is_virtual
1788 && per_objfile->per_bfd->abbrev.s.section != NULL);
1789 }
1790
1791 /* See declaration. */
1792
1793 void
1794 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1795 const dwarf2_debug_sections &names)
1796 {
1797 flagword aflag = bfd_section_flags (sectp);
1798
1799 if ((aflag & SEC_HAS_CONTENTS) == 0)
1800 {
1801 }
1802 else if (elf_section_data (sectp)->this_hdr.sh_size
1803 > bfd_get_file_size (abfd))
1804 {
1805 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1806 warning (_("Discarding section %s which has a section size (%s"
1807 ") larger than the file size [in module %s]"),
1808 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1809 bfd_get_filename (abfd));
1810 }
1811 else if (names.info.matches (sectp->name))
1812 {
1813 this->info.s.section = sectp;
1814 this->info.size = bfd_section_size (sectp);
1815 }
1816 else if (names.abbrev.matches (sectp->name))
1817 {
1818 this->abbrev.s.section = sectp;
1819 this->abbrev.size = bfd_section_size (sectp);
1820 }
1821 else if (names.line.matches (sectp->name))
1822 {
1823 this->line.s.section = sectp;
1824 this->line.size = bfd_section_size (sectp);
1825 }
1826 else if (names.loc.matches (sectp->name))
1827 {
1828 this->loc.s.section = sectp;
1829 this->loc.size = bfd_section_size (sectp);
1830 }
1831 else if (names.loclists.matches (sectp->name))
1832 {
1833 this->loclists.s.section = sectp;
1834 this->loclists.size = bfd_section_size (sectp);
1835 }
1836 else if (names.macinfo.matches (sectp->name))
1837 {
1838 this->macinfo.s.section = sectp;
1839 this->macinfo.size = bfd_section_size (sectp);
1840 }
1841 else if (names.macro.matches (sectp->name))
1842 {
1843 this->macro.s.section = sectp;
1844 this->macro.size = bfd_section_size (sectp);
1845 }
1846 else if (names.str.matches (sectp->name))
1847 {
1848 this->str.s.section = sectp;
1849 this->str.size = bfd_section_size (sectp);
1850 }
1851 else if (names.str_offsets.matches (sectp->name))
1852 {
1853 this->str_offsets.s.section = sectp;
1854 this->str_offsets.size = bfd_section_size (sectp);
1855 }
1856 else if (names.line_str.matches (sectp->name))
1857 {
1858 this->line_str.s.section = sectp;
1859 this->line_str.size = bfd_section_size (sectp);
1860 }
1861 else if (names.addr.matches (sectp->name))
1862 {
1863 this->addr.s.section = sectp;
1864 this->addr.size = bfd_section_size (sectp);
1865 }
1866 else if (names.frame.matches (sectp->name))
1867 {
1868 this->frame.s.section = sectp;
1869 this->frame.size = bfd_section_size (sectp);
1870 }
1871 else if (names.eh_frame.matches (sectp->name))
1872 {
1873 this->eh_frame.s.section = sectp;
1874 this->eh_frame.size = bfd_section_size (sectp);
1875 }
1876 else if (names.ranges.matches (sectp->name))
1877 {
1878 this->ranges.s.section = sectp;
1879 this->ranges.size = bfd_section_size (sectp);
1880 }
1881 else if (names.rnglists.matches (sectp->name))
1882 {
1883 this->rnglists.s.section = sectp;
1884 this->rnglists.size = bfd_section_size (sectp);
1885 }
1886 else if (names.types.matches (sectp->name))
1887 {
1888 struct dwarf2_section_info type_section;
1889
1890 memset (&type_section, 0, sizeof (type_section));
1891 type_section.s.section = sectp;
1892 type_section.size = bfd_section_size (sectp);
1893
1894 this->types.push_back (type_section);
1895 }
1896 else if (names.gdb_index.matches (sectp->name))
1897 {
1898 this->gdb_index.s.section = sectp;
1899 this->gdb_index.size = bfd_section_size (sectp);
1900 }
1901 else if (names.debug_names.matches (sectp->name))
1902 {
1903 this->debug_names.s.section = sectp;
1904 this->debug_names.size = bfd_section_size (sectp);
1905 }
1906 else if (names.debug_aranges.matches (sectp->name))
1907 {
1908 this->debug_aranges.s.section = sectp;
1909 this->debug_aranges.size = bfd_section_size (sectp);
1910 }
1911
1912 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1913 && bfd_section_vma (sectp) == 0)
1914 this->has_section_at_zero = true;
1915 }
1916
1917 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1918 SECTION_NAME. */
1919
1920 void
1921 dwarf2_get_section_info (struct objfile *objfile,
1922 enum dwarf2_section_enum sect,
1923 asection **sectp, const gdb_byte **bufp,
1924 bfd_size_type *sizep)
1925 {
1926 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1927 struct dwarf2_section_info *info;
1928
1929 /* We may see an objfile without any DWARF, in which case we just
1930 return nothing. */
1931 if (per_objfile == NULL)
1932 {
1933 *sectp = NULL;
1934 *bufp = NULL;
1935 *sizep = 0;
1936 return;
1937 }
1938 switch (sect)
1939 {
1940 case DWARF2_DEBUG_FRAME:
1941 info = &per_objfile->per_bfd->frame;
1942 break;
1943 case DWARF2_EH_FRAME:
1944 info = &per_objfile->per_bfd->eh_frame;
1945 break;
1946 default:
1947 gdb_assert_not_reached ("unexpected section");
1948 }
1949
1950 info->read (objfile);
1951
1952 *sectp = info->get_bfd_section ();
1953 *bufp = info->buffer;
1954 *sizep = info->size;
1955 }
1956
1957 \f
1958 /* DWARF quick_symbol_functions support. */
1959
1960 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1961 unique line tables, so we maintain a separate table of all .debug_line
1962 derived entries to support the sharing.
1963 All the quick functions need is the list of file names. We discard the
1964 line_header when we're done and don't need to record it here. */
1965 struct quick_file_names
1966 {
1967 /* The data used to construct the hash key. */
1968 struct stmt_list_hash hash;
1969
1970 /* The number of entries in file_names, real_names. */
1971 unsigned int num_file_names;
1972
1973 /* The file names from the line table, after being run through
1974 file_full_name. */
1975 const char **file_names;
1976
1977 /* The file names from the line table after being run through
1978 gdb_realpath. These are computed lazily. */
1979 const char **real_names;
1980 };
1981
1982 /* When using the index (and thus not using psymtabs), each CU has an
1983 object of this type. This is used to hold information needed by
1984 the various "quick" methods. */
1985 struct dwarf2_per_cu_quick_data
1986 {
1987 /* The file table. This can be NULL if there was no file table
1988 or it's currently not read in.
1989 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
1990 struct quick_file_names *file_names;
1991
1992 /* A temporary mark bit used when iterating over all CUs in
1993 expand_symtabs_matching. */
1994 unsigned int mark : 1;
1995
1996 /* True if we've tried to read the file table and found there isn't one.
1997 There will be no point in trying to read it again next time. */
1998 unsigned int no_file_data : 1;
1999 };
2000
2001 /* A subclass of psymbol_functions that arranges to read the DWARF
2002 partial symbols when needed. */
2003 struct lazy_dwarf_reader : public psymbol_functions
2004 {
2005 using psymbol_functions::psymbol_functions;
2006
2007 bool can_lazily_read_symbols () override
2008 {
2009 return true;
2010 }
2011
2012 void read_partial_symbols (struct objfile *objfile) override
2013 {
2014 if (dwarf2_has_info (objfile, nullptr))
2015 dwarf2_build_psymtabs (objfile, this);
2016 }
2017 };
2018
2019 static quick_symbol_functions_up
2020 make_lazy_dwarf_reader ()
2021 {
2022 return quick_symbol_functions_up (new lazy_dwarf_reader);
2023 }
2024
2025 struct dwarf2_base_index_functions : public quick_symbol_functions
2026 {
2027 bool has_symbols (struct objfile *objfile) override;
2028
2029 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2030
2031 void forget_cached_source_info (struct objfile *objfile) override;
2032
2033 enum language lookup_global_symbol_language (struct objfile *objfile,
2034 const char *name,
2035 domain_enum domain,
2036 bool *symbol_found_p) override
2037 {
2038 *symbol_found_p = false;
2039 return language_unknown;
2040 }
2041
2042 void print_stats (struct objfile *objfile, bool print_bcache) override;
2043
2044 void expand_all_symtabs (struct objfile *objfile) override;
2045
2046 struct compunit_symtab *find_pc_sect_compunit_symtab
2047 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2048 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2049
2050 struct compunit_symtab *find_compunit_symtab_by_address
2051 (struct objfile *objfile, CORE_ADDR address) override
2052 {
2053 return nullptr;
2054 }
2055
2056 void map_symbol_filenames (struct objfile *objfile,
2057 gdb::function_view<symbol_filename_ftype> fun,
2058 bool need_fullname) override;
2059 };
2060
2061 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2062 {
2063 void dump (struct objfile *objfile) override;
2064
2065 void expand_matching_symbols
2066 (struct objfile *,
2067 const lookup_name_info &lookup_name,
2068 domain_enum domain,
2069 int global,
2070 symbol_compare_ftype *ordered_compare) override;
2071
2072 bool expand_symtabs_matching
2073 (struct objfile *objfile,
2074 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2075 const lookup_name_info *lookup_name,
2076 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2077 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2078 block_search_flags search_flags,
2079 domain_enum domain,
2080 enum search_domain kind) override;
2081 };
2082
2083 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2084 {
2085 void dump (struct objfile *objfile) override;
2086
2087 void expand_matching_symbols
2088 (struct objfile *,
2089 const lookup_name_info &lookup_name,
2090 domain_enum domain,
2091 int global,
2092 symbol_compare_ftype *ordered_compare) override;
2093
2094 bool expand_symtabs_matching
2095 (struct objfile *objfile,
2096 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2097 const lookup_name_info *lookup_name,
2098 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2099 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2100 block_search_flags search_flags,
2101 domain_enum domain,
2102 enum search_domain kind) override;
2103 };
2104
2105 static quick_symbol_functions_up
2106 make_dwarf_gdb_index ()
2107 {
2108 return quick_symbol_functions_up (new dwarf2_gdb_index);
2109 }
2110
2111 static quick_symbol_functions_up
2112 make_dwarf_debug_names ()
2113 {
2114 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2115 }
2116
2117 /* Utility hash function for a stmt_list_hash. */
2118
2119 static hashval_t
2120 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2121 {
2122 hashval_t v = 0;
2123
2124 if (stmt_list_hash->dwo_unit != NULL)
2125 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2126 v += to_underlying (stmt_list_hash->line_sect_off);
2127 return v;
2128 }
2129
2130 /* Utility equality function for a stmt_list_hash. */
2131
2132 static int
2133 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2134 const struct stmt_list_hash *rhs)
2135 {
2136 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2137 return 0;
2138 if (lhs->dwo_unit != NULL
2139 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2140 return 0;
2141
2142 return lhs->line_sect_off == rhs->line_sect_off;
2143 }
2144
2145 /* Hash function for a quick_file_names. */
2146
2147 static hashval_t
2148 hash_file_name_entry (const void *e)
2149 {
2150 const struct quick_file_names *file_data
2151 = (const struct quick_file_names *) e;
2152
2153 return hash_stmt_list_entry (&file_data->hash);
2154 }
2155
2156 /* Equality function for a quick_file_names. */
2157
2158 static int
2159 eq_file_name_entry (const void *a, const void *b)
2160 {
2161 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2162 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2163
2164 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2165 }
2166
2167 /* Delete function for a quick_file_names. */
2168
2169 static void
2170 delete_file_name_entry (void *e)
2171 {
2172 struct quick_file_names *file_data = (struct quick_file_names *) e;
2173 int i;
2174
2175 for (i = 0; i < file_data->num_file_names; ++i)
2176 {
2177 xfree ((void*) file_data->file_names[i]);
2178 if (file_data->real_names)
2179 xfree ((void*) file_data->real_names[i]);
2180 }
2181
2182 /* The space for the struct itself lives on the obstack, so we don't
2183 free it here. */
2184 }
2185
2186 /* Create a quick_file_names hash table. */
2187
2188 static htab_up
2189 create_quick_file_names_table (unsigned int nr_initial_entries)
2190 {
2191 return htab_up (htab_create_alloc (nr_initial_entries,
2192 hash_file_name_entry, eq_file_name_entry,
2193 delete_file_name_entry, xcalloc, xfree));
2194 }
2195
2196 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2197 function is unrelated to symtabs, symtab would have to be created afterwards.
2198 You should call age_cached_comp_units after processing the CU. */
2199
2200 static dwarf2_cu *
2201 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2202 bool skip_partial)
2203 {
2204 if (per_cu->is_debug_types)
2205 load_full_type_unit (per_cu, per_objfile);
2206 else
2207 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2208 skip_partial, language_minimal);
2209
2210 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2211 if (cu == nullptr)
2212 return nullptr; /* Dummy CU. */
2213
2214 dwarf2_find_base_address (cu->dies, cu);
2215
2216 return cu;
2217 }
2218
2219 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2220
2221 static void
2222 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2223 dwarf2_per_objfile *per_objfile, bool skip_partial)
2224 {
2225 /* Skip type_unit_groups, reading the type units they contain
2226 is handled elsewhere. */
2227 if (per_cu->type_unit_group_p ())
2228 return;
2229
2230 {
2231 /* The destructor of dwarf2_queue_guard frees any entries left on
2232 the queue. After this point we're guaranteed to leave this function
2233 with the dwarf queue empty. */
2234 dwarf2_queue_guard q_guard (per_objfile);
2235
2236 if (!per_objfile->symtab_set_p (per_cu))
2237 {
2238 queue_comp_unit (per_cu, per_objfile, language_minimal);
2239 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2240
2241 /* If we just loaded a CU from a DWO, and we're working with an index
2242 that may badly handle TUs, load all the TUs in that DWO as well.
2243 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2244 if (!per_cu->is_debug_types
2245 && cu != NULL
2246 && cu->dwo_unit != NULL
2247 && per_objfile->per_bfd->index_table != NULL
2248 && per_objfile->per_bfd->index_table->version <= 7
2249 /* DWP files aren't supported yet. */
2250 && get_dwp_file (per_objfile) == NULL)
2251 queue_and_load_all_dwo_tus (cu);
2252 }
2253
2254 process_queue (per_objfile);
2255 }
2256
2257 /* Age the cache, releasing compilation units that have not
2258 been used recently. */
2259 per_objfile->age_comp_units ();
2260 }
2261
2262 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2263 the per-objfile for which this symtab is instantiated.
2264
2265 Returns the resulting symbol table. */
2266
2267 static struct compunit_symtab *
2268 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2269 dwarf2_per_objfile *per_objfile,
2270 bool skip_partial)
2271 {
2272 gdb_assert (per_objfile->per_bfd->using_index);
2273
2274 if (!per_objfile->symtab_set_p (per_cu))
2275 {
2276 free_cached_comp_units freer (per_objfile);
2277 scoped_restore decrementer = increment_reading_symtab ();
2278 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2279 process_cu_includes (per_objfile);
2280 }
2281
2282 return per_objfile->get_symtab (per_cu);
2283 }
2284
2285 /* See read.h. */
2286
2287 dwarf2_per_cu_data_up
2288 dwarf2_per_bfd::allocate_per_cu ()
2289 {
2290 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2291 result->per_bfd = this;
2292 result->index = all_comp_units.size ();
2293 return result;
2294 }
2295
2296 /* See read.h. */
2297
2298 signatured_type_up
2299 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2300 {
2301 signatured_type_up result (new signatured_type (signature));
2302 result->per_bfd = this;
2303 result->index = all_comp_units.size ();
2304 result->is_debug_types = true;
2305 tu_stats.nr_tus++;
2306 return result;
2307 }
2308
2309 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2310 obstack, and constructed with the specified field values. */
2311
2312 static dwarf2_per_cu_data_up
2313 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2314 struct dwarf2_section_info *section,
2315 int is_dwz,
2316 sect_offset sect_off, ULONGEST length)
2317 {
2318 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2319 the_cu->sect_off = sect_off;
2320 the_cu->length = length;
2321 the_cu->section = section;
2322 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2323 struct dwarf2_per_cu_quick_data);
2324 the_cu->is_dwz = is_dwz;
2325 return the_cu;
2326 }
2327
2328 /* A helper for create_cus_from_index that handles a given list of
2329 CUs. */
2330
2331 static void
2332 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2333 const gdb_byte *cu_list, offset_type n_elements,
2334 struct dwarf2_section_info *section,
2335 int is_dwz)
2336 {
2337 for (offset_type i = 0; i < n_elements; i += 2)
2338 {
2339 gdb_static_assert (sizeof (ULONGEST) >= 8);
2340
2341 sect_offset sect_off
2342 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2343 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2344 cu_list += 2 * 8;
2345
2346 dwarf2_per_cu_data_up per_cu
2347 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2348 length);
2349 per_bfd->all_comp_units.push_back (std::move (per_cu));
2350 }
2351 }
2352
2353 /* Read the CU list from the mapped index, and use it to create all
2354 the CU objects for PER_BFD. */
2355
2356 static void
2357 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2358 const gdb_byte *cu_list, offset_type cu_list_elements,
2359 const gdb_byte *dwz_list, offset_type dwz_elements)
2360 {
2361 gdb_assert (per_bfd->all_comp_units.empty ());
2362 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2363
2364 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2365 &per_bfd->info, 0);
2366
2367 if (dwz_elements == 0)
2368 return;
2369
2370 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2371 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2372 &dwz->info, 1);
2373 }
2374
2375 /* Create the signatured type hash table from the index. */
2376
2377 static void
2378 create_signatured_type_table_from_index
2379 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2380 const gdb_byte *bytes, offset_type elements)
2381 {
2382 htab_up sig_types_hash = allocate_signatured_type_table ();
2383
2384 for (offset_type i = 0; i < elements; i += 3)
2385 {
2386 signatured_type_up sig_type;
2387 ULONGEST signature;
2388 void **slot;
2389 cu_offset type_offset_in_tu;
2390
2391 gdb_static_assert (sizeof (ULONGEST) >= 8);
2392 sect_offset sect_off
2393 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2394 type_offset_in_tu
2395 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2396 BFD_ENDIAN_LITTLE);
2397 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2398 bytes += 3 * 8;
2399
2400 sig_type = per_bfd->allocate_signatured_type (signature);
2401 sig_type->type_offset_in_tu = type_offset_in_tu;
2402 sig_type->section = section;
2403 sig_type->sect_off = sect_off;
2404 sig_type->v.quick
2405 = OBSTACK_ZALLOC (&per_bfd->obstack,
2406 struct dwarf2_per_cu_quick_data);
2407
2408 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2409 *slot = sig_type.get ();
2410
2411 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2412 }
2413
2414 per_bfd->signatured_types = std::move (sig_types_hash);
2415 }
2416
2417 /* Create the signatured type hash table from .debug_names. */
2418
2419 static void
2420 create_signatured_type_table_from_debug_names
2421 (dwarf2_per_objfile *per_objfile,
2422 const mapped_debug_names &map,
2423 struct dwarf2_section_info *section,
2424 struct dwarf2_section_info *abbrev_section)
2425 {
2426 struct objfile *objfile = per_objfile->objfile;
2427
2428 section->read (objfile);
2429 abbrev_section->read (objfile);
2430
2431 htab_up sig_types_hash = allocate_signatured_type_table ();
2432
2433 for (uint32_t i = 0; i < map.tu_count; ++i)
2434 {
2435 signatured_type_up sig_type;
2436 void **slot;
2437
2438 sect_offset sect_off
2439 = (sect_offset) (extract_unsigned_integer
2440 (map.tu_table_reordered + i * map.offset_size,
2441 map.offset_size,
2442 map.dwarf5_byte_order));
2443
2444 comp_unit_head cu_header;
2445 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2446 abbrev_section,
2447 section->buffer + to_underlying (sect_off),
2448 rcuh_kind::TYPE);
2449
2450 sig_type = per_objfile->per_bfd->allocate_signatured_type
2451 (cu_header.signature);
2452 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2453 sig_type->section = section;
2454 sig_type->sect_off = sect_off;
2455 sig_type->v.quick
2456 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2457 struct dwarf2_per_cu_quick_data);
2458
2459 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2460 *slot = sig_type.get ();
2461
2462 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2463 }
2464
2465 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2466 }
2467
2468 /* Read the address map data from the mapped index, and use it to
2469 populate the psymtabs_addrmap. */
2470
2471 static void
2472 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2473 struct mapped_index *index)
2474 {
2475 struct objfile *objfile = per_objfile->objfile;
2476 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2477 struct gdbarch *gdbarch = objfile->arch ();
2478 const gdb_byte *iter, *end;
2479 struct addrmap *mutable_map;
2480 CORE_ADDR baseaddr;
2481
2482 auto_obstack temp_obstack;
2483
2484 mutable_map = addrmap_create_mutable (&temp_obstack);
2485
2486 iter = index->address_table.data ();
2487 end = iter + index->address_table.size ();
2488
2489 baseaddr = objfile->text_section_offset ();
2490
2491 while (iter < end)
2492 {
2493 ULONGEST hi, lo, cu_index;
2494 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2495 iter += 8;
2496 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2497 iter += 8;
2498 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2499 iter += 4;
2500
2501 if (lo > hi)
2502 {
2503 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2504 hex_string (lo), hex_string (hi));
2505 continue;
2506 }
2507
2508 if (cu_index >= per_bfd->all_comp_units.size ())
2509 {
2510 complaint (_(".gdb_index address table has invalid CU number %u"),
2511 (unsigned) cu_index);
2512 continue;
2513 }
2514
2515 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2516 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2517 addrmap_set_empty (mutable_map, lo, hi - 1,
2518 per_bfd->get_cu (cu_index));
2519 }
2520
2521 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2522 &per_bfd->obstack);
2523 }
2524
2525 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2526 populate the psymtabs_addrmap. */
2527
2528 static void
2529 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2530 struct dwarf2_section_info *section)
2531 {
2532 struct objfile *objfile = per_objfile->objfile;
2533 bfd *abfd = objfile->obfd;
2534 struct gdbarch *gdbarch = objfile->arch ();
2535 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2536 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2537
2538 auto_obstack temp_obstack;
2539 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2540
2541 std::unordered_map<sect_offset,
2542 dwarf2_per_cu_data *,
2543 gdb::hash_enum<sect_offset>>
2544 debug_info_offset_to_per_cu;
2545 for (const auto &per_cu : per_bfd->all_comp_units)
2546 {
2547 const auto insertpair
2548 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2549 per_cu.get ());
2550 if (!insertpair.second)
2551 {
2552 warning (_("Section .debug_aranges in %s has duplicate "
2553 "debug_info_offset %s, ignoring .debug_aranges."),
2554 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2555 return;
2556 }
2557 }
2558
2559 section->read (objfile);
2560
2561 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2562
2563 const gdb_byte *addr = section->buffer;
2564
2565 while (addr < section->buffer + section->size)
2566 {
2567 const gdb_byte *const entry_addr = addr;
2568 unsigned int bytes_read;
2569
2570 const LONGEST entry_length = read_initial_length (abfd, addr,
2571 &bytes_read);
2572 addr += bytes_read;
2573
2574 const gdb_byte *const entry_end = addr + entry_length;
2575 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2576 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2577 if (addr + entry_length > section->buffer + section->size)
2578 {
2579 warning (_("Section .debug_aranges in %s entry at offset %s "
2580 "length %s exceeds section length %s, "
2581 "ignoring .debug_aranges."),
2582 objfile_name (objfile),
2583 plongest (entry_addr - section->buffer),
2584 plongest (bytes_read + entry_length),
2585 pulongest (section->size));
2586 return;
2587 }
2588
2589 /* The version number. */
2590 const uint16_t version = read_2_bytes (abfd, addr);
2591 addr += 2;
2592 if (version != 2)
2593 {
2594 warning (_("Section .debug_aranges in %s entry at offset %s "
2595 "has unsupported version %d, ignoring .debug_aranges."),
2596 objfile_name (objfile),
2597 plongest (entry_addr - section->buffer), version);
2598 return;
2599 }
2600
2601 const uint64_t debug_info_offset
2602 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2603 addr += offset_size;
2604 const auto per_cu_it
2605 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2606 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2607 {
2608 warning (_("Section .debug_aranges in %s entry at offset %s "
2609 "debug_info_offset %s does not exists, "
2610 "ignoring .debug_aranges."),
2611 objfile_name (objfile),
2612 plongest (entry_addr - section->buffer),
2613 pulongest (debug_info_offset));
2614 return;
2615 }
2616 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2617
2618 const uint8_t address_size = *addr++;
2619 if (address_size < 1 || address_size > 8)
2620 {
2621 warning (_("Section .debug_aranges in %s entry at offset %s "
2622 "address_size %u is invalid, ignoring .debug_aranges."),
2623 objfile_name (objfile),
2624 plongest (entry_addr - section->buffer), address_size);
2625 return;
2626 }
2627
2628 const uint8_t segment_selector_size = *addr++;
2629 if (segment_selector_size != 0)
2630 {
2631 warning (_("Section .debug_aranges in %s entry at offset %s "
2632 "segment_selector_size %u is not supported, "
2633 "ignoring .debug_aranges."),
2634 objfile_name (objfile),
2635 plongest (entry_addr - section->buffer),
2636 segment_selector_size);
2637 return;
2638 }
2639
2640 /* Must pad to an alignment boundary that is twice the address
2641 size. It is undocumented by the DWARF standard but GCC does
2642 use it. */
2643 for (size_t padding = ((-(addr - section->buffer))
2644 & (2 * address_size - 1));
2645 padding > 0; padding--)
2646 if (*addr++ != 0)
2647 {
2648 warning (_("Section .debug_aranges in %s entry at offset %s "
2649 "padding is not zero, ignoring .debug_aranges."),
2650 objfile_name (objfile),
2651 plongest (entry_addr - section->buffer));
2652 return;
2653 }
2654
2655 for (;;)
2656 {
2657 if (addr + 2 * address_size > entry_end)
2658 {
2659 warning (_("Section .debug_aranges in %s entry at offset %s "
2660 "address list is not properly terminated, "
2661 "ignoring .debug_aranges."),
2662 objfile_name (objfile),
2663 plongest (entry_addr - section->buffer));
2664 return;
2665 }
2666 ULONGEST start = extract_unsigned_integer (addr, address_size,
2667 dwarf5_byte_order);
2668 addr += address_size;
2669 ULONGEST length = extract_unsigned_integer (addr, address_size,
2670 dwarf5_byte_order);
2671 addr += address_size;
2672 if (start == 0 && length == 0)
2673 break;
2674 if (start == 0 && !per_bfd->has_section_at_zero)
2675 {
2676 /* Symbol was eliminated due to a COMDAT group. */
2677 continue;
2678 }
2679 ULONGEST end = start + length;
2680 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2681 - baseaddr);
2682 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2683 - baseaddr);
2684 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2685 }
2686 }
2687
2688 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2689 &per_bfd->obstack);
2690 }
2691
2692 /* A helper function that reads the .gdb_index from BUFFER and fills
2693 in MAP. FILENAME is the name of the file containing the data;
2694 it is used for error reporting. DEPRECATED_OK is true if it is
2695 ok to use deprecated sections.
2696
2697 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2698 out parameters that are filled in with information about the CU and
2699 TU lists in the section.
2700
2701 Returns true if all went well, false otherwise. */
2702
2703 static bool
2704 read_gdb_index_from_buffer (const char *filename,
2705 bool deprecated_ok,
2706 gdb::array_view<const gdb_byte> buffer,
2707 struct mapped_index *map,
2708 const gdb_byte **cu_list,
2709 offset_type *cu_list_elements,
2710 const gdb_byte **types_list,
2711 offset_type *types_list_elements)
2712 {
2713 const gdb_byte *addr = &buffer[0];
2714 offset_view metadata (buffer);
2715
2716 /* Version check. */
2717 offset_type version = metadata[0];
2718 /* Versions earlier than 3 emitted every copy of a psymbol. This
2719 causes the index to behave very poorly for certain requests. Version 3
2720 contained incomplete addrmap. So, it seems better to just ignore such
2721 indices. */
2722 if (version < 4)
2723 {
2724 static int warning_printed = 0;
2725 if (!warning_printed)
2726 {
2727 warning (_("Skipping obsolete .gdb_index section in %s."),
2728 filename);
2729 warning_printed = 1;
2730 }
2731 return 0;
2732 }
2733 /* Index version 4 uses a different hash function than index version
2734 5 and later.
2735
2736 Versions earlier than 6 did not emit psymbols for inlined
2737 functions. Using these files will cause GDB not to be able to
2738 set breakpoints on inlined functions by name, so we ignore these
2739 indices unless the user has done
2740 "set use-deprecated-index-sections on". */
2741 if (version < 6 && !deprecated_ok)
2742 {
2743 static int warning_printed = 0;
2744 if (!warning_printed)
2745 {
2746 warning (_("\
2747 Skipping deprecated .gdb_index section in %s.\n\
2748 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2749 to use the section anyway."),
2750 filename);
2751 warning_printed = 1;
2752 }
2753 return 0;
2754 }
2755 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2756 of the TU (for symbols coming from TUs),
2757 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2758 Plus gold-generated indices can have duplicate entries for global symbols,
2759 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2760 These are just performance bugs, and we can't distinguish gdb-generated
2761 indices from gold-generated ones, so issue no warning here. */
2762
2763 /* Indexes with higher version than the one supported by GDB may be no
2764 longer backward compatible. */
2765 if (version > 8)
2766 return 0;
2767
2768 map->version = version;
2769
2770 int i = 1;
2771 *cu_list = addr + metadata[i];
2772 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2773 ++i;
2774
2775 *types_list = addr + metadata[i];
2776 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2777 ++i;
2778
2779 const gdb_byte *address_table = addr + metadata[i];
2780 const gdb_byte *address_table_end = addr + metadata[i + 1];
2781 map->address_table
2782 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2783 ++i;
2784
2785 const gdb_byte *symbol_table = addr + metadata[i];
2786 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2787 map->symbol_table
2788 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2789 symbol_table_end));
2790
2791 ++i;
2792 map->constant_pool = buffer.slice (metadata[i]);
2793
2794 return 1;
2795 }
2796
2797 /* Callback types for dwarf2_read_gdb_index. */
2798
2799 typedef gdb::function_view
2800 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2801 get_gdb_index_contents_ftype;
2802 typedef gdb::function_view
2803 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2804 get_gdb_index_contents_dwz_ftype;
2805
2806 /* Read .gdb_index. If everything went ok, initialize the "quick"
2807 elements of all the CUs and return 1. Otherwise, return 0. */
2808
2809 static int
2810 dwarf2_read_gdb_index
2811 (dwarf2_per_objfile *per_objfile,
2812 get_gdb_index_contents_ftype get_gdb_index_contents,
2813 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2814 {
2815 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2816 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2817 struct dwz_file *dwz;
2818 struct objfile *objfile = per_objfile->objfile;
2819 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2820
2821 gdb::array_view<const gdb_byte> main_index_contents
2822 = get_gdb_index_contents (objfile, per_bfd);
2823
2824 if (main_index_contents.empty ())
2825 return 0;
2826
2827 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2828 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2829 use_deprecated_index_sections,
2830 main_index_contents, map.get (), &cu_list,
2831 &cu_list_elements, &types_list,
2832 &types_list_elements))
2833 return 0;
2834
2835 /* Don't use the index if it's empty. */
2836 if (map->symbol_table.empty ())
2837 return 0;
2838
2839 /* If there is a .dwz file, read it so we can get its CU list as
2840 well. */
2841 dwz = dwarf2_get_dwz_file (per_bfd);
2842 if (dwz != NULL)
2843 {
2844 struct mapped_index dwz_map;
2845 const gdb_byte *dwz_types_ignore;
2846 offset_type dwz_types_elements_ignore;
2847
2848 gdb::array_view<const gdb_byte> dwz_index_content
2849 = get_gdb_index_contents_dwz (objfile, dwz);
2850
2851 if (dwz_index_content.empty ())
2852 return 0;
2853
2854 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2855 1, dwz_index_content, &dwz_map,
2856 &dwz_list, &dwz_list_elements,
2857 &dwz_types_ignore,
2858 &dwz_types_elements_ignore))
2859 {
2860 warning (_("could not read '.gdb_index' section from %s; skipping"),
2861 bfd_get_filename (dwz->dwz_bfd.get ()));
2862 return 0;
2863 }
2864 }
2865
2866 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2867 dwz_list_elements);
2868
2869 if (types_list_elements)
2870 {
2871 /* We can only handle a single .debug_types when we have an
2872 index. */
2873 if (per_bfd->types.size () != 1)
2874 return 0;
2875
2876 dwarf2_section_info *section = &per_bfd->types[0];
2877
2878 create_signatured_type_table_from_index (per_bfd, section, types_list,
2879 types_list_elements);
2880 }
2881
2882 create_addrmap_from_index (per_objfile, map.get ());
2883
2884 per_bfd->index_table = std::move (map);
2885 per_bfd->using_index = 1;
2886 per_bfd->quick_file_names_table =
2887 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2888
2889 return 1;
2890 }
2891
2892 /* die_reader_func for dw2_get_file_names. */
2893
2894 static void
2895 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2896 struct die_info *comp_unit_die)
2897 {
2898 struct dwarf2_cu *cu = reader->cu;
2899 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2900 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2901 struct dwarf2_per_cu_data *lh_cu;
2902 struct attribute *attr;
2903 void **slot;
2904 struct quick_file_names *qfn;
2905
2906 gdb_assert (! this_cu->is_debug_types);
2907
2908 /* Our callers never want to match partial units -- instead they
2909 will match the enclosing full CU. */
2910 if (comp_unit_die->tag == DW_TAG_partial_unit)
2911 {
2912 this_cu->v.quick->no_file_data = 1;
2913 return;
2914 }
2915
2916 lh_cu = this_cu;
2917 slot = NULL;
2918
2919 line_header_up lh;
2920 sect_offset line_offset {};
2921
2922 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2923 if (attr != nullptr && attr->form_is_unsigned ())
2924 {
2925 struct quick_file_names find_entry;
2926
2927 line_offset = (sect_offset) attr->as_unsigned ();
2928
2929 /* We may have already read in this line header (TU line header sharing).
2930 If we have we're done. */
2931 find_entry.hash.dwo_unit = cu->dwo_unit;
2932 find_entry.hash.line_sect_off = line_offset;
2933 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2934 &find_entry, INSERT);
2935 if (*slot != NULL)
2936 {
2937 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
2938 return;
2939 }
2940
2941 lh = dwarf_decode_line_header (line_offset, cu);
2942 }
2943 if (lh == NULL)
2944 {
2945 lh_cu->v.quick->no_file_data = 1;
2946 return;
2947 }
2948
2949 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2950 qfn->hash.dwo_unit = cu->dwo_unit;
2951 qfn->hash.line_sect_off = line_offset;
2952 gdb_assert (slot != NULL);
2953 *slot = qfn;
2954
2955 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
2956
2957 int offset = 0;
2958 if (strcmp (fnd.name, "<unknown>") != 0)
2959 ++offset;
2960
2961 qfn->num_file_names = offset + lh->file_names_size ();
2962 qfn->file_names =
2963 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2964 qfn->num_file_names);
2965 if (offset != 0)
2966 qfn->file_names[0] = xstrdup (fnd.name);
2967 for (int i = 0; i < lh->file_names_size (); ++i)
2968 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
2969 fnd.comp_dir).release ();
2970 qfn->real_names = NULL;
2971
2972 lh_cu->v.quick->file_names = qfn;
2973 }
2974
2975 /* A helper for the "quick" functions which attempts to read the line
2976 table for THIS_CU. */
2977
2978 static struct quick_file_names *
2979 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2980 dwarf2_per_objfile *per_objfile)
2981 {
2982 /* This should never be called for TUs. */
2983 gdb_assert (! this_cu->is_debug_types);
2984 /* Nor type unit groups. */
2985 gdb_assert (! this_cu->type_unit_group_p ());
2986
2987 if (this_cu->v.quick->file_names != NULL)
2988 return this_cu->v.quick->file_names;
2989 /* If we know there is no line data, no point in looking again. */
2990 if (this_cu->v.quick->no_file_data)
2991 return NULL;
2992
2993 cutu_reader reader (this_cu, per_objfile);
2994 if (!reader.dummy_p)
2995 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2996
2997 if (this_cu->v.quick->no_file_data)
2998 return NULL;
2999 return this_cu->v.quick->file_names;
3000 }
3001
3002 /* A helper for the "quick" functions which computes and caches the
3003 real path for a given file name from the line table. */
3004
3005 static const char *
3006 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3007 struct quick_file_names *qfn, int index)
3008 {
3009 if (qfn->real_names == NULL)
3010 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3011 qfn->num_file_names, const char *);
3012
3013 if (qfn->real_names[index] == NULL)
3014 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3015
3016 return qfn->real_names[index];
3017 }
3018
3019 struct symtab *
3020 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3021 {
3022 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3023 dwarf2_per_cu_data *dwarf_cu
3024 = per_objfile->per_bfd->all_comp_units.back ().get ();
3025 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3026
3027 if (cust == NULL)
3028 return NULL;
3029
3030 return compunit_primary_filetab (cust);
3031 }
3032
3033 /* Traversal function for dw2_forget_cached_source_info. */
3034
3035 static int
3036 dw2_free_cached_file_names (void **slot, void *info)
3037 {
3038 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3039
3040 if (file_data->real_names)
3041 {
3042 int i;
3043
3044 for (i = 0; i < file_data->num_file_names; ++i)
3045 {
3046 xfree ((void*) file_data->real_names[i]);
3047 file_data->real_names[i] = NULL;
3048 }
3049 }
3050
3051 return 1;
3052 }
3053
3054 void
3055 dwarf2_base_index_functions::forget_cached_source_info
3056 (struct objfile *objfile)
3057 {
3058 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3059
3060 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3061 dw2_free_cached_file_names, NULL);
3062 }
3063
3064 /* Struct used to manage iterating over all CUs looking for a symbol. */
3065
3066 struct dw2_symtab_iterator
3067 {
3068 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3069 dwarf2_per_objfile *per_objfile;
3070 /* If set, only look for symbols that match that block. Valid values are
3071 GLOBAL_BLOCK and STATIC_BLOCK. */
3072 gdb::optional<block_enum> block_index;
3073 /* The kind of symbol we're looking for. */
3074 domain_enum domain;
3075 /* The list of CUs from the index entry of the symbol,
3076 or NULL if not found. */
3077 offset_view vec;
3078 /* The next element in VEC to look at. */
3079 int next;
3080 /* The number of elements in VEC, or zero if there is no match. */
3081 int length;
3082 /* Have we seen a global version of the symbol?
3083 If so we can ignore all further global instances.
3084 This is to work around gold/15646, inefficient gold-generated
3085 indices. */
3086 int global_seen;
3087 };
3088
3089 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3090
3091 static void
3092 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3093 dwarf2_per_objfile *per_objfile,
3094 gdb::optional<block_enum> block_index,
3095 domain_enum domain, offset_type namei)
3096 {
3097 iter->per_objfile = per_objfile;
3098 iter->block_index = block_index;
3099 iter->domain = domain;
3100 iter->next = 0;
3101 iter->global_seen = 0;
3102 iter->vec = {};
3103 iter->length = 0;
3104
3105 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3106 /* index is NULL if OBJF_READNOW. */
3107 if (index == NULL)
3108 return;
3109
3110 gdb_assert (!index->symbol_name_slot_invalid (namei));
3111 offset_type vec_idx = index->symbol_vec_index (namei);
3112
3113 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3114 iter->length = iter->vec[0];
3115 }
3116
3117 /* Return the next matching CU or NULL if there are no more. */
3118
3119 static struct dwarf2_per_cu_data *
3120 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3121 {
3122 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3123
3124 for ( ; iter->next < iter->length; ++iter->next)
3125 {
3126 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3127 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3128 gdb_index_symbol_kind symbol_kind =
3129 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3130 /* Only check the symbol attributes if they're present.
3131 Indices prior to version 7 don't record them,
3132 and indices >= 7 may elide them for certain symbols
3133 (gold does this). */
3134 int attrs_valid =
3135 (per_objfile->per_bfd->index_table->version >= 7
3136 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3137
3138 /* Don't crash on bad data. */
3139 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3140 {
3141 complaint (_(".gdb_index entry has bad CU index"
3142 " [in module %s]"), objfile_name (per_objfile->objfile));
3143 continue;
3144 }
3145
3146 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3147
3148 /* Skip if already read in. */
3149 if (per_objfile->symtab_set_p (per_cu))
3150 continue;
3151
3152 /* Check static vs global. */
3153 if (attrs_valid)
3154 {
3155 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3156
3157 if (iter->block_index.has_value ())
3158 {
3159 bool want_static = *iter->block_index == STATIC_BLOCK;
3160
3161 if (is_static != want_static)
3162 continue;
3163 }
3164
3165 /* Work around gold/15646. */
3166 if (!is_static
3167 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3168 {
3169 if (iter->global_seen)
3170 continue;
3171
3172 iter->global_seen = 1;
3173 }
3174 }
3175
3176 /* Only check the symbol's kind if it has one. */
3177 if (attrs_valid)
3178 {
3179 switch (iter->domain)
3180 {
3181 case VAR_DOMAIN:
3182 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3183 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3184 /* Some types are also in VAR_DOMAIN. */
3185 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3186 continue;
3187 break;
3188 case STRUCT_DOMAIN:
3189 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3190 continue;
3191 break;
3192 case LABEL_DOMAIN:
3193 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3194 continue;
3195 break;
3196 case MODULE_DOMAIN:
3197 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3198 continue;
3199 break;
3200 default:
3201 break;
3202 }
3203 }
3204
3205 ++iter->next;
3206 return per_cu;
3207 }
3208
3209 return NULL;
3210 }
3211
3212 void
3213 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3214 bool print_bcache)
3215 {
3216 if (print_bcache)
3217 return;
3218
3219 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3220 int total = per_objfile->per_bfd->all_comp_units.size ();
3221 int count = 0;
3222
3223 for (int i = 0; i < total; ++i)
3224 {
3225 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3226
3227 if (!per_objfile->symtab_set_p (per_cu))
3228 ++count;
3229 }
3230 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3231 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3232 }
3233
3234 /* This dumps minimal information about the index.
3235 It is called via "mt print objfiles".
3236 One use is to verify .gdb_index has been loaded by the
3237 gdb.dwarf2/gdb-index.exp testcase. */
3238
3239 void
3240 dwarf2_gdb_index::dump (struct objfile *objfile)
3241 {
3242 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3243
3244 gdb_assert (per_objfile->per_bfd->using_index);
3245 printf_filtered (".gdb_index:");
3246 if (per_objfile->per_bfd->index_table != NULL)
3247 {
3248 printf_filtered (" version %d\n",
3249 per_objfile->per_bfd->index_table->version);
3250 }
3251 else
3252 printf_filtered (" faked for \"readnow\"\n");
3253 printf_filtered ("\n");
3254 }
3255
3256 void
3257 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3258 {
3259 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3260 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3261
3262 for (int i = 0; i < total_units; ++i)
3263 {
3264 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3265
3266 /* We don't want to directly expand a partial CU, because if we
3267 read it with the wrong language, then assertion failures can
3268 be triggered later on. See PR symtab/23010. So, tell
3269 dw2_instantiate_symtab to skip partial CUs -- any important
3270 partial CU will be read via DW_TAG_imported_unit anyway. */
3271 dw2_instantiate_symtab (per_cu, per_objfile, true);
3272 }
3273 }
3274
3275 static bool
3276 dw2_expand_symtabs_matching_symbol
3277 (mapped_index_base &index,
3278 const lookup_name_info &lookup_name_in,
3279 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3280 gdb::function_view<bool (offset_type)> match_callback,
3281 dwarf2_per_objfile *per_objfile);
3282
3283 static bool
3284 dw2_expand_symtabs_matching_one
3285 (dwarf2_per_cu_data *per_cu,
3286 dwarf2_per_objfile *per_objfile,
3287 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3288 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3289
3290 void
3291 dwarf2_gdb_index::expand_matching_symbols
3292 (struct objfile *objfile,
3293 const lookup_name_info &name, domain_enum domain,
3294 int global,
3295 symbol_compare_ftype *ordered_compare)
3296 {
3297 /* Used for Ada. */
3298 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3299
3300 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3301
3302 if (per_objfile->per_bfd->index_table != nullptr)
3303 {
3304 mapped_index &index = *per_objfile->per_bfd->index_table;
3305
3306 const char *match_name = name.ada ().lookup_name ().c_str ();
3307 auto matcher = [&] (const char *symname)
3308 {
3309 if (ordered_compare == nullptr)
3310 return true;
3311 return ordered_compare (symname, match_name) == 0;
3312 };
3313
3314 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3315 [&] (offset_type namei)
3316 {
3317 struct dw2_symtab_iterator iter;
3318 struct dwarf2_per_cu_data *per_cu;
3319
3320 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3321 namei);
3322 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3323 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3324 nullptr);
3325 return true;
3326 }, per_objfile);
3327 }
3328 else
3329 {
3330 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3331 proceed assuming all symtabs have been read in. */
3332 }
3333 }
3334
3335 /* Starting from a search name, return the string that finds the upper
3336 bound of all strings that start with SEARCH_NAME in a sorted name
3337 list. Returns the empty string to indicate that the upper bound is
3338 the end of the list. */
3339
3340 static std::string
3341 make_sort_after_prefix_name (const char *search_name)
3342 {
3343 /* When looking to complete "func", we find the upper bound of all
3344 symbols that start with "func" by looking for where we'd insert
3345 the closest string that would follow "func" in lexicographical
3346 order. Usually, that's "func"-with-last-character-incremented,
3347 i.e. "fund". Mind non-ASCII characters, though. Usually those
3348 will be UTF-8 multi-byte sequences, but we can't be certain.
3349 Especially mind the 0xff character, which is a valid character in
3350 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3351 rule out compilers allowing it in identifiers. Note that
3352 conveniently, strcmp/strcasecmp are specified to compare
3353 characters interpreted as unsigned char. So what we do is treat
3354 the whole string as a base 256 number composed of a sequence of
3355 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3356 to 0, and carries 1 to the following more-significant position.
3357 If the very first character in SEARCH_NAME ends up incremented
3358 and carries/overflows, then the upper bound is the end of the
3359 list. The string after the empty string is also the empty
3360 string.
3361
3362 Some examples of this operation:
3363
3364 SEARCH_NAME => "+1" RESULT
3365
3366 "abc" => "abd"
3367 "ab\xff" => "ac"
3368 "\xff" "a" "\xff" => "\xff" "b"
3369 "\xff" => ""
3370 "\xff\xff" => ""
3371 "" => ""
3372
3373 Then, with these symbols for example:
3374
3375 func
3376 func1
3377 fund
3378
3379 completing "func" looks for symbols between "func" and
3380 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3381 which finds "func" and "func1", but not "fund".
3382
3383 And with:
3384
3385 funcÿ (Latin1 'ÿ' [0xff])
3386 funcÿ1
3387 fund
3388
3389 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3390 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3391
3392 And with:
3393
3394 ÿÿ (Latin1 'ÿ' [0xff])
3395 ÿÿ1
3396
3397 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3398 the end of the list.
3399 */
3400 std::string after = search_name;
3401 while (!after.empty () && (unsigned char) after.back () == 0xff)
3402 after.pop_back ();
3403 if (!after.empty ())
3404 after.back () = (unsigned char) after.back () + 1;
3405 return after;
3406 }
3407
3408 /* See declaration. */
3409
3410 std::pair<std::vector<name_component>::const_iterator,
3411 std::vector<name_component>::const_iterator>
3412 mapped_index_base::find_name_components_bounds
3413 (const lookup_name_info &lookup_name_without_params, language lang,
3414 dwarf2_per_objfile *per_objfile) const
3415 {
3416 auto *name_cmp
3417 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3418
3419 const char *lang_name
3420 = lookup_name_without_params.language_lookup_name (lang);
3421
3422 /* Comparison function object for lower_bound that matches against a
3423 given symbol name. */
3424 auto lookup_compare_lower = [&] (const name_component &elem,
3425 const char *name)
3426 {
3427 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3428 const char *elem_name = elem_qualified + elem.name_offset;
3429 return name_cmp (elem_name, name) < 0;
3430 };
3431
3432 /* Comparison function object for upper_bound that matches against a
3433 given symbol name. */
3434 auto lookup_compare_upper = [&] (const char *name,
3435 const name_component &elem)
3436 {
3437 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3438 const char *elem_name = elem_qualified + elem.name_offset;
3439 return name_cmp (name, elem_name) < 0;
3440 };
3441
3442 auto begin = this->name_components.begin ();
3443 auto end = this->name_components.end ();
3444
3445 /* Find the lower bound. */
3446 auto lower = [&] ()
3447 {
3448 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3449 return begin;
3450 else
3451 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3452 } ();
3453
3454 /* Find the upper bound. */
3455 auto upper = [&] ()
3456 {
3457 if (lookup_name_without_params.completion_mode ())
3458 {
3459 /* In completion mode, we want UPPER to point past all
3460 symbols names that have the same prefix. I.e., with
3461 these symbols, and completing "func":
3462
3463 function << lower bound
3464 function1
3465 other_function << upper bound
3466
3467 We find the upper bound by looking for the insertion
3468 point of "func"-with-last-character-incremented,
3469 i.e. "fund". */
3470 std::string after = make_sort_after_prefix_name (lang_name);
3471 if (after.empty ())
3472 return end;
3473 return std::lower_bound (lower, end, after.c_str (),
3474 lookup_compare_lower);
3475 }
3476 else
3477 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3478 } ();
3479
3480 return {lower, upper};
3481 }
3482
3483 /* See declaration. */
3484
3485 void
3486 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3487 {
3488 if (!this->name_components.empty ())
3489 return;
3490
3491 this->name_components_casing = case_sensitivity;
3492 auto *name_cmp
3493 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3494
3495 /* The code below only knows how to break apart components of C++
3496 symbol names (and other languages that use '::' as
3497 namespace/module separator) and Ada symbol names. */
3498 auto count = this->symbol_name_count ();
3499 for (offset_type idx = 0; idx < count; idx++)
3500 {
3501 if (this->symbol_name_slot_invalid (idx))
3502 continue;
3503
3504 const char *name = this->symbol_name_at (idx, per_objfile);
3505
3506 /* Add each name component to the name component table. */
3507 unsigned int previous_len = 0;
3508
3509 if (strstr (name, "::") != nullptr)
3510 {
3511 for (unsigned int current_len = cp_find_first_component (name);
3512 name[current_len] != '\0';
3513 current_len += cp_find_first_component (name + current_len))
3514 {
3515 gdb_assert (name[current_len] == ':');
3516 this->name_components.push_back ({previous_len, idx});
3517 /* Skip the '::'. */
3518 current_len += 2;
3519 previous_len = current_len;
3520 }
3521 }
3522 else
3523 {
3524 /* Handle the Ada encoded (aka mangled) form here. */
3525 for (const char *iter = strstr (name, "__");
3526 iter != nullptr;
3527 iter = strstr (iter, "__"))
3528 {
3529 this->name_components.push_back ({previous_len, idx});
3530 iter += 2;
3531 previous_len = iter - name;
3532 }
3533 }
3534
3535 this->name_components.push_back ({previous_len, idx});
3536 }
3537
3538 /* Sort name_components elements by name. */
3539 auto name_comp_compare = [&] (const name_component &left,
3540 const name_component &right)
3541 {
3542 const char *left_qualified
3543 = this->symbol_name_at (left.idx, per_objfile);
3544 const char *right_qualified
3545 = this->symbol_name_at (right.idx, per_objfile);
3546
3547 const char *left_name = left_qualified + left.name_offset;
3548 const char *right_name = right_qualified + right.name_offset;
3549
3550 return name_cmp (left_name, right_name) < 0;
3551 };
3552
3553 std::sort (this->name_components.begin (),
3554 this->name_components.end (),
3555 name_comp_compare);
3556 }
3557
3558 /* Helper for dw2_expand_symtabs_matching that works with a
3559 mapped_index_base instead of the containing objfile. This is split
3560 to a separate function in order to be able to unit test the
3561 name_components matching using a mock mapped_index_base. For each
3562 symbol name that matches, calls MATCH_CALLBACK, passing it the
3563 symbol's index in the mapped_index_base symbol table. */
3564
3565 static bool
3566 dw2_expand_symtabs_matching_symbol
3567 (mapped_index_base &index,
3568 const lookup_name_info &lookup_name_in,
3569 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3570 gdb::function_view<bool (offset_type)> match_callback,
3571 dwarf2_per_objfile *per_objfile)
3572 {
3573 lookup_name_info lookup_name_without_params
3574 = lookup_name_in.make_ignore_params ();
3575
3576 /* Build the symbol name component sorted vector, if we haven't
3577 yet. */
3578 index.build_name_components (per_objfile);
3579
3580 /* The same symbol may appear more than once in the range though.
3581 E.g., if we're looking for symbols that complete "w", and we have
3582 a symbol named "w1::w2", we'll find the two name components for
3583 that same symbol in the range. To be sure we only call the
3584 callback once per symbol, we first collect the symbol name
3585 indexes that matched in a temporary vector and ignore
3586 duplicates. */
3587 std::vector<offset_type> matches;
3588
3589 struct name_and_matcher
3590 {
3591 symbol_name_matcher_ftype *matcher;
3592 const char *name;
3593
3594 bool operator== (const name_and_matcher &other) const
3595 {
3596 return matcher == other.matcher && strcmp (name, other.name) == 0;
3597 }
3598 };
3599
3600 /* A vector holding all the different symbol name matchers, for all
3601 languages. */
3602 std::vector<name_and_matcher> matchers;
3603
3604 for (int i = 0; i < nr_languages; i++)
3605 {
3606 enum language lang_e = (enum language) i;
3607
3608 const language_defn *lang = language_def (lang_e);
3609 symbol_name_matcher_ftype *name_matcher
3610 = lang->get_symbol_name_matcher (lookup_name_without_params);
3611
3612 name_and_matcher key {
3613 name_matcher,
3614 lookup_name_without_params.language_lookup_name (lang_e)
3615 };
3616
3617 /* Don't insert the same comparison routine more than once.
3618 Note that we do this linear walk. This is not a problem in
3619 practice because the number of supported languages is
3620 low. */
3621 if (std::find (matchers.begin (), matchers.end (), key)
3622 != matchers.end ())
3623 continue;
3624 matchers.push_back (std::move (key));
3625
3626 auto bounds
3627 = index.find_name_components_bounds (lookup_name_without_params,
3628 lang_e, per_objfile);
3629
3630 /* Now for each symbol name in range, check to see if we have a name
3631 match, and if so, call the MATCH_CALLBACK callback. */
3632
3633 for (; bounds.first != bounds.second; ++bounds.first)
3634 {
3635 const char *qualified
3636 = index.symbol_name_at (bounds.first->idx, per_objfile);
3637
3638 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3639 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3640 continue;
3641
3642 matches.push_back (bounds.first->idx);
3643 }
3644 }
3645
3646 std::sort (matches.begin (), matches.end ());
3647
3648 /* Finally call the callback, once per match. */
3649 ULONGEST prev = -1;
3650 bool result = true;
3651 for (offset_type idx : matches)
3652 {
3653 if (prev != idx)
3654 {
3655 if (!match_callback (idx))
3656 {
3657 result = false;
3658 break;
3659 }
3660 prev = idx;
3661 }
3662 }
3663
3664 /* Above we use a type wider than idx's for 'prev', since 0 and
3665 (offset_type)-1 are both possible values. */
3666 static_assert (sizeof (prev) > sizeof (offset_type), "");
3667
3668 return result;
3669 }
3670
3671 #if GDB_SELF_TEST
3672
3673 namespace selftests { namespace dw2_expand_symtabs_matching {
3674
3675 /* A mock .gdb_index/.debug_names-like name index table, enough to
3676 exercise dw2_expand_symtabs_matching_symbol, which works with the
3677 mapped_index_base interface. Builds an index from the symbol list
3678 passed as parameter to the constructor. */
3679 class mock_mapped_index : public mapped_index_base
3680 {
3681 public:
3682 mock_mapped_index (gdb::array_view<const char *> symbols)
3683 : m_symbol_table (symbols)
3684 {}
3685
3686 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3687
3688 /* Return the number of names in the symbol table. */
3689 size_t symbol_name_count () const override
3690 {
3691 return m_symbol_table.size ();
3692 }
3693
3694 /* Get the name of the symbol at IDX in the symbol table. */
3695 const char *symbol_name_at
3696 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3697 {
3698 return m_symbol_table[idx];
3699 }
3700
3701 private:
3702 gdb::array_view<const char *> m_symbol_table;
3703 };
3704
3705 /* Convenience function that converts a NULL pointer to a "<null>"
3706 string, to pass to print routines. */
3707
3708 static const char *
3709 string_or_null (const char *str)
3710 {
3711 return str != NULL ? str : "<null>";
3712 }
3713
3714 /* Check if a lookup_name_info built from
3715 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3716 index. EXPECTED_LIST is the list of expected matches, in expected
3717 matching order. If no match expected, then an empty list is
3718 specified. Returns true on success. On failure prints a warning
3719 indicating the file:line that failed, and returns false. */
3720
3721 static bool
3722 check_match (const char *file, int line,
3723 mock_mapped_index &mock_index,
3724 const char *name, symbol_name_match_type match_type,
3725 bool completion_mode,
3726 std::initializer_list<const char *> expected_list,
3727 dwarf2_per_objfile *per_objfile)
3728 {
3729 lookup_name_info lookup_name (name, match_type, completion_mode);
3730
3731 bool matched = true;
3732
3733 auto mismatch = [&] (const char *expected_str,
3734 const char *got)
3735 {
3736 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3737 "expected=\"%s\", got=\"%s\"\n"),
3738 file, line,
3739 (match_type == symbol_name_match_type::FULL
3740 ? "FULL" : "WILD"),
3741 name, string_or_null (expected_str), string_or_null (got));
3742 matched = false;
3743 };
3744
3745 auto expected_it = expected_list.begin ();
3746 auto expected_end = expected_list.end ();
3747
3748 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3749 nullptr,
3750 [&] (offset_type idx)
3751 {
3752 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3753 const char *expected_str
3754 = expected_it == expected_end ? NULL : *expected_it++;
3755
3756 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3757 mismatch (expected_str, matched_name);
3758 return true;
3759 }, per_objfile);
3760
3761 const char *expected_str
3762 = expected_it == expected_end ? NULL : *expected_it++;
3763 if (expected_str != NULL)
3764 mismatch (expected_str, NULL);
3765
3766 return matched;
3767 }
3768
3769 /* The symbols added to the mock mapped_index for testing (in
3770 canonical form). */
3771 static const char *test_symbols[] = {
3772 "function",
3773 "std::bar",
3774 "std::zfunction",
3775 "std::zfunction2",
3776 "w1::w2",
3777 "ns::foo<char*>",
3778 "ns::foo<int>",
3779 "ns::foo<long>",
3780 "ns2::tmpl<int>::foo2",
3781 "(anonymous namespace)::A::B::C",
3782
3783 /* These are used to check that the increment-last-char in the
3784 matching algorithm for completion doesn't match "t1_fund" when
3785 completing "t1_func". */
3786 "t1_func",
3787 "t1_func1",
3788 "t1_fund",
3789 "t1_fund1",
3790
3791 /* A UTF-8 name with multi-byte sequences to make sure that
3792 cp-name-parser understands this as a single identifier ("função"
3793 is "function" in PT). */
3794 u8"u8função",
3795
3796 /* \377 (0xff) is Latin1 'ÿ'. */
3797 "yfunc\377",
3798
3799 /* \377 (0xff) is Latin1 'ÿ'. */
3800 "\377",
3801 "\377\377123",
3802
3803 /* A name with all sorts of complications. Starts with "z" to make
3804 it easier for the completion tests below. */
3805 #define Z_SYM_NAME \
3806 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3807 "::tuple<(anonymous namespace)::ui*, " \
3808 "std::default_delete<(anonymous namespace)::ui>, void>"
3809
3810 Z_SYM_NAME
3811 };
3812
3813 /* Returns true if the mapped_index_base::find_name_component_bounds
3814 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3815 in completion mode. */
3816
3817 static bool
3818 check_find_bounds_finds (mapped_index_base &index,
3819 const char *search_name,
3820 gdb::array_view<const char *> expected_syms,
3821 dwarf2_per_objfile *per_objfile)
3822 {
3823 lookup_name_info lookup_name (search_name,
3824 symbol_name_match_type::FULL, true);
3825
3826 auto bounds = index.find_name_components_bounds (lookup_name,
3827 language_cplus,
3828 per_objfile);
3829
3830 size_t distance = std::distance (bounds.first, bounds.second);
3831 if (distance != expected_syms.size ())
3832 return false;
3833
3834 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3835 {
3836 auto nc_elem = bounds.first + exp_elem;
3837 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3838 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3839 return false;
3840 }
3841
3842 return true;
3843 }
3844
3845 /* Test the lower-level mapped_index::find_name_component_bounds
3846 method. */
3847
3848 static void
3849 test_mapped_index_find_name_component_bounds ()
3850 {
3851 mock_mapped_index mock_index (test_symbols);
3852
3853 mock_index.build_name_components (NULL /* per_objfile */);
3854
3855 /* Test the lower-level mapped_index::find_name_component_bounds
3856 method in completion mode. */
3857 {
3858 static const char *expected_syms[] = {
3859 "t1_func",
3860 "t1_func1",
3861 };
3862
3863 SELF_CHECK (check_find_bounds_finds
3864 (mock_index, "t1_func", expected_syms,
3865 NULL /* per_objfile */));
3866 }
3867
3868 /* Check that the increment-last-char in the name matching algorithm
3869 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3870 {
3871 static const char *expected_syms1[] = {
3872 "\377",
3873 "\377\377123",
3874 };
3875 SELF_CHECK (check_find_bounds_finds
3876 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3877
3878 static const char *expected_syms2[] = {
3879 "\377\377123",
3880 };
3881 SELF_CHECK (check_find_bounds_finds
3882 (mock_index, "\377\377", expected_syms2,
3883 NULL /* per_objfile */));
3884 }
3885 }
3886
3887 /* Test dw2_expand_symtabs_matching_symbol. */
3888
3889 static void
3890 test_dw2_expand_symtabs_matching_symbol ()
3891 {
3892 mock_mapped_index mock_index (test_symbols);
3893
3894 /* We let all tests run until the end even if some fails, for debug
3895 convenience. */
3896 bool any_mismatch = false;
3897
3898 /* Create the expected symbols list (an initializer_list). Needed
3899 because lists have commas, and we need to pass them to CHECK,
3900 which is a macro. */
3901 #define EXPECT(...) { __VA_ARGS__ }
3902
3903 /* Wrapper for check_match that passes down the current
3904 __FILE__/__LINE__. */
3905 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3906 any_mismatch |= !check_match (__FILE__, __LINE__, \
3907 mock_index, \
3908 NAME, MATCH_TYPE, COMPLETION_MODE, \
3909 EXPECTED_LIST, NULL)
3910
3911 /* Identity checks. */
3912 for (const char *sym : test_symbols)
3913 {
3914 /* Should be able to match all existing symbols. */
3915 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3916 EXPECT (sym));
3917
3918 /* Should be able to match all existing symbols with
3919 parameters. */
3920 std::string with_params = std::string (sym) + "(int)";
3921 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3922 EXPECT (sym));
3923
3924 /* Should be able to match all existing symbols with
3925 parameters and qualifiers. */
3926 with_params = std::string (sym) + " ( int ) const";
3927 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3928 EXPECT (sym));
3929
3930 /* This should really find sym, but cp-name-parser.y doesn't
3931 know about lvalue/rvalue qualifiers yet. */
3932 with_params = std::string (sym) + " ( int ) &&";
3933 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3934 {});
3935 }
3936
3937 /* Check that the name matching algorithm for completion doesn't get
3938 confused with Latin1 'ÿ' / 0xff. */
3939 {
3940 static const char str[] = "\377";
3941 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3942 EXPECT ("\377", "\377\377123"));
3943 }
3944
3945 /* Check that the increment-last-char in the matching algorithm for
3946 completion doesn't match "t1_fund" when completing "t1_func". */
3947 {
3948 static const char str[] = "t1_func";
3949 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3950 EXPECT ("t1_func", "t1_func1"));
3951 }
3952
3953 /* Check that completion mode works at each prefix of the expected
3954 symbol name. */
3955 {
3956 static const char str[] = "function(int)";
3957 size_t len = strlen (str);
3958 std::string lookup;
3959
3960 for (size_t i = 1; i < len; i++)
3961 {
3962 lookup.assign (str, i);
3963 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3964 EXPECT ("function"));
3965 }
3966 }
3967
3968 /* While "w" is a prefix of both components, the match function
3969 should still only be called once. */
3970 {
3971 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3972 EXPECT ("w1::w2"));
3973 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3974 EXPECT ("w1::w2"));
3975 }
3976
3977 /* Same, with a "complicated" symbol. */
3978 {
3979 static const char str[] = Z_SYM_NAME;
3980 size_t len = strlen (str);
3981 std::string lookup;
3982
3983 for (size_t i = 1; i < len; i++)
3984 {
3985 lookup.assign (str, i);
3986 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3987 EXPECT (Z_SYM_NAME));
3988 }
3989 }
3990
3991 /* In FULL mode, an incomplete symbol doesn't match. */
3992 {
3993 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3994 {});
3995 }
3996
3997 /* A complete symbol with parameters matches any overload, since the
3998 index has no overload info. */
3999 {
4000 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4001 EXPECT ("std::zfunction", "std::zfunction2"));
4002 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4003 EXPECT ("std::zfunction", "std::zfunction2"));
4004 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4005 EXPECT ("std::zfunction", "std::zfunction2"));
4006 }
4007
4008 /* Check that whitespace is ignored appropriately. A symbol with a
4009 template argument list. */
4010 {
4011 static const char expected[] = "ns::foo<int>";
4012 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4013 EXPECT (expected));
4014 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4015 EXPECT (expected));
4016 }
4017
4018 /* Check that whitespace is ignored appropriately. A symbol with a
4019 template argument list that includes a pointer. */
4020 {
4021 static const char expected[] = "ns::foo<char*>";
4022 /* Try both completion and non-completion modes. */
4023 static const bool completion_mode[2] = {false, true};
4024 for (size_t i = 0; i < 2; i++)
4025 {
4026 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4027 completion_mode[i], EXPECT (expected));
4028 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4029 completion_mode[i], EXPECT (expected));
4030
4031 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4032 completion_mode[i], EXPECT (expected));
4033 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4034 completion_mode[i], EXPECT (expected));
4035 }
4036 }
4037
4038 {
4039 /* Check method qualifiers are ignored. */
4040 static const char expected[] = "ns::foo<char*>";
4041 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4042 symbol_name_match_type::FULL, true, EXPECT (expected));
4043 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4044 symbol_name_match_type::FULL, true, EXPECT (expected));
4045 CHECK_MATCH ("foo < char * > ( int ) const",
4046 symbol_name_match_type::WILD, true, EXPECT (expected));
4047 CHECK_MATCH ("foo < char * > ( int ) &&",
4048 symbol_name_match_type::WILD, true, EXPECT (expected));
4049 }
4050
4051 /* Test lookup names that don't match anything. */
4052 {
4053 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4054 {});
4055
4056 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4057 {});
4058 }
4059
4060 /* Some wild matching tests, exercising "(anonymous namespace)",
4061 which should not be confused with a parameter list. */
4062 {
4063 static const char *syms[] = {
4064 "A::B::C",
4065 "B::C",
4066 "C",
4067 "A :: B :: C ( int )",
4068 "B :: C ( int )",
4069 "C ( int )",
4070 };
4071
4072 for (const char *s : syms)
4073 {
4074 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4075 EXPECT ("(anonymous namespace)::A::B::C"));
4076 }
4077 }
4078
4079 {
4080 static const char expected[] = "ns2::tmpl<int>::foo2";
4081 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4082 EXPECT (expected));
4083 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4084 EXPECT (expected));
4085 }
4086
4087 SELF_CHECK (!any_mismatch);
4088
4089 #undef EXPECT
4090 #undef CHECK_MATCH
4091 }
4092
4093 static void
4094 run_test ()
4095 {
4096 test_mapped_index_find_name_component_bounds ();
4097 test_dw2_expand_symtabs_matching_symbol ();
4098 }
4099
4100 }} // namespace selftests::dw2_expand_symtabs_matching
4101
4102 #endif /* GDB_SELF_TEST */
4103
4104 /* If FILE_MATCHER is NULL or if PER_CU has
4105 dwarf2_per_cu_quick_data::MARK set (see
4106 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4107 EXPANSION_NOTIFY on it. */
4108
4109 static bool
4110 dw2_expand_symtabs_matching_one
4111 (dwarf2_per_cu_data *per_cu,
4112 dwarf2_per_objfile *per_objfile,
4113 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4114 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4115 {
4116 if (file_matcher == NULL || per_cu->v.quick->mark)
4117 {
4118 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4119
4120 compunit_symtab *symtab
4121 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4122 gdb_assert (symtab != nullptr);
4123
4124 if (expansion_notify != NULL && symtab_was_null)
4125 return expansion_notify (symtab);
4126 }
4127 return true;
4128 }
4129
4130 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4131 matched, to expand corresponding CUs that were marked. IDX is the
4132 index of the symbol name that matched. */
4133
4134 static bool
4135 dw2_expand_marked_cus
4136 (dwarf2_per_objfile *per_objfile, offset_type idx,
4137 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4138 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4139 block_search_flags search_flags,
4140 search_domain kind)
4141 {
4142 offset_type vec_len, vec_idx;
4143 bool global_seen = false;
4144 mapped_index &index = *per_objfile->per_bfd->index_table;
4145
4146 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4147 vec_len = vec[0];
4148 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4149 {
4150 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4151 /* This value is only valid for index versions >= 7. */
4152 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4153 gdb_index_symbol_kind symbol_kind =
4154 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4155 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4156 /* Only check the symbol attributes if they're present.
4157 Indices prior to version 7 don't record them,
4158 and indices >= 7 may elide them for certain symbols
4159 (gold does this). */
4160 int attrs_valid =
4161 (index.version >= 7
4162 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4163
4164 /* Work around gold/15646. */
4165 if (attrs_valid
4166 && !is_static
4167 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4168 {
4169 if (global_seen)
4170 continue;
4171
4172 global_seen = true;
4173 }
4174
4175 /* Only check the symbol's kind if it has one. */
4176 if (attrs_valid)
4177 {
4178 if (is_static)
4179 {
4180 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4181 continue;
4182 }
4183 else
4184 {
4185 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4186 continue;
4187 }
4188
4189 switch (kind)
4190 {
4191 case VARIABLES_DOMAIN:
4192 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4193 continue;
4194 break;
4195 case FUNCTIONS_DOMAIN:
4196 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4197 continue;
4198 break;
4199 case TYPES_DOMAIN:
4200 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4201 continue;
4202 break;
4203 case MODULES_DOMAIN:
4204 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4205 continue;
4206 break;
4207 default:
4208 break;
4209 }
4210 }
4211
4212 /* Don't crash on bad data. */
4213 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4214 {
4215 complaint (_(".gdb_index entry has bad CU index"
4216 " [in module %s]"), objfile_name (per_objfile->objfile));
4217 continue;
4218 }
4219
4220 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4221 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4222 expansion_notify))
4223 return false;
4224 }
4225
4226 return true;
4227 }
4228
4229 /* If FILE_MATCHER is non-NULL, set all the
4230 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4231 that match FILE_MATCHER. */
4232
4233 static void
4234 dw_expand_symtabs_matching_file_matcher
4235 (dwarf2_per_objfile *per_objfile,
4236 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4237 {
4238 if (file_matcher == NULL)
4239 return;
4240
4241 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4242 htab_eq_pointer,
4243 NULL, xcalloc, xfree));
4244 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4245 htab_eq_pointer,
4246 NULL, xcalloc, xfree));
4247
4248 /* The rule is CUs specify all the files, including those used by
4249 any TU, so there's no need to scan TUs here. */
4250
4251 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4252 {
4253 QUIT;
4254
4255 if (per_cu->is_debug_types)
4256 continue;
4257 per_cu->v.quick->mark = 0;
4258
4259 /* We only need to look at symtabs not already expanded. */
4260 if (per_objfile->symtab_set_p (per_cu.get ()))
4261 continue;
4262
4263 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4264 per_objfile);
4265 if (file_data == NULL)
4266 continue;
4267
4268 if (htab_find (visited_not_found.get (), file_data) != NULL)
4269 continue;
4270 else if (htab_find (visited_found.get (), file_data) != NULL)
4271 {
4272 per_cu->v.quick->mark = 1;
4273 continue;
4274 }
4275
4276 for (int j = 0; j < file_data->num_file_names; ++j)
4277 {
4278 const char *this_real_name;
4279
4280 if (file_matcher (file_data->file_names[j], false))
4281 {
4282 per_cu->v.quick->mark = 1;
4283 break;
4284 }
4285
4286 /* Before we invoke realpath, which can get expensive when many
4287 files are involved, do a quick comparison of the basenames. */
4288 if (!basenames_may_differ
4289 && !file_matcher (lbasename (file_data->file_names[j]),
4290 true))
4291 continue;
4292
4293 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4294 if (file_matcher (this_real_name, false))
4295 {
4296 per_cu->v.quick->mark = 1;
4297 break;
4298 }
4299 }
4300
4301 void **slot = htab_find_slot (per_cu->v.quick->mark
4302 ? visited_found.get ()
4303 : visited_not_found.get (),
4304 file_data, INSERT);
4305 *slot = file_data;
4306 }
4307 }
4308
4309 bool
4310 dwarf2_gdb_index::expand_symtabs_matching
4311 (struct objfile *objfile,
4312 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4313 const lookup_name_info *lookup_name,
4314 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4315 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4316 block_search_flags search_flags,
4317 domain_enum domain,
4318 enum search_domain kind)
4319 {
4320 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4321
4322 /* index_table is NULL if OBJF_READNOW. */
4323 if (!per_objfile->per_bfd->index_table)
4324 return true;
4325
4326 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4327
4328 if (symbol_matcher == NULL && lookup_name == NULL)
4329 {
4330 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4331 {
4332 QUIT;
4333
4334 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
4335 file_matcher,
4336 expansion_notify))
4337 return false;
4338 }
4339 return true;
4340 }
4341
4342 mapped_index &index = *per_objfile->per_bfd->index_table;
4343
4344 bool result
4345 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4346 symbol_matcher,
4347 [&] (offset_type idx)
4348 {
4349 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4350 expansion_notify, search_flags, kind))
4351 return false;
4352 return true;
4353 }, per_objfile);
4354
4355 return result;
4356 }
4357
4358 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4359 symtab. */
4360
4361 static struct compunit_symtab *
4362 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4363 CORE_ADDR pc)
4364 {
4365 int i;
4366
4367 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4368 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4369 return cust;
4370
4371 if (cust->includes == NULL)
4372 return NULL;
4373
4374 for (i = 0; cust->includes[i]; ++i)
4375 {
4376 struct compunit_symtab *s = cust->includes[i];
4377
4378 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4379 if (s != NULL)
4380 return s;
4381 }
4382
4383 return NULL;
4384 }
4385
4386 struct compunit_symtab *
4387 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4388 (struct objfile *objfile,
4389 struct bound_minimal_symbol msymbol,
4390 CORE_ADDR pc,
4391 struct obj_section *section,
4392 int warn_if_readin)
4393 {
4394 struct dwarf2_per_cu_data *data;
4395 struct compunit_symtab *result;
4396
4397 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4398 if (per_objfile->per_bfd->index_addrmap == nullptr)
4399 return NULL;
4400
4401 CORE_ADDR baseaddr = objfile->text_section_offset ();
4402 data = ((struct dwarf2_per_cu_data *)
4403 addrmap_find (per_objfile->per_bfd->index_addrmap,
4404 pc - baseaddr));
4405 if (!data)
4406 return NULL;
4407
4408 if (warn_if_readin && per_objfile->symtab_set_p (data))
4409 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4410 paddress (objfile->arch (), pc));
4411
4412 result = recursively_find_pc_sect_compunit_symtab
4413 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4414
4415 gdb_assert (result != NULL);
4416 return result;
4417 }
4418
4419 void
4420 dwarf2_base_index_functions::map_symbol_filenames
4421 (struct objfile *objfile,
4422 gdb::function_view<symbol_filename_ftype> fun,
4423 bool need_fullname)
4424 {
4425 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4426
4427 /* Use caches to ensure we only call FUN once for each filename. */
4428 filename_seen_cache filenames_cache;
4429 std::unordered_set<quick_file_names *> qfn_cache;
4430
4431 /* The rule is CUs specify all the files, including those used by any TU,
4432 so there's no need to scan TUs here. We can ignore file names coming
4433 from already-expanded CUs. It is possible that an expanded CU might
4434 reuse the file names data from a currently unexpanded CU, in this
4435 case we don't want to report the files from the unexpanded CU. */
4436
4437 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4438 {
4439 if (per_objfile->symtab_set_p (per_cu.get ()))
4440 {
4441 if (per_cu->v.quick->file_names != nullptr)
4442 qfn_cache.insert (per_cu->v.quick->file_names);
4443 }
4444 }
4445
4446 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4447 {
4448 /* We only need to look at symtabs not already expanded. */
4449 if (per_objfile->symtab_set_p (per_cu.get ()))
4450 continue;
4451
4452 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4453 per_objfile);
4454 if (file_data == nullptr
4455 || qfn_cache.find (file_data) != qfn_cache.end ())
4456 continue;
4457
4458 for (int j = 0; j < file_data->num_file_names; ++j)
4459 {
4460 const char *filename = file_data->file_names[j];
4461 filenames_cache.seen (filename);
4462 }
4463 }
4464
4465 filenames_cache.traverse ([&] (const char *filename)
4466 {
4467 gdb::unique_xmalloc_ptr<char> this_real_name;
4468
4469 if (need_fullname)
4470 this_real_name = gdb_realpath (filename);
4471 fun (filename, this_real_name.get ());
4472 });
4473 }
4474
4475 bool
4476 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4477 {
4478 return true;
4479 }
4480
4481 /* DWARF-5 debug_names reader. */
4482
4483 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4484 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4485
4486 /* A helper function that reads the .debug_names section in SECTION
4487 and fills in MAP. FILENAME is the name of the file containing the
4488 section; it is used for error reporting.
4489
4490 Returns true if all went well, false otherwise. */
4491
4492 static bool
4493 read_debug_names_from_section (struct objfile *objfile,
4494 const char *filename,
4495 struct dwarf2_section_info *section,
4496 mapped_debug_names &map)
4497 {
4498 if (section->empty ())
4499 return false;
4500
4501 /* Older elfutils strip versions could keep the section in the main
4502 executable while splitting it for the separate debug info file. */
4503 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4504 return false;
4505
4506 section->read (objfile);
4507
4508 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4509
4510 const gdb_byte *addr = section->buffer;
4511
4512 bfd *const abfd = section->get_bfd_owner ();
4513
4514 unsigned int bytes_read;
4515 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4516 addr += bytes_read;
4517
4518 map.dwarf5_is_dwarf64 = bytes_read != 4;
4519 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4520 if (bytes_read + length != section->size)
4521 {
4522 /* There may be multiple per-CU indices. */
4523 warning (_("Section .debug_names in %s length %s does not match "
4524 "section length %s, ignoring .debug_names."),
4525 filename, plongest (bytes_read + length),
4526 pulongest (section->size));
4527 return false;
4528 }
4529
4530 /* The version number. */
4531 uint16_t version = read_2_bytes (abfd, addr);
4532 addr += 2;
4533 if (version != 5)
4534 {
4535 warning (_("Section .debug_names in %s has unsupported version %d, "
4536 "ignoring .debug_names."),
4537 filename, version);
4538 return false;
4539 }
4540
4541 /* Padding. */
4542 uint16_t padding = read_2_bytes (abfd, addr);
4543 addr += 2;
4544 if (padding != 0)
4545 {
4546 warning (_("Section .debug_names in %s has unsupported padding %d, "
4547 "ignoring .debug_names."),
4548 filename, padding);
4549 return false;
4550 }
4551
4552 /* comp_unit_count - The number of CUs in the CU list. */
4553 map.cu_count = read_4_bytes (abfd, addr);
4554 addr += 4;
4555
4556 /* local_type_unit_count - The number of TUs in the local TU
4557 list. */
4558 map.tu_count = read_4_bytes (abfd, addr);
4559 addr += 4;
4560
4561 /* foreign_type_unit_count - The number of TUs in the foreign TU
4562 list. */
4563 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4564 addr += 4;
4565 if (foreign_tu_count != 0)
4566 {
4567 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4568 "ignoring .debug_names."),
4569 filename, static_cast<unsigned long> (foreign_tu_count));
4570 return false;
4571 }
4572
4573 /* bucket_count - The number of hash buckets in the hash lookup
4574 table. */
4575 map.bucket_count = read_4_bytes (abfd, addr);
4576 addr += 4;
4577
4578 /* name_count - The number of unique names in the index. */
4579 map.name_count = read_4_bytes (abfd, addr);
4580 addr += 4;
4581
4582 /* abbrev_table_size - The size in bytes of the abbreviations
4583 table. */
4584 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4585 addr += 4;
4586
4587 /* augmentation_string_size - The size in bytes of the augmentation
4588 string. This value is rounded up to a multiple of 4. */
4589 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4590 addr += 4;
4591 map.augmentation_is_gdb = ((augmentation_string_size
4592 == sizeof (dwarf5_augmentation))
4593 && memcmp (addr, dwarf5_augmentation,
4594 sizeof (dwarf5_augmentation)) == 0);
4595 augmentation_string_size += (-augmentation_string_size) & 3;
4596 addr += augmentation_string_size;
4597
4598 /* List of CUs */
4599 map.cu_table_reordered = addr;
4600 addr += map.cu_count * map.offset_size;
4601
4602 /* List of Local TUs */
4603 map.tu_table_reordered = addr;
4604 addr += map.tu_count * map.offset_size;
4605
4606 /* Hash Lookup Table */
4607 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4608 addr += map.bucket_count * 4;
4609 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4610 addr += map.name_count * 4;
4611
4612 /* Name Table */
4613 map.name_table_string_offs_reordered = addr;
4614 addr += map.name_count * map.offset_size;
4615 map.name_table_entry_offs_reordered = addr;
4616 addr += map.name_count * map.offset_size;
4617
4618 const gdb_byte *abbrev_table_start = addr;
4619 for (;;)
4620 {
4621 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4622 addr += bytes_read;
4623 if (index_num == 0)
4624 break;
4625
4626 const auto insertpair
4627 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4628 if (!insertpair.second)
4629 {
4630 warning (_("Section .debug_names in %s has duplicate index %s, "
4631 "ignoring .debug_names."),
4632 filename, pulongest (index_num));
4633 return false;
4634 }
4635 mapped_debug_names::index_val &indexval = insertpair.first->second;
4636 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4637 addr += bytes_read;
4638
4639 for (;;)
4640 {
4641 mapped_debug_names::index_val::attr attr;
4642 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4643 addr += bytes_read;
4644 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4645 addr += bytes_read;
4646 if (attr.form == DW_FORM_implicit_const)
4647 {
4648 attr.implicit_const = read_signed_leb128 (abfd, addr,
4649 &bytes_read);
4650 addr += bytes_read;
4651 }
4652 if (attr.dw_idx == 0 && attr.form == 0)
4653 break;
4654 indexval.attr_vec.push_back (std::move (attr));
4655 }
4656 }
4657 if (addr != abbrev_table_start + abbrev_table_size)
4658 {
4659 warning (_("Section .debug_names in %s has abbreviation_table "
4660 "of size %s vs. written as %u, ignoring .debug_names."),
4661 filename, plongest (addr - abbrev_table_start),
4662 abbrev_table_size);
4663 return false;
4664 }
4665 map.entry_pool = addr;
4666
4667 return true;
4668 }
4669
4670 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4671 list. */
4672
4673 static void
4674 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4675 const mapped_debug_names &map,
4676 dwarf2_section_info &section,
4677 bool is_dwz)
4678 {
4679 if (!map.augmentation_is_gdb)
4680 {
4681 for (uint32_t i = 0; i < map.cu_count; ++i)
4682 {
4683 sect_offset sect_off
4684 = (sect_offset) (extract_unsigned_integer
4685 (map.cu_table_reordered + i * map.offset_size,
4686 map.offset_size,
4687 map.dwarf5_byte_order));
4688 /* We don't know the length of the CU, because the CU list in a
4689 .debug_names index can be incomplete, so we can't use the start
4690 of the next CU as end of this CU. We create the CUs here with
4691 length 0, and in cutu_reader::cutu_reader we'll fill in the
4692 actual length. */
4693 dwarf2_per_cu_data_up per_cu
4694 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4695 sect_off, 0);
4696 per_bfd->all_comp_units.push_back (std::move (per_cu));
4697 }
4698 return;
4699 }
4700
4701 sect_offset sect_off_prev;
4702 for (uint32_t i = 0; i <= map.cu_count; ++i)
4703 {
4704 sect_offset sect_off_next;
4705 if (i < map.cu_count)
4706 {
4707 sect_off_next
4708 = (sect_offset) (extract_unsigned_integer
4709 (map.cu_table_reordered + i * map.offset_size,
4710 map.offset_size,
4711 map.dwarf5_byte_order));
4712 }
4713 else
4714 sect_off_next = (sect_offset) section.size;
4715 if (i >= 1)
4716 {
4717 const ULONGEST length = sect_off_next - sect_off_prev;
4718 dwarf2_per_cu_data_up per_cu
4719 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4720 sect_off_prev, length);
4721 per_bfd->all_comp_units.push_back (std::move (per_cu));
4722 }
4723 sect_off_prev = sect_off_next;
4724 }
4725 }
4726
4727 /* Read the CU list from the mapped index, and use it to create all
4728 the CU objects for this dwarf2_per_objfile. */
4729
4730 static void
4731 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4732 const mapped_debug_names &map,
4733 const mapped_debug_names &dwz_map)
4734 {
4735 gdb_assert (per_bfd->all_comp_units.empty ());
4736 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4737
4738 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4739 false /* is_dwz */);
4740
4741 if (dwz_map.cu_count == 0)
4742 return;
4743
4744 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4745 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4746 true /* is_dwz */);
4747 }
4748
4749 /* Read .debug_names. If everything went ok, initialize the "quick"
4750 elements of all the CUs and return true. Otherwise, return false. */
4751
4752 static bool
4753 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4754 {
4755 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4756 mapped_debug_names dwz_map;
4757 struct objfile *objfile = per_objfile->objfile;
4758 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4759
4760 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4761 &per_bfd->debug_names, *map))
4762 return false;
4763
4764 /* Don't use the index if it's empty. */
4765 if (map->name_count == 0)
4766 return false;
4767
4768 /* If there is a .dwz file, read it so we can get its CU list as
4769 well. */
4770 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4771 if (dwz != NULL)
4772 {
4773 if (!read_debug_names_from_section (objfile,
4774 bfd_get_filename (dwz->dwz_bfd.get ()),
4775 &dwz->debug_names, dwz_map))
4776 {
4777 warning (_("could not read '.debug_names' section from %s; skipping"),
4778 bfd_get_filename (dwz->dwz_bfd.get ()));
4779 return false;
4780 }
4781 }
4782
4783 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4784
4785 if (map->tu_count != 0)
4786 {
4787 /* We can only handle a single .debug_types when we have an
4788 index. */
4789 if (per_bfd->types.size () != 1)
4790 return false;
4791
4792 dwarf2_section_info *section = &per_bfd->types[0];
4793
4794 create_signatured_type_table_from_debug_names
4795 (per_objfile, *map, section, &per_bfd->abbrev);
4796 }
4797
4798 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4799
4800 per_bfd->debug_names_table = std::move (map);
4801 per_bfd->using_index = 1;
4802 per_bfd->quick_file_names_table =
4803 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4804
4805 return true;
4806 }
4807
4808 /* Type used to manage iterating over all CUs looking for a symbol for
4809 .debug_names. */
4810
4811 class dw2_debug_names_iterator
4812 {
4813 public:
4814 dw2_debug_names_iterator (const mapped_debug_names &map,
4815 block_search_flags block_index,
4816 domain_enum domain,
4817 const char *name, dwarf2_per_objfile *per_objfile)
4818 : m_map (map), m_block_index (block_index), m_domain (domain),
4819 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4820 m_per_objfile (per_objfile)
4821 {}
4822
4823 dw2_debug_names_iterator (const mapped_debug_names &map,
4824 search_domain search, uint32_t namei,
4825 dwarf2_per_objfile *per_objfile,
4826 domain_enum domain = UNDEF_DOMAIN)
4827 : m_map (map),
4828 m_domain (domain),
4829 m_search (search),
4830 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4831 m_per_objfile (per_objfile)
4832 {}
4833
4834 dw2_debug_names_iterator (const mapped_debug_names &map,
4835 block_search_flags block_index, domain_enum domain,
4836 uint32_t namei, dwarf2_per_objfile *per_objfile)
4837 : m_map (map), m_block_index (block_index), m_domain (domain),
4838 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4839 m_per_objfile (per_objfile)
4840 {}
4841
4842 /* Return the next matching CU or NULL if there are no more. */
4843 dwarf2_per_cu_data *next ();
4844
4845 private:
4846 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4847 const char *name,
4848 dwarf2_per_objfile *per_objfile);
4849 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4850 uint32_t namei,
4851 dwarf2_per_objfile *per_objfile);
4852
4853 /* The internalized form of .debug_names. */
4854 const mapped_debug_names &m_map;
4855
4856 /* Restrict the search to these blocks. */
4857 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4858 | SEARCH_STATIC_BLOCK);
4859
4860 /* The kind of symbol we're looking for. */
4861 const domain_enum m_domain = UNDEF_DOMAIN;
4862 const search_domain m_search = ALL_DOMAIN;
4863
4864 /* The list of CUs from the index entry of the symbol, or NULL if
4865 not found. */
4866 const gdb_byte *m_addr;
4867
4868 dwarf2_per_objfile *m_per_objfile;
4869 };
4870
4871 const char *
4872 mapped_debug_names::namei_to_name
4873 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4874 {
4875 const ULONGEST namei_string_offs
4876 = extract_unsigned_integer ((name_table_string_offs_reordered
4877 + namei * offset_size),
4878 offset_size,
4879 dwarf5_byte_order);
4880 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4881 }
4882
4883 /* Find a slot in .debug_names for the object named NAME. If NAME is
4884 found, return pointer to its pool data. If NAME cannot be found,
4885 return NULL. */
4886
4887 const gdb_byte *
4888 dw2_debug_names_iterator::find_vec_in_debug_names
4889 (const mapped_debug_names &map, const char *name,
4890 dwarf2_per_objfile *per_objfile)
4891 {
4892 int (*cmp) (const char *, const char *);
4893
4894 gdb::unique_xmalloc_ptr<char> without_params;
4895 if (current_language->la_language == language_cplus
4896 || current_language->la_language == language_fortran
4897 || current_language->la_language == language_d)
4898 {
4899 /* NAME is already canonical. Drop any qualifiers as
4900 .debug_names does not contain any. */
4901
4902 if (strchr (name, '(') != NULL)
4903 {
4904 without_params = cp_remove_params (name);
4905 if (without_params != NULL)
4906 name = without_params.get ();
4907 }
4908 }
4909
4910 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4911
4912 const uint32_t full_hash = dwarf5_djb_hash (name);
4913 uint32_t namei
4914 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4915 (map.bucket_table_reordered
4916 + (full_hash % map.bucket_count)), 4,
4917 map.dwarf5_byte_order);
4918 if (namei == 0)
4919 return NULL;
4920 --namei;
4921 if (namei >= map.name_count)
4922 {
4923 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4924 "[in module %s]"),
4925 namei, map.name_count,
4926 objfile_name (per_objfile->objfile));
4927 return NULL;
4928 }
4929
4930 for (;;)
4931 {
4932 const uint32_t namei_full_hash
4933 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4934 (map.hash_table_reordered + namei), 4,
4935 map.dwarf5_byte_order);
4936 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4937 return NULL;
4938
4939 if (full_hash == namei_full_hash)
4940 {
4941 const char *const namei_string = map.namei_to_name (namei, per_objfile);
4942
4943 #if 0 /* An expensive sanity check. */
4944 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4945 {
4946 complaint (_("Wrong .debug_names hash for string at index %u "
4947 "[in module %s]"),
4948 namei, objfile_name (dwarf2_per_objfile->objfile));
4949 return NULL;
4950 }
4951 #endif
4952
4953 if (cmp (namei_string, name) == 0)
4954 {
4955 const ULONGEST namei_entry_offs
4956 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4957 + namei * map.offset_size),
4958 map.offset_size, map.dwarf5_byte_order);
4959 return map.entry_pool + namei_entry_offs;
4960 }
4961 }
4962
4963 ++namei;
4964 if (namei >= map.name_count)
4965 return NULL;
4966 }
4967 }
4968
4969 const gdb_byte *
4970 dw2_debug_names_iterator::find_vec_in_debug_names
4971 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
4972 {
4973 if (namei >= map.name_count)
4974 {
4975 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4976 "[in module %s]"),
4977 namei, map.name_count,
4978 objfile_name (per_objfile->objfile));
4979 return NULL;
4980 }
4981
4982 const ULONGEST namei_entry_offs
4983 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4984 + namei * map.offset_size),
4985 map.offset_size, map.dwarf5_byte_order);
4986 return map.entry_pool + namei_entry_offs;
4987 }
4988
4989 /* See dw2_debug_names_iterator. */
4990
4991 dwarf2_per_cu_data *
4992 dw2_debug_names_iterator::next ()
4993 {
4994 if (m_addr == NULL)
4995 return NULL;
4996
4997 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4998 struct objfile *objfile = m_per_objfile->objfile;
4999 bfd *const abfd = objfile->obfd;
5000
5001 again:
5002
5003 unsigned int bytes_read;
5004 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5005 m_addr += bytes_read;
5006 if (abbrev == 0)
5007 return NULL;
5008
5009 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5010 if (indexval_it == m_map.abbrev_map.cend ())
5011 {
5012 complaint (_("Wrong .debug_names undefined abbrev code %s "
5013 "[in module %s]"),
5014 pulongest (abbrev), objfile_name (objfile));
5015 return NULL;
5016 }
5017 const mapped_debug_names::index_val &indexval = indexval_it->second;
5018 enum class symbol_linkage {
5019 unknown,
5020 static_,
5021 extern_,
5022 } symbol_linkage_ = symbol_linkage::unknown;
5023 dwarf2_per_cu_data *per_cu = NULL;
5024 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5025 {
5026 ULONGEST ull;
5027 switch (attr.form)
5028 {
5029 case DW_FORM_implicit_const:
5030 ull = attr.implicit_const;
5031 break;
5032 case DW_FORM_flag_present:
5033 ull = 1;
5034 break;
5035 case DW_FORM_udata:
5036 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5037 m_addr += bytes_read;
5038 break;
5039 case DW_FORM_ref4:
5040 ull = read_4_bytes (abfd, m_addr);
5041 m_addr += 4;
5042 break;
5043 case DW_FORM_ref8:
5044 ull = read_8_bytes (abfd, m_addr);
5045 m_addr += 8;
5046 break;
5047 case DW_FORM_ref_sig8:
5048 ull = read_8_bytes (abfd, m_addr);
5049 m_addr += 8;
5050 break;
5051 default:
5052 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5053 dwarf_form_name (attr.form),
5054 objfile_name (objfile));
5055 return NULL;
5056 }
5057 switch (attr.dw_idx)
5058 {
5059 case DW_IDX_compile_unit:
5060 /* Don't crash on bad data. */
5061 if (ull >= per_bfd->all_comp_units.size ())
5062 {
5063 complaint (_(".debug_names entry has bad CU index %s"
5064 " [in module %s]"),
5065 pulongest (ull),
5066 objfile_name (objfile));
5067 continue;
5068 }
5069 per_cu = per_bfd->get_cu (ull);
5070 break;
5071 case DW_IDX_type_unit:
5072 /* Don't crash on bad data. */
5073 if (ull >= per_bfd->tu_stats.nr_tus)
5074 {
5075 complaint (_(".debug_names entry has bad TU index %s"
5076 " [in module %s]"),
5077 pulongest (ull),
5078 objfile_name (objfile));
5079 continue;
5080 }
5081 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5082 break;
5083 case DW_IDX_die_offset:
5084 /* In a per-CU index (as opposed to a per-module index), index
5085 entries without CU attribute implicitly refer to the single CU. */
5086 if (per_cu == NULL)
5087 per_cu = per_bfd->get_cu (0);
5088 break;
5089 case DW_IDX_GNU_internal:
5090 if (!m_map.augmentation_is_gdb)
5091 break;
5092 symbol_linkage_ = symbol_linkage::static_;
5093 break;
5094 case DW_IDX_GNU_external:
5095 if (!m_map.augmentation_is_gdb)
5096 break;
5097 symbol_linkage_ = symbol_linkage::extern_;
5098 break;
5099 }
5100 }
5101
5102 /* Skip if already read in. */
5103 if (m_per_objfile->symtab_set_p (per_cu))
5104 goto again;
5105
5106 /* Check static vs global. */
5107 if (symbol_linkage_ != symbol_linkage::unknown)
5108 {
5109 if (symbol_linkage_ == symbol_linkage::static_)
5110 {
5111 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5112 goto again;
5113 }
5114 else
5115 {
5116 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5117 goto again;
5118 }
5119 }
5120
5121 /* Match dw2_symtab_iter_next, symbol_kind
5122 and debug_names::psymbol_tag. */
5123 switch (m_domain)
5124 {
5125 case VAR_DOMAIN:
5126 switch (indexval.dwarf_tag)
5127 {
5128 case DW_TAG_variable:
5129 case DW_TAG_subprogram:
5130 /* Some types are also in VAR_DOMAIN. */
5131 case DW_TAG_typedef:
5132 case DW_TAG_structure_type:
5133 break;
5134 default:
5135 goto again;
5136 }
5137 break;
5138 case STRUCT_DOMAIN:
5139 switch (indexval.dwarf_tag)
5140 {
5141 case DW_TAG_typedef:
5142 case DW_TAG_structure_type:
5143 break;
5144 default:
5145 goto again;
5146 }
5147 break;
5148 case LABEL_DOMAIN:
5149 switch (indexval.dwarf_tag)
5150 {
5151 case 0:
5152 case DW_TAG_variable:
5153 break;
5154 default:
5155 goto again;
5156 }
5157 break;
5158 case MODULE_DOMAIN:
5159 switch (indexval.dwarf_tag)
5160 {
5161 case DW_TAG_module:
5162 break;
5163 default:
5164 goto again;
5165 }
5166 break;
5167 default:
5168 break;
5169 }
5170
5171 /* Match dw2_expand_symtabs_matching, symbol_kind and
5172 debug_names::psymbol_tag. */
5173 switch (m_search)
5174 {
5175 case VARIABLES_DOMAIN:
5176 switch (indexval.dwarf_tag)
5177 {
5178 case DW_TAG_variable:
5179 break;
5180 default:
5181 goto again;
5182 }
5183 break;
5184 case FUNCTIONS_DOMAIN:
5185 switch (indexval.dwarf_tag)
5186 {
5187 case DW_TAG_subprogram:
5188 break;
5189 default:
5190 goto again;
5191 }
5192 break;
5193 case TYPES_DOMAIN:
5194 switch (indexval.dwarf_tag)
5195 {
5196 case DW_TAG_typedef:
5197 case DW_TAG_structure_type:
5198 break;
5199 default:
5200 goto again;
5201 }
5202 break;
5203 case MODULES_DOMAIN:
5204 switch (indexval.dwarf_tag)
5205 {
5206 case DW_TAG_module:
5207 break;
5208 default:
5209 goto again;
5210 }
5211 default:
5212 break;
5213 }
5214
5215 return per_cu;
5216 }
5217
5218 /* This dumps minimal information about .debug_names. It is called
5219 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5220 uses this to verify that .debug_names has been loaded. */
5221
5222 void
5223 dwarf2_debug_names_index::dump (struct objfile *objfile)
5224 {
5225 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5226
5227 gdb_assert (per_objfile->per_bfd->using_index);
5228 printf_filtered (".debug_names:");
5229 if (per_objfile->per_bfd->debug_names_table)
5230 printf_filtered (" exists\n");
5231 else
5232 printf_filtered (" faked for \"readnow\"\n");
5233 printf_filtered ("\n");
5234 }
5235
5236 void
5237 dwarf2_debug_names_index::expand_matching_symbols
5238 (struct objfile *objfile,
5239 const lookup_name_info &name, domain_enum domain,
5240 int global,
5241 symbol_compare_ftype *ordered_compare)
5242 {
5243 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5244
5245 /* debug_names_table is NULL if OBJF_READNOW. */
5246 if (!per_objfile->per_bfd->debug_names_table)
5247 return;
5248
5249 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5250 const block_search_flags block_flags
5251 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5252
5253 const char *match_name = name.ada ().lookup_name ().c_str ();
5254 auto matcher = [&] (const char *symname)
5255 {
5256 if (ordered_compare == nullptr)
5257 return true;
5258 return ordered_compare (symname, match_name) == 0;
5259 };
5260
5261 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5262 [&] (offset_type namei)
5263 {
5264 /* The name was matched, now expand corresponding CUs that were
5265 marked. */
5266 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5267 per_objfile);
5268
5269 struct dwarf2_per_cu_data *per_cu;
5270 while ((per_cu = iter.next ()) != NULL)
5271 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5272 nullptr);
5273 return true;
5274 }, per_objfile);
5275 }
5276
5277 bool
5278 dwarf2_debug_names_index::expand_symtabs_matching
5279 (struct objfile *objfile,
5280 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5281 const lookup_name_info *lookup_name,
5282 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5283 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5284 block_search_flags search_flags,
5285 domain_enum domain,
5286 enum search_domain kind)
5287 {
5288 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5289
5290 /* debug_names_table is NULL if OBJF_READNOW. */
5291 if (!per_objfile->per_bfd->debug_names_table)
5292 return true;
5293
5294 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5295
5296 if (symbol_matcher == NULL && lookup_name == NULL)
5297 {
5298 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
5299 {
5300 QUIT;
5301
5302 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
5303 file_matcher,
5304 expansion_notify))
5305 return false;
5306 }
5307 return true;
5308 }
5309
5310 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5311
5312 bool result
5313 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5314 symbol_matcher,
5315 [&] (offset_type namei)
5316 {
5317 /* The name was matched, now expand corresponding CUs that were
5318 marked. */
5319 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5320
5321 struct dwarf2_per_cu_data *per_cu;
5322 while ((per_cu = iter.next ()) != NULL)
5323 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5324 file_matcher,
5325 expansion_notify))
5326 return false;
5327 return true;
5328 }, per_objfile);
5329
5330 return result;
5331 }
5332
5333 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5334 to either a dwarf2_per_bfd or dwz_file object. */
5335
5336 template <typename T>
5337 static gdb::array_view<const gdb_byte>
5338 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5339 {
5340 dwarf2_section_info *section = &section_owner->gdb_index;
5341
5342 if (section->empty ())
5343 return {};
5344
5345 /* Older elfutils strip versions could keep the section in the main
5346 executable while splitting it for the separate debug info file. */
5347 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5348 return {};
5349
5350 section->read (obj);
5351
5352 /* dwarf2_section_info::size is a bfd_size_type, while
5353 gdb::array_view works with size_t. On 32-bit hosts, with
5354 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5355 is 32-bit. So we need an explicit narrowing conversion here.
5356 This is fine, because it's impossible to allocate or mmap an
5357 array/buffer larger than what size_t can represent. */
5358 return gdb::make_array_view (section->buffer, section->size);
5359 }
5360
5361 /* Lookup the index cache for the contents of the index associated to
5362 DWARF2_OBJ. */
5363
5364 static gdb::array_view<const gdb_byte>
5365 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5366 {
5367 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5368 if (build_id == nullptr)
5369 return {};
5370
5371 return global_index_cache.lookup_gdb_index (build_id,
5372 &dwarf2_per_bfd->index_cache_res);
5373 }
5374
5375 /* Same as the above, but for DWZ. */
5376
5377 static gdb::array_view<const gdb_byte>
5378 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5379 {
5380 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5381 if (build_id == nullptr)
5382 return {};
5383
5384 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5385 }
5386
5387 /* See dwarf2/public.h. */
5388
5389 void
5390 dwarf2_initialize_objfile (struct objfile *objfile)
5391 {
5392 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5393 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5394
5395 dwarf_read_debug_printf ("called");
5396
5397 /* If we're about to read full symbols, don't bother with the
5398 indices. In this case we also don't care if some other debug
5399 format is making psymtabs, because they are all about to be
5400 expanded anyway. */
5401 if ((objfile->flags & OBJF_READNOW))
5402 {
5403 dwarf_read_debug_printf ("readnow requested");
5404
5405 /* When using READNOW, the using_index flag (set below) indicates that
5406 PER_BFD was already initialized, when we loaded some other objfile. */
5407 if (per_bfd->using_index)
5408 {
5409 dwarf_read_debug_printf ("using_index already set");
5410 per_objfile->resize_symtabs ();
5411 objfile->qf.push_front (make_dwarf_gdb_index ());
5412 return;
5413 }
5414
5415 per_bfd->using_index = 1;
5416 create_all_comp_units (per_objfile);
5417 per_bfd->quick_file_names_table
5418 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5419 per_objfile->resize_symtabs ();
5420
5421 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5422 {
5423 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5424
5425 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5426 struct dwarf2_per_cu_quick_data);
5427 }
5428
5429 /* Arrange for gdb to see the "quick" functions. However, these
5430 functions will be no-ops because we will have expanded all
5431 symtabs. */
5432 objfile->qf.push_front (make_dwarf_gdb_index ());
5433 return;
5434 }
5435
5436 /* Was a debug names index already read when we processed an objfile sharing
5437 PER_BFD? */
5438 if (per_bfd->debug_names_table != nullptr)
5439 {
5440 dwarf_read_debug_printf ("re-using shared debug names table");
5441 per_objfile->resize_symtabs ();
5442 objfile->qf.push_front (make_dwarf_debug_names ());
5443 return;
5444 }
5445
5446 /* Was a GDB index already read when we processed an objfile sharing
5447 PER_BFD? */
5448 if (per_bfd->index_table != nullptr)
5449 {
5450 dwarf_read_debug_printf ("re-using shared index table");
5451 per_objfile->resize_symtabs ();
5452 objfile->qf.push_front (make_dwarf_gdb_index ());
5453 return;
5454 }
5455
5456 /* There might already be partial symtabs built for this BFD. This happens
5457 when loading the same binary twice with the index-cache enabled. If so,
5458 don't try to read an index. The objfile / per_objfile initialization will
5459 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5460 code path. */
5461 if (per_bfd->partial_symtabs != nullptr)
5462 {
5463 dwarf_read_debug_printf ("re-using shared partial symtabs");
5464 objfile->qf.push_front (make_lazy_dwarf_reader ());
5465 return;
5466 }
5467
5468 if (dwarf2_read_debug_names (per_objfile))
5469 {
5470 dwarf_read_debug_printf ("found debug names");
5471 per_objfile->resize_symtabs ();
5472 objfile->qf.push_front (make_dwarf_debug_names ());
5473 return;
5474 }
5475
5476 if (dwarf2_read_gdb_index (per_objfile,
5477 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5478 get_gdb_index_contents_from_section<dwz_file>))
5479 {
5480 dwarf_read_debug_printf ("found gdb index from file");
5481 per_objfile->resize_symtabs ();
5482 objfile->qf.push_front (make_dwarf_gdb_index ());
5483 return;
5484 }
5485
5486 /* ... otherwise, try to find the index in the index cache. */
5487 if (dwarf2_read_gdb_index (per_objfile,
5488 get_gdb_index_contents_from_cache,
5489 get_gdb_index_contents_from_cache_dwz))
5490 {
5491 dwarf_read_debug_printf ("found gdb index from cache");
5492 global_index_cache.hit ();
5493 per_objfile->resize_symtabs ();
5494 objfile->qf.push_front (make_dwarf_gdb_index ());
5495 return;
5496 }
5497
5498 global_index_cache.miss ();
5499 objfile->qf.push_front (make_lazy_dwarf_reader ());
5500 }
5501
5502 \f
5503
5504 /* Build a partial symbol table. */
5505
5506 void
5507 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5508 {
5509 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5510 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5511
5512 if (per_bfd->partial_symtabs != nullptr)
5513 {
5514 /* Partial symbols were already read, so now we can simply
5515 attach them. */
5516 if (psf == nullptr)
5517 {
5518 psf = new psymbol_functions (per_bfd->partial_symtabs);
5519 objfile->qf.emplace_front (psf);
5520 }
5521 else
5522 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5523 per_objfile->resize_symtabs ();
5524 return;
5525 }
5526
5527 if (psf == nullptr)
5528 {
5529 psf = new psymbol_functions;
5530 objfile->qf.emplace_front (psf);
5531 }
5532 const std::shared_ptr<psymtab_storage> &partial_symtabs
5533 = psf->get_partial_symtabs ();
5534
5535 /* Set the local reference to partial symtabs, so that we don't try
5536 to read them again if reading another objfile with the same BFD.
5537 If we can't in fact share, this won't make a difference anyway as
5538 the dwarf2_per_bfd object won't be shared. */
5539 per_bfd->partial_symtabs = partial_symtabs;
5540
5541 try
5542 {
5543 /* This isn't really ideal: all the data we allocate on the
5544 objfile's obstack is still uselessly kept around. However,
5545 freeing it seems unsafe. */
5546 psymtab_discarder psymtabs (partial_symtabs.get ());
5547 dwarf2_build_psymtabs_hard (per_objfile);
5548 psymtabs.keep ();
5549
5550 per_objfile->resize_symtabs ();
5551
5552 /* (maybe) store an index in the cache. */
5553 global_index_cache.store (per_objfile);
5554 }
5555 catch (const gdb_exception_error &except)
5556 {
5557 exception_print (gdb_stderr, except);
5558 }
5559 }
5560
5561 /* Find the base address of the compilation unit for range lists and
5562 location lists. It will normally be specified by DW_AT_low_pc.
5563 In DWARF-3 draft 4, the base address could be overridden by
5564 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5565 compilation units with discontinuous ranges. */
5566
5567 static void
5568 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5569 {
5570 struct attribute *attr;
5571
5572 cu->base_address.reset ();
5573
5574 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5575 if (attr != nullptr)
5576 cu->base_address = attr->as_address ();
5577 else
5578 {
5579 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5580 if (attr != nullptr)
5581 cu->base_address = attr->as_address ();
5582 }
5583 }
5584
5585 /* Helper function that returns the proper abbrev section for
5586 THIS_CU. */
5587
5588 static struct dwarf2_section_info *
5589 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5590 {
5591 struct dwarf2_section_info *abbrev;
5592 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5593
5594 if (this_cu->is_dwz)
5595 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5596 else
5597 abbrev = &per_bfd->abbrev;
5598
5599 return abbrev;
5600 }
5601
5602 /* Fetch the abbreviation table offset from a comp or type unit header. */
5603
5604 static sect_offset
5605 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5606 struct dwarf2_section_info *section,
5607 sect_offset sect_off)
5608 {
5609 bfd *abfd = section->get_bfd_owner ();
5610 const gdb_byte *info_ptr;
5611 unsigned int initial_length_size, offset_size;
5612 uint16_t version;
5613
5614 section->read (per_objfile->objfile);
5615 info_ptr = section->buffer + to_underlying (sect_off);
5616 read_initial_length (abfd, info_ptr, &initial_length_size);
5617 offset_size = initial_length_size == 4 ? 4 : 8;
5618 info_ptr += initial_length_size;
5619
5620 version = read_2_bytes (abfd, info_ptr);
5621 info_ptr += 2;
5622 if (version >= 5)
5623 {
5624 /* Skip unit type and address size. */
5625 info_ptr += 2;
5626 }
5627
5628 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5629 }
5630
5631 /* A partial symtab that is used only for include files. */
5632 struct dwarf2_include_psymtab : public partial_symtab
5633 {
5634 dwarf2_include_psymtab (const char *filename,
5635 psymtab_storage *partial_symtabs,
5636 objfile_per_bfd_storage *objfile_per_bfd)
5637 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5638 {
5639 }
5640
5641 void read_symtab (struct objfile *objfile) override
5642 {
5643 /* It's an include file, no symbols to read for it.
5644 Everything is in the includer symtab. */
5645
5646 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5647 expansion of the includer psymtab. We use the dependencies[0] field to
5648 model the includer. But if we go the regular route of calling
5649 expand_psymtab here, and having expand_psymtab call expand_dependencies
5650 to expand the includer, we'll only use expand_psymtab on the includer
5651 (making it a non-toplevel psymtab), while if we expand the includer via
5652 another path, we'll use read_symtab (making it a toplevel psymtab).
5653 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5654 psymtab, and trigger read_symtab on the includer here directly. */
5655 includer ()->read_symtab (objfile);
5656 }
5657
5658 void expand_psymtab (struct objfile *objfile) override
5659 {
5660 /* This is not called by read_symtab, and should not be called by any
5661 expand_dependencies. */
5662 gdb_assert (false);
5663 }
5664
5665 bool readin_p (struct objfile *objfile) const override
5666 {
5667 return includer ()->readin_p (objfile);
5668 }
5669
5670 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5671 {
5672 return nullptr;
5673 }
5674
5675 private:
5676 partial_symtab *includer () const
5677 {
5678 /* An include psymtab has exactly one dependency: the psymtab that
5679 includes it. */
5680 gdb_assert (this->number_of_dependencies == 1);
5681 return this->dependencies[0];
5682 }
5683 };
5684
5685 /* Allocate a new partial symtab for file named NAME and mark this new
5686 partial symtab as being an include of PST. */
5687
5688 static void
5689 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5690 const char *name,
5691 dwarf2_psymtab *pst,
5692 psymtab_storage *partial_symtabs,
5693 objfile_per_bfd_storage *objfile_per_bfd)
5694 {
5695 dwarf2_include_psymtab *subpst
5696 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5697
5698 if (!IS_ABSOLUTE_PATH (subpst->filename))
5699 subpst->dirname = pst->dirname;
5700
5701 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5702 subpst->dependencies[0] = pst;
5703 subpst->number_of_dependencies = 1;
5704 }
5705
5706 /* Read the Line Number Program data and extract the list of files
5707 included by the source file represented by PST. Build an include
5708 partial symtab for each of these included files. */
5709
5710 static void
5711 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5712 struct die_info *die,
5713 dwarf2_psymtab *pst)
5714 {
5715 line_header_up lh;
5716 struct attribute *attr;
5717
5718 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5719 if (attr != nullptr && attr->form_is_unsigned ())
5720 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5721 if (lh == NULL)
5722 return; /* No linetable, so no includes. */
5723
5724 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5725 that we pass in the raw text_low here; that is ok because we're
5726 only decoding the line table to make include partial symtabs, and
5727 so the addresses aren't really used. */
5728 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5729 pst->raw_text_low (), 1);
5730 }
5731
5732 static hashval_t
5733 hash_signatured_type (const void *item)
5734 {
5735 const struct signatured_type *sig_type
5736 = (const struct signatured_type *) item;
5737
5738 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5739 return sig_type->signature;
5740 }
5741
5742 static int
5743 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5744 {
5745 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5746 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5747
5748 return lhs->signature == rhs->signature;
5749 }
5750
5751 /* Allocate a hash table for signatured types. */
5752
5753 static htab_up
5754 allocate_signatured_type_table ()
5755 {
5756 return htab_up (htab_create_alloc (41,
5757 hash_signatured_type,
5758 eq_signatured_type,
5759 NULL, xcalloc, xfree));
5760 }
5761
5762 /* A helper for create_debug_types_hash_table. Read types from SECTION
5763 and fill them into TYPES_HTAB. It will process only type units,
5764 therefore DW_UT_type. */
5765
5766 static void
5767 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5768 struct dwo_file *dwo_file,
5769 dwarf2_section_info *section, htab_up &types_htab,
5770 rcuh_kind section_kind)
5771 {
5772 struct objfile *objfile = per_objfile->objfile;
5773 struct dwarf2_section_info *abbrev_section;
5774 bfd *abfd;
5775 const gdb_byte *info_ptr, *end_ptr;
5776
5777 abbrev_section = &dwo_file->sections.abbrev;
5778
5779 dwarf_read_debug_printf ("Reading %s for %s",
5780 section->get_name (),
5781 abbrev_section->get_file_name ());
5782
5783 section->read (objfile);
5784 info_ptr = section->buffer;
5785
5786 if (info_ptr == NULL)
5787 return;
5788
5789 /* We can't set abfd until now because the section may be empty or
5790 not present, in which case the bfd is unknown. */
5791 abfd = section->get_bfd_owner ();
5792
5793 /* We don't use cutu_reader here because we don't need to read
5794 any dies: the signature is in the header. */
5795
5796 end_ptr = info_ptr + section->size;
5797 while (info_ptr < end_ptr)
5798 {
5799 signatured_type_up sig_type;
5800 struct dwo_unit *dwo_tu;
5801 void **slot;
5802 const gdb_byte *ptr = info_ptr;
5803 struct comp_unit_head header;
5804 unsigned int length;
5805
5806 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5807
5808 /* Initialize it due to a false compiler warning. */
5809 header.signature = -1;
5810 header.type_cu_offset_in_tu = (cu_offset) -1;
5811
5812 /* We need to read the type's signature in order to build the hash
5813 table, but we don't need anything else just yet. */
5814
5815 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5816 abbrev_section, ptr, section_kind);
5817
5818 length = header.get_length ();
5819
5820 /* Skip dummy type units. */
5821 if (ptr >= info_ptr + length
5822 || peek_abbrev_code (abfd, ptr) == 0
5823 || (header.unit_type != DW_UT_type
5824 && header.unit_type != DW_UT_split_type))
5825 {
5826 info_ptr += length;
5827 continue;
5828 }
5829
5830 if (types_htab == NULL)
5831 types_htab = allocate_dwo_unit_table ();
5832
5833 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5834 dwo_tu->dwo_file = dwo_file;
5835 dwo_tu->signature = header.signature;
5836 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5837 dwo_tu->section = section;
5838 dwo_tu->sect_off = sect_off;
5839 dwo_tu->length = length;
5840
5841 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5842 gdb_assert (slot != NULL);
5843 if (*slot != NULL)
5844 complaint (_("debug type entry at offset %s is duplicate to"
5845 " the entry at offset %s, signature %s"),
5846 sect_offset_str (sect_off),
5847 sect_offset_str (dwo_tu->sect_off),
5848 hex_string (header.signature));
5849 *slot = dwo_tu;
5850
5851 dwarf_read_debug_printf_v (" offset %s, signature %s",
5852 sect_offset_str (sect_off),
5853 hex_string (header.signature));
5854
5855 info_ptr += length;
5856 }
5857 }
5858
5859 /* Create the hash table of all entries in the .debug_types
5860 (or .debug_types.dwo) section(s).
5861 DWO_FILE is a pointer to the DWO file object.
5862
5863 The result is a pointer to the hash table or NULL if there are no types.
5864
5865 Note: This function processes DWO files only, not DWP files. */
5866
5867 static void
5868 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5869 struct dwo_file *dwo_file,
5870 gdb::array_view<dwarf2_section_info> type_sections,
5871 htab_up &types_htab)
5872 {
5873 for (dwarf2_section_info &section : type_sections)
5874 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5875 rcuh_kind::TYPE);
5876 }
5877
5878 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5879 If SLOT is non-NULL, it is the entry to use in the hash table.
5880 Otherwise we find one. */
5881
5882 static struct signatured_type *
5883 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5884 {
5885 if (per_objfile->per_bfd->all_comp_units.size ()
5886 == per_objfile->per_bfd->all_comp_units.capacity ())
5887 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5888
5889 signatured_type_up sig_type_holder
5890 = per_objfile->per_bfd->allocate_signatured_type (sig);
5891 signatured_type *sig_type = sig_type_holder.get ();
5892
5893 per_objfile->resize_symtabs ();
5894
5895 per_objfile->per_bfd->all_comp_units.emplace_back
5896 (sig_type_holder.release ());
5897 if (per_objfile->per_bfd->using_index)
5898 {
5899 sig_type->v.quick =
5900 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
5901 struct dwarf2_per_cu_quick_data);
5902 }
5903
5904 if (slot == NULL)
5905 {
5906 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5907 sig_type, INSERT);
5908 }
5909 gdb_assert (*slot == NULL);
5910 *slot = sig_type;
5911 /* The rest of sig_type must be filled in by the caller. */
5912 return sig_type;
5913 }
5914
5915 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5916 Fill in SIG_ENTRY with DWO_ENTRY. */
5917
5918 static void
5919 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
5920 struct signatured_type *sig_entry,
5921 struct dwo_unit *dwo_entry)
5922 {
5923 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5924
5925 /* Make sure we're not clobbering something we don't expect to. */
5926 gdb_assert (! sig_entry->queued);
5927 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
5928 if (per_bfd->using_index)
5929 {
5930 gdb_assert (sig_entry->v.quick != NULL);
5931 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
5932 }
5933 else
5934 gdb_assert (sig_entry->v.psymtab == NULL);
5935 gdb_assert (sig_entry->signature == dwo_entry->signature);
5936 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5937 gdb_assert (sig_entry->type_unit_group == NULL);
5938 gdb_assert (sig_entry->dwo_unit == NULL);
5939
5940 sig_entry->section = dwo_entry->section;
5941 sig_entry->sect_off = dwo_entry->sect_off;
5942 sig_entry->length = dwo_entry->length;
5943 sig_entry->reading_dwo_directly = 1;
5944 sig_entry->per_bfd = per_bfd;
5945 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5946 sig_entry->dwo_unit = dwo_entry;
5947 }
5948
5949 /* Subroutine of lookup_signatured_type.
5950 If we haven't read the TU yet, create the signatured_type data structure
5951 for a TU to be read in directly from a DWO file, bypassing the stub.
5952 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5953 using .gdb_index, then when reading a CU we want to stay in the DWO file
5954 containing that CU. Otherwise we could end up reading several other DWO
5955 files (due to comdat folding) to process the transitive closure of all the
5956 mentioned TUs, and that can be slow. The current DWO file will have every
5957 type signature that it needs.
5958 We only do this for .gdb_index because in the psymtab case we already have
5959 to read all the DWOs to build the type unit groups. */
5960
5961 static struct signatured_type *
5962 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5963 {
5964 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5965 struct dwo_file *dwo_file;
5966 struct dwo_unit find_dwo_entry, *dwo_entry;
5967 void **slot;
5968
5969 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
5970
5971 /* If TU skeletons have been removed then we may not have read in any
5972 TUs yet. */
5973 if (per_objfile->per_bfd->signatured_types == NULL)
5974 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5975
5976 /* We only ever need to read in one copy of a signatured type.
5977 Use the global signatured_types array to do our own comdat-folding
5978 of types. If this is the first time we're reading this TU, and
5979 the TU has an entry in .gdb_index, replace the recorded data from
5980 .gdb_index with this TU. */
5981
5982 signatured_type find_sig_entry (sig);
5983 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5984 &find_sig_entry, INSERT);
5985 signatured_type *sig_entry = (struct signatured_type *) *slot;
5986
5987 /* We can get here with the TU already read, *or* in the process of being
5988 read. Don't reassign the global entry to point to this DWO if that's
5989 the case. Also note that if the TU is already being read, it may not
5990 have come from a DWO, the program may be a mix of Fission-compiled
5991 code and non-Fission-compiled code. */
5992
5993 /* Have we already tried to read this TU?
5994 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5995 needn't exist in the global table yet). */
5996 if (sig_entry != NULL && sig_entry->tu_read)
5997 return sig_entry;
5998
5999 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6000 dwo_unit of the TU itself. */
6001 dwo_file = cu->dwo_unit->dwo_file;
6002
6003 /* Ok, this is the first time we're reading this TU. */
6004 if (dwo_file->tus == NULL)
6005 return NULL;
6006 find_dwo_entry.signature = sig;
6007 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6008 &find_dwo_entry);
6009 if (dwo_entry == NULL)
6010 return NULL;
6011
6012 /* If the global table doesn't have an entry for this TU, add one. */
6013 if (sig_entry == NULL)
6014 sig_entry = add_type_unit (per_objfile, sig, slot);
6015
6016 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6017 sig_entry->tu_read = 1;
6018 return sig_entry;
6019 }
6020
6021 /* Subroutine of lookup_signatured_type.
6022 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6023 then try the DWP file. If the TU stub (skeleton) has been removed then
6024 it won't be in .gdb_index. */
6025
6026 static struct signatured_type *
6027 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6028 {
6029 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6030 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6031 struct dwo_unit *dwo_entry;
6032 void **slot;
6033
6034 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6035 gdb_assert (dwp_file != NULL);
6036
6037 /* If TU skeletons have been removed then we may not have read in any
6038 TUs yet. */
6039 if (per_objfile->per_bfd->signatured_types == NULL)
6040 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6041
6042 signatured_type find_sig_entry (sig);
6043 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6044 &find_sig_entry, INSERT);
6045 signatured_type *sig_entry = (struct signatured_type *) *slot;
6046
6047 /* Have we already tried to read this TU?
6048 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6049 needn't exist in the global table yet). */
6050 if (sig_entry != NULL)
6051 return sig_entry;
6052
6053 if (dwp_file->tus == NULL)
6054 return NULL;
6055 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6056 1 /* is_debug_types */);
6057 if (dwo_entry == NULL)
6058 return NULL;
6059
6060 sig_entry = add_type_unit (per_objfile, sig, slot);
6061 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6062
6063 return sig_entry;
6064 }
6065
6066 /* Lookup a signature based type for DW_FORM_ref_sig8.
6067 Returns NULL if signature SIG is not present in the table.
6068 It is up to the caller to complain about this. */
6069
6070 static struct signatured_type *
6071 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6072 {
6073 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6074
6075 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6076 {
6077 /* We're in a DWO/DWP file, and we're using .gdb_index.
6078 These cases require special processing. */
6079 if (get_dwp_file (per_objfile) == NULL)
6080 return lookup_dwo_signatured_type (cu, sig);
6081 else
6082 return lookup_dwp_signatured_type (cu, sig);
6083 }
6084 else
6085 {
6086 if (per_objfile->per_bfd->signatured_types == NULL)
6087 return NULL;
6088 signatured_type find_entry (sig);
6089 return ((struct signatured_type *)
6090 htab_find (per_objfile->per_bfd->signatured_types.get (),
6091 &find_entry));
6092 }
6093 }
6094
6095 /* Low level DIE reading support. */
6096
6097 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6098
6099 static void
6100 init_cu_die_reader (struct die_reader_specs *reader,
6101 struct dwarf2_cu *cu,
6102 struct dwarf2_section_info *section,
6103 struct dwo_file *dwo_file,
6104 struct abbrev_table *abbrev_table)
6105 {
6106 gdb_assert (section->readin && section->buffer != NULL);
6107 reader->abfd = section->get_bfd_owner ();
6108 reader->cu = cu;
6109 reader->dwo_file = dwo_file;
6110 reader->die_section = section;
6111 reader->buffer = section->buffer;
6112 reader->buffer_end = section->buffer + section->size;
6113 reader->abbrev_table = abbrev_table;
6114 }
6115
6116 /* Subroutine of cutu_reader to simplify it.
6117 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6118 There's just a lot of work to do, and cutu_reader is big enough
6119 already.
6120
6121 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6122 from it to the DIE in the DWO. If NULL we are skipping the stub.
6123 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6124 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6125 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6126 STUB_COMP_DIR may be non-NULL.
6127 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6128 are filled in with the info of the DIE from the DWO file.
6129 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6130 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6131 kept around for at least as long as *RESULT_READER.
6132
6133 The result is non-zero if a valid (non-dummy) DIE was found. */
6134
6135 static int
6136 read_cutu_die_from_dwo (dwarf2_cu *cu,
6137 struct dwo_unit *dwo_unit,
6138 struct die_info *stub_comp_unit_die,
6139 const char *stub_comp_dir,
6140 struct die_reader_specs *result_reader,
6141 const gdb_byte **result_info_ptr,
6142 struct die_info **result_comp_unit_die,
6143 abbrev_table_up *result_dwo_abbrev_table)
6144 {
6145 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6146 dwarf2_per_cu_data *per_cu = cu->per_cu;
6147 struct objfile *objfile = per_objfile->objfile;
6148 bfd *abfd;
6149 const gdb_byte *begin_info_ptr, *info_ptr;
6150 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6151 int i,num_extra_attrs;
6152 struct dwarf2_section_info *dwo_abbrev_section;
6153 struct die_info *comp_unit_die;
6154
6155 /* At most one of these may be provided. */
6156 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6157
6158 /* These attributes aren't processed until later:
6159 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6160 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6161 referenced later. However, these attributes are found in the stub
6162 which we won't have later. In order to not impose this complication
6163 on the rest of the code, we read them here and copy them to the
6164 DWO CU/TU die. */
6165
6166 stmt_list = NULL;
6167 low_pc = NULL;
6168 high_pc = NULL;
6169 ranges = NULL;
6170 comp_dir = NULL;
6171
6172 if (stub_comp_unit_die != NULL)
6173 {
6174 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6175 DWO file. */
6176 if (!per_cu->is_debug_types)
6177 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6178 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6179 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6180 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6181 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6182
6183 cu->addr_base = stub_comp_unit_die->addr_base ();
6184
6185 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6186 We need the value before we can process DW_AT_ranges values from the
6187 DWO. */
6188 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6189
6190 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6191 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6192 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6193 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6194 section. */
6195 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6196 }
6197 else if (stub_comp_dir != NULL)
6198 {
6199 /* Reconstruct the comp_dir attribute to simplify the code below. */
6200 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6201 comp_dir->name = DW_AT_comp_dir;
6202 comp_dir->form = DW_FORM_string;
6203 comp_dir->set_string_noncanonical (stub_comp_dir);
6204 }
6205
6206 /* Set up for reading the DWO CU/TU. */
6207 cu->dwo_unit = dwo_unit;
6208 dwarf2_section_info *section = dwo_unit->section;
6209 section->read (objfile);
6210 abfd = section->get_bfd_owner ();
6211 begin_info_ptr = info_ptr = (section->buffer
6212 + to_underlying (dwo_unit->sect_off));
6213 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6214
6215 if (per_cu->is_debug_types)
6216 {
6217 signatured_type *sig_type = (struct signatured_type *) per_cu;
6218
6219 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6220 section, dwo_abbrev_section,
6221 info_ptr, rcuh_kind::TYPE);
6222 /* This is not an assert because it can be caused by bad debug info. */
6223 if (sig_type->signature != cu->header.signature)
6224 {
6225 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6226 " TU at offset %s [in module %s]"),
6227 hex_string (sig_type->signature),
6228 hex_string (cu->header.signature),
6229 sect_offset_str (dwo_unit->sect_off),
6230 bfd_get_filename (abfd));
6231 }
6232 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6233 /* For DWOs coming from DWP files, we don't know the CU length
6234 nor the type's offset in the TU until now. */
6235 dwo_unit->length = cu->header.get_length ();
6236 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6237
6238 /* Establish the type offset that can be used to lookup the type.
6239 For DWO files, we don't know it until now. */
6240 sig_type->type_offset_in_section
6241 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6242 }
6243 else
6244 {
6245 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6246 section, dwo_abbrev_section,
6247 info_ptr, rcuh_kind::COMPILE);
6248 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6249 /* For DWOs coming from DWP files, we don't know the CU length
6250 until now. */
6251 dwo_unit->length = cu->header.get_length ();
6252 }
6253
6254 dwo_abbrev_section->read (objfile);
6255 *result_dwo_abbrev_table
6256 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6257 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6258 result_dwo_abbrev_table->get ());
6259
6260 /* Read in the die, but leave space to copy over the attributes
6261 from the stub. This has the benefit of simplifying the rest of
6262 the code - all the work to maintain the illusion of a single
6263 DW_TAG_{compile,type}_unit DIE is done here. */
6264 num_extra_attrs = ((stmt_list != NULL)
6265 + (low_pc != NULL)
6266 + (high_pc != NULL)
6267 + (ranges != NULL)
6268 + (comp_dir != NULL));
6269 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6270 num_extra_attrs);
6271
6272 /* Copy over the attributes from the stub to the DIE we just read in. */
6273 comp_unit_die = *result_comp_unit_die;
6274 i = comp_unit_die->num_attrs;
6275 if (stmt_list != NULL)
6276 comp_unit_die->attrs[i++] = *stmt_list;
6277 if (low_pc != NULL)
6278 comp_unit_die->attrs[i++] = *low_pc;
6279 if (high_pc != NULL)
6280 comp_unit_die->attrs[i++] = *high_pc;
6281 if (ranges != NULL)
6282 comp_unit_die->attrs[i++] = *ranges;
6283 if (comp_dir != NULL)
6284 comp_unit_die->attrs[i++] = *comp_dir;
6285 comp_unit_die->num_attrs += num_extra_attrs;
6286
6287 if (dwarf_die_debug)
6288 {
6289 fprintf_unfiltered (gdb_stdlog,
6290 "Read die from %s@0x%x of %s:\n",
6291 section->get_name (),
6292 (unsigned) (begin_info_ptr - section->buffer),
6293 bfd_get_filename (abfd));
6294 dump_die (comp_unit_die, dwarf_die_debug);
6295 }
6296
6297 /* Skip dummy compilation units. */
6298 if (info_ptr >= begin_info_ptr + dwo_unit->length
6299 || peek_abbrev_code (abfd, info_ptr) == 0)
6300 return 0;
6301
6302 *result_info_ptr = info_ptr;
6303 return 1;
6304 }
6305
6306 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6307 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6308 signature is part of the header. */
6309 static gdb::optional<ULONGEST>
6310 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6311 {
6312 if (cu->header.version >= 5)
6313 return cu->header.signature;
6314 struct attribute *attr;
6315 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6316 if (attr == nullptr || !attr->form_is_unsigned ())
6317 return gdb::optional<ULONGEST> ();
6318 return attr->as_unsigned ();
6319 }
6320
6321 /* Subroutine of cutu_reader to simplify it.
6322 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6323 Returns NULL if the specified DWO unit cannot be found. */
6324
6325 static struct dwo_unit *
6326 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6327 {
6328 dwarf2_per_cu_data *per_cu = cu->per_cu;
6329 struct dwo_unit *dwo_unit;
6330 const char *comp_dir;
6331
6332 gdb_assert (cu != NULL);
6333
6334 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6335 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6336 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6337
6338 if (per_cu->is_debug_types)
6339 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6340 else
6341 {
6342 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6343
6344 if (!signature.has_value ())
6345 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6346 " [in module %s]"),
6347 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6348
6349 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6350 }
6351
6352 return dwo_unit;
6353 }
6354
6355 /* Subroutine of cutu_reader to simplify it.
6356 See it for a description of the parameters.
6357 Read a TU directly from a DWO file, bypassing the stub. */
6358
6359 void
6360 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6361 dwarf2_per_objfile *per_objfile,
6362 dwarf2_cu *existing_cu)
6363 {
6364 struct signatured_type *sig_type;
6365
6366 /* Verify we can do the following downcast, and that we have the
6367 data we need. */
6368 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6369 sig_type = (struct signatured_type *) this_cu;
6370 gdb_assert (sig_type->dwo_unit != NULL);
6371
6372 dwarf2_cu *cu;
6373
6374 if (existing_cu != nullptr)
6375 {
6376 cu = existing_cu;
6377 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6378 /* There's no need to do the rereading_dwo_cu handling that
6379 cutu_reader does since we don't read the stub. */
6380 }
6381 else
6382 {
6383 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6384 in per_objfile yet. */
6385 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6386 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6387 cu = m_new_cu.get ();
6388 }
6389
6390 /* A future optimization, if needed, would be to use an existing
6391 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6392 could share abbrev tables. */
6393
6394 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6395 NULL /* stub_comp_unit_die */,
6396 sig_type->dwo_unit->dwo_file->comp_dir,
6397 this, &info_ptr,
6398 &comp_unit_die,
6399 &m_dwo_abbrev_table) == 0)
6400 {
6401 /* Dummy die. */
6402 dummy_p = true;
6403 }
6404 }
6405
6406 /* Initialize a CU (or TU) and read its DIEs.
6407 If the CU defers to a DWO file, read the DWO file as well.
6408
6409 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6410 Otherwise the table specified in the comp unit header is read in and used.
6411 This is an optimization for when we already have the abbrev table.
6412
6413 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6414 allocated. */
6415
6416 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6417 dwarf2_per_objfile *per_objfile,
6418 struct abbrev_table *abbrev_table,
6419 dwarf2_cu *existing_cu,
6420 bool skip_partial)
6421 : die_reader_specs {},
6422 m_this_cu (this_cu)
6423 {
6424 struct objfile *objfile = per_objfile->objfile;
6425 struct dwarf2_section_info *section = this_cu->section;
6426 bfd *abfd = section->get_bfd_owner ();
6427 const gdb_byte *begin_info_ptr;
6428 struct signatured_type *sig_type = NULL;
6429 struct dwarf2_section_info *abbrev_section;
6430 /* Non-zero if CU currently points to a DWO file and we need to
6431 reread it. When this happens we need to reread the skeleton die
6432 before we can reread the DWO file (this only applies to CUs, not TUs). */
6433 int rereading_dwo_cu = 0;
6434
6435 if (dwarf_die_debug)
6436 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6437 this_cu->is_debug_types ? "type" : "comp",
6438 sect_offset_str (this_cu->sect_off));
6439
6440 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6441 file (instead of going through the stub), short-circuit all of this. */
6442 if (this_cu->reading_dwo_directly)
6443 {
6444 /* Narrow down the scope of possibilities to have to understand. */
6445 gdb_assert (this_cu->is_debug_types);
6446 gdb_assert (abbrev_table == NULL);
6447 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6448 return;
6449 }
6450
6451 /* This is cheap if the section is already read in. */
6452 section->read (objfile);
6453
6454 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6455
6456 abbrev_section = get_abbrev_section_for_cu (this_cu);
6457
6458 dwarf2_cu *cu;
6459
6460 if (existing_cu != nullptr)
6461 {
6462 cu = existing_cu;
6463 /* If this CU is from a DWO file we need to start over, we need to
6464 refetch the attributes from the skeleton CU.
6465 This could be optimized by retrieving those attributes from when we
6466 were here the first time: the previous comp_unit_die was stored in
6467 comp_unit_obstack. But there's no data yet that we need this
6468 optimization. */
6469 if (cu->dwo_unit != NULL)
6470 rereading_dwo_cu = 1;
6471 }
6472 else
6473 {
6474 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6475 in per_objfile yet. */
6476 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6477 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6478 cu = m_new_cu.get ();
6479 }
6480
6481 /* Get the header. */
6482 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6483 {
6484 /* We already have the header, there's no need to read it in again. */
6485 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6486 }
6487 else
6488 {
6489 if (this_cu->is_debug_types)
6490 {
6491 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6492 section, abbrev_section,
6493 info_ptr, rcuh_kind::TYPE);
6494
6495 /* Since per_cu is the first member of struct signatured_type,
6496 we can go from a pointer to one to a pointer to the other. */
6497 sig_type = (struct signatured_type *) this_cu;
6498 gdb_assert (sig_type->signature == cu->header.signature);
6499 gdb_assert (sig_type->type_offset_in_tu
6500 == cu->header.type_cu_offset_in_tu);
6501 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6502
6503 /* LENGTH has not been set yet for type units if we're
6504 using .gdb_index. */
6505 this_cu->length = cu->header.get_length ();
6506
6507 /* Establish the type offset that can be used to lookup the type. */
6508 sig_type->type_offset_in_section =
6509 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6510
6511 this_cu->dwarf_version = cu->header.version;
6512 }
6513 else
6514 {
6515 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6516 section, abbrev_section,
6517 info_ptr,
6518 rcuh_kind::COMPILE);
6519
6520 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6521 if (this_cu->length == 0)
6522 this_cu->length = cu->header.get_length ();
6523 else
6524 gdb_assert (this_cu->length == cu->header.get_length ());
6525 this_cu->dwarf_version = cu->header.version;
6526 }
6527 }
6528
6529 /* Skip dummy compilation units. */
6530 if (info_ptr >= begin_info_ptr + this_cu->length
6531 || peek_abbrev_code (abfd, info_ptr) == 0)
6532 {
6533 dummy_p = true;
6534 return;
6535 }
6536
6537 /* If we don't have them yet, read the abbrevs for this compilation unit.
6538 And if we need to read them now, make sure they're freed when we're
6539 done. */
6540 if (abbrev_table != NULL)
6541 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6542 else
6543 {
6544 abbrev_section->read (objfile);
6545 m_abbrev_table_holder
6546 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6547 abbrev_table = m_abbrev_table_holder.get ();
6548 }
6549
6550 /* Read the top level CU/TU die. */
6551 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6552 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6553
6554 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6555 {
6556 dummy_p = true;
6557 return;
6558 }
6559
6560 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6561 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6562 table from the DWO file and pass the ownership over to us. It will be
6563 referenced from READER, so we must make sure to free it after we're done
6564 with READER.
6565
6566 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6567 DWO CU, that this test will fail (the attribute will not be present). */
6568 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6569 if (dwo_name != nullptr)
6570 {
6571 struct dwo_unit *dwo_unit;
6572 struct die_info *dwo_comp_unit_die;
6573
6574 if (comp_unit_die->has_children)
6575 {
6576 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6577 " has children (offset %s) [in module %s]"),
6578 sect_offset_str (this_cu->sect_off),
6579 bfd_get_filename (abfd));
6580 }
6581 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6582 if (dwo_unit != NULL)
6583 {
6584 if (read_cutu_die_from_dwo (cu, dwo_unit,
6585 comp_unit_die, NULL,
6586 this, &info_ptr,
6587 &dwo_comp_unit_die,
6588 &m_dwo_abbrev_table) == 0)
6589 {
6590 /* Dummy die. */
6591 dummy_p = true;
6592 return;
6593 }
6594 comp_unit_die = dwo_comp_unit_die;
6595 }
6596 else
6597 {
6598 /* Yikes, we couldn't find the rest of the DIE, we only have
6599 the stub. A complaint has already been logged. There's
6600 not much more we can do except pass on the stub DIE to
6601 die_reader_func. We don't want to throw an error on bad
6602 debug info. */
6603 }
6604 }
6605 }
6606
6607 void
6608 cutu_reader::keep ()
6609 {
6610 /* Done, clean up. */
6611 gdb_assert (!dummy_p);
6612 if (m_new_cu != NULL)
6613 {
6614 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6615 now. */
6616 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6617 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6618 }
6619 }
6620
6621 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6622 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6623 assumed to have already done the lookup to find the DWO file).
6624
6625 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6626 THIS_CU->is_debug_types, but nothing else.
6627
6628 We fill in THIS_CU->length.
6629
6630 THIS_CU->cu is always freed when done.
6631 This is done in order to not leave THIS_CU->cu in a state where we have
6632 to care whether it refers to the "main" CU or the DWO CU.
6633
6634 When parent_cu is passed, it is used to provide a default value for
6635 str_offsets_base and addr_base from the parent. */
6636
6637 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6638 dwarf2_per_objfile *per_objfile,
6639 struct dwarf2_cu *parent_cu,
6640 struct dwo_file *dwo_file)
6641 : die_reader_specs {},
6642 m_this_cu (this_cu)
6643 {
6644 struct objfile *objfile = per_objfile->objfile;
6645 struct dwarf2_section_info *section = this_cu->section;
6646 bfd *abfd = section->get_bfd_owner ();
6647 struct dwarf2_section_info *abbrev_section;
6648 const gdb_byte *begin_info_ptr, *info_ptr;
6649
6650 if (dwarf_die_debug)
6651 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6652 this_cu->is_debug_types ? "type" : "comp",
6653 sect_offset_str (this_cu->sect_off));
6654
6655 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6656
6657 abbrev_section = (dwo_file != NULL
6658 ? &dwo_file->sections.abbrev
6659 : get_abbrev_section_for_cu (this_cu));
6660
6661 /* This is cheap if the section is already read in. */
6662 section->read (objfile);
6663
6664 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6665
6666 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6667 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6668 section, abbrev_section, info_ptr,
6669 (this_cu->is_debug_types
6670 ? rcuh_kind::TYPE
6671 : rcuh_kind::COMPILE));
6672
6673 if (parent_cu != nullptr)
6674 {
6675 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6676 m_new_cu->addr_base = parent_cu->addr_base;
6677 }
6678 this_cu->length = m_new_cu->header.get_length ();
6679
6680 /* Skip dummy compilation units. */
6681 if (info_ptr >= begin_info_ptr + this_cu->length
6682 || peek_abbrev_code (abfd, info_ptr) == 0)
6683 {
6684 dummy_p = true;
6685 return;
6686 }
6687
6688 abbrev_section->read (objfile);
6689 m_abbrev_table_holder
6690 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6691
6692 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6693 m_abbrev_table_holder.get ());
6694 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6695 }
6696
6697 \f
6698 /* Type Unit Groups.
6699
6700 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6701 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6702 so that all types coming from the same compilation (.o file) are grouped
6703 together. A future step could be to put the types in the same symtab as
6704 the CU the types ultimately came from. */
6705
6706 static hashval_t
6707 hash_type_unit_group (const void *item)
6708 {
6709 const struct type_unit_group *tu_group
6710 = (const struct type_unit_group *) item;
6711
6712 return hash_stmt_list_entry (&tu_group->hash);
6713 }
6714
6715 static int
6716 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6717 {
6718 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6719 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6720
6721 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6722 }
6723
6724 /* Allocate a hash table for type unit groups. */
6725
6726 static htab_up
6727 allocate_type_unit_groups_table ()
6728 {
6729 return htab_up (htab_create_alloc (3,
6730 hash_type_unit_group,
6731 eq_type_unit_group,
6732 htab_delete_entry<type_unit_group>,
6733 xcalloc, xfree));
6734 }
6735
6736 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6737 partial symtabs. We combine several TUs per psymtab to not let the size
6738 of any one psymtab grow too big. */
6739 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6740 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6741
6742 /* Helper routine for get_type_unit_group.
6743 Create the type_unit_group object used to hold one or more TUs. */
6744
6745 static std::unique_ptr<type_unit_group>
6746 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6747 {
6748 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6749 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6750
6751 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6752 tu_group->per_bfd = per_bfd;
6753
6754 if (per_bfd->using_index)
6755 {
6756 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6757 struct dwarf2_per_cu_quick_data);
6758 }
6759 else
6760 {
6761 unsigned int line_offset = to_underlying (line_offset_struct);
6762 dwarf2_psymtab *pst;
6763 std::string name;
6764
6765 /* Give the symtab a useful name for debug purposes. */
6766 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6767 name = string_printf ("<type_units_%d>",
6768 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6769 else
6770 name = string_printf ("<type_units_at_0x%x>", line_offset);
6771
6772 pst = create_partial_symtab (tu_group.get (), per_objfile,
6773 name.c_str ());
6774 pst->anonymous = true;
6775 }
6776
6777 tu_group->hash.dwo_unit = cu->dwo_unit;
6778 tu_group->hash.line_sect_off = line_offset_struct;
6779
6780 return tu_group;
6781 }
6782
6783 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6784 STMT_LIST is a DW_AT_stmt_list attribute. */
6785
6786 static struct type_unit_group *
6787 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6788 {
6789 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6790 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6791 struct type_unit_group *tu_group;
6792 void **slot;
6793 unsigned int line_offset;
6794 struct type_unit_group type_unit_group_for_lookup;
6795
6796 if (per_objfile->per_bfd->type_unit_groups == NULL)
6797 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6798
6799 /* Do we need to create a new group, or can we use an existing one? */
6800
6801 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6802 {
6803 line_offset = stmt_list->as_unsigned ();
6804 ++tu_stats->nr_symtab_sharers;
6805 }
6806 else
6807 {
6808 /* Ugh, no stmt_list. Rare, but we have to handle it.
6809 We can do various things here like create one group per TU or
6810 spread them over multiple groups to split up the expansion work.
6811 To avoid worst case scenarios (too many groups or too large groups)
6812 we, umm, group them in bunches. */
6813 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6814 | (tu_stats->nr_stmt_less_type_units
6815 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6816 ++tu_stats->nr_stmt_less_type_units;
6817 }
6818
6819 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6820 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6821 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6822 &type_unit_group_for_lookup, INSERT);
6823 if (*slot == nullptr)
6824 {
6825 sect_offset line_offset_struct = (sect_offset) line_offset;
6826 std::unique_ptr<type_unit_group> grp
6827 = create_type_unit_group (cu, line_offset_struct);
6828 *slot = grp.release ();
6829 ++tu_stats->nr_symtabs;
6830 }
6831
6832 tu_group = (struct type_unit_group *) *slot;
6833 gdb_assert (tu_group != nullptr);
6834 return tu_group;
6835 }
6836 \f
6837 /* Partial symbol tables. */
6838
6839 /* Create a psymtab named NAME and assign it to PER_CU.
6840
6841 The caller must fill in the following details:
6842 dirname, textlow, texthigh. */
6843
6844 static dwarf2_psymtab *
6845 create_partial_symtab (dwarf2_per_cu_data *per_cu,
6846 dwarf2_per_objfile *per_objfile,
6847 const char *name)
6848 {
6849 dwarf2_psymtab *pst
6850 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6851 per_objfile->objfile->per_bfd, per_cu);
6852
6853 pst->psymtabs_addrmap_supported = true;
6854
6855 /* This is the glue that links PST into GDB's symbol API. */
6856 per_cu->v.psymtab = pst;
6857
6858 return pst;
6859 }
6860
6861 /* DIE reader function for process_psymtab_comp_unit. */
6862
6863 static void
6864 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6865 const gdb_byte *info_ptr,
6866 struct die_info *comp_unit_die,
6867 enum language pretend_language)
6868 {
6869 struct dwarf2_cu *cu = reader->cu;
6870 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6871 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6872 struct objfile *objfile = per_objfile->objfile;
6873 struct gdbarch *gdbarch = objfile->arch ();
6874 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6875 CORE_ADDR baseaddr;
6876 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6877 dwarf2_psymtab *pst;
6878 enum pc_bounds_kind cu_bounds_kind;
6879 const char *filename;
6880
6881 gdb_assert (! per_cu->is_debug_types);
6882
6883 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
6884
6885 /* Allocate a new partial symbol table structure. */
6886 gdb::unique_xmalloc_ptr<char> debug_filename;
6887 static const char artificial[] = "<artificial>";
6888 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6889 if (filename == NULL)
6890 filename = "";
6891 else if (strcmp (filename, artificial) == 0)
6892 {
6893 debug_filename.reset (concat (artificial, "@",
6894 sect_offset_str (per_cu->sect_off),
6895 (char *) NULL));
6896 filename = debug_filename.get ();
6897 }
6898
6899 pst = create_partial_symtab (per_cu, per_objfile, filename);
6900
6901 /* This must be done before calling dwarf2_build_include_psymtabs. */
6902 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6903
6904 baseaddr = objfile->text_section_offset ();
6905
6906 dwarf2_find_base_address (comp_unit_die, cu);
6907
6908 /* Possibly set the default values of LOWPC and HIGHPC from
6909 `DW_AT_ranges'. */
6910 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6911 &best_highpc, cu, pst);
6912 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6913 {
6914 CORE_ADDR low
6915 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
6916 - baseaddr);
6917 CORE_ADDR high
6918 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
6919 - baseaddr - 1);
6920 /* Store the contiguous range if it is not empty; it can be
6921 empty for CUs with no code. */
6922 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
6923 low, high, pst);
6924 }
6925
6926 /* Check if comp unit has_children.
6927 If so, read the rest of the partial symbols from this comp unit.
6928 If not, there's no more debug_info for this comp unit. */
6929 if (comp_unit_die->has_children)
6930 {
6931 struct partial_die_info *first_die;
6932 CORE_ADDR lowpc, highpc;
6933
6934 lowpc = ((CORE_ADDR) -1);
6935 highpc = ((CORE_ADDR) 0);
6936
6937 first_die = load_partial_dies (reader, info_ptr, 1);
6938
6939 scan_partial_symbols (first_die, &lowpc, &highpc,
6940 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6941
6942 /* If we didn't find a lowpc, set it to highpc to avoid
6943 complaints from `maint check'. */
6944 if (lowpc == ((CORE_ADDR) -1))
6945 lowpc = highpc;
6946
6947 /* If the compilation unit didn't have an explicit address range,
6948 then use the information extracted from its child dies. */
6949 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6950 {
6951 best_lowpc = lowpc;
6952 best_highpc = highpc;
6953 }
6954 }
6955 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
6956 best_lowpc + baseaddr)
6957 - baseaddr);
6958 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
6959 best_highpc + baseaddr)
6960 - baseaddr);
6961
6962 pst->end ();
6963
6964 if (!cu->per_cu->imported_symtabs_empty ())
6965 {
6966 int i;
6967 int len = cu->per_cu->imported_symtabs_size ();
6968
6969 /* Fill in 'dependencies' here; we fill in 'users' in a
6970 post-pass. */
6971 pst->number_of_dependencies = len;
6972 pst->dependencies
6973 = per_bfd->partial_symtabs->allocate_dependencies (len);
6974 for (i = 0; i < len; ++i)
6975 {
6976 pst->dependencies[i]
6977 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
6978 }
6979
6980 cu->per_cu->imported_symtabs_free ();
6981 }
6982
6983 /* Get the list of files included in the current compilation unit,
6984 and build a psymtab for each of them. */
6985 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6986
6987 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
6988 ", %d global, %d static syms",
6989 per_cu->is_debug_types ? "type" : "comp",
6990 sect_offset_str (per_cu->sect_off),
6991 paddress (gdbarch, pst->text_low (objfile)),
6992 paddress (gdbarch, pst->text_high (objfile)),
6993 (int) pst->global_psymbols.size (),
6994 (int) pst->static_psymbols.size ());
6995 }
6996
6997 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6998 Process compilation unit THIS_CU for a psymtab. */
6999
7000 static void
7001 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7002 dwarf2_per_objfile *per_objfile,
7003 bool want_partial_unit,
7004 enum language pretend_language)
7005 {
7006 /* If this compilation unit was already read in, free the
7007 cached copy in order to read it in again. This is
7008 necessary because we skipped some symbols when we first
7009 read in the compilation unit (see load_partial_dies).
7010 This problem could be avoided, but the benefit is unclear. */
7011 per_objfile->remove_cu (this_cu);
7012
7013 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7014
7015 if (reader.comp_unit_die == nullptr)
7016 return;
7017
7018 switch (reader.comp_unit_die->tag)
7019 {
7020 case DW_TAG_compile_unit:
7021 this_cu->unit_type = DW_UT_compile;
7022 break;
7023 case DW_TAG_partial_unit:
7024 this_cu->unit_type = DW_UT_partial;
7025 break;
7026 case DW_TAG_type_unit:
7027 this_cu->unit_type = DW_UT_type;
7028 break;
7029 default:
7030 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7031 dwarf_tag_name (reader.comp_unit_die->tag),
7032 sect_offset_str (reader.cu->per_cu->sect_off),
7033 objfile_name (per_objfile->objfile));
7034 }
7035
7036 if (reader.dummy_p)
7037 {
7038 /* Nothing. */
7039 }
7040 else if (this_cu->is_debug_types)
7041 build_type_psymtabs_reader (&reader, reader.info_ptr,
7042 reader.comp_unit_die);
7043 else if (want_partial_unit
7044 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7045 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7046 reader.comp_unit_die,
7047 pretend_language);
7048
7049 this_cu->lang = reader.cu->language;
7050
7051 /* Age out any secondary CUs. */
7052 per_objfile->age_comp_units ();
7053 }
7054
7055 /* Reader function for build_type_psymtabs. */
7056
7057 static void
7058 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7059 const gdb_byte *info_ptr,
7060 struct die_info *type_unit_die)
7061 {
7062 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7063 struct dwarf2_cu *cu = reader->cu;
7064 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7065 struct signatured_type *sig_type;
7066 struct type_unit_group *tu_group;
7067 struct attribute *attr;
7068 struct partial_die_info *first_die;
7069 CORE_ADDR lowpc, highpc;
7070 dwarf2_psymtab *pst;
7071
7072 gdb_assert (per_cu->is_debug_types);
7073 sig_type = (struct signatured_type *) per_cu;
7074
7075 if (! type_unit_die->has_children)
7076 return;
7077
7078 attr = type_unit_die->attr (DW_AT_stmt_list);
7079 tu_group = get_type_unit_group (cu, attr);
7080
7081 if (tu_group->tus == nullptr)
7082 tu_group->tus = new std::vector<signatured_type *>;
7083 tu_group->tus->push_back (sig_type);
7084
7085 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7086 pst = create_partial_symtab (per_cu, per_objfile, "");
7087 pst->anonymous = true;
7088
7089 first_die = load_partial_dies (reader, info_ptr, 1);
7090
7091 lowpc = (CORE_ADDR) -1;
7092 highpc = (CORE_ADDR) 0;
7093 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7094
7095 pst->end ();
7096 }
7097
7098 /* Struct used to sort TUs by their abbreviation table offset. */
7099
7100 struct tu_abbrev_offset
7101 {
7102 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7103 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7104 {}
7105
7106 /* This is used when sorting. */
7107 bool operator< (const tu_abbrev_offset &other) const
7108 {
7109 return abbrev_offset < other.abbrev_offset;
7110 }
7111
7112 signatured_type *sig_type;
7113 sect_offset abbrev_offset;
7114 };
7115
7116 /* Efficiently read all the type units.
7117
7118 The efficiency is because we sort TUs by the abbrev table they use and
7119 only read each abbrev table once. In one program there are 200K TUs
7120 sharing 8K abbrev tables.
7121
7122 The main purpose of this function is to support building the
7123 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7124 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7125 can collapse the search space by grouping them by stmt_list.
7126 The savings can be significant, in the same program from above the 200K TUs
7127 share 8K stmt_list tables.
7128
7129 FUNC is expected to call get_type_unit_group, which will create the
7130 struct type_unit_group if necessary and add it to
7131 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7132
7133 static void
7134 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7135 {
7136 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7137 abbrev_table_up abbrev_table;
7138 sect_offset abbrev_offset;
7139
7140 /* It's up to the caller to not call us multiple times. */
7141 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7142
7143 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7144 return;
7145
7146 /* TUs typically share abbrev tables, and there can be way more TUs than
7147 abbrev tables. Sort by abbrev table to reduce the number of times we
7148 read each abbrev table in.
7149 Alternatives are to punt or to maintain a cache of abbrev tables.
7150 This is simpler and efficient enough for now.
7151
7152 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7153 symtab to use). Typically TUs with the same abbrev offset have the same
7154 stmt_list value too so in practice this should work well.
7155
7156 The basic algorithm here is:
7157
7158 sort TUs by abbrev table
7159 for each TU with same abbrev table:
7160 read abbrev table if first user
7161 read TU top level DIE
7162 [IWBN if DWO skeletons had DW_AT_stmt_list]
7163 call FUNC */
7164
7165 dwarf_read_debug_printf ("Building type unit groups ...");
7166
7167 /* Sort in a separate table to maintain the order of all_comp_units
7168 for .gdb_index: TU indices directly index all_type_units. */
7169 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7170 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7171
7172 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7173 {
7174 if (cu->is_debug_types)
7175 {
7176 auto sig_type = static_cast<signatured_type *> (cu.get ());
7177 sorted_by_abbrev.emplace_back
7178 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7179 sig_type->sect_off));
7180 }
7181 }
7182
7183 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7184
7185 abbrev_offset = (sect_offset) ~(unsigned) 0;
7186
7187 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7188 {
7189 /* Switch to the next abbrev table if necessary. */
7190 if (abbrev_table == NULL
7191 || tu.abbrev_offset != abbrev_offset)
7192 {
7193 abbrev_offset = tu.abbrev_offset;
7194 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7195 abbrev_table =
7196 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7197 ++tu_stats->nr_uniq_abbrev_tables;
7198 }
7199
7200 cutu_reader reader (tu.sig_type, per_objfile,
7201 abbrev_table.get (), nullptr, false);
7202 if (!reader.dummy_p)
7203 build_type_psymtabs_reader (&reader, reader.info_ptr,
7204 reader.comp_unit_die);
7205 }
7206 }
7207
7208 /* Print collected type unit statistics. */
7209
7210 static void
7211 print_tu_stats (dwarf2_per_objfile *per_objfile)
7212 {
7213 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7214
7215 dwarf_read_debug_printf ("Type unit statistics:");
7216 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7217 dwarf_read_debug_printf (" %d uniq abbrev tables",
7218 tu_stats->nr_uniq_abbrev_tables);
7219 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7220 tu_stats->nr_symtabs);
7221 dwarf_read_debug_printf (" %d symtab sharers",
7222 tu_stats->nr_symtab_sharers);
7223 dwarf_read_debug_printf (" %d type units without a stmt_list",
7224 tu_stats->nr_stmt_less_type_units);
7225 dwarf_read_debug_printf (" %d all_type_units reallocs",
7226 tu_stats->nr_all_type_units_reallocs);
7227 }
7228
7229 /* Traversal function for build_type_psymtabs. */
7230
7231 static int
7232 build_type_psymtab_dependencies (void **slot, void *info)
7233 {
7234 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7235 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7236 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7237 dwarf2_psymtab *pst = tu_group->v.psymtab;
7238 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7239 int i;
7240
7241 gdb_assert (len > 0);
7242 gdb_assert (tu_group->type_unit_group_p ());
7243
7244 pst->number_of_dependencies = len;
7245 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7246 for (i = 0; i < len; ++i)
7247 {
7248 struct signatured_type *iter = tu_group->tus->at (i);
7249 gdb_assert (iter->is_debug_types);
7250 pst->dependencies[i] = iter->v.psymtab;
7251 iter->type_unit_group = tu_group;
7252 }
7253
7254 delete tu_group->tus;
7255 tu_group->tus = nullptr;
7256
7257 return 1;
7258 }
7259
7260 /* Traversal function for process_skeletonless_type_unit.
7261 Read a TU in a DWO file and build partial symbols for it. */
7262
7263 static int
7264 process_skeletonless_type_unit (void **slot, void *info)
7265 {
7266 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7267 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7268
7269 /* If this TU doesn't exist in the global table, add it and read it in. */
7270
7271 if (per_objfile->per_bfd->signatured_types == NULL)
7272 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7273
7274 signatured_type find_entry (dwo_unit->signature);
7275 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7276 &find_entry, INSERT);
7277 /* If we've already seen this type there's nothing to do. What's happening
7278 is we're doing our own version of comdat-folding here. */
7279 if (*slot != NULL)
7280 return 1;
7281
7282 /* This does the job that create_all_comp_units would have done for
7283 this TU. */
7284 signatured_type *entry
7285 = add_type_unit (per_objfile, dwo_unit->signature, slot);
7286 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7287 *slot = entry;
7288
7289 /* This does the job that build_type_psymtabs would have done. */
7290 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7291 if (!reader.dummy_p)
7292 build_type_psymtabs_reader (&reader, reader.info_ptr,
7293 reader.comp_unit_die);
7294
7295 return 1;
7296 }
7297
7298 /* Traversal function for process_skeletonless_type_units. */
7299
7300 static int
7301 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7302 {
7303 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7304
7305 if (dwo_file->tus != NULL)
7306 htab_traverse_noresize (dwo_file->tus.get (),
7307 process_skeletonless_type_unit, info);
7308
7309 return 1;
7310 }
7311
7312 /* Scan all TUs of DWO files, verifying we've processed them.
7313 This is needed in case a TU was emitted without its skeleton.
7314 Note: This can't be done until we know what all the DWO files are. */
7315
7316 static void
7317 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7318 {
7319 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7320 if (get_dwp_file (per_objfile) == NULL
7321 && per_objfile->per_bfd->dwo_files != NULL)
7322 {
7323 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7324 process_dwo_file_for_skeletonless_type_units,
7325 per_objfile);
7326 }
7327 }
7328
7329 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7330
7331 static void
7332 set_partial_user (dwarf2_per_objfile *per_objfile)
7333 {
7334 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7335 {
7336 dwarf2_psymtab *pst = per_cu->v.psymtab;
7337
7338 if (pst == NULL)
7339 continue;
7340
7341 for (int j = 0; j < pst->number_of_dependencies; ++j)
7342 {
7343 /* Set the 'user' field only if it is not already set. */
7344 if (pst->dependencies[j]->user == NULL)
7345 pst->dependencies[j]->user = pst;
7346 }
7347 }
7348 }
7349
7350 /* Build the partial symbol table by doing a quick pass through the
7351 .debug_info and .debug_abbrev sections. */
7352
7353 static void
7354 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7355 {
7356 struct objfile *objfile = per_objfile->objfile;
7357 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7358
7359 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7360 objfile_name (objfile));
7361
7362 scoped_restore restore_reading_psyms
7363 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7364
7365 per_bfd->info.read (objfile);
7366
7367 /* Any cached compilation units will be linked by the per-objfile
7368 read_in_chain. Make sure to free them when we're done. */
7369 free_cached_comp_units freer (per_objfile);
7370
7371 create_all_comp_units (per_objfile);
7372 build_type_psymtabs (per_objfile);
7373
7374 /* Create a temporary address map on a temporary obstack. We later
7375 copy this to the final obstack. */
7376 auto_obstack temp_obstack;
7377
7378 scoped_restore save_psymtabs_addrmap
7379 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7380 addrmap_create_mutable (&temp_obstack));
7381
7382 for (const auto &per_cu : per_bfd->all_comp_units)
7383 {
7384 if (per_cu->v.psymtab != NULL)
7385 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7386 continue;
7387 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7388 language_minimal);
7389 }
7390
7391 /* This has to wait until we read the CUs, we need the list of DWOs. */
7392 process_skeletonless_type_units (per_objfile);
7393
7394 /* Now that all TUs have been processed we can fill in the dependencies. */
7395 if (per_bfd->type_unit_groups != NULL)
7396 {
7397 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7398 build_type_psymtab_dependencies, per_objfile);
7399 }
7400
7401 if (dwarf_read_debug > 0)
7402 print_tu_stats (per_objfile);
7403
7404 set_partial_user (per_objfile);
7405
7406 per_bfd->partial_symtabs->psymtabs_addrmap
7407 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7408 per_bfd->partial_symtabs->obstack ());
7409 /* At this point we want to keep the address map. */
7410 save_psymtabs_addrmap.release ();
7411
7412 dwarf_read_debug_printf ("Done building psymtabs of %s",
7413 objfile_name (objfile));
7414 }
7415
7416 /* Load the partial DIEs for a secondary CU into memory.
7417 This is also used when rereading a primary CU with load_all_dies. */
7418
7419 static void
7420 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7421 dwarf2_per_objfile *per_objfile,
7422 dwarf2_cu *existing_cu)
7423 {
7424 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7425
7426 if (!reader.dummy_p)
7427 {
7428 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7429 language_minimal);
7430
7431 /* Check if comp unit has_children.
7432 If so, read the rest of the partial symbols from this comp unit.
7433 If not, there's no more debug_info for this comp unit. */
7434 if (reader.comp_unit_die->has_children)
7435 load_partial_dies (&reader, reader.info_ptr, 0);
7436
7437 reader.keep ();
7438 }
7439 }
7440
7441 static void
7442 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7443 struct dwarf2_section_info *section,
7444 struct dwarf2_section_info *abbrev_section,
7445 unsigned int is_dwz,
7446 htab_up &types_htab,
7447 rcuh_kind section_kind)
7448 {
7449 const gdb_byte *info_ptr;
7450 struct objfile *objfile = per_objfile->objfile;
7451
7452 dwarf_read_debug_printf ("Reading %s for %s",
7453 section->get_name (),
7454 section->get_file_name ());
7455
7456 section->read (objfile);
7457
7458 info_ptr = section->buffer;
7459
7460 while (info_ptr < section->buffer + section->size)
7461 {
7462 dwarf2_per_cu_data_up this_cu;
7463
7464 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7465
7466 comp_unit_head cu_header;
7467 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7468 abbrev_section, info_ptr,
7469 section_kind);
7470
7471 /* Save the compilation unit for later lookup. */
7472 if (cu_header.unit_type != DW_UT_type)
7473 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7474 else
7475 {
7476 if (types_htab == nullptr)
7477 types_htab = allocate_signatured_type_table ();
7478
7479 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7480 (cu_header.signature);
7481 signatured_type *sig_ptr = sig_type.get ();
7482 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7483 this_cu.reset (sig_type.release ());
7484
7485 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7486 gdb_assert (slot != nullptr);
7487 if (*slot != nullptr)
7488 complaint (_("debug type entry at offset %s is duplicate to"
7489 " the entry at offset %s, signature %s"),
7490 sect_offset_str (sect_off),
7491 sect_offset_str (sig_ptr->sect_off),
7492 hex_string (sig_ptr->signature));
7493 *slot = sig_ptr;
7494 }
7495 this_cu->sect_off = sect_off;
7496 this_cu->length = cu_header.length + cu_header.initial_length_size;
7497 this_cu->is_dwz = is_dwz;
7498 this_cu->section = section;
7499
7500 info_ptr = info_ptr + this_cu->length;
7501 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7502 }
7503 }
7504
7505 /* Create a list of all compilation units in OBJFILE.
7506 This is only done for -readnow and building partial symtabs. */
7507
7508 static void
7509 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7510 {
7511 htab_up types_htab;
7512
7513 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7514 &per_objfile->per_bfd->abbrev, 0,
7515 types_htab, rcuh_kind::COMPILE);
7516 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7517 read_comp_units_from_section (per_objfile, &section,
7518 &per_objfile->per_bfd->abbrev, 0,
7519 types_htab, rcuh_kind::TYPE);
7520
7521 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7522 if (dwz != NULL)
7523 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7524 types_htab, rcuh_kind::COMPILE);
7525
7526 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7527 }
7528
7529 /* Process all loaded DIEs for compilation unit CU, starting at
7530 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7531 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7532 DW_AT_ranges). See the comments of add_partial_subprogram on how
7533 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7534
7535 static void
7536 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7537 CORE_ADDR *highpc, int set_addrmap,
7538 struct dwarf2_cu *cu)
7539 {
7540 struct partial_die_info *pdi;
7541
7542 /* Now, march along the PDI's, descending into ones which have
7543 interesting children but skipping the children of the other ones,
7544 until we reach the end of the compilation unit. */
7545
7546 pdi = first_die;
7547
7548 while (pdi != NULL)
7549 {
7550 pdi->fixup (cu);
7551
7552 /* Anonymous namespaces or modules have no name but have interesting
7553 children, so we need to look at them. Ditto for anonymous
7554 enums. */
7555
7556 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7557 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7558 || pdi->tag == DW_TAG_imported_unit
7559 || pdi->tag == DW_TAG_inlined_subroutine)
7560 {
7561 switch (pdi->tag)
7562 {
7563 case DW_TAG_subprogram:
7564 case DW_TAG_inlined_subroutine:
7565 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7566 if (cu->language == language_cplus)
7567 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7568 set_addrmap, cu);
7569 break;
7570 case DW_TAG_constant:
7571 case DW_TAG_variable:
7572 case DW_TAG_typedef:
7573 case DW_TAG_union_type:
7574 if (!pdi->is_declaration
7575 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7576 {
7577 add_partial_symbol (pdi, cu);
7578 }
7579 break;
7580 case DW_TAG_class_type:
7581 case DW_TAG_interface_type:
7582 case DW_TAG_structure_type:
7583 if (!pdi->is_declaration)
7584 {
7585 add_partial_symbol (pdi, cu);
7586 }
7587 if ((cu->language == language_rust
7588 || cu->language == language_cplus) && pdi->has_children)
7589 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7590 set_addrmap, cu);
7591 break;
7592 case DW_TAG_enumeration_type:
7593 if (!pdi->is_declaration)
7594 add_partial_enumeration (pdi, cu);
7595 break;
7596 case DW_TAG_base_type:
7597 case DW_TAG_subrange_type:
7598 /* File scope base type definitions are added to the partial
7599 symbol table. */
7600 add_partial_symbol (pdi, cu);
7601 break;
7602 case DW_TAG_namespace:
7603 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7604 break;
7605 case DW_TAG_module:
7606 if (!pdi->is_declaration)
7607 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7608 break;
7609 case DW_TAG_imported_unit:
7610 {
7611 struct dwarf2_per_cu_data *per_cu;
7612
7613 /* For now we don't handle imported units in type units. */
7614 if (cu->per_cu->is_debug_types)
7615 {
7616 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7617 " supported in type units [in module %s]"),
7618 objfile_name (cu->per_objfile->objfile));
7619 }
7620
7621 per_cu = dwarf2_find_containing_comp_unit
7622 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
7623
7624 /* Go read the partial unit, if needed. */
7625 if (per_cu->v.psymtab == NULL)
7626 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7627 cu->language);
7628
7629 cu->per_cu->imported_symtabs_push (per_cu);
7630 }
7631 break;
7632 case DW_TAG_imported_declaration:
7633 add_partial_symbol (pdi, cu);
7634 break;
7635 default:
7636 break;
7637 }
7638 }
7639
7640 /* If the die has a sibling, skip to the sibling. */
7641
7642 pdi = pdi->die_sibling;
7643 }
7644 }
7645
7646 /* Functions used to compute the fully scoped name of a partial DIE.
7647
7648 Normally, this is simple. For C++, the parent DIE's fully scoped
7649 name is concatenated with "::" and the partial DIE's name.
7650 Enumerators are an exception; they use the scope of their parent
7651 enumeration type, i.e. the name of the enumeration type is not
7652 prepended to the enumerator.
7653
7654 There are two complexities. One is DW_AT_specification; in this
7655 case "parent" means the parent of the target of the specification,
7656 instead of the direct parent of the DIE. The other is compilers
7657 which do not emit DW_TAG_namespace; in this case we try to guess
7658 the fully qualified name of structure types from their members'
7659 linkage names. This must be done using the DIE's children rather
7660 than the children of any DW_AT_specification target. We only need
7661 to do this for structures at the top level, i.e. if the target of
7662 any DW_AT_specification (if any; otherwise the DIE itself) does not
7663 have a parent. */
7664
7665 /* Compute the scope prefix associated with PDI's parent, in
7666 compilation unit CU. The result will be allocated on CU's
7667 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7668 field. NULL is returned if no prefix is necessary. */
7669 static const char *
7670 partial_die_parent_scope (struct partial_die_info *pdi,
7671 struct dwarf2_cu *cu)
7672 {
7673 const char *grandparent_scope;
7674 struct partial_die_info *parent, *real_pdi;
7675
7676 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7677 then this means the parent of the specification DIE. */
7678
7679 real_pdi = pdi;
7680 while (real_pdi->has_specification)
7681 {
7682 auto res = find_partial_die (real_pdi->spec_offset,
7683 real_pdi->spec_is_dwz, cu);
7684 real_pdi = res.pdi;
7685 cu = res.cu;
7686 }
7687
7688 parent = real_pdi->die_parent;
7689 if (parent == NULL)
7690 return NULL;
7691
7692 if (parent->scope_set)
7693 return parent->scope;
7694
7695 parent->fixup (cu);
7696
7697 grandparent_scope = partial_die_parent_scope (parent, cu);
7698
7699 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7700 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7701 Work around this problem here. */
7702 if (cu->language == language_cplus
7703 && parent->tag == DW_TAG_namespace
7704 && strcmp (parent->name (cu), "::") == 0
7705 && grandparent_scope == NULL)
7706 {
7707 parent->scope = NULL;
7708 parent->scope_set = 1;
7709 return NULL;
7710 }
7711
7712 /* Nested subroutines in Fortran get a prefix. */
7713 if (pdi->tag == DW_TAG_enumerator)
7714 /* Enumerators should not get the name of the enumeration as a prefix. */
7715 parent->scope = grandparent_scope;
7716 else if (parent->tag == DW_TAG_namespace
7717 || parent->tag == DW_TAG_module
7718 || parent->tag == DW_TAG_structure_type
7719 || parent->tag == DW_TAG_class_type
7720 || parent->tag == DW_TAG_interface_type
7721 || parent->tag == DW_TAG_union_type
7722 || parent->tag == DW_TAG_enumeration_type
7723 || (cu->language == language_fortran
7724 && parent->tag == DW_TAG_subprogram
7725 && pdi->tag == DW_TAG_subprogram))
7726 {
7727 if (grandparent_scope == NULL)
7728 parent->scope = parent->name (cu);
7729 else
7730 parent->scope = typename_concat (&cu->comp_unit_obstack,
7731 grandparent_scope,
7732 parent->name (cu), 0, cu);
7733 }
7734 else
7735 {
7736 /* FIXME drow/2004-04-01: What should we be doing with
7737 function-local names? For partial symbols, we should probably be
7738 ignoring them. */
7739 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7740 dwarf_tag_name (parent->tag),
7741 sect_offset_str (pdi->sect_off));
7742 parent->scope = grandparent_scope;
7743 }
7744
7745 parent->scope_set = 1;
7746 return parent->scope;
7747 }
7748
7749 /* Return the fully scoped name associated with PDI, from compilation unit
7750 CU. The result will be allocated with malloc. */
7751
7752 static gdb::unique_xmalloc_ptr<char>
7753 partial_die_full_name (struct partial_die_info *pdi,
7754 struct dwarf2_cu *cu)
7755 {
7756 const char *parent_scope;
7757
7758 /* If this is a template instantiation, we can not work out the
7759 template arguments from partial DIEs. So, unfortunately, we have
7760 to go through the full DIEs. At least any work we do building
7761 types here will be reused if full symbols are loaded later. */
7762 if (pdi->has_template_arguments)
7763 {
7764 pdi->fixup (cu);
7765
7766 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
7767 {
7768 struct die_info *die;
7769 struct attribute attr;
7770 struct dwarf2_cu *ref_cu = cu;
7771
7772 /* DW_FORM_ref_addr is using section offset. */
7773 attr.name = (enum dwarf_attribute) 0;
7774 attr.form = DW_FORM_ref_addr;
7775 attr.u.unsnd = to_underlying (pdi->sect_off);
7776 die = follow_die_ref (NULL, &attr, &ref_cu);
7777
7778 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7779 }
7780 }
7781
7782 parent_scope = partial_die_parent_scope (pdi, cu);
7783 if (parent_scope == NULL)
7784 return NULL;
7785 else
7786 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7787 pdi->name (cu),
7788 0, cu));
7789 }
7790
7791 static void
7792 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7793 {
7794 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7795 struct objfile *objfile = per_objfile->objfile;
7796 struct gdbarch *gdbarch = objfile->arch ();
7797 CORE_ADDR addr = 0;
7798 const char *actual_name = NULL;
7799 CORE_ADDR baseaddr;
7800
7801 baseaddr = objfile->text_section_offset ();
7802
7803 gdb::unique_xmalloc_ptr<char> built_actual_name
7804 = partial_die_full_name (pdi, cu);
7805 if (built_actual_name != NULL)
7806 actual_name = built_actual_name.get ();
7807
7808 if (actual_name == NULL)
7809 actual_name = pdi->name (cu);
7810
7811 partial_symbol psymbol;
7812 memset (&psymbol, 0, sizeof (psymbol));
7813 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
7814 psymbol.ginfo.set_section_index (-1);
7815
7816 /* The code below indicates that the psymbol should be installed by
7817 setting this. */
7818 gdb::optional<psymbol_placement> where;
7819
7820 switch (pdi->tag)
7821 {
7822 case DW_TAG_inlined_subroutine:
7823 case DW_TAG_subprogram:
7824 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7825 - baseaddr);
7826 if (pdi->is_external
7827 || cu->language == language_ada
7828 || (cu->language == language_fortran
7829 && pdi->die_parent != NULL
7830 && pdi->die_parent->tag == DW_TAG_subprogram))
7831 {
7832 /* Normally, only "external" DIEs are part of the global scope.
7833 But in Ada and Fortran, we want to be able to access nested
7834 procedures globally. So all Ada and Fortran subprograms are
7835 stored in the global scope. */
7836 where = psymbol_placement::GLOBAL;
7837 }
7838 else
7839 where = psymbol_placement::STATIC;
7840
7841 psymbol.domain = VAR_DOMAIN;
7842 psymbol.aclass = LOC_BLOCK;
7843 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7844 psymbol.ginfo.value.address = addr;
7845
7846 if (pdi->main_subprogram && actual_name != NULL)
7847 set_objfile_main_name (objfile, actual_name, cu->language);
7848 break;
7849 case DW_TAG_constant:
7850 psymbol.domain = VAR_DOMAIN;
7851 psymbol.aclass = LOC_STATIC;
7852 where = (pdi->is_external
7853 ? psymbol_placement::GLOBAL
7854 : psymbol_placement::STATIC);
7855 break;
7856 case DW_TAG_variable:
7857 if (pdi->d.locdesc)
7858 addr = decode_locdesc (pdi->d.locdesc, cu);
7859
7860 if (pdi->d.locdesc
7861 && addr == 0
7862 && !per_objfile->per_bfd->has_section_at_zero)
7863 {
7864 /* A global or static variable may also have been stripped
7865 out by the linker if unused, in which case its address
7866 will be nullified; do not add such variables into partial
7867 symbol table then. */
7868 }
7869 else if (pdi->is_external)
7870 {
7871 /* Global Variable.
7872 Don't enter into the minimal symbol tables as there is
7873 a minimal symbol table entry from the ELF symbols already.
7874 Enter into partial symbol table if it has a location
7875 descriptor or a type.
7876 If the location descriptor is missing, new_symbol will create
7877 a LOC_UNRESOLVED symbol, the address of the variable will then
7878 be determined from the minimal symbol table whenever the variable
7879 is referenced.
7880 The address for the partial symbol table entry is not
7881 used by GDB, but it comes in handy for debugging partial symbol
7882 table building. */
7883
7884 if (pdi->d.locdesc || pdi->has_type)
7885 {
7886 psymbol.domain = VAR_DOMAIN;
7887 psymbol.aclass = LOC_STATIC;
7888 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7889 psymbol.ginfo.value.address = addr;
7890 where = psymbol_placement::GLOBAL;
7891 }
7892 }
7893 else
7894 {
7895 int has_loc = pdi->d.locdesc != NULL;
7896
7897 /* Static Variable. Skip symbols whose value we cannot know (those
7898 without location descriptors or constant values). */
7899 if (!has_loc && !pdi->has_const_value)
7900 return;
7901
7902 psymbol.domain = VAR_DOMAIN;
7903 psymbol.aclass = LOC_STATIC;
7904 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7905 if (has_loc)
7906 psymbol.ginfo.value.address = addr;
7907 where = psymbol_placement::STATIC;
7908 }
7909 break;
7910 case DW_TAG_array_type:
7911 case DW_TAG_typedef:
7912 case DW_TAG_base_type:
7913 case DW_TAG_subrange_type:
7914 psymbol.domain = VAR_DOMAIN;
7915 psymbol.aclass = LOC_TYPEDEF;
7916 where = psymbol_placement::STATIC;
7917 break;
7918 case DW_TAG_imported_declaration:
7919 case DW_TAG_namespace:
7920 psymbol.domain = VAR_DOMAIN;
7921 psymbol.aclass = LOC_TYPEDEF;
7922 where = psymbol_placement::GLOBAL;
7923 break;
7924 case DW_TAG_module:
7925 /* With Fortran 77 there might be a "BLOCK DATA" module
7926 available without any name. If so, we skip the module as it
7927 doesn't bring any value. */
7928 if (actual_name != nullptr)
7929 {
7930 psymbol.domain = MODULE_DOMAIN;
7931 psymbol.aclass = LOC_TYPEDEF;
7932 where = psymbol_placement::GLOBAL;
7933 }
7934 break;
7935 case DW_TAG_class_type:
7936 case DW_TAG_interface_type:
7937 case DW_TAG_structure_type:
7938 case DW_TAG_union_type:
7939 case DW_TAG_enumeration_type:
7940 /* Skip external references. The DWARF standard says in the section
7941 about "Structure, Union, and Class Type Entries": "An incomplete
7942 structure, union or class type is represented by a structure,
7943 union or class entry that does not have a byte size attribute
7944 and that has a DW_AT_declaration attribute." */
7945 if (!pdi->has_byte_size && pdi->is_declaration)
7946 return;
7947
7948 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7949 static vs. global. */
7950 psymbol.domain = STRUCT_DOMAIN;
7951 psymbol.aclass = LOC_TYPEDEF;
7952 where = (cu->language == language_cplus
7953 ? psymbol_placement::GLOBAL
7954 : psymbol_placement::STATIC);
7955 break;
7956 case DW_TAG_enumerator:
7957 psymbol.domain = VAR_DOMAIN;
7958 psymbol.aclass = LOC_CONST;
7959 where = (cu->language == language_cplus
7960 ? psymbol_placement::GLOBAL
7961 : psymbol_placement::STATIC);
7962 break;
7963 default:
7964 break;
7965 }
7966
7967 if (where.has_value ())
7968 {
7969 if (built_actual_name != nullptr)
7970 actual_name = objfile->intern (actual_name);
7971 if (pdi->linkage_name == nullptr || cu->language == language_ada)
7972 psymbol.ginfo.set_linkage_name (actual_name);
7973 else
7974 {
7975 psymbol.ginfo.set_demangled_name (actual_name,
7976 &objfile->objfile_obstack);
7977 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
7978 }
7979 cu->per_cu->v.psymtab->add_psymbol
7980 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
7981 objfile);
7982 }
7983 }
7984
7985 /* Read a partial die corresponding to a namespace; also, add a symbol
7986 corresponding to that namespace to the symbol table. NAMESPACE is
7987 the name of the enclosing namespace. */
7988
7989 static void
7990 add_partial_namespace (struct partial_die_info *pdi,
7991 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7992 int set_addrmap, struct dwarf2_cu *cu)
7993 {
7994 /* Add a symbol for the namespace. */
7995
7996 add_partial_symbol (pdi, cu);
7997
7998 /* Now scan partial symbols in that namespace. */
7999
8000 if (pdi->has_children)
8001 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8002 }
8003
8004 /* Read a partial die corresponding to a Fortran module. */
8005
8006 static void
8007 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8008 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8009 {
8010 /* Add a symbol for the namespace. */
8011
8012 add_partial_symbol (pdi, cu);
8013
8014 /* Now scan partial symbols in that module. */
8015
8016 if (pdi->has_children)
8017 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8018 }
8019
8020 /* Read a partial die corresponding to a subprogram or an inlined
8021 subprogram and create a partial symbol for that subprogram.
8022 When the CU language allows it, this routine also defines a partial
8023 symbol for each nested subprogram that this subprogram contains.
8024 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8025 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8026
8027 PDI may also be a lexical block, in which case we simply search
8028 recursively for subprograms defined inside that lexical block.
8029 Again, this is only performed when the CU language allows this
8030 type of definitions. */
8031
8032 static void
8033 add_partial_subprogram (struct partial_die_info *pdi,
8034 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8035 int set_addrmap, struct dwarf2_cu *cu)
8036 {
8037 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8038 {
8039 if (pdi->has_pc_info)
8040 {
8041 if (pdi->lowpc < *lowpc)
8042 *lowpc = pdi->lowpc;
8043 if (pdi->highpc > *highpc)
8044 *highpc = pdi->highpc;
8045 if (set_addrmap)
8046 {
8047 struct objfile *objfile = cu->per_objfile->objfile;
8048 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8049 struct gdbarch *gdbarch = objfile->arch ();
8050 CORE_ADDR baseaddr;
8051 CORE_ADDR this_highpc;
8052 CORE_ADDR this_lowpc;
8053
8054 baseaddr = objfile->text_section_offset ();
8055 this_lowpc
8056 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8057 pdi->lowpc + baseaddr)
8058 - baseaddr);
8059 this_highpc
8060 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8061 pdi->highpc + baseaddr)
8062 - baseaddr);
8063 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8064 this_lowpc, this_highpc - 1,
8065 cu->per_cu->v.psymtab);
8066 }
8067 }
8068
8069 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8070 {
8071 if (!pdi->is_declaration)
8072 /* Ignore subprogram DIEs that do not have a name, they are
8073 illegal. Do not emit a complaint at this point, we will
8074 do so when we convert this psymtab into a symtab. */
8075 if (pdi->name (cu))
8076 add_partial_symbol (pdi, cu);
8077 }
8078 }
8079
8080 if (! pdi->has_children)
8081 return;
8082
8083 if (cu->language == language_ada || cu->language == 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->language == 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->language == 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 cu->language = pretend_language;
9294 cu->language_defn = language_def (cu->language);
9295
9296 dwarf2_find_base_address (cu->dies, cu);
9297
9298 /* Before we start reading the top-level DIE, ensure it has a valid tag
9299 type. */
9300 switch (cu->dies->tag)
9301 {
9302 case DW_TAG_compile_unit:
9303 case DW_TAG_partial_unit:
9304 case DW_TAG_type_unit:
9305 break;
9306 default:
9307 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9308 dwarf_tag_name (cu->dies->tag),
9309 sect_offset_str (cu->per_cu->sect_off),
9310 objfile_name (per_objfile->objfile));
9311 }
9312
9313 /* Do line number decoding in read_file_scope () */
9314 process_die (cu->dies, cu);
9315
9316 /* For now fudge the Go package. */
9317 if (cu->language == language_go)
9318 fixup_go_packaging (cu);
9319
9320 /* Now that we have processed all the DIEs in the CU, all the types
9321 should be complete, and it should now be safe to compute all of the
9322 physnames. */
9323 compute_delayed_physnames (cu);
9324
9325 if (cu->language == language_rust)
9326 rust_union_quirks (cu);
9327
9328 /* Some compilers don't define a DW_AT_high_pc attribute for the
9329 compilation unit. If the DW_AT_high_pc is missing, synthesize
9330 it, by scanning the DIE's below the compilation unit. */
9331 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9332
9333 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9334 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9335
9336 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9337 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9338 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9339 addrmap to help ensure it has an accurate map of pc values belonging to
9340 this comp unit. */
9341 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9342
9343 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9344 SECT_OFF_TEXT (objfile),
9345 0);
9346
9347 if (cust != NULL)
9348 {
9349 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9350
9351 /* Set symtab language to language from DW_AT_language. If the
9352 compilation is from a C file generated by language preprocessors, do
9353 not set the language if it was already deduced by start_subfile. */
9354 if (!(cu->language == language_c
9355 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9356 COMPUNIT_FILETABS (cust)->language = cu->language;
9357
9358 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9359 produce DW_AT_location with location lists but it can be possibly
9360 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9361 there were bugs in prologue debug info, fixed later in GCC-4.5
9362 by "unwind info for epilogues" patch (which is not directly related).
9363
9364 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9365 needed, it would be wrong due to missing DW_AT_producer there.
9366
9367 Still one can confuse GDB by using non-standard GCC compilation
9368 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9369 */
9370 if (cu->has_loclist && gcc_4_minor >= 5)
9371 cust->locations_valid = 1;
9372
9373 if (gcc_4_minor >= 5)
9374 cust->epilogue_unwind_valid = 1;
9375
9376 cust->call_site_htab = cu->call_site_htab;
9377 }
9378
9379 per_objfile->set_symtab (cu->per_cu, cust);
9380
9381 /* Push it for inclusion processing later. */
9382 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9383
9384 /* Not needed any more. */
9385 cu->reset_builder ();
9386 }
9387
9388 /* Generate full symbol information for type unit CU, whose DIEs have
9389 already been loaded into memory. */
9390
9391 static void
9392 process_full_type_unit (dwarf2_cu *cu,
9393 enum language pretend_language)
9394 {
9395 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9396 struct objfile *objfile = per_objfile->objfile;
9397 struct compunit_symtab *cust;
9398 struct signatured_type *sig_type;
9399
9400 gdb_assert (cu->per_cu->is_debug_types);
9401 sig_type = (struct signatured_type *) cu->per_cu;
9402
9403 /* Clear the list here in case something was left over. */
9404 cu->method_list.clear ();
9405
9406 cu->language = pretend_language;
9407 cu->language_defn = language_def (cu->language);
9408
9409 /* The symbol tables are set up in read_type_unit_scope. */
9410 process_die (cu->dies, cu);
9411
9412 /* For now fudge the Go package. */
9413 if (cu->language == language_go)
9414 fixup_go_packaging (cu);
9415
9416 /* Now that we have processed all the DIEs in the CU, all the types
9417 should be complete, and it should now be safe to compute all of the
9418 physnames. */
9419 compute_delayed_physnames (cu);
9420
9421 if (cu->language == language_rust)
9422 rust_union_quirks (cu);
9423
9424 /* TUs share symbol tables.
9425 If this is the first TU to use this symtab, complete the construction
9426 of it with end_expandable_symtab. Otherwise, complete the addition of
9427 this TU's symbols to the existing symtab. */
9428 type_unit_group_unshareable *tug_unshare =
9429 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9430 if (tug_unshare->compunit_symtab == NULL)
9431 {
9432 buildsym_compunit *builder = cu->get_builder ();
9433 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9434 tug_unshare->compunit_symtab = cust;
9435
9436 if (cust != NULL)
9437 {
9438 /* Set symtab language to language from DW_AT_language. If the
9439 compilation is from a C file generated by language preprocessors,
9440 do not set the language if it was already deduced by
9441 start_subfile. */
9442 if (!(cu->language == language_c
9443 && COMPUNIT_FILETABS (cust)->language != language_c))
9444 COMPUNIT_FILETABS (cust)->language = cu->language;
9445 }
9446 }
9447 else
9448 {
9449 cu->get_builder ()->augment_type_symtab ();
9450 cust = tug_unshare->compunit_symtab;
9451 }
9452
9453 per_objfile->set_symtab (cu->per_cu, cust);
9454
9455 /* Not needed any more. */
9456 cu->reset_builder ();
9457 }
9458
9459 /* Process an imported unit DIE. */
9460
9461 static void
9462 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9463 {
9464 struct attribute *attr;
9465
9466 /* For now we don't handle imported units in type units. */
9467 if (cu->per_cu->is_debug_types)
9468 {
9469 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9470 " supported in type units [in module %s]"),
9471 objfile_name (cu->per_objfile->objfile));
9472 }
9473
9474 attr = dwarf2_attr (die, DW_AT_import, cu);
9475 if (attr != NULL)
9476 {
9477 sect_offset sect_off = attr->get_ref_die_offset ();
9478 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9479 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9480 dwarf2_per_cu_data *per_cu
9481 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9482
9483 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9484 into another compilation unit, at root level. Regard this as a hint,
9485 and ignore it. */
9486 if (die->parent && die->parent->parent == NULL
9487 && per_cu->unit_type == DW_UT_compile
9488 && per_cu->lang == language_cplus)
9489 return;
9490
9491 /* If necessary, add it to the queue and load its DIEs. */
9492 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9493 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9494 false, cu->language);
9495
9496 cu->per_cu->imported_symtabs_push (per_cu);
9497 }
9498 }
9499
9500 /* RAII object that represents a process_die scope: i.e.,
9501 starts/finishes processing a DIE. */
9502 class process_die_scope
9503 {
9504 public:
9505 process_die_scope (die_info *die, dwarf2_cu *cu)
9506 : m_die (die), m_cu (cu)
9507 {
9508 /* We should only be processing DIEs not already in process. */
9509 gdb_assert (!m_die->in_process);
9510 m_die->in_process = true;
9511 }
9512
9513 ~process_die_scope ()
9514 {
9515 m_die->in_process = false;
9516
9517 /* If we're done processing the DIE for the CU that owns the line
9518 header, we don't need the line header anymore. */
9519 if (m_cu->line_header_die_owner == m_die)
9520 {
9521 delete m_cu->line_header;
9522 m_cu->line_header = NULL;
9523 m_cu->line_header_die_owner = NULL;
9524 }
9525 }
9526
9527 private:
9528 die_info *m_die;
9529 dwarf2_cu *m_cu;
9530 };
9531
9532 /* Process a die and its children. */
9533
9534 static void
9535 process_die (struct die_info *die, struct dwarf2_cu *cu)
9536 {
9537 process_die_scope scope (die, cu);
9538
9539 switch (die->tag)
9540 {
9541 case DW_TAG_padding:
9542 break;
9543 case DW_TAG_compile_unit:
9544 case DW_TAG_partial_unit:
9545 read_file_scope (die, cu);
9546 break;
9547 case DW_TAG_type_unit:
9548 read_type_unit_scope (die, cu);
9549 break;
9550 case DW_TAG_subprogram:
9551 /* Nested subprograms in Fortran get a prefix. */
9552 if (cu->language == language_fortran
9553 && die->parent != NULL
9554 && die->parent->tag == DW_TAG_subprogram)
9555 cu->processing_has_namespace_info = true;
9556 /* Fall through. */
9557 case DW_TAG_inlined_subroutine:
9558 read_func_scope (die, cu);
9559 break;
9560 case DW_TAG_lexical_block:
9561 case DW_TAG_try_block:
9562 case DW_TAG_catch_block:
9563 read_lexical_block_scope (die, cu);
9564 break;
9565 case DW_TAG_call_site:
9566 case DW_TAG_GNU_call_site:
9567 read_call_site_scope (die, cu);
9568 break;
9569 case DW_TAG_class_type:
9570 case DW_TAG_interface_type:
9571 case DW_TAG_structure_type:
9572 case DW_TAG_union_type:
9573 process_structure_scope (die, cu);
9574 break;
9575 case DW_TAG_enumeration_type:
9576 process_enumeration_scope (die, cu);
9577 break;
9578
9579 /* These dies have a type, but processing them does not create
9580 a symbol or recurse to process the children. Therefore we can
9581 read them on-demand through read_type_die. */
9582 case DW_TAG_subroutine_type:
9583 case DW_TAG_set_type:
9584 case DW_TAG_pointer_type:
9585 case DW_TAG_ptr_to_member_type:
9586 case DW_TAG_reference_type:
9587 case DW_TAG_rvalue_reference_type:
9588 case DW_TAG_string_type:
9589 break;
9590
9591 case DW_TAG_array_type:
9592 /* We only need to handle this case for Ada -- in other
9593 languages, it's normal for the compiler to emit a typedef
9594 instead. */
9595 if (cu->language != language_ada)
9596 break;
9597 /* FALLTHROUGH */
9598 case DW_TAG_base_type:
9599 case DW_TAG_subrange_type:
9600 case DW_TAG_typedef:
9601 /* Add a typedef symbol for the type definition, if it has a
9602 DW_AT_name. */
9603 new_symbol (die, read_type_die (die, cu), cu);
9604 break;
9605 case DW_TAG_common_block:
9606 read_common_block (die, cu);
9607 break;
9608 case DW_TAG_common_inclusion:
9609 break;
9610 case DW_TAG_namespace:
9611 cu->processing_has_namespace_info = true;
9612 read_namespace (die, cu);
9613 break;
9614 case DW_TAG_module:
9615 cu->processing_has_namespace_info = true;
9616 read_module (die, cu);
9617 break;
9618 case DW_TAG_imported_declaration:
9619 cu->processing_has_namespace_info = true;
9620 if (read_namespace_alias (die, cu))
9621 break;
9622 /* The declaration is not a global namespace alias. */
9623 /* Fall through. */
9624 case DW_TAG_imported_module:
9625 cu->processing_has_namespace_info = true;
9626 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9627 || cu->language != language_fortran))
9628 complaint (_("Tag '%s' has unexpected children"),
9629 dwarf_tag_name (die->tag));
9630 read_import_statement (die, cu);
9631 break;
9632
9633 case DW_TAG_imported_unit:
9634 process_imported_unit_die (die, cu);
9635 break;
9636
9637 case DW_TAG_variable:
9638 read_variable (die, cu);
9639 break;
9640
9641 default:
9642 new_symbol (die, NULL, cu);
9643 break;
9644 }
9645 }
9646 \f
9647 /* DWARF name computation. */
9648
9649 /* A helper function for dwarf2_compute_name which determines whether DIE
9650 needs to have the name of the scope prepended to the name listed in the
9651 die. */
9652
9653 static int
9654 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9655 {
9656 struct attribute *attr;
9657
9658 switch (die->tag)
9659 {
9660 case DW_TAG_namespace:
9661 case DW_TAG_typedef:
9662 case DW_TAG_class_type:
9663 case DW_TAG_interface_type:
9664 case DW_TAG_structure_type:
9665 case DW_TAG_union_type:
9666 case DW_TAG_enumeration_type:
9667 case DW_TAG_enumerator:
9668 case DW_TAG_subprogram:
9669 case DW_TAG_inlined_subroutine:
9670 case DW_TAG_member:
9671 case DW_TAG_imported_declaration:
9672 return 1;
9673
9674 case DW_TAG_variable:
9675 case DW_TAG_constant:
9676 /* We only need to prefix "globally" visible variables. These include
9677 any variable marked with DW_AT_external or any variable that
9678 lives in a namespace. [Variables in anonymous namespaces
9679 require prefixing, but they are not DW_AT_external.] */
9680
9681 if (dwarf2_attr (die, DW_AT_specification, cu))
9682 {
9683 struct dwarf2_cu *spec_cu = cu;
9684
9685 return die_needs_namespace (die_specification (die, &spec_cu),
9686 spec_cu);
9687 }
9688
9689 attr = dwarf2_attr (die, DW_AT_external, cu);
9690 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9691 && die->parent->tag != DW_TAG_module)
9692 return 0;
9693 /* A variable in a lexical block of some kind does not need a
9694 namespace, even though in C++ such variables may be external
9695 and have a mangled name. */
9696 if (die->parent->tag == DW_TAG_lexical_block
9697 || die->parent->tag == DW_TAG_try_block
9698 || die->parent->tag == DW_TAG_catch_block
9699 || die->parent->tag == DW_TAG_subprogram)
9700 return 0;
9701 return 1;
9702
9703 default:
9704 return 0;
9705 }
9706 }
9707
9708 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9709 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9710 defined for the given DIE. */
9711
9712 static struct attribute *
9713 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9714 {
9715 struct attribute *attr;
9716
9717 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9718 if (attr == NULL)
9719 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9720
9721 return attr;
9722 }
9723
9724 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9725 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9726 defined for the given DIE. */
9727
9728 static const char *
9729 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9730 {
9731 const char *linkage_name;
9732
9733 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9734 if (linkage_name == NULL)
9735 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9736
9737 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9738 See https://github.com/rust-lang/rust/issues/32925. */
9739 if (cu->language == language_rust && linkage_name != NULL
9740 && strchr (linkage_name, '{') != NULL)
9741 linkage_name = NULL;
9742
9743 return linkage_name;
9744 }
9745
9746 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9747 compute the physname for the object, which include a method's:
9748 - formal parameters (C++),
9749 - receiver type (Go),
9750
9751 The term "physname" is a bit confusing.
9752 For C++, for example, it is the demangled name.
9753 For Go, for example, it's the mangled name.
9754
9755 For Ada, return the DIE's linkage name rather than the fully qualified
9756 name. PHYSNAME is ignored..
9757
9758 The result is allocated on the objfile->per_bfd's obstack and
9759 canonicalized. */
9760
9761 static const char *
9762 dwarf2_compute_name (const char *name,
9763 struct die_info *die, struct dwarf2_cu *cu,
9764 int physname)
9765 {
9766 struct objfile *objfile = cu->per_objfile->objfile;
9767
9768 if (name == NULL)
9769 name = dwarf2_name (die, cu);
9770
9771 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9772 but otherwise compute it by typename_concat inside GDB.
9773 FIXME: Actually this is not really true, or at least not always true.
9774 It's all very confusing. compute_and_set_names doesn't try to demangle
9775 Fortran names because there is no mangling standard. So new_symbol
9776 will set the demangled name to the result of dwarf2_full_name, and it is
9777 the demangled name that GDB uses if it exists. */
9778 if (cu->language == language_ada
9779 || (cu->language == language_fortran && physname))
9780 {
9781 /* For Ada unit, we prefer the linkage name over the name, as
9782 the former contains the exported name, which the user expects
9783 to be able to reference. Ideally, we want the user to be able
9784 to reference this entity using either natural or linkage name,
9785 but we haven't started looking at this enhancement yet. */
9786 const char *linkage_name = dw2_linkage_name (die, cu);
9787
9788 if (linkage_name != NULL)
9789 return linkage_name;
9790 }
9791
9792 /* These are the only languages we know how to qualify names in. */
9793 if (name != NULL
9794 && (cu->language == language_cplus
9795 || cu->language == language_fortran || cu->language == language_d
9796 || cu->language == language_rust))
9797 {
9798 if (die_needs_namespace (die, cu))
9799 {
9800 const char *prefix;
9801 const char *canonical_name = NULL;
9802
9803 string_file buf;
9804
9805 prefix = determine_prefix (die, cu);
9806 if (*prefix != '\0')
9807 {
9808 gdb::unique_xmalloc_ptr<char> prefixed_name
9809 (typename_concat (NULL, prefix, name, physname, cu));
9810
9811 buf.puts (prefixed_name.get ());
9812 }
9813 else
9814 buf.puts (name);
9815
9816 /* Template parameters may be specified in the DIE's DW_AT_name, or
9817 as children with DW_TAG_template_type_param or
9818 DW_TAG_value_type_param. If the latter, add them to the name
9819 here. If the name already has template parameters, then
9820 skip this step; some versions of GCC emit both, and
9821 it is more efficient to use the pre-computed name.
9822
9823 Something to keep in mind about this process: it is very
9824 unlikely, or in some cases downright impossible, to produce
9825 something that will match the mangled name of a function.
9826 If the definition of the function has the same debug info,
9827 we should be able to match up with it anyway. But fallbacks
9828 using the minimal symbol, for instance to find a method
9829 implemented in a stripped copy of libstdc++, will not work.
9830 If we do not have debug info for the definition, we will have to
9831 match them up some other way.
9832
9833 When we do name matching there is a related problem with function
9834 templates; two instantiated function templates are allowed to
9835 differ only by their return types, which we do not add here. */
9836
9837 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9838 {
9839 struct attribute *attr;
9840 struct die_info *child;
9841 int first = 1;
9842 const language_defn *cplus_lang = language_def (cu->language);
9843
9844 die->building_fullname = 1;
9845
9846 for (child = die->child; child != NULL; child = child->sibling)
9847 {
9848 struct type *type;
9849 LONGEST value;
9850 const gdb_byte *bytes;
9851 struct dwarf2_locexpr_baton *baton;
9852 struct value *v;
9853
9854 if (child->tag != DW_TAG_template_type_param
9855 && child->tag != DW_TAG_template_value_param)
9856 continue;
9857
9858 if (first)
9859 {
9860 buf.puts ("<");
9861 first = 0;
9862 }
9863 else
9864 buf.puts (", ");
9865
9866 attr = dwarf2_attr (child, DW_AT_type, cu);
9867 if (attr == NULL)
9868 {
9869 complaint (_("template parameter missing DW_AT_type"));
9870 buf.puts ("UNKNOWN_TYPE");
9871 continue;
9872 }
9873 type = die_type (child, cu);
9874
9875 if (child->tag == DW_TAG_template_type_param)
9876 {
9877 cplus_lang->print_type (type, "", &buf, -1, 0,
9878 &type_print_raw_options);
9879 continue;
9880 }
9881
9882 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9883 if (attr == NULL)
9884 {
9885 complaint (_("template parameter missing "
9886 "DW_AT_const_value"));
9887 buf.puts ("UNKNOWN_VALUE");
9888 continue;
9889 }
9890
9891 dwarf2_const_value_attr (attr, type, name,
9892 &cu->comp_unit_obstack, cu,
9893 &value, &bytes, &baton);
9894
9895 if (type->has_no_signedness ())
9896 /* GDB prints characters as NUMBER 'CHAR'. If that's
9897 changed, this can use value_print instead. */
9898 cplus_lang->printchar (value, type, &buf);
9899 else
9900 {
9901 struct value_print_options opts;
9902
9903 if (baton != NULL)
9904 v = dwarf2_evaluate_loc_desc (type, NULL,
9905 baton->data,
9906 baton->size,
9907 baton->per_cu,
9908 baton->per_objfile);
9909 else if (bytes != NULL)
9910 {
9911 v = allocate_value (type);
9912 memcpy (value_contents_writeable (v), bytes,
9913 TYPE_LENGTH (type));
9914 }
9915 else
9916 v = value_from_longest (type, value);
9917
9918 /* Specify decimal so that we do not depend on
9919 the radix. */
9920 get_formatted_print_options (&opts, 'd');
9921 opts.raw = 1;
9922 value_print (v, &buf, &opts);
9923 release_value (v);
9924 }
9925 }
9926
9927 die->building_fullname = 0;
9928
9929 if (!first)
9930 {
9931 /* Close the argument list, with a space if necessary
9932 (nested templates). */
9933 if (!buf.empty () && buf.string ().back () == '>')
9934 buf.puts (" >");
9935 else
9936 buf.puts (">");
9937 }
9938 }
9939
9940 /* For C++ methods, append formal parameter type
9941 information, if PHYSNAME. */
9942
9943 if (physname && die->tag == DW_TAG_subprogram
9944 && cu->language == language_cplus)
9945 {
9946 struct type *type = read_type_die (die, cu);
9947
9948 c_type_print_args (type, &buf, 1, cu->language,
9949 &type_print_raw_options);
9950
9951 if (cu->language == language_cplus)
9952 {
9953 /* Assume that an artificial first parameter is
9954 "this", but do not crash if it is not. RealView
9955 marks unnamed (and thus unused) parameters as
9956 artificial; there is no way to differentiate
9957 the two cases. */
9958 if (type->num_fields () > 0
9959 && TYPE_FIELD_ARTIFICIAL (type, 0)
9960 && type->field (0).type ()->code () == TYPE_CODE_PTR
9961 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
9962 buf.puts (" const");
9963 }
9964 }
9965
9966 const std::string &intermediate_name = buf.string ();
9967
9968 if (cu->language == language_cplus)
9969 canonical_name
9970 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9971 objfile);
9972
9973 /* If we only computed INTERMEDIATE_NAME, or if
9974 INTERMEDIATE_NAME is already canonical, then we need to
9975 intern it. */
9976 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9977 name = objfile->intern (intermediate_name);
9978 else
9979 name = canonical_name;
9980 }
9981 }
9982
9983 return name;
9984 }
9985
9986 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9987 If scope qualifiers are appropriate they will be added. The result
9988 will be allocated on the storage_obstack, or NULL if the DIE does
9989 not have a name. NAME may either be from a previous call to
9990 dwarf2_name or NULL.
9991
9992 The output string will be canonicalized (if C++). */
9993
9994 static const char *
9995 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9996 {
9997 return dwarf2_compute_name (name, die, cu, 0);
9998 }
9999
10000 /* Construct a physname for the given DIE in CU. NAME may either be
10001 from a previous call to dwarf2_name or NULL. The result will be
10002 allocated on the objfile_objstack or NULL if the DIE does not have a
10003 name.
10004
10005 The output string will be canonicalized (if C++). */
10006
10007 static const char *
10008 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10009 {
10010 struct objfile *objfile = cu->per_objfile->objfile;
10011 const char *retval, *mangled = NULL, *canon = NULL;
10012 int need_copy = 1;
10013
10014 /* In this case dwarf2_compute_name is just a shortcut not building anything
10015 on its own. */
10016 if (!die_needs_namespace (die, cu))
10017 return dwarf2_compute_name (name, die, cu, 1);
10018
10019 if (cu->language != language_rust)
10020 mangled = dw2_linkage_name (die, cu);
10021
10022 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10023 has computed. */
10024 gdb::unique_xmalloc_ptr<char> demangled;
10025 if (mangled != NULL)
10026 {
10027
10028 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10029 {
10030 /* Do nothing (do not demangle the symbol name). */
10031 }
10032 else
10033 {
10034 /* Use DMGL_RET_DROP for C++ template functions to suppress
10035 their return type. It is easier for GDB users to search
10036 for such functions as `name(params)' than `long name(params)'.
10037 In such case the minimal symbol names do not match the full
10038 symbol names but for template functions there is never a need
10039 to look up their definition from their declaration so
10040 the only disadvantage remains the minimal symbol variant
10041 `long name(params)' does not have the proper inferior type. */
10042 demangled.reset (gdb_demangle (mangled,
10043 (DMGL_PARAMS | DMGL_ANSI
10044 | DMGL_RET_DROP)));
10045 }
10046 if (demangled)
10047 canon = demangled.get ();
10048 else
10049 {
10050 canon = mangled;
10051 need_copy = 0;
10052 }
10053 }
10054
10055 if (canon == NULL || check_physname)
10056 {
10057 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10058
10059 if (canon != NULL && strcmp (physname, canon) != 0)
10060 {
10061 /* It may not mean a bug in GDB. The compiler could also
10062 compute DW_AT_linkage_name incorrectly. But in such case
10063 GDB would need to be bug-to-bug compatible. */
10064
10065 complaint (_("Computed physname <%s> does not match demangled <%s> "
10066 "(from linkage <%s>) - DIE at %s [in module %s]"),
10067 physname, canon, mangled, sect_offset_str (die->sect_off),
10068 objfile_name (objfile));
10069
10070 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10071 is available here - over computed PHYSNAME. It is safer
10072 against both buggy GDB and buggy compilers. */
10073
10074 retval = canon;
10075 }
10076 else
10077 {
10078 retval = physname;
10079 need_copy = 0;
10080 }
10081 }
10082 else
10083 retval = canon;
10084
10085 if (need_copy)
10086 retval = objfile->intern (retval);
10087
10088 return retval;
10089 }
10090
10091 /* Inspect DIE in CU for a namespace alias. If one exists, record
10092 a new symbol for it.
10093
10094 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10095
10096 static int
10097 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10098 {
10099 struct attribute *attr;
10100
10101 /* If the die does not have a name, this is not a namespace
10102 alias. */
10103 attr = dwarf2_attr (die, DW_AT_name, cu);
10104 if (attr != NULL)
10105 {
10106 int num;
10107 struct die_info *d = die;
10108 struct dwarf2_cu *imported_cu = cu;
10109
10110 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10111 keep inspecting DIEs until we hit the underlying import. */
10112 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10113 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10114 {
10115 attr = dwarf2_attr (d, DW_AT_import, cu);
10116 if (attr == NULL)
10117 break;
10118
10119 d = follow_die_ref (d, attr, &imported_cu);
10120 if (d->tag != DW_TAG_imported_declaration)
10121 break;
10122 }
10123
10124 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10125 {
10126 complaint (_("DIE at %s has too many recursively imported "
10127 "declarations"), sect_offset_str (d->sect_off));
10128 return 0;
10129 }
10130
10131 if (attr != NULL)
10132 {
10133 struct type *type;
10134 sect_offset sect_off = attr->get_ref_die_offset ();
10135
10136 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10137 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10138 {
10139 /* This declaration is a global namespace alias. Add
10140 a symbol for it whose type is the aliased namespace. */
10141 new_symbol (die, type, cu);
10142 return 1;
10143 }
10144 }
10145 }
10146
10147 return 0;
10148 }
10149
10150 /* Return the using directives repository (global or local?) to use in the
10151 current context for CU.
10152
10153 For Ada, imported declarations can materialize renamings, which *may* be
10154 global. However it is impossible (for now?) in DWARF to distinguish
10155 "external" imported declarations and "static" ones. As all imported
10156 declarations seem to be static in all other languages, make them all CU-wide
10157 global only in Ada. */
10158
10159 static struct using_direct **
10160 using_directives (struct dwarf2_cu *cu)
10161 {
10162 if (cu->language == language_ada
10163 && cu->get_builder ()->outermost_context_p ())
10164 return cu->get_builder ()->get_global_using_directives ();
10165 else
10166 return cu->get_builder ()->get_local_using_directives ();
10167 }
10168
10169 /* Read the import statement specified by the given die and record it. */
10170
10171 static void
10172 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10173 {
10174 struct objfile *objfile = cu->per_objfile->objfile;
10175 struct attribute *import_attr;
10176 struct die_info *imported_die, *child_die;
10177 struct dwarf2_cu *imported_cu;
10178 const char *imported_name;
10179 const char *imported_name_prefix;
10180 const char *canonical_name;
10181 const char *import_alias;
10182 const char *imported_declaration = NULL;
10183 const char *import_prefix;
10184 std::vector<const char *> excludes;
10185
10186 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10187 if (import_attr == NULL)
10188 {
10189 complaint (_("Tag '%s' has no DW_AT_import"),
10190 dwarf_tag_name (die->tag));
10191 return;
10192 }
10193
10194 imported_cu = cu;
10195 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10196 imported_name = dwarf2_name (imported_die, imported_cu);
10197 if (imported_name == NULL)
10198 {
10199 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10200
10201 The import in the following code:
10202 namespace A
10203 {
10204 typedef int B;
10205 }
10206
10207 int main ()
10208 {
10209 using A::B;
10210 B b;
10211 return b;
10212 }
10213
10214 ...
10215 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10216 <52> DW_AT_decl_file : 1
10217 <53> DW_AT_decl_line : 6
10218 <54> DW_AT_import : <0x75>
10219 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10220 <59> DW_AT_name : B
10221 <5b> DW_AT_decl_file : 1
10222 <5c> DW_AT_decl_line : 2
10223 <5d> DW_AT_type : <0x6e>
10224 ...
10225 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10226 <76> DW_AT_byte_size : 4
10227 <77> DW_AT_encoding : 5 (signed)
10228
10229 imports the wrong die ( 0x75 instead of 0x58 ).
10230 This case will be ignored until the gcc bug is fixed. */
10231 return;
10232 }
10233
10234 /* Figure out the local name after import. */
10235 import_alias = dwarf2_name (die, cu);
10236
10237 /* Figure out where the statement is being imported to. */
10238 import_prefix = determine_prefix (die, cu);
10239
10240 /* Figure out what the scope of the imported die is and prepend it
10241 to the name of the imported die. */
10242 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10243
10244 if (imported_die->tag != DW_TAG_namespace
10245 && imported_die->tag != DW_TAG_module)
10246 {
10247 imported_declaration = imported_name;
10248 canonical_name = imported_name_prefix;
10249 }
10250 else if (strlen (imported_name_prefix) > 0)
10251 canonical_name = obconcat (&objfile->objfile_obstack,
10252 imported_name_prefix,
10253 (cu->language == language_d ? "." : "::"),
10254 imported_name, (char *) NULL);
10255 else
10256 canonical_name = imported_name;
10257
10258 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10259 for (child_die = die->child; child_die && child_die->tag;
10260 child_die = child_die->sibling)
10261 {
10262 /* DWARF-4: A Fortran use statement with a “rename list” may be
10263 represented by an imported module entry with an import attribute
10264 referring to the module and owned entries corresponding to those
10265 entities that are renamed as part of being imported. */
10266
10267 if (child_die->tag != DW_TAG_imported_declaration)
10268 {
10269 complaint (_("child DW_TAG_imported_declaration expected "
10270 "- DIE at %s [in module %s]"),
10271 sect_offset_str (child_die->sect_off),
10272 objfile_name (objfile));
10273 continue;
10274 }
10275
10276 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10277 if (import_attr == NULL)
10278 {
10279 complaint (_("Tag '%s' has no DW_AT_import"),
10280 dwarf_tag_name (child_die->tag));
10281 continue;
10282 }
10283
10284 imported_cu = cu;
10285 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10286 &imported_cu);
10287 imported_name = dwarf2_name (imported_die, imported_cu);
10288 if (imported_name == NULL)
10289 {
10290 complaint (_("child DW_TAG_imported_declaration has unknown "
10291 "imported name - DIE at %s [in module %s]"),
10292 sect_offset_str (child_die->sect_off),
10293 objfile_name (objfile));
10294 continue;
10295 }
10296
10297 excludes.push_back (imported_name);
10298
10299 process_die (child_die, cu);
10300 }
10301
10302 add_using_directive (using_directives (cu),
10303 import_prefix,
10304 canonical_name,
10305 import_alias,
10306 imported_declaration,
10307 excludes,
10308 0,
10309 &objfile->objfile_obstack);
10310 }
10311
10312 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10313 types, but gives them a size of zero. Starting with version 14,
10314 ICC is compatible with GCC. */
10315
10316 static bool
10317 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10318 {
10319 if (!cu->checked_producer)
10320 check_producer (cu);
10321
10322 return cu->producer_is_icc_lt_14;
10323 }
10324
10325 /* ICC generates a DW_AT_type for C void functions. This was observed on
10326 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10327 which says that void functions should not have a DW_AT_type. */
10328
10329 static bool
10330 producer_is_icc (struct dwarf2_cu *cu)
10331 {
10332 if (!cu->checked_producer)
10333 check_producer (cu);
10334
10335 return cu->producer_is_icc;
10336 }
10337
10338 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10339 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10340 this, it was first present in GCC release 4.3.0. */
10341
10342 static bool
10343 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10344 {
10345 if (!cu->checked_producer)
10346 check_producer (cu);
10347
10348 return cu->producer_is_gcc_lt_4_3;
10349 }
10350
10351 static file_and_directory
10352 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10353 {
10354 file_and_directory res;
10355
10356 /* Find the filename. Do not use dwarf2_name here, since the filename
10357 is not a source language identifier. */
10358 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10359 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10360
10361 if (res.comp_dir == NULL
10362 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10363 && IS_ABSOLUTE_PATH (res.name))
10364 {
10365 res.comp_dir_storage = ldirname (res.name);
10366 if (!res.comp_dir_storage.empty ())
10367 res.comp_dir = res.comp_dir_storage.c_str ();
10368 }
10369 if (res.comp_dir != NULL)
10370 {
10371 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10372 directory, get rid of it. */
10373 const char *cp = strchr (res.comp_dir, ':');
10374
10375 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10376 res.comp_dir = cp + 1;
10377 }
10378
10379 if (res.name == NULL)
10380 res.name = "<unknown>";
10381
10382 return res;
10383 }
10384
10385 /* Handle DW_AT_stmt_list for a compilation unit.
10386 DIE is the DW_TAG_compile_unit die for CU.
10387 COMP_DIR is the compilation directory. LOWPC is passed to
10388 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10389
10390 static void
10391 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10392 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10393 {
10394 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10395 struct attribute *attr;
10396 struct line_header line_header_local;
10397 hashval_t line_header_local_hash;
10398 void **slot;
10399 int decode_mapping;
10400
10401 gdb_assert (! cu->per_cu->is_debug_types);
10402
10403 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10404 if (attr == NULL || !attr->form_is_unsigned ())
10405 return;
10406
10407 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10408
10409 /* The line header hash table is only created if needed (it exists to
10410 prevent redundant reading of the line table for partial_units).
10411 If we're given a partial_unit, we'll need it. If we're given a
10412 compile_unit, then use the line header hash table if it's already
10413 created, but don't create one just yet. */
10414
10415 if (per_objfile->line_header_hash == NULL
10416 && die->tag == DW_TAG_partial_unit)
10417 {
10418 per_objfile->line_header_hash
10419 .reset (htab_create_alloc (127, line_header_hash_voidp,
10420 line_header_eq_voidp,
10421 htab_delete_entry<line_header>,
10422 xcalloc, xfree));
10423 }
10424
10425 line_header_local.sect_off = line_offset;
10426 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10427 line_header_local_hash = line_header_hash (&line_header_local);
10428 if (per_objfile->line_header_hash != NULL)
10429 {
10430 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10431 &line_header_local,
10432 line_header_local_hash, NO_INSERT);
10433
10434 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10435 is not present in *SLOT (since if there is something in *SLOT then
10436 it will be for a partial_unit). */
10437 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10438 {
10439 gdb_assert (*slot != NULL);
10440 cu->line_header = (struct line_header *) *slot;
10441 return;
10442 }
10443 }
10444
10445 /* dwarf_decode_line_header does not yet provide sufficient information.
10446 We always have to call also dwarf_decode_lines for it. */
10447 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10448 if (lh == NULL)
10449 return;
10450
10451 cu->line_header = lh.release ();
10452 cu->line_header_die_owner = die;
10453
10454 if (per_objfile->line_header_hash == NULL)
10455 slot = NULL;
10456 else
10457 {
10458 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10459 &line_header_local,
10460 line_header_local_hash, INSERT);
10461 gdb_assert (slot != NULL);
10462 }
10463 if (slot != NULL && *slot == NULL)
10464 {
10465 /* This newly decoded line number information unit will be owned
10466 by line_header_hash hash table. */
10467 *slot = cu->line_header;
10468 cu->line_header_die_owner = NULL;
10469 }
10470 else
10471 {
10472 /* We cannot free any current entry in (*slot) as that struct line_header
10473 may be already used by multiple CUs. Create only temporary decoded
10474 line_header for this CU - it may happen at most once for each line
10475 number information unit. And if we're not using line_header_hash
10476 then this is what we want as well. */
10477 gdb_assert (die->tag != DW_TAG_partial_unit);
10478 }
10479 decode_mapping = (die->tag != DW_TAG_partial_unit);
10480 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10481 decode_mapping);
10482
10483 }
10484
10485 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10486
10487 static void
10488 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10489 {
10490 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10491 struct objfile *objfile = per_objfile->objfile;
10492 struct gdbarch *gdbarch = objfile->arch ();
10493 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10494 CORE_ADDR highpc = ((CORE_ADDR) 0);
10495 struct attribute *attr;
10496 struct die_info *child_die;
10497 CORE_ADDR baseaddr;
10498
10499 prepare_one_comp_unit (cu, die, cu->language);
10500 baseaddr = objfile->text_section_offset ();
10501
10502 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10503
10504 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10505 from finish_block. */
10506 if (lowpc == ((CORE_ADDR) -1))
10507 lowpc = highpc;
10508 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10509
10510 file_and_directory fnd = find_file_and_directory (die, cu);
10511
10512 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10513 standardised yet. As a workaround for the language detection we fall
10514 back to the DW_AT_producer string. */
10515 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10516 cu->language = language_opencl;
10517
10518 /* Similar hack for Go. */
10519 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10520 set_cu_language (DW_LANG_Go, cu);
10521
10522 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10523
10524 gdb_assert (per_objfile->sym_cu == nullptr);
10525 scoped_restore restore_sym_cu
10526 = make_scoped_restore (&per_objfile->sym_cu, cu);
10527
10528 /* Decode line number information if present. We do this before
10529 processing child DIEs, so that the line header table is available
10530 for DW_AT_decl_file. */
10531 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10532
10533 /* Process all dies in compilation unit. */
10534 if (die->child != NULL)
10535 {
10536 child_die = die->child;
10537 while (child_die && child_die->tag)
10538 {
10539 process_die (child_die, cu);
10540 child_die = child_die->sibling;
10541 }
10542 }
10543 per_objfile->sym_cu = nullptr;
10544
10545 /* Decode macro information, if present. Dwarf 2 macro information
10546 refers to information in the line number info statement program
10547 header, so we can only read it if we've read the header
10548 successfully. */
10549 attr = dwarf2_attr (die, DW_AT_macros, cu);
10550 if (attr == NULL)
10551 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10552 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10553 {
10554 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10555 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10556
10557 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10558 }
10559 else
10560 {
10561 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10562 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10563 {
10564 unsigned int macro_offset = attr->as_unsigned ();
10565
10566 dwarf_decode_macros (cu, macro_offset, 0);
10567 }
10568 }
10569 }
10570
10571 void
10572 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10573 {
10574 struct type_unit_group *tu_group;
10575 int first_time;
10576 struct attribute *attr;
10577 unsigned int i;
10578 struct signatured_type *sig_type;
10579
10580 gdb_assert (per_cu->is_debug_types);
10581 sig_type = (struct signatured_type *) per_cu;
10582
10583 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10584
10585 /* If we're using .gdb_index (includes -readnow) then
10586 per_cu->type_unit_group may not have been set up yet. */
10587 if (sig_type->type_unit_group == NULL)
10588 sig_type->type_unit_group = get_type_unit_group (this, attr);
10589 tu_group = sig_type->type_unit_group;
10590
10591 /* If we've already processed this stmt_list there's no real need to
10592 do it again, we could fake it and just recreate the part we need
10593 (file name,index -> symtab mapping). If data shows this optimization
10594 is useful we can do it then. */
10595 type_unit_group_unshareable *tug_unshare
10596 = per_objfile->get_type_unit_group_unshareable (tu_group);
10597 first_time = tug_unshare->compunit_symtab == NULL;
10598
10599 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10600 debug info. */
10601 line_header_up lh;
10602 if (attr != NULL && attr->form_is_unsigned ())
10603 {
10604 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10605 lh = dwarf_decode_line_header (line_offset, this);
10606 }
10607 if (lh == NULL)
10608 {
10609 if (first_time)
10610 start_symtab ("", NULL, 0);
10611 else
10612 {
10613 gdb_assert (tug_unshare->symtabs == NULL);
10614 gdb_assert (m_builder == nullptr);
10615 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10616 m_builder.reset (new struct buildsym_compunit
10617 (COMPUNIT_OBJFILE (cust), "",
10618 COMPUNIT_DIRNAME (cust),
10619 compunit_language (cust),
10620 0, cust));
10621 list_in_scope = get_builder ()->get_file_symbols ();
10622 }
10623 return;
10624 }
10625
10626 line_header = lh.release ();
10627 line_header_die_owner = die;
10628
10629 if (first_time)
10630 {
10631 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10632
10633 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10634 still initializing it, and our caller (a few levels up)
10635 process_full_type_unit still needs to know if this is the first
10636 time. */
10637
10638 tug_unshare->symtabs
10639 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10640 struct symtab *, line_header->file_names_size ());
10641
10642 auto &file_names = line_header->file_names ();
10643 for (i = 0; i < file_names.size (); ++i)
10644 {
10645 file_entry &fe = file_names[i];
10646 dwarf2_start_subfile (this, fe.name,
10647 fe.include_dir (line_header));
10648 buildsym_compunit *b = get_builder ();
10649 if (b->get_current_subfile ()->symtab == NULL)
10650 {
10651 /* NOTE: start_subfile will recognize when it's been
10652 passed a file it has already seen. So we can't
10653 assume there's a simple mapping from
10654 cu->line_header->file_names to subfiles, plus
10655 cu->line_header->file_names may contain dups. */
10656 b->get_current_subfile ()->symtab
10657 = allocate_symtab (cust, b->get_current_subfile ()->name);
10658 }
10659
10660 fe.symtab = b->get_current_subfile ()->symtab;
10661 tug_unshare->symtabs[i] = fe.symtab;
10662 }
10663 }
10664 else
10665 {
10666 gdb_assert (m_builder == nullptr);
10667 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10668 m_builder.reset (new struct buildsym_compunit
10669 (COMPUNIT_OBJFILE (cust), "",
10670 COMPUNIT_DIRNAME (cust),
10671 compunit_language (cust),
10672 0, cust));
10673 list_in_scope = get_builder ()->get_file_symbols ();
10674
10675 auto &file_names = line_header->file_names ();
10676 for (i = 0; i < file_names.size (); ++i)
10677 {
10678 file_entry &fe = file_names[i];
10679 fe.symtab = tug_unshare->symtabs[i];
10680 }
10681 }
10682
10683 /* The main symtab is allocated last. Type units don't have DW_AT_name
10684 so they don't have a "real" (so to speak) symtab anyway.
10685 There is later code that will assign the main symtab to all symbols
10686 that don't have one. We need to handle the case of a symbol with a
10687 missing symtab (DW_AT_decl_file) anyway. */
10688 }
10689
10690 /* Process DW_TAG_type_unit.
10691 For TUs we want to skip the first top level sibling if it's not the
10692 actual type being defined by this TU. In this case the first top
10693 level sibling is there to provide context only. */
10694
10695 static void
10696 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10697 {
10698 struct die_info *child_die;
10699
10700 prepare_one_comp_unit (cu, die, language_minimal);
10701
10702 /* Initialize (or reinitialize) the machinery for building symtabs.
10703 We do this before processing child DIEs, so that the line header table
10704 is available for DW_AT_decl_file. */
10705 cu->setup_type_unit_groups (die);
10706
10707 if (die->child != NULL)
10708 {
10709 child_die = die->child;
10710 while (child_die && child_die->tag)
10711 {
10712 process_die (child_die, cu);
10713 child_die = child_die->sibling;
10714 }
10715 }
10716 }
10717 \f
10718 /* DWO/DWP files.
10719
10720 http://gcc.gnu.org/wiki/DebugFission
10721 http://gcc.gnu.org/wiki/DebugFissionDWP
10722
10723 To simplify handling of both DWO files ("object" files with the DWARF info)
10724 and DWP files (a file with the DWOs packaged up into one file), we treat
10725 DWP files as having a collection of virtual DWO files. */
10726
10727 static hashval_t
10728 hash_dwo_file (const void *item)
10729 {
10730 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10731 hashval_t hash;
10732
10733 hash = htab_hash_string (dwo_file->dwo_name);
10734 if (dwo_file->comp_dir != NULL)
10735 hash += htab_hash_string (dwo_file->comp_dir);
10736 return hash;
10737 }
10738
10739 static int
10740 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10741 {
10742 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10743 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10744
10745 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10746 return 0;
10747 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10748 return lhs->comp_dir == rhs->comp_dir;
10749 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10750 }
10751
10752 /* Allocate a hash table for DWO files. */
10753
10754 static htab_up
10755 allocate_dwo_file_hash_table ()
10756 {
10757 return htab_up (htab_create_alloc (41,
10758 hash_dwo_file,
10759 eq_dwo_file,
10760 htab_delete_entry<dwo_file>,
10761 xcalloc, xfree));
10762 }
10763
10764 /* Lookup DWO file DWO_NAME. */
10765
10766 static void **
10767 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
10768 const char *dwo_name,
10769 const char *comp_dir)
10770 {
10771 struct dwo_file find_entry;
10772 void **slot;
10773
10774 if (per_objfile->per_bfd->dwo_files == NULL)
10775 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
10776
10777 find_entry.dwo_name = dwo_name;
10778 find_entry.comp_dir = comp_dir;
10779 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
10780 INSERT);
10781
10782 return slot;
10783 }
10784
10785 static hashval_t
10786 hash_dwo_unit (const void *item)
10787 {
10788 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10789
10790 /* This drops the top 32 bits of the id, but is ok for a hash. */
10791 return dwo_unit->signature;
10792 }
10793
10794 static int
10795 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10796 {
10797 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10798 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10799
10800 /* The signature is assumed to be unique within the DWO file.
10801 So while object file CU dwo_id's always have the value zero,
10802 that's OK, assuming each object file DWO file has only one CU,
10803 and that's the rule for now. */
10804 return lhs->signature == rhs->signature;
10805 }
10806
10807 /* Allocate a hash table for DWO CUs,TUs.
10808 There is one of these tables for each of CUs,TUs for each DWO file. */
10809
10810 static htab_up
10811 allocate_dwo_unit_table ()
10812 {
10813 /* Start out with a pretty small number.
10814 Generally DWO files contain only one CU and maybe some TUs. */
10815 return htab_up (htab_create_alloc (3,
10816 hash_dwo_unit,
10817 eq_dwo_unit,
10818 NULL, xcalloc, xfree));
10819 }
10820
10821 /* die_reader_func for create_dwo_cu. */
10822
10823 static void
10824 create_dwo_cu_reader (const struct die_reader_specs *reader,
10825 const gdb_byte *info_ptr,
10826 struct die_info *comp_unit_die,
10827 struct dwo_file *dwo_file,
10828 struct dwo_unit *dwo_unit)
10829 {
10830 struct dwarf2_cu *cu = reader->cu;
10831 sect_offset sect_off = cu->per_cu->sect_off;
10832 struct dwarf2_section_info *section = cu->per_cu->section;
10833
10834 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10835 if (!signature.has_value ())
10836 {
10837 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10838 " its dwo_id [in module %s]"),
10839 sect_offset_str (sect_off), dwo_file->dwo_name);
10840 return;
10841 }
10842
10843 dwo_unit->dwo_file = dwo_file;
10844 dwo_unit->signature = *signature;
10845 dwo_unit->section = section;
10846 dwo_unit->sect_off = sect_off;
10847 dwo_unit->length = cu->per_cu->length;
10848
10849 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10850 sect_offset_str (sect_off),
10851 hex_string (dwo_unit->signature));
10852 }
10853
10854 /* Create the dwo_units for the CUs in a DWO_FILE.
10855 Note: This function processes DWO files only, not DWP files. */
10856
10857 static void
10858 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10859 dwarf2_cu *cu, struct dwo_file &dwo_file,
10860 dwarf2_section_info &section, htab_up &cus_htab)
10861 {
10862 struct objfile *objfile = per_objfile->objfile;
10863 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10864 const gdb_byte *info_ptr, *end_ptr;
10865
10866 section.read (objfile);
10867 info_ptr = section.buffer;
10868
10869 if (info_ptr == NULL)
10870 return;
10871
10872 dwarf_read_debug_printf ("Reading %s for %s:",
10873 section.get_name (),
10874 section.get_file_name ());
10875
10876 end_ptr = info_ptr + section.size;
10877 while (info_ptr < end_ptr)
10878 {
10879 struct dwarf2_per_cu_data per_cu;
10880 struct dwo_unit read_unit {};
10881 struct dwo_unit *dwo_unit;
10882 void **slot;
10883 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10884
10885 per_cu.per_bfd = per_bfd;
10886 per_cu.is_debug_types = 0;
10887 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10888 per_cu.section = &section;
10889
10890 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
10891 if (!reader.dummy_p)
10892 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
10893 &dwo_file, &read_unit);
10894 info_ptr += per_cu.length;
10895
10896 // If the unit could not be parsed, skip it.
10897 if (read_unit.dwo_file == NULL)
10898 continue;
10899
10900 if (cus_htab == NULL)
10901 cus_htab = allocate_dwo_unit_table ();
10902
10903 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
10904 struct dwo_unit);
10905 *dwo_unit = read_unit;
10906 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
10907 gdb_assert (slot != NULL);
10908 if (*slot != NULL)
10909 {
10910 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10911 sect_offset dup_sect_off = dup_cu->sect_off;
10912
10913 complaint (_("debug cu entry at offset %s is duplicate to"
10914 " the entry at offset %s, signature %s"),
10915 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
10916 hex_string (dwo_unit->signature));
10917 }
10918 *slot = (void *)dwo_unit;
10919 }
10920 }
10921
10922 /* DWP file .debug_{cu,tu}_index section format:
10923 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10924 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10925
10926 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10927 officially standard DWP format was published with DWARF v5 and is called
10928 Version 5. There are no versions 3 or 4.
10929
10930 DWP Version 1:
10931
10932 Both index sections have the same format, and serve to map a 64-bit
10933 signature to a set of section numbers. Each section begins with a header,
10934 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10935 indexes, and a pool of 32-bit section numbers. The index sections will be
10936 aligned at 8-byte boundaries in the file.
10937
10938 The index section header consists of:
10939
10940 V, 32 bit version number
10941 -, 32 bits unused
10942 N, 32 bit number of compilation units or type units in the index
10943 M, 32 bit number of slots in the hash table
10944
10945 Numbers are recorded using the byte order of the application binary.
10946
10947 The hash table begins at offset 16 in the section, and consists of an array
10948 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10949 order of the application binary). Unused slots in the hash table are 0.
10950 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10951
10952 The parallel table begins immediately after the hash table
10953 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10954 array of 32-bit indexes (using the byte order of the application binary),
10955 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10956 table contains a 32-bit index into the pool of section numbers. For unused
10957 hash table slots, the corresponding entry in the parallel table will be 0.
10958
10959 The pool of section numbers begins immediately following the hash table
10960 (at offset 16 + 12 * M from the beginning of the section). The pool of
10961 section numbers consists of an array of 32-bit words (using the byte order
10962 of the application binary). Each item in the array is indexed starting
10963 from 0. The hash table entry provides the index of the first section
10964 number in the set. Additional section numbers in the set follow, and the
10965 set is terminated by a 0 entry (section number 0 is not used in ELF).
10966
10967 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10968 section must be the first entry in the set, and the .debug_abbrev.dwo must
10969 be the second entry. Other members of the set may follow in any order.
10970
10971 ---
10972
10973 DWP Versions 2 and 5:
10974
10975 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10976 and the entries in the index tables are now offsets into these sections.
10977 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10978 section.
10979
10980 Index Section Contents:
10981 Header
10982 Hash Table of Signatures dwp_hash_table.hash_table
10983 Parallel Table of Indices dwp_hash_table.unit_table
10984 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10985 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10986
10987 The index section header consists of:
10988
10989 V, 32 bit version number
10990 L, 32 bit number of columns in the table of section offsets
10991 N, 32 bit number of compilation units or type units in the index
10992 M, 32 bit number of slots in the hash table
10993
10994 Numbers are recorded using the byte order of the application binary.
10995
10996 The hash table has the same format as version 1.
10997 The parallel table of indices has the same format as version 1,
10998 except that the entries are origin-1 indices into the table of sections
10999 offsets and the table of section sizes.
11000
11001 The table of offsets begins immediately following the parallel table
11002 (at offset 16 + 12 * M from the beginning of the section). The table is
11003 a two-dimensional array of 32-bit words (using the byte order of the
11004 application binary), with L columns and N+1 rows, in row-major order.
11005 Each row in the array is indexed starting from 0. The first row provides
11006 a key to the remaining rows: each column in this row provides an identifier
11007 for a debug section, and the offsets in the same column of subsequent rows
11008 refer to that section. The section identifiers for Version 2 are:
11009
11010 DW_SECT_INFO 1 .debug_info.dwo
11011 DW_SECT_TYPES 2 .debug_types.dwo
11012 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11013 DW_SECT_LINE 4 .debug_line.dwo
11014 DW_SECT_LOC 5 .debug_loc.dwo
11015 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11016 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11017 DW_SECT_MACRO 8 .debug_macro.dwo
11018
11019 The section identifiers for Version 5 are:
11020
11021 DW_SECT_INFO_V5 1 .debug_info.dwo
11022 DW_SECT_RESERVED_V5 2 --
11023 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11024 DW_SECT_LINE_V5 4 .debug_line.dwo
11025 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11026 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11027 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11028 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11029
11030 The offsets provided by the CU and TU index sections are the base offsets
11031 for the contributions made by each CU or TU to the corresponding section
11032 in the package file. Each CU and TU header contains an abbrev_offset
11033 field, used to find the abbreviations table for that CU or TU within the
11034 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11035 be interpreted as relative to the base offset given in the index section.
11036 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11037 should be interpreted as relative to the base offset for .debug_line.dwo,
11038 and offsets into other debug sections obtained from DWARF attributes should
11039 also be interpreted as relative to the corresponding base offset.
11040
11041 The table of sizes begins immediately following the table of offsets.
11042 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11043 with L columns and N rows, in row-major order. Each row in the array is
11044 indexed starting from 1 (row 0 is shared by the two tables).
11045
11046 ---
11047
11048 Hash table lookup is handled the same in version 1 and 2:
11049
11050 We assume that N and M will not exceed 2^32 - 1.
11051 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11052
11053 Given a 64-bit compilation unit signature or a type signature S, an entry
11054 in the hash table is located as follows:
11055
11056 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11057 the low-order k bits all set to 1.
11058
11059 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11060
11061 3) If the hash table entry at index H matches the signature, use that
11062 entry. If the hash table entry at index H is unused (all zeroes),
11063 terminate the search: the signature is not present in the table.
11064
11065 4) Let H = (H + H') modulo M. Repeat at Step 3.
11066
11067 Because M > N and H' and M are relatively prime, the search is guaranteed
11068 to stop at an unused slot or find the match. */
11069
11070 /* Create a hash table to map DWO IDs to their CU/TU entry in
11071 .debug_{info,types}.dwo in DWP_FILE.
11072 Returns NULL if there isn't one.
11073 Note: This function processes DWP files only, not DWO files. */
11074
11075 static struct dwp_hash_table *
11076 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11077 struct dwp_file *dwp_file, int is_debug_types)
11078 {
11079 struct objfile *objfile = per_objfile->objfile;
11080 bfd *dbfd = dwp_file->dbfd.get ();
11081 const gdb_byte *index_ptr, *index_end;
11082 struct dwarf2_section_info *index;
11083 uint32_t version, nr_columns, nr_units, nr_slots;
11084 struct dwp_hash_table *htab;
11085
11086 if (is_debug_types)
11087 index = &dwp_file->sections.tu_index;
11088 else
11089 index = &dwp_file->sections.cu_index;
11090
11091 if (index->empty ())
11092 return NULL;
11093 index->read (objfile);
11094
11095 index_ptr = index->buffer;
11096 index_end = index_ptr + index->size;
11097
11098 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11099 For now it's safe to just read 4 bytes (particularly as it's difficult to
11100 tell if you're dealing with Version 5 before you've read the version). */
11101 version = read_4_bytes (dbfd, index_ptr);
11102 index_ptr += 4;
11103 if (version == 2 || version == 5)
11104 nr_columns = read_4_bytes (dbfd, index_ptr);
11105 else
11106 nr_columns = 0;
11107 index_ptr += 4;
11108 nr_units = read_4_bytes (dbfd, index_ptr);
11109 index_ptr += 4;
11110 nr_slots = read_4_bytes (dbfd, index_ptr);
11111 index_ptr += 4;
11112
11113 if (version != 1 && version != 2 && version != 5)
11114 {
11115 error (_("Dwarf Error: unsupported DWP file version (%s)"
11116 " [in module %s]"),
11117 pulongest (version), dwp_file->name);
11118 }
11119 if (nr_slots != (nr_slots & -nr_slots))
11120 {
11121 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11122 " is not power of 2 [in module %s]"),
11123 pulongest (nr_slots), dwp_file->name);
11124 }
11125
11126 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11127 htab->version = version;
11128 htab->nr_columns = nr_columns;
11129 htab->nr_units = nr_units;
11130 htab->nr_slots = nr_slots;
11131 htab->hash_table = index_ptr;
11132 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11133
11134 /* Exit early if the table is empty. */
11135 if (nr_slots == 0 || nr_units == 0
11136 || (version == 2 && nr_columns == 0)
11137 || (version == 5 && nr_columns == 0))
11138 {
11139 /* All must be zero. */
11140 if (nr_slots != 0 || nr_units != 0
11141 || (version == 2 && nr_columns != 0)
11142 || (version == 5 && nr_columns != 0))
11143 {
11144 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11145 " all zero [in modules %s]"),
11146 dwp_file->name);
11147 }
11148 return htab;
11149 }
11150
11151 if (version == 1)
11152 {
11153 htab->section_pool.v1.indices =
11154 htab->unit_table + sizeof (uint32_t) * nr_slots;
11155 /* It's harder to decide whether the section is too small in v1.
11156 V1 is deprecated anyway so we punt. */
11157 }
11158 else if (version == 2)
11159 {
11160 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11161 int *ids = htab->section_pool.v2.section_ids;
11162 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11163 /* Reverse map for error checking. */
11164 int ids_seen[DW_SECT_MAX + 1];
11165 int i;
11166
11167 if (nr_columns < 2)
11168 {
11169 error (_("Dwarf Error: bad DWP hash table, too few columns"
11170 " in section table [in module %s]"),
11171 dwp_file->name);
11172 }
11173 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11174 {
11175 error (_("Dwarf Error: bad DWP hash table, too many columns"
11176 " in section table [in module %s]"),
11177 dwp_file->name);
11178 }
11179 memset (ids, 255, sizeof_ids);
11180 memset (ids_seen, 255, sizeof (ids_seen));
11181 for (i = 0; i < nr_columns; ++i)
11182 {
11183 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11184
11185 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11186 {
11187 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11188 " in section table [in module %s]"),
11189 id, dwp_file->name);
11190 }
11191 if (ids_seen[id] != -1)
11192 {
11193 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11194 " id %d in section table [in module %s]"),
11195 id, dwp_file->name);
11196 }
11197 ids_seen[id] = i;
11198 ids[i] = id;
11199 }
11200 /* Must have exactly one info or types section. */
11201 if (((ids_seen[DW_SECT_INFO] != -1)
11202 + (ids_seen[DW_SECT_TYPES] != -1))
11203 != 1)
11204 {
11205 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11206 " DWO info/types section [in module %s]"),
11207 dwp_file->name);
11208 }
11209 /* Must have an abbrev section. */
11210 if (ids_seen[DW_SECT_ABBREV] == -1)
11211 {
11212 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11213 " section [in module %s]"),
11214 dwp_file->name);
11215 }
11216 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11217 htab->section_pool.v2.sizes =
11218 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11219 * nr_units * nr_columns);
11220 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11221 * nr_units * nr_columns))
11222 > index_end)
11223 {
11224 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11225 " [in module %s]"),
11226 dwp_file->name);
11227 }
11228 }
11229 else /* version == 5 */
11230 {
11231 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11232 int *ids = htab->section_pool.v5.section_ids;
11233 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11234 /* Reverse map for error checking. */
11235 int ids_seen[DW_SECT_MAX_V5 + 1];
11236
11237 if (nr_columns < 2)
11238 {
11239 error (_("Dwarf Error: bad DWP hash table, too few columns"
11240 " in section table [in module %s]"),
11241 dwp_file->name);
11242 }
11243 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11244 {
11245 error (_("Dwarf Error: bad DWP hash table, too many columns"
11246 " in section table [in module %s]"),
11247 dwp_file->name);
11248 }
11249 memset (ids, 255, sizeof_ids);
11250 memset (ids_seen, 255, sizeof (ids_seen));
11251 for (int i = 0; i < nr_columns; ++i)
11252 {
11253 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11254
11255 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11256 {
11257 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11258 " in section table [in module %s]"),
11259 id, dwp_file->name);
11260 }
11261 if (ids_seen[id] != -1)
11262 {
11263 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11264 " id %d in section table [in module %s]"),
11265 id, dwp_file->name);
11266 }
11267 ids_seen[id] = i;
11268 ids[i] = id;
11269 }
11270 /* Must have seen an info section. */
11271 if (ids_seen[DW_SECT_INFO_V5] == -1)
11272 {
11273 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11274 " DWO info/types section [in module %s]"),
11275 dwp_file->name);
11276 }
11277 /* Must have an abbrev section. */
11278 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11279 {
11280 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11281 " section [in module %s]"),
11282 dwp_file->name);
11283 }
11284 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11285 htab->section_pool.v5.sizes
11286 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11287 * nr_units * nr_columns);
11288 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11289 * nr_units * nr_columns))
11290 > index_end)
11291 {
11292 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11293 " [in module %s]"),
11294 dwp_file->name);
11295 }
11296 }
11297
11298 return htab;
11299 }
11300
11301 /* Update SECTIONS with the data from SECTP.
11302
11303 This function is like the other "locate" section routines, but in
11304 this context the sections to read comes from the DWP V1 hash table,
11305 not the full ELF section table.
11306
11307 The result is non-zero for success, or zero if an error was found. */
11308
11309 static int
11310 locate_v1_virtual_dwo_sections (asection *sectp,
11311 struct virtual_v1_dwo_sections *sections)
11312 {
11313 const struct dwop_section_names *names = &dwop_section_names;
11314
11315 if (names->abbrev_dwo.matches (sectp->name))
11316 {
11317 /* There can be only one. */
11318 if (sections->abbrev.s.section != NULL)
11319 return 0;
11320 sections->abbrev.s.section = sectp;
11321 sections->abbrev.size = bfd_section_size (sectp);
11322 }
11323 else if (names->info_dwo.matches (sectp->name)
11324 || names->types_dwo.matches (sectp->name))
11325 {
11326 /* There can be only one. */
11327 if (sections->info_or_types.s.section != NULL)
11328 return 0;
11329 sections->info_or_types.s.section = sectp;
11330 sections->info_or_types.size = bfd_section_size (sectp);
11331 }
11332 else if (names->line_dwo.matches (sectp->name))
11333 {
11334 /* There can be only one. */
11335 if (sections->line.s.section != NULL)
11336 return 0;
11337 sections->line.s.section = sectp;
11338 sections->line.size = bfd_section_size (sectp);
11339 }
11340 else if (names->loc_dwo.matches (sectp->name))
11341 {
11342 /* There can be only one. */
11343 if (sections->loc.s.section != NULL)
11344 return 0;
11345 sections->loc.s.section = sectp;
11346 sections->loc.size = bfd_section_size (sectp);
11347 }
11348 else if (names->macinfo_dwo.matches (sectp->name))
11349 {
11350 /* There can be only one. */
11351 if (sections->macinfo.s.section != NULL)
11352 return 0;
11353 sections->macinfo.s.section = sectp;
11354 sections->macinfo.size = bfd_section_size (sectp);
11355 }
11356 else if (names->macro_dwo.matches (sectp->name))
11357 {
11358 /* There can be only one. */
11359 if (sections->macro.s.section != NULL)
11360 return 0;
11361 sections->macro.s.section = sectp;
11362 sections->macro.size = bfd_section_size (sectp);
11363 }
11364 else if (names->str_offsets_dwo.matches (sectp->name))
11365 {
11366 /* There can be only one. */
11367 if (sections->str_offsets.s.section != NULL)
11368 return 0;
11369 sections->str_offsets.s.section = sectp;
11370 sections->str_offsets.size = bfd_section_size (sectp);
11371 }
11372 else
11373 {
11374 /* No other kind of section is valid. */
11375 return 0;
11376 }
11377
11378 return 1;
11379 }
11380
11381 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11382 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11383 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11384 This is for DWP version 1 files. */
11385
11386 static struct dwo_unit *
11387 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11388 struct dwp_file *dwp_file,
11389 uint32_t unit_index,
11390 const char *comp_dir,
11391 ULONGEST signature, int is_debug_types)
11392 {
11393 const struct dwp_hash_table *dwp_htab =
11394 is_debug_types ? dwp_file->tus : dwp_file->cus;
11395 bfd *dbfd = dwp_file->dbfd.get ();
11396 const char *kind = is_debug_types ? "TU" : "CU";
11397 struct dwo_file *dwo_file;
11398 struct dwo_unit *dwo_unit;
11399 struct virtual_v1_dwo_sections sections;
11400 void **dwo_file_slot;
11401 int i;
11402
11403 gdb_assert (dwp_file->version == 1);
11404
11405 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11406 kind, pulongest (unit_index), hex_string (signature),
11407 dwp_file->name);
11408
11409 /* Fetch the sections of this DWO unit.
11410 Put a limit on the number of sections we look for so that bad data
11411 doesn't cause us to loop forever. */
11412
11413 #define MAX_NR_V1_DWO_SECTIONS \
11414 (1 /* .debug_info or .debug_types */ \
11415 + 1 /* .debug_abbrev */ \
11416 + 1 /* .debug_line */ \
11417 + 1 /* .debug_loc */ \
11418 + 1 /* .debug_str_offsets */ \
11419 + 1 /* .debug_macro or .debug_macinfo */ \
11420 + 1 /* trailing zero */)
11421
11422 memset (&sections, 0, sizeof (sections));
11423
11424 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11425 {
11426 asection *sectp;
11427 uint32_t section_nr =
11428 read_4_bytes (dbfd,
11429 dwp_htab->section_pool.v1.indices
11430 + (unit_index + i) * sizeof (uint32_t));
11431
11432 if (section_nr == 0)
11433 break;
11434 if (section_nr >= dwp_file->num_sections)
11435 {
11436 error (_("Dwarf Error: bad DWP hash table, section number too large"
11437 " [in module %s]"),
11438 dwp_file->name);
11439 }
11440
11441 sectp = dwp_file->elf_sections[section_nr];
11442 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11443 {
11444 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11445 " [in module %s]"),
11446 dwp_file->name);
11447 }
11448 }
11449
11450 if (i < 2
11451 || sections.info_or_types.empty ()
11452 || sections.abbrev.empty ())
11453 {
11454 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11455 " [in module %s]"),
11456 dwp_file->name);
11457 }
11458 if (i == MAX_NR_V1_DWO_SECTIONS)
11459 {
11460 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11461 " [in module %s]"),
11462 dwp_file->name);
11463 }
11464
11465 /* It's easier for the rest of the code if we fake a struct dwo_file and
11466 have dwo_unit "live" in that. At least for now.
11467
11468 The DWP file can be made up of a random collection of CUs and TUs.
11469 However, for each CU + set of TUs that came from the same original DWO
11470 file, we can combine them back into a virtual DWO file to save space
11471 (fewer struct dwo_file objects to allocate). Remember that for really
11472 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11473
11474 std::string virtual_dwo_name =
11475 string_printf ("virtual-dwo/%d-%d-%d-%d",
11476 sections.abbrev.get_id (),
11477 sections.line.get_id (),
11478 sections.loc.get_id (),
11479 sections.str_offsets.get_id ());
11480 /* Can we use an existing virtual DWO file? */
11481 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11482 comp_dir);
11483 /* Create one if necessary. */
11484 if (*dwo_file_slot == NULL)
11485 {
11486 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11487 virtual_dwo_name.c_str ());
11488
11489 dwo_file = new struct dwo_file;
11490 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11491 dwo_file->comp_dir = comp_dir;
11492 dwo_file->sections.abbrev = sections.abbrev;
11493 dwo_file->sections.line = sections.line;
11494 dwo_file->sections.loc = sections.loc;
11495 dwo_file->sections.macinfo = sections.macinfo;
11496 dwo_file->sections.macro = sections.macro;
11497 dwo_file->sections.str_offsets = sections.str_offsets;
11498 /* The "str" section is global to the entire DWP file. */
11499 dwo_file->sections.str = dwp_file->sections.str;
11500 /* The info or types section is assigned below to dwo_unit,
11501 there's no need to record it in dwo_file.
11502 Also, we can't simply record type sections in dwo_file because
11503 we record a pointer into the vector in dwo_unit. As we collect more
11504 types we'll grow the vector and eventually have to reallocate space
11505 for it, invalidating all copies of pointers into the previous
11506 contents. */
11507 *dwo_file_slot = dwo_file;
11508 }
11509 else
11510 {
11511 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11512 virtual_dwo_name.c_str ());
11513
11514 dwo_file = (struct dwo_file *) *dwo_file_slot;
11515 }
11516
11517 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11518 dwo_unit->dwo_file = dwo_file;
11519 dwo_unit->signature = signature;
11520 dwo_unit->section =
11521 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11522 *dwo_unit->section = sections.info_or_types;
11523 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11524
11525 return dwo_unit;
11526 }
11527
11528 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11529 simplify them. Given a pointer to the containing section SECTION, and
11530 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11531 virtual section of just that piece. */
11532
11533 static struct dwarf2_section_info
11534 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11535 struct dwarf2_section_info *section,
11536 bfd_size_type offset, bfd_size_type size)
11537 {
11538 struct dwarf2_section_info result;
11539 asection *sectp;
11540
11541 gdb_assert (section != NULL);
11542 gdb_assert (!section->is_virtual);
11543
11544 memset (&result, 0, sizeof (result));
11545 result.s.containing_section = section;
11546 result.is_virtual = true;
11547
11548 if (size == 0)
11549 return result;
11550
11551 sectp = section->get_bfd_section ();
11552
11553 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11554 bounds of the real section. This is a pretty-rare event, so just
11555 flag an error (easier) instead of a warning and trying to cope. */
11556 if (sectp == NULL
11557 || offset + size > bfd_section_size (sectp))
11558 {
11559 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11560 " in section %s [in module %s]"),
11561 sectp ? bfd_section_name (sectp) : "<unknown>",
11562 objfile_name (per_objfile->objfile));
11563 }
11564
11565 result.virtual_offset = offset;
11566 result.size = size;
11567 return result;
11568 }
11569
11570 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11571 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11572 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11573 This is for DWP version 2 files. */
11574
11575 static struct dwo_unit *
11576 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11577 struct dwp_file *dwp_file,
11578 uint32_t unit_index,
11579 const char *comp_dir,
11580 ULONGEST signature, int is_debug_types)
11581 {
11582 const struct dwp_hash_table *dwp_htab =
11583 is_debug_types ? dwp_file->tus : dwp_file->cus;
11584 bfd *dbfd = dwp_file->dbfd.get ();
11585 const char *kind = is_debug_types ? "TU" : "CU";
11586 struct dwo_file *dwo_file;
11587 struct dwo_unit *dwo_unit;
11588 struct virtual_v2_or_v5_dwo_sections sections;
11589 void **dwo_file_slot;
11590 int i;
11591
11592 gdb_assert (dwp_file->version == 2);
11593
11594 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11595 kind, pulongest (unit_index), hex_string (signature),
11596 dwp_file->name);
11597
11598 /* Fetch the section offsets of this DWO unit. */
11599
11600 memset (&sections, 0, sizeof (sections));
11601
11602 for (i = 0; i < dwp_htab->nr_columns; ++i)
11603 {
11604 uint32_t offset = read_4_bytes (dbfd,
11605 dwp_htab->section_pool.v2.offsets
11606 + (((unit_index - 1) * dwp_htab->nr_columns
11607 + i)
11608 * sizeof (uint32_t)));
11609 uint32_t size = read_4_bytes (dbfd,
11610 dwp_htab->section_pool.v2.sizes
11611 + (((unit_index - 1) * dwp_htab->nr_columns
11612 + i)
11613 * sizeof (uint32_t)));
11614
11615 switch (dwp_htab->section_pool.v2.section_ids[i])
11616 {
11617 case DW_SECT_INFO:
11618 case DW_SECT_TYPES:
11619 sections.info_or_types_offset = offset;
11620 sections.info_or_types_size = size;
11621 break;
11622 case DW_SECT_ABBREV:
11623 sections.abbrev_offset = offset;
11624 sections.abbrev_size = size;
11625 break;
11626 case DW_SECT_LINE:
11627 sections.line_offset = offset;
11628 sections.line_size = size;
11629 break;
11630 case DW_SECT_LOC:
11631 sections.loc_offset = offset;
11632 sections.loc_size = size;
11633 break;
11634 case DW_SECT_STR_OFFSETS:
11635 sections.str_offsets_offset = offset;
11636 sections.str_offsets_size = size;
11637 break;
11638 case DW_SECT_MACINFO:
11639 sections.macinfo_offset = offset;
11640 sections.macinfo_size = size;
11641 break;
11642 case DW_SECT_MACRO:
11643 sections.macro_offset = offset;
11644 sections.macro_size = size;
11645 break;
11646 }
11647 }
11648
11649 /* It's easier for the rest of the code if we fake a struct dwo_file and
11650 have dwo_unit "live" in that. At least for now.
11651
11652 The DWP file can be made up of a random collection of CUs and TUs.
11653 However, for each CU + set of TUs that came from the same original DWO
11654 file, we can combine them back into a virtual DWO file to save space
11655 (fewer struct dwo_file objects to allocate). Remember that for really
11656 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11657
11658 std::string virtual_dwo_name =
11659 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11660 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11661 (long) (sections.line_size ? sections.line_offset : 0),
11662 (long) (sections.loc_size ? sections.loc_offset : 0),
11663 (long) (sections.str_offsets_size
11664 ? sections.str_offsets_offset : 0));
11665 /* Can we use an existing virtual DWO file? */
11666 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11667 comp_dir);
11668 /* Create one if necessary. */
11669 if (*dwo_file_slot == NULL)
11670 {
11671 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11672 virtual_dwo_name.c_str ());
11673
11674 dwo_file = new struct dwo_file;
11675 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11676 dwo_file->comp_dir = comp_dir;
11677 dwo_file->sections.abbrev =
11678 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11679 sections.abbrev_offset,
11680 sections.abbrev_size);
11681 dwo_file->sections.line =
11682 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11683 sections.line_offset,
11684 sections.line_size);
11685 dwo_file->sections.loc =
11686 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11687 sections.loc_offset, sections.loc_size);
11688 dwo_file->sections.macinfo =
11689 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11690 sections.macinfo_offset,
11691 sections.macinfo_size);
11692 dwo_file->sections.macro =
11693 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11694 sections.macro_offset,
11695 sections.macro_size);
11696 dwo_file->sections.str_offsets =
11697 create_dwp_v2_or_v5_section (per_objfile,
11698 &dwp_file->sections.str_offsets,
11699 sections.str_offsets_offset,
11700 sections.str_offsets_size);
11701 /* The "str" section is global to the entire DWP file. */
11702 dwo_file->sections.str = dwp_file->sections.str;
11703 /* The info or types section is assigned below to dwo_unit,
11704 there's no need to record it in dwo_file.
11705 Also, we can't simply record type sections in dwo_file because
11706 we record a pointer into the vector in dwo_unit. As we collect more
11707 types we'll grow the vector and eventually have to reallocate space
11708 for it, invalidating all copies of pointers into the previous
11709 contents. */
11710 *dwo_file_slot = dwo_file;
11711 }
11712 else
11713 {
11714 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11715 virtual_dwo_name.c_str ());
11716
11717 dwo_file = (struct dwo_file *) *dwo_file_slot;
11718 }
11719
11720 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11721 dwo_unit->dwo_file = dwo_file;
11722 dwo_unit->signature = signature;
11723 dwo_unit->section =
11724 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11725 *dwo_unit->section = create_dwp_v2_or_v5_section
11726 (per_objfile,
11727 is_debug_types
11728 ? &dwp_file->sections.types
11729 : &dwp_file->sections.info,
11730 sections.info_or_types_offset,
11731 sections.info_or_types_size);
11732 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11733
11734 return dwo_unit;
11735 }
11736
11737 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11738 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11739 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11740 This is for DWP version 5 files. */
11741
11742 static struct dwo_unit *
11743 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11744 struct dwp_file *dwp_file,
11745 uint32_t unit_index,
11746 const char *comp_dir,
11747 ULONGEST signature, int is_debug_types)
11748 {
11749 const struct dwp_hash_table *dwp_htab
11750 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11751 bfd *dbfd = dwp_file->dbfd.get ();
11752 const char *kind = is_debug_types ? "TU" : "CU";
11753 struct dwo_file *dwo_file;
11754 struct dwo_unit *dwo_unit;
11755 struct virtual_v2_or_v5_dwo_sections sections {};
11756 void **dwo_file_slot;
11757
11758 gdb_assert (dwp_file->version == 5);
11759
11760 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11761 kind, pulongest (unit_index), hex_string (signature),
11762 dwp_file->name);
11763
11764 /* Fetch the section offsets of this DWO unit. */
11765
11766 /* memset (&sections, 0, sizeof (sections)); */
11767
11768 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11769 {
11770 uint32_t offset = read_4_bytes (dbfd,
11771 dwp_htab->section_pool.v5.offsets
11772 + (((unit_index - 1)
11773 * dwp_htab->nr_columns
11774 + i)
11775 * sizeof (uint32_t)));
11776 uint32_t size = read_4_bytes (dbfd,
11777 dwp_htab->section_pool.v5.sizes
11778 + (((unit_index - 1) * dwp_htab->nr_columns
11779 + i)
11780 * sizeof (uint32_t)));
11781
11782 switch (dwp_htab->section_pool.v5.section_ids[i])
11783 {
11784 case DW_SECT_ABBREV_V5:
11785 sections.abbrev_offset = offset;
11786 sections.abbrev_size = size;
11787 break;
11788 case DW_SECT_INFO_V5:
11789 sections.info_or_types_offset = offset;
11790 sections.info_or_types_size = size;
11791 break;
11792 case DW_SECT_LINE_V5:
11793 sections.line_offset = offset;
11794 sections.line_size = size;
11795 break;
11796 case DW_SECT_LOCLISTS_V5:
11797 sections.loclists_offset = offset;
11798 sections.loclists_size = size;
11799 break;
11800 case DW_SECT_MACRO_V5:
11801 sections.macro_offset = offset;
11802 sections.macro_size = size;
11803 break;
11804 case DW_SECT_RNGLISTS_V5:
11805 sections.rnglists_offset = offset;
11806 sections.rnglists_size = size;
11807 break;
11808 case DW_SECT_STR_OFFSETS_V5:
11809 sections.str_offsets_offset = offset;
11810 sections.str_offsets_size = size;
11811 break;
11812 case DW_SECT_RESERVED_V5:
11813 default:
11814 break;
11815 }
11816 }
11817
11818 /* It's easier for the rest of the code if we fake a struct dwo_file and
11819 have dwo_unit "live" in that. At least for now.
11820
11821 The DWP file can be made up of a random collection of CUs and TUs.
11822 However, for each CU + set of TUs that came from the same original DWO
11823 file, we can combine them back into a virtual DWO file to save space
11824 (fewer struct dwo_file objects to allocate). Remember that for really
11825 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11826
11827 std::string virtual_dwo_name =
11828 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
11829 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11830 (long) (sections.line_size ? sections.line_offset : 0),
11831 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11832 (long) (sections.str_offsets_size
11833 ? sections.str_offsets_offset : 0),
11834 (long) (sections.macro_size ? sections.macro_offset : 0),
11835 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11836 /* Can we use an existing virtual DWO file? */
11837 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11838 virtual_dwo_name.c_str (),
11839 comp_dir);
11840 /* Create one if necessary. */
11841 if (*dwo_file_slot == NULL)
11842 {
11843 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11844 virtual_dwo_name.c_str ());
11845
11846 dwo_file = new struct dwo_file;
11847 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11848 dwo_file->comp_dir = comp_dir;
11849 dwo_file->sections.abbrev =
11850 create_dwp_v2_or_v5_section (per_objfile,
11851 &dwp_file->sections.abbrev,
11852 sections.abbrev_offset,
11853 sections.abbrev_size);
11854 dwo_file->sections.line =
11855 create_dwp_v2_or_v5_section (per_objfile,
11856 &dwp_file->sections.line,
11857 sections.line_offset, sections.line_size);
11858 dwo_file->sections.macro =
11859 create_dwp_v2_or_v5_section (per_objfile,
11860 &dwp_file->sections.macro,
11861 sections.macro_offset,
11862 sections.macro_size);
11863 dwo_file->sections.loclists =
11864 create_dwp_v2_or_v5_section (per_objfile,
11865 &dwp_file->sections.loclists,
11866 sections.loclists_offset,
11867 sections.loclists_size);
11868 dwo_file->sections.rnglists =
11869 create_dwp_v2_or_v5_section (per_objfile,
11870 &dwp_file->sections.rnglists,
11871 sections.rnglists_offset,
11872 sections.rnglists_size);
11873 dwo_file->sections.str_offsets =
11874 create_dwp_v2_or_v5_section (per_objfile,
11875 &dwp_file->sections.str_offsets,
11876 sections.str_offsets_offset,
11877 sections.str_offsets_size);
11878 /* The "str" section is global to the entire DWP file. */
11879 dwo_file->sections.str = dwp_file->sections.str;
11880 /* The info or types section is assigned below to dwo_unit,
11881 there's no need to record it in dwo_file.
11882 Also, we can't simply record type sections in dwo_file because
11883 we record a pointer into the vector in dwo_unit. As we collect more
11884 types we'll grow the vector and eventually have to reallocate space
11885 for it, invalidating all copies of pointers into the previous
11886 contents. */
11887 *dwo_file_slot = dwo_file;
11888 }
11889 else
11890 {
11891 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11892 virtual_dwo_name.c_str ());
11893
11894 dwo_file = (struct dwo_file *) *dwo_file_slot;
11895 }
11896
11897 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11898 dwo_unit->dwo_file = dwo_file;
11899 dwo_unit->signature = signature;
11900 dwo_unit->section
11901 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11902 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
11903 &dwp_file->sections.info,
11904 sections.info_or_types_offset,
11905 sections.info_or_types_size);
11906 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11907
11908 return dwo_unit;
11909 }
11910
11911 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11912 Returns NULL if the signature isn't found. */
11913
11914 static struct dwo_unit *
11915 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
11916 struct dwp_file *dwp_file, const char *comp_dir,
11917 ULONGEST signature, int is_debug_types)
11918 {
11919 const struct dwp_hash_table *dwp_htab =
11920 is_debug_types ? dwp_file->tus : dwp_file->cus;
11921 bfd *dbfd = dwp_file->dbfd.get ();
11922 uint32_t mask = dwp_htab->nr_slots - 1;
11923 uint32_t hash = signature & mask;
11924 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11925 unsigned int i;
11926 void **slot;
11927 struct dwo_unit find_dwo_cu;
11928
11929 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11930 find_dwo_cu.signature = signature;
11931 slot = htab_find_slot (is_debug_types
11932 ? dwp_file->loaded_tus.get ()
11933 : dwp_file->loaded_cus.get (),
11934 &find_dwo_cu, INSERT);
11935
11936 if (*slot != NULL)
11937 return (struct dwo_unit *) *slot;
11938
11939 /* Use a for loop so that we don't loop forever on bad debug info. */
11940 for (i = 0; i < dwp_htab->nr_slots; ++i)
11941 {
11942 ULONGEST signature_in_table;
11943
11944 signature_in_table =
11945 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11946 if (signature_in_table == signature)
11947 {
11948 uint32_t unit_index =
11949 read_4_bytes (dbfd,
11950 dwp_htab->unit_table + hash * sizeof (uint32_t));
11951
11952 if (dwp_file->version == 1)
11953 {
11954 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11955 unit_index, comp_dir,
11956 signature, is_debug_types);
11957 }
11958 else if (dwp_file->version == 2)
11959 {
11960 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11961 unit_index, comp_dir,
11962 signature, is_debug_types);
11963 }
11964 else /* version == 5 */
11965 {
11966 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11967 unit_index, comp_dir,
11968 signature, is_debug_types);
11969 }
11970 return (struct dwo_unit *) *slot;
11971 }
11972 if (signature_in_table == 0)
11973 return NULL;
11974 hash = (hash + hash2) & mask;
11975 }
11976
11977 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11978 " [in module %s]"),
11979 dwp_file->name);
11980 }
11981
11982 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11983 Open the file specified by FILE_NAME and hand it off to BFD for
11984 preliminary analysis. Return a newly initialized bfd *, which
11985 includes a canonicalized copy of FILE_NAME.
11986 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11987 SEARCH_CWD is true if the current directory is to be searched.
11988 It will be searched before debug-file-directory.
11989 If successful, the file is added to the bfd include table of the
11990 objfile's bfd (see gdb_bfd_record_inclusion).
11991 If unable to find/open the file, return NULL.
11992 NOTE: This function is derived from symfile_bfd_open. */
11993
11994 static gdb_bfd_ref_ptr
11995 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
11996 const char *file_name, int is_dwp, int search_cwd)
11997 {
11998 int desc;
11999 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12000 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12001 to debug_file_directory. */
12002 const char *search_path;
12003 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12004
12005 gdb::unique_xmalloc_ptr<char> search_path_holder;
12006 if (search_cwd)
12007 {
12008 if (*debug_file_directory != '\0')
12009 {
12010 search_path_holder.reset (concat (".", dirname_separator_string,
12011 debug_file_directory,
12012 (char *) NULL));
12013 search_path = search_path_holder.get ();
12014 }
12015 else
12016 search_path = ".";
12017 }
12018 else
12019 search_path = debug_file_directory;
12020
12021 /* Add the path for the executable binary to the list of search paths. */
12022 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12023 search_path_holder.reset (concat (objfile_dir.c_str (),
12024 dirname_separator_string,
12025 search_path, nullptr));
12026 search_path = search_path_holder.get ();
12027
12028 openp_flags flags = OPF_RETURN_REALPATH;
12029 if (is_dwp)
12030 flags |= OPF_SEARCH_IN_PATH;
12031
12032 gdb::unique_xmalloc_ptr<char> absolute_name;
12033 desc = openp (search_path, flags, file_name,
12034 O_RDONLY | O_BINARY, &absolute_name);
12035 if (desc < 0)
12036 return NULL;
12037
12038 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12039 gnutarget, desc));
12040 if (sym_bfd == NULL)
12041 return NULL;
12042 bfd_set_cacheable (sym_bfd.get (), 1);
12043
12044 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12045 return NULL;
12046
12047 /* Success. Record the bfd as having been included by the objfile's bfd.
12048 This is important because things like demangled_names_hash lives in the
12049 objfile's per_bfd space and may have references to things like symbol
12050 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12051 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12052
12053 return sym_bfd;
12054 }
12055
12056 /* Try to open DWO file FILE_NAME.
12057 COMP_DIR is the DW_AT_comp_dir attribute.
12058 The result is the bfd handle of the file.
12059 If there is a problem finding or opening the file, return NULL.
12060 Upon success, the canonicalized path of the file is stored in the bfd,
12061 same as symfile_bfd_open. */
12062
12063 static gdb_bfd_ref_ptr
12064 open_dwo_file (dwarf2_per_objfile *per_objfile,
12065 const char *file_name, const char *comp_dir)
12066 {
12067 if (IS_ABSOLUTE_PATH (file_name))
12068 return try_open_dwop_file (per_objfile, file_name,
12069 0 /*is_dwp*/, 0 /*search_cwd*/);
12070
12071 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12072
12073 if (comp_dir != NULL)
12074 {
12075 gdb::unique_xmalloc_ptr<char> path_to_try
12076 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12077
12078 /* NOTE: If comp_dir is a relative path, this will also try the
12079 search path, which seems useful. */
12080 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12081 0 /*is_dwp*/,
12082 1 /*search_cwd*/));
12083 if (abfd != NULL)
12084 return abfd;
12085 }
12086
12087 /* That didn't work, try debug-file-directory, which, despite its name,
12088 is a list of paths. */
12089
12090 if (*debug_file_directory == '\0')
12091 return NULL;
12092
12093 return try_open_dwop_file (per_objfile, file_name,
12094 0 /*is_dwp*/, 1 /*search_cwd*/);
12095 }
12096
12097 /* This function is mapped across the sections and remembers the offset and
12098 size of each of the DWO debugging sections we are interested in. */
12099
12100 static void
12101 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12102 dwo_sections *dwo_sections)
12103 {
12104 const struct dwop_section_names *names = &dwop_section_names;
12105
12106 if (names->abbrev_dwo.matches (sectp->name))
12107 {
12108 dwo_sections->abbrev.s.section = sectp;
12109 dwo_sections->abbrev.size = bfd_section_size (sectp);
12110 }
12111 else if (names->info_dwo.matches (sectp->name))
12112 {
12113 dwo_sections->info.s.section = sectp;
12114 dwo_sections->info.size = bfd_section_size (sectp);
12115 }
12116 else if (names->line_dwo.matches (sectp->name))
12117 {
12118 dwo_sections->line.s.section = sectp;
12119 dwo_sections->line.size = bfd_section_size (sectp);
12120 }
12121 else if (names->loc_dwo.matches (sectp->name))
12122 {
12123 dwo_sections->loc.s.section = sectp;
12124 dwo_sections->loc.size = bfd_section_size (sectp);
12125 }
12126 else if (names->loclists_dwo.matches (sectp->name))
12127 {
12128 dwo_sections->loclists.s.section = sectp;
12129 dwo_sections->loclists.size = bfd_section_size (sectp);
12130 }
12131 else if (names->macinfo_dwo.matches (sectp->name))
12132 {
12133 dwo_sections->macinfo.s.section = sectp;
12134 dwo_sections->macinfo.size = bfd_section_size (sectp);
12135 }
12136 else if (names->macro_dwo.matches (sectp->name))
12137 {
12138 dwo_sections->macro.s.section = sectp;
12139 dwo_sections->macro.size = bfd_section_size (sectp);
12140 }
12141 else if (names->rnglists_dwo.matches (sectp->name))
12142 {
12143 dwo_sections->rnglists.s.section = sectp;
12144 dwo_sections->rnglists.size = bfd_section_size (sectp);
12145 }
12146 else if (names->str_dwo.matches (sectp->name))
12147 {
12148 dwo_sections->str.s.section = sectp;
12149 dwo_sections->str.size = bfd_section_size (sectp);
12150 }
12151 else if (names->str_offsets_dwo.matches (sectp->name))
12152 {
12153 dwo_sections->str_offsets.s.section = sectp;
12154 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12155 }
12156 else if (names->types_dwo.matches (sectp->name))
12157 {
12158 struct dwarf2_section_info type_section;
12159
12160 memset (&type_section, 0, sizeof (type_section));
12161 type_section.s.section = sectp;
12162 type_section.size = bfd_section_size (sectp);
12163 dwo_sections->types.push_back (type_section);
12164 }
12165 }
12166
12167 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12168 by PER_CU. This is for the non-DWP case.
12169 The result is NULL if DWO_NAME can't be found. */
12170
12171 static struct dwo_file *
12172 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12173 const char *comp_dir)
12174 {
12175 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12176
12177 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12178 if (dbfd == NULL)
12179 {
12180 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12181
12182 return NULL;
12183 }
12184
12185 dwo_file_up dwo_file (new struct dwo_file);
12186 dwo_file->dwo_name = dwo_name;
12187 dwo_file->comp_dir = comp_dir;
12188 dwo_file->dbfd = std::move (dbfd);
12189
12190 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12191 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12192 &dwo_file->sections);
12193
12194 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12195 dwo_file->cus);
12196
12197 if (cu->per_cu->dwarf_version < 5)
12198 {
12199 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12200 dwo_file->sections.types, dwo_file->tus);
12201 }
12202 else
12203 {
12204 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12205 &dwo_file->sections.info, dwo_file->tus,
12206 rcuh_kind::COMPILE);
12207 }
12208
12209 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12210
12211 return dwo_file.release ();
12212 }
12213
12214 /* This function is mapped across the sections and remembers the offset and
12215 size of each of the DWP debugging sections common to version 1 and 2 that
12216 we are interested in. */
12217
12218 static void
12219 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12220 dwp_file *dwp_file)
12221 {
12222 const struct dwop_section_names *names = &dwop_section_names;
12223 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12224
12225 /* Record the ELF section number for later lookup: this is what the
12226 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12227 gdb_assert (elf_section_nr < dwp_file->num_sections);
12228 dwp_file->elf_sections[elf_section_nr] = sectp;
12229
12230 /* Look for specific sections that we need. */
12231 if (names->str_dwo.matches (sectp->name))
12232 {
12233 dwp_file->sections.str.s.section = sectp;
12234 dwp_file->sections.str.size = bfd_section_size (sectp);
12235 }
12236 else if (names->cu_index.matches (sectp->name))
12237 {
12238 dwp_file->sections.cu_index.s.section = sectp;
12239 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12240 }
12241 else if (names->tu_index.matches (sectp->name))
12242 {
12243 dwp_file->sections.tu_index.s.section = sectp;
12244 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12245 }
12246 }
12247
12248 /* This function is mapped across the sections and remembers the offset and
12249 size of each of the DWP version 2 debugging sections that we are interested
12250 in. This is split into a separate function because we don't know if we
12251 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12252
12253 static void
12254 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12255 {
12256 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12257 const struct dwop_section_names *names = &dwop_section_names;
12258 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12259
12260 /* Record the ELF section number for later lookup: this is what the
12261 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12262 gdb_assert (elf_section_nr < dwp_file->num_sections);
12263 dwp_file->elf_sections[elf_section_nr] = sectp;
12264
12265 /* Look for specific sections that we need. */
12266 if (names->abbrev_dwo.matches (sectp->name))
12267 {
12268 dwp_file->sections.abbrev.s.section = sectp;
12269 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12270 }
12271 else if (names->info_dwo.matches (sectp->name))
12272 {
12273 dwp_file->sections.info.s.section = sectp;
12274 dwp_file->sections.info.size = bfd_section_size (sectp);
12275 }
12276 else if (names->line_dwo.matches (sectp->name))
12277 {
12278 dwp_file->sections.line.s.section = sectp;
12279 dwp_file->sections.line.size = bfd_section_size (sectp);
12280 }
12281 else if (names->loc_dwo.matches (sectp->name))
12282 {
12283 dwp_file->sections.loc.s.section = sectp;
12284 dwp_file->sections.loc.size = bfd_section_size (sectp);
12285 }
12286 else if (names->macinfo_dwo.matches (sectp->name))
12287 {
12288 dwp_file->sections.macinfo.s.section = sectp;
12289 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12290 }
12291 else if (names->macro_dwo.matches (sectp->name))
12292 {
12293 dwp_file->sections.macro.s.section = sectp;
12294 dwp_file->sections.macro.size = bfd_section_size (sectp);
12295 }
12296 else if (names->str_offsets_dwo.matches (sectp->name))
12297 {
12298 dwp_file->sections.str_offsets.s.section = sectp;
12299 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12300 }
12301 else if (names->types_dwo.matches (sectp->name))
12302 {
12303 dwp_file->sections.types.s.section = sectp;
12304 dwp_file->sections.types.size = bfd_section_size (sectp);
12305 }
12306 }
12307
12308 /* This function is mapped across the sections and remembers the offset and
12309 size of each of the DWP version 5 debugging sections that we are interested
12310 in. This is split into a separate function because we don't know if we
12311 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12312
12313 static void
12314 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12315 {
12316 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12317 const struct dwop_section_names *names = &dwop_section_names;
12318 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12319
12320 /* Record the ELF section number for later lookup: this is what the
12321 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12322 gdb_assert (elf_section_nr < dwp_file->num_sections);
12323 dwp_file->elf_sections[elf_section_nr] = sectp;
12324
12325 /* Look for specific sections that we need. */
12326 if (names->abbrev_dwo.matches (sectp->name))
12327 {
12328 dwp_file->sections.abbrev.s.section = sectp;
12329 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12330 }
12331 else if (names->info_dwo.matches (sectp->name))
12332 {
12333 dwp_file->sections.info.s.section = sectp;
12334 dwp_file->sections.info.size = bfd_section_size (sectp);
12335 }
12336 else if (names->line_dwo.matches (sectp->name))
12337 {
12338 dwp_file->sections.line.s.section = sectp;
12339 dwp_file->sections.line.size = bfd_section_size (sectp);
12340 }
12341 else if (names->loclists_dwo.matches (sectp->name))
12342 {
12343 dwp_file->sections.loclists.s.section = sectp;
12344 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12345 }
12346 else if (names->macro_dwo.matches (sectp->name))
12347 {
12348 dwp_file->sections.macro.s.section = sectp;
12349 dwp_file->sections.macro.size = bfd_section_size (sectp);
12350 }
12351 else if (names->rnglists_dwo.matches (sectp->name))
12352 {
12353 dwp_file->sections.rnglists.s.section = sectp;
12354 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12355 }
12356 else if (names->str_offsets_dwo.matches (sectp->name))
12357 {
12358 dwp_file->sections.str_offsets.s.section = sectp;
12359 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12360 }
12361 }
12362
12363 /* Hash function for dwp_file loaded CUs/TUs. */
12364
12365 static hashval_t
12366 hash_dwp_loaded_cutus (const void *item)
12367 {
12368 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12369
12370 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12371 return dwo_unit->signature;
12372 }
12373
12374 /* Equality function for dwp_file loaded CUs/TUs. */
12375
12376 static int
12377 eq_dwp_loaded_cutus (const void *a, const void *b)
12378 {
12379 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12380 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12381
12382 return dua->signature == dub->signature;
12383 }
12384
12385 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12386
12387 static htab_up
12388 allocate_dwp_loaded_cutus_table ()
12389 {
12390 return htab_up (htab_create_alloc (3,
12391 hash_dwp_loaded_cutus,
12392 eq_dwp_loaded_cutus,
12393 NULL, xcalloc, xfree));
12394 }
12395
12396 /* Try to open DWP file FILE_NAME.
12397 The result is the bfd handle of the file.
12398 If there is a problem finding or opening the file, return NULL.
12399 Upon success, the canonicalized path of the file is stored in the bfd,
12400 same as symfile_bfd_open. */
12401
12402 static gdb_bfd_ref_ptr
12403 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12404 {
12405 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12406 1 /*is_dwp*/,
12407 1 /*search_cwd*/));
12408 if (abfd != NULL)
12409 return abfd;
12410
12411 /* Work around upstream bug 15652.
12412 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12413 [Whether that's a "bug" is debatable, but it is getting in our way.]
12414 We have no real idea where the dwp file is, because gdb's realpath-ing
12415 of the executable's path may have discarded the needed info.
12416 [IWBN if the dwp file name was recorded in the executable, akin to
12417 .gnu_debuglink, but that doesn't exist yet.]
12418 Strip the directory from FILE_NAME and search again. */
12419 if (*debug_file_directory != '\0')
12420 {
12421 /* Don't implicitly search the current directory here.
12422 If the user wants to search "." to handle this case,
12423 it must be added to debug-file-directory. */
12424 return try_open_dwop_file (per_objfile, lbasename (file_name),
12425 1 /*is_dwp*/,
12426 0 /*search_cwd*/);
12427 }
12428
12429 return NULL;
12430 }
12431
12432 /* Initialize the use of the DWP file for the current objfile.
12433 By convention the name of the DWP file is ${objfile}.dwp.
12434 The result is NULL if it can't be found. */
12435
12436 static std::unique_ptr<struct dwp_file>
12437 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12438 {
12439 struct objfile *objfile = per_objfile->objfile;
12440
12441 /* Try to find first .dwp for the binary file before any symbolic links
12442 resolving. */
12443
12444 /* If the objfile is a debug file, find the name of the real binary
12445 file and get the name of dwp file from there. */
12446 std::string dwp_name;
12447 if (objfile->separate_debug_objfile_backlink != NULL)
12448 {
12449 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12450 const char *backlink_basename = lbasename (backlink->original_name);
12451
12452 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12453 }
12454 else
12455 dwp_name = objfile->original_name;
12456
12457 dwp_name += ".dwp";
12458
12459 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12460 if (dbfd == NULL
12461 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12462 {
12463 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12464 dwp_name = objfile_name (objfile);
12465 dwp_name += ".dwp";
12466 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12467 }
12468
12469 if (dbfd == NULL)
12470 {
12471 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12472
12473 return std::unique_ptr<dwp_file> ();
12474 }
12475
12476 const char *name = bfd_get_filename (dbfd.get ());
12477 std::unique_ptr<struct dwp_file> dwp_file
12478 (new struct dwp_file (name, std::move (dbfd)));
12479
12480 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12481 dwp_file->elf_sections =
12482 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12483 dwp_file->num_sections, asection *);
12484
12485 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12486 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12487 dwp_file.get ());
12488
12489 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12490
12491 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12492
12493 /* The DWP file version is stored in the hash table. Oh well. */
12494 if (dwp_file->cus && dwp_file->tus
12495 && dwp_file->cus->version != dwp_file->tus->version)
12496 {
12497 /* Technically speaking, we should try to limp along, but this is
12498 pretty bizarre. We use pulongest here because that's the established
12499 portability solution (e.g, we cannot use %u for uint32_t). */
12500 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12501 " TU version %s [in DWP file %s]"),
12502 pulongest (dwp_file->cus->version),
12503 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12504 }
12505
12506 if (dwp_file->cus)
12507 dwp_file->version = dwp_file->cus->version;
12508 else if (dwp_file->tus)
12509 dwp_file->version = dwp_file->tus->version;
12510 else
12511 dwp_file->version = 2;
12512
12513 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12514 {
12515 if (dwp_file->version == 2)
12516 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12517 dwp_file.get ());
12518 else
12519 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12520 dwp_file.get ());
12521 }
12522
12523 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12524 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12525
12526 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12527 dwarf_read_debug_printf (" %s CUs, %s TUs",
12528 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12529 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12530
12531 return dwp_file;
12532 }
12533
12534 /* Wrapper around open_and_init_dwp_file, only open it once. */
12535
12536 static struct dwp_file *
12537 get_dwp_file (dwarf2_per_objfile *per_objfile)
12538 {
12539 if (!per_objfile->per_bfd->dwp_checked)
12540 {
12541 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12542 per_objfile->per_bfd->dwp_checked = 1;
12543 }
12544 return per_objfile->per_bfd->dwp_file.get ();
12545 }
12546
12547 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12548 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12549 or in the DWP file for the objfile, referenced by THIS_UNIT.
12550 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12551 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12552
12553 This is called, for example, when wanting to read a variable with a
12554 complex location. Therefore we don't want to do file i/o for every call.
12555 Therefore we don't want to look for a DWO file on every call.
12556 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12557 then we check if we've already seen DWO_NAME, and only THEN do we check
12558 for a DWO file.
12559
12560 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12561 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12562
12563 static struct dwo_unit *
12564 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12565 ULONGEST signature, int is_debug_types)
12566 {
12567 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12568 struct objfile *objfile = per_objfile->objfile;
12569 const char *kind = is_debug_types ? "TU" : "CU";
12570 void **dwo_file_slot;
12571 struct dwo_file *dwo_file;
12572 struct dwp_file *dwp_file;
12573
12574 /* First see if there's a DWP file.
12575 If we have a DWP file but didn't find the DWO inside it, don't
12576 look for the original DWO file. It makes gdb behave differently
12577 depending on whether one is debugging in the build tree. */
12578
12579 dwp_file = get_dwp_file (per_objfile);
12580 if (dwp_file != NULL)
12581 {
12582 const struct dwp_hash_table *dwp_htab =
12583 is_debug_types ? dwp_file->tus : dwp_file->cus;
12584
12585 if (dwp_htab != NULL)
12586 {
12587 struct dwo_unit *dwo_cutu =
12588 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12589 is_debug_types);
12590
12591 if (dwo_cutu != NULL)
12592 {
12593 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12594 kind, hex_string (signature),
12595 host_address_to_string (dwo_cutu));
12596
12597 return dwo_cutu;
12598 }
12599 }
12600 }
12601 else
12602 {
12603 /* No DWP file, look for the DWO file. */
12604
12605 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12606 if (*dwo_file_slot == NULL)
12607 {
12608 /* Read in the file and build a table of the CUs/TUs it contains. */
12609 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12610 }
12611 /* NOTE: This will be NULL if unable to open the file. */
12612 dwo_file = (struct dwo_file *) *dwo_file_slot;
12613
12614 if (dwo_file != NULL)
12615 {
12616 struct dwo_unit *dwo_cutu = NULL;
12617
12618 if (is_debug_types && dwo_file->tus)
12619 {
12620 struct dwo_unit find_dwo_cutu;
12621
12622 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12623 find_dwo_cutu.signature = signature;
12624 dwo_cutu
12625 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12626 &find_dwo_cutu);
12627 }
12628 else if (!is_debug_types && dwo_file->cus)
12629 {
12630 struct dwo_unit find_dwo_cutu;
12631
12632 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12633 find_dwo_cutu.signature = signature;
12634 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12635 &find_dwo_cutu);
12636 }
12637
12638 if (dwo_cutu != NULL)
12639 {
12640 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12641 kind, dwo_name, hex_string (signature),
12642 host_address_to_string (dwo_cutu));
12643
12644 return dwo_cutu;
12645 }
12646 }
12647 }
12648
12649 /* We didn't find it. This could mean a dwo_id mismatch, or
12650 someone deleted the DWO/DWP file, or the search path isn't set up
12651 correctly to find the file. */
12652
12653 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12654 kind, dwo_name, hex_string (signature));
12655
12656 /* This is a warning and not a complaint because it can be caused by
12657 pilot error (e.g., user accidentally deleting the DWO). */
12658 {
12659 /* Print the name of the DWP file if we looked there, helps the user
12660 better diagnose the problem. */
12661 std::string dwp_text;
12662
12663 if (dwp_file != NULL)
12664 dwp_text = string_printf (" [in DWP file %s]",
12665 lbasename (dwp_file->name));
12666
12667 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12668 " [in module %s]"),
12669 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12670 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12671 }
12672 return NULL;
12673 }
12674
12675 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12676 See lookup_dwo_cutu_unit for details. */
12677
12678 static struct dwo_unit *
12679 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12680 ULONGEST signature)
12681 {
12682 gdb_assert (!cu->per_cu->is_debug_types);
12683
12684 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12685 }
12686
12687 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12688 See lookup_dwo_cutu_unit for details. */
12689
12690 static struct dwo_unit *
12691 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12692 {
12693 gdb_assert (cu->per_cu->is_debug_types);
12694
12695 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12696
12697 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12698 }
12699
12700 /* Traversal function for queue_and_load_all_dwo_tus. */
12701
12702 static int
12703 queue_and_load_dwo_tu (void **slot, void *info)
12704 {
12705 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12706 dwarf2_cu *cu = (dwarf2_cu *) info;
12707 ULONGEST signature = dwo_unit->signature;
12708 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12709
12710 if (sig_type != NULL)
12711 {
12712 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12713 a real dependency of PER_CU on SIG_TYPE. That is detected later
12714 while processing PER_CU. */
12715 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12716 cu->language))
12717 load_full_type_unit (sig_type, cu->per_objfile);
12718 cu->per_cu->imported_symtabs_push (sig_type);
12719 }
12720
12721 return 1;
12722 }
12723
12724 /* Queue all TUs contained in the DWO of CU to be read in.
12725 The DWO may have the only definition of the type, though it may not be
12726 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12727 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12728
12729 static void
12730 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12731 {
12732 struct dwo_unit *dwo_unit;
12733 struct dwo_file *dwo_file;
12734
12735 gdb_assert (cu != nullptr);
12736 gdb_assert (!cu->per_cu->is_debug_types);
12737 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12738
12739 dwo_unit = cu->dwo_unit;
12740 gdb_assert (dwo_unit != NULL);
12741
12742 dwo_file = dwo_unit->dwo_file;
12743 if (dwo_file->tus != NULL)
12744 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12745 }
12746
12747 /* Read in various DIEs. */
12748
12749 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12750 Inherit only the children of the DW_AT_abstract_origin DIE not being
12751 already referenced by DW_AT_abstract_origin from the children of the
12752 current DIE. */
12753
12754 static void
12755 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12756 {
12757 struct die_info *child_die;
12758 sect_offset *offsetp;
12759 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12760 struct die_info *origin_die;
12761 /* Iterator of the ORIGIN_DIE children. */
12762 struct die_info *origin_child_die;
12763 struct attribute *attr;
12764 struct dwarf2_cu *origin_cu;
12765 struct pending **origin_previous_list_in_scope;
12766
12767 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12768 if (!attr)
12769 return;
12770
12771 /* Note that following die references may follow to a die in a
12772 different cu. */
12773
12774 origin_cu = cu;
12775 origin_die = follow_die_ref (die, attr, &origin_cu);
12776
12777 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12778 symbols in. */
12779 origin_previous_list_in_scope = origin_cu->list_in_scope;
12780 origin_cu->list_in_scope = cu->list_in_scope;
12781
12782 if (die->tag != origin_die->tag
12783 && !(die->tag == DW_TAG_inlined_subroutine
12784 && origin_die->tag == DW_TAG_subprogram))
12785 complaint (_("DIE %s and its abstract origin %s have different tags"),
12786 sect_offset_str (die->sect_off),
12787 sect_offset_str (origin_die->sect_off));
12788
12789 /* Find if the concrete and abstract trees are structurally the
12790 same. This is a shallow traversal and it is not bullet-proof;
12791 the compiler can trick the debugger into believing that the trees
12792 are isomorphic, whereas they actually are not. However, the
12793 likelyhood of this happening is pretty low, and a full-fledged
12794 check would be an overkill. */
12795 bool are_isomorphic = true;
12796 die_info *concrete_child = die->child;
12797 die_info *abstract_child = origin_die->child;
12798 while (concrete_child != nullptr || abstract_child != nullptr)
12799 {
12800 if (concrete_child == nullptr
12801 || abstract_child == nullptr
12802 || concrete_child->tag != abstract_child->tag)
12803 {
12804 are_isomorphic = false;
12805 break;
12806 }
12807
12808 concrete_child = concrete_child->sibling;
12809 abstract_child = abstract_child->sibling;
12810 }
12811
12812 /* Walk the origin's children in parallel to the concrete children.
12813 This helps match an origin child in case the debug info misses
12814 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12815 origin tree may not have the same tree structure as the concrete
12816 DIE, though. */
12817 die_info *corresponding_abstract_child
12818 = are_isomorphic ? origin_die->child : nullptr;
12819
12820 std::vector<sect_offset> offsets;
12821
12822 for (child_die = die->child;
12823 child_die && child_die->tag;
12824 child_die = child_die->sibling)
12825 {
12826 struct die_info *child_origin_die;
12827 struct dwarf2_cu *child_origin_cu;
12828
12829 /* We are trying to process concrete instance entries:
12830 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12831 it's not relevant to our analysis here. i.e. detecting DIEs that are
12832 present in the abstract instance but not referenced in the concrete
12833 one. */
12834 if (child_die->tag == DW_TAG_call_site
12835 || child_die->tag == DW_TAG_GNU_call_site)
12836 {
12837 if (are_isomorphic)
12838 corresponding_abstract_child
12839 = corresponding_abstract_child->sibling;
12840 continue;
12841 }
12842
12843 /* For each CHILD_DIE, find the corresponding child of
12844 ORIGIN_DIE. If there is more than one layer of
12845 DW_AT_abstract_origin, follow them all; there shouldn't be,
12846 but GCC versions at least through 4.4 generate this (GCC PR
12847 40573). */
12848 child_origin_die = child_die;
12849 child_origin_cu = cu;
12850 while (1)
12851 {
12852 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12853 child_origin_cu);
12854 if (attr == NULL)
12855 break;
12856 child_origin_die = follow_die_ref (child_origin_die, attr,
12857 &child_origin_cu);
12858 }
12859
12860 /* If missing DW_AT_abstract_origin, try the corresponding child
12861 of the origin. Clang emits such lexical scopes. */
12862 if (child_origin_die == child_die
12863 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12864 && are_isomorphic
12865 && child_die->tag == DW_TAG_lexical_block)
12866 child_origin_die = corresponding_abstract_child;
12867
12868 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12869 counterpart may exist. */
12870 if (child_origin_die != child_die)
12871 {
12872 if (child_die->tag != child_origin_die->tag
12873 && !(child_die->tag == DW_TAG_inlined_subroutine
12874 && child_origin_die->tag == DW_TAG_subprogram))
12875 complaint (_("Child DIE %s and its abstract origin %s have "
12876 "different tags"),
12877 sect_offset_str (child_die->sect_off),
12878 sect_offset_str (child_origin_die->sect_off));
12879 if (child_origin_die->parent != origin_die)
12880 complaint (_("Child DIE %s and its abstract origin %s have "
12881 "different parents"),
12882 sect_offset_str (child_die->sect_off),
12883 sect_offset_str (child_origin_die->sect_off));
12884 else
12885 offsets.push_back (child_origin_die->sect_off);
12886 }
12887
12888 if (are_isomorphic)
12889 corresponding_abstract_child = corresponding_abstract_child->sibling;
12890 }
12891 std::sort (offsets.begin (), offsets.end ());
12892 sect_offset *offsets_end = offsets.data () + offsets.size ();
12893 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12894 if (offsetp[-1] == *offsetp)
12895 complaint (_("Multiple children of DIE %s refer "
12896 "to DIE %s as their abstract origin"),
12897 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12898
12899 offsetp = offsets.data ();
12900 origin_child_die = origin_die->child;
12901 while (origin_child_die && origin_child_die->tag)
12902 {
12903 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12904 while (offsetp < offsets_end
12905 && *offsetp < origin_child_die->sect_off)
12906 offsetp++;
12907 if (offsetp >= offsets_end
12908 || *offsetp > origin_child_die->sect_off)
12909 {
12910 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12911 Check whether we're already processing ORIGIN_CHILD_DIE.
12912 This can happen with mutually referenced abstract_origins.
12913 PR 16581. */
12914 if (!origin_child_die->in_process)
12915 process_die (origin_child_die, origin_cu);
12916 }
12917 origin_child_die = origin_child_die->sibling;
12918 }
12919 origin_cu->list_in_scope = origin_previous_list_in_scope;
12920
12921 if (cu != origin_cu)
12922 compute_delayed_physnames (origin_cu);
12923 }
12924
12925 static void
12926 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12927 {
12928 struct objfile *objfile = cu->per_objfile->objfile;
12929 struct gdbarch *gdbarch = objfile->arch ();
12930 struct context_stack *newobj;
12931 CORE_ADDR lowpc;
12932 CORE_ADDR highpc;
12933 struct die_info *child_die;
12934 struct attribute *attr, *call_line, *call_file;
12935 const char *name;
12936 CORE_ADDR baseaddr;
12937 struct block *block;
12938 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12939 std::vector<struct symbol *> template_args;
12940 struct template_symbol *templ_func = NULL;
12941
12942 if (inlined_func)
12943 {
12944 /* If we do not have call site information, we can't show the
12945 caller of this inlined function. That's too confusing, so
12946 only use the scope for local variables. */
12947 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12948 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12949 if (call_line == NULL || call_file == NULL)
12950 {
12951 read_lexical_block_scope (die, cu);
12952 return;
12953 }
12954 }
12955
12956 baseaddr = objfile->text_section_offset ();
12957
12958 name = dwarf2_name (die, cu);
12959
12960 /* Ignore functions with missing or empty names. These are actually
12961 illegal according to the DWARF standard. */
12962 if (name == NULL)
12963 {
12964 complaint (_("missing name for subprogram DIE at %s"),
12965 sect_offset_str (die->sect_off));
12966 return;
12967 }
12968
12969 /* Ignore functions with missing or invalid low and high pc attributes. */
12970 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12971 <= PC_BOUNDS_INVALID)
12972 {
12973 attr = dwarf2_attr (die, DW_AT_external, cu);
12974 if (attr == nullptr || !attr->as_boolean ())
12975 complaint (_("cannot get low and high bounds "
12976 "for subprogram DIE at %s"),
12977 sect_offset_str (die->sect_off));
12978 return;
12979 }
12980
12981 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12982 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12983
12984 /* If we have any template arguments, then we must allocate a
12985 different sort of symbol. */
12986 for (child_die = die->child; child_die; child_die = child_die->sibling)
12987 {
12988 if (child_die->tag == DW_TAG_template_type_param
12989 || child_die->tag == DW_TAG_template_value_param)
12990 {
12991 templ_func = new (&objfile->objfile_obstack) template_symbol;
12992 templ_func->subclass = SYMBOL_TEMPLATE;
12993 break;
12994 }
12995 }
12996
12997 gdb_assert (cu->get_builder () != nullptr);
12998 newobj = cu->get_builder ()->push_context (0, lowpc);
12999 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13000 (struct symbol *) templ_func);
13001
13002 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13003 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13004 cu->language);
13005
13006 /* If there is a location expression for DW_AT_frame_base, record
13007 it. */
13008 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13009 if (attr != nullptr)
13010 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13011
13012 /* If there is a location for the static link, record it. */
13013 newobj->static_link = NULL;
13014 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13015 if (attr != nullptr)
13016 {
13017 newobj->static_link
13018 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13019 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13020 cu->addr_type ());
13021 }
13022
13023 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13024
13025 if (die->child != NULL)
13026 {
13027 child_die = die->child;
13028 while (child_die && child_die->tag)
13029 {
13030 if (child_die->tag == DW_TAG_template_type_param
13031 || child_die->tag == DW_TAG_template_value_param)
13032 {
13033 struct symbol *arg = new_symbol (child_die, NULL, cu);
13034
13035 if (arg != NULL)
13036 template_args.push_back (arg);
13037 }
13038 else
13039 process_die (child_die, cu);
13040 child_die = child_die->sibling;
13041 }
13042 }
13043
13044 inherit_abstract_dies (die, cu);
13045
13046 /* If we have a DW_AT_specification, we might need to import using
13047 directives from the context of the specification DIE. See the
13048 comment in determine_prefix. */
13049 if (cu->language == language_cplus
13050 && dwarf2_attr (die, DW_AT_specification, cu))
13051 {
13052 struct dwarf2_cu *spec_cu = cu;
13053 struct die_info *spec_die = die_specification (die, &spec_cu);
13054
13055 while (spec_die)
13056 {
13057 child_die = spec_die->child;
13058 while (child_die && child_die->tag)
13059 {
13060 if (child_die->tag == DW_TAG_imported_module)
13061 process_die (child_die, spec_cu);
13062 child_die = child_die->sibling;
13063 }
13064
13065 /* In some cases, GCC generates specification DIEs that
13066 themselves contain DW_AT_specification attributes. */
13067 spec_die = die_specification (spec_die, &spec_cu);
13068 }
13069 }
13070
13071 struct context_stack cstk = cu->get_builder ()->pop_context ();
13072 /* Make a block for the local symbols within. */
13073 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13074 cstk.static_link, lowpc, highpc);
13075
13076 /* For C++, set the block's scope. */
13077 if ((cu->language == language_cplus
13078 || cu->language == language_fortran
13079 || cu->language == language_d
13080 || cu->language == language_rust)
13081 && cu->processing_has_namespace_info)
13082 block_set_scope (block, determine_prefix (die, cu),
13083 &objfile->objfile_obstack);
13084
13085 /* If we have address ranges, record them. */
13086 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13087
13088 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13089
13090 /* Attach template arguments to function. */
13091 if (!template_args.empty ())
13092 {
13093 gdb_assert (templ_func != NULL);
13094
13095 templ_func->n_template_arguments = template_args.size ();
13096 templ_func->template_arguments
13097 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13098 templ_func->n_template_arguments);
13099 memcpy (templ_func->template_arguments,
13100 template_args.data (),
13101 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13102
13103 /* Make sure that the symtab is set on the new symbols. Even
13104 though they don't appear in this symtab directly, other parts
13105 of gdb assume that symbols do, and this is reasonably
13106 true. */
13107 for (symbol *sym : template_args)
13108 symbol_set_symtab (sym, symbol_symtab (templ_func));
13109 }
13110
13111 /* In C++, we can have functions nested inside functions (e.g., when
13112 a function declares a class that has methods). This means that
13113 when we finish processing a function scope, we may need to go
13114 back to building a containing block's symbol lists. */
13115 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13116 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13117
13118 /* If we've finished processing a top-level function, subsequent
13119 symbols go in the file symbol list. */
13120 if (cu->get_builder ()->outermost_context_p ())
13121 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13122 }
13123
13124 /* Process all the DIES contained within a lexical block scope. Start
13125 a new scope, process the dies, and then close the scope. */
13126
13127 static void
13128 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13129 {
13130 struct objfile *objfile = cu->per_objfile->objfile;
13131 struct gdbarch *gdbarch = objfile->arch ();
13132 CORE_ADDR lowpc, highpc;
13133 struct die_info *child_die;
13134 CORE_ADDR baseaddr;
13135
13136 baseaddr = objfile->text_section_offset ();
13137
13138 /* Ignore blocks with missing or invalid low and high pc attributes. */
13139 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13140 as multiple lexical blocks? Handling children in a sane way would
13141 be nasty. Might be easier to properly extend generic blocks to
13142 describe ranges. */
13143 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13144 {
13145 case PC_BOUNDS_NOT_PRESENT:
13146 /* DW_TAG_lexical_block has no attributes, process its children as if
13147 there was no wrapping by that DW_TAG_lexical_block.
13148 GCC does no longer produces such DWARF since GCC r224161. */
13149 for (child_die = die->child;
13150 child_die != NULL && child_die->tag;
13151 child_die = child_die->sibling)
13152 {
13153 /* We might already be processing this DIE. This can happen
13154 in an unusual circumstance -- where a subroutine A
13155 appears lexically in another subroutine B, but A actually
13156 inlines B. The recursion is broken here, rather than in
13157 inherit_abstract_dies, because it seems better to simply
13158 drop concrete children here. */
13159 if (!child_die->in_process)
13160 process_die (child_die, cu);
13161 }
13162 return;
13163 case PC_BOUNDS_INVALID:
13164 return;
13165 }
13166 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13167 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13168
13169 cu->get_builder ()->push_context (0, lowpc);
13170 if (die->child != NULL)
13171 {
13172 child_die = die->child;
13173 while (child_die && child_die->tag)
13174 {
13175 process_die (child_die, cu);
13176 child_die = child_die->sibling;
13177 }
13178 }
13179 inherit_abstract_dies (die, cu);
13180 struct context_stack cstk = cu->get_builder ()->pop_context ();
13181
13182 if (*cu->get_builder ()->get_local_symbols () != NULL
13183 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13184 {
13185 struct block *block
13186 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13187 cstk.start_addr, highpc);
13188
13189 /* Note that recording ranges after traversing children, as we
13190 do here, means that recording a parent's ranges entails
13191 walking across all its children's ranges as they appear in
13192 the address map, which is quadratic behavior.
13193
13194 It would be nicer to record the parent's ranges before
13195 traversing its children, simply overriding whatever you find
13196 there. But since we don't even decide whether to create a
13197 block until after we've traversed its children, that's hard
13198 to do. */
13199 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13200 }
13201 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13202 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13203 }
13204
13205 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13206
13207 static void
13208 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13209 {
13210 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13211 struct objfile *objfile = per_objfile->objfile;
13212 struct gdbarch *gdbarch = objfile->arch ();
13213 CORE_ADDR pc, baseaddr;
13214 struct attribute *attr;
13215 struct call_site *call_site, call_site_local;
13216 void **slot;
13217 int nparams;
13218 struct die_info *child_die;
13219
13220 baseaddr = objfile->text_section_offset ();
13221
13222 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13223 if (attr == NULL)
13224 {
13225 /* This was a pre-DWARF-5 GNU extension alias
13226 for DW_AT_call_return_pc. */
13227 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13228 }
13229 if (!attr)
13230 {
13231 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13232 "DIE %s [in module %s]"),
13233 sect_offset_str (die->sect_off), objfile_name (objfile));
13234 return;
13235 }
13236 pc = attr->as_address () + baseaddr;
13237 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13238
13239 if (cu->call_site_htab == NULL)
13240 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13241 NULL, &objfile->objfile_obstack,
13242 hashtab_obstack_allocate, NULL);
13243 call_site_local.pc = pc;
13244 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13245 if (*slot != NULL)
13246 {
13247 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13248 "DIE %s [in module %s]"),
13249 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13250 objfile_name (objfile));
13251 return;
13252 }
13253
13254 /* Count parameters at the caller. */
13255
13256 nparams = 0;
13257 for (child_die = die->child; child_die && child_die->tag;
13258 child_die = child_die->sibling)
13259 {
13260 if (child_die->tag != DW_TAG_call_site_parameter
13261 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13262 {
13263 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13264 "DW_TAG_call_site child DIE %s [in module %s]"),
13265 child_die->tag, sect_offset_str (child_die->sect_off),
13266 objfile_name (objfile));
13267 continue;
13268 }
13269
13270 nparams++;
13271 }
13272
13273 call_site
13274 = ((struct call_site *)
13275 obstack_alloc (&objfile->objfile_obstack,
13276 sizeof (*call_site)
13277 + (sizeof (*call_site->parameter) * (nparams - 1))));
13278 *slot = call_site;
13279 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13280 call_site->pc = pc;
13281
13282 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13283 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13284 {
13285 struct die_info *func_die;
13286
13287 /* Skip also over DW_TAG_inlined_subroutine. */
13288 for (func_die = die->parent;
13289 func_die && func_die->tag != DW_TAG_subprogram
13290 && func_die->tag != DW_TAG_subroutine_type;
13291 func_die = func_die->parent);
13292
13293 /* DW_AT_call_all_calls is a superset
13294 of DW_AT_call_all_tail_calls. */
13295 if (func_die
13296 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13297 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13298 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13299 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13300 {
13301 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13302 not complete. But keep CALL_SITE for look ups via call_site_htab,
13303 both the initial caller containing the real return address PC and
13304 the final callee containing the current PC of a chain of tail
13305 calls do not need to have the tail call list complete. But any
13306 function candidate for a virtual tail call frame searched via
13307 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13308 determined unambiguously. */
13309 }
13310 else
13311 {
13312 struct type *func_type = NULL;
13313
13314 if (func_die)
13315 func_type = get_die_type (func_die, cu);
13316 if (func_type != NULL)
13317 {
13318 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13319
13320 /* Enlist this call site to the function. */
13321 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13322 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13323 }
13324 else
13325 complaint (_("Cannot find function owning DW_TAG_call_site "
13326 "DIE %s [in module %s]"),
13327 sect_offset_str (die->sect_off), objfile_name (objfile));
13328 }
13329 }
13330
13331 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13332 if (attr == NULL)
13333 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13334 if (attr == NULL)
13335 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13336 if (attr == NULL)
13337 {
13338 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13339 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13340 }
13341 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13342 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13343 /* Keep NULL DWARF_BLOCK. */;
13344 else if (attr->form_is_block ())
13345 {
13346 struct dwarf2_locexpr_baton *dlbaton;
13347 struct dwarf_block *block = attr->as_block ();
13348
13349 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13350 dlbaton->data = block->data;
13351 dlbaton->size = block->size;
13352 dlbaton->per_objfile = per_objfile;
13353 dlbaton->per_cu = cu->per_cu;
13354
13355 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13356 }
13357 else if (attr->form_is_ref ())
13358 {
13359 struct dwarf2_cu *target_cu = cu;
13360 struct die_info *target_die;
13361
13362 target_die = follow_die_ref (die, attr, &target_cu);
13363 gdb_assert (target_cu->per_objfile->objfile == objfile);
13364 if (die_is_declaration (target_die, target_cu))
13365 {
13366 const char *target_physname;
13367
13368 /* Prefer the mangled name; otherwise compute the demangled one. */
13369 target_physname = dw2_linkage_name (target_die, target_cu);
13370 if (target_physname == NULL)
13371 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13372 if (target_physname == NULL)
13373 complaint (_("DW_AT_call_target target DIE has invalid "
13374 "physname, for referencing DIE %s [in module %s]"),
13375 sect_offset_str (die->sect_off), objfile_name (objfile));
13376 else
13377 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13378 }
13379 else
13380 {
13381 CORE_ADDR lowpc;
13382
13383 /* DW_AT_entry_pc should be preferred. */
13384 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13385 <= PC_BOUNDS_INVALID)
13386 complaint (_("DW_AT_call_target target DIE has invalid "
13387 "low pc, for referencing DIE %s [in module %s]"),
13388 sect_offset_str (die->sect_off), objfile_name (objfile));
13389 else
13390 {
13391 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13392 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13393 }
13394 }
13395 }
13396 else
13397 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13398 "block nor reference, for DIE %s [in module %s]"),
13399 sect_offset_str (die->sect_off), objfile_name (objfile));
13400
13401 call_site->per_cu = cu->per_cu;
13402 call_site->per_objfile = per_objfile;
13403
13404 for (child_die = die->child;
13405 child_die && child_die->tag;
13406 child_die = child_die->sibling)
13407 {
13408 struct call_site_parameter *parameter;
13409 struct attribute *loc, *origin;
13410
13411 if (child_die->tag != DW_TAG_call_site_parameter
13412 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13413 {
13414 /* Already printed the complaint above. */
13415 continue;
13416 }
13417
13418 gdb_assert (call_site->parameter_count < nparams);
13419 parameter = &call_site->parameter[call_site->parameter_count];
13420
13421 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13422 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13423 register is contained in DW_AT_call_value. */
13424
13425 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13426 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13427 if (origin == NULL)
13428 {
13429 /* This was a pre-DWARF-5 GNU extension alias
13430 for DW_AT_call_parameter. */
13431 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13432 }
13433 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13434 {
13435 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13436
13437 sect_offset sect_off = origin->get_ref_die_offset ();
13438 if (!cu->header.offset_in_cu_p (sect_off))
13439 {
13440 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13441 binding can be done only inside one CU. Such referenced DIE
13442 therefore cannot be even moved to DW_TAG_partial_unit. */
13443 complaint (_("DW_AT_call_parameter offset is not in CU for "
13444 "DW_TAG_call_site child DIE %s [in module %s]"),
13445 sect_offset_str (child_die->sect_off),
13446 objfile_name (objfile));
13447 continue;
13448 }
13449 parameter->u.param_cu_off
13450 = (cu_offset) (sect_off - cu->header.sect_off);
13451 }
13452 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13453 {
13454 complaint (_("No DW_FORM_block* DW_AT_location for "
13455 "DW_TAG_call_site child DIE %s [in module %s]"),
13456 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13457 continue;
13458 }
13459 else
13460 {
13461 struct dwarf_block *block = loc->as_block ();
13462
13463 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13464 (block->data, &block->data[block->size]);
13465 if (parameter->u.dwarf_reg != -1)
13466 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13467 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13468 &block->data[block->size],
13469 &parameter->u.fb_offset))
13470 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13471 else
13472 {
13473 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13474 "for DW_FORM_block* DW_AT_location is supported for "
13475 "DW_TAG_call_site child DIE %s "
13476 "[in module %s]"),
13477 sect_offset_str (child_die->sect_off),
13478 objfile_name (objfile));
13479 continue;
13480 }
13481 }
13482
13483 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13484 if (attr == NULL)
13485 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13486 if (attr == NULL || !attr->form_is_block ())
13487 {
13488 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13489 "DW_TAG_call_site child DIE %s [in module %s]"),
13490 sect_offset_str (child_die->sect_off),
13491 objfile_name (objfile));
13492 continue;
13493 }
13494
13495 struct dwarf_block *block = attr->as_block ();
13496 parameter->value = block->data;
13497 parameter->value_size = block->size;
13498
13499 /* Parameters are not pre-cleared by memset above. */
13500 parameter->data_value = NULL;
13501 parameter->data_value_size = 0;
13502 call_site->parameter_count++;
13503
13504 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13505 if (attr == NULL)
13506 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13507 if (attr != nullptr)
13508 {
13509 if (!attr->form_is_block ())
13510 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13511 "DW_TAG_call_site child DIE %s [in module %s]"),
13512 sect_offset_str (child_die->sect_off),
13513 objfile_name (objfile));
13514 else
13515 {
13516 block = attr->as_block ();
13517 parameter->data_value = block->data;
13518 parameter->data_value_size = block->size;
13519 }
13520 }
13521 }
13522 }
13523
13524 /* Helper function for read_variable. If DIE represents a virtual
13525 table, then return the type of the concrete object that is
13526 associated with the virtual table. Otherwise, return NULL. */
13527
13528 static struct type *
13529 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13530 {
13531 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13532 if (attr == NULL)
13533 return NULL;
13534
13535 /* Find the type DIE. */
13536 struct die_info *type_die = NULL;
13537 struct dwarf2_cu *type_cu = cu;
13538
13539 if (attr->form_is_ref ())
13540 type_die = follow_die_ref (die, attr, &type_cu);
13541 if (type_die == NULL)
13542 return NULL;
13543
13544 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13545 return NULL;
13546 return die_containing_type (type_die, type_cu);
13547 }
13548
13549 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13550
13551 static void
13552 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13553 {
13554 struct rust_vtable_symbol *storage = NULL;
13555
13556 if (cu->language == language_rust)
13557 {
13558 struct type *containing_type = rust_containing_type (die, cu);
13559
13560 if (containing_type != NULL)
13561 {
13562 struct objfile *objfile = cu->per_objfile->objfile;
13563
13564 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13565 storage->concrete_type = containing_type;
13566 storage->subclass = SYMBOL_RUST_VTABLE;
13567 }
13568 }
13569
13570 struct symbol *res = new_symbol (die, NULL, cu, storage);
13571 struct attribute *abstract_origin
13572 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13573 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13574 if (res == NULL && loc && abstract_origin)
13575 {
13576 /* We have a variable without a name, but with a location and an abstract
13577 origin. This may be a concrete instance of an abstract variable
13578 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13579 later. */
13580 struct dwarf2_cu *origin_cu = cu;
13581 struct die_info *origin_die
13582 = follow_die_ref (die, abstract_origin, &origin_cu);
13583 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13584 per_objfile->per_bfd->abstract_to_concrete
13585 [origin_die->sect_off].push_back (die->sect_off);
13586 }
13587 }
13588
13589 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13590 reading .debug_rnglists.
13591 Callback's type should be:
13592 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13593 Return true if the attributes are present and valid, otherwise,
13594 return false. */
13595
13596 template <typename Callback>
13597 static bool
13598 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13599 dwarf_tag tag, Callback &&callback)
13600 {
13601 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13602 struct objfile *objfile = per_objfile->objfile;
13603 bfd *obfd = objfile->obfd;
13604 /* Base address selection entry. */
13605 gdb::optional<CORE_ADDR> base;
13606 const gdb_byte *buffer;
13607 CORE_ADDR baseaddr;
13608 bool overflow = false;
13609 ULONGEST addr_index;
13610 struct dwarf2_section_info *rnglists_section;
13611
13612 base = cu->base_address;
13613 rnglists_section = cu_debug_rnglists_section (cu, tag);
13614 rnglists_section->read (objfile);
13615
13616 if (offset >= rnglists_section->size)
13617 {
13618 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13619 offset);
13620 return false;
13621 }
13622 buffer = rnglists_section->buffer + offset;
13623
13624 baseaddr = objfile->text_section_offset ();
13625
13626 while (1)
13627 {
13628 /* Initialize it due to a false compiler warning. */
13629 CORE_ADDR range_beginning = 0, range_end = 0;
13630 const gdb_byte *buf_end = (rnglists_section->buffer
13631 + rnglists_section->size);
13632 unsigned int bytes_read;
13633
13634 if (buffer == buf_end)
13635 {
13636 overflow = true;
13637 break;
13638 }
13639 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13640 switch (rlet)
13641 {
13642 case DW_RLE_end_of_list:
13643 break;
13644 case DW_RLE_base_address:
13645 if (buffer + cu->header.addr_size > buf_end)
13646 {
13647 overflow = true;
13648 break;
13649 }
13650 base = cu->header.read_address (obfd, buffer, &bytes_read);
13651 buffer += bytes_read;
13652 break;
13653 case DW_RLE_base_addressx:
13654 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13655 buffer += bytes_read;
13656 base = read_addr_index (cu, addr_index);
13657 break;
13658 case DW_RLE_start_length:
13659 if (buffer + cu->header.addr_size > buf_end)
13660 {
13661 overflow = true;
13662 break;
13663 }
13664 range_beginning = cu->header.read_address (obfd, buffer,
13665 &bytes_read);
13666 buffer += bytes_read;
13667 range_end = (range_beginning
13668 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13669 buffer += bytes_read;
13670 if (buffer > buf_end)
13671 {
13672 overflow = true;
13673 break;
13674 }
13675 break;
13676 case DW_RLE_startx_length:
13677 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13678 buffer += bytes_read;
13679 range_beginning = read_addr_index (cu, addr_index);
13680 if (buffer > buf_end)
13681 {
13682 overflow = true;
13683 break;
13684 }
13685 range_end = (range_beginning
13686 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13687 buffer += bytes_read;
13688 break;
13689 case DW_RLE_offset_pair:
13690 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13691 buffer += bytes_read;
13692 if (buffer > buf_end)
13693 {
13694 overflow = true;
13695 break;
13696 }
13697 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13698 buffer += bytes_read;
13699 if (buffer > buf_end)
13700 {
13701 overflow = true;
13702 break;
13703 }
13704 break;
13705 case DW_RLE_start_end:
13706 if (buffer + 2 * cu->header.addr_size > buf_end)
13707 {
13708 overflow = true;
13709 break;
13710 }
13711 range_beginning = cu->header.read_address (obfd, buffer,
13712 &bytes_read);
13713 buffer += bytes_read;
13714 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13715 buffer += bytes_read;
13716 break;
13717 case DW_RLE_startx_endx:
13718 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13719 buffer += bytes_read;
13720 range_beginning = read_addr_index (cu, addr_index);
13721 if (buffer > buf_end)
13722 {
13723 overflow = true;
13724 break;
13725 }
13726 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13727 buffer += bytes_read;
13728 range_end = read_addr_index (cu, addr_index);
13729 break;
13730 default:
13731 complaint (_("Invalid .debug_rnglists data (no base address)"));
13732 return false;
13733 }
13734 if (rlet == DW_RLE_end_of_list || overflow)
13735 break;
13736 if (rlet == DW_RLE_base_address)
13737 continue;
13738
13739 if (range_beginning > range_end)
13740 {
13741 /* Inverted range entries are invalid. */
13742 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13743 return false;
13744 }
13745
13746 /* Empty range entries have no effect. */
13747 if (range_beginning == range_end)
13748 continue;
13749
13750 /* Only DW_RLE_offset_pair needs the base address added. */
13751 if (rlet == DW_RLE_offset_pair)
13752 {
13753 if (!base.has_value ())
13754 {
13755 /* We have no valid base address for the DW_RLE_offset_pair. */
13756 complaint (_("Invalid .debug_rnglists data (no base address for "
13757 "DW_RLE_offset_pair)"));
13758 return false;
13759 }
13760
13761 range_beginning += *base;
13762 range_end += *base;
13763 }
13764
13765 /* A not-uncommon case of bad debug info.
13766 Don't pollute the addrmap with bad data. */
13767 if (range_beginning + baseaddr == 0
13768 && !per_objfile->per_bfd->has_section_at_zero)
13769 {
13770 complaint (_(".debug_rnglists entry has start address of zero"
13771 " [in module %s]"), objfile_name (objfile));
13772 continue;
13773 }
13774
13775 callback (range_beginning, range_end);
13776 }
13777
13778 if (overflow)
13779 {
13780 complaint (_("Offset %d is not terminated "
13781 "for DW_AT_ranges attribute"),
13782 offset);
13783 return false;
13784 }
13785
13786 return true;
13787 }
13788
13789 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13790 Callback's type should be:
13791 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13792 Return 1 if the attributes are present and valid, otherwise, return 0. */
13793
13794 template <typename Callback>
13795 static int
13796 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
13797 Callback &&callback)
13798 {
13799 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13800 struct objfile *objfile = per_objfile->objfile;
13801 struct comp_unit_head *cu_header = &cu->header;
13802 bfd *obfd = objfile->obfd;
13803 unsigned int addr_size = cu_header->addr_size;
13804 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13805 /* Base address selection entry. */
13806 gdb::optional<CORE_ADDR> base;
13807 unsigned int dummy;
13808 const gdb_byte *buffer;
13809 CORE_ADDR baseaddr;
13810
13811 if (cu_header->version >= 5)
13812 return dwarf2_rnglists_process (offset, cu, tag, callback);
13813
13814 base = cu->base_address;
13815
13816 per_objfile->per_bfd->ranges.read (objfile);
13817 if (offset >= per_objfile->per_bfd->ranges.size)
13818 {
13819 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13820 offset);
13821 return 0;
13822 }
13823 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13824
13825 baseaddr = objfile->text_section_offset ();
13826
13827 while (1)
13828 {
13829 CORE_ADDR range_beginning, range_end;
13830
13831 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13832 buffer += addr_size;
13833 range_end = cu->header.read_address (obfd, buffer, &dummy);
13834 buffer += addr_size;
13835 offset += 2 * addr_size;
13836
13837 /* An end of list marker is a pair of zero addresses. */
13838 if (range_beginning == 0 && range_end == 0)
13839 /* Found the end of list entry. */
13840 break;
13841
13842 /* Each base address selection entry is a pair of 2 values.
13843 The first is the largest possible address, the second is
13844 the base address. Check for a base address here. */
13845 if ((range_beginning & mask) == mask)
13846 {
13847 /* If we found the largest possible address, then we already
13848 have the base address in range_end. */
13849 base = range_end;
13850 continue;
13851 }
13852
13853 if (!base.has_value ())
13854 {
13855 /* We have no valid base address for the ranges
13856 data. */
13857 complaint (_("Invalid .debug_ranges data (no base address)"));
13858 return 0;
13859 }
13860
13861 if (range_beginning > range_end)
13862 {
13863 /* Inverted range entries are invalid. */
13864 complaint (_("Invalid .debug_ranges data (inverted range)"));
13865 return 0;
13866 }
13867
13868 /* Empty range entries have no effect. */
13869 if (range_beginning == range_end)
13870 continue;
13871
13872 range_beginning += *base;
13873 range_end += *base;
13874
13875 /* A not-uncommon case of bad debug info.
13876 Don't pollute the addrmap with bad data. */
13877 if (range_beginning + baseaddr == 0
13878 && !per_objfile->per_bfd->has_section_at_zero)
13879 {
13880 complaint (_(".debug_ranges entry has start address of zero"
13881 " [in module %s]"), objfile_name (objfile));
13882 continue;
13883 }
13884
13885 callback (range_beginning, range_end);
13886 }
13887
13888 return 1;
13889 }
13890
13891 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13892 Return 1 if the attributes are present and valid, otherwise, return 0.
13893 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
13894
13895 static int
13896 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13897 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13898 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
13899 {
13900 struct objfile *objfile = cu->per_objfile->objfile;
13901 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
13902 struct gdbarch *gdbarch = objfile->arch ();
13903 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13904 int low_set = 0;
13905 CORE_ADDR low = 0;
13906 CORE_ADDR high = 0;
13907 int retval;
13908
13909 retval = dwarf2_ranges_process (offset, cu, tag,
13910 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13911 {
13912 if (ranges_pst != NULL)
13913 {
13914 CORE_ADDR lowpc;
13915 CORE_ADDR highpc;
13916
13917 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13918 range_beginning + baseaddr)
13919 - baseaddr);
13920 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13921 range_end + baseaddr)
13922 - baseaddr);
13923 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
13924 lowpc, highpc - 1, ranges_pst);
13925 }
13926
13927 /* FIXME: This is recording everything as a low-high
13928 segment of consecutive addresses. We should have a
13929 data structure for discontiguous block ranges
13930 instead. */
13931 if (! low_set)
13932 {
13933 low = range_beginning;
13934 high = range_end;
13935 low_set = 1;
13936 }
13937 else
13938 {
13939 if (range_beginning < low)
13940 low = range_beginning;
13941 if (range_end > high)
13942 high = range_end;
13943 }
13944 });
13945 if (!retval)
13946 return 0;
13947
13948 if (! low_set)
13949 /* If the first entry is an end-of-list marker, the range
13950 describes an empty scope, i.e. no instructions. */
13951 return 0;
13952
13953 if (low_return)
13954 *low_return = low;
13955 if (high_return)
13956 *high_return = high;
13957 return 1;
13958 }
13959
13960 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13961 definition for the return value. *LOWPC and *HIGHPC are set iff
13962 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13963
13964 static enum pc_bounds_kind
13965 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13966 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13967 dwarf2_psymtab *pst)
13968 {
13969 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13970 struct attribute *attr;
13971 struct attribute *attr_high;
13972 CORE_ADDR low = 0;
13973 CORE_ADDR high = 0;
13974 enum pc_bounds_kind ret;
13975
13976 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13977 if (attr_high)
13978 {
13979 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13980 if (attr != nullptr)
13981 {
13982 low = attr->as_address ();
13983 high = attr_high->as_address ();
13984 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13985 high += low;
13986 }
13987 else
13988 /* Found high w/o low attribute. */
13989 return PC_BOUNDS_INVALID;
13990
13991 /* Found consecutive range of addresses. */
13992 ret = PC_BOUNDS_HIGH_LOW;
13993 }
13994 else
13995 {
13996 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13997 if (attr != nullptr && attr->form_is_unsigned ())
13998 {
13999 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14000 on DWARF version). */
14001 ULONGEST ranges_offset = attr->as_unsigned ();
14002
14003 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14004 this value. */
14005 if (die->tag != DW_TAG_compile_unit)
14006 ranges_offset += cu->gnu_ranges_base;
14007
14008 /* Value of the DW_AT_ranges attribute is the offset in the
14009 .debug_ranges section. */
14010 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14011 die->tag))
14012 return PC_BOUNDS_INVALID;
14013 /* Found discontinuous range of addresses. */
14014 ret = PC_BOUNDS_RANGES;
14015 }
14016 else
14017 return PC_BOUNDS_NOT_PRESENT;
14018 }
14019
14020 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14021 if (high <= low)
14022 return PC_BOUNDS_INVALID;
14023
14024 /* When using the GNU linker, .gnu.linkonce. sections are used to
14025 eliminate duplicate copies of functions and vtables and such.
14026 The linker will arbitrarily choose one and discard the others.
14027 The AT_*_pc values for such functions refer to local labels in
14028 these sections. If the section from that file was discarded, the
14029 labels are not in the output, so the relocs get a value of 0.
14030 If this is a discarded function, mark the pc bounds as invalid,
14031 so that GDB will ignore it. */
14032 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14033 return PC_BOUNDS_INVALID;
14034
14035 *lowpc = low;
14036 if (highpc)
14037 *highpc = high;
14038 return ret;
14039 }
14040
14041 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14042 its low and high PC addresses. Do nothing if these addresses could not
14043 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14044 and HIGHPC to the high address if greater than HIGHPC. */
14045
14046 static void
14047 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14048 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14049 struct dwarf2_cu *cu)
14050 {
14051 CORE_ADDR low, high;
14052 struct die_info *child = die->child;
14053
14054 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14055 {
14056 *lowpc = std::min (*lowpc, low);
14057 *highpc = std::max (*highpc, high);
14058 }
14059
14060 /* If the language does not allow nested subprograms (either inside
14061 subprograms or lexical blocks), we're done. */
14062 if (cu->language != language_ada)
14063 return;
14064
14065 /* Check all the children of the given DIE. If it contains nested
14066 subprograms, then check their pc bounds. Likewise, we need to
14067 check lexical blocks as well, as they may also contain subprogram
14068 definitions. */
14069 while (child && child->tag)
14070 {
14071 if (child->tag == DW_TAG_subprogram
14072 || child->tag == DW_TAG_lexical_block)
14073 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14074 child = child->sibling;
14075 }
14076 }
14077
14078 /* Get the low and high pc's represented by the scope DIE, and store
14079 them in *LOWPC and *HIGHPC. If the correct values can't be
14080 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14081
14082 static void
14083 get_scope_pc_bounds (struct die_info *die,
14084 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14085 struct dwarf2_cu *cu)
14086 {
14087 CORE_ADDR best_low = (CORE_ADDR) -1;
14088 CORE_ADDR best_high = (CORE_ADDR) 0;
14089 CORE_ADDR current_low, current_high;
14090
14091 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14092 >= PC_BOUNDS_RANGES)
14093 {
14094 best_low = current_low;
14095 best_high = current_high;
14096 }
14097 else
14098 {
14099 struct die_info *child = die->child;
14100
14101 while (child && child->tag)
14102 {
14103 switch (child->tag) {
14104 case DW_TAG_subprogram:
14105 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14106 break;
14107 case DW_TAG_namespace:
14108 case DW_TAG_module:
14109 /* FIXME: carlton/2004-01-16: Should we do this for
14110 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14111 that current GCC's always emit the DIEs corresponding
14112 to definitions of methods of classes as children of a
14113 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14114 the DIEs giving the declarations, which could be
14115 anywhere). But I don't see any reason why the
14116 standards says that they have to be there. */
14117 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14118
14119 if (current_low != ((CORE_ADDR) -1))
14120 {
14121 best_low = std::min (best_low, current_low);
14122 best_high = std::max (best_high, current_high);
14123 }
14124 break;
14125 default:
14126 /* Ignore. */
14127 break;
14128 }
14129
14130 child = child->sibling;
14131 }
14132 }
14133
14134 *lowpc = best_low;
14135 *highpc = best_high;
14136 }
14137
14138 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14139 in DIE. */
14140
14141 static void
14142 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14143 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14144 {
14145 struct objfile *objfile = cu->per_objfile->objfile;
14146 struct gdbarch *gdbarch = objfile->arch ();
14147 struct attribute *attr;
14148 struct attribute *attr_high;
14149
14150 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14151 if (attr_high)
14152 {
14153 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14154 if (attr != nullptr)
14155 {
14156 CORE_ADDR low = attr->as_address ();
14157 CORE_ADDR high = attr_high->as_address ();
14158
14159 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14160 high += low;
14161
14162 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14163 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14164 cu->get_builder ()->record_block_range (block, low, high - 1);
14165 }
14166 }
14167
14168 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14169 if (attr != nullptr && attr->form_is_unsigned ())
14170 {
14171 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14172 on DWARF version). */
14173 ULONGEST ranges_offset = attr->as_unsigned ();
14174
14175 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14176 this value. */
14177 if (die->tag != DW_TAG_compile_unit)
14178 ranges_offset += cu->gnu_ranges_base;
14179
14180 std::vector<blockrange> blockvec;
14181 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14182 [&] (CORE_ADDR start, CORE_ADDR end)
14183 {
14184 start += baseaddr;
14185 end += baseaddr;
14186 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14187 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14188 cu->get_builder ()->record_block_range (block, start, end - 1);
14189 blockvec.emplace_back (start, end);
14190 });
14191
14192 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14193 }
14194 }
14195
14196 /* Check whether the producer field indicates either of GCC < 4.6, or the
14197 Intel C/C++ compiler, and cache the result in CU. */
14198
14199 static void
14200 check_producer (struct dwarf2_cu *cu)
14201 {
14202 int major, minor;
14203
14204 if (cu->producer == NULL)
14205 {
14206 /* For unknown compilers expect their behavior is DWARF version
14207 compliant.
14208
14209 GCC started to support .debug_types sections by -gdwarf-4 since
14210 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14211 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14212 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14213 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14214 }
14215 else if (producer_is_gcc (cu->producer, &major, &minor))
14216 {
14217 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14218 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14219 }
14220 else if (producer_is_icc (cu->producer, &major, &minor))
14221 {
14222 cu->producer_is_icc = true;
14223 cu->producer_is_icc_lt_14 = major < 14;
14224 }
14225 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14226 cu->producer_is_codewarrior = true;
14227 else
14228 {
14229 /* For other non-GCC compilers, expect their behavior is DWARF version
14230 compliant. */
14231 }
14232
14233 cu->checked_producer = true;
14234 }
14235
14236 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14237 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14238 during 4.6.0 experimental. */
14239
14240 static bool
14241 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14242 {
14243 if (!cu->checked_producer)
14244 check_producer (cu);
14245
14246 return cu->producer_is_gxx_lt_4_6;
14247 }
14248
14249
14250 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14251 with incorrect is_stmt attributes. */
14252
14253 static bool
14254 producer_is_codewarrior (struct dwarf2_cu *cu)
14255 {
14256 if (!cu->checked_producer)
14257 check_producer (cu);
14258
14259 return cu->producer_is_codewarrior;
14260 }
14261
14262 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14263 If that attribute is not available, return the appropriate
14264 default. */
14265
14266 static enum dwarf_access_attribute
14267 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14268 {
14269 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14270 if (attr != nullptr)
14271 {
14272 LONGEST value = attr->constant_value (-1);
14273 if (value == DW_ACCESS_public
14274 || value == DW_ACCESS_protected
14275 || value == DW_ACCESS_private)
14276 return (dwarf_access_attribute) value;
14277 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14278 plongest (value));
14279 }
14280
14281 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14282 {
14283 /* The default DWARF 2 accessibility for members is public, the default
14284 accessibility for inheritance is private. */
14285
14286 if (die->tag != DW_TAG_inheritance)
14287 return DW_ACCESS_public;
14288 else
14289 return DW_ACCESS_private;
14290 }
14291 else
14292 {
14293 /* DWARF 3+ defines the default accessibility a different way. The same
14294 rules apply now for DW_TAG_inheritance as for the members and it only
14295 depends on the container kind. */
14296
14297 if (die->parent->tag == DW_TAG_class_type)
14298 return DW_ACCESS_private;
14299 else
14300 return DW_ACCESS_public;
14301 }
14302 }
14303
14304 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14305 offset. If the attribute was not found return 0, otherwise return
14306 1. If it was found but could not properly be handled, set *OFFSET
14307 to 0. */
14308
14309 static int
14310 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14311 LONGEST *offset)
14312 {
14313 struct attribute *attr;
14314
14315 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14316 if (attr != NULL)
14317 {
14318 *offset = 0;
14319
14320 /* Note that we do not check for a section offset first here.
14321 This is because DW_AT_data_member_location is new in DWARF 4,
14322 so if we see it, we can assume that a constant form is really
14323 a constant and not a section offset. */
14324 if (attr->form_is_constant ())
14325 *offset = attr->constant_value (0);
14326 else if (attr->form_is_section_offset ())
14327 dwarf2_complex_location_expr_complaint ();
14328 else if (attr->form_is_block ())
14329 *offset = decode_locdesc (attr->as_block (), cu);
14330 else
14331 dwarf2_complex_location_expr_complaint ();
14332
14333 return 1;
14334 }
14335
14336 return 0;
14337 }
14338
14339 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14340
14341 static void
14342 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14343 struct field *field)
14344 {
14345 struct attribute *attr;
14346
14347 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14348 if (attr != NULL)
14349 {
14350 if (attr->form_is_constant ())
14351 {
14352 LONGEST offset = attr->constant_value (0);
14353 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14354 }
14355 else if (attr->form_is_section_offset ())
14356 dwarf2_complex_location_expr_complaint ();
14357 else if (attr->form_is_block ())
14358 {
14359 bool handled;
14360 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14361 if (handled)
14362 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14363 else
14364 {
14365 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14366 struct objfile *objfile = per_objfile->objfile;
14367 struct dwarf2_locexpr_baton *dlbaton
14368 = XOBNEW (&objfile->objfile_obstack,
14369 struct dwarf2_locexpr_baton);
14370 dlbaton->data = attr->as_block ()->data;
14371 dlbaton->size = attr->as_block ()->size;
14372 /* When using this baton, we want to compute the address
14373 of the field, not the value. This is why
14374 is_reference is set to false here. */
14375 dlbaton->is_reference = false;
14376 dlbaton->per_objfile = per_objfile;
14377 dlbaton->per_cu = cu->per_cu;
14378
14379 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14380 }
14381 }
14382 else
14383 dwarf2_complex_location_expr_complaint ();
14384 }
14385 }
14386
14387 /* Add an aggregate field to the field list. */
14388
14389 static void
14390 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14391 struct dwarf2_cu *cu)
14392 {
14393 struct objfile *objfile = cu->per_objfile->objfile;
14394 struct gdbarch *gdbarch = objfile->arch ();
14395 struct nextfield *new_field;
14396 struct attribute *attr;
14397 struct field *fp;
14398 const char *fieldname = "";
14399
14400 if (die->tag == DW_TAG_inheritance)
14401 {
14402 fip->baseclasses.emplace_back ();
14403 new_field = &fip->baseclasses.back ();
14404 }
14405 else
14406 {
14407 fip->fields.emplace_back ();
14408 new_field = &fip->fields.back ();
14409 }
14410
14411 new_field->offset = die->sect_off;
14412
14413 new_field->accessibility = dwarf2_access_attribute (die, cu);
14414 if (new_field->accessibility != DW_ACCESS_public)
14415 fip->non_public_fields = true;
14416
14417 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14418 if (attr != nullptr)
14419 new_field->virtuality = attr->as_virtuality ();
14420 else
14421 new_field->virtuality = DW_VIRTUALITY_none;
14422
14423 fp = &new_field->field;
14424
14425 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14426 {
14427 /* Data member other than a C++ static data member. */
14428
14429 /* Get type of field. */
14430 fp->set_type (die_type (die, cu));
14431
14432 SET_FIELD_BITPOS (*fp, 0);
14433
14434 /* Get bit size of field (zero if none). */
14435 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14436 if (attr != nullptr)
14437 {
14438 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14439 }
14440 else
14441 {
14442 FIELD_BITSIZE (*fp) = 0;
14443 }
14444
14445 /* Get bit offset of field. */
14446 handle_data_member_location (die, cu, fp);
14447 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14448 if (attr != nullptr && attr->form_is_constant ())
14449 {
14450 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14451 {
14452 /* For big endian bits, the DW_AT_bit_offset gives the
14453 additional bit offset from the MSB of the containing
14454 anonymous object to the MSB of the field. We don't
14455 have to do anything special since we don't need to
14456 know the size of the anonymous object. */
14457 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14458 + attr->constant_value (0)));
14459 }
14460 else
14461 {
14462 /* For little endian bits, compute the bit offset to the
14463 MSB of the anonymous object, subtract off the number of
14464 bits from the MSB of the field to the MSB of the
14465 object, and then subtract off the number of bits of
14466 the field itself. The result is the bit offset of
14467 the LSB of the field. */
14468 int anonymous_size;
14469 int bit_offset = attr->constant_value (0);
14470
14471 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14472 if (attr != nullptr && attr->form_is_constant ())
14473 {
14474 /* The size of the anonymous object containing
14475 the bit field is explicit, so use the
14476 indicated size (in bytes). */
14477 anonymous_size = attr->constant_value (0);
14478 }
14479 else
14480 {
14481 /* The size of the anonymous object containing
14482 the bit field must be inferred from the type
14483 attribute of the data member containing the
14484 bit field. */
14485 anonymous_size = TYPE_LENGTH (fp->type ());
14486 }
14487 SET_FIELD_BITPOS (*fp,
14488 (FIELD_BITPOS (*fp)
14489 + anonymous_size * bits_per_byte
14490 - bit_offset - FIELD_BITSIZE (*fp)));
14491 }
14492 }
14493 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14494 if (attr != NULL)
14495 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14496 + attr->constant_value (0)));
14497
14498 /* Get name of field. */
14499 fieldname = dwarf2_name (die, cu);
14500 if (fieldname == NULL)
14501 fieldname = "";
14502
14503 /* The name is already allocated along with this objfile, so we don't
14504 need to duplicate it for the type. */
14505 fp->name = fieldname;
14506
14507 /* Change accessibility for artificial fields (e.g. virtual table
14508 pointer or virtual base class pointer) to private. */
14509 if (dwarf2_attr (die, DW_AT_artificial, cu))
14510 {
14511 FIELD_ARTIFICIAL (*fp) = 1;
14512 new_field->accessibility = DW_ACCESS_private;
14513 fip->non_public_fields = true;
14514 }
14515 }
14516 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14517 {
14518 /* C++ static member. */
14519
14520 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14521 is a declaration, but all versions of G++ as of this writing
14522 (so through at least 3.2.1) incorrectly generate
14523 DW_TAG_variable tags. */
14524
14525 const char *physname;
14526
14527 /* Get name of field. */
14528 fieldname = dwarf2_name (die, cu);
14529 if (fieldname == NULL)
14530 return;
14531
14532 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14533 if (attr
14534 /* Only create a symbol if this is an external value.
14535 new_symbol checks this and puts the value in the global symbol
14536 table, which we want. If it is not external, new_symbol
14537 will try to put the value in cu->list_in_scope which is wrong. */
14538 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14539 {
14540 /* A static const member, not much different than an enum as far as
14541 we're concerned, except that we can support more types. */
14542 new_symbol (die, NULL, cu);
14543 }
14544
14545 /* Get physical name. */
14546 physname = dwarf2_physname (fieldname, die, cu);
14547
14548 /* The name is already allocated along with this objfile, so we don't
14549 need to duplicate it for the type. */
14550 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14551 fp->set_type (die_type (die, cu));
14552 FIELD_NAME (*fp) = fieldname;
14553 }
14554 else if (die->tag == DW_TAG_inheritance)
14555 {
14556 /* C++ base class field. */
14557 handle_data_member_location (die, cu, fp);
14558 FIELD_BITSIZE (*fp) = 0;
14559 fp->set_type (die_type (die, cu));
14560 FIELD_NAME (*fp) = fp->type ()->name ();
14561 }
14562 else
14563 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14564 }
14565
14566 /* Can the type given by DIE define another type? */
14567
14568 static bool
14569 type_can_define_types (const struct die_info *die)
14570 {
14571 switch (die->tag)
14572 {
14573 case DW_TAG_typedef:
14574 case DW_TAG_class_type:
14575 case DW_TAG_structure_type:
14576 case DW_TAG_union_type:
14577 case DW_TAG_enumeration_type:
14578 return true;
14579
14580 default:
14581 return false;
14582 }
14583 }
14584
14585 /* Add a type definition defined in the scope of the FIP's class. */
14586
14587 static void
14588 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14589 struct dwarf2_cu *cu)
14590 {
14591 struct decl_field fp;
14592 memset (&fp, 0, sizeof (fp));
14593
14594 gdb_assert (type_can_define_types (die));
14595
14596 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14597 fp.name = dwarf2_name (die, cu);
14598 fp.type = read_type_die (die, cu);
14599
14600 /* Save accessibility. */
14601 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14602 switch (accessibility)
14603 {
14604 case DW_ACCESS_public:
14605 /* The assumed value if neither private nor protected. */
14606 break;
14607 case DW_ACCESS_private:
14608 fp.is_private = 1;
14609 break;
14610 case DW_ACCESS_protected:
14611 fp.is_protected = 1;
14612 break;
14613 }
14614
14615 if (die->tag == DW_TAG_typedef)
14616 fip->typedef_field_list.push_back (fp);
14617 else
14618 fip->nested_types_list.push_back (fp);
14619 }
14620
14621 /* A convenience typedef that's used when finding the discriminant
14622 field for a variant part. */
14623 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14624 offset_map_type;
14625
14626 /* Compute the discriminant range for a given variant. OBSTACK is
14627 where the results will be stored. VARIANT is the variant to
14628 process. IS_UNSIGNED indicates whether the discriminant is signed
14629 or unsigned. */
14630
14631 static const gdb::array_view<discriminant_range>
14632 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14633 bool is_unsigned)
14634 {
14635 std::vector<discriminant_range> ranges;
14636
14637 if (variant.default_branch)
14638 return {};
14639
14640 if (variant.discr_list_data == nullptr)
14641 {
14642 discriminant_range r
14643 = {variant.discriminant_value, variant.discriminant_value};
14644 ranges.push_back (r);
14645 }
14646 else
14647 {
14648 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14649 variant.discr_list_data->size);
14650 while (!data.empty ())
14651 {
14652 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14653 {
14654 complaint (_("invalid discriminant marker: %d"), data[0]);
14655 break;
14656 }
14657 bool is_range = data[0] == DW_DSC_range;
14658 data = data.slice (1);
14659
14660 ULONGEST low, high;
14661 unsigned int bytes_read;
14662
14663 if (data.empty ())
14664 {
14665 complaint (_("DW_AT_discr_list missing low value"));
14666 break;
14667 }
14668 if (is_unsigned)
14669 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14670 else
14671 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14672 &bytes_read);
14673 data = data.slice (bytes_read);
14674
14675 if (is_range)
14676 {
14677 if (data.empty ())
14678 {
14679 complaint (_("DW_AT_discr_list missing high value"));
14680 break;
14681 }
14682 if (is_unsigned)
14683 high = read_unsigned_leb128 (nullptr, data.data (),
14684 &bytes_read);
14685 else
14686 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14687 &bytes_read);
14688 data = data.slice (bytes_read);
14689 }
14690 else
14691 high = low;
14692
14693 ranges.push_back ({ low, high });
14694 }
14695 }
14696
14697 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14698 ranges.size ());
14699 std::copy (ranges.begin (), ranges.end (), result);
14700 return gdb::array_view<discriminant_range> (result, ranges.size ());
14701 }
14702
14703 static const gdb::array_view<variant_part> create_variant_parts
14704 (struct obstack *obstack,
14705 const offset_map_type &offset_map,
14706 struct field_info *fi,
14707 const std::vector<variant_part_builder> &variant_parts);
14708
14709 /* Fill in a "struct variant" for a given variant field. RESULT is
14710 the variant to fill in. OBSTACK is where any needed allocations
14711 will be done. OFFSET_MAP holds the mapping from section offsets to
14712 fields for the type. FI describes the fields of the type we're
14713 processing. FIELD is the variant field we're converting. */
14714
14715 static void
14716 create_one_variant (variant &result, struct obstack *obstack,
14717 const offset_map_type &offset_map,
14718 struct field_info *fi, const variant_field &field)
14719 {
14720 result.discriminants = convert_variant_range (obstack, field, false);
14721 result.first_field = field.first_field + fi->baseclasses.size ();
14722 result.last_field = field.last_field + fi->baseclasses.size ();
14723 result.parts = create_variant_parts (obstack, offset_map, fi,
14724 field.variant_parts);
14725 }
14726
14727 /* Fill in a "struct variant_part" for a given variant part. RESULT
14728 is the variant part to fill in. OBSTACK is where any needed
14729 allocations will be done. OFFSET_MAP holds the mapping from
14730 section offsets to fields for the type. FI describes the fields of
14731 the type we're processing. BUILDER is the variant part to be
14732 converted. */
14733
14734 static void
14735 create_one_variant_part (variant_part &result,
14736 struct obstack *obstack,
14737 const offset_map_type &offset_map,
14738 struct field_info *fi,
14739 const variant_part_builder &builder)
14740 {
14741 auto iter = offset_map.find (builder.discriminant_offset);
14742 if (iter == offset_map.end ())
14743 {
14744 result.discriminant_index = -1;
14745 /* Doesn't matter. */
14746 result.is_unsigned = false;
14747 }
14748 else
14749 {
14750 result.discriminant_index = iter->second;
14751 result.is_unsigned
14752 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
14753 }
14754
14755 size_t n = builder.variants.size ();
14756 variant *output = new (obstack) variant[n];
14757 for (size_t i = 0; i < n; ++i)
14758 create_one_variant (output[i], obstack, offset_map, fi,
14759 builder.variants[i]);
14760
14761 result.variants = gdb::array_view<variant> (output, n);
14762 }
14763
14764 /* Create a vector of variant parts that can be attached to a type.
14765 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14766 holds the mapping from section offsets to fields for the type. FI
14767 describes the fields of the type we're processing. VARIANT_PARTS
14768 is the vector to convert. */
14769
14770 static const gdb::array_view<variant_part>
14771 create_variant_parts (struct obstack *obstack,
14772 const offset_map_type &offset_map,
14773 struct field_info *fi,
14774 const std::vector<variant_part_builder> &variant_parts)
14775 {
14776 if (variant_parts.empty ())
14777 return {};
14778
14779 size_t n = variant_parts.size ();
14780 variant_part *result = new (obstack) variant_part[n];
14781 for (size_t i = 0; i < n; ++i)
14782 create_one_variant_part (result[i], obstack, offset_map, fi,
14783 variant_parts[i]);
14784
14785 return gdb::array_view<variant_part> (result, n);
14786 }
14787
14788 /* Compute the variant part vector for FIP, attaching it to TYPE when
14789 done. */
14790
14791 static void
14792 add_variant_property (struct field_info *fip, struct type *type,
14793 struct dwarf2_cu *cu)
14794 {
14795 /* Map section offsets of fields to their field index. Note the
14796 field index here does not take the number of baseclasses into
14797 account. */
14798 offset_map_type offset_map;
14799 for (int i = 0; i < fip->fields.size (); ++i)
14800 offset_map[fip->fields[i].offset] = i;
14801
14802 struct objfile *objfile = cu->per_objfile->objfile;
14803 gdb::array_view<variant_part> parts
14804 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14805 fip->variant_parts);
14806
14807 struct dynamic_prop prop;
14808 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14809 obstack_copy (&objfile->objfile_obstack, &parts,
14810 sizeof (parts)));
14811
14812 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14813 }
14814
14815 /* Create the vector of fields, and attach it to the type. */
14816
14817 static void
14818 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14819 struct dwarf2_cu *cu)
14820 {
14821 int nfields = fip->nfields ();
14822
14823 /* Record the field count, allocate space for the array of fields,
14824 and create blank accessibility bitfields if necessary. */
14825 type->set_num_fields (nfields);
14826 type->set_fields
14827 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14828
14829 if (fip->non_public_fields && cu->language != language_ada)
14830 {
14831 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14832
14833 TYPE_FIELD_PRIVATE_BITS (type) =
14834 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14835 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14836
14837 TYPE_FIELD_PROTECTED_BITS (type) =
14838 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14839 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14840
14841 TYPE_FIELD_IGNORE_BITS (type) =
14842 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14843 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14844 }
14845
14846 /* If the type has baseclasses, allocate and clear a bit vector for
14847 TYPE_FIELD_VIRTUAL_BITS. */
14848 if (!fip->baseclasses.empty () && cu->language != language_ada)
14849 {
14850 int num_bytes = B_BYTES (fip->baseclasses.size ());
14851 unsigned char *pointer;
14852
14853 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14854 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14855 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14856 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14857 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14858 }
14859
14860 if (!fip->variant_parts.empty ())
14861 add_variant_property (fip, type, cu);
14862
14863 /* Copy the saved-up fields into the field vector. */
14864 for (int i = 0; i < nfields; ++i)
14865 {
14866 struct nextfield &field
14867 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14868 : fip->fields[i - fip->baseclasses.size ()]);
14869
14870 type->field (i) = field.field;
14871 switch (field.accessibility)
14872 {
14873 case DW_ACCESS_private:
14874 if (cu->language != language_ada)
14875 SET_TYPE_FIELD_PRIVATE (type, i);
14876 break;
14877
14878 case DW_ACCESS_protected:
14879 if (cu->language != language_ada)
14880 SET_TYPE_FIELD_PROTECTED (type, i);
14881 break;
14882
14883 case DW_ACCESS_public:
14884 break;
14885
14886 default:
14887 /* Unknown accessibility. Complain and treat it as public. */
14888 {
14889 complaint (_("unsupported accessibility %d"),
14890 field.accessibility);
14891 }
14892 break;
14893 }
14894 if (i < fip->baseclasses.size ())
14895 {
14896 switch (field.virtuality)
14897 {
14898 case DW_VIRTUALITY_virtual:
14899 case DW_VIRTUALITY_pure_virtual:
14900 if (cu->language == language_ada)
14901 error (_("unexpected virtuality in component of Ada type"));
14902 SET_TYPE_FIELD_VIRTUAL (type, i);
14903 break;
14904 }
14905 }
14906 }
14907 }
14908
14909 /* Return true if this member function is a constructor, false
14910 otherwise. */
14911
14912 static int
14913 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14914 {
14915 const char *fieldname;
14916 const char *type_name;
14917 int len;
14918
14919 if (die->parent == NULL)
14920 return 0;
14921
14922 if (die->parent->tag != DW_TAG_structure_type
14923 && die->parent->tag != DW_TAG_union_type
14924 && die->parent->tag != DW_TAG_class_type)
14925 return 0;
14926
14927 fieldname = dwarf2_name (die, cu);
14928 type_name = dwarf2_name (die->parent, cu);
14929 if (fieldname == NULL || type_name == NULL)
14930 return 0;
14931
14932 len = strlen (fieldname);
14933 return (strncmp (fieldname, type_name, len) == 0
14934 && (type_name[len] == '\0' || type_name[len] == '<'));
14935 }
14936
14937 /* Add a member function to the proper fieldlist. */
14938
14939 static void
14940 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14941 struct type *type, struct dwarf2_cu *cu)
14942 {
14943 struct objfile *objfile = cu->per_objfile->objfile;
14944 struct attribute *attr;
14945 int i;
14946 struct fnfieldlist *flp = nullptr;
14947 struct fn_field *fnp;
14948 const char *fieldname;
14949 struct type *this_type;
14950
14951 if (cu->language == language_ada)
14952 error (_("unexpected member function in Ada type"));
14953
14954 /* Get name of member function. */
14955 fieldname = dwarf2_name (die, cu);
14956 if (fieldname == NULL)
14957 return;
14958
14959 /* Look up member function name in fieldlist. */
14960 for (i = 0; i < fip->fnfieldlists.size (); i++)
14961 {
14962 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14963 {
14964 flp = &fip->fnfieldlists[i];
14965 break;
14966 }
14967 }
14968
14969 /* Create a new fnfieldlist if necessary. */
14970 if (flp == nullptr)
14971 {
14972 fip->fnfieldlists.emplace_back ();
14973 flp = &fip->fnfieldlists.back ();
14974 flp->name = fieldname;
14975 i = fip->fnfieldlists.size () - 1;
14976 }
14977
14978 /* Create a new member function field and add it to the vector of
14979 fnfieldlists. */
14980 flp->fnfields.emplace_back ();
14981 fnp = &flp->fnfields.back ();
14982
14983 /* Delay processing of the physname until later. */
14984 if (cu->language == language_cplus)
14985 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14986 die, cu);
14987 else
14988 {
14989 const char *physname = dwarf2_physname (fieldname, die, cu);
14990 fnp->physname = physname ? physname : "";
14991 }
14992
14993 fnp->type = alloc_type (objfile);
14994 this_type = read_type_die (die, cu);
14995 if (this_type && this_type->code () == TYPE_CODE_FUNC)
14996 {
14997 int nparams = this_type->num_fields ();
14998
14999 /* TYPE is the domain of this method, and THIS_TYPE is the type
15000 of the method itself (TYPE_CODE_METHOD). */
15001 smash_to_method_type (fnp->type, type,
15002 TYPE_TARGET_TYPE (this_type),
15003 this_type->fields (),
15004 this_type->num_fields (),
15005 this_type->has_varargs ());
15006
15007 /* Handle static member functions.
15008 Dwarf2 has no clean way to discern C++ static and non-static
15009 member functions. G++ helps GDB by marking the first
15010 parameter for non-static member functions (which is the this
15011 pointer) as artificial. We obtain this information from
15012 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15013 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15014 fnp->voffset = VOFFSET_STATIC;
15015 }
15016 else
15017 complaint (_("member function type missing for '%s'"),
15018 dwarf2_full_name (fieldname, die, cu));
15019
15020 /* Get fcontext from DW_AT_containing_type if present. */
15021 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15022 fnp->fcontext = die_containing_type (die, cu);
15023
15024 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15025 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15026
15027 /* Get accessibility. */
15028 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15029 switch (accessibility)
15030 {
15031 case DW_ACCESS_private:
15032 fnp->is_private = 1;
15033 break;
15034 case DW_ACCESS_protected:
15035 fnp->is_protected = 1;
15036 break;
15037 }
15038
15039 /* Check for artificial methods. */
15040 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15041 if (attr && attr->as_boolean ())
15042 fnp->is_artificial = 1;
15043
15044 /* Check for defaulted methods. */
15045 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15046 if (attr != nullptr)
15047 fnp->defaulted = attr->defaulted ();
15048
15049 /* Check for deleted methods. */
15050 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15051 if (attr != nullptr && attr->as_boolean ())
15052 fnp->is_deleted = 1;
15053
15054 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15055
15056 /* Get index in virtual function table if it is a virtual member
15057 function. For older versions of GCC, this is an offset in the
15058 appropriate virtual table, as specified by DW_AT_containing_type.
15059 For everyone else, it is an expression to be evaluated relative
15060 to the object address. */
15061
15062 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15063 if (attr != nullptr)
15064 {
15065 if (attr->form_is_block () && attr->as_block ()->size > 0)
15066 {
15067 struct dwarf_block *block = attr->as_block ();
15068
15069 if (block->data[0] == DW_OP_constu)
15070 {
15071 /* Old-style GCC. */
15072 fnp->voffset = decode_locdesc (block, cu) + 2;
15073 }
15074 else if (block->data[0] == DW_OP_deref
15075 || (block->size > 1
15076 && block->data[0] == DW_OP_deref_size
15077 && block->data[1] == cu->header.addr_size))
15078 {
15079 fnp->voffset = decode_locdesc (block, cu);
15080 if ((fnp->voffset % cu->header.addr_size) != 0)
15081 dwarf2_complex_location_expr_complaint ();
15082 else
15083 fnp->voffset /= cu->header.addr_size;
15084 fnp->voffset += 2;
15085 }
15086 else
15087 dwarf2_complex_location_expr_complaint ();
15088
15089 if (!fnp->fcontext)
15090 {
15091 /* If there is no `this' field and no DW_AT_containing_type,
15092 we cannot actually find a base class context for the
15093 vtable! */
15094 if (this_type->num_fields () == 0
15095 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15096 {
15097 complaint (_("cannot determine context for virtual member "
15098 "function \"%s\" (offset %s)"),
15099 fieldname, sect_offset_str (die->sect_off));
15100 }
15101 else
15102 {
15103 fnp->fcontext
15104 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15105 }
15106 }
15107 }
15108 else if (attr->form_is_section_offset ())
15109 {
15110 dwarf2_complex_location_expr_complaint ();
15111 }
15112 else
15113 {
15114 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15115 fieldname);
15116 }
15117 }
15118 else
15119 {
15120 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15121 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15122 {
15123 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15124 complaint (_("Member function \"%s\" (offset %s) is virtual "
15125 "but the vtable offset is not specified"),
15126 fieldname, sect_offset_str (die->sect_off));
15127 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15128 TYPE_CPLUS_DYNAMIC (type) = 1;
15129 }
15130 }
15131 }
15132
15133 /* Create the vector of member function fields, and attach it to the type. */
15134
15135 static void
15136 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15137 struct dwarf2_cu *cu)
15138 {
15139 if (cu->language == language_ada)
15140 error (_("unexpected member functions in Ada type"));
15141
15142 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15143 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15144 TYPE_ALLOC (type,
15145 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15146
15147 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15148 {
15149 struct fnfieldlist &nf = fip->fnfieldlists[i];
15150 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15151
15152 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15153 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15154 fn_flp->fn_fields = (struct fn_field *)
15155 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15156
15157 for (int k = 0; k < nf.fnfields.size (); ++k)
15158 fn_flp->fn_fields[k] = nf.fnfields[k];
15159 }
15160
15161 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15162 }
15163
15164 /* Returns non-zero if NAME is the name of a vtable member in CU's
15165 language, zero otherwise. */
15166 static int
15167 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15168 {
15169 static const char vptr[] = "_vptr";
15170
15171 /* Look for the C++ form of the vtable. */
15172 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15173 return 1;
15174
15175 return 0;
15176 }
15177
15178 /* GCC outputs unnamed structures that are really pointers to member
15179 functions, with the ABI-specified layout. If TYPE describes
15180 such a structure, smash it into a member function type.
15181
15182 GCC shouldn't do this; it should just output pointer to member DIEs.
15183 This is GCC PR debug/28767. */
15184
15185 static void
15186 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15187 {
15188 struct type *pfn_type, *self_type, *new_type;
15189
15190 /* Check for a structure with no name and two children. */
15191 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15192 return;
15193
15194 /* Check for __pfn and __delta members. */
15195 if (TYPE_FIELD_NAME (type, 0) == NULL
15196 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15197 || TYPE_FIELD_NAME (type, 1) == NULL
15198 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15199 return;
15200
15201 /* Find the type of the method. */
15202 pfn_type = type->field (0).type ();
15203 if (pfn_type == NULL
15204 || pfn_type->code () != TYPE_CODE_PTR
15205 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15206 return;
15207
15208 /* Look for the "this" argument. */
15209 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15210 if (pfn_type->num_fields () == 0
15211 /* || pfn_type->field (0).type () == NULL */
15212 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15213 return;
15214
15215 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15216 new_type = alloc_type (objfile);
15217 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15218 pfn_type->fields (), pfn_type->num_fields (),
15219 pfn_type->has_varargs ());
15220 smash_to_methodptr_type (type, new_type);
15221 }
15222
15223 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15224 requires rewriting, then copy it and return the updated copy.
15225 Otherwise return nullptr. */
15226
15227 static struct type *
15228 rewrite_array_type (struct type *type)
15229 {
15230 if (type->code () != TYPE_CODE_ARRAY)
15231 return nullptr;
15232
15233 struct type *index_type = type->index_type ();
15234 range_bounds *current_bounds = index_type->bounds ();
15235
15236 /* Handle multi-dimensional arrays. */
15237 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15238 if (new_target == nullptr)
15239 {
15240 /* Maybe we don't need to rewrite this array. */
15241 if (current_bounds->low.kind () == PROP_CONST
15242 && current_bounds->high.kind () == PROP_CONST)
15243 return nullptr;
15244 }
15245
15246 /* Either the target type was rewritten, or the bounds have to be
15247 updated. Either way we want to copy the type and update
15248 everything. */
15249 struct type *copy = copy_type (type);
15250 int nfields = copy->num_fields ();
15251 field *new_fields
15252 = ((struct field *) TYPE_ZALLOC (copy,
15253 nfields * sizeof (struct field)));
15254 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15255 copy->set_fields (new_fields);
15256 if (new_target != nullptr)
15257 TYPE_TARGET_TYPE (copy) = new_target;
15258
15259 struct type *index_copy = copy_type (index_type);
15260 range_bounds *bounds
15261 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15262 sizeof (range_bounds));
15263 *bounds = *current_bounds;
15264 bounds->low.set_const_val (1);
15265 bounds->high.set_const_val (0);
15266 index_copy->set_bounds (bounds);
15267 copy->set_index_type (index_copy);
15268
15269 return copy;
15270 }
15271
15272 /* While some versions of GCC will generate complicated DWARF for an
15273 array (see quirk_ada_thick_pointer), more recent versions were
15274 modified to emit an explicit thick pointer structure. However, in
15275 this case, the array still has DWARF expressions for its ranges,
15276 and these must be ignored. */
15277
15278 static void
15279 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15280 struct type *type)
15281 {
15282 gdb_assert (cu->language == language_ada);
15283
15284 /* Check for a structure with two children. */
15285 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15286 return;
15287
15288 /* Check for P_ARRAY and P_BOUNDS members. */
15289 if (TYPE_FIELD_NAME (type, 0) == NULL
15290 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15291 || TYPE_FIELD_NAME (type, 1) == NULL
15292 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15293 return;
15294
15295 /* Make sure we're looking at a pointer to an array. */
15296 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15297 return;
15298
15299 /* The Ada code already knows how to handle these types, so all that
15300 we need to do is turn the bounds into static bounds. However, we
15301 don't want to rewrite existing array or index types in-place,
15302 because those may be referenced in other contexts where this
15303 rewriting is undesirable. */
15304 struct type *new_ary_type
15305 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15306 if (new_ary_type != nullptr)
15307 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15308 }
15309
15310 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15311 appropriate error checking and issuing complaints if there is a
15312 problem. */
15313
15314 static ULONGEST
15315 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15316 {
15317 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15318
15319 if (attr == nullptr)
15320 return 0;
15321
15322 if (!attr->form_is_constant ())
15323 {
15324 complaint (_("DW_AT_alignment must have constant form"
15325 " - DIE at %s [in module %s]"),
15326 sect_offset_str (die->sect_off),
15327 objfile_name (cu->per_objfile->objfile));
15328 return 0;
15329 }
15330
15331 LONGEST val = attr->constant_value (0);
15332 if (val < 0)
15333 {
15334 complaint (_("DW_AT_alignment value must not be negative"
15335 " - DIE at %s [in module %s]"),
15336 sect_offset_str (die->sect_off),
15337 objfile_name (cu->per_objfile->objfile));
15338 return 0;
15339 }
15340 ULONGEST align = val;
15341
15342 if (align == 0)
15343 {
15344 complaint (_("DW_AT_alignment value must not be zero"
15345 " - DIE at %s [in module %s]"),
15346 sect_offset_str (die->sect_off),
15347 objfile_name (cu->per_objfile->objfile));
15348 return 0;
15349 }
15350 if ((align & (align - 1)) != 0)
15351 {
15352 complaint (_("DW_AT_alignment value must be a power of 2"
15353 " - DIE at %s [in module %s]"),
15354 sect_offset_str (die->sect_off),
15355 objfile_name (cu->per_objfile->objfile));
15356 return 0;
15357 }
15358
15359 return align;
15360 }
15361
15362 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15363 the alignment for TYPE. */
15364
15365 static void
15366 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15367 struct type *type)
15368 {
15369 if (!set_type_align (type, get_alignment (cu, die)))
15370 complaint (_("DW_AT_alignment value too large"
15371 " - DIE at %s [in module %s]"),
15372 sect_offset_str (die->sect_off),
15373 objfile_name (cu->per_objfile->objfile));
15374 }
15375
15376 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15377 constant for a type, according to DWARF5 spec, Table 5.5. */
15378
15379 static bool
15380 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15381 {
15382 switch (value)
15383 {
15384 case DW_CC_normal:
15385 case DW_CC_pass_by_reference:
15386 case DW_CC_pass_by_value:
15387 return true;
15388
15389 default:
15390 complaint (_("unrecognized DW_AT_calling_convention value "
15391 "(%s) for a type"), pulongest (value));
15392 return false;
15393 }
15394 }
15395
15396 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15397 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15398 also according to GNU-specific values (see include/dwarf2.h). */
15399
15400 static bool
15401 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15402 {
15403 switch (value)
15404 {
15405 case DW_CC_normal:
15406 case DW_CC_program:
15407 case DW_CC_nocall:
15408 return true;
15409
15410 case DW_CC_GNU_renesas_sh:
15411 case DW_CC_GNU_borland_fastcall_i386:
15412 case DW_CC_GDB_IBM_OpenCL:
15413 return true;
15414
15415 default:
15416 complaint (_("unrecognized DW_AT_calling_convention value "
15417 "(%s) for a subroutine"), pulongest (value));
15418 return false;
15419 }
15420 }
15421
15422 /* Called when we find the DIE that starts a structure or union scope
15423 (definition) to create a type for the structure or union. Fill in
15424 the type's name and general properties; the members will not be
15425 processed until process_structure_scope. A symbol table entry for
15426 the type will also not be done until process_structure_scope (assuming
15427 the type has a name).
15428
15429 NOTE: we need to call these functions regardless of whether or not the
15430 DIE has a DW_AT_name attribute, since it might be an anonymous
15431 structure or union. This gets the type entered into our set of
15432 user defined types. */
15433
15434 static struct type *
15435 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15436 {
15437 struct objfile *objfile = cu->per_objfile->objfile;
15438 struct type *type;
15439 struct attribute *attr;
15440 const char *name;
15441
15442 /* If the definition of this type lives in .debug_types, read that type.
15443 Don't follow DW_AT_specification though, that will take us back up
15444 the chain and we want to go down. */
15445 attr = die->attr (DW_AT_signature);
15446 if (attr != nullptr)
15447 {
15448 type = get_DW_AT_signature_type (die, attr, cu);
15449
15450 /* The type's CU may not be the same as CU.
15451 Ensure TYPE is recorded with CU in die_type_hash. */
15452 return set_die_type (die, type, cu);
15453 }
15454
15455 type = alloc_type (objfile);
15456 INIT_CPLUS_SPECIFIC (type);
15457
15458 name = dwarf2_name (die, cu);
15459 if (name != NULL)
15460 {
15461 if (cu->language == language_cplus
15462 || cu->language == language_d
15463 || cu->language == language_rust)
15464 {
15465 const char *full_name = dwarf2_full_name (name, die, cu);
15466
15467 /* dwarf2_full_name might have already finished building the DIE's
15468 type. If so, there is no need to continue. */
15469 if (get_die_type (die, cu) != NULL)
15470 return get_die_type (die, cu);
15471
15472 type->set_name (full_name);
15473 }
15474 else
15475 {
15476 /* The name is already allocated along with this objfile, so
15477 we don't need to duplicate it for the type. */
15478 type->set_name (name);
15479 }
15480 }
15481
15482 if (die->tag == DW_TAG_structure_type)
15483 {
15484 type->set_code (TYPE_CODE_STRUCT);
15485 }
15486 else if (die->tag == DW_TAG_union_type)
15487 {
15488 type->set_code (TYPE_CODE_UNION);
15489 }
15490 else
15491 {
15492 type->set_code (TYPE_CODE_STRUCT);
15493 }
15494
15495 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15496 type->set_is_declared_class (true);
15497
15498 /* Store the calling convention in the type if it's available in
15499 the die. Otherwise the calling convention remains set to
15500 the default value DW_CC_normal. */
15501 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15502 if (attr != nullptr
15503 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15504 {
15505 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15506 TYPE_CPLUS_CALLING_CONVENTION (type)
15507 = (enum dwarf_calling_convention) (attr->constant_value (0));
15508 }
15509
15510 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15511 if (attr != nullptr)
15512 {
15513 if (attr->form_is_constant ())
15514 TYPE_LENGTH (type) = attr->constant_value (0);
15515 else
15516 {
15517 struct dynamic_prop prop;
15518 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15519 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15520 TYPE_LENGTH (type) = 0;
15521 }
15522 }
15523 else
15524 {
15525 TYPE_LENGTH (type) = 0;
15526 }
15527
15528 maybe_set_alignment (cu, die, type);
15529
15530 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15531 {
15532 /* ICC<14 does not output the required DW_AT_declaration on
15533 incomplete types, but gives them a size of zero. */
15534 type->set_is_stub (true);
15535 }
15536 else
15537 type->set_stub_is_supported (true);
15538
15539 if (die_is_declaration (die, cu))
15540 type->set_is_stub (true);
15541 else if (attr == NULL && die->child == NULL
15542 && producer_is_realview (cu->producer))
15543 /* RealView does not output the required DW_AT_declaration
15544 on incomplete types. */
15545 type->set_is_stub (true);
15546
15547 /* We need to add the type field to the die immediately so we don't
15548 infinitely recurse when dealing with pointers to the structure
15549 type within the structure itself. */
15550 set_die_type (die, type, cu);
15551
15552 /* set_die_type should be already done. */
15553 set_descriptive_type (type, die, cu);
15554
15555 return type;
15556 }
15557
15558 static void handle_struct_member_die
15559 (struct die_info *child_die,
15560 struct type *type,
15561 struct field_info *fi,
15562 std::vector<struct symbol *> *template_args,
15563 struct dwarf2_cu *cu);
15564
15565 /* A helper for handle_struct_member_die that handles
15566 DW_TAG_variant_part. */
15567
15568 static void
15569 handle_variant_part (struct die_info *die, struct type *type,
15570 struct field_info *fi,
15571 std::vector<struct symbol *> *template_args,
15572 struct dwarf2_cu *cu)
15573 {
15574 variant_part_builder *new_part;
15575 if (fi->current_variant_part == nullptr)
15576 {
15577 fi->variant_parts.emplace_back ();
15578 new_part = &fi->variant_parts.back ();
15579 }
15580 else if (!fi->current_variant_part->processing_variant)
15581 {
15582 complaint (_("nested DW_TAG_variant_part seen "
15583 "- DIE at %s [in module %s]"),
15584 sect_offset_str (die->sect_off),
15585 objfile_name (cu->per_objfile->objfile));
15586 return;
15587 }
15588 else
15589 {
15590 variant_field &current = fi->current_variant_part->variants.back ();
15591 current.variant_parts.emplace_back ();
15592 new_part = &current.variant_parts.back ();
15593 }
15594
15595 /* When we recurse, we want callees to add to this new variant
15596 part. */
15597 scoped_restore save_current_variant_part
15598 = make_scoped_restore (&fi->current_variant_part, new_part);
15599
15600 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15601 if (discr == NULL)
15602 {
15603 /* It's a univariant form, an extension we support. */
15604 }
15605 else if (discr->form_is_ref ())
15606 {
15607 struct dwarf2_cu *target_cu = cu;
15608 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15609
15610 new_part->discriminant_offset = target_die->sect_off;
15611 }
15612 else
15613 {
15614 complaint (_("DW_AT_discr does not have DIE reference form"
15615 " - DIE at %s [in module %s]"),
15616 sect_offset_str (die->sect_off),
15617 objfile_name (cu->per_objfile->objfile));
15618 }
15619
15620 for (die_info *child_die = die->child;
15621 child_die != NULL;
15622 child_die = child_die->sibling)
15623 handle_struct_member_die (child_die, type, fi, template_args, cu);
15624 }
15625
15626 /* A helper for handle_struct_member_die that handles
15627 DW_TAG_variant. */
15628
15629 static void
15630 handle_variant (struct die_info *die, struct type *type,
15631 struct field_info *fi,
15632 std::vector<struct symbol *> *template_args,
15633 struct dwarf2_cu *cu)
15634 {
15635 if (fi->current_variant_part == nullptr)
15636 {
15637 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15638 "- DIE at %s [in module %s]"),
15639 sect_offset_str (die->sect_off),
15640 objfile_name (cu->per_objfile->objfile));
15641 return;
15642 }
15643 if (fi->current_variant_part->processing_variant)
15644 {
15645 complaint (_("nested DW_TAG_variant seen "
15646 "- DIE at %s [in module %s]"),
15647 sect_offset_str (die->sect_off),
15648 objfile_name (cu->per_objfile->objfile));
15649 return;
15650 }
15651
15652 scoped_restore save_processing_variant
15653 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15654 true);
15655
15656 fi->current_variant_part->variants.emplace_back ();
15657 variant_field &variant = fi->current_variant_part->variants.back ();
15658 variant.first_field = fi->fields.size ();
15659
15660 /* In a variant we want to get the discriminant and also add a
15661 field for our sole member child. */
15662 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15663 if (discr == nullptr || !discr->form_is_constant ())
15664 {
15665 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15666 if (discr == nullptr || discr->as_block ()->size == 0)
15667 variant.default_branch = true;
15668 else
15669 variant.discr_list_data = discr->as_block ();
15670 }
15671 else
15672 variant.discriminant_value = discr->constant_value (0);
15673
15674 for (die_info *variant_child = die->child;
15675 variant_child != NULL;
15676 variant_child = variant_child->sibling)
15677 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15678
15679 variant.last_field = fi->fields.size ();
15680 }
15681
15682 /* A helper for process_structure_scope that handles a single member
15683 DIE. */
15684
15685 static void
15686 handle_struct_member_die (struct die_info *child_die, struct type *type,
15687 struct field_info *fi,
15688 std::vector<struct symbol *> *template_args,
15689 struct dwarf2_cu *cu)
15690 {
15691 if (child_die->tag == DW_TAG_member
15692 || child_die->tag == DW_TAG_variable)
15693 {
15694 /* NOTE: carlton/2002-11-05: A C++ static data member
15695 should be a DW_TAG_member that is a declaration, but
15696 all versions of G++ as of this writing (so through at
15697 least 3.2.1) incorrectly generate DW_TAG_variable
15698 tags for them instead. */
15699 dwarf2_add_field (fi, child_die, cu);
15700 }
15701 else if (child_die->tag == DW_TAG_subprogram)
15702 {
15703 /* Rust doesn't have member functions in the C++ sense.
15704 However, it does emit ordinary functions as children
15705 of a struct DIE. */
15706 if (cu->language == language_rust)
15707 read_func_scope (child_die, cu);
15708 else
15709 {
15710 /* C++ member function. */
15711 dwarf2_add_member_fn (fi, child_die, type, cu);
15712 }
15713 }
15714 else if (child_die->tag == DW_TAG_inheritance)
15715 {
15716 /* C++ base class field. */
15717 dwarf2_add_field (fi, child_die, cu);
15718 }
15719 else if (type_can_define_types (child_die))
15720 dwarf2_add_type_defn (fi, child_die, cu);
15721 else if (child_die->tag == DW_TAG_template_type_param
15722 || child_die->tag == DW_TAG_template_value_param)
15723 {
15724 struct symbol *arg = new_symbol (child_die, NULL, cu);
15725
15726 if (arg != NULL)
15727 template_args->push_back (arg);
15728 }
15729 else if (child_die->tag == DW_TAG_variant_part)
15730 handle_variant_part (child_die, type, fi, template_args, cu);
15731 else if (child_die->tag == DW_TAG_variant)
15732 handle_variant (child_die, type, fi, template_args, cu);
15733 }
15734
15735 /* Finish creating a structure or union type, including filling in
15736 its members and creating a symbol for it. */
15737
15738 static void
15739 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15740 {
15741 struct objfile *objfile = cu->per_objfile->objfile;
15742 struct die_info *child_die;
15743 struct type *type;
15744
15745 type = get_die_type (die, cu);
15746 if (type == NULL)
15747 type = read_structure_type (die, cu);
15748
15749 bool has_template_parameters = false;
15750 if (die->child != NULL && ! die_is_declaration (die, cu))
15751 {
15752 struct field_info fi;
15753 std::vector<struct symbol *> template_args;
15754
15755 child_die = die->child;
15756
15757 while (child_die && child_die->tag)
15758 {
15759 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15760 child_die = child_die->sibling;
15761 }
15762
15763 /* Attach template arguments to type. */
15764 if (!template_args.empty ())
15765 {
15766 has_template_parameters = true;
15767 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15768 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15769 TYPE_TEMPLATE_ARGUMENTS (type)
15770 = XOBNEWVEC (&objfile->objfile_obstack,
15771 struct symbol *,
15772 TYPE_N_TEMPLATE_ARGUMENTS (type));
15773 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15774 template_args.data (),
15775 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15776 * sizeof (struct symbol *)));
15777 }
15778
15779 /* Attach fields and member functions to the type. */
15780 if (fi.nfields () > 0)
15781 dwarf2_attach_fields_to_type (&fi, type, cu);
15782 if (!fi.fnfieldlists.empty ())
15783 {
15784 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15785
15786 /* Get the type which refers to the base class (possibly this
15787 class itself) which contains the vtable pointer for the current
15788 class from the DW_AT_containing_type attribute. This use of
15789 DW_AT_containing_type is a GNU extension. */
15790
15791 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15792 {
15793 struct type *t = die_containing_type (die, cu);
15794
15795 set_type_vptr_basetype (type, t);
15796 if (type == t)
15797 {
15798 int i;
15799
15800 /* Our own class provides vtbl ptr. */
15801 for (i = t->num_fields () - 1;
15802 i >= TYPE_N_BASECLASSES (t);
15803 --i)
15804 {
15805 const char *fieldname = TYPE_FIELD_NAME (t, i);
15806
15807 if (is_vtable_name (fieldname, cu))
15808 {
15809 set_type_vptr_fieldno (type, i);
15810 break;
15811 }
15812 }
15813
15814 /* Complain if virtual function table field not found. */
15815 if (i < TYPE_N_BASECLASSES (t))
15816 complaint (_("virtual function table pointer "
15817 "not found when defining class '%s'"),
15818 type->name () ? type->name () : "");
15819 }
15820 else
15821 {
15822 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15823 }
15824 }
15825 else if (cu->producer
15826 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15827 {
15828 /* The IBM XLC compiler does not provide direct indication
15829 of the containing type, but the vtable pointer is
15830 always named __vfp. */
15831
15832 int i;
15833
15834 for (i = type->num_fields () - 1;
15835 i >= TYPE_N_BASECLASSES (type);
15836 --i)
15837 {
15838 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15839 {
15840 set_type_vptr_fieldno (type, i);
15841 set_type_vptr_basetype (type, type);
15842 break;
15843 }
15844 }
15845 }
15846 }
15847
15848 /* Copy fi.typedef_field_list linked list elements content into the
15849 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15850 if (!fi.typedef_field_list.empty ())
15851 {
15852 int count = fi.typedef_field_list.size ();
15853
15854 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15855 TYPE_TYPEDEF_FIELD_ARRAY (type)
15856 = ((struct decl_field *)
15857 TYPE_ALLOC (type,
15858 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15859 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15860
15861 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15862 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15863 }
15864
15865 /* Copy fi.nested_types_list linked list elements content into the
15866 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15867 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15868 {
15869 int count = fi.nested_types_list.size ();
15870
15871 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15872 TYPE_NESTED_TYPES_ARRAY (type)
15873 = ((struct decl_field *)
15874 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15875 TYPE_NESTED_TYPES_COUNT (type) = count;
15876
15877 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15878 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15879 }
15880 }
15881
15882 quirk_gcc_member_function_pointer (type, objfile);
15883 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15884 cu->rust_unions.push_back (type);
15885 else if (cu->language == language_ada)
15886 quirk_ada_thick_pointer_struct (die, cu, type);
15887
15888 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15889 snapshots) has been known to create a die giving a declaration
15890 for a class that has, as a child, a die giving a definition for a
15891 nested class. So we have to process our children even if the
15892 current die is a declaration. Normally, of course, a declaration
15893 won't have any children at all. */
15894
15895 child_die = die->child;
15896
15897 while (child_die != NULL && child_die->tag)
15898 {
15899 if (child_die->tag == DW_TAG_member
15900 || child_die->tag == DW_TAG_variable
15901 || child_die->tag == DW_TAG_inheritance
15902 || child_die->tag == DW_TAG_template_value_param
15903 || child_die->tag == DW_TAG_template_type_param)
15904 {
15905 /* Do nothing. */
15906 }
15907 else
15908 process_die (child_die, cu);
15909
15910 child_die = child_die->sibling;
15911 }
15912
15913 /* Do not consider external references. According to the DWARF standard,
15914 these DIEs are identified by the fact that they have no byte_size
15915 attribute, and a declaration attribute. */
15916 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15917 || !die_is_declaration (die, cu)
15918 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15919 {
15920 struct symbol *sym = new_symbol (die, type, cu);
15921
15922 if (has_template_parameters)
15923 {
15924 struct symtab *symtab;
15925 if (sym != nullptr)
15926 symtab = symbol_symtab (sym);
15927 else if (cu->line_header != nullptr)
15928 {
15929 /* Any related symtab will do. */
15930 symtab
15931 = cu->line_header->file_names ()[0].symtab;
15932 }
15933 else
15934 {
15935 symtab = nullptr;
15936 complaint (_("could not find suitable "
15937 "symtab for template parameter"
15938 " - DIE at %s [in module %s]"),
15939 sect_offset_str (die->sect_off),
15940 objfile_name (objfile));
15941 }
15942
15943 if (symtab != nullptr)
15944 {
15945 /* Make sure that the symtab is set on the new symbols.
15946 Even though they don't appear in this symtab directly,
15947 other parts of gdb assume that symbols do, and this is
15948 reasonably true. */
15949 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15950 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15951 }
15952 }
15953 }
15954 }
15955
15956 /* Assuming DIE is an enumeration type, and TYPE is its associated
15957 type, update TYPE using some information only available in DIE's
15958 children. In particular, the fields are computed. */
15959
15960 static void
15961 update_enumeration_type_from_children (struct die_info *die,
15962 struct type *type,
15963 struct dwarf2_cu *cu)
15964 {
15965 struct die_info *child_die;
15966 int unsigned_enum = 1;
15967 int flag_enum = 1;
15968
15969 auto_obstack obstack;
15970 std::vector<struct field> fields;
15971
15972 for (child_die = die->child;
15973 child_die != NULL && child_die->tag;
15974 child_die = child_die->sibling)
15975 {
15976 struct attribute *attr;
15977 LONGEST value;
15978 const gdb_byte *bytes;
15979 struct dwarf2_locexpr_baton *baton;
15980 const char *name;
15981
15982 if (child_die->tag != DW_TAG_enumerator)
15983 continue;
15984
15985 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15986 if (attr == NULL)
15987 continue;
15988
15989 name = dwarf2_name (child_die, cu);
15990 if (name == NULL)
15991 name = "<anonymous enumerator>";
15992
15993 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15994 &value, &bytes, &baton);
15995 if (value < 0)
15996 {
15997 unsigned_enum = 0;
15998 flag_enum = 0;
15999 }
16000 else
16001 {
16002 if (count_one_bits_ll (value) >= 2)
16003 flag_enum = 0;
16004 }
16005
16006 fields.emplace_back ();
16007 struct field &field = fields.back ();
16008 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16009 SET_FIELD_ENUMVAL (field, value);
16010 }
16011
16012 if (!fields.empty ())
16013 {
16014 type->set_num_fields (fields.size ());
16015 type->set_fields
16016 ((struct field *)
16017 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16018 memcpy (type->fields (), fields.data (),
16019 sizeof (struct field) * fields.size ());
16020 }
16021
16022 if (unsigned_enum)
16023 type->set_is_unsigned (true);
16024
16025 if (flag_enum)
16026 type->set_is_flag_enum (true);
16027 }
16028
16029 /* Given a DW_AT_enumeration_type die, set its type. We do not
16030 complete the type's fields yet, or create any symbols. */
16031
16032 static struct type *
16033 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16034 {
16035 struct objfile *objfile = cu->per_objfile->objfile;
16036 struct type *type;
16037 struct attribute *attr;
16038 const char *name;
16039
16040 /* If the definition of this type lives in .debug_types, read that type.
16041 Don't follow DW_AT_specification though, that will take us back up
16042 the chain and we want to go down. */
16043 attr = die->attr (DW_AT_signature);
16044 if (attr != nullptr)
16045 {
16046 type = get_DW_AT_signature_type (die, attr, cu);
16047
16048 /* The type's CU may not be the same as CU.
16049 Ensure TYPE is recorded with CU in die_type_hash. */
16050 return set_die_type (die, type, cu);
16051 }
16052
16053 type = alloc_type (objfile);
16054
16055 type->set_code (TYPE_CODE_ENUM);
16056 name = dwarf2_full_name (NULL, die, cu);
16057 if (name != NULL)
16058 type->set_name (name);
16059
16060 attr = dwarf2_attr (die, DW_AT_type, cu);
16061 if (attr != NULL)
16062 {
16063 struct type *underlying_type = die_type (die, cu);
16064
16065 TYPE_TARGET_TYPE (type) = underlying_type;
16066 }
16067
16068 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16069 if (attr != nullptr)
16070 {
16071 TYPE_LENGTH (type) = attr->constant_value (0);
16072 }
16073 else
16074 {
16075 TYPE_LENGTH (type) = 0;
16076 }
16077
16078 maybe_set_alignment (cu, die, type);
16079
16080 /* The enumeration DIE can be incomplete. In Ada, any type can be
16081 declared as private in the package spec, and then defined only
16082 inside the package body. Such types are known as Taft Amendment
16083 Types. When another package uses such a type, an incomplete DIE
16084 may be generated by the compiler. */
16085 if (die_is_declaration (die, cu))
16086 type->set_is_stub (true);
16087
16088 /* If this type has an underlying type that is not a stub, then we
16089 may use its attributes. We always use the "unsigned" attribute
16090 in this situation, because ordinarily we guess whether the type
16091 is unsigned -- but the guess can be wrong and the underlying type
16092 can tell us the reality. However, we defer to a local size
16093 attribute if one exists, because this lets the compiler override
16094 the underlying type if needed. */
16095 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16096 {
16097 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16098 underlying_type = check_typedef (underlying_type);
16099
16100 type->set_is_unsigned (underlying_type->is_unsigned ());
16101
16102 if (TYPE_LENGTH (type) == 0)
16103 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16104
16105 if (TYPE_RAW_ALIGN (type) == 0
16106 && TYPE_RAW_ALIGN (underlying_type) != 0)
16107 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16108 }
16109
16110 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16111
16112 set_die_type (die, type, cu);
16113
16114 /* Finish the creation of this type by using the enum's children.
16115 Note that, as usual, this must come after set_die_type to avoid
16116 infinite recursion when trying to compute the names of the
16117 enumerators. */
16118 update_enumeration_type_from_children (die, type, cu);
16119
16120 return type;
16121 }
16122
16123 /* Given a pointer to a die which begins an enumeration, process all
16124 the dies that define the members of the enumeration, and create the
16125 symbol for the enumeration type.
16126
16127 NOTE: We reverse the order of the element list. */
16128
16129 static void
16130 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16131 {
16132 struct type *this_type;
16133
16134 this_type = get_die_type (die, cu);
16135 if (this_type == NULL)
16136 this_type = read_enumeration_type (die, cu);
16137
16138 if (die->child != NULL)
16139 {
16140 struct die_info *child_die;
16141 const char *name;
16142
16143 child_die = die->child;
16144 while (child_die && child_die->tag)
16145 {
16146 if (child_die->tag != DW_TAG_enumerator)
16147 {
16148 process_die (child_die, cu);
16149 }
16150 else
16151 {
16152 name = dwarf2_name (child_die, cu);
16153 if (name)
16154 new_symbol (child_die, this_type, cu);
16155 }
16156
16157 child_die = child_die->sibling;
16158 }
16159 }
16160
16161 /* If we are reading an enum from a .debug_types unit, and the enum
16162 is a declaration, and the enum is not the signatured type in the
16163 unit, then we do not want to add a symbol for it. Adding a
16164 symbol would in some cases obscure the true definition of the
16165 enum, giving users an incomplete type when the definition is
16166 actually available. Note that we do not want to do this for all
16167 enums which are just declarations, because C++0x allows forward
16168 enum declarations. */
16169 if (cu->per_cu->is_debug_types
16170 && die_is_declaration (die, cu))
16171 {
16172 struct signatured_type *sig_type;
16173
16174 sig_type = (struct signatured_type *) cu->per_cu;
16175 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16176 if (sig_type->type_offset_in_section != die->sect_off)
16177 return;
16178 }
16179
16180 new_symbol (die, this_type, cu);
16181 }
16182
16183 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16184 expression for an index type and finds the corresponding field
16185 offset in the hidden "P_BOUNDS" structure. Returns true on success
16186 and updates *FIELD, false if it fails to recognize an
16187 expression. */
16188
16189 static bool
16190 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16191 int *bounds_offset, struct field *field,
16192 struct dwarf2_cu *cu)
16193 {
16194 struct attribute *attr = dwarf2_attr (die, name, cu);
16195 if (attr == nullptr || !attr->form_is_block ())
16196 return false;
16197
16198 const struct dwarf_block *block = attr->as_block ();
16199 const gdb_byte *start = block->data;
16200 const gdb_byte *end = block->data + block->size;
16201
16202 /* The expression to recognize generally looks like:
16203
16204 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16205 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16206
16207 However, the second "plus_uconst" may be missing:
16208
16209 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16210 DW_OP_deref_size: 4)
16211
16212 This happens when the field is at the start of the structure.
16213
16214 Also, the final deref may not be sized:
16215
16216 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16217 DW_OP_deref)
16218
16219 This happens when the size of the index type happens to be the
16220 same as the architecture's word size. This can occur with or
16221 without the second plus_uconst. */
16222
16223 if (end - start < 2)
16224 return false;
16225 if (*start++ != DW_OP_push_object_address)
16226 return false;
16227 if (*start++ != DW_OP_plus_uconst)
16228 return false;
16229
16230 uint64_t this_bound_off;
16231 start = gdb_read_uleb128 (start, end, &this_bound_off);
16232 if (start == nullptr || (int) this_bound_off != this_bound_off)
16233 return false;
16234 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16235 is consistent among all bounds. */
16236 if (*bounds_offset == -1)
16237 *bounds_offset = this_bound_off;
16238 else if (*bounds_offset != this_bound_off)
16239 return false;
16240
16241 if (start == end || *start++ != DW_OP_deref)
16242 return false;
16243
16244 int offset = 0;
16245 if (start ==end)
16246 return false;
16247 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16248 {
16249 /* This means an offset of 0. */
16250 }
16251 else if (*start++ != DW_OP_plus_uconst)
16252 return false;
16253 else
16254 {
16255 /* The size is the parameter to DW_OP_plus_uconst. */
16256 uint64_t val;
16257 start = gdb_read_uleb128 (start, end, &val);
16258 if (start == nullptr)
16259 return false;
16260 if ((int) val != val)
16261 return false;
16262 offset = val;
16263 }
16264
16265 if (start == end)
16266 return false;
16267
16268 uint64_t size;
16269 if (*start == DW_OP_deref_size)
16270 {
16271 start = gdb_read_uleb128 (start + 1, end, &size);
16272 if (start == nullptr)
16273 return false;
16274 }
16275 else if (*start == DW_OP_deref)
16276 {
16277 size = cu->header.addr_size;
16278 ++start;
16279 }
16280 else
16281 return false;
16282
16283 SET_FIELD_BITPOS (*field, 8 * offset);
16284 if (size != TYPE_LENGTH (field->type ()))
16285 FIELD_BITSIZE (*field) = 8 * size;
16286
16287 return true;
16288 }
16289
16290 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16291 some kinds of Ada arrays:
16292
16293 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16294 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16295 <11e0> DW_AT_data_location: 2 byte block: 97 6
16296 (DW_OP_push_object_address; DW_OP_deref)
16297 <11e3> DW_AT_type : <0x1173>
16298 <11e7> DW_AT_sibling : <0x1201>
16299 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16300 <11ec> DW_AT_type : <0x1206>
16301 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16302 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16303 DW_OP_deref_size: 4)
16304 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16305 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16306 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16307
16308 This actually represents a "thick pointer", which is a structure
16309 with two elements: one that is a pointer to the array data, and one
16310 that is a pointer to another structure; this second structure holds
16311 the array bounds.
16312
16313 This returns a new type on success, or nullptr if this didn't
16314 recognize the type. */
16315
16316 static struct type *
16317 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16318 struct type *type)
16319 {
16320 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16321 /* So far we've only seen this with block form. */
16322 if (attr == nullptr || !attr->form_is_block ())
16323 return nullptr;
16324
16325 /* Note that this will fail if the structure layout is changed by
16326 the compiler. However, we have no good way to recognize some
16327 other layout, because we don't know what expression the compiler
16328 might choose to emit should this happen. */
16329 struct dwarf_block *blk = attr->as_block ();
16330 if (blk->size != 2
16331 || blk->data[0] != DW_OP_push_object_address
16332 || blk->data[1] != DW_OP_deref)
16333 return nullptr;
16334
16335 int bounds_offset = -1;
16336 int max_align = -1;
16337 std::vector<struct field> range_fields;
16338 for (struct die_info *child_die = die->child;
16339 child_die;
16340 child_die = child_die->sibling)
16341 {
16342 if (child_die->tag == DW_TAG_subrange_type)
16343 {
16344 struct type *underlying = read_subrange_index_type (child_die, cu);
16345
16346 int this_align = type_align (underlying);
16347 if (this_align > max_align)
16348 max_align = this_align;
16349
16350 range_fields.emplace_back ();
16351 range_fields.emplace_back ();
16352
16353 struct field &lower = range_fields[range_fields.size () - 2];
16354 struct field &upper = range_fields[range_fields.size () - 1];
16355
16356 lower.set_type (underlying);
16357 FIELD_ARTIFICIAL (lower) = 1;
16358
16359 upper.set_type (underlying);
16360 FIELD_ARTIFICIAL (upper) = 1;
16361
16362 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16363 &bounds_offset, &lower, cu)
16364 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16365 &bounds_offset, &upper, cu))
16366 return nullptr;
16367 }
16368 }
16369
16370 /* This shouldn't really happen, but double-check that we found
16371 where the bounds are stored. */
16372 if (bounds_offset == -1)
16373 return nullptr;
16374
16375 struct objfile *objfile = cu->per_objfile->objfile;
16376 for (int i = 0; i < range_fields.size (); i += 2)
16377 {
16378 char name[20];
16379
16380 /* Set the name of each field in the bounds. */
16381 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16382 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16383 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16384 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16385 }
16386
16387 struct type *bounds = alloc_type (objfile);
16388 bounds->set_code (TYPE_CODE_STRUCT);
16389
16390 bounds->set_num_fields (range_fields.size ());
16391 bounds->set_fields
16392 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16393 * sizeof (struct field))));
16394 memcpy (bounds->fields (), range_fields.data (),
16395 bounds->num_fields () * sizeof (struct field));
16396
16397 int last_fieldno = range_fields.size () - 1;
16398 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16399 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16400 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16401
16402 /* Rewrite the existing array type in place. Specifically, we
16403 remove any dynamic properties we might have read, and we replace
16404 the index types. */
16405 struct type *iter = type;
16406 for (int i = 0; i < range_fields.size (); i += 2)
16407 {
16408 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16409 iter->main_type->dyn_prop_list = nullptr;
16410 iter->set_index_type
16411 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16412 iter = TYPE_TARGET_TYPE (iter);
16413 }
16414
16415 struct type *result = alloc_type (objfile);
16416 result->set_code (TYPE_CODE_STRUCT);
16417
16418 result->set_num_fields (2);
16419 result->set_fields
16420 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16421 * sizeof (struct field))));
16422
16423 /* The names are chosen to coincide with what the compiler does with
16424 -fgnat-encodings=all, which the Ada code in gdb already
16425 understands. */
16426 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16427 result->field (0).set_type (lookup_pointer_type (type));
16428
16429 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16430 result->field (1).set_type (lookup_pointer_type (bounds));
16431 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16432
16433 result->set_name (type->name ());
16434 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16435 + TYPE_LENGTH (result->field (1).type ()));
16436
16437 return result;
16438 }
16439
16440 /* Extract all information from a DW_TAG_array_type DIE and put it in
16441 the DIE's type field. For now, this only handles one dimensional
16442 arrays. */
16443
16444 static struct type *
16445 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16446 {
16447 struct objfile *objfile = cu->per_objfile->objfile;
16448 struct die_info *child_die;
16449 struct type *type;
16450 struct type *element_type, *range_type, *index_type;
16451 struct attribute *attr;
16452 const char *name;
16453 struct dynamic_prop *byte_stride_prop = NULL;
16454 unsigned int bit_stride = 0;
16455
16456 element_type = die_type (die, cu);
16457
16458 /* The die_type call above may have already set the type for this DIE. */
16459 type = get_die_type (die, cu);
16460 if (type)
16461 return type;
16462
16463 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16464 if (attr != NULL)
16465 {
16466 int stride_ok;
16467 struct type *prop_type = cu->addr_sized_int_type (false);
16468
16469 byte_stride_prop
16470 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16471 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16472 prop_type);
16473 if (!stride_ok)
16474 {
16475 complaint (_("unable to read array DW_AT_byte_stride "
16476 " - DIE at %s [in module %s]"),
16477 sect_offset_str (die->sect_off),
16478 objfile_name (cu->per_objfile->objfile));
16479 /* Ignore this attribute. We will likely not be able to print
16480 arrays of this type correctly, but there is little we can do
16481 to help if we cannot read the attribute's value. */
16482 byte_stride_prop = NULL;
16483 }
16484 }
16485
16486 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16487 if (attr != NULL)
16488 bit_stride = attr->constant_value (0);
16489
16490 /* Irix 6.2 native cc creates array types without children for
16491 arrays with unspecified length. */
16492 if (die->child == NULL)
16493 {
16494 index_type = objfile_type (objfile)->builtin_int;
16495 range_type = create_static_range_type (NULL, index_type, 0, -1);
16496 type = create_array_type_with_stride (NULL, element_type, range_type,
16497 byte_stride_prop, bit_stride);
16498 return set_die_type (die, type, cu);
16499 }
16500
16501 std::vector<struct type *> range_types;
16502 child_die = die->child;
16503 while (child_die && child_die->tag)
16504 {
16505 if (child_die->tag == DW_TAG_subrange_type)
16506 {
16507 struct type *child_type = read_type_die (child_die, cu);
16508
16509 if (child_type != NULL)
16510 {
16511 /* The range type was succesfully read. Save it for the
16512 array type creation. */
16513 range_types.push_back (child_type);
16514 }
16515 }
16516 child_die = child_die->sibling;
16517 }
16518
16519 if (range_types.empty ())
16520 {
16521 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16522 sect_offset_str (die->sect_off),
16523 objfile_name (cu->per_objfile->objfile));
16524 return NULL;
16525 }
16526
16527 /* Dwarf2 dimensions are output from left to right, create the
16528 necessary array types in backwards order. */
16529
16530 type = element_type;
16531
16532 if (read_array_order (die, cu) == DW_ORD_col_major)
16533 {
16534 int i = 0;
16535
16536 while (i < range_types.size ())
16537 {
16538 type = create_array_type_with_stride (NULL, type, range_types[i++],
16539 byte_stride_prop, bit_stride);
16540 bit_stride = 0;
16541 byte_stride_prop = nullptr;
16542 }
16543 }
16544 else
16545 {
16546 size_t ndim = range_types.size ();
16547 while (ndim-- > 0)
16548 {
16549 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16550 byte_stride_prop, bit_stride);
16551 bit_stride = 0;
16552 byte_stride_prop = nullptr;
16553 }
16554 }
16555
16556 gdb_assert (type != element_type);
16557
16558 /* Understand Dwarf2 support for vector types (like they occur on
16559 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16560 array type. This is not part of the Dwarf2/3 standard yet, but a
16561 custom vendor extension. The main difference between a regular
16562 array and the vector variant is that vectors are passed by value
16563 to functions. */
16564 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16565 if (attr != nullptr)
16566 make_vector_type (type);
16567
16568 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16569 implementation may choose to implement triple vectors using this
16570 attribute. */
16571 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16572 if (attr != nullptr && attr->form_is_unsigned ())
16573 {
16574 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16575 TYPE_LENGTH (type) = attr->as_unsigned ();
16576 else
16577 complaint (_("DW_AT_byte_size for array type smaller "
16578 "than the total size of elements"));
16579 }
16580
16581 name = dwarf2_name (die, cu);
16582 if (name)
16583 type->set_name (name);
16584
16585 maybe_set_alignment (cu, die, type);
16586
16587 struct type *replacement_type = nullptr;
16588 if (cu->language == language_ada)
16589 {
16590 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16591 if (replacement_type != nullptr)
16592 type = replacement_type;
16593 }
16594
16595 /* Install the type in the die. */
16596 set_die_type (die, type, cu, replacement_type != nullptr);
16597
16598 /* set_die_type should be already done. */
16599 set_descriptive_type (type, die, cu);
16600
16601 return type;
16602 }
16603
16604 static enum dwarf_array_dim_ordering
16605 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16606 {
16607 struct attribute *attr;
16608
16609 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16610
16611 if (attr != nullptr)
16612 {
16613 LONGEST val = attr->constant_value (-1);
16614 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16615 return (enum dwarf_array_dim_ordering) val;
16616 }
16617
16618 /* GNU F77 is a special case, as at 08/2004 array type info is the
16619 opposite order to the dwarf2 specification, but data is still
16620 laid out as per normal fortran.
16621
16622 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16623 version checking. */
16624
16625 if (cu->language == language_fortran
16626 && cu->producer && strstr (cu->producer, "GNU F77"))
16627 {
16628 return DW_ORD_row_major;
16629 }
16630
16631 switch (cu->language_defn->array_ordering ())
16632 {
16633 case array_column_major:
16634 return DW_ORD_col_major;
16635 case array_row_major:
16636 default:
16637 return DW_ORD_row_major;
16638 };
16639 }
16640
16641 /* Extract all information from a DW_TAG_set_type DIE and put it in
16642 the DIE's type field. */
16643
16644 static struct type *
16645 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16646 {
16647 struct type *domain_type, *set_type;
16648 struct attribute *attr;
16649
16650 domain_type = die_type (die, cu);
16651
16652 /* The die_type call above may have already set the type for this DIE. */
16653 set_type = get_die_type (die, cu);
16654 if (set_type)
16655 return set_type;
16656
16657 set_type = create_set_type (NULL, domain_type);
16658
16659 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16660 if (attr != nullptr && attr->form_is_unsigned ())
16661 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16662
16663 maybe_set_alignment (cu, die, set_type);
16664
16665 return set_die_type (die, set_type, cu);
16666 }
16667
16668 /* A helper for read_common_block that creates a locexpr baton.
16669 SYM is the symbol which we are marking as computed.
16670 COMMON_DIE is the DIE for the common block.
16671 COMMON_LOC is the location expression attribute for the common
16672 block itself.
16673 MEMBER_LOC is the location expression attribute for the particular
16674 member of the common block that we are processing.
16675 CU is the CU from which the above come. */
16676
16677 static void
16678 mark_common_block_symbol_computed (struct symbol *sym,
16679 struct die_info *common_die,
16680 struct attribute *common_loc,
16681 struct attribute *member_loc,
16682 struct dwarf2_cu *cu)
16683 {
16684 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16685 struct objfile *objfile = per_objfile->objfile;
16686 struct dwarf2_locexpr_baton *baton;
16687 gdb_byte *ptr;
16688 unsigned int cu_off;
16689 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16690 LONGEST offset = 0;
16691
16692 gdb_assert (common_loc && member_loc);
16693 gdb_assert (common_loc->form_is_block ());
16694 gdb_assert (member_loc->form_is_block ()
16695 || member_loc->form_is_constant ());
16696
16697 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16698 baton->per_objfile = per_objfile;
16699 baton->per_cu = cu->per_cu;
16700 gdb_assert (baton->per_cu);
16701
16702 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16703
16704 if (member_loc->form_is_constant ())
16705 {
16706 offset = member_loc->constant_value (0);
16707 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16708 }
16709 else
16710 baton->size += member_loc->as_block ()->size;
16711
16712 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16713 baton->data = ptr;
16714
16715 *ptr++ = DW_OP_call4;
16716 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16717 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16718 ptr += 4;
16719
16720 if (member_loc->form_is_constant ())
16721 {
16722 *ptr++ = DW_OP_addr;
16723 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16724 ptr += cu->header.addr_size;
16725 }
16726 else
16727 {
16728 /* We have to copy the data here, because DW_OP_call4 will only
16729 use a DW_AT_location attribute. */
16730 struct dwarf_block *block = member_loc->as_block ();
16731 memcpy (ptr, block->data, block->size);
16732 ptr += block->size;
16733 }
16734
16735 *ptr++ = DW_OP_plus;
16736 gdb_assert (ptr - baton->data == baton->size);
16737
16738 SYMBOL_LOCATION_BATON (sym) = baton;
16739 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16740 }
16741
16742 /* Create appropriate locally-scoped variables for all the
16743 DW_TAG_common_block entries. Also create a struct common_block
16744 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16745 is used to separate the common blocks name namespace from regular
16746 variable names. */
16747
16748 static void
16749 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16750 {
16751 struct attribute *attr;
16752
16753 attr = dwarf2_attr (die, DW_AT_location, cu);
16754 if (attr != nullptr)
16755 {
16756 /* Support the .debug_loc offsets. */
16757 if (attr->form_is_block ())
16758 {
16759 /* Ok. */
16760 }
16761 else if (attr->form_is_section_offset ())
16762 {
16763 dwarf2_complex_location_expr_complaint ();
16764 attr = NULL;
16765 }
16766 else
16767 {
16768 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16769 "common block member");
16770 attr = NULL;
16771 }
16772 }
16773
16774 if (die->child != NULL)
16775 {
16776 struct objfile *objfile = cu->per_objfile->objfile;
16777 struct die_info *child_die;
16778 size_t n_entries = 0, size;
16779 struct common_block *common_block;
16780 struct symbol *sym;
16781
16782 for (child_die = die->child;
16783 child_die && child_die->tag;
16784 child_die = child_die->sibling)
16785 ++n_entries;
16786
16787 size = (sizeof (struct common_block)
16788 + (n_entries - 1) * sizeof (struct symbol *));
16789 common_block
16790 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16791 size);
16792 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16793 common_block->n_entries = 0;
16794
16795 for (child_die = die->child;
16796 child_die && child_die->tag;
16797 child_die = child_die->sibling)
16798 {
16799 /* Create the symbol in the DW_TAG_common_block block in the current
16800 symbol scope. */
16801 sym = new_symbol (child_die, NULL, cu);
16802 if (sym != NULL)
16803 {
16804 struct attribute *member_loc;
16805
16806 common_block->contents[common_block->n_entries++] = sym;
16807
16808 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16809 cu);
16810 if (member_loc)
16811 {
16812 /* GDB has handled this for a long time, but it is
16813 not specified by DWARF. It seems to have been
16814 emitted by gfortran at least as recently as:
16815 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16816 complaint (_("Variable in common block has "
16817 "DW_AT_data_member_location "
16818 "- DIE at %s [in module %s]"),
16819 sect_offset_str (child_die->sect_off),
16820 objfile_name (objfile));
16821
16822 if (member_loc->form_is_section_offset ())
16823 dwarf2_complex_location_expr_complaint ();
16824 else if (member_loc->form_is_constant ()
16825 || member_loc->form_is_block ())
16826 {
16827 if (attr != nullptr)
16828 mark_common_block_symbol_computed (sym, die, attr,
16829 member_loc, cu);
16830 }
16831 else
16832 dwarf2_complex_location_expr_complaint ();
16833 }
16834 }
16835 }
16836
16837 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16838 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16839 }
16840 }
16841
16842 /* Create a type for a C++ namespace. */
16843
16844 static struct type *
16845 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16846 {
16847 struct objfile *objfile = cu->per_objfile->objfile;
16848 const char *previous_prefix, *name;
16849 int is_anonymous;
16850 struct type *type;
16851
16852 /* For extensions, reuse the type of the original namespace. */
16853 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16854 {
16855 struct die_info *ext_die;
16856 struct dwarf2_cu *ext_cu = cu;
16857
16858 ext_die = dwarf2_extension (die, &ext_cu);
16859 type = read_type_die (ext_die, ext_cu);
16860
16861 /* EXT_CU may not be the same as CU.
16862 Ensure TYPE is recorded with CU in die_type_hash. */
16863 return set_die_type (die, type, cu);
16864 }
16865
16866 name = namespace_name (die, &is_anonymous, cu);
16867
16868 /* Now build the name of the current namespace. */
16869
16870 previous_prefix = determine_prefix (die, cu);
16871 if (previous_prefix[0] != '\0')
16872 name = typename_concat (&objfile->objfile_obstack,
16873 previous_prefix, name, 0, cu);
16874
16875 /* Create the type. */
16876 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16877
16878 return set_die_type (die, type, cu);
16879 }
16880
16881 /* Read a namespace scope. */
16882
16883 static void
16884 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16885 {
16886 struct objfile *objfile = cu->per_objfile->objfile;
16887 int is_anonymous;
16888
16889 /* Add a symbol associated to this if we haven't seen the namespace
16890 before. Also, add a using directive if it's an anonymous
16891 namespace. */
16892
16893 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16894 {
16895 struct type *type;
16896
16897 type = read_type_die (die, cu);
16898 new_symbol (die, type, cu);
16899
16900 namespace_name (die, &is_anonymous, cu);
16901 if (is_anonymous)
16902 {
16903 const char *previous_prefix = determine_prefix (die, cu);
16904
16905 std::vector<const char *> excludes;
16906 add_using_directive (using_directives (cu),
16907 previous_prefix, type->name (), NULL,
16908 NULL, excludes, 0, &objfile->objfile_obstack);
16909 }
16910 }
16911
16912 if (die->child != NULL)
16913 {
16914 struct die_info *child_die = die->child;
16915
16916 while (child_die && child_die->tag)
16917 {
16918 process_die (child_die, cu);
16919 child_die = child_die->sibling;
16920 }
16921 }
16922 }
16923
16924 /* Read a Fortran module as type. This DIE can be only a declaration used for
16925 imported module. Still we need that type as local Fortran "use ... only"
16926 declaration imports depend on the created type in determine_prefix. */
16927
16928 static struct type *
16929 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16930 {
16931 struct objfile *objfile = cu->per_objfile->objfile;
16932 const char *module_name;
16933 struct type *type;
16934
16935 module_name = dwarf2_name (die, cu);
16936 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16937
16938 return set_die_type (die, type, cu);
16939 }
16940
16941 /* Read a Fortran module. */
16942
16943 static void
16944 read_module (struct die_info *die, struct dwarf2_cu *cu)
16945 {
16946 struct die_info *child_die = die->child;
16947 struct type *type;
16948
16949 type = read_type_die (die, cu);
16950 new_symbol (die, type, cu);
16951
16952 while (child_die && child_die->tag)
16953 {
16954 process_die (child_die, cu);
16955 child_die = child_die->sibling;
16956 }
16957 }
16958
16959 /* Return the name of the namespace represented by DIE. Set
16960 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16961 namespace. */
16962
16963 static const char *
16964 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16965 {
16966 struct die_info *current_die;
16967 const char *name = NULL;
16968
16969 /* Loop through the extensions until we find a name. */
16970
16971 for (current_die = die;
16972 current_die != NULL;
16973 current_die = dwarf2_extension (die, &cu))
16974 {
16975 /* We don't use dwarf2_name here so that we can detect the absence
16976 of a name -> anonymous namespace. */
16977 name = dwarf2_string_attr (die, DW_AT_name, cu);
16978
16979 if (name != NULL)
16980 break;
16981 }
16982
16983 /* Is it an anonymous namespace? */
16984
16985 *is_anonymous = (name == NULL);
16986 if (*is_anonymous)
16987 name = CP_ANONYMOUS_NAMESPACE_STR;
16988
16989 return name;
16990 }
16991
16992 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16993 the user defined type vector. */
16994
16995 static struct type *
16996 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16997 {
16998 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16999 struct comp_unit_head *cu_header = &cu->header;
17000 struct type *type;
17001 struct attribute *attr_byte_size;
17002 struct attribute *attr_address_class;
17003 int byte_size, addr_class;
17004 struct type *target_type;
17005
17006 target_type = die_type (die, cu);
17007
17008 /* The die_type call above may have already set the type for this DIE. */
17009 type = get_die_type (die, cu);
17010 if (type)
17011 return type;
17012
17013 type = lookup_pointer_type (target_type);
17014
17015 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17016 if (attr_byte_size)
17017 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17018 else
17019 byte_size = cu_header->addr_size;
17020
17021 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17022 if (attr_address_class)
17023 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17024 else
17025 addr_class = DW_ADDR_none;
17026
17027 ULONGEST alignment = get_alignment (cu, die);
17028
17029 /* If the pointer size, alignment, or address class is different
17030 than the default, create a type variant marked as such and set
17031 the length accordingly. */
17032 if (TYPE_LENGTH (type) != byte_size
17033 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17034 && alignment != TYPE_RAW_ALIGN (type))
17035 || addr_class != DW_ADDR_none)
17036 {
17037 if (gdbarch_address_class_type_flags_p (gdbarch))
17038 {
17039 type_instance_flags type_flags
17040 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17041 addr_class);
17042 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17043 == 0);
17044 type = make_type_with_address_space (type, type_flags);
17045 }
17046 else if (TYPE_LENGTH (type) != byte_size)
17047 {
17048 complaint (_("invalid pointer size %d"), byte_size);
17049 }
17050 else if (TYPE_RAW_ALIGN (type) != alignment)
17051 {
17052 complaint (_("Invalid DW_AT_alignment"
17053 " - DIE at %s [in module %s]"),
17054 sect_offset_str (die->sect_off),
17055 objfile_name (cu->per_objfile->objfile));
17056 }
17057 else
17058 {
17059 /* Should we also complain about unhandled address classes? */
17060 }
17061 }
17062
17063 TYPE_LENGTH (type) = byte_size;
17064 set_type_align (type, alignment);
17065 return set_die_type (die, type, cu);
17066 }
17067
17068 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17069 the user defined type vector. */
17070
17071 static struct type *
17072 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17073 {
17074 struct type *type;
17075 struct type *to_type;
17076 struct type *domain;
17077
17078 to_type = die_type (die, cu);
17079 domain = die_containing_type (die, cu);
17080
17081 /* The calls above may have already set the type for this DIE. */
17082 type = get_die_type (die, cu);
17083 if (type)
17084 return type;
17085
17086 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17087 type = lookup_methodptr_type (to_type);
17088 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17089 {
17090 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17091
17092 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17093 to_type->fields (), to_type->num_fields (),
17094 to_type->has_varargs ());
17095 type = lookup_methodptr_type (new_type);
17096 }
17097 else
17098 type = lookup_memberptr_type (to_type, domain);
17099
17100 return set_die_type (die, type, cu);
17101 }
17102
17103 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17104 the user defined type vector. */
17105
17106 static struct type *
17107 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17108 enum type_code refcode)
17109 {
17110 struct comp_unit_head *cu_header = &cu->header;
17111 struct type *type, *target_type;
17112 struct attribute *attr;
17113
17114 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17115
17116 target_type = die_type (die, cu);
17117
17118 /* The die_type call above may have already set the type for this DIE. */
17119 type = get_die_type (die, cu);
17120 if (type)
17121 return type;
17122
17123 type = lookup_reference_type (target_type, refcode);
17124 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17125 if (attr != nullptr)
17126 {
17127 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17128 }
17129 else
17130 {
17131 TYPE_LENGTH (type) = cu_header->addr_size;
17132 }
17133 maybe_set_alignment (cu, die, type);
17134 return set_die_type (die, type, cu);
17135 }
17136
17137 /* Add the given cv-qualifiers to the element type of the array. GCC
17138 outputs DWARF type qualifiers that apply to an array, not the
17139 element type. But GDB relies on the array element type to carry
17140 the cv-qualifiers. This mimics section 6.7.3 of the C99
17141 specification. */
17142
17143 static struct type *
17144 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17145 struct type *base_type, int cnst, int voltl)
17146 {
17147 struct type *el_type, *inner_array;
17148
17149 base_type = copy_type (base_type);
17150 inner_array = base_type;
17151
17152 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17153 {
17154 TYPE_TARGET_TYPE (inner_array) =
17155 copy_type (TYPE_TARGET_TYPE (inner_array));
17156 inner_array = TYPE_TARGET_TYPE (inner_array);
17157 }
17158
17159 el_type = TYPE_TARGET_TYPE (inner_array);
17160 cnst |= TYPE_CONST (el_type);
17161 voltl |= TYPE_VOLATILE (el_type);
17162 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17163
17164 return set_die_type (die, base_type, cu);
17165 }
17166
17167 static struct type *
17168 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17169 {
17170 struct type *base_type, *cv_type;
17171
17172 base_type = die_type (die, cu);
17173
17174 /* The die_type call above may have already set the type for this DIE. */
17175 cv_type = get_die_type (die, cu);
17176 if (cv_type)
17177 return cv_type;
17178
17179 /* In case the const qualifier is applied to an array type, the element type
17180 is so qualified, not the array type (section 6.7.3 of C99). */
17181 if (base_type->code () == TYPE_CODE_ARRAY)
17182 return add_array_cv_type (die, cu, base_type, 1, 0);
17183
17184 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17185 return set_die_type (die, cv_type, cu);
17186 }
17187
17188 static struct type *
17189 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17190 {
17191 struct type *base_type, *cv_type;
17192
17193 base_type = die_type (die, cu);
17194
17195 /* The die_type call above may have already set the type for this DIE. */
17196 cv_type = get_die_type (die, cu);
17197 if (cv_type)
17198 return cv_type;
17199
17200 /* In case the volatile qualifier is applied to an array type, the
17201 element type is so qualified, not the array type (section 6.7.3
17202 of C99). */
17203 if (base_type->code () == TYPE_CODE_ARRAY)
17204 return add_array_cv_type (die, cu, base_type, 0, 1);
17205
17206 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17207 return set_die_type (die, cv_type, cu);
17208 }
17209
17210 /* Handle DW_TAG_restrict_type. */
17211
17212 static struct type *
17213 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17214 {
17215 struct type *base_type, *cv_type;
17216
17217 base_type = die_type (die, cu);
17218
17219 /* The die_type call above may have already set the type for this DIE. */
17220 cv_type = get_die_type (die, cu);
17221 if (cv_type)
17222 return cv_type;
17223
17224 cv_type = make_restrict_type (base_type);
17225 return set_die_type (die, cv_type, cu);
17226 }
17227
17228 /* Handle DW_TAG_atomic_type. */
17229
17230 static struct type *
17231 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17232 {
17233 struct type *base_type, *cv_type;
17234
17235 base_type = die_type (die, cu);
17236
17237 /* The die_type call above may have already set the type for this DIE. */
17238 cv_type = get_die_type (die, cu);
17239 if (cv_type)
17240 return cv_type;
17241
17242 cv_type = make_atomic_type (base_type);
17243 return set_die_type (die, cv_type, cu);
17244 }
17245
17246 /* Extract all information from a DW_TAG_string_type DIE and add to
17247 the user defined type vector. It isn't really a user defined type,
17248 but it behaves like one, with other DIE's using an AT_user_def_type
17249 attribute to reference it. */
17250
17251 static struct type *
17252 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17253 {
17254 struct objfile *objfile = cu->per_objfile->objfile;
17255 struct gdbarch *gdbarch = objfile->arch ();
17256 struct type *type, *range_type, *index_type, *char_type;
17257 struct attribute *attr;
17258 struct dynamic_prop prop;
17259 bool length_is_constant = true;
17260 LONGEST length;
17261
17262 /* There are a couple of places where bit sizes might be made use of
17263 when parsing a DW_TAG_string_type, however, no producer that we know
17264 of make use of these. Handling bit sizes that are a multiple of the
17265 byte size is easy enough, but what about other bit sizes? Lets deal
17266 with that problem when we have to. Warn about these attributes being
17267 unsupported, then parse the type and ignore them like we always
17268 have. */
17269 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17270 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17271 {
17272 static bool warning_printed = false;
17273 if (!warning_printed)
17274 {
17275 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17276 "currently supported on DW_TAG_string_type."));
17277 warning_printed = true;
17278 }
17279 }
17280
17281 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17282 if (attr != nullptr && !attr->form_is_constant ())
17283 {
17284 /* The string length describes the location at which the length of
17285 the string can be found. The size of the length field can be
17286 specified with one of the attributes below. */
17287 struct type *prop_type;
17288 struct attribute *len
17289 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17290 if (len == nullptr)
17291 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17292 if (len != nullptr && len->form_is_constant ())
17293 {
17294 /* Pass 0 as the default as we know this attribute is constant
17295 and the default value will not be returned. */
17296 LONGEST sz = len->constant_value (0);
17297 prop_type = cu->per_objfile->int_type (sz, true);
17298 }
17299 else
17300 {
17301 /* If the size is not specified then we assume it is the size of
17302 an address on this target. */
17303 prop_type = cu->addr_sized_int_type (true);
17304 }
17305
17306 /* Convert the attribute into a dynamic property. */
17307 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17308 length = 1;
17309 else
17310 length_is_constant = false;
17311 }
17312 else if (attr != nullptr)
17313 {
17314 /* This DW_AT_string_length just contains the length with no
17315 indirection. There's no need to create a dynamic property in this
17316 case. Pass 0 for the default value as we know it will not be
17317 returned in this case. */
17318 length = attr->constant_value (0);
17319 }
17320 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17321 {
17322 /* We don't currently support non-constant byte sizes for strings. */
17323 length = attr->constant_value (1);
17324 }
17325 else
17326 {
17327 /* Use 1 as a fallback length if we have nothing else. */
17328 length = 1;
17329 }
17330
17331 index_type = objfile_type (objfile)->builtin_int;
17332 if (length_is_constant)
17333 range_type = create_static_range_type (NULL, index_type, 1, length);
17334 else
17335 {
17336 struct dynamic_prop low_bound;
17337
17338 low_bound.set_const_val (1);
17339 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17340 }
17341 char_type = language_string_char_type (cu->language_defn, gdbarch);
17342 type = create_string_type (NULL, char_type, range_type);
17343
17344 return set_die_type (die, type, cu);
17345 }
17346
17347 /* Assuming that DIE corresponds to a function, returns nonzero
17348 if the function is prototyped. */
17349
17350 static int
17351 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17352 {
17353 struct attribute *attr;
17354
17355 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17356 if (attr && attr->as_boolean ())
17357 return 1;
17358
17359 /* The DWARF standard implies that the DW_AT_prototyped attribute
17360 is only meaningful for C, but the concept also extends to other
17361 languages that allow unprototyped functions (Eg: Objective C).
17362 For all other languages, assume that functions are always
17363 prototyped. */
17364 if (cu->language != language_c
17365 && cu->language != language_objc
17366 && cu->language != language_opencl)
17367 return 1;
17368
17369 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17370 prototyped and unprototyped functions; default to prototyped,
17371 since that is more common in modern code (and RealView warns
17372 about unprototyped functions). */
17373 if (producer_is_realview (cu->producer))
17374 return 1;
17375
17376 return 0;
17377 }
17378
17379 /* Handle DIES due to C code like:
17380
17381 struct foo
17382 {
17383 int (*funcp)(int a, long l);
17384 int b;
17385 };
17386
17387 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17388
17389 static struct type *
17390 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17391 {
17392 struct objfile *objfile = cu->per_objfile->objfile;
17393 struct type *type; /* Type that this function returns. */
17394 struct type *ftype; /* Function that returns above type. */
17395 struct attribute *attr;
17396
17397 type = die_type (die, cu);
17398
17399 /* The die_type call above may have already set the type for this DIE. */
17400 ftype = get_die_type (die, cu);
17401 if (ftype)
17402 return ftype;
17403
17404 ftype = lookup_function_type (type);
17405
17406 if (prototyped_function_p (die, cu))
17407 ftype->set_is_prototyped (true);
17408
17409 /* Store the calling convention in the type if it's available in
17410 the subroutine die. Otherwise set the calling convention to
17411 the default value DW_CC_normal. */
17412 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17413 if (attr != nullptr
17414 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17415 TYPE_CALLING_CONVENTION (ftype)
17416 = (enum dwarf_calling_convention) attr->constant_value (0);
17417 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17418 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17419 else
17420 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17421
17422 /* Record whether the function returns normally to its caller or not
17423 if the DWARF producer set that information. */
17424 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17425 if (attr && attr->as_boolean ())
17426 TYPE_NO_RETURN (ftype) = 1;
17427
17428 /* We need to add the subroutine type to the die immediately so
17429 we don't infinitely recurse when dealing with parameters
17430 declared as the same subroutine type. */
17431 set_die_type (die, ftype, cu);
17432
17433 if (die->child != NULL)
17434 {
17435 struct type *void_type = objfile_type (objfile)->builtin_void;
17436 struct die_info *child_die;
17437 int nparams, iparams;
17438
17439 /* Count the number of parameters.
17440 FIXME: GDB currently ignores vararg functions, but knows about
17441 vararg member functions. */
17442 nparams = 0;
17443 child_die = die->child;
17444 while (child_die && child_die->tag)
17445 {
17446 if (child_die->tag == DW_TAG_formal_parameter)
17447 nparams++;
17448 else if (child_die->tag == DW_TAG_unspecified_parameters)
17449 ftype->set_has_varargs (true);
17450
17451 child_die = child_die->sibling;
17452 }
17453
17454 /* Allocate storage for parameters and fill them in. */
17455 ftype->set_num_fields (nparams);
17456 ftype->set_fields
17457 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17458
17459 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17460 even if we error out during the parameters reading below. */
17461 for (iparams = 0; iparams < nparams; iparams++)
17462 ftype->field (iparams).set_type (void_type);
17463
17464 iparams = 0;
17465 child_die = die->child;
17466 while (child_die && child_die->tag)
17467 {
17468 if (child_die->tag == DW_TAG_formal_parameter)
17469 {
17470 struct type *arg_type;
17471
17472 /* DWARF version 2 has no clean way to discern C++
17473 static and non-static member functions. G++ helps
17474 GDB by marking the first parameter for non-static
17475 member functions (which is the this pointer) as
17476 artificial. We pass this information to
17477 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17478
17479 DWARF version 3 added DW_AT_object_pointer, which GCC
17480 4.5 does not yet generate. */
17481 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17482 if (attr != nullptr)
17483 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17484 else
17485 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17486 arg_type = die_type (child_die, cu);
17487
17488 /* RealView does not mark THIS as const, which the testsuite
17489 expects. GCC marks THIS as const in method definitions,
17490 but not in the class specifications (GCC PR 43053). */
17491 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17492 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17493 {
17494 int is_this = 0;
17495 struct dwarf2_cu *arg_cu = cu;
17496 const char *name = dwarf2_name (child_die, cu);
17497
17498 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17499 if (attr != nullptr)
17500 {
17501 /* If the compiler emits this, use it. */
17502 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17503 is_this = 1;
17504 }
17505 else if (name && strcmp (name, "this") == 0)
17506 /* Function definitions will have the argument names. */
17507 is_this = 1;
17508 else if (name == NULL && iparams == 0)
17509 /* Declarations may not have the names, so like
17510 elsewhere in GDB, assume an artificial first
17511 argument is "this". */
17512 is_this = 1;
17513
17514 if (is_this)
17515 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17516 arg_type, 0);
17517 }
17518
17519 ftype->field (iparams).set_type (arg_type);
17520 iparams++;
17521 }
17522 child_die = child_die->sibling;
17523 }
17524 }
17525
17526 return ftype;
17527 }
17528
17529 static struct type *
17530 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17531 {
17532 struct objfile *objfile = cu->per_objfile->objfile;
17533 const char *name = NULL;
17534 struct type *this_type, *target_type;
17535
17536 name = dwarf2_full_name (NULL, die, cu);
17537 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17538 this_type->set_target_is_stub (true);
17539 set_die_type (die, this_type, cu);
17540 target_type = die_type (die, cu);
17541 if (target_type != this_type)
17542 TYPE_TARGET_TYPE (this_type) = target_type;
17543 else
17544 {
17545 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17546 spec and cause infinite loops in GDB. */
17547 complaint (_("Self-referential DW_TAG_typedef "
17548 "- DIE at %s [in module %s]"),
17549 sect_offset_str (die->sect_off), objfile_name (objfile));
17550 TYPE_TARGET_TYPE (this_type) = NULL;
17551 }
17552 if (name == NULL)
17553 {
17554 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17555 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17556 Handle these by just returning the target type, rather than
17557 constructing an anonymous typedef type and trying to handle this
17558 elsewhere. */
17559 set_die_type (die, target_type, cu);
17560 return target_type;
17561 }
17562 return this_type;
17563 }
17564
17565 /* Helper for get_dwarf2_rational_constant that computes the value of
17566 a given gmp_mpz given an attribute. */
17567
17568 static void
17569 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17570 {
17571 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17572 location expression that pushes an implicit value. */
17573 if (attr->form == DW_FORM_exprloc)
17574 {
17575 dwarf_block *blk = attr->as_block ();
17576 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17577 {
17578 uint64_t len;
17579 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17580 blk->data + blk->size,
17581 &len);
17582 if (ptr - blk->data + len <= blk->size)
17583 {
17584 mpz_import (value->val, len,
17585 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17586 1, 0, 0, ptr);
17587 return;
17588 }
17589 }
17590
17591 /* On failure set it to 1. */
17592 *value = gdb_mpz (1);
17593 }
17594 else if (attr->form_is_block ())
17595 {
17596 dwarf_block *blk = attr->as_block ();
17597 mpz_import (value->val, blk->size,
17598 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17599 1, 0, 0, blk->data);
17600 }
17601 else
17602 *value = gdb_mpz (attr->constant_value (1));
17603 }
17604
17605 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17606 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17607
17608 If the numerator and/or numerator attribute is missing,
17609 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17610 untouched. */
17611
17612 static void
17613 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17614 gdb_mpz *numerator, gdb_mpz *denominator)
17615 {
17616 struct attribute *num_attr, *denom_attr;
17617
17618 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17619 if (num_attr == nullptr)
17620 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17621 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17622
17623 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17624 if (denom_attr == nullptr)
17625 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17626 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17627
17628 if (num_attr == nullptr || denom_attr == nullptr)
17629 return;
17630
17631 get_mpz (cu, numerator, num_attr);
17632 get_mpz (cu, denominator, denom_attr);
17633 }
17634
17635 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17636 rational constant, rather than a signed one.
17637
17638 If the rational constant has a negative value, a complaint
17639 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17640
17641 static void
17642 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17643 struct dwarf2_cu *cu,
17644 gdb_mpz *numerator,
17645 gdb_mpz *denominator)
17646 {
17647 gdb_mpz num (1);
17648 gdb_mpz denom (1);
17649
17650 get_dwarf2_rational_constant (die, cu, &num, &denom);
17651 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17652 {
17653 mpz_neg (num.val, num.val);
17654 mpz_neg (denom.val, denom.val);
17655 }
17656 else if (mpz_sgn (num.val) == -1)
17657 {
17658 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17659 " in DIE at %s"),
17660 sect_offset_str (die->sect_off));
17661 return;
17662 }
17663 else if (mpz_sgn (denom.val) == -1)
17664 {
17665 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17666 " in DIE at %s"),
17667 sect_offset_str (die->sect_off));
17668 return;
17669 }
17670
17671 *numerator = std::move (num);
17672 *denominator = std::move (denom);
17673 }
17674
17675 /* Assuming that ENCODING is a string whose contents starting at the
17676 K'th character is "_nn" where "nn" is a decimal number, scan that
17677 number and set RESULT to the value. K is updated to point to the
17678 character immediately following the number.
17679
17680 If the string does not conform to the format described above, false
17681 is returned, and K may or may not be changed. */
17682
17683 static bool
17684 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17685 {
17686 /* The next character should be an underscore ('_') followed
17687 by a digit. */
17688 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17689 return false;
17690
17691 /* Skip the underscore. */
17692 k++;
17693 int start = k;
17694
17695 /* Determine the number of digits for our number. */
17696 while (isdigit (encoding[k]))
17697 k++;
17698 if (k == start)
17699 return false;
17700
17701 std::string copy (&encoding[start], k - start);
17702 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17703 return false;
17704
17705 return true;
17706 }
17707
17708 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17709 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17710 DENOM, update OFFSET, and return true on success. Return false on
17711 failure. */
17712
17713 static bool
17714 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17715 gdb_mpz *num, gdb_mpz *denom)
17716 {
17717 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17718 return false;
17719 return ada_get_gnat_encoded_number (encoding, offset, denom);
17720 }
17721
17722 /* Assuming DIE corresponds to a fixed point type, finish the creation
17723 of the corresponding TYPE by setting its type-specific data. CU is
17724 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17725 encodings. It is nullptr if the GNAT encoding should be
17726 ignored. */
17727
17728 static void
17729 finish_fixed_point_type (struct type *type, const char *suffix,
17730 struct die_info *die, struct dwarf2_cu *cu)
17731 {
17732 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17733 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17734
17735 /* If GNAT encodings are preferred, don't examine the
17736 attributes. */
17737 struct attribute *attr = nullptr;
17738 if (suffix == nullptr)
17739 {
17740 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17741 if (attr == nullptr)
17742 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17743 if (attr == nullptr)
17744 attr = dwarf2_attr (die, DW_AT_small, cu);
17745 }
17746
17747 /* Numerator and denominator of our fixed-point type's scaling factor.
17748 The default is a scaling factor of 1, which we use as a fallback
17749 when we are not able to decode it (problem with the debugging info,
17750 unsupported forms, bug in GDB, etc...). Using that as the default
17751 allows us to at least print the unscaled value, which might still
17752 be useful to a user. */
17753 gdb_mpz scale_num (1);
17754 gdb_mpz scale_denom (1);
17755
17756 if (attr == nullptr)
17757 {
17758 int offset = 0;
17759 if (suffix != nullptr
17760 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17761 &scale_denom)
17762 /* The number might be encoded as _nn_dd_nn_dd, where the
17763 second ratio is the 'small value. In this situation, we
17764 want the second value. */
17765 && (suffix[offset] != '_'
17766 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17767 &scale_denom)))
17768 {
17769 /* Found it. */
17770 }
17771 else
17772 {
17773 /* Scaling factor not found. Assume a scaling factor of 1,
17774 and hope for the best. At least the user will be able to
17775 see the encoded value. */
17776 scale_num = 1;
17777 scale_denom = 1;
17778 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17779 sect_offset_str (die->sect_off));
17780 }
17781 }
17782 else if (attr->name == DW_AT_binary_scale)
17783 {
17784 LONGEST scale_exp = attr->constant_value (0);
17785 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17786
17787 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
17788 }
17789 else if (attr->name == DW_AT_decimal_scale)
17790 {
17791 LONGEST scale_exp = attr->constant_value (0);
17792 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17793
17794 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
17795 }
17796 else if (attr->name == DW_AT_small)
17797 {
17798 struct die_info *scale_die;
17799 struct dwarf2_cu *scale_cu = cu;
17800
17801 scale_die = follow_die_ref (die, attr, &scale_cu);
17802 if (scale_die->tag == DW_TAG_constant)
17803 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17804 &scale_num, &scale_denom);
17805 else
17806 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17807 " (DIE at %s)"),
17808 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17809 }
17810 else
17811 {
17812 complaint (_("unsupported scale attribute %s for fixed-point type"
17813 " (DIE at %s)"),
17814 dwarf_attr_name (attr->name),
17815 sect_offset_str (die->sect_off));
17816 }
17817
17818 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
17819 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17820 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
17821 mpq_canonicalize (scaling_factor.val);
17822 }
17823
17824 /* The gnat-encoding suffix for fixed point. */
17825
17826 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
17827
17828 /* If NAME encodes an Ada fixed-point type, return a pointer to the
17829 "XF" suffix of the name. The text after this is what encodes the
17830 'small and 'delta information. Otherwise, return nullptr. */
17831
17832 static const char *
17833 gnat_encoded_fixed_point_type_info (const char *name)
17834 {
17835 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17836 }
17837
17838 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17839 (which may be different from NAME) to the architecture back-end to allow
17840 it to guess the correct format if necessary. */
17841
17842 static struct type *
17843 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17844 const char *name_hint, enum bfd_endian byte_order)
17845 {
17846 struct gdbarch *gdbarch = objfile->arch ();
17847 const struct floatformat **format;
17848 struct type *type;
17849
17850 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17851 if (format)
17852 type = init_float_type (objfile, bits, name, format, byte_order);
17853 else
17854 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17855
17856 return type;
17857 }
17858
17859 /* Allocate an integer type of size BITS and name NAME. */
17860
17861 static struct type *
17862 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17863 int bits, int unsigned_p, const char *name)
17864 {
17865 struct type *type;
17866
17867 /* Versions of Intel's C Compiler generate an integer type called "void"
17868 instead of using DW_TAG_unspecified_type. This has been seen on
17869 at least versions 14, 17, and 18. */
17870 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17871 && strcmp (name, "void") == 0)
17872 type = objfile_type (objfile)->builtin_void;
17873 else
17874 type = init_integer_type (objfile, bits, unsigned_p, name);
17875
17876 return type;
17877 }
17878
17879 /* Return true if DIE has a DW_AT_small attribute whose value is
17880 a constant rational, where both the numerator and denominator
17881 are equal to zero.
17882
17883 CU is the DIE's Compilation Unit. */
17884
17885 static bool
17886 has_zero_over_zero_small_attribute (struct die_info *die,
17887 struct dwarf2_cu *cu)
17888 {
17889 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17890 if (attr == nullptr)
17891 return false;
17892
17893 struct dwarf2_cu *scale_cu = cu;
17894 struct die_info *scale_die
17895 = follow_die_ref (die, attr, &scale_cu);
17896
17897 if (scale_die->tag != DW_TAG_constant)
17898 return false;
17899
17900 gdb_mpz num (1), denom (1);
17901 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
17902 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
17903 }
17904
17905 /* Initialise and return a floating point type of size BITS suitable for
17906 use as a component of a complex number. The NAME_HINT is passed through
17907 when initialising the floating point type and is the name of the complex
17908 type.
17909
17910 As DWARF doesn't currently provide an explicit name for the components
17911 of a complex number, but it can be helpful to have these components
17912 named, we try to select a suitable name based on the size of the
17913 component. */
17914 static struct type *
17915 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17916 struct objfile *objfile,
17917 int bits, const char *name_hint,
17918 enum bfd_endian byte_order)
17919 {
17920 gdbarch *gdbarch = objfile->arch ();
17921 struct type *tt = nullptr;
17922
17923 /* Try to find a suitable floating point builtin type of size BITS.
17924 We're going to use the name of this type as the name for the complex
17925 target type that we are about to create. */
17926 switch (cu->language)
17927 {
17928 case language_fortran:
17929 switch (bits)
17930 {
17931 case 32:
17932 tt = builtin_f_type (gdbarch)->builtin_real;
17933 break;
17934 case 64:
17935 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17936 break;
17937 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17938 case 128:
17939 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17940 break;
17941 }
17942 break;
17943 default:
17944 switch (bits)
17945 {
17946 case 32:
17947 tt = builtin_type (gdbarch)->builtin_float;
17948 break;
17949 case 64:
17950 tt = builtin_type (gdbarch)->builtin_double;
17951 break;
17952 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17953 case 128:
17954 tt = builtin_type (gdbarch)->builtin_long_double;
17955 break;
17956 }
17957 break;
17958 }
17959
17960 /* If the type we found doesn't match the size we were looking for, then
17961 pretend we didn't find a type at all, the complex target type we
17962 create will then be nameless. */
17963 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17964 tt = nullptr;
17965
17966 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17967 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17968 }
17969
17970 /* Find a representation of a given base type and install
17971 it in the TYPE field of the die. */
17972
17973 static struct type *
17974 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17975 {
17976 struct objfile *objfile = cu->per_objfile->objfile;
17977 struct type *type;
17978 struct attribute *attr;
17979 int encoding = 0, bits = 0;
17980 const char *name;
17981 gdbarch *arch;
17982
17983 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17984 if (attr != nullptr && attr->form_is_constant ())
17985 encoding = attr->constant_value (0);
17986 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17987 if (attr != nullptr)
17988 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
17989 name = dwarf2_name (die, cu);
17990 if (!name)
17991 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17992
17993 arch = objfile->arch ();
17994 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17995
17996 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17997 if (attr != nullptr && attr->form_is_constant ())
17998 {
17999 int endianity = attr->constant_value (0);
18000
18001 switch (endianity)
18002 {
18003 case DW_END_big:
18004 byte_order = BFD_ENDIAN_BIG;
18005 break;
18006 case DW_END_little:
18007 byte_order = BFD_ENDIAN_LITTLE;
18008 break;
18009 default:
18010 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18011 break;
18012 }
18013 }
18014
18015 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18016 && cu->language == language_ada
18017 && has_zero_over_zero_small_attribute (die, cu))
18018 {
18019 /* brobecker/2018-02-24: This is a fixed point type for which
18020 the scaling factor is represented as fraction whose value
18021 does not make sense (zero divided by zero), so we should
18022 normally never see these. However, there is a small category
18023 of fixed point types for which GNAT is unable to provide
18024 the scaling factor via the standard DWARF mechanisms, and
18025 for which the info is provided via the GNAT encodings instead.
18026 This is likely what this DIE is about. */
18027 encoding = (encoding == DW_ATE_signed_fixed
18028 ? DW_ATE_signed
18029 : DW_ATE_unsigned);
18030 }
18031
18032 /* With GNAT encodings, fixed-point information will be encoded in
18033 the type name. Note that this can also occur with the above
18034 zero-over-zero case, which is why this is a separate "if" rather
18035 than an "else if". */
18036 const char *gnat_encoding_suffix = nullptr;
18037 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18038 && cu->language == language_ada
18039 && name != nullptr)
18040 {
18041 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18042 if (gnat_encoding_suffix != nullptr)
18043 {
18044 gdb_assert (startswith (gnat_encoding_suffix,
18045 GNAT_FIXED_POINT_SUFFIX));
18046 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18047 name, gnat_encoding_suffix - name);
18048 /* Use -1 here so that SUFFIX points at the "_" after the
18049 "XF". */
18050 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18051
18052 encoding = (encoding == DW_ATE_signed
18053 ? DW_ATE_signed_fixed
18054 : DW_ATE_unsigned_fixed);
18055 }
18056 }
18057
18058 switch (encoding)
18059 {
18060 case DW_ATE_address:
18061 /* Turn DW_ATE_address into a void * pointer. */
18062 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18063 type = init_pointer_type (objfile, bits, name, type);
18064 break;
18065 case DW_ATE_boolean:
18066 type = init_boolean_type (objfile, bits, 1, name);
18067 break;
18068 case DW_ATE_complex_float:
18069 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18070 byte_order);
18071 if (type->code () == TYPE_CODE_ERROR)
18072 {
18073 if (name == nullptr)
18074 {
18075 struct obstack *obstack
18076 = &cu->per_objfile->objfile->objfile_obstack;
18077 name = obconcat (obstack, "_Complex ", type->name (),
18078 nullptr);
18079 }
18080 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18081 }
18082 else
18083 type = init_complex_type (name, type);
18084 break;
18085 case DW_ATE_decimal_float:
18086 type = init_decfloat_type (objfile, bits, name);
18087 break;
18088 case DW_ATE_float:
18089 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18090 break;
18091 case DW_ATE_signed:
18092 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18093 break;
18094 case DW_ATE_unsigned:
18095 if (cu->language == language_fortran
18096 && name
18097 && startswith (name, "character("))
18098 type = init_character_type (objfile, bits, 1, name);
18099 else
18100 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18101 break;
18102 case DW_ATE_signed_char:
18103 if (cu->language == language_ada || cu->language == language_m2
18104 || cu->language == language_pascal
18105 || cu->language == language_fortran)
18106 type = init_character_type (objfile, bits, 0, name);
18107 else
18108 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18109 break;
18110 case DW_ATE_unsigned_char:
18111 if (cu->language == language_ada || cu->language == language_m2
18112 || cu->language == language_pascal
18113 || cu->language == language_fortran
18114 || cu->language == language_rust)
18115 type = init_character_type (objfile, bits, 1, name);
18116 else
18117 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18118 break;
18119 case DW_ATE_UTF:
18120 {
18121 if (bits == 16)
18122 type = builtin_type (arch)->builtin_char16;
18123 else if (bits == 32)
18124 type = builtin_type (arch)->builtin_char32;
18125 else
18126 {
18127 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18128 bits);
18129 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18130 }
18131 return set_die_type (die, type, cu);
18132 }
18133 break;
18134 case DW_ATE_signed_fixed:
18135 type = init_fixed_point_type (objfile, bits, 0, name);
18136 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18137 break;
18138 case DW_ATE_unsigned_fixed:
18139 type = init_fixed_point_type (objfile, bits, 1, name);
18140 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18141 break;
18142
18143 default:
18144 complaint (_("unsupported DW_AT_encoding: '%s'"),
18145 dwarf_type_encoding_name (encoding));
18146 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18147 break;
18148 }
18149
18150 if (name && strcmp (name, "char") == 0)
18151 type->set_has_no_signedness (true);
18152
18153 maybe_set_alignment (cu, die, type);
18154
18155 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18156
18157 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18158 {
18159 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18160 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18161 {
18162 unsigned real_bit_size = attr->as_unsigned ();
18163 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18164 /* Only use the attributes if they make sense together. */
18165 if (attr == nullptr
18166 || (attr->as_unsigned () + real_bit_size
18167 <= 8 * TYPE_LENGTH (type)))
18168 {
18169 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18170 = real_bit_size;
18171 if (attr != nullptr)
18172 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18173 = attr->as_unsigned ();
18174 }
18175 }
18176 }
18177
18178 return set_die_type (die, type, cu);
18179 }
18180
18181 /* A helper function that returns the name of DIE, if it refers to a
18182 variable declaration. */
18183
18184 static const char *
18185 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18186 {
18187 if (die->tag != DW_TAG_variable)
18188 return nullptr;
18189
18190 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18191 if (attr == nullptr || !attr->as_boolean ())
18192 return nullptr;
18193
18194 attr = dwarf2_attr (die, DW_AT_name, cu);
18195 if (attr == nullptr)
18196 return nullptr;
18197 return attr->as_string ();
18198 }
18199
18200 /* Parse dwarf attribute if it's a block, reference or constant and put the
18201 resulting value of the attribute into struct bound_prop.
18202 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18203
18204 static int
18205 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18206 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18207 struct type *default_type)
18208 {
18209 struct dwarf2_property_baton *baton;
18210 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18211 struct objfile *objfile = per_objfile->objfile;
18212 struct obstack *obstack = &objfile->objfile_obstack;
18213
18214 gdb_assert (default_type != NULL);
18215
18216 if (attr == NULL || prop == NULL)
18217 return 0;
18218
18219 if (attr->form_is_block ())
18220 {
18221 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18222 baton->property_type = default_type;
18223 baton->locexpr.per_cu = cu->per_cu;
18224 baton->locexpr.per_objfile = per_objfile;
18225
18226 struct dwarf_block *block = attr->as_block ();
18227 baton->locexpr.size = block->size;
18228 baton->locexpr.data = block->data;
18229 switch (attr->name)
18230 {
18231 case DW_AT_string_length:
18232 baton->locexpr.is_reference = true;
18233 break;
18234 default:
18235 baton->locexpr.is_reference = false;
18236 break;
18237 }
18238
18239 prop->set_locexpr (baton);
18240 gdb_assert (prop->baton () != NULL);
18241 }
18242 else if (attr->form_is_ref ())
18243 {
18244 struct dwarf2_cu *target_cu = cu;
18245 struct die_info *target_die;
18246 struct attribute *target_attr;
18247
18248 target_die = follow_die_ref (die, attr, &target_cu);
18249 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18250 if (target_attr == NULL)
18251 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18252 target_cu);
18253 if (target_attr == NULL)
18254 {
18255 const char *name = var_decl_name (target_die, target_cu);
18256 if (name != nullptr)
18257 {
18258 prop->set_variable_name (name);
18259 return 1;
18260 }
18261 return 0;
18262 }
18263
18264 switch (target_attr->name)
18265 {
18266 case DW_AT_location:
18267 if (target_attr->form_is_section_offset ())
18268 {
18269 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18270 baton->property_type = die_type (target_die, target_cu);
18271 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18272 prop->set_loclist (baton);
18273 gdb_assert (prop->baton () != NULL);
18274 }
18275 else if (target_attr->form_is_block ())
18276 {
18277 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18278 baton->property_type = die_type (target_die, target_cu);
18279 baton->locexpr.per_cu = cu->per_cu;
18280 baton->locexpr.per_objfile = per_objfile;
18281 struct dwarf_block *block = target_attr->as_block ();
18282 baton->locexpr.size = block->size;
18283 baton->locexpr.data = block->data;
18284 baton->locexpr.is_reference = true;
18285 prop->set_locexpr (baton);
18286 gdb_assert (prop->baton () != NULL);
18287 }
18288 else
18289 {
18290 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18291 "dynamic property");
18292 return 0;
18293 }
18294 break;
18295 case DW_AT_data_member_location:
18296 {
18297 LONGEST offset;
18298
18299 if (!handle_data_member_location (target_die, target_cu,
18300 &offset))
18301 return 0;
18302
18303 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18304 baton->property_type = read_type_die (target_die->parent,
18305 target_cu);
18306 baton->offset_info.offset = offset;
18307 baton->offset_info.type = die_type (target_die, target_cu);
18308 prop->set_addr_offset (baton);
18309 break;
18310 }
18311 }
18312 }
18313 else if (attr->form_is_constant ())
18314 prop->set_const_val (attr->constant_value (0));
18315 else
18316 {
18317 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18318 dwarf2_name (die, cu));
18319 return 0;
18320 }
18321
18322 return 1;
18323 }
18324
18325 /* See read.h. */
18326
18327 struct type *
18328 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18329 {
18330 struct type *int_type;
18331
18332 /* Helper macro to examine the various builtin types. */
18333 #define TRY_TYPE(F) \
18334 int_type = (unsigned_p \
18335 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18336 : objfile_type (objfile)->builtin_ ## F); \
18337 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18338 return int_type
18339
18340 TRY_TYPE (char);
18341 TRY_TYPE (short);
18342 TRY_TYPE (int);
18343 TRY_TYPE (long);
18344 TRY_TYPE (long_long);
18345
18346 #undef TRY_TYPE
18347
18348 gdb_assert_not_reached ("unable to find suitable integer type");
18349 }
18350
18351 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18352 present (which is valid) then compute the default type based on the
18353 compilation units address size. */
18354
18355 static struct type *
18356 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18357 {
18358 struct type *index_type = die_type (die, cu);
18359
18360 /* Dwarf-2 specifications explicitly allows to create subrange types
18361 without specifying a base type.
18362 In that case, the base type must be set to the type of
18363 the lower bound, upper bound or count, in that order, if any of these
18364 three attributes references an object that has a type.
18365 If no base type is found, the Dwarf-2 specifications say that
18366 a signed integer type of size equal to the size of an address should
18367 be used.
18368 For the following C code: `extern char gdb_int [];'
18369 GCC produces an empty range DIE.
18370 FIXME: muller/2010-05-28: Possible references to object for low bound,
18371 high bound or count are not yet handled by this code. */
18372 if (index_type->code () == TYPE_CODE_VOID)
18373 index_type = cu->addr_sized_int_type (false);
18374
18375 return index_type;
18376 }
18377
18378 /* Read the given DW_AT_subrange DIE. */
18379
18380 static struct type *
18381 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18382 {
18383 struct type *base_type, *orig_base_type;
18384 struct type *range_type;
18385 struct attribute *attr;
18386 struct dynamic_prop low, high;
18387 int low_default_is_valid;
18388 int high_bound_is_count = 0;
18389 const char *name;
18390 ULONGEST negative_mask;
18391
18392 orig_base_type = read_subrange_index_type (die, cu);
18393
18394 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18395 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18396 creating the range type, but we use the result of check_typedef
18397 when examining properties of the type. */
18398 base_type = check_typedef (orig_base_type);
18399
18400 /* The die_type call above may have already set the type for this DIE. */
18401 range_type = get_die_type (die, cu);
18402 if (range_type)
18403 return range_type;
18404
18405 high.set_const_val (0);
18406
18407 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18408 omitting DW_AT_lower_bound. */
18409 switch (cu->language)
18410 {
18411 case language_c:
18412 case language_cplus:
18413 low.set_const_val (0);
18414 low_default_is_valid = 1;
18415 break;
18416 case language_fortran:
18417 low.set_const_val (1);
18418 low_default_is_valid = 1;
18419 break;
18420 case language_d:
18421 case language_objc:
18422 case language_rust:
18423 low.set_const_val (0);
18424 low_default_is_valid = (cu->header.version >= 4);
18425 break;
18426 case language_ada:
18427 case language_m2:
18428 case language_pascal:
18429 low.set_const_val (1);
18430 low_default_is_valid = (cu->header.version >= 4);
18431 break;
18432 default:
18433 low.set_const_val (0);
18434 low_default_is_valid = 0;
18435 break;
18436 }
18437
18438 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18439 if (attr != nullptr)
18440 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18441 else if (!low_default_is_valid)
18442 complaint (_("Missing DW_AT_lower_bound "
18443 "- DIE at %s [in module %s]"),
18444 sect_offset_str (die->sect_off),
18445 objfile_name (cu->per_objfile->objfile));
18446
18447 struct attribute *attr_ub, *attr_count;
18448 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18449 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18450 {
18451 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18452 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18453 {
18454 /* If bounds are constant do the final calculation here. */
18455 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18456 high.set_const_val (low.const_val () + high.const_val () - 1);
18457 else
18458 high_bound_is_count = 1;
18459 }
18460 else
18461 {
18462 if (attr_ub != NULL)
18463 complaint (_("Unresolved DW_AT_upper_bound "
18464 "- DIE at %s [in module %s]"),
18465 sect_offset_str (die->sect_off),
18466 objfile_name (cu->per_objfile->objfile));
18467 if (attr_count != NULL)
18468 complaint (_("Unresolved DW_AT_count "
18469 "- DIE at %s [in module %s]"),
18470 sect_offset_str (die->sect_off),
18471 objfile_name (cu->per_objfile->objfile));
18472 }
18473 }
18474
18475 LONGEST bias = 0;
18476 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18477 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18478 bias = bias_attr->constant_value (0);
18479
18480 /* Normally, the DWARF producers are expected to use a signed
18481 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18482 But this is unfortunately not always the case, as witnessed
18483 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18484 is used instead. To work around that ambiguity, we treat
18485 the bounds as signed, and thus sign-extend their values, when
18486 the base type is signed. */
18487 negative_mask =
18488 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18489 if (low.kind () == PROP_CONST
18490 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18491 low.set_const_val (low.const_val () | negative_mask);
18492 if (high.kind () == PROP_CONST
18493 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18494 high.set_const_val (high.const_val () | negative_mask);
18495
18496 /* Check for bit and byte strides. */
18497 struct dynamic_prop byte_stride_prop;
18498 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18499 if (attr_byte_stride != nullptr)
18500 {
18501 struct type *prop_type = cu->addr_sized_int_type (false);
18502 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18503 prop_type);
18504 }
18505
18506 struct dynamic_prop bit_stride_prop;
18507 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18508 if (attr_bit_stride != nullptr)
18509 {
18510 /* It only makes sense to have either a bit or byte stride. */
18511 if (attr_byte_stride != nullptr)
18512 {
18513 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18514 "- DIE at %s [in module %s]"),
18515 sect_offset_str (die->sect_off),
18516 objfile_name (cu->per_objfile->objfile));
18517 attr_bit_stride = nullptr;
18518 }
18519 else
18520 {
18521 struct type *prop_type = cu->addr_sized_int_type (false);
18522 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18523 prop_type);
18524 }
18525 }
18526
18527 if (attr_byte_stride != nullptr
18528 || attr_bit_stride != nullptr)
18529 {
18530 bool byte_stride_p = (attr_byte_stride != nullptr);
18531 struct dynamic_prop *stride
18532 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18533
18534 range_type
18535 = create_range_type_with_stride (NULL, orig_base_type, &low,
18536 &high, bias, stride, byte_stride_p);
18537 }
18538 else
18539 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18540
18541 if (high_bound_is_count)
18542 range_type->bounds ()->flag_upper_bound_is_count = 1;
18543
18544 /* Ada expects an empty array on no boundary attributes. */
18545 if (attr == NULL && cu->language != language_ada)
18546 range_type->bounds ()->high.set_undefined ();
18547
18548 name = dwarf2_name (die, cu);
18549 if (name)
18550 range_type->set_name (name);
18551
18552 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18553 if (attr != nullptr)
18554 TYPE_LENGTH (range_type) = attr->constant_value (0);
18555
18556 maybe_set_alignment (cu, die, range_type);
18557
18558 set_die_type (die, range_type, cu);
18559
18560 /* set_die_type should be already done. */
18561 set_descriptive_type (range_type, die, cu);
18562
18563 return range_type;
18564 }
18565
18566 static struct type *
18567 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18568 {
18569 struct type *type;
18570
18571 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18572 type->set_name (dwarf2_name (die, cu));
18573
18574 /* In Ada, an unspecified type is typically used when the description
18575 of the type is deferred to a different unit. When encountering
18576 such a type, we treat it as a stub, and try to resolve it later on,
18577 when needed. */
18578 if (cu->language == language_ada)
18579 type->set_is_stub (true);
18580
18581 return set_die_type (die, type, cu);
18582 }
18583
18584 /* Read a single die and all its descendents. Set the die's sibling
18585 field to NULL; set other fields in the die correctly, and set all
18586 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18587 location of the info_ptr after reading all of those dies. PARENT
18588 is the parent of the die in question. */
18589
18590 static struct die_info *
18591 read_die_and_children (const struct die_reader_specs *reader,
18592 const gdb_byte *info_ptr,
18593 const gdb_byte **new_info_ptr,
18594 struct die_info *parent)
18595 {
18596 struct die_info *die;
18597 const gdb_byte *cur_ptr;
18598
18599 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18600 if (die == NULL)
18601 {
18602 *new_info_ptr = cur_ptr;
18603 return NULL;
18604 }
18605 store_in_ref_table (die, reader->cu);
18606
18607 if (die->has_children)
18608 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18609 else
18610 {
18611 die->child = NULL;
18612 *new_info_ptr = cur_ptr;
18613 }
18614
18615 die->sibling = NULL;
18616 die->parent = parent;
18617 return die;
18618 }
18619
18620 /* Read a die, all of its descendents, and all of its siblings; set
18621 all of the fields of all of the dies correctly. Arguments are as
18622 in read_die_and_children. */
18623
18624 static struct die_info *
18625 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18626 const gdb_byte *info_ptr,
18627 const gdb_byte **new_info_ptr,
18628 struct die_info *parent)
18629 {
18630 struct die_info *first_die, *last_sibling;
18631 const gdb_byte *cur_ptr;
18632
18633 cur_ptr = info_ptr;
18634 first_die = last_sibling = NULL;
18635
18636 while (1)
18637 {
18638 struct die_info *die
18639 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18640
18641 if (die == NULL)
18642 {
18643 *new_info_ptr = cur_ptr;
18644 return first_die;
18645 }
18646
18647 if (!first_die)
18648 first_die = die;
18649 else
18650 last_sibling->sibling = die;
18651
18652 last_sibling = die;
18653 }
18654 }
18655
18656 /* Read a die, all of its descendents, and all of its siblings; set
18657 all of the fields of all of the dies correctly. Arguments are as
18658 in read_die_and_children.
18659 This the main entry point for reading a DIE and all its children. */
18660
18661 static struct die_info *
18662 read_die_and_siblings (const struct die_reader_specs *reader,
18663 const gdb_byte *info_ptr,
18664 const gdb_byte **new_info_ptr,
18665 struct die_info *parent)
18666 {
18667 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18668 new_info_ptr, parent);
18669
18670 if (dwarf_die_debug)
18671 {
18672 fprintf_unfiltered (gdb_stdlog,
18673 "Read die from %s@0x%x of %s:\n",
18674 reader->die_section->get_name (),
18675 (unsigned) (info_ptr - reader->die_section->buffer),
18676 bfd_get_filename (reader->abfd));
18677 dump_die (die, dwarf_die_debug);
18678 }
18679
18680 return die;
18681 }
18682
18683 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18684 attributes.
18685 The caller is responsible for filling in the extra attributes
18686 and updating (*DIEP)->num_attrs.
18687 Set DIEP to point to a newly allocated die with its information,
18688 except for its child, sibling, and parent fields. */
18689
18690 static const gdb_byte *
18691 read_full_die_1 (const struct die_reader_specs *reader,
18692 struct die_info **diep, const gdb_byte *info_ptr,
18693 int num_extra_attrs)
18694 {
18695 unsigned int abbrev_number, bytes_read, i;
18696 const struct abbrev_info *abbrev;
18697 struct die_info *die;
18698 struct dwarf2_cu *cu = reader->cu;
18699 bfd *abfd = reader->abfd;
18700
18701 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18702 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18703 info_ptr += bytes_read;
18704 if (!abbrev_number)
18705 {
18706 *diep = NULL;
18707 return info_ptr;
18708 }
18709
18710 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18711 if (!abbrev)
18712 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18713 abbrev_number,
18714 bfd_get_filename (abfd));
18715
18716 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18717 die->sect_off = sect_off;
18718 die->tag = abbrev->tag;
18719 die->abbrev = abbrev_number;
18720 die->has_children = abbrev->has_children;
18721
18722 /* Make the result usable.
18723 The caller needs to update num_attrs after adding the extra
18724 attributes. */
18725 die->num_attrs = abbrev->num_attrs;
18726
18727 bool any_need_reprocess = false;
18728 for (i = 0; i < abbrev->num_attrs; ++i)
18729 {
18730 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18731 info_ptr);
18732 if (die->attrs[i].requires_reprocessing_p ())
18733 any_need_reprocess = true;
18734 }
18735
18736 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18737 if (attr != nullptr && attr->form_is_unsigned ())
18738 cu->str_offsets_base = attr->as_unsigned ();
18739
18740 attr = die->attr (DW_AT_loclists_base);
18741 if (attr != nullptr)
18742 cu->loclist_base = attr->as_unsigned ();
18743
18744 auto maybe_addr_base = die->addr_base ();
18745 if (maybe_addr_base.has_value ())
18746 cu->addr_base = *maybe_addr_base;
18747
18748 attr = die->attr (DW_AT_rnglists_base);
18749 if (attr != nullptr)
18750 cu->rnglists_base = attr->as_unsigned ();
18751
18752 if (any_need_reprocess)
18753 {
18754 for (i = 0; i < abbrev->num_attrs; ++i)
18755 {
18756 if (die->attrs[i].requires_reprocessing_p ())
18757 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18758 }
18759 }
18760 *diep = die;
18761 return info_ptr;
18762 }
18763
18764 /* Read a die and all its attributes.
18765 Set DIEP to point to a newly allocated die with its information,
18766 except for its child, sibling, and parent fields. */
18767
18768 static const gdb_byte *
18769 read_full_die (const struct die_reader_specs *reader,
18770 struct die_info **diep, const gdb_byte *info_ptr)
18771 {
18772 const gdb_byte *result;
18773
18774 result = read_full_die_1 (reader, diep, info_ptr, 0);
18775
18776 if (dwarf_die_debug)
18777 {
18778 fprintf_unfiltered (gdb_stdlog,
18779 "Read die from %s@0x%x of %s:\n",
18780 reader->die_section->get_name (),
18781 (unsigned) (info_ptr - reader->die_section->buffer),
18782 bfd_get_filename (reader->abfd));
18783 dump_die (*diep, dwarf_die_debug);
18784 }
18785
18786 return result;
18787 }
18788 \f
18789
18790 /* Returns nonzero if TAG represents a type that we might generate a partial
18791 symbol for. */
18792
18793 static int
18794 is_type_tag_for_partial (int tag, enum language lang)
18795 {
18796 switch (tag)
18797 {
18798 #if 0
18799 /* Some types that would be reasonable to generate partial symbols for,
18800 that we don't at present. Note that normally this does not
18801 matter, mainly because C compilers don't give names to these
18802 types, but instead emit DW_TAG_typedef. */
18803 case DW_TAG_file_type:
18804 case DW_TAG_ptr_to_member_type:
18805 case DW_TAG_set_type:
18806 case DW_TAG_string_type:
18807 case DW_TAG_subroutine_type:
18808 #endif
18809
18810 /* GNAT may emit an array with a name, but no typedef, so we
18811 need to make a symbol in this case. */
18812 case DW_TAG_array_type:
18813 return lang == language_ada;
18814
18815 case DW_TAG_base_type:
18816 case DW_TAG_class_type:
18817 case DW_TAG_interface_type:
18818 case DW_TAG_enumeration_type:
18819 case DW_TAG_structure_type:
18820 case DW_TAG_subrange_type:
18821 case DW_TAG_typedef:
18822 case DW_TAG_union_type:
18823 return 1;
18824 default:
18825 return 0;
18826 }
18827 }
18828
18829 /* Load all DIEs that are interesting for partial symbols into memory. */
18830
18831 static struct partial_die_info *
18832 load_partial_dies (const struct die_reader_specs *reader,
18833 const gdb_byte *info_ptr, int building_psymtab)
18834 {
18835 struct dwarf2_cu *cu = reader->cu;
18836 struct objfile *objfile = cu->per_objfile->objfile;
18837 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18838 unsigned int bytes_read;
18839 unsigned int load_all = 0;
18840 int nesting_level = 1;
18841
18842 parent_die = NULL;
18843 last_die = NULL;
18844
18845 gdb_assert (cu->per_cu != NULL);
18846 if (cu->load_all_dies)
18847 load_all = 1;
18848
18849 cu->partial_dies
18850 = htab_create_alloc_ex (cu->header.length / 12,
18851 partial_die_hash,
18852 partial_die_eq,
18853 NULL,
18854 &cu->comp_unit_obstack,
18855 hashtab_obstack_allocate,
18856 dummy_obstack_deallocate);
18857
18858 while (1)
18859 {
18860 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18861 &bytes_read);
18862
18863 /* A NULL abbrev means the end of a series of children. */
18864 if (abbrev == NULL)
18865 {
18866 if (--nesting_level == 0)
18867 return first_die;
18868
18869 info_ptr += bytes_read;
18870 last_die = parent_die;
18871 parent_die = parent_die->die_parent;
18872 continue;
18873 }
18874
18875 /* Check for template arguments. We never save these; if
18876 they're seen, we just mark the parent, and go on our way. */
18877 if (parent_die != NULL
18878 && cu->language == language_cplus
18879 && (abbrev->tag == DW_TAG_template_type_param
18880 || abbrev->tag == DW_TAG_template_value_param))
18881 {
18882 parent_die->has_template_arguments = 1;
18883
18884 if (!load_all)
18885 {
18886 /* We don't need a partial DIE for the template argument. */
18887 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18888 continue;
18889 }
18890 }
18891
18892 /* We only recurse into c++ subprograms looking for template arguments.
18893 Skip their other children. */
18894 if (!load_all
18895 && cu->language == language_cplus
18896 && parent_die != NULL
18897 && parent_die->tag == DW_TAG_subprogram
18898 && abbrev->tag != DW_TAG_inlined_subroutine)
18899 {
18900 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18901 continue;
18902 }
18903
18904 /* Check whether this DIE is interesting enough to save. Normally
18905 we would not be interested in members here, but there may be
18906 later variables referencing them via DW_AT_specification (for
18907 static members). */
18908 if (!load_all
18909 && !is_type_tag_for_partial (abbrev->tag, cu->language)
18910 && abbrev->tag != DW_TAG_constant
18911 && abbrev->tag != DW_TAG_enumerator
18912 && abbrev->tag != DW_TAG_subprogram
18913 && abbrev->tag != DW_TAG_inlined_subroutine
18914 && abbrev->tag != DW_TAG_lexical_block
18915 && abbrev->tag != DW_TAG_variable
18916 && abbrev->tag != DW_TAG_namespace
18917 && abbrev->tag != DW_TAG_module
18918 && abbrev->tag != DW_TAG_member
18919 && abbrev->tag != DW_TAG_imported_unit
18920 && abbrev->tag != DW_TAG_imported_declaration)
18921 {
18922 /* Otherwise we skip to the next sibling, if any. */
18923 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18924 continue;
18925 }
18926
18927 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18928 abbrev);
18929
18930 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18931
18932 /* This two-pass algorithm for processing partial symbols has a
18933 high cost in cache pressure. Thus, handle some simple cases
18934 here which cover the majority of C partial symbols. DIEs
18935 which neither have specification tags in them, nor could have
18936 specification tags elsewhere pointing at them, can simply be
18937 processed and discarded.
18938
18939 This segment is also optional; scan_partial_symbols and
18940 add_partial_symbol will handle these DIEs if we chain
18941 them in normally. When compilers which do not emit large
18942 quantities of duplicate debug information are more common,
18943 this code can probably be removed. */
18944
18945 /* Any complete simple types at the top level (pretty much all
18946 of them, for a language without namespaces), can be processed
18947 directly. */
18948 if (parent_die == NULL
18949 && pdi.has_specification == 0
18950 && pdi.is_declaration == 0
18951 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18952 || pdi.tag == DW_TAG_base_type
18953 || pdi.tag == DW_TAG_array_type
18954 || pdi.tag == DW_TAG_subrange_type))
18955 {
18956 if (building_psymtab && pdi.raw_name != NULL)
18957 add_partial_symbol (&pdi, cu);
18958
18959 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18960 continue;
18961 }
18962
18963 /* The exception for DW_TAG_typedef with has_children above is
18964 a workaround of GCC PR debug/47510. In the case of this complaint
18965 type_name_or_error will error on such types later.
18966
18967 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18968 it could not find the child DIEs referenced later, this is checked
18969 above. In correct DWARF DW_TAG_typedef should have no children. */
18970
18971 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18972 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18973 "- DIE at %s [in module %s]"),
18974 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18975
18976 /* If we're at the second level, and we're an enumerator, and
18977 our parent has no specification (meaning possibly lives in a
18978 namespace elsewhere), then we can add the partial symbol now
18979 instead of queueing it. */
18980 if (pdi.tag == DW_TAG_enumerator
18981 && parent_die != NULL
18982 && parent_die->die_parent == NULL
18983 && parent_die->tag == DW_TAG_enumeration_type
18984 && parent_die->has_specification == 0)
18985 {
18986 if (pdi.raw_name == NULL)
18987 complaint (_("malformed enumerator DIE ignored"));
18988 else if (building_psymtab)
18989 add_partial_symbol (&pdi, cu);
18990
18991 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18992 continue;
18993 }
18994
18995 struct partial_die_info *part_die
18996 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18997
18998 /* We'll save this DIE so link it in. */
18999 part_die->die_parent = parent_die;
19000 part_die->die_sibling = NULL;
19001 part_die->die_child = NULL;
19002
19003 if (last_die && last_die == parent_die)
19004 last_die->die_child = part_die;
19005 else if (last_die)
19006 last_die->die_sibling = part_die;
19007
19008 last_die = part_die;
19009
19010 if (first_die == NULL)
19011 first_die = part_die;
19012
19013 /* Maybe add the DIE to the hash table. Not all DIEs that we
19014 find interesting need to be in the hash table, because we
19015 also have the parent/sibling/child chains; only those that we
19016 might refer to by offset later during partial symbol reading.
19017
19018 For now this means things that might have be the target of a
19019 DW_AT_specification, DW_AT_abstract_origin, or
19020 DW_AT_extension. DW_AT_extension will refer only to
19021 namespaces; DW_AT_abstract_origin refers to functions (and
19022 many things under the function DIE, but we do not recurse
19023 into function DIEs during partial symbol reading) and
19024 possibly variables as well; DW_AT_specification refers to
19025 declarations. Declarations ought to have the DW_AT_declaration
19026 flag. It happens that GCC forgets to put it in sometimes, but
19027 only for functions, not for types.
19028
19029 Adding more things than necessary to the hash table is harmless
19030 except for the performance cost. Adding too few will result in
19031 wasted time in find_partial_die, when we reread the compilation
19032 unit with load_all_dies set. */
19033
19034 if (load_all
19035 || abbrev->tag == DW_TAG_constant
19036 || abbrev->tag == DW_TAG_subprogram
19037 || abbrev->tag == DW_TAG_variable
19038 || abbrev->tag == DW_TAG_namespace
19039 || part_die->is_declaration)
19040 {
19041 void **slot;
19042
19043 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19044 to_underlying (part_die->sect_off),
19045 INSERT);
19046 *slot = part_die;
19047 }
19048
19049 /* For some DIEs we want to follow their children (if any). For C
19050 we have no reason to follow the children of structures; for other
19051 languages we have to, so that we can get at method physnames
19052 to infer fully qualified class names, for DW_AT_specification,
19053 and for C++ template arguments. For C++, we also look one level
19054 inside functions to find template arguments (if the name of the
19055 function does not already contain the template arguments).
19056
19057 For Ada and Fortran, we need to scan the children of subprograms
19058 and lexical blocks as well because these languages allow the
19059 definition of nested entities that could be interesting for the
19060 debugger, such as nested subprograms for instance. */
19061 if (last_die->has_children
19062 && (load_all
19063 || last_die->tag == DW_TAG_namespace
19064 || last_die->tag == DW_TAG_module
19065 || last_die->tag == DW_TAG_enumeration_type
19066 || (cu->language == language_cplus
19067 && last_die->tag == DW_TAG_subprogram
19068 && (last_die->raw_name == NULL
19069 || strchr (last_die->raw_name, '<') == NULL))
19070 || (cu->language != language_c
19071 && (last_die->tag == DW_TAG_class_type
19072 || last_die->tag == DW_TAG_interface_type
19073 || last_die->tag == DW_TAG_structure_type
19074 || last_die->tag == DW_TAG_union_type))
19075 || ((cu->language == language_ada
19076 || cu->language == language_fortran)
19077 && (last_die->tag == DW_TAG_subprogram
19078 || last_die->tag == DW_TAG_lexical_block))))
19079 {
19080 nesting_level++;
19081 parent_die = last_die;
19082 continue;
19083 }
19084
19085 /* Otherwise we skip to the next sibling, if any. */
19086 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19087
19088 /* Back to the top, do it again. */
19089 }
19090 }
19091
19092 partial_die_info::partial_die_info (sect_offset sect_off_,
19093 const struct abbrev_info *abbrev)
19094 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19095 {
19096 }
19097
19098 /* See class definition. */
19099
19100 const char *
19101 partial_die_info::name (dwarf2_cu *cu)
19102 {
19103 if (!canonical_name && raw_name != nullptr)
19104 {
19105 struct objfile *objfile = cu->per_objfile->objfile;
19106 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19107 canonical_name = 1;
19108 }
19109
19110 return raw_name;
19111 }
19112
19113 /* Read a minimal amount of information into the minimal die structure.
19114 INFO_PTR should point just after the initial uleb128 of a DIE. */
19115
19116 const gdb_byte *
19117 partial_die_info::read (const struct die_reader_specs *reader,
19118 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19119 {
19120 struct dwarf2_cu *cu = reader->cu;
19121 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19122 unsigned int i;
19123 int has_low_pc_attr = 0;
19124 int has_high_pc_attr = 0;
19125 int high_pc_relative = 0;
19126
19127 for (i = 0; i < abbrev.num_attrs; ++i)
19128 {
19129 attribute attr;
19130 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19131 /* String and address offsets that need to do the reprocessing have
19132 already been read at this point, so there is no need to wait until
19133 the loop terminates to do the reprocessing. */
19134 if (attr.requires_reprocessing_p ())
19135 read_attribute_reprocess (reader, &attr, tag);
19136 /* Store the data if it is of an attribute we want to keep in a
19137 partial symbol table. */
19138 switch (attr.name)
19139 {
19140 case DW_AT_name:
19141 switch (tag)
19142 {
19143 case DW_TAG_compile_unit:
19144 case DW_TAG_partial_unit:
19145 case DW_TAG_type_unit:
19146 /* Compilation units have a DW_AT_name that is a filename, not
19147 a source language identifier. */
19148 case DW_TAG_enumeration_type:
19149 case DW_TAG_enumerator:
19150 /* These tags always have simple identifiers already; no need
19151 to canonicalize them. */
19152 canonical_name = 1;
19153 raw_name = attr.as_string ();
19154 break;
19155 default:
19156 canonical_name = 0;
19157 raw_name = attr.as_string ();
19158 break;
19159 }
19160 break;
19161 case DW_AT_linkage_name:
19162 case DW_AT_MIPS_linkage_name:
19163 /* Note that both forms of linkage name might appear. We
19164 assume they will be the same, and we only store the last
19165 one we see. */
19166 linkage_name = attr.as_string ();
19167 break;
19168 case DW_AT_low_pc:
19169 has_low_pc_attr = 1;
19170 lowpc = attr.as_address ();
19171 break;
19172 case DW_AT_high_pc:
19173 has_high_pc_attr = 1;
19174 highpc = attr.as_address ();
19175 if (cu->header.version >= 4 && attr.form_is_constant ())
19176 high_pc_relative = 1;
19177 break;
19178 case DW_AT_location:
19179 /* Support the .debug_loc offsets. */
19180 if (attr.form_is_block ())
19181 {
19182 d.locdesc = attr.as_block ();
19183 }
19184 else if (attr.form_is_section_offset ())
19185 {
19186 dwarf2_complex_location_expr_complaint ();
19187 }
19188 else
19189 {
19190 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19191 "partial symbol information");
19192 }
19193 break;
19194 case DW_AT_external:
19195 is_external = attr.as_boolean ();
19196 break;
19197 case DW_AT_declaration:
19198 is_declaration = attr.as_boolean ();
19199 break;
19200 case DW_AT_type:
19201 has_type = 1;
19202 break;
19203 case DW_AT_abstract_origin:
19204 case DW_AT_specification:
19205 case DW_AT_extension:
19206 has_specification = 1;
19207 spec_offset = attr.get_ref_die_offset ();
19208 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19209 || cu->per_cu->is_dwz);
19210 break;
19211 case DW_AT_sibling:
19212 /* Ignore absolute siblings, they might point outside of
19213 the current compile unit. */
19214 if (attr.form == DW_FORM_ref_addr)
19215 complaint (_("ignoring absolute DW_AT_sibling"));
19216 else
19217 {
19218 const gdb_byte *buffer = reader->buffer;
19219 sect_offset off = attr.get_ref_die_offset ();
19220 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19221
19222 if (sibling_ptr < info_ptr)
19223 complaint (_("DW_AT_sibling points backwards"));
19224 else if (sibling_ptr > reader->buffer_end)
19225 reader->die_section->overflow_complaint ();
19226 else
19227 sibling = sibling_ptr;
19228 }
19229 break;
19230 case DW_AT_byte_size:
19231 has_byte_size = 1;
19232 break;
19233 case DW_AT_const_value:
19234 has_const_value = 1;
19235 break;
19236 case DW_AT_calling_convention:
19237 /* DWARF doesn't provide a way to identify a program's source-level
19238 entry point. DW_AT_calling_convention attributes are only meant
19239 to describe functions' calling conventions.
19240
19241 However, because it's a necessary piece of information in
19242 Fortran, and before DWARF 4 DW_CC_program was the only
19243 piece of debugging information whose definition refers to
19244 a 'main program' at all, several compilers marked Fortran
19245 main programs with DW_CC_program --- even when those
19246 functions use the standard calling conventions.
19247
19248 Although DWARF now specifies a way to provide this
19249 information, we support this practice for backward
19250 compatibility. */
19251 if (attr.constant_value (0) == DW_CC_program
19252 && cu->language == language_fortran)
19253 main_subprogram = 1;
19254 break;
19255 case DW_AT_inline:
19256 {
19257 LONGEST value = attr.constant_value (-1);
19258 if (value == DW_INL_inlined
19259 || value == DW_INL_declared_inlined)
19260 may_be_inlined = 1;
19261 }
19262 break;
19263
19264 case DW_AT_import:
19265 if (tag == DW_TAG_imported_unit)
19266 {
19267 d.sect_off = attr.get_ref_die_offset ();
19268 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19269 || cu->per_cu->is_dwz);
19270 }
19271 break;
19272
19273 case DW_AT_main_subprogram:
19274 main_subprogram = attr.as_boolean ();
19275 break;
19276
19277 case DW_AT_ranges:
19278 {
19279 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19280 on DWARF version). */
19281 ULONGEST ranges_offset = attr.as_unsigned ();
19282
19283 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19284 this value. */
19285 if (tag != DW_TAG_compile_unit)
19286 ranges_offset += cu->gnu_ranges_base;
19287
19288 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19289 nullptr, tag))
19290 has_pc_info = 1;
19291 }
19292 break;
19293
19294 default:
19295 break;
19296 }
19297 }
19298
19299 /* For Ada, if both the name and the linkage name appear, we prefer
19300 the latter. This lets "catch exception" work better, regardless
19301 of the order in which the name and linkage name were emitted.
19302 Really, though, this is just a workaround for the fact that gdb
19303 doesn't store both the name and the linkage name. */
19304 if (cu->language == language_ada && linkage_name != nullptr)
19305 raw_name = linkage_name;
19306
19307 if (high_pc_relative)
19308 highpc += lowpc;
19309
19310 if (has_low_pc_attr && has_high_pc_attr)
19311 {
19312 /* When using the GNU linker, .gnu.linkonce. sections are used to
19313 eliminate duplicate copies of functions and vtables and such.
19314 The linker will arbitrarily choose one and discard the others.
19315 The AT_*_pc values for such functions refer to local labels in
19316 these sections. If the section from that file was discarded, the
19317 labels are not in the output, so the relocs get a value of 0.
19318 If this is a discarded function, mark the pc bounds as invalid,
19319 so that GDB will ignore it. */
19320 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19321 {
19322 struct objfile *objfile = per_objfile->objfile;
19323 struct gdbarch *gdbarch = objfile->arch ();
19324
19325 complaint (_("DW_AT_low_pc %s is zero "
19326 "for DIE at %s [in module %s]"),
19327 paddress (gdbarch, lowpc),
19328 sect_offset_str (sect_off),
19329 objfile_name (objfile));
19330 }
19331 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19332 else if (lowpc >= highpc)
19333 {
19334 struct objfile *objfile = per_objfile->objfile;
19335 struct gdbarch *gdbarch = objfile->arch ();
19336
19337 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19338 "for DIE at %s [in module %s]"),
19339 paddress (gdbarch, lowpc),
19340 paddress (gdbarch, highpc),
19341 sect_offset_str (sect_off),
19342 objfile_name (objfile));
19343 }
19344 else
19345 has_pc_info = 1;
19346 }
19347
19348 return info_ptr;
19349 }
19350
19351 /* Find a cached partial DIE at OFFSET in CU. */
19352
19353 struct partial_die_info *
19354 dwarf2_cu::find_partial_die (sect_offset sect_off)
19355 {
19356 struct partial_die_info *lookup_die = NULL;
19357 struct partial_die_info part_die (sect_off);
19358
19359 lookup_die = ((struct partial_die_info *)
19360 htab_find_with_hash (partial_dies, &part_die,
19361 to_underlying (sect_off)));
19362
19363 return lookup_die;
19364 }
19365
19366 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19367 except in the case of .debug_types DIEs which do not reference
19368 outside their CU (they do however referencing other types via
19369 DW_FORM_ref_sig8). */
19370
19371 static const struct cu_partial_die_info
19372 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19373 {
19374 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19375 struct objfile *objfile = per_objfile->objfile;
19376 struct partial_die_info *pd = NULL;
19377
19378 if (offset_in_dwz == cu->per_cu->is_dwz
19379 && cu->header.offset_in_cu_p (sect_off))
19380 {
19381 pd = cu->find_partial_die (sect_off);
19382 if (pd != NULL)
19383 return { cu, pd };
19384 /* We missed recording what we needed.
19385 Load all dies and try again. */
19386 }
19387 else
19388 {
19389 /* TUs don't reference other CUs/TUs (except via type signatures). */
19390 if (cu->per_cu->is_debug_types)
19391 {
19392 error (_("Dwarf Error: Type Unit at offset %s contains"
19393 " external reference to offset %s [in module %s].\n"),
19394 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19395 bfd_get_filename (objfile->obfd));
19396 }
19397 dwarf2_per_cu_data *per_cu
19398 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19399 per_objfile);
19400
19401 cu = per_objfile->get_cu (per_cu);
19402 if (cu == NULL || cu->partial_dies == NULL)
19403 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19404
19405 cu = per_objfile->get_cu (per_cu);
19406
19407 cu->last_used = 0;
19408 pd = cu->find_partial_die (sect_off);
19409 }
19410
19411 /* If we didn't find it, and not all dies have been loaded,
19412 load them all and try again. */
19413
19414 if (pd == NULL && cu->load_all_dies == 0)
19415 {
19416 cu->load_all_dies = 1;
19417
19418 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19419 THIS_CU->cu may already be in use. So we can't just free it and
19420 replace its DIEs with the ones we read in. Instead, we leave those
19421 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19422 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19423 set. */
19424 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19425
19426 pd = cu->find_partial_die (sect_off);
19427 }
19428
19429 if (pd == NULL)
19430 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
19431 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19432 return { cu, pd };
19433 }
19434
19435 /* See if we can figure out if the class lives in a namespace. We do
19436 this by looking for a member function; its demangled name will
19437 contain namespace info, if there is any. */
19438
19439 static void
19440 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19441 struct dwarf2_cu *cu)
19442 {
19443 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19444 what template types look like, because the demangler
19445 frequently doesn't give the same name as the debug info. We
19446 could fix this by only using the demangled name to get the
19447 prefix (but see comment in read_structure_type). */
19448
19449 struct partial_die_info *real_pdi;
19450 struct partial_die_info *child_pdi;
19451
19452 /* If this DIE (this DIE's specification, if any) has a parent, then
19453 we should not do this. We'll prepend the parent's fully qualified
19454 name when we create the partial symbol. */
19455
19456 real_pdi = struct_pdi;
19457 while (real_pdi->has_specification)
19458 {
19459 auto res = find_partial_die (real_pdi->spec_offset,
19460 real_pdi->spec_is_dwz, cu);
19461 real_pdi = res.pdi;
19462 cu = res.cu;
19463 }
19464
19465 if (real_pdi->die_parent != NULL)
19466 return;
19467
19468 for (child_pdi = struct_pdi->die_child;
19469 child_pdi != NULL;
19470 child_pdi = child_pdi->die_sibling)
19471 {
19472 if (child_pdi->tag == DW_TAG_subprogram
19473 && child_pdi->linkage_name != NULL)
19474 {
19475 gdb::unique_xmalloc_ptr<char> actual_class_name
19476 (cu->language_defn->class_name_from_physname
19477 (child_pdi->linkage_name));
19478 if (actual_class_name != NULL)
19479 {
19480 struct objfile *objfile = cu->per_objfile->objfile;
19481 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19482 struct_pdi->canonical_name = 1;
19483 }
19484 break;
19485 }
19486 }
19487 }
19488
19489 /* Return true if a DIE with TAG may have the DW_AT_const_value
19490 attribute. */
19491
19492 static bool
19493 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19494 {
19495 switch (tag)
19496 {
19497 case DW_TAG_constant:
19498 case DW_TAG_enumerator:
19499 case DW_TAG_formal_parameter:
19500 case DW_TAG_template_value_param:
19501 case DW_TAG_variable:
19502 return true;
19503 }
19504
19505 return false;
19506 }
19507
19508 void
19509 partial_die_info::fixup (struct dwarf2_cu *cu)
19510 {
19511 /* Once we've fixed up a die, there's no point in doing so again.
19512 This also avoids a memory leak if we were to call
19513 guess_partial_die_structure_name multiple times. */
19514 if (fixup_called)
19515 return;
19516
19517 /* If we found a reference attribute and the DIE has no name, try
19518 to find a name in the referred to DIE. */
19519
19520 if (raw_name == NULL && has_specification)
19521 {
19522 struct partial_die_info *spec_die;
19523
19524 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19525 spec_die = res.pdi;
19526 cu = res.cu;
19527
19528 spec_die->fixup (cu);
19529
19530 if (spec_die->raw_name)
19531 {
19532 raw_name = spec_die->raw_name;
19533 canonical_name = spec_die->canonical_name;
19534
19535 /* Copy DW_AT_external attribute if it is set. */
19536 if (spec_die->is_external)
19537 is_external = spec_die->is_external;
19538 }
19539 }
19540
19541 if (!has_const_value && has_specification
19542 && can_have_DW_AT_const_value_p (tag))
19543 {
19544 struct partial_die_info *spec_die;
19545
19546 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19547 spec_die = res.pdi;
19548 cu = res.cu;
19549
19550 spec_die->fixup (cu);
19551
19552 if (spec_die->has_const_value)
19553 {
19554 /* Copy DW_AT_const_value attribute if it is set. */
19555 has_const_value = spec_die->has_const_value;
19556 }
19557 }
19558
19559 /* Set default names for some unnamed DIEs. */
19560
19561 if (raw_name == NULL && tag == DW_TAG_namespace)
19562 {
19563 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19564 canonical_name = 1;
19565 }
19566
19567 /* If there is no parent die to provide a namespace, and there are
19568 children, see if we can determine the namespace from their linkage
19569 name. */
19570 if (cu->language == language_cplus
19571 && !cu->per_objfile->per_bfd->types.empty ()
19572 && die_parent == NULL
19573 && has_children
19574 && (tag == DW_TAG_class_type
19575 || tag == DW_TAG_structure_type
19576 || tag == DW_TAG_union_type))
19577 guess_partial_die_structure_name (this, cu);
19578
19579 /* GCC might emit a nameless struct or union that has a linkage
19580 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19581 if (raw_name == NULL
19582 && (tag == DW_TAG_class_type
19583 || tag == DW_TAG_interface_type
19584 || tag == DW_TAG_structure_type
19585 || tag == DW_TAG_union_type)
19586 && linkage_name != NULL)
19587 {
19588 gdb::unique_xmalloc_ptr<char> demangled
19589 (gdb_demangle (linkage_name, DMGL_TYPES));
19590 if (demangled != nullptr)
19591 {
19592 const char *base;
19593
19594 /* Strip any leading namespaces/classes, keep only the base name.
19595 DW_AT_name for named DIEs does not contain the prefixes. */
19596 base = strrchr (demangled.get (), ':');
19597 if (base && base > demangled.get () && base[-1] == ':')
19598 base++;
19599 else
19600 base = demangled.get ();
19601
19602 struct objfile *objfile = cu->per_objfile->objfile;
19603 raw_name = objfile->intern (base);
19604 canonical_name = 1;
19605 }
19606 }
19607
19608 fixup_called = 1;
19609 }
19610
19611 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19612 contents from the given SECTION in the HEADER.
19613
19614 HEADER_OFFSET is the offset of the header in the section. */
19615 static void
19616 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19617 struct dwarf2_section_info *section,
19618 sect_offset header_offset)
19619 {
19620 unsigned int bytes_read;
19621 bfd *abfd = section->get_bfd_owner ();
19622 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19623
19624 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19625 info_ptr += bytes_read;
19626
19627 header->version = read_2_bytes (abfd, info_ptr);
19628 info_ptr += 2;
19629
19630 header->addr_size = read_1_byte (abfd, info_ptr);
19631 info_ptr += 1;
19632
19633 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19634 info_ptr += 1;
19635
19636 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19637 }
19638
19639 /* Return the DW_AT_loclists_base value for the CU. */
19640 static ULONGEST
19641 lookup_loclist_base (struct dwarf2_cu *cu)
19642 {
19643 /* For the .dwo unit, the loclist_base points to the first offset following
19644 the header. The header consists of the following entities-
19645 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19646 bit format)
19647 2. version (2 bytes)
19648 3. address size (1 byte)
19649 4. segment selector size (1 byte)
19650 5. offset entry count (4 bytes)
19651 These sizes are derived as per the DWARFv5 standard. */
19652 if (cu->dwo_unit != nullptr)
19653 {
19654 if (cu->header.initial_length_size == 4)
19655 return LOCLIST_HEADER_SIZE32;
19656 return LOCLIST_HEADER_SIZE64;
19657 }
19658 return cu->loclist_base;
19659 }
19660
19661 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19662 array of offsets in the .debug_loclists section. */
19663
19664 static sect_offset
19665 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19666 {
19667 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19668 struct objfile *objfile = per_objfile->objfile;
19669 bfd *abfd = objfile->obfd;
19670 ULONGEST loclist_header_size =
19671 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19672 : LOCLIST_HEADER_SIZE64);
19673 ULONGEST loclist_base = lookup_loclist_base (cu);
19674
19675 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19676 ULONGEST start_offset =
19677 loclist_base + loclist_index * cu->header.offset_size;
19678
19679 /* Get loclists section. */
19680 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19681
19682 /* Read the loclists section content. */
19683 section->read (objfile);
19684 if (section->buffer == NULL)
19685 error (_("DW_FORM_loclistx used without .debug_loclists "
19686 "section [in module %s]"), objfile_name (objfile));
19687
19688 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19689 so if loclist_base is smaller than the header size, we have a problem. */
19690 if (loclist_base < loclist_header_size)
19691 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19692 objfile_name (objfile));
19693
19694 /* Read the header of the loclists contribution. */
19695 struct loclists_rnglists_header header;
19696 read_loclists_rnglists_header (&header, section,
19697 (sect_offset) (loclist_base - loclist_header_size));
19698
19699 /* Verify the loclist index is valid. */
19700 if (loclist_index >= header.offset_entry_count)
19701 error (_("DW_FORM_loclistx pointing outside of "
19702 ".debug_loclists offset array [in module %s]"),
19703 objfile_name (objfile));
19704
19705 /* Validate that reading won't go beyond the end of the section. */
19706 if (start_offset + cu->header.offset_size > section->size)
19707 error (_("Reading DW_FORM_loclistx index beyond end of"
19708 ".debug_loclists section [in module %s]"),
19709 objfile_name (objfile));
19710
19711 const gdb_byte *info_ptr = section->buffer + start_offset;
19712
19713 if (cu->header.offset_size == 4)
19714 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19715 else
19716 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19717 }
19718
19719 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19720 array of offsets in the .debug_rnglists section. */
19721
19722 static sect_offset
19723 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19724 dwarf_tag tag)
19725 {
19726 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19727 struct objfile *objfile = dwarf2_per_objfile->objfile;
19728 bfd *abfd = objfile->obfd;
19729 ULONGEST rnglist_header_size =
19730 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19731 : RNGLIST_HEADER_SIZE64);
19732
19733 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19734 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19735 doesn't apply. */
19736 ULONGEST rnglist_base =
19737 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19738
19739 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19740 ULONGEST start_offset =
19741 rnglist_base + rnglist_index * cu->header.offset_size;
19742
19743 /* Get rnglists section. */
19744 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19745
19746 /* Read the rnglists section content. */
19747 section->read (objfile);
19748 if (section->buffer == nullptr)
19749 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19750 "[in module %s]"),
19751 objfile_name (objfile));
19752
19753 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19754 so if rnglist_base is smaller than the header size, we have a problem. */
19755 if (rnglist_base < rnglist_header_size)
19756 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19757 objfile_name (objfile));
19758
19759 /* Read the header of the rnglists contribution. */
19760 struct loclists_rnglists_header header;
19761 read_loclists_rnglists_header (&header, section,
19762 (sect_offset) (rnglist_base - rnglist_header_size));
19763
19764 /* Verify the rnglist index is valid. */
19765 if (rnglist_index >= header.offset_entry_count)
19766 error (_("DW_FORM_rnglistx index pointing outside of "
19767 ".debug_rnglists offset array [in module %s]"),
19768 objfile_name (objfile));
19769
19770 /* Validate that reading won't go beyond the end of the section. */
19771 if (start_offset + cu->header.offset_size > section->size)
19772 error (_("Reading DW_FORM_rnglistx index beyond end of"
19773 ".debug_rnglists section [in module %s]"),
19774 objfile_name (objfile));
19775
19776 const gdb_byte *info_ptr = section->buffer + start_offset;
19777
19778 if (cu->header.offset_size == 4)
19779 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19780 else
19781 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19782 }
19783
19784 /* Process the attributes that had to be skipped in the first round. These
19785 attributes are the ones that need str_offsets_base or addr_base attributes.
19786 They could not have been processed in the first round, because at the time
19787 the values of str_offsets_base or addr_base may not have been known. */
19788 static void
19789 read_attribute_reprocess (const struct die_reader_specs *reader,
19790 struct attribute *attr, dwarf_tag tag)
19791 {
19792 struct dwarf2_cu *cu = reader->cu;
19793 switch (attr->form)
19794 {
19795 case DW_FORM_addrx:
19796 case DW_FORM_GNU_addr_index:
19797 attr->set_address (read_addr_index (cu,
19798 attr->as_unsigned_reprocess ()));
19799 break;
19800 case DW_FORM_loclistx:
19801 {
19802 sect_offset loclists_sect_off
19803 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19804
19805 attr->set_unsigned (to_underlying (loclists_sect_off));
19806 }
19807 break;
19808 case DW_FORM_rnglistx:
19809 {
19810 sect_offset rnglists_sect_off
19811 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19812
19813 attr->set_unsigned (to_underlying (rnglists_sect_off));
19814 }
19815 break;
19816 case DW_FORM_strx:
19817 case DW_FORM_strx1:
19818 case DW_FORM_strx2:
19819 case DW_FORM_strx3:
19820 case DW_FORM_strx4:
19821 case DW_FORM_GNU_str_index:
19822 {
19823 unsigned int str_index = attr->as_unsigned_reprocess ();
19824 gdb_assert (!attr->canonical_string_p ());
19825 if (reader->dwo_file != NULL)
19826 attr->set_string_noncanonical (read_dwo_str_index (reader,
19827 str_index));
19828 else
19829 attr->set_string_noncanonical (read_stub_str_index (cu,
19830 str_index));
19831 break;
19832 }
19833 default:
19834 gdb_assert_not_reached (_("Unexpected DWARF form."));
19835 }
19836 }
19837
19838 /* Read an attribute value described by an attribute form. */
19839
19840 static const gdb_byte *
19841 read_attribute_value (const struct die_reader_specs *reader,
19842 struct attribute *attr, unsigned form,
19843 LONGEST implicit_const, const gdb_byte *info_ptr)
19844 {
19845 struct dwarf2_cu *cu = reader->cu;
19846 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19847 struct objfile *objfile = per_objfile->objfile;
19848 bfd *abfd = reader->abfd;
19849 struct comp_unit_head *cu_header = &cu->header;
19850 unsigned int bytes_read;
19851 struct dwarf_block *blk;
19852
19853 attr->form = (enum dwarf_form) form;
19854 switch (form)
19855 {
19856 case DW_FORM_ref_addr:
19857 if (cu_header->version == 2)
19858 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
19859 &bytes_read));
19860 else
19861 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19862 &bytes_read));
19863 info_ptr += bytes_read;
19864 break;
19865 case DW_FORM_GNU_ref_alt:
19866 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19867 &bytes_read));
19868 info_ptr += bytes_read;
19869 break;
19870 case DW_FORM_addr:
19871 {
19872 struct gdbarch *gdbarch = objfile->arch ();
19873 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
19874 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19875 attr->set_address (addr);
19876 info_ptr += bytes_read;
19877 }
19878 break;
19879 case DW_FORM_block2:
19880 blk = dwarf_alloc_block (cu);
19881 blk->size = read_2_bytes (abfd, info_ptr);
19882 info_ptr += 2;
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_block4:
19888 blk = dwarf_alloc_block (cu);
19889 blk->size = read_4_bytes (abfd, info_ptr);
19890 info_ptr += 4;
19891 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19892 info_ptr += blk->size;
19893 attr->set_block (blk);
19894 break;
19895 case DW_FORM_data2:
19896 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
19897 info_ptr += 2;
19898 break;
19899 case DW_FORM_data4:
19900 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
19901 info_ptr += 4;
19902 break;
19903 case DW_FORM_data8:
19904 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
19905 info_ptr += 8;
19906 break;
19907 case DW_FORM_data16:
19908 blk = dwarf_alloc_block (cu);
19909 blk->size = 16;
19910 blk->data = read_n_bytes (abfd, info_ptr, 16);
19911 info_ptr += 16;
19912 attr->set_block (blk);
19913 break;
19914 case DW_FORM_sec_offset:
19915 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19916 &bytes_read));
19917 info_ptr += bytes_read;
19918 break;
19919 case DW_FORM_loclistx:
19920 {
19921 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19922 &bytes_read));
19923 info_ptr += bytes_read;
19924 }
19925 break;
19926 case DW_FORM_string:
19927 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19928 &bytes_read));
19929 info_ptr += bytes_read;
19930 break;
19931 case DW_FORM_strp:
19932 if (!cu->per_cu->is_dwz)
19933 {
19934 attr->set_string_noncanonical
19935 (read_indirect_string (per_objfile,
19936 abfd, info_ptr, cu_header,
19937 &bytes_read));
19938 info_ptr += bytes_read;
19939 break;
19940 }
19941 /* FALLTHROUGH */
19942 case DW_FORM_line_strp:
19943 if (!cu->per_cu->is_dwz)
19944 {
19945 attr->set_string_noncanonical
19946 (per_objfile->read_line_string (info_ptr, cu_header,
19947 &bytes_read));
19948 info_ptr += bytes_read;
19949 break;
19950 }
19951 /* FALLTHROUGH */
19952 case DW_FORM_GNU_strp_alt:
19953 {
19954 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19955 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19956 &bytes_read);
19957
19958 attr->set_string_noncanonical
19959 (dwz->read_string (objfile, str_offset));
19960 info_ptr += bytes_read;
19961 }
19962 break;
19963 case DW_FORM_exprloc:
19964 case DW_FORM_block:
19965 blk = dwarf_alloc_block (cu);
19966 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19967 info_ptr += bytes_read;
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_block1:
19973 blk = dwarf_alloc_block (cu);
19974 blk->size = read_1_byte (abfd, info_ptr);
19975 info_ptr += 1;
19976 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19977 info_ptr += blk->size;
19978 attr->set_block (blk);
19979 break;
19980 case DW_FORM_data1:
19981 case DW_FORM_flag:
19982 attr->set_unsigned (read_1_byte (abfd, info_ptr));
19983 info_ptr += 1;
19984 break;
19985 case DW_FORM_flag_present:
19986 attr->set_unsigned (1);
19987 break;
19988 case DW_FORM_sdata:
19989 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
19990 info_ptr += bytes_read;
19991 break;
19992 case DW_FORM_rnglistx:
19993 {
19994 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19995 &bytes_read));
19996 info_ptr += bytes_read;
19997 }
19998 break;
19999 case DW_FORM_udata:
20000 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20001 info_ptr += bytes_read;
20002 break;
20003 case DW_FORM_ref1:
20004 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20005 + read_1_byte (abfd, info_ptr)));
20006 info_ptr += 1;
20007 break;
20008 case DW_FORM_ref2:
20009 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20010 + read_2_bytes (abfd, info_ptr)));
20011 info_ptr += 2;
20012 break;
20013 case DW_FORM_ref4:
20014 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20015 + read_4_bytes (abfd, info_ptr)));
20016 info_ptr += 4;
20017 break;
20018 case DW_FORM_ref8:
20019 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20020 + read_8_bytes (abfd, info_ptr)));
20021 info_ptr += 8;
20022 break;
20023 case DW_FORM_ref_sig8:
20024 attr->set_signature (read_8_bytes (abfd, info_ptr));
20025 info_ptr += 8;
20026 break;
20027 case DW_FORM_ref_udata:
20028 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20029 + read_unsigned_leb128 (abfd, info_ptr,
20030 &bytes_read)));
20031 info_ptr += bytes_read;
20032 break;
20033 case DW_FORM_indirect:
20034 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20035 info_ptr += bytes_read;
20036 if (form == DW_FORM_implicit_const)
20037 {
20038 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20039 info_ptr += bytes_read;
20040 }
20041 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20042 info_ptr);
20043 break;
20044 case DW_FORM_implicit_const:
20045 attr->set_signed (implicit_const);
20046 break;
20047 case DW_FORM_addrx:
20048 case DW_FORM_GNU_addr_index:
20049 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20050 &bytes_read));
20051 info_ptr += bytes_read;
20052 break;
20053 case DW_FORM_strx:
20054 case DW_FORM_strx1:
20055 case DW_FORM_strx2:
20056 case DW_FORM_strx3:
20057 case DW_FORM_strx4:
20058 case DW_FORM_GNU_str_index:
20059 {
20060 ULONGEST str_index;
20061 if (form == DW_FORM_strx1)
20062 {
20063 str_index = read_1_byte (abfd, info_ptr);
20064 info_ptr += 1;
20065 }
20066 else if (form == DW_FORM_strx2)
20067 {
20068 str_index = read_2_bytes (abfd, info_ptr);
20069 info_ptr += 2;
20070 }
20071 else if (form == DW_FORM_strx3)
20072 {
20073 str_index = read_3_bytes (abfd, info_ptr);
20074 info_ptr += 3;
20075 }
20076 else if (form == DW_FORM_strx4)
20077 {
20078 str_index = read_4_bytes (abfd, info_ptr);
20079 info_ptr += 4;
20080 }
20081 else
20082 {
20083 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20084 info_ptr += bytes_read;
20085 }
20086 attr->set_unsigned_reprocess (str_index);
20087 }
20088 break;
20089 default:
20090 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20091 dwarf_form_name (form),
20092 bfd_get_filename (abfd));
20093 }
20094
20095 /* Super hack. */
20096 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20097 attr->form = DW_FORM_GNU_ref_alt;
20098
20099 /* We have seen instances where the compiler tried to emit a byte
20100 size attribute of -1 which ended up being encoded as an unsigned
20101 0xffffffff. Although 0xffffffff is technically a valid size value,
20102 an object of this size seems pretty unlikely so we can relatively
20103 safely treat these cases as if the size attribute was invalid and
20104 treat them as zero by default. */
20105 if (attr->name == DW_AT_byte_size
20106 && form == DW_FORM_data4
20107 && attr->as_unsigned () >= 0xffffffff)
20108 {
20109 complaint
20110 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20111 hex_string (attr->as_unsigned ()));
20112 attr->set_unsigned (0);
20113 }
20114
20115 return info_ptr;
20116 }
20117
20118 /* Read an attribute described by an abbreviated attribute. */
20119
20120 static const gdb_byte *
20121 read_attribute (const struct die_reader_specs *reader,
20122 struct attribute *attr, const struct attr_abbrev *abbrev,
20123 const gdb_byte *info_ptr)
20124 {
20125 attr->name = abbrev->name;
20126 attr->string_is_canonical = 0;
20127 attr->requires_reprocessing = 0;
20128 return read_attribute_value (reader, attr, abbrev->form,
20129 abbrev->implicit_const, info_ptr);
20130 }
20131
20132 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20133
20134 static const char *
20135 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20136 LONGEST str_offset)
20137 {
20138 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20139 str_offset, "DW_FORM_strp");
20140 }
20141
20142 /* Return pointer to string at .debug_str offset as read from BUF.
20143 BUF is assumed to be in a compilation unit described by CU_HEADER.
20144 Return *BYTES_READ_PTR count of bytes read from BUF. */
20145
20146 static const char *
20147 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20148 const gdb_byte *buf,
20149 const struct comp_unit_head *cu_header,
20150 unsigned int *bytes_read_ptr)
20151 {
20152 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20153
20154 return read_indirect_string_at_offset (per_objfile, str_offset);
20155 }
20156
20157 /* See read.h. */
20158
20159 const char *
20160 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20161 const struct comp_unit_head *cu_header,
20162 unsigned int *bytes_read_ptr)
20163 {
20164 bfd *abfd = objfile->obfd;
20165 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20166
20167 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20168 }
20169
20170 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20171 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20172 ADDR_SIZE is the size of addresses from the CU header. */
20173
20174 static CORE_ADDR
20175 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20176 gdb::optional<ULONGEST> addr_base, int addr_size)
20177 {
20178 struct objfile *objfile = per_objfile->objfile;
20179 bfd *abfd = objfile->obfd;
20180 const gdb_byte *info_ptr;
20181 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20182
20183 per_objfile->per_bfd->addr.read (objfile);
20184 if (per_objfile->per_bfd->addr.buffer == NULL)
20185 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20186 objfile_name (objfile));
20187 if (addr_base_or_zero + addr_index * addr_size
20188 >= per_objfile->per_bfd->addr.size)
20189 error (_("DW_FORM_addr_index pointing outside of "
20190 ".debug_addr section [in module %s]"),
20191 objfile_name (objfile));
20192 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20193 + addr_index * addr_size);
20194 if (addr_size == 4)
20195 return bfd_get_32 (abfd, info_ptr);
20196 else
20197 return bfd_get_64 (abfd, info_ptr);
20198 }
20199
20200 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20201
20202 static CORE_ADDR
20203 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20204 {
20205 return read_addr_index_1 (cu->per_objfile, addr_index,
20206 cu->addr_base, cu->header.addr_size);
20207 }
20208
20209 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20210
20211 static CORE_ADDR
20212 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20213 unsigned int *bytes_read)
20214 {
20215 bfd *abfd = cu->per_objfile->objfile->obfd;
20216 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20217
20218 return read_addr_index (cu, addr_index);
20219 }
20220
20221 /* See read.h. */
20222
20223 CORE_ADDR
20224 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20225 dwarf2_per_objfile *per_objfile,
20226 unsigned int addr_index)
20227 {
20228 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20229 gdb::optional<ULONGEST> addr_base;
20230 int addr_size;
20231
20232 /* We need addr_base and addr_size.
20233 If we don't have PER_CU->cu, we have to get it.
20234 Nasty, but the alternative is storing the needed info in PER_CU,
20235 which at this point doesn't seem justified: it's not clear how frequently
20236 it would get used and it would increase the size of every PER_CU.
20237 Entry points like dwarf2_per_cu_addr_size do a similar thing
20238 so we're not in uncharted territory here.
20239 Alas we need to be a bit more complicated as addr_base is contained
20240 in the DIE.
20241
20242 We don't need to read the entire CU(/TU).
20243 We just need the header and top level die.
20244
20245 IWBN to use the aging mechanism to let us lazily later discard the CU.
20246 For now we skip this optimization. */
20247
20248 if (cu != NULL)
20249 {
20250 addr_base = cu->addr_base;
20251 addr_size = cu->header.addr_size;
20252 }
20253 else
20254 {
20255 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20256 addr_base = reader.cu->addr_base;
20257 addr_size = reader.cu->header.addr_size;
20258 }
20259
20260 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20261 }
20262
20263 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20264 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20265 DWO file. */
20266
20267 static const char *
20268 read_str_index (struct dwarf2_cu *cu,
20269 struct dwarf2_section_info *str_section,
20270 struct dwarf2_section_info *str_offsets_section,
20271 ULONGEST str_offsets_base, ULONGEST str_index)
20272 {
20273 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20274 struct objfile *objfile = per_objfile->objfile;
20275 const char *objf_name = objfile_name (objfile);
20276 bfd *abfd = objfile->obfd;
20277 const gdb_byte *info_ptr;
20278 ULONGEST str_offset;
20279 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20280
20281 str_section->read (objfile);
20282 str_offsets_section->read (objfile);
20283 if (str_section->buffer == NULL)
20284 error (_("%s used without %s section"
20285 " in CU at offset %s [in module %s]"),
20286 form_name, str_section->get_name (),
20287 sect_offset_str (cu->header.sect_off), objf_name);
20288 if (str_offsets_section->buffer == NULL)
20289 error (_("%s used without %s section"
20290 " in CU at offset %s [in module %s]"),
20291 form_name, str_section->get_name (),
20292 sect_offset_str (cu->header.sect_off), objf_name);
20293 info_ptr = (str_offsets_section->buffer
20294 + str_offsets_base
20295 + str_index * cu->header.offset_size);
20296 if (cu->header.offset_size == 4)
20297 str_offset = bfd_get_32 (abfd, info_ptr);
20298 else
20299 str_offset = bfd_get_64 (abfd, info_ptr);
20300 if (str_offset >= str_section->size)
20301 error (_("Offset from %s pointing outside of"
20302 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20303 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20304 return (const char *) (str_section->buffer + str_offset);
20305 }
20306
20307 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20308
20309 static const char *
20310 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20311 {
20312 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20313 ? reader->cu->header.addr_size : 0;
20314 return read_str_index (reader->cu,
20315 &reader->dwo_file->sections.str,
20316 &reader->dwo_file->sections.str_offsets,
20317 str_offsets_base, str_index);
20318 }
20319
20320 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20321
20322 static const char *
20323 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20324 {
20325 struct objfile *objfile = cu->per_objfile->objfile;
20326 const char *objf_name = objfile_name (objfile);
20327 static const char form_name[] = "DW_FORM_GNU_str_index";
20328 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20329
20330 if (!cu->str_offsets_base.has_value ())
20331 error (_("%s used in Fission stub without %s"
20332 " in CU at offset 0x%lx [in module %s]"),
20333 form_name, str_offsets_attr_name,
20334 (long) cu->header.offset_size, objf_name);
20335
20336 return read_str_index (cu,
20337 &cu->per_objfile->per_bfd->str,
20338 &cu->per_objfile->per_bfd->str_offsets,
20339 *cu->str_offsets_base, str_index);
20340 }
20341
20342 /* Return the length of an LEB128 number in BUF. */
20343
20344 static int
20345 leb128_size (const gdb_byte *buf)
20346 {
20347 const gdb_byte *begin = buf;
20348 gdb_byte byte;
20349
20350 while (1)
20351 {
20352 byte = *buf++;
20353 if ((byte & 128) == 0)
20354 return buf - begin;
20355 }
20356 }
20357
20358 static void
20359 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20360 {
20361 switch (lang)
20362 {
20363 case DW_LANG_C89:
20364 case DW_LANG_C99:
20365 case DW_LANG_C11:
20366 case DW_LANG_C:
20367 case DW_LANG_UPC:
20368 cu->language = language_c;
20369 break;
20370 case DW_LANG_Java:
20371 case DW_LANG_C_plus_plus:
20372 case DW_LANG_C_plus_plus_11:
20373 case DW_LANG_C_plus_plus_14:
20374 cu->language = language_cplus;
20375 break;
20376 case DW_LANG_D:
20377 cu->language = language_d;
20378 break;
20379 case DW_LANG_Fortran77:
20380 case DW_LANG_Fortran90:
20381 case DW_LANG_Fortran95:
20382 case DW_LANG_Fortran03:
20383 case DW_LANG_Fortran08:
20384 cu->language = language_fortran;
20385 break;
20386 case DW_LANG_Go:
20387 cu->language = language_go;
20388 break;
20389 case DW_LANG_Mips_Assembler:
20390 cu->language = language_asm;
20391 break;
20392 case DW_LANG_Ada83:
20393 case DW_LANG_Ada95:
20394 cu->language = language_ada;
20395 break;
20396 case DW_LANG_Modula2:
20397 cu->language = language_m2;
20398 break;
20399 case DW_LANG_Pascal83:
20400 cu->language = language_pascal;
20401 break;
20402 case DW_LANG_ObjC:
20403 cu->language = language_objc;
20404 break;
20405 case DW_LANG_Rust:
20406 case DW_LANG_Rust_old:
20407 cu->language = language_rust;
20408 break;
20409 case DW_LANG_Cobol74:
20410 case DW_LANG_Cobol85:
20411 default:
20412 cu->language = language_minimal;
20413 break;
20414 }
20415 cu->language_defn = language_def (cu->language);
20416 }
20417
20418 /* Return the named attribute or NULL if not there. */
20419
20420 static struct attribute *
20421 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20422 {
20423 for (;;)
20424 {
20425 unsigned int i;
20426 struct attribute *spec = NULL;
20427
20428 for (i = 0; i < die->num_attrs; ++i)
20429 {
20430 if (die->attrs[i].name == name)
20431 return &die->attrs[i];
20432 if (die->attrs[i].name == DW_AT_specification
20433 || die->attrs[i].name == DW_AT_abstract_origin)
20434 spec = &die->attrs[i];
20435 }
20436
20437 if (!spec)
20438 break;
20439
20440 die = follow_die_ref (die, spec, &cu);
20441 }
20442
20443 return NULL;
20444 }
20445
20446 /* Return the string associated with a string-typed attribute, or NULL if it
20447 is either not found or is of an incorrect type. */
20448
20449 static const char *
20450 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20451 {
20452 struct attribute *attr;
20453 const char *str = NULL;
20454
20455 attr = dwarf2_attr (die, name, cu);
20456
20457 if (attr != NULL)
20458 {
20459 str = attr->as_string ();
20460 if (str == nullptr)
20461 complaint (_("string type expected for attribute %s for "
20462 "DIE at %s in module %s"),
20463 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20464 objfile_name (cu->per_objfile->objfile));
20465 }
20466
20467 return str;
20468 }
20469
20470 /* Return the dwo name or NULL if not present. If present, it is in either
20471 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20472 static const char *
20473 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20474 {
20475 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20476 if (dwo_name == nullptr)
20477 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20478 return dwo_name;
20479 }
20480
20481 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20482 and holds a non-zero value. This function should only be used for
20483 DW_FORM_flag or DW_FORM_flag_present attributes. */
20484
20485 static int
20486 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20487 {
20488 struct attribute *attr = dwarf2_attr (die, name, cu);
20489
20490 return attr != nullptr && attr->as_boolean ();
20491 }
20492
20493 static int
20494 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20495 {
20496 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20497 which value is non-zero. However, we have to be careful with
20498 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20499 (via dwarf2_flag_true_p) follows this attribute. So we may
20500 end up accidently finding a declaration attribute that belongs
20501 to a different DIE referenced by the specification attribute,
20502 even though the given DIE does not have a declaration attribute. */
20503 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20504 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20505 }
20506
20507 /* Return the die giving the specification for DIE, if there is
20508 one. *SPEC_CU is the CU containing DIE on input, and the CU
20509 containing the return value on output. If there is no
20510 specification, but there is an abstract origin, that is
20511 returned. */
20512
20513 static struct die_info *
20514 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20515 {
20516 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20517 *spec_cu);
20518
20519 if (spec_attr == NULL)
20520 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20521
20522 if (spec_attr == NULL)
20523 return NULL;
20524 else
20525 return follow_die_ref (die, spec_attr, spec_cu);
20526 }
20527
20528 /* A convenience function to find the proper .debug_line section for a CU. */
20529
20530 static struct dwarf2_section_info *
20531 get_debug_line_section (struct dwarf2_cu *cu)
20532 {
20533 struct dwarf2_section_info *section;
20534 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20535
20536 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20537 DWO file. */
20538 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20539 section = &cu->dwo_unit->dwo_file->sections.line;
20540 else if (cu->per_cu->is_dwz)
20541 {
20542 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20543
20544 section = &dwz->line;
20545 }
20546 else
20547 section = &per_objfile->per_bfd->line;
20548
20549 return section;
20550 }
20551
20552 /* Read the statement program header starting at OFFSET in
20553 .debug_line, or .debug_line.dwo. Return a pointer
20554 to a struct line_header, allocated using xmalloc.
20555 Returns NULL if there is a problem reading the header, e.g., if it
20556 has a version we don't understand.
20557
20558 NOTE: the strings in the include directory and file name tables of
20559 the returned object point into the dwarf line section buffer,
20560 and must not be freed. */
20561
20562 static line_header_up
20563 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20564 {
20565 struct dwarf2_section_info *section;
20566 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20567
20568 section = get_debug_line_section (cu);
20569 section->read (per_objfile->objfile);
20570 if (section->buffer == NULL)
20571 {
20572 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20573 complaint (_("missing .debug_line.dwo section"));
20574 else
20575 complaint (_("missing .debug_line section"));
20576 return 0;
20577 }
20578
20579 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20580 per_objfile, section, &cu->header);
20581 }
20582
20583 /* Subroutine of dwarf_decode_lines to simplify it.
20584 Return the file name of the psymtab for the given file_entry.
20585 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20586 If space for the result is malloc'd, *NAME_HOLDER will be set.
20587 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20588
20589 static const char *
20590 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20591 const dwarf2_psymtab *pst,
20592 const char *comp_dir,
20593 gdb::unique_xmalloc_ptr<char> *name_holder)
20594 {
20595 const char *include_name = fe.name;
20596 const char *include_name_to_compare = include_name;
20597 const char *pst_filename;
20598 int file_is_pst;
20599
20600 const char *dir_name = fe.include_dir (lh);
20601
20602 gdb::unique_xmalloc_ptr<char> hold_compare;
20603 if (!IS_ABSOLUTE_PATH (include_name)
20604 && (dir_name != NULL || comp_dir != NULL))
20605 {
20606 /* Avoid creating a duplicate psymtab for PST.
20607 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20608 Before we do the comparison, however, we need to account
20609 for DIR_NAME and COMP_DIR.
20610 First prepend dir_name (if non-NULL). If we still don't
20611 have an absolute path prepend comp_dir (if non-NULL).
20612 However, the directory we record in the include-file's
20613 psymtab does not contain COMP_DIR (to match the
20614 corresponding symtab(s)).
20615
20616 Example:
20617
20618 bash$ cd /tmp
20619 bash$ gcc -g ./hello.c
20620 include_name = "hello.c"
20621 dir_name = "."
20622 DW_AT_comp_dir = comp_dir = "/tmp"
20623 DW_AT_name = "./hello.c"
20624
20625 */
20626
20627 if (dir_name != NULL)
20628 {
20629 name_holder->reset (concat (dir_name, SLASH_STRING,
20630 include_name, (char *) NULL));
20631 include_name = name_holder->get ();
20632 include_name_to_compare = include_name;
20633 }
20634 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20635 {
20636 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20637 include_name, (char *) NULL));
20638 include_name_to_compare = hold_compare.get ();
20639 }
20640 }
20641
20642 pst_filename = pst->filename;
20643 gdb::unique_xmalloc_ptr<char> copied_name;
20644 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20645 {
20646 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20647 pst_filename, (char *) NULL));
20648 pst_filename = copied_name.get ();
20649 }
20650
20651 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20652
20653 if (file_is_pst)
20654 return NULL;
20655 return include_name;
20656 }
20657
20658 /* State machine to track the state of the line number program. */
20659
20660 class lnp_state_machine
20661 {
20662 public:
20663 /* Initialize a machine state for the start of a line number
20664 program. */
20665 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20666 bool record_lines_p);
20667
20668 file_entry *current_file ()
20669 {
20670 /* lh->file_names is 0-based, but the file name numbers in the
20671 statement program are 1-based. */
20672 return m_line_header->file_name_at (m_file);
20673 }
20674
20675 /* Record the line in the state machine. END_SEQUENCE is true if
20676 we're processing the end of a sequence. */
20677 void record_line (bool end_sequence);
20678
20679 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20680 nop-out rest of the lines in this sequence. */
20681 void check_line_address (struct dwarf2_cu *cu,
20682 const gdb_byte *line_ptr,
20683 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20684
20685 void handle_set_discriminator (unsigned int discriminator)
20686 {
20687 m_discriminator = discriminator;
20688 m_line_has_non_zero_discriminator |= discriminator != 0;
20689 }
20690
20691 /* Handle DW_LNE_set_address. */
20692 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20693 {
20694 m_op_index = 0;
20695 address += baseaddr;
20696 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20697 }
20698
20699 /* Handle DW_LNS_advance_pc. */
20700 void handle_advance_pc (CORE_ADDR adjust);
20701
20702 /* Handle a special opcode. */
20703 void handle_special_opcode (unsigned char op_code);
20704
20705 /* Handle DW_LNS_advance_line. */
20706 void handle_advance_line (int line_delta)
20707 {
20708 advance_line (line_delta);
20709 }
20710
20711 /* Handle DW_LNS_set_file. */
20712 void handle_set_file (file_name_index file);
20713
20714 /* Handle DW_LNS_negate_stmt. */
20715 void handle_negate_stmt ()
20716 {
20717 m_is_stmt = !m_is_stmt;
20718 }
20719
20720 /* Handle DW_LNS_const_add_pc. */
20721 void handle_const_add_pc ();
20722
20723 /* Handle DW_LNS_fixed_advance_pc. */
20724 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20725 {
20726 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20727 m_op_index = 0;
20728 }
20729
20730 /* Handle DW_LNS_copy. */
20731 void handle_copy ()
20732 {
20733 record_line (false);
20734 m_discriminator = 0;
20735 }
20736
20737 /* Handle DW_LNE_end_sequence. */
20738 void handle_end_sequence ()
20739 {
20740 m_currently_recording_lines = true;
20741 }
20742
20743 private:
20744 /* Advance the line by LINE_DELTA. */
20745 void advance_line (int line_delta)
20746 {
20747 m_line += line_delta;
20748
20749 if (line_delta != 0)
20750 m_line_has_non_zero_discriminator = m_discriminator != 0;
20751 }
20752
20753 struct dwarf2_cu *m_cu;
20754
20755 gdbarch *m_gdbarch;
20756
20757 /* True if we're recording lines.
20758 Otherwise we're building partial symtabs and are just interested in
20759 finding include files mentioned by the line number program. */
20760 bool m_record_lines_p;
20761
20762 /* The line number header. */
20763 line_header *m_line_header;
20764
20765 /* These are part of the standard DWARF line number state machine,
20766 and initialized according to the DWARF spec. */
20767
20768 unsigned char m_op_index = 0;
20769 /* The line table index of the current file. */
20770 file_name_index m_file = 1;
20771 unsigned int m_line = 1;
20772
20773 /* These are initialized in the constructor. */
20774
20775 CORE_ADDR m_address;
20776 bool m_is_stmt;
20777 unsigned int m_discriminator;
20778
20779 /* Additional bits of state we need to track. */
20780
20781 /* The last file that we called dwarf2_start_subfile for.
20782 This is only used for TLLs. */
20783 unsigned int m_last_file = 0;
20784 /* The last file a line number was recorded for. */
20785 struct subfile *m_last_subfile = NULL;
20786
20787 /* The address of the last line entry. */
20788 CORE_ADDR m_last_address;
20789
20790 /* Set to true when a previous line at the same address (using
20791 m_last_address) had m_is_stmt true. This is reset to false when a
20792 line entry at a new address (m_address different to m_last_address) is
20793 processed. */
20794 bool m_stmt_at_address = false;
20795
20796 /* When true, record the lines we decode. */
20797 bool m_currently_recording_lines = false;
20798
20799 /* The last line number that was recorded, used to coalesce
20800 consecutive entries for the same line. This can happen, for
20801 example, when discriminators are present. PR 17276. */
20802 unsigned int m_last_line = 0;
20803 bool m_line_has_non_zero_discriminator = false;
20804 };
20805
20806 void
20807 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20808 {
20809 CORE_ADDR addr_adj = (((m_op_index + adjust)
20810 / m_line_header->maximum_ops_per_instruction)
20811 * m_line_header->minimum_instruction_length);
20812 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20813 m_op_index = ((m_op_index + adjust)
20814 % m_line_header->maximum_ops_per_instruction);
20815 }
20816
20817 void
20818 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20819 {
20820 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20821 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20822 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20823 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20824 / m_line_header->maximum_ops_per_instruction)
20825 * m_line_header->minimum_instruction_length);
20826 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20827 m_op_index = ((m_op_index + adj_opcode_d)
20828 % m_line_header->maximum_ops_per_instruction);
20829
20830 int line_delta = m_line_header->line_base + adj_opcode_r;
20831 advance_line (line_delta);
20832 record_line (false);
20833 m_discriminator = 0;
20834 }
20835
20836 void
20837 lnp_state_machine::handle_set_file (file_name_index file)
20838 {
20839 m_file = file;
20840
20841 const file_entry *fe = current_file ();
20842 if (fe == NULL)
20843 dwarf2_debug_line_missing_file_complaint ();
20844 else if (m_record_lines_p)
20845 {
20846 const char *dir = fe->include_dir (m_line_header);
20847
20848 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20849 m_line_has_non_zero_discriminator = m_discriminator != 0;
20850 dwarf2_start_subfile (m_cu, fe->name, dir);
20851 }
20852 }
20853
20854 void
20855 lnp_state_machine::handle_const_add_pc ()
20856 {
20857 CORE_ADDR adjust
20858 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20859
20860 CORE_ADDR addr_adj
20861 = (((m_op_index + adjust)
20862 / m_line_header->maximum_ops_per_instruction)
20863 * m_line_header->minimum_instruction_length);
20864
20865 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20866 m_op_index = ((m_op_index + adjust)
20867 % m_line_header->maximum_ops_per_instruction);
20868 }
20869
20870 /* Return non-zero if we should add LINE to the line number table.
20871 LINE is the line to add, LAST_LINE is the last line that was added,
20872 LAST_SUBFILE is the subfile for LAST_LINE.
20873 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20874 had a non-zero discriminator.
20875
20876 We have to be careful in the presence of discriminators.
20877 E.g., for this line:
20878
20879 for (i = 0; i < 100000; i++);
20880
20881 clang can emit four line number entries for that one line,
20882 each with a different discriminator.
20883 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20884
20885 However, we want gdb to coalesce all four entries into one.
20886 Otherwise the user could stepi into the middle of the line and
20887 gdb would get confused about whether the pc really was in the
20888 middle of the line.
20889
20890 Things are further complicated by the fact that two consecutive
20891 line number entries for the same line is a heuristic used by gcc
20892 to denote the end of the prologue. So we can't just discard duplicate
20893 entries, we have to be selective about it. The heuristic we use is
20894 that we only collapse consecutive entries for the same line if at least
20895 one of those entries has a non-zero discriminator. PR 17276.
20896
20897 Note: Addresses in the line number state machine can never go backwards
20898 within one sequence, thus this coalescing is ok. */
20899
20900 static int
20901 dwarf_record_line_p (struct dwarf2_cu *cu,
20902 unsigned int line, unsigned int last_line,
20903 int line_has_non_zero_discriminator,
20904 struct subfile *last_subfile)
20905 {
20906 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20907 return 1;
20908 if (line != last_line)
20909 return 1;
20910 /* Same line for the same file that we've seen already.
20911 As a last check, for pr 17276, only record the line if the line
20912 has never had a non-zero discriminator. */
20913 if (!line_has_non_zero_discriminator)
20914 return 1;
20915 return 0;
20916 }
20917
20918 /* Use the CU's builder to record line number LINE beginning at
20919 address ADDRESS in the line table of subfile SUBFILE. */
20920
20921 static void
20922 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20923 unsigned int line, CORE_ADDR address, bool is_stmt,
20924 struct dwarf2_cu *cu)
20925 {
20926 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20927
20928 if (dwarf_line_debug)
20929 {
20930 fprintf_unfiltered (gdb_stdlog,
20931 "Recording line %u, file %s, address %s\n",
20932 line, lbasename (subfile->name),
20933 paddress (gdbarch, address));
20934 }
20935
20936 if (cu != nullptr)
20937 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20938 }
20939
20940 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20941 Mark the end of a set of line number records.
20942 The arguments are the same as for dwarf_record_line_1.
20943 If SUBFILE is NULL the request is ignored. */
20944
20945 static void
20946 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20947 CORE_ADDR address, struct dwarf2_cu *cu)
20948 {
20949 if (subfile == NULL)
20950 return;
20951
20952 if (dwarf_line_debug)
20953 {
20954 fprintf_unfiltered (gdb_stdlog,
20955 "Finishing current line, file %s, address %s\n",
20956 lbasename (subfile->name),
20957 paddress (gdbarch, address));
20958 }
20959
20960 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20961 }
20962
20963 void
20964 lnp_state_machine::record_line (bool end_sequence)
20965 {
20966 if (dwarf_line_debug)
20967 {
20968 fprintf_unfiltered (gdb_stdlog,
20969 "Processing actual line %u: file %u,"
20970 " address %s, is_stmt %u, discrim %u%s\n",
20971 m_line, m_file,
20972 paddress (m_gdbarch, m_address),
20973 m_is_stmt, m_discriminator,
20974 (end_sequence ? "\t(end sequence)" : ""));
20975 }
20976
20977 file_entry *fe = current_file ();
20978
20979 if (fe == NULL)
20980 dwarf2_debug_line_missing_file_complaint ();
20981 /* For now we ignore lines not starting on an instruction boundary.
20982 But not when processing end_sequence for compatibility with the
20983 previous version of the code. */
20984 else if (m_op_index == 0 || end_sequence)
20985 {
20986 fe->included_p = 1;
20987 if (m_record_lines_p)
20988 {
20989 /* When we switch files we insert an end maker in the first file,
20990 switch to the second file and add a new line entry. The
20991 problem is that the end marker inserted in the first file will
20992 discard any previous line entries at the same address. If the
20993 line entries in the first file are marked as is-stmt, while
20994 the new line in the second file is non-stmt, then this means
20995 the end marker will discard is-stmt lines so we can have a
20996 non-stmt line. This means that there are less addresses at
20997 which the user can insert a breakpoint.
20998
20999 To improve this we track the last address in m_last_address,
21000 and whether we have seen an is-stmt at this address. Then
21001 when switching files, if we have seen a stmt at the current
21002 address, and we are switching to create a non-stmt line, then
21003 discard the new line. */
21004 bool file_changed
21005 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21006 bool ignore_this_line
21007 = ((file_changed && !end_sequence && m_last_address == m_address
21008 && !m_is_stmt && m_stmt_at_address)
21009 || (!end_sequence && m_line == 0));
21010
21011 if ((file_changed && !ignore_this_line) || end_sequence)
21012 {
21013 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21014 m_currently_recording_lines ? m_cu : nullptr);
21015 }
21016
21017 if (!end_sequence && !ignore_this_line)
21018 {
21019 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21020
21021 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21022 m_line_has_non_zero_discriminator,
21023 m_last_subfile))
21024 {
21025 buildsym_compunit *builder = m_cu->get_builder ();
21026 dwarf_record_line_1 (m_gdbarch,
21027 builder->get_current_subfile (),
21028 m_line, m_address, is_stmt,
21029 m_currently_recording_lines ? m_cu : nullptr);
21030 }
21031 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21032 m_last_line = m_line;
21033 }
21034 }
21035 }
21036
21037 /* Track whether we have seen any m_is_stmt true at m_address in case we
21038 have multiple line table entries all at m_address. */
21039 if (m_last_address != m_address)
21040 {
21041 m_stmt_at_address = false;
21042 m_last_address = m_address;
21043 }
21044 m_stmt_at_address |= m_is_stmt;
21045 }
21046
21047 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21048 line_header *lh, bool record_lines_p)
21049 {
21050 m_cu = cu;
21051 m_gdbarch = arch;
21052 m_record_lines_p = record_lines_p;
21053 m_line_header = lh;
21054
21055 m_currently_recording_lines = true;
21056
21057 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21058 was a line entry for it so that the backend has a chance to adjust it
21059 and also record it in case it needs it. This is currently used by MIPS
21060 code, cf. `mips_adjust_dwarf2_line'. */
21061 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21062 m_is_stmt = lh->default_is_stmt;
21063 m_discriminator = 0;
21064
21065 m_last_address = m_address;
21066 m_stmt_at_address = false;
21067 }
21068
21069 void
21070 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21071 const gdb_byte *line_ptr,
21072 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21073 {
21074 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21075 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21076 located at 0x0. In this case, additionally check that if
21077 ADDRESS < UNRELOCATED_LOWPC. */
21078
21079 if ((address == 0 && address < unrelocated_lowpc)
21080 || address == (CORE_ADDR) -1)
21081 {
21082 /* This line table is for a function which has been
21083 GCd by the linker. Ignore it. PR gdb/12528 */
21084
21085 struct objfile *objfile = cu->per_objfile->objfile;
21086 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21087
21088 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21089 line_offset, objfile_name (objfile));
21090 m_currently_recording_lines = false;
21091 /* Note: m_currently_recording_lines is left as false until we see
21092 DW_LNE_end_sequence. */
21093 }
21094 }
21095
21096 /* Subroutine of dwarf_decode_lines to simplify it.
21097 Process the line number information in LH.
21098 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21099 program in order to set included_p for every referenced header. */
21100
21101 static void
21102 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21103 const int decode_for_pst_p, CORE_ADDR lowpc)
21104 {
21105 const gdb_byte *line_ptr, *extended_end;
21106 const gdb_byte *line_end;
21107 unsigned int bytes_read, extended_len;
21108 unsigned char op_code, extended_op;
21109 CORE_ADDR baseaddr;
21110 struct objfile *objfile = cu->per_objfile->objfile;
21111 bfd *abfd = objfile->obfd;
21112 struct gdbarch *gdbarch = objfile->arch ();
21113 /* True if we're recording line info (as opposed to building partial
21114 symtabs and just interested in finding include files mentioned by
21115 the line number program). */
21116 bool record_lines_p = !decode_for_pst_p;
21117
21118 baseaddr = objfile->text_section_offset ();
21119
21120 line_ptr = lh->statement_program_start;
21121 line_end = lh->statement_program_end;
21122
21123 /* Read the statement sequences until there's nothing left. */
21124 while (line_ptr < line_end)
21125 {
21126 /* The DWARF line number program state machine. Reset the state
21127 machine at the start of each sequence. */
21128 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21129 bool end_sequence = false;
21130
21131 if (record_lines_p)
21132 {
21133 /* Start a subfile for the current file of the state
21134 machine. */
21135 const file_entry *fe = state_machine.current_file ();
21136
21137 if (fe != NULL)
21138 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21139 }
21140
21141 /* Decode the table. */
21142 while (line_ptr < line_end && !end_sequence)
21143 {
21144 op_code = read_1_byte (abfd, line_ptr);
21145 line_ptr += 1;
21146
21147 if (op_code >= lh->opcode_base)
21148 {
21149 /* Special opcode. */
21150 state_machine.handle_special_opcode (op_code);
21151 }
21152 else switch (op_code)
21153 {
21154 case DW_LNS_extended_op:
21155 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21156 &bytes_read);
21157 line_ptr += bytes_read;
21158 extended_end = line_ptr + extended_len;
21159 extended_op = read_1_byte (abfd, line_ptr);
21160 line_ptr += 1;
21161 if (DW_LNE_lo_user <= extended_op
21162 && extended_op <= DW_LNE_hi_user)
21163 {
21164 /* Vendor extension, ignore. */
21165 line_ptr = extended_end;
21166 break;
21167 }
21168 switch (extended_op)
21169 {
21170 case DW_LNE_end_sequence:
21171 state_machine.handle_end_sequence ();
21172 end_sequence = true;
21173 break;
21174 case DW_LNE_set_address:
21175 {
21176 CORE_ADDR address
21177 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21178 line_ptr += bytes_read;
21179
21180 state_machine.check_line_address (cu, line_ptr,
21181 lowpc - baseaddr, address);
21182 state_machine.handle_set_address (baseaddr, address);
21183 }
21184 break;
21185 case DW_LNE_define_file:
21186 {
21187 const char *cur_file;
21188 unsigned int mod_time, length;
21189 dir_index dindex;
21190
21191 cur_file = read_direct_string (abfd, line_ptr,
21192 &bytes_read);
21193 line_ptr += bytes_read;
21194 dindex = (dir_index)
21195 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21196 line_ptr += bytes_read;
21197 mod_time =
21198 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21199 line_ptr += bytes_read;
21200 length =
21201 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21202 line_ptr += bytes_read;
21203 lh->add_file_name (cur_file, dindex, mod_time, length);
21204 }
21205 break;
21206 case DW_LNE_set_discriminator:
21207 {
21208 /* The discriminator is not interesting to the
21209 debugger; just ignore it. We still need to
21210 check its value though:
21211 if there are consecutive entries for the same
21212 (non-prologue) line we want to coalesce them.
21213 PR 17276. */
21214 unsigned int discr
21215 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21216 line_ptr += bytes_read;
21217
21218 state_machine.handle_set_discriminator (discr);
21219 }
21220 break;
21221 default:
21222 complaint (_("mangled .debug_line section"));
21223 return;
21224 }
21225 /* Make sure that we parsed the extended op correctly. If e.g.
21226 we expected a different address size than the producer used,
21227 we may have read the wrong number of bytes. */
21228 if (line_ptr != extended_end)
21229 {
21230 complaint (_("mangled .debug_line section"));
21231 return;
21232 }
21233 break;
21234 case DW_LNS_copy:
21235 state_machine.handle_copy ();
21236 break;
21237 case DW_LNS_advance_pc:
21238 {
21239 CORE_ADDR adjust
21240 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21241 line_ptr += bytes_read;
21242
21243 state_machine.handle_advance_pc (adjust);
21244 }
21245 break;
21246 case DW_LNS_advance_line:
21247 {
21248 int line_delta
21249 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21250 line_ptr += bytes_read;
21251
21252 state_machine.handle_advance_line (line_delta);
21253 }
21254 break;
21255 case DW_LNS_set_file:
21256 {
21257 file_name_index file
21258 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21259 &bytes_read);
21260 line_ptr += bytes_read;
21261
21262 state_machine.handle_set_file (file);
21263 }
21264 break;
21265 case DW_LNS_set_column:
21266 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21267 line_ptr += bytes_read;
21268 break;
21269 case DW_LNS_negate_stmt:
21270 state_machine.handle_negate_stmt ();
21271 break;
21272 case DW_LNS_set_basic_block:
21273 break;
21274 /* Add to the address register of the state machine the
21275 address increment value corresponding to special opcode
21276 255. I.e., this value is scaled by the minimum
21277 instruction length since special opcode 255 would have
21278 scaled the increment. */
21279 case DW_LNS_const_add_pc:
21280 state_machine.handle_const_add_pc ();
21281 break;
21282 case DW_LNS_fixed_advance_pc:
21283 {
21284 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21285 line_ptr += 2;
21286
21287 state_machine.handle_fixed_advance_pc (addr_adj);
21288 }
21289 break;
21290 default:
21291 {
21292 /* Unknown standard opcode, ignore it. */
21293 int i;
21294
21295 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21296 {
21297 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21298 line_ptr += bytes_read;
21299 }
21300 }
21301 }
21302 }
21303
21304 if (!end_sequence)
21305 dwarf2_debug_line_missing_end_sequence_complaint ();
21306
21307 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21308 in which case we still finish recording the last line). */
21309 state_machine.record_line (true);
21310 }
21311 }
21312
21313 /* Decode the Line Number Program (LNP) for the given line_header
21314 structure and CU. The actual information extracted and the type
21315 of structures created from the LNP depends on the value of PST.
21316
21317 1. If PST is NULL, then this procedure uses the data from the program
21318 to create all necessary symbol tables, and their linetables.
21319
21320 2. If PST is not NULL, this procedure reads the program to determine
21321 the list of files included by the unit represented by PST, and
21322 builds all the associated partial symbol tables.
21323
21324 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21325 It is used for relative paths in the line table.
21326 NOTE: When processing partial symtabs (pst != NULL),
21327 comp_dir == pst->dirname.
21328
21329 NOTE: It is important that psymtabs have the same file name (via strcmp)
21330 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21331 symtab we don't use it in the name of the psymtabs we create.
21332 E.g. expand_line_sal requires this when finding psymtabs to expand.
21333 A good testcase for this is mb-inline.exp.
21334
21335 LOWPC is the lowest address in CU (or 0 if not known).
21336
21337 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21338 for its PC<->lines mapping information. Otherwise only the filename
21339 table is read in. */
21340
21341 static void
21342 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21343 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21344 CORE_ADDR lowpc, int decode_mapping)
21345 {
21346 struct objfile *objfile = cu->per_objfile->objfile;
21347 const int decode_for_pst_p = (pst != NULL);
21348
21349 if (decode_mapping)
21350 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21351
21352 if (decode_for_pst_p)
21353 {
21354 /* Now that we're done scanning the Line Header Program, we can
21355 create the psymtab of each included file. */
21356 for (auto &file_entry : lh->file_names ())
21357 if (file_entry.included_p == 1)
21358 {
21359 gdb::unique_xmalloc_ptr<char> name_holder;
21360 const char *include_name =
21361 psymtab_include_file_name (lh, file_entry, pst,
21362 comp_dir, &name_holder);
21363 if (include_name != NULL)
21364 dwarf2_create_include_psymtab
21365 (cu->per_objfile->per_bfd, include_name, pst,
21366 cu->per_objfile->per_bfd->partial_symtabs.get (),
21367 objfile->per_bfd);
21368 }
21369 }
21370 else
21371 {
21372 /* Make sure a symtab is created for every file, even files
21373 which contain only variables (i.e. no code with associated
21374 line numbers). */
21375 buildsym_compunit *builder = cu->get_builder ();
21376 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21377
21378 for (auto &fe : lh->file_names ())
21379 {
21380 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21381 if (builder->get_current_subfile ()->symtab == NULL)
21382 {
21383 builder->get_current_subfile ()->symtab
21384 = allocate_symtab (cust,
21385 builder->get_current_subfile ()->name);
21386 }
21387 fe.symtab = builder->get_current_subfile ()->symtab;
21388 }
21389 }
21390 }
21391
21392 /* Start a subfile for DWARF. FILENAME is the name of the file and
21393 DIRNAME the name of the source directory which contains FILENAME
21394 or NULL if not known.
21395 This routine tries to keep line numbers from identical absolute and
21396 relative file names in a common subfile.
21397
21398 Using the `list' example from the GDB testsuite, which resides in
21399 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21400 of /srcdir/list0.c yields the following debugging information for list0.c:
21401
21402 DW_AT_name: /srcdir/list0.c
21403 DW_AT_comp_dir: /compdir
21404 files.files[0].name: list0.h
21405 files.files[0].dir: /srcdir
21406 files.files[1].name: list0.c
21407 files.files[1].dir: /srcdir
21408
21409 The line number information for list0.c has to end up in a single
21410 subfile, so that `break /srcdir/list0.c:1' works as expected.
21411 start_subfile will ensure that this happens provided that we pass the
21412 concatenation of files.files[1].dir and files.files[1].name as the
21413 subfile's name. */
21414
21415 static void
21416 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21417 const char *dirname)
21418 {
21419 gdb::unique_xmalloc_ptr<char> copy;
21420
21421 /* In order not to lose the line information directory,
21422 we concatenate it to the filename when it makes sense.
21423 Note that the Dwarf3 standard says (speaking of filenames in line
21424 information): ``The directory index is ignored for file names
21425 that represent full path names''. Thus ignoring dirname in the
21426 `else' branch below isn't an issue. */
21427
21428 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21429 {
21430 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21431 filename = copy.get ();
21432 }
21433
21434 cu->get_builder ()->start_subfile (filename);
21435 }
21436
21437 static void
21438 var_decode_location (struct attribute *attr, struct symbol *sym,
21439 struct dwarf2_cu *cu)
21440 {
21441 struct objfile *objfile = cu->per_objfile->objfile;
21442 struct comp_unit_head *cu_header = &cu->header;
21443
21444 /* NOTE drow/2003-01-30: There used to be a comment and some special
21445 code here to turn a symbol with DW_AT_external and a
21446 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21447 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21448 with some versions of binutils) where shared libraries could have
21449 relocations against symbols in their debug information - the
21450 minimal symbol would have the right address, but the debug info
21451 would not. It's no longer necessary, because we will explicitly
21452 apply relocations when we read in the debug information now. */
21453
21454 /* A DW_AT_location attribute with no contents indicates that a
21455 variable has been optimized away. */
21456 if (attr->form_is_block () && attr->as_block ()->size == 0)
21457 {
21458 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21459 return;
21460 }
21461
21462 /* Handle one degenerate form of location expression specially, to
21463 preserve GDB's previous behavior when section offsets are
21464 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21465 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21466
21467 if (attr->form_is_block ())
21468 {
21469 struct dwarf_block *block = attr->as_block ();
21470
21471 if ((block->data[0] == DW_OP_addr
21472 && block->size == 1 + cu_header->addr_size)
21473 || ((block->data[0] == DW_OP_GNU_addr_index
21474 || block->data[0] == DW_OP_addrx)
21475 && (block->size
21476 == 1 + leb128_size (&block->data[1]))))
21477 {
21478 unsigned int dummy;
21479
21480 if (block->data[0] == DW_OP_addr)
21481 SET_SYMBOL_VALUE_ADDRESS
21482 (sym, cu->header.read_address (objfile->obfd,
21483 block->data + 1,
21484 &dummy));
21485 else
21486 SET_SYMBOL_VALUE_ADDRESS
21487 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21488 &dummy));
21489 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21490 fixup_symbol_section (sym, objfile);
21491 SET_SYMBOL_VALUE_ADDRESS
21492 (sym,
21493 SYMBOL_VALUE_ADDRESS (sym)
21494 + objfile->section_offsets[sym->section_index ()]);
21495 return;
21496 }
21497 }
21498
21499 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21500 expression evaluator, and use LOC_COMPUTED only when necessary
21501 (i.e. when the value of a register or memory location is
21502 referenced, or a thread-local block, etc.). Then again, it might
21503 not be worthwhile. I'm assuming that it isn't unless performance
21504 or memory numbers show me otherwise. */
21505
21506 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21507
21508 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21509 cu->has_loclist = true;
21510 }
21511
21512 /* Given a pointer to a DWARF information entry, figure out if we need
21513 to make a symbol table entry for it, and if so, create a new entry
21514 and return a pointer to it.
21515 If TYPE is NULL, determine symbol type from the die, otherwise
21516 used the passed type.
21517 If SPACE is not NULL, use it to hold the new symbol. If it is
21518 NULL, allocate a new symbol on the objfile's obstack. */
21519
21520 static struct symbol *
21521 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21522 struct symbol *space)
21523 {
21524 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21525 struct objfile *objfile = per_objfile->objfile;
21526 struct gdbarch *gdbarch = objfile->arch ();
21527 struct symbol *sym = NULL;
21528 const char *name;
21529 struct attribute *attr = NULL;
21530 struct attribute *attr2 = NULL;
21531 CORE_ADDR baseaddr;
21532 struct pending **list_to_add = NULL;
21533
21534 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21535
21536 baseaddr = objfile->text_section_offset ();
21537
21538 name = dwarf2_name (die, cu);
21539 if (name)
21540 {
21541 int suppress_add = 0;
21542
21543 if (space)
21544 sym = space;
21545 else
21546 sym = new (&objfile->objfile_obstack) symbol;
21547 OBJSTAT (objfile, n_syms++);
21548
21549 /* Cache this symbol's name and the name's demangled form (if any). */
21550 sym->set_language (cu->language, &objfile->objfile_obstack);
21551 /* Fortran does not have mangling standard and the mangling does differ
21552 between gfortran, iFort etc. */
21553 const char *physname
21554 = (cu->language == language_fortran
21555 ? dwarf2_full_name (name, die, cu)
21556 : dwarf2_physname (name, die, cu));
21557 const char *linkagename = dw2_linkage_name (die, cu);
21558
21559 if (linkagename == nullptr || cu->language == language_ada)
21560 sym->set_linkage_name (physname);
21561 else
21562 {
21563 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21564 sym->set_linkage_name (linkagename);
21565 }
21566
21567 /* Default assumptions.
21568 Use the passed type or decode it from the die. */
21569 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21570 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21571 if (type != NULL)
21572 SYMBOL_TYPE (sym) = type;
21573 else
21574 SYMBOL_TYPE (sym) = die_type (die, cu);
21575 attr = dwarf2_attr (die,
21576 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21577 cu);
21578 if (attr != nullptr)
21579 SYMBOL_LINE (sym) = attr->constant_value (0);
21580
21581 attr = dwarf2_attr (die,
21582 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21583 cu);
21584 if (attr != nullptr && attr->is_nonnegative ())
21585 {
21586 file_name_index file_index
21587 = (file_name_index) attr->as_nonnegative ();
21588 struct file_entry *fe;
21589
21590 if (cu->line_header != NULL)
21591 fe = cu->line_header->file_name_at (file_index);
21592 else
21593 fe = NULL;
21594
21595 if (fe == NULL)
21596 complaint (_("file index out of range"));
21597 else
21598 symbol_set_symtab (sym, fe->symtab);
21599 }
21600
21601 switch (die->tag)
21602 {
21603 case DW_TAG_label:
21604 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21605 if (attr != nullptr)
21606 {
21607 CORE_ADDR addr;
21608
21609 addr = attr->as_address ();
21610 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21611 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21612 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21613 }
21614 else
21615 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21616 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21617 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21618 add_symbol_to_list (sym, cu->list_in_scope);
21619 break;
21620 case DW_TAG_subprogram:
21621 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21622 finish_block. */
21623 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21624 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21625 if ((attr2 != nullptr && attr2->as_boolean ())
21626 || cu->language == language_ada
21627 || cu->language == language_fortran)
21628 {
21629 /* Subprograms marked external are stored as a global symbol.
21630 Ada and Fortran subprograms, whether marked external or
21631 not, are always stored as a global symbol, because we want
21632 to be able to access them globally. For instance, we want
21633 to be able to break on a nested subprogram without having
21634 to specify the context. */
21635 list_to_add = cu->get_builder ()->get_global_symbols ();
21636 }
21637 else
21638 {
21639 list_to_add = cu->list_in_scope;
21640 }
21641 break;
21642 case DW_TAG_inlined_subroutine:
21643 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21644 finish_block. */
21645 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21646 SYMBOL_INLINED (sym) = 1;
21647 list_to_add = cu->list_in_scope;
21648 break;
21649 case DW_TAG_template_value_param:
21650 suppress_add = 1;
21651 /* Fall through. */
21652 case DW_TAG_constant:
21653 case DW_TAG_variable:
21654 case DW_TAG_member:
21655 /* Compilation with minimal debug info may result in
21656 variables with missing type entries. Change the
21657 misleading `void' type to something sensible. */
21658 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21659 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21660
21661 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21662 /* In the case of DW_TAG_member, we should only be called for
21663 static const members. */
21664 if (die->tag == DW_TAG_member)
21665 {
21666 /* dwarf2_add_field uses die_is_declaration,
21667 so we do the same. */
21668 gdb_assert (die_is_declaration (die, cu));
21669 gdb_assert (attr);
21670 }
21671 if (attr != nullptr)
21672 {
21673 dwarf2_const_value (attr, sym, cu);
21674 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21675 if (!suppress_add)
21676 {
21677 if (attr2 != nullptr && attr2->as_boolean ())
21678 list_to_add = cu->get_builder ()->get_global_symbols ();
21679 else
21680 list_to_add = cu->list_in_scope;
21681 }
21682 break;
21683 }
21684 attr = dwarf2_attr (die, DW_AT_location, cu);
21685 if (attr != nullptr)
21686 {
21687 var_decode_location (attr, sym, cu);
21688 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21689
21690 /* Fortran explicitly imports any global symbols to the local
21691 scope by DW_TAG_common_block. */
21692 if (cu->language == language_fortran && die->parent
21693 && die->parent->tag == DW_TAG_common_block)
21694 attr2 = NULL;
21695
21696 if (SYMBOL_CLASS (sym) == LOC_STATIC
21697 && SYMBOL_VALUE_ADDRESS (sym) == 0
21698 && !per_objfile->per_bfd->has_section_at_zero)
21699 {
21700 /* When a static variable is eliminated by the linker,
21701 the corresponding debug information is not stripped
21702 out, but the variable address is set to null;
21703 do not add such variables into symbol table. */
21704 }
21705 else if (attr2 != nullptr && attr2->as_boolean ())
21706 {
21707 if (SYMBOL_CLASS (sym) == LOC_STATIC
21708 && (objfile->flags & OBJF_MAINLINE) == 0
21709 && per_objfile->per_bfd->can_copy)
21710 {
21711 /* A global static variable might be subject to
21712 copy relocation. We first check for a local
21713 minsym, though, because maybe the symbol was
21714 marked hidden, in which case this would not
21715 apply. */
21716 bound_minimal_symbol found
21717 = (lookup_minimal_symbol_linkage
21718 (sym->linkage_name (), objfile));
21719 if (found.minsym != nullptr)
21720 sym->maybe_copied = 1;
21721 }
21722
21723 /* A variable with DW_AT_external is never static,
21724 but it may be block-scoped. */
21725 list_to_add
21726 = ((cu->list_in_scope
21727 == cu->get_builder ()->get_file_symbols ())
21728 ? cu->get_builder ()->get_global_symbols ()
21729 : cu->list_in_scope);
21730 }
21731 else
21732 list_to_add = cu->list_in_scope;
21733 }
21734 else
21735 {
21736 /* We do not know the address of this symbol.
21737 If it is an external symbol and we have type information
21738 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21739 The address of the variable will then be determined from
21740 the minimal symbol table whenever the variable is
21741 referenced. */
21742 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21743
21744 /* Fortran explicitly imports any global symbols to the local
21745 scope by DW_TAG_common_block. */
21746 if (cu->language == language_fortran && die->parent
21747 && die->parent->tag == DW_TAG_common_block)
21748 {
21749 /* SYMBOL_CLASS doesn't matter here because
21750 read_common_block is going to reset it. */
21751 if (!suppress_add)
21752 list_to_add = cu->list_in_scope;
21753 }
21754 else if (attr2 != nullptr && attr2->as_boolean ()
21755 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21756 {
21757 /* A variable with DW_AT_external is never static, but it
21758 may be block-scoped. */
21759 list_to_add
21760 = ((cu->list_in_scope
21761 == cu->get_builder ()->get_file_symbols ())
21762 ? cu->get_builder ()->get_global_symbols ()
21763 : cu->list_in_scope);
21764
21765 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21766 }
21767 else if (!die_is_declaration (die, cu))
21768 {
21769 /* Use the default LOC_OPTIMIZED_OUT class. */
21770 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21771 if (!suppress_add)
21772 list_to_add = cu->list_in_scope;
21773 }
21774 }
21775 break;
21776 case DW_TAG_formal_parameter:
21777 {
21778 /* If we are inside a function, mark this as an argument. If
21779 not, we might be looking at an argument to an inlined function
21780 when we do not have enough information to show inlined frames;
21781 pretend it's a local variable in that case so that the user can
21782 still see it. */
21783 struct context_stack *curr
21784 = cu->get_builder ()->get_current_context_stack ();
21785 if (curr != nullptr && curr->name != nullptr)
21786 SYMBOL_IS_ARGUMENT (sym) = 1;
21787 attr = dwarf2_attr (die, DW_AT_location, cu);
21788 if (attr != nullptr)
21789 {
21790 var_decode_location (attr, sym, cu);
21791 }
21792 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21793 if (attr != nullptr)
21794 {
21795 dwarf2_const_value (attr, sym, cu);
21796 }
21797
21798 list_to_add = cu->list_in_scope;
21799 }
21800 break;
21801 case DW_TAG_unspecified_parameters:
21802 /* From varargs functions; gdb doesn't seem to have any
21803 interest in this information, so just ignore it for now.
21804 (FIXME?) */
21805 break;
21806 case DW_TAG_template_type_param:
21807 suppress_add = 1;
21808 /* Fall through. */
21809 case DW_TAG_class_type:
21810 case DW_TAG_interface_type:
21811 case DW_TAG_structure_type:
21812 case DW_TAG_union_type:
21813 case DW_TAG_set_type:
21814 case DW_TAG_enumeration_type:
21815 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21816 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21817
21818 {
21819 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21820 really ever be static objects: otherwise, if you try
21821 to, say, break of a class's method and you're in a file
21822 which doesn't mention that class, it won't work unless
21823 the check for all static symbols in lookup_symbol_aux
21824 saves you. See the OtherFileClass tests in
21825 gdb.c++/namespace.exp. */
21826
21827 if (!suppress_add)
21828 {
21829 buildsym_compunit *builder = cu->get_builder ();
21830 list_to_add
21831 = (cu->list_in_scope == builder->get_file_symbols ()
21832 && cu->language == language_cplus
21833 ? builder->get_global_symbols ()
21834 : cu->list_in_scope);
21835
21836 /* The semantics of C++ state that "struct foo {
21837 ... }" also defines a typedef for "foo". */
21838 if (cu->language == language_cplus
21839 || cu->language == language_ada
21840 || cu->language == language_d
21841 || cu->language == language_rust)
21842 {
21843 /* The symbol's name is already allocated along
21844 with this objfile, so we don't need to
21845 duplicate it for the type. */
21846 if (SYMBOL_TYPE (sym)->name () == 0)
21847 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21848 }
21849 }
21850 }
21851 break;
21852 case DW_TAG_typedef:
21853 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21854 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21855 list_to_add = cu->list_in_scope;
21856 break;
21857 case DW_TAG_array_type:
21858 case DW_TAG_base_type:
21859 case DW_TAG_subrange_type:
21860 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21861 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21862 list_to_add = cu->list_in_scope;
21863 break;
21864 case DW_TAG_enumerator:
21865 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21866 if (attr != nullptr)
21867 {
21868 dwarf2_const_value (attr, sym, cu);
21869 }
21870 {
21871 /* NOTE: carlton/2003-11-10: See comment above in the
21872 DW_TAG_class_type, etc. block. */
21873
21874 list_to_add
21875 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21876 && cu->language == language_cplus
21877 ? cu->get_builder ()->get_global_symbols ()
21878 : cu->list_in_scope);
21879 }
21880 break;
21881 case DW_TAG_imported_declaration:
21882 case DW_TAG_namespace:
21883 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21884 list_to_add = cu->get_builder ()->get_global_symbols ();
21885 break;
21886 case DW_TAG_module:
21887 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21888 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21889 list_to_add = cu->get_builder ()->get_global_symbols ();
21890 break;
21891 case DW_TAG_common_block:
21892 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21893 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21894 add_symbol_to_list (sym, cu->list_in_scope);
21895 break;
21896 default:
21897 /* Not a tag we recognize. Hopefully we aren't processing
21898 trash data, but since we must specifically ignore things
21899 we don't recognize, there is nothing else we should do at
21900 this point. */
21901 complaint (_("unsupported tag: '%s'"),
21902 dwarf_tag_name (die->tag));
21903 break;
21904 }
21905
21906 if (suppress_add)
21907 {
21908 sym->hash_next = objfile->template_symbols;
21909 objfile->template_symbols = sym;
21910 list_to_add = NULL;
21911 }
21912
21913 if (list_to_add != NULL)
21914 add_symbol_to_list (sym, list_to_add);
21915
21916 /* For the benefit of old versions of GCC, check for anonymous
21917 namespaces based on the demangled name. */
21918 if (!cu->processing_has_namespace_info
21919 && cu->language == language_cplus)
21920 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21921 }
21922 return (sym);
21923 }
21924
21925 /* Given an attr with a DW_FORM_dataN value in host byte order,
21926 zero-extend it as appropriate for the symbol's type. The DWARF
21927 standard (v4) is not entirely clear about the meaning of using
21928 DW_FORM_dataN for a constant with a signed type, where the type is
21929 wider than the data. The conclusion of a discussion on the DWARF
21930 list was that this is unspecified. We choose to always zero-extend
21931 because that is the interpretation long in use by GCC. */
21932
21933 static gdb_byte *
21934 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21935 struct dwarf2_cu *cu, LONGEST *value, int bits)
21936 {
21937 struct objfile *objfile = cu->per_objfile->objfile;
21938 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21939 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21940 LONGEST l = attr->constant_value (0);
21941
21942 if (bits < sizeof (*value) * 8)
21943 {
21944 l &= ((LONGEST) 1 << bits) - 1;
21945 *value = l;
21946 }
21947 else if (bits == sizeof (*value) * 8)
21948 *value = l;
21949 else
21950 {
21951 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21952 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21953 return bytes;
21954 }
21955
21956 return NULL;
21957 }
21958
21959 /* Read a constant value from an attribute. Either set *VALUE, or if
21960 the value does not fit in *VALUE, set *BYTES - either already
21961 allocated on the objfile obstack, or newly allocated on OBSTACK,
21962 or, set *BATON, if we translated the constant to a location
21963 expression. */
21964
21965 static void
21966 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21967 const char *name, struct obstack *obstack,
21968 struct dwarf2_cu *cu,
21969 LONGEST *value, const gdb_byte **bytes,
21970 struct dwarf2_locexpr_baton **baton)
21971 {
21972 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21973 struct objfile *objfile = per_objfile->objfile;
21974 struct comp_unit_head *cu_header = &cu->header;
21975 struct dwarf_block *blk;
21976 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21977 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21978
21979 *value = 0;
21980 *bytes = NULL;
21981 *baton = NULL;
21982
21983 switch (attr->form)
21984 {
21985 case DW_FORM_addr:
21986 case DW_FORM_addrx:
21987 case DW_FORM_GNU_addr_index:
21988 {
21989 gdb_byte *data;
21990
21991 if (TYPE_LENGTH (type) != cu_header->addr_size)
21992 dwarf2_const_value_length_mismatch_complaint (name,
21993 cu_header->addr_size,
21994 TYPE_LENGTH (type));
21995 /* Symbols of this form are reasonably rare, so we just
21996 piggyback on the existing location code rather than writing
21997 a new implementation of symbol_computed_ops. */
21998 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21999 (*baton)->per_objfile = per_objfile;
22000 (*baton)->per_cu = cu->per_cu;
22001 gdb_assert ((*baton)->per_cu);
22002
22003 (*baton)->size = 2 + cu_header->addr_size;
22004 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22005 (*baton)->data = data;
22006
22007 data[0] = DW_OP_addr;
22008 store_unsigned_integer (&data[1], cu_header->addr_size,
22009 byte_order, attr->as_address ());
22010 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22011 }
22012 break;
22013 case DW_FORM_string:
22014 case DW_FORM_strp:
22015 case DW_FORM_strx:
22016 case DW_FORM_GNU_str_index:
22017 case DW_FORM_GNU_strp_alt:
22018 /* The string is already allocated on the objfile obstack, point
22019 directly to it. */
22020 *bytes = (const gdb_byte *) attr->as_string ();
22021 break;
22022 case DW_FORM_block1:
22023 case DW_FORM_block2:
22024 case DW_FORM_block4:
22025 case DW_FORM_block:
22026 case DW_FORM_exprloc:
22027 case DW_FORM_data16:
22028 blk = attr->as_block ();
22029 if (TYPE_LENGTH (type) != blk->size)
22030 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22031 TYPE_LENGTH (type));
22032 *bytes = blk->data;
22033 break;
22034
22035 /* The DW_AT_const_value attributes are supposed to carry the
22036 symbol's value "represented as it would be on the target
22037 architecture." By the time we get here, it's already been
22038 converted to host endianness, so we just need to sign- or
22039 zero-extend it as appropriate. */
22040 case DW_FORM_data1:
22041 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22042 break;
22043 case DW_FORM_data2:
22044 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22045 break;
22046 case DW_FORM_data4:
22047 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22048 break;
22049 case DW_FORM_data8:
22050 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22051 break;
22052
22053 case DW_FORM_sdata:
22054 case DW_FORM_implicit_const:
22055 *value = attr->as_signed ();
22056 break;
22057
22058 case DW_FORM_udata:
22059 *value = attr->as_unsigned ();
22060 break;
22061
22062 default:
22063 complaint (_("unsupported const value attribute form: '%s'"),
22064 dwarf_form_name (attr->form));
22065 *value = 0;
22066 break;
22067 }
22068 }
22069
22070
22071 /* Copy constant value from an attribute to a symbol. */
22072
22073 static void
22074 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22075 struct dwarf2_cu *cu)
22076 {
22077 struct objfile *objfile = cu->per_objfile->objfile;
22078 LONGEST value;
22079 const gdb_byte *bytes;
22080 struct dwarf2_locexpr_baton *baton;
22081
22082 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22083 sym->print_name (),
22084 &objfile->objfile_obstack, cu,
22085 &value, &bytes, &baton);
22086
22087 if (baton != NULL)
22088 {
22089 SYMBOL_LOCATION_BATON (sym) = baton;
22090 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22091 }
22092 else if (bytes != NULL)
22093 {
22094 SYMBOL_VALUE_BYTES (sym) = bytes;
22095 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22096 }
22097 else
22098 {
22099 SYMBOL_VALUE (sym) = value;
22100 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22101 }
22102 }
22103
22104 /* Return the type of the die in question using its DW_AT_type attribute. */
22105
22106 static struct type *
22107 die_type (struct die_info *die, struct dwarf2_cu *cu)
22108 {
22109 struct attribute *type_attr;
22110
22111 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22112 if (!type_attr)
22113 {
22114 struct objfile *objfile = cu->per_objfile->objfile;
22115 /* A missing DW_AT_type represents a void type. */
22116 return objfile_type (objfile)->builtin_void;
22117 }
22118
22119 return lookup_die_type (die, type_attr, cu);
22120 }
22121
22122 /* True iff CU's producer generates GNAT Ada auxiliary information
22123 that allows to find parallel types through that information instead
22124 of having to do expensive parallel lookups by type name. */
22125
22126 static int
22127 need_gnat_info (struct dwarf2_cu *cu)
22128 {
22129 /* Assume that the Ada compiler was GNAT, which always produces
22130 the auxiliary information. */
22131 return (cu->language == language_ada);
22132 }
22133
22134 /* Return the auxiliary type of the die in question using its
22135 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22136 attribute is not present. */
22137
22138 static struct type *
22139 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22140 {
22141 struct attribute *type_attr;
22142
22143 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22144 if (!type_attr)
22145 return NULL;
22146
22147 return lookup_die_type (die, type_attr, cu);
22148 }
22149
22150 /* If DIE has a descriptive_type attribute, then set the TYPE's
22151 descriptive type accordingly. */
22152
22153 static void
22154 set_descriptive_type (struct type *type, struct die_info *die,
22155 struct dwarf2_cu *cu)
22156 {
22157 struct type *descriptive_type = die_descriptive_type (die, cu);
22158
22159 if (descriptive_type)
22160 {
22161 ALLOCATE_GNAT_AUX_TYPE (type);
22162 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22163 }
22164 }
22165
22166 /* Return the containing type of the die in question using its
22167 DW_AT_containing_type attribute. */
22168
22169 static struct type *
22170 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22171 {
22172 struct attribute *type_attr;
22173 struct objfile *objfile = cu->per_objfile->objfile;
22174
22175 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22176 if (!type_attr)
22177 error (_("Dwarf Error: Problem turning containing type into gdb type "
22178 "[in module %s]"), objfile_name (objfile));
22179
22180 return lookup_die_type (die, type_attr, cu);
22181 }
22182
22183 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22184
22185 static struct type *
22186 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22187 {
22188 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22189 struct objfile *objfile = per_objfile->objfile;
22190 char *saved;
22191
22192 std::string message
22193 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22194 objfile_name (objfile),
22195 sect_offset_str (cu->header.sect_off),
22196 sect_offset_str (die->sect_off));
22197 saved = obstack_strdup (&objfile->objfile_obstack, message);
22198
22199 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22200 }
22201
22202 /* Look up the type of DIE in CU using its type attribute ATTR.
22203 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22204 DW_AT_containing_type.
22205 If there is no type substitute an error marker. */
22206
22207 static struct type *
22208 lookup_die_type (struct die_info *die, const struct attribute *attr,
22209 struct dwarf2_cu *cu)
22210 {
22211 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22212 struct objfile *objfile = per_objfile->objfile;
22213 struct type *this_type;
22214
22215 gdb_assert (attr->name == DW_AT_type
22216 || attr->name == DW_AT_GNAT_descriptive_type
22217 || attr->name == DW_AT_containing_type);
22218
22219 /* First see if we have it cached. */
22220
22221 if (attr->form == DW_FORM_GNU_ref_alt)
22222 {
22223 struct dwarf2_per_cu_data *per_cu;
22224 sect_offset sect_off = attr->get_ref_die_offset ();
22225
22226 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22227 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22228 }
22229 else if (attr->form_is_ref ())
22230 {
22231 sect_offset sect_off = attr->get_ref_die_offset ();
22232
22233 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22234 }
22235 else if (attr->form == DW_FORM_ref_sig8)
22236 {
22237 ULONGEST signature = attr->as_signature ();
22238
22239 return get_signatured_type (die, signature, cu);
22240 }
22241 else
22242 {
22243 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22244 " at %s [in module %s]"),
22245 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22246 objfile_name (objfile));
22247 return build_error_marker_type (cu, die);
22248 }
22249
22250 /* If not cached we need to read it in. */
22251
22252 if (this_type == NULL)
22253 {
22254 struct die_info *type_die = NULL;
22255 struct dwarf2_cu *type_cu = cu;
22256
22257 if (attr->form_is_ref ())
22258 type_die = follow_die_ref (die, attr, &type_cu);
22259 if (type_die == NULL)
22260 return build_error_marker_type (cu, die);
22261 /* If we find the type now, it's probably because the type came
22262 from an inter-CU reference and the type's CU got expanded before
22263 ours. */
22264 this_type = read_type_die (type_die, type_cu);
22265 }
22266
22267 /* If we still don't have a type use an error marker. */
22268
22269 if (this_type == NULL)
22270 return build_error_marker_type (cu, die);
22271
22272 return this_type;
22273 }
22274
22275 /* Return the type in DIE, CU.
22276 Returns NULL for invalid types.
22277
22278 This first does a lookup in die_type_hash,
22279 and only reads the die in if necessary.
22280
22281 NOTE: This can be called when reading in partial or full symbols. */
22282
22283 static struct type *
22284 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22285 {
22286 struct type *this_type;
22287
22288 this_type = get_die_type (die, cu);
22289 if (this_type)
22290 return this_type;
22291
22292 return read_type_die_1 (die, cu);
22293 }
22294
22295 /* Read the type in DIE, CU.
22296 Returns NULL for invalid types. */
22297
22298 static struct type *
22299 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22300 {
22301 struct type *this_type = NULL;
22302
22303 switch (die->tag)
22304 {
22305 case DW_TAG_class_type:
22306 case DW_TAG_interface_type:
22307 case DW_TAG_structure_type:
22308 case DW_TAG_union_type:
22309 this_type = read_structure_type (die, cu);
22310 break;
22311 case DW_TAG_enumeration_type:
22312 this_type = read_enumeration_type (die, cu);
22313 break;
22314 case DW_TAG_subprogram:
22315 case DW_TAG_subroutine_type:
22316 case DW_TAG_inlined_subroutine:
22317 this_type = read_subroutine_type (die, cu);
22318 break;
22319 case DW_TAG_array_type:
22320 this_type = read_array_type (die, cu);
22321 break;
22322 case DW_TAG_set_type:
22323 this_type = read_set_type (die, cu);
22324 break;
22325 case DW_TAG_pointer_type:
22326 this_type = read_tag_pointer_type (die, cu);
22327 break;
22328 case DW_TAG_ptr_to_member_type:
22329 this_type = read_tag_ptr_to_member_type (die, cu);
22330 break;
22331 case DW_TAG_reference_type:
22332 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22333 break;
22334 case DW_TAG_rvalue_reference_type:
22335 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22336 break;
22337 case DW_TAG_const_type:
22338 this_type = read_tag_const_type (die, cu);
22339 break;
22340 case DW_TAG_volatile_type:
22341 this_type = read_tag_volatile_type (die, cu);
22342 break;
22343 case DW_TAG_restrict_type:
22344 this_type = read_tag_restrict_type (die, cu);
22345 break;
22346 case DW_TAG_string_type:
22347 this_type = read_tag_string_type (die, cu);
22348 break;
22349 case DW_TAG_typedef:
22350 this_type = read_typedef (die, cu);
22351 break;
22352 case DW_TAG_subrange_type:
22353 this_type = read_subrange_type (die, cu);
22354 break;
22355 case DW_TAG_base_type:
22356 this_type = read_base_type (die, cu);
22357 break;
22358 case DW_TAG_unspecified_type:
22359 this_type = read_unspecified_type (die, cu);
22360 break;
22361 case DW_TAG_namespace:
22362 this_type = read_namespace_type (die, cu);
22363 break;
22364 case DW_TAG_module:
22365 this_type = read_module_type (die, cu);
22366 break;
22367 case DW_TAG_atomic_type:
22368 this_type = read_tag_atomic_type (die, cu);
22369 break;
22370 default:
22371 complaint (_("unexpected tag in read_type_die: '%s'"),
22372 dwarf_tag_name (die->tag));
22373 break;
22374 }
22375
22376 return this_type;
22377 }
22378
22379 /* See if we can figure out if the class lives in a namespace. We do
22380 this by looking for a member function; its demangled name will
22381 contain namespace info, if there is any.
22382 Return the computed name or NULL.
22383 Space for the result is allocated on the objfile's obstack.
22384 This is the full-die version of guess_partial_die_structure_name.
22385 In this case we know DIE has no useful parent. */
22386
22387 static const char *
22388 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22389 {
22390 struct die_info *spec_die;
22391 struct dwarf2_cu *spec_cu;
22392 struct die_info *child;
22393 struct objfile *objfile = cu->per_objfile->objfile;
22394
22395 spec_cu = cu;
22396 spec_die = die_specification (die, &spec_cu);
22397 if (spec_die != NULL)
22398 {
22399 die = spec_die;
22400 cu = spec_cu;
22401 }
22402
22403 for (child = die->child;
22404 child != NULL;
22405 child = child->sibling)
22406 {
22407 if (child->tag == DW_TAG_subprogram)
22408 {
22409 const char *linkage_name = dw2_linkage_name (child, cu);
22410
22411 if (linkage_name != NULL)
22412 {
22413 gdb::unique_xmalloc_ptr<char> actual_name
22414 (cu->language_defn->class_name_from_physname (linkage_name));
22415 const char *name = NULL;
22416
22417 if (actual_name != NULL)
22418 {
22419 const char *die_name = dwarf2_name (die, cu);
22420
22421 if (die_name != NULL
22422 && strcmp (die_name, actual_name.get ()) != 0)
22423 {
22424 /* Strip off the class name from the full name.
22425 We want the prefix. */
22426 int die_name_len = strlen (die_name);
22427 int actual_name_len = strlen (actual_name.get ());
22428 const char *ptr = actual_name.get ();
22429
22430 /* Test for '::' as a sanity check. */
22431 if (actual_name_len > die_name_len + 2
22432 && ptr[actual_name_len - die_name_len - 1] == ':')
22433 name = obstack_strndup (
22434 &objfile->per_bfd->storage_obstack,
22435 ptr, actual_name_len - die_name_len - 2);
22436 }
22437 }
22438 return name;
22439 }
22440 }
22441 }
22442
22443 return NULL;
22444 }
22445
22446 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22447 prefix part in such case. See
22448 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22449
22450 static const char *
22451 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22452 {
22453 struct attribute *attr;
22454 const char *base;
22455
22456 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22457 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22458 return NULL;
22459
22460 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22461 return NULL;
22462
22463 attr = dw2_linkage_name_attr (die, cu);
22464 const char *attr_name = attr->as_string ();
22465 if (attr == NULL || attr_name == NULL)
22466 return NULL;
22467
22468 /* dwarf2_name had to be already called. */
22469 gdb_assert (attr->canonical_string_p ());
22470
22471 /* Strip the base name, keep any leading namespaces/classes. */
22472 base = strrchr (attr_name, ':');
22473 if (base == NULL || base == attr_name || base[-1] != ':')
22474 return "";
22475
22476 struct objfile *objfile = cu->per_objfile->objfile;
22477 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22478 attr_name,
22479 &base[-1] - attr_name);
22480 }
22481
22482 /* Return the name of the namespace/class that DIE is defined within,
22483 or "" if we can't tell. The caller should not xfree the result.
22484
22485 For example, if we're within the method foo() in the following
22486 code:
22487
22488 namespace N {
22489 class C {
22490 void foo () {
22491 }
22492 };
22493 }
22494
22495 then determine_prefix on foo's die will return "N::C". */
22496
22497 static const char *
22498 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22499 {
22500 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22501 struct die_info *parent, *spec_die;
22502 struct dwarf2_cu *spec_cu;
22503 struct type *parent_type;
22504 const char *retval;
22505
22506 if (cu->language != language_cplus
22507 && cu->language != language_fortran && cu->language != language_d
22508 && cu->language != language_rust)
22509 return "";
22510
22511 retval = anonymous_struct_prefix (die, cu);
22512 if (retval)
22513 return retval;
22514
22515 /* We have to be careful in the presence of DW_AT_specification.
22516 For example, with GCC 3.4, given the code
22517
22518 namespace N {
22519 void foo() {
22520 // Definition of N::foo.
22521 }
22522 }
22523
22524 then we'll have a tree of DIEs like this:
22525
22526 1: DW_TAG_compile_unit
22527 2: DW_TAG_namespace // N
22528 3: DW_TAG_subprogram // declaration of N::foo
22529 4: DW_TAG_subprogram // definition of N::foo
22530 DW_AT_specification // refers to die #3
22531
22532 Thus, when processing die #4, we have to pretend that we're in
22533 the context of its DW_AT_specification, namely the contex of die
22534 #3. */
22535 spec_cu = cu;
22536 spec_die = die_specification (die, &spec_cu);
22537 if (spec_die == NULL)
22538 parent = die->parent;
22539 else
22540 {
22541 parent = spec_die->parent;
22542 cu = spec_cu;
22543 }
22544
22545 if (parent == NULL)
22546 return "";
22547 else if (parent->building_fullname)
22548 {
22549 const char *name;
22550 const char *parent_name;
22551
22552 /* It has been seen on RealView 2.2 built binaries,
22553 DW_TAG_template_type_param types actually _defined_ as
22554 children of the parent class:
22555
22556 enum E {};
22557 template class <class Enum> Class{};
22558 Class<enum E> class_e;
22559
22560 1: DW_TAG_class_type (Class)
22561 2: DW_TAG_enumeration_type (E)
22562 3: DW_TAG_enumerator (enum1:0)
22563 3: DW_TAG_enumerator (enum2:1)
22564 ...
22565 2: DW_TAG_template_type_param
22566 DW_AT_type DW_FORM_ref_udata (E)
22567
22568 Besides being broken debug info, it can put GDB into an
22569 infinite loop. Consider:
22570
22571 When we're building the full name for Class<E>, we'll start
22572 at Class, and go look over its template type parameters,
22573 finding E. We'll then try to build the full name of E, and
22574 reach here. We're now trying to build the full name of E,
22575 and look over the parent DIE for containing scope. In the
22576 broken case, if we followed the parent DIE of E, we'd again
22577 find Class, and once again go look at its template type
22578 arguments, etc., etc. Simply don't consider such parent die
22579 as source-level parent of this die (it can't be, the language
22580 doesn't allow it), and break the loop here. */
22581 name = dwarf2_name (die, cu);
22582 parent_name = dwarf2_name (parent, cu);
22583 complaint (_("template param type '%s' defined within parent '%s'"),
22584 name ? name : "<unknown>",
22585 parent_name ? parent_name : "<unknown>");
22586 return "";
22587 }
22588 else
22589 switch (parent->tag)
22590 {
22591 case DW_TAG_namespace:
22592 parent_type = read_type_die (parent, cu);
22593 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22594 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22595 Work around this problem here. */
22596 if (cu->language == language_cplus
22597 && strcmp (parent_type->name (), "::") == 0)
22598 return "";
22599 /* We give a name to even anonymous namespaces. */
22600 return parent_type->name ();
22601 case DW_TAG_class_type:
22602 case DW_TAG_interface_type:
22603 case DW_TAG_structure_type:
22604 case DW_TAG_union_type:
22605 case DW_TAG_module:
22606 parent_type = read_type_die (parent, cu);
22607 if (parent_type->name () != NULL)
22608 return parent_type->name ();
22609 else
22610 /* An anonymous structure is only allowed non-static data
22611 members; no typedefs, no member functions, et cetera.
22612 So it does not need a prefix. */
22613 return "";
22614 case DW_TAG_compile_unit:
22615 case DW_TAG_partial_unit:
22616 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22617 if (cu->language == language_cplus
22618 && !per_objfile->per_bfd->types.empty ()
22619 && die->child != NULL
22620 && (die->tag == DW_TAG_class_type
22621 || die->tag == DW_TAG_structure_type
22622 || die->tag == DW_TAG_union_type))
22623 {
22624 const char *name = guess_full_die_structure_name (die, cu);
22625 if (name != NULL)
22626 return name;
22627 }
22628 return "";
22629 case DW_TAG_subprogram:
22630 /* Nested subroutines in Fortran get a prefix with the name
22631 of the parent's subroutine. */
22632 if (cu->language == language_fortran)
22633 {
22634 if ((die->tag == DW_TAG_subprogram)
22635 && (dwarf2_name (parent, cu) != NULL))
22636 return dwarf2_name (parent, cu);
22637 }
22638 return determine_prefix (parent, cu);
22639 case DW_TAG_enumeration_type:
22640 parent_type = read_type_die (parent, cu);
22641 if (parent_type->is_declared_class ())
22642 {
22643 if (parent_type->name () != NULL)
22644 return parent_type->name ();
22645 return "";
22646 }
22647 /* Fall through. */
22648 default:
22649 return determine_prefix (parent, cu);
22650 }
22651 }
22652
22653 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22654 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22655 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22656 an obconcat, otherwise allocate storage for the result. The CU argument is
22657 used to determine the language and hence, the appropriate separator. */
22658
22659 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22660
22661 static char *
22662 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22663 int physname, struct dwarf2_cu *cu)
22664 {
22665 const char *lead = "";
22666 const char *sep;
22667
22668 if (suffix == NULL || suffix[0] == '\0'
22669 || prefix == NULL || prefix[0] == '\0')
22670 sep = "";
22671 else if (cu->language == language_d)
22672 {
22673 /* For D, the 'main' function could be defined in any module, but it
22674 should never be prefixed. */
22675 if (strcmp (suffix, "D main") == 0)
22676 {
22677 prefix = "";
22678 sep = "";
22679 }
22680 else
22681 sep = ".";
22682 }
22683 else if (cu->language == language_fortran && physname)
22684 {
22685 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22686 DW_AT_MIPS_linkage_name is preferred and used instead. */
22687
22688 lead = "__";
22689 sep = "_MOD_";
22690 }
22691 else
22692 sep = "::";
22693
22694 if (prefix == NULL)
22695 prefix = "";
22696 if (suffix == NULL)
22697 suffix = "";
22698
22699 if (obs == NULL)
22700 {
22701 char *retval
22702 = ((char *)
22703 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22704
22705 strcpy (retval, lead);
22706 strcat (retval, prefix);
22707 strcat (retval, sep);
22708 strcat (retval, suffix);
22709 return retval;
22710 }
22711 else
22712 {
22713 /* We have an obstack. */
22714 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22715 }
22716 }
22717
22718 /* Get name of a die, return NULL if not found. */
22719
22720 static const char *
22721 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22722 struct objfile *objfile)
22723 {
22724 if (name && cu->language == language_cplus)
22725 {
22726 gdb::unique_xmalloc_ptr<char> canon_name
22727 = cp_canonicalize_string (name);
22728
22729 if (canon_name != nullptr)
22730 name = objfile->intern (canon_name.get ());
22731 }
22732
22733 return name;
22734 }
22735
22736 /* Get name of a die, return NULL if not found.
22737 Anonymous namespaces are converted to their magic string. */
22738
22739 static const char *
22740 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22741 {
22742 struct attribute *attr;
22743 struct objfile *objfile = cu->per_objfile->objfile;
22744
22745 attr = dwarf2_attr (die, DW_AT_name, cu);
22746 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22747 if (attr_name == nullptr
22748 && die->tag != DW_TAG_namespace
22749 && die->tag != DW_TAG_class_type
22750 && die->tag != DW_TAG_interface_type
22751 && die->tag != DW_TAG_structure_type
22752 && die->tag != DW_TAG_union_type)
22753 return NULL;
22754
22755 switch (die->tag)
22756 {
22757 case DW_TAG_compile_unit:
22758 case DW_TAG_partial_unit:
22759 /* Compilation units have a DW_AT_name that is a filename, not
22760 a source language identifier. */
22761 case DW_TAG_enumeration_type:
22762 case DW_TAG_enumerator:
22763 /* These tags always have simple identifiers already; no need
22764 to canonicalize them. */
22765 return attr_name;
22766
22767 case DW_TAG_namespace:
22768 if (attr_name != nullptr)
22769 return attr_name;
22770 return CP_ANONYMOUS_NAMESPACE_STR;
22771
22772 case DW_TAG_class_type:
22773 case DW_TAG_interface_type:
22774 case DW_TAG_structure_type:
22775 case DW_TAG_union_type:
22776 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22777 structures or unions. These were of the form "._%d" in GCC 4.1,
22778 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22779 and GCC 4.4. We work around this problem by ignoring these. */
22780 if (attr_name != nullptr
22781 && (startswith (attr_name, "._")
22782 || startswith (attr_name, "<anonymous")))
22783 return NULL;
22784
22785 /* GCC might emit a nameless typedef that has a linkage name. See
22786 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22787 if (!attr || attr_name == NULL)
22788 {
22789 attr = dw2_linkage_name_attr (die, cu);
22790 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22791 if (attr == NULL || attr_name == NULL)
22792 return NULL;
22793
22794 /* Avoid demangling attr_name the second time on a second
22795 call for the same DIE. */
22796 if (!attr->canonical_string_p ())
22797 {
22798 gdb::unique_xmalloc_ptr<char> demangled
22799 (gdb_demangle (attr_name, DMGL_TYPES));
22800 if (demangled == nullptr)
22801 return nullptr;
22802
22803 attr->set_string_canonical (objfile->intern (demangled.get ()));
22804 attr_name = attr->as_string ();
22805 }
22806
22807 /* Strip any leading namespaces/classes, keep only the
22808 base name. DW_AT_name for named DIEs does not
22809 contain the prefixes. */
22810 const char *base = strrchr (attr_name, ':');
22811 if (base && base > attr_name && base[-1] == ':')
22812 return &base[1];
22813 else
22814 return attr_name;
22815 }
22816 break;
22817
22818 default:
22819 break;
22820 }
22821
22822 if (!attr->canonical_string_p ())
22823 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22824 objfile));
22825 return attr->as_string ();
22826 }
22827
22828 /* Return the die that this die in an extension of, or NULL if there
22829 is none. *EXT_CU is the CU containing DIE on input, and the CU
22830 containing the return value on output. */
22831
22832 static struct die_info *
22833 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22834 {
22835 struct attribute *attr;
22836
22837 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22838 if (attr == NULL)
22839 return NULL;
22840
22841 return follow_die_ref (die, attr, ext_cu);
22842 }
22843
22844 static void
22845 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22846 {
22847 unsigned int i;
22848
22849 print_spaces (indent, f);
22850 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22851 dwarf_tag_name (die->tag), die->abbrev,
22852 sect_offset_str (die->sect_off));
22853
22854 if (die->parent != NULL)
22855 {
22856 print_spaces (indent, f);
22857 fprintf_unfiltered (f, " parent at offset: %s\n",
22858 sect_offset_str (die->parent->sect_off));
22859 }
22860
22861 print_spaces (indent, f);
22862 fprintf_unfiltered (f, " has children: %s\n",
22863 dwarf_bool_name (die->child != NULL));
22864
22865 print_spaces (indent, f);
22866 fprintf_unfiltered (f, " attributes:\n");
22867
22868 for (i = 0; i < die->num_attrs; ++i)
22869 {
22870 print_spaces (indent, f);
22871 fprintf_unfiltered (f, " %s (%s) ",
22872 dwarf_attr_name (die->attrs[i].name),
22873 dwarf_form_name (die->attrs[i].form));
22874
22875 switch (die->attrs[i].form)
22876 {
22877 case DW_FORM_addr:
22878 case DW_FORM_addrx:
22879 case DW_FORM_GNU_addr_index:
22880 fprintf_unfiltered (f, "address: ");
22881 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
22882 break;
22883 case DW_FORM_block2:
22884 case DW_FORM_block4:
22885 case DW_FORM_block:
22886 case DW_FORM_block1:
22887 fprintf_unfiltered (f, "block: size %s",
22888 pulongest (die->attrs[i].as_block ()->size));
22889 break;
22890 case DW_FORM_exprloc:
22891 fprintf_unfiltered (f, "expression: size %s",
22892 pulongest (die->attrs[i].as_block ()->size));
22893 break;
22894 case DW_FORM_data16:
22895 fprintf_unfiltered (f, "constant of 16 bytes");
22896 break;
22897 case DW_FORM_ref_addr:
22898 fprintf_unfiltered (f, "ref address: ");
22899 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
22900 break;
22901 case DW_FORM_GNU_ref_alt:
22902 fprintf_unfiltered (f, "alt ref address: ");
22903 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
22904 break;
22905 case DW_FORM_ref1:
22906 case DW_FORM_ref2:
22907 case DW_FORM_ref4:
22908 case DW_FORM_ref8:
22909 case DW_FORM_ref_udata:
22910 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22911 (long) (die->attrs[i].as_unsigned ()));
22912 break;
22913 case DW_FORM_data1:
22914 case DW_FORM_data2:
22915 case DW_FORM_data4:
22916 case DW_FORM_data8:
22917 case DW_FORM_udata:
22918 fprintf_unfiltered (f, "constant: %s",
22919 pulongest (die->attrs[i].as_unsigned ()));
22920 break;
22921 case DW_FORM_sec_offset:
22922 fprintf_unfiltered (f, "section offset: %s",
22923 pulongest (die->attrs[i].as_unsigned ()));
22924 break;
22925 case DW_FORM_ref_sig8:
22926 fprintf_unfiltered (f, "signature: %s",
22927 hex_string (die->attrs[i].as_signature ()));
22928 break;
22929 case DW_FORM_string:
22930 case DW_FORM_strp:
22931 case DW_FORM_line_strp:
22932 case DW_FORM_strx:
22933 case DW_FORM_GNU_str_index:
22934 case DW_FORM_GNU_strp_alt:
22935 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22936 die->attrs[i].as_string ()
22937 ? die->attrs[i].as_string () : "",
22938 die->attrs[i].canonical_string_p () ? "is" : "not");
22939 break;
22940 case DW_FORM_flag:
22941 if (die->attrs[i].as_boolean ())
22942 fprintf_unfiltered (f, "flag: TRUE");
22943 else
22944 fprintf_unfiltered (f, "flag: FALSE");
22945 break;
22946 case DW_FORM_flag_present:
22947 fprintf_unfiltered (f, "flag: TRUE");
22948 break;
22949 case DW_FORM_indirect:
22950 /* The reader will have reduced the indirect form to
22951 the "base form" so this form should not occur. */
22952 fprintf_unfiltered (f,
22953 "unexpected attribute form: DW_FORM_indirect");
22954 break;
22955 case DW_FORM_sdata:
22956 case DW_FORM_implicit_const:
22957 fprintf_unfiltered (f, "constant: %s",
22958 plongest (die->attrs[i].as_signed ()));
22959 break;
22960 default:
22961 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22962 die->attrs[i].form);
22963 break;
22964 }
22965 fprintf_unfiltered (f, "\n");
22966 }
22967 }
22968
22969 static void
22970 dump_die_for_error (struct die_info *die)
22971 {
22972 dump_die_shallow (gdb_stderr, 0, die);
22973 }
22974
22975 static void
22976 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22977 {
22978 int indent = level * 4;
22979
22980 gdb_assert (die != NULL);
22981
22982 if (level >= max_level)
22983 return;
22984
22985 dump_die_shallow (f, indent, die);
22986
22987 if (die->child != NULL)
22988 {
22989 print_spaces (indent, f);
22990 fprintf_unfiltered (f, " Children:");
22991 if (level + 1 < max_level)
22992 {
22993 fprintf_unfiltered (f, "\n");
22994 dump_die_1 (f, level + 1, max_level, die->child);
22995 }
22996 else
22997 {
22998 fprintf_unfiltered (f,
22999 " [not printed, max nesting level reached]\n");
23000 }
23001 }
23002
23003 if (die->sibling != NULL && level > 0)
23004 {
23005 dump_die_1 (f, level, max_level, die->sibling);
23006 }
23007 }
23008
23009 /* This is called from the pdie macro in gdbinit.in.
23010 It's not static so gcc will keep a copy callable from gdb. */
23011
23012 void
23013 dump_die (struct die_info *die, int max_level)
23014 {
23015 dump_die_1 (gdb_stdlog, 0, max_level, die);
23016 }
23017
23018 static void
23019 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23020 {
23021 void **slot;
23022
23023 slot = htab_find_slot_with_hash (cu->die_hash, die,
23024 to_underlying (die->sect_off),
23025 INSERT);
23026
23027 *slot = die;
23028 }
23029
23030 /* Follow reference or signature attribute ATTR of SRC_DIE.
23031 On entry *REF_CU is the CU of SRC_DIE.
23032 On exit *REF_CU is the CU of the result. */
23033
23034 static struct die_info *
23035 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23036 struct dwarf2_cu **ref_cu)
23037 {
23038 struct die_info *die;
23039
23040 if (attr->form_is_ref ())
23041 die = follow_die_ref (src_die, attr, ref_cu);
23042 else if (attr->form == DW_FORM_ref_sig8)
23043 die = follow_die_sig (src_die, attr, ref_cu);
23044 else
23045 {
23046 dump_die_for_error (src_die);
23047 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23048 objfile_name ((*ref_cu)->per_objfile->objfile));
23049 }
23050
23051 return die;
23052 }
23053
23054 /* Follow reference OFFSET.
23055 On entry *REF_CU is the CU of the source die referencing OFFSET.
23056 On exit *REF_CU is the CU of the result.
23057 Returns NULL if OFFSET is invalid. */
23058
23059 static struct die_info *
23060 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23061 struct dwarf2_cu **ref_cu)
23062 {
23063 struct die_info temp_die;
23064 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23065 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23066
23067 gdb_assert (cu->per_cu != NULL);
23068
23069 target_cu = cu;
23070
23071 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23072 "source CU contains target offset: %d",
23073 sect_offset_str (cu->per_cu->sect_off),
23074 sect_offset_str (sect_off),
23075 cu->header.offset_in_cu_p (sect_off));
23076
23077 if (cu->per_cu->is_debug_types)
23078 {
23079 /* .debug_types CUs cannot reference anything outside their CU.
23080 If they need to, they have to reference a signatured type via
23081 DW_FORM_ref_sig8. */
23082 if (!cu->header.offset_in_cu_p (sect_off))
23083 return NULL;
23084 }
23085 else if (offset_in_dwz != cu->per_cu->is_dwz
23086 || !cu->header.offset_in_cu_p (sect_off))
23087 {
23088 struct dwarf2_per_cu_data *per_cu;
23089
23090 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23091 per_objfile);
23092
23093 dwarf_read_debug_printf_v ("target CU offset: %s, "
23094 "target CU DIEs loaded: %d",
23095 sect_offset_str (per_cu->sect_off),
23096 per_objfile->get_cu (per_cu) != nullptr);
23097
23098 /* If necessary, add it to the queue and load its DIEs.
23099
23100 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23101 it doesn't mean they are currently loaded. Since we require them
23102 to be loaded, we must check for ourselves. */
23103 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23104 || per_objfile->get_cu (per_cu) == nullptr)
23105 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23106 false, cu->language);
23107
23108 target_cu = per_objfile->get_cu (per_cu);
23109 gdb_assert (target_cu != nullptr);
23110 }
23111 else if (cu->dies == NULL)
23112 {
23113 /* We're loading full DIEs during partial symbol reading. */
23114 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23115 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23116 language_minimal);
23117 }
23118
23119 *ref_cu = target_cu;
23120 temp_die.sect_off = sect_off;
23121
23122 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23123 &temp_die,
23124 to_underlying (sect_off));
23125 }
23126
23127 /* Follow reference attribute ATTR of SRC_DIE.
23128 On entry *REF_CU is the CU of SRC_DIE.
23129 On exit *REF_CU is the CU of the result. */
23130
23131 static struct die_info *
23132 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23133 struct dwarf2_cu **ref_cu)
23134 {
23135 sect_offset sect_off = attr->get_ref_die_offset ();
23136 struct dwarf2_cu *cu = *ref_cu;
23137 struct die_info *die;
23138
23139 die = follow_die_offset (sect_off,
23140 (attr->form == DW_FORM_GNU_ref_alt
23141 || cu->per_cu->is_dwz),
23142 ref_cu);
23143 if (!die)
23144 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23145 "at %s [in module %s]"),
23146 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23147 objfile_name (cu->per_objfile->objfile));
23148
23149 return die;
23150 }
23151
23152 /* See read.h. */
23153
23154 struct dwarf2_locexpr_baton
23155 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23156 dwarf2_per_cu_data *per_cu,
23157 dwarf2_per_objfile *per_objfile,
23158 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23159 bool resolve_abstract_p)
23160 {
23161 struct die_info *die;
23162 struct attribute *attr;
23163 struct dwarf2_locexpr_baton retval;
23164 struct objfile *objfile = per_objfile->objfile;
23165
23166 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23167 if (cu == nullptr)
23168 cu = load_cu (per_cu, per_objfile, false);
23169
23170 if (cu == nullptr)
23171 {
23172 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23173 Instead just throw an error, not much else we can do. */
23174 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23175 sect_offset_str (sect_off), objfile_name (objfile));
23176 }
23177
23178 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23179 if (!die)
23180 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23181 sect_offset_str (sect_off), objfile_name (objfile));
23182
23183 attr = dwarf2_attr (die, DW_AT_location, cu);
23184 if (!attr && resolve_abstract_p
23185 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23186 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23187 {
23188 CORE_ADDR pc = get_frame_pc ();
23189 CORE_ADDR baseaddr = objfile->text_section_offset ();
23190 struct gdbarch *gdbarch = objfile->arch ();
23191
23192 for (const auto &cand_off
23193 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23194 {
23195 struct dwarf2_cu *cand_cu = cu;
23196 struct die_info *cand
23197 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23198 if (!cand
23199 || !cand->parent
23200 || cand->parent->tag != DW_TAG_subprogram)
23201 continue;
23202
23203 CORE_ADDR pc_low, pc_high;
23204 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23205 if (pc_low == ((CORE_ADDR) -1))
23206 continue;
23207 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23208 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23209 if (!(pc_low <= pc && pc < pc_high))
23210 continue;
23211
23212 die = cand;
23213 attr = dwarf2_attr (die, DW_AT_location, cu);
23214 break;
23215 }
23216 }
23217
23218 if (!attr)
23219 {
23220 /* DWARF: "If there is no such attribute, then there is no effect.".
23221 DATA is ignored if SIZE is 0. */
23222
23223 retval.data = NULL;
23224 retval.size = 0;
23225 }
23226 else if (attr->form_is_section_offset ())
23227 {
23228 struct dwarf2_loclist_baton loclist_baton;
23229 CORE_ADDR pc = get_frame_pc ();
23230 size_t size;
23231
23232 fill_in_loclist_baton (cu, &loclist_baton, attr);
23233
23234 retval.data = dwarf2_find_location_expression (&loclist_baton,
23235 &size, pc);
23236 retval.size = size;
23237 }
23238 else
23239 {
23240 if (!attr->form_is_block ())
23241 error (_("Dwarf Error: DIE at %s referenced in module %s "
23242 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23243 sect_offset_str (sect_off), objfile_name (objfile));
23244
23245 struct dwarf_block *block = attr->as_block ();
23246 retval.data = block->data;
23247 retval.size = block->size;
23248 }
23249 retval.per_objfile = per_objfile;
23250 retval.per_cu = cu->per_cu;
23251
23252 per_objfile->age_comp_units ();
23253
23254 return retval;
23255 }
23256
23257 /* See read.h. */
23258
23259 struct dwarf2_locexpr_baton
23260 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23261 dwarf2_per_cu_data *per_cu,
23262 dwarf2_per_objfile *per_objfile,
23263 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23264 {
23265 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23266
23267 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23268 get_frame_pc);
23269 }
23270
23271 /* Write a constant of a given type as target-ordered bytes into
23272 OBSTACK. */
23273
23274 static const gdb_byte *
23275 write_constant_as_bytes (struct obstack *obstack,
23276 enum bfd_endian byte_order,
23277 struct type *type,
23278 ULONGEST value,
23279 LONGEST *len)
23280 {
23281 gdb_byte *result;
23282
23283 *len = TYPE_LENGTH (type);
23284 result = (gdb_byte *) obstack_alloc (obstack, *len);
23285 store_unsigned_integer (result, *len, byte_order, value);
23286
23287 return result;
23288 }
23289
23290 /* See read.h. */
23291
23292 const gdb_byte *
23293 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23294 dwarf2_per_cu_data *per_cu,
23295 dwarf2_per_objfile *per_objfile,
23296 obstack *obstack,
23297 LONGEST *len)
23298 {
23299 struct die_info *die;
23300 struct attribute *attr;
23301 const gdb_byte *result = NULL;
23302 struct type *type;
23303 LONGEST value;
23304 enum bfd_endian byte_order;
23305 struct objfile *objfile = per_objfile->objfile;
23306
23307 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23308 if (cu == nullptr)
23309 cu = load_cu (per_cu, per_objfile, false);
23310
23311 if (cu == nullptr)
23312 {
23313 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23314 Instead just throw an error, not much else we can do. */
23315 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23316 sect_offset_str (sect_off), objfile_name (objfile));
23317 }
23318
23319 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23320 if (!die)
23321 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23322 sect_offset_str (sect_off), objfile_name (objfile));
23323
23324 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23325 if (attr == NULL)
23326 return NULL;
23327
23328 byte_order = (bfd_big_endian (objfile->obfd)
23329 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23330
23331 switch (attr->form)
23332 {
23333 case DW_FORM_addr:
23334 case DW_FORM_addrx:
23335 case DW_FORM_GNU_addr_index:
23336 {
23337 gdb_byte *tem;
23338
23339 *len = cu->header.addr_size;
23340 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23341 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23342 result = tem;
23343 }
23344 break;
23345 case DW_FORM_string:
23346 case DW_FORM_strp:
23347 case DW_FORM_strx:
23348 case DW_FORM_GNU_str_index:
23349 case DW_FORM_GNU_strp_alt:
23350 /* The string is already allocated on the objfile obstack, point
23351 directly to it. */
23352 {
23353 const char *attr_name = attr->as_string ();
23354 result = (const gdb_byte *) attr_name;
23355 *len = strlen (attr_name);
23356 }
23357 break;
23358 case DW_FORM_block1:
23359 case DW_FORM_block2:
23360 case DW_FORM_block4:
23361 case DW_FORM_block:
23362 case DW_FORM_exprloc:
23363 case DW_FORM_data16:
23364 {
23365 struct dwarf_block *block = attr->as_block ();
23366 result = block->data;
23367 *len = block->size;
23368 }
23369 break;
23370
23371 /* The DW_AT_const_value attributes are supposed to carry the
23372 symbol's value "represented as it would be on the target
23373 architecture." By the time we get here, it's already been
23374 converted to host endianness, so we just need to sign- or
23375 zero-extend it as appropriate. */
23376 case DW_FORM_data1:
23377 type = die_type (die, cu);
23378 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23379 if (result == NULL)
23380 result = write_constant_as_bytes (obstack, byte_order,
23381 type, value, len);
23382 break;
23383 case DW_FORM_data2:
23384 type = die_type (die, cu);
23385 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23386 if (result == NULL)
23387 result = write_constant_as_bytes (obstack, byte_order,
23388 type, value, len);
23389 break;
23390 case DW_FORM_data4:
23391 type = die_type (die, cu);
23392 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23393 if (result == NULL)
23394 result = write_constant_as_bytes (obstack, byte_order,
23395 type, value, len);
23396 break;
23397 case DW_FORM_data8:
23398 type = die_type (die, cu);
23399 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23400 if (result == NULL)
23401 result = write_constant_as_bytes (obstack, byte_order,
23402 type, value, len);
23403 break;
23404
23405 case DW_FORM_sdata:
23406 case DW_FORM_implicit_const:
23407 type = die_type (die, cu);
23408 result = write_constant_as_bytes (obstack, byte_order,
23409 type, attr->as_signed (), len);
23410 break;
23411
23412 case DW_FORM_udata:
23413 type = die_type (die, cu);
23414 result = write_constant_as_bytes (obstack, byte_order,
23415 type, attr->as_unsigned (), len);
23416 break;
23417
23418 default:
23419 complaint (_("unsupported const value attribute form: '%s'"),
23420 dwarf_form_name (attr->form));
23421 break;
23422 }
23423
23424 return result;
23425 }
23426
23427 /* See read.h. */
23428
23429 struct type *
23430 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23431 dwarf2_per_cu_data *per_cu,
23432 dwarf2_per_objfile *per_objfile,
23433 const char **var_name)
23434 {
23435 struct die_info *die;
23436
23437 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23438 if (cu == nullptr)
23439 cu = load_cu (per_cu, per_objfile, false);
23440
23441 if (cu == nullptr)
23442 return nullptr;
23443
23444 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23445 if (!die)
23446 return NULL;
23447
23448 if (var_name != nullptr)
23449 *var_name = var_decl_name (die, cu);
23450 return die_type (die, cu);
23451 }
23452
23453 /* See read.h. */
23454
23455 struct type *
23456 dwarf2_get_die_type (cu_offset die_offset,
23457 dwarf2_per_cu_data *per_cu,
23458 dwarf2_per_objfile *per_objfile)
23459 {
23460 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23461 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23462 }
23463
23464 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23465 On entry *REF_CU is the CU of SRC_DIE.
23466 On exit *REF_CU is the CU of the result.
23467 Returns NULL if the referenced DIE isn't found. */
23468
23469 static struct die_info *
23470 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23471 struct dwarf2_cu **ref_cu)
23472 {
23473 struct die_info temp_die;
23474 struct dwarf2_cu *sig_cu;
23475 struct die_info *die;
23476 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23477
23478
23479 /* While it might be nice to assert sig_type->type == NULL here,
23480 we can get here for DW_AT_imported_declaration where we need
23481 the DIE not the type. */
23482
23483 /* If necessary, add it to the queue and load its DIEs.
23484
23485 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23486 it doesn't mean they are currently loaded. Since we require them
23487 to be loaded, we must check for ourselves. */
23488 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23489 language_minimal)
23490 || per_objfile->get_cu (sig_type) == nullptr)
23491 read_signatured_type (sig_type, per_objfile);
23492
23493 sig_cu = per_objfile->get_cu (sig_type);
23494 gdb_assert (sig_cu != NULL);
23495 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23496 temp_die.sect_off = sig_type->type_offset_in_section;
23497 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23498 to_underlying (temp_die.sect_off));
23499 if (die)
23500 {
23501 /* For .gdb_index version 7 keep track of included TUs.
23502 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23503 if (per_objfile->per_bfd->index_table != NULL
23504 && per_objfile->per_bfd->index_table->version <= 7)
23505 {
23506 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23507 }
23508
23509 *ref_cu = sig_cu;
23510 return die;
23511 }
23512
23513 return NULL;
23514 }
23515
23516 /* Follow signatured type referenced by ATTR in SRC_DIE.
23517 On entry *REF_CU is the CU of SRC_DIE.
23518 On exit *REF_CU is the CU of the result.
23519 The result is the DIE of the type.
23520 If the referenced type cannot be found an error is thrown. */
23521
23522 static struct die_info *
23523 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23524 struct dwarf2_cu **ref_cu)
23525 {
23526 ULONGEST signature = attr->as_signature ();
23527 struct signatured_type *sig_type;
23528 struct die_info *die;
23529
23530 gdb_assert (attr->form == DW_FORM_ref_sig8);
23531
23532 sig_type = lookup_signatured_type (*ref_cu, signature);
23533 /* sig_type will be NULL if the signatured type is missing from
23534 the debug info. */
23535 if (sig_type == NULL)
23536 {
23537 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23538 " from DIE at %s [in module %s]"),
23539 hex_string (signature), sect_offset_str (src_die->sect_off),
23540 objfile_name ((*ref_cu)->per_objfile->objfile));
23541 }
23542
23543 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23544 if (die == NULL)
23545 {
23546 dump_die_for_error (src_die);
23547 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23548 " from DIE at %s [in module %s]"),
23549 hex_string (signature), sect_offset_str (src_die->sect_off),
23550 objfile_name ((*ref_cu)->per_objfile->objfile));
23551 }
23552
23553 return die;
23554 }
23555
23556 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23557 reading in and processing the type unit if necessary. */
23558
23559 static struct type *
23560 get_signatured_type (struct die_info *die, ULONGEST signature,
23561 struct dwarf2_cu *cu)
23562 {
23563 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23564 struct signatured_type *sig_type;
23565 struct dwarf2_cu *type_cu;
23566 struct die_info *type_die;
23567 struct type *type;
23568
23569 sig_type = lookup_signatured_type (cu, signature);
23570 /* sig_type will be NULL if the signatured type is missing from
23571 the debug info. */
23572 if (sig_type == NULL)
23573 {
23574 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23575 " from DIE at %s [in module %s]"),
23576 hex_string (signature), sect_offset_str (die->sect_off),
23577 objfile_name (per_objfile->objfile));
23578 return build_error_marker_type (cu, die);
23579 }
23580
23581 /* If we already know the type we're done. */
23582 type = per_objfile->get_type_for_signatured_type (sig_type);
23583 if (type != nullptr)
23584 return type;
23585
23586 type_cu = cu;
23587 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23588 if (type_die != NULL)
23589 {
23590 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23591 is created. This is important, for example, because for c++ classes
23592 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23593 type = read_type_die (type_die, type_cu);
23594 if (type == NULL)
23595 {
23596 complaint (_("Dwarf Error: Cannot build signatured type %s"
23597 " referenced from DIE at %s [in module %s]"),
23598 hex_string (signature), sect_offset_str (die->sect_off),
23599 objfile_name (per_objfile->objfile));
23600 type = build_error_marker_type (cu, die);
23601 }
23602 }
23603 else
23604 {
23605 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23606 " from DIE at %s [in module %s]"),
23607 hex_string (signature), sect_offset_str (die->sect_off),
23608 objfile_name (per_objfile->objfile));
23609 type = build_error_marker_type (cu, die);
23610 }
23611
23612 per_objfile->set_type_for_signatured_type (sig_type, type);
23613
23614 return type;
23615 }
23616
23617 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23618 reading in and processing the type unit if necessary. */
23619
23620 static struct type *
23621 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23622 struct dwarf2_cu *cu) /* ARI: editCase function */
23623 {
23624 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23625 if (attr->form_is_ref ())
23626 {
23627 struct dwarf2_cu *type_cu = cu;
23628 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23629
23630 return read_type_die (type_die, type_cu);
23631 }
23632 else if (attr->form == DW_FORM_ref_sig8)
23633 {
23634 return get_signatured_type (die, attr->as_signature (), cu);
23635 }
23636 else
23637 {
23638 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23639
23640 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23641 " at %s [in module %s]"),
23642 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23643 objfile_name (per_objfile->objfile));
23644 return build_error_marker_type (cu, die);
23645 }
23646 }
23647
23648 /* Load the DIEs associated with type unit PER_CU into memory. */
23649
23650 static void
23651 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23652 dwarf2_per_objfile *per_objfile)
23653 {
23654 struct signatured_type *sig_type;
23655
23656 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23657 gdb_assert (! per_cu->type_unit_group_p ());
23658
23659 /* We have the per_cu, but we need the signatured_type.
23660 Fortunately this is an easy translation. */
23661 gdb_assert (per_cu->is_debug_types);
23662 sig_type = (struct signatured_type *) per_cu;
23663
23664 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23665
23666 read_signatured_type (sig_type, per_objfile);
23667
23668 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23669 }
23670
23671 /* Read in a signatured type and build its CU and DIEs.
23672 If the type is a stub for the real type in a DWO file,
23673 read in the real type from the DWO file as well. */
23674
23675 static void
23676 read_signatured_type (signatured_type *sig_type,
23677 dwarf2_per_objfile *per_objfile)
23678 {
23679 gdb_assert (sig_type->is_debug_types);
23680 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23681
23682 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23683
23684 if (!reader.dummy_p)
23685 {
23686 struct dwarf2_cu *cu = reader.cu;
23687 const gdb_byte *info_ptr = reader.info_ptr;
23688
23689 gdb_assert (cu->die_hash == NULL);
23690 cu->die_hash =
23691 htab_create_alloc_ex (cu->header.length / 12,
23692 die_hash,
23693 die_eq,
23694 NULL,
23695 &cu->comp_unit_obstack,
23696 hashtab_obstack_allocate,
23697 dummy_obstack_deallocate);
23698
23699 if (reader.comp_unit_die->has_children)
23700 reader.comp_unit_die->child
23701 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23702 reader.comp_unit_die);
23703 cu->dies = reader.comp_unit_die;
23704 /* comp_unit_die is not stored in die_hash, no need. */
23705
23706 /* We try not to read any attributes in this function, because
23707 not all CUs needed for references have been loaded yet, and
23708 symbol table processing isn't initialized. But we have to
23709 set the CU language, or we won't be able to build types
23710 correctly. Similarly, if we do not read the producer, we can
23711 not apply producer-specific interpretation. */
23712 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23713
23714 reader.keep ();
23715 }
23716
23717 sig_type->tu_read = 1;
23718 }
23719
23720 /* Decode simple location descriptions.
23721 Given a pointer to a dwarf block that defines a location, compute
23722 the location and return the value. If COMPUTED is non-null, it is
23723 set to true to indicate that decoding was successful, and false
23724 otherwise. If COMPUTED is null, then this function may emit a
23725 complaint. */
23726
23727 static CORE_ADDR
23728 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23729 {
23730 struct objfile *objfile = cu->per_objfile->objfile;
23731 size_t i;
23732 size_t size = blk->size;
23733 const gdb_byte *data = blk->data;
23734 CORE_ADDR stack[64];
23735 int stacki;
23736 unsigned int bytes_read, unsnd;
23737 gdb_byte op;
23738
23739 if (computed != nullptr)
23740 *computed = false;
23741
23742 i = 0;
23743 stacki = 0;
23744 stack[stacki] = 0;
23745 stack[++stacki] = 0;
23746
23747 while (i < size)
23748 {
23749 op = data[i++];
23750 switch (op)
23751 {
23752 case DW_OP_lit0:
23753 case DW_OP_lit1:
23754 case DW_OP_lit2:
23755 case DW_OP_lit3:
23756 case DW_OP_lit4:
23757 case DW_OP_lit5:
23758 case DW_OP_lit6:
23759 case DW_OP_lit7:
23760 case DW_OP_lit8:
23761 case DW_OP_lit9:
23762 case DW_OP_lit10:
23763 case DW_OP_lit11:
23764 case DW_OP_lit12:
23765 case DW_OP_lit13:
23766 case DW_OP_lit14:
23767 case DW_OP_lit15:
23768 case DW_OP_lit16:
23769 case DW_OP_lit17:
23770 case DW_OP_lit18:
23771 case DW_OP_lit19:
23772 case DW_OP_lit20:
23773 case DW_OP_lit21:
23774 case DW_OP_lit22:
23775 case DW_OP_lit23:
23776 case DW_OP_lit24:
23777 case DW_OP_lit25:
23778 case DW_OP_lit26:
23779 case DW_OP_lit27:
23780 case DW_OP_lit28:
23781 case DW_OP_lit29:
23782 case DW_OP_lit30:
23783 case DW_OP_lit31:
23784 stack[++stacki] = op - DW_OP_lit0;
23785 break;
23786
23787 case DW_OP_reg0:
23788 case DW_OP_reg1:
23789 case DW_OP_reg2:
23790 case DW_OP_reg3:
23791 case DW_OP_reg4:
23792 case DW_OP_reg5:
23793 case DW_OP_reg6:
23794 case DW_OP_reg7:
23795 case DW_OP_reg8:
23796 case DW_OP_reg9:
23797 case DW_OP_reg10:
23798 case DW_OP_reg11:
23799 case DW_OP_reg12:
23800 case DW_OP_reg13:
23801 case DW_OP_reg14:
23802 case DW_OP_reg15:
23803 case DW_OP_reg16:
23804 case DW_OP_reg17:
23805 case DW_OP_reg18:
23806 case DW_OP_reg19:
23807 case DW_OP_reg20:
23808 case DW_OP_reg21:
23809 case DW_OP_reg22:
23810 case DW_OP_reg23:
23811 case DW_OP_reg24:
23812 case DW_OP_reg25:
23813 case DW_OP_reg26:
23814 case DW_OP_reg27:
23815 case DW_OP_reg28:
23816 case DW_OP_reg29:
23817 case DW_OP_reg30:
23818 case DW_OP_reg31:
23819 stack[++stacki] = op - DW_OP_reg0;
23820 if (i < size)
23821 {
23822 if (computed == nullptr)
23823 dwarf2_complex_location_expr_complaint ();
23824 else
23825 return 0;
23826 }
23827 break;
23828
23829 case DW_OP_regx:
23830 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23831 i += bytes_read;
23832 stack[++stacki] = unsnd;
23833 if (i < size)
23834 {
23835 if (computed == nullptr)
23836 dwarf2_complex_location_expr_complaint ();
23837 else
23838 return 0;
23839 }
23840 break;
23841
23842 case DW_OP_addr:
23843 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23844 &bytes_read);
23845 i += bytes_read;
23846 break;
23847
23848 case DW_OP_const1u:
23849 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23850 i += 1;
23851 break;
23852
23853 case DW_OP_const1s:
23854 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23855 i += 1;
23856 break;
23857
23858 case DW_OP_const2u:
23859 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23860 i += 2;
23861 break;
23862
23863 case DW_OP_const2s:
23864 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23865 i += 2;
23866 break;
23867
23868 case DW_OP_const4u:
23869 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23870 i += 4;
23871 break;
23872
23873 case DW_OP_const4s:
23874 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23875 i += 4;
23876 break;
23877
23878 case DW_OP_const8u:
23879 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23880 i += 8;
23881 break;
23882
23883 case DW_OP_constu:
23884 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23885 &bytes_read);
23886 i += bytes_read;
23887 break;
23888
23889 case DW_OP_consts:
23890 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23891 i += bytes_read;
23892 break;
23893
23894 case DW_OP_dup:
23895 stack[stacki + 1] = stack[stacki];
23896 stacki++;
23897 break;
23898
23899 case DW_OP_plus:
23900 stack[stacki - 1] += stack[stacki];
23901 stacki--;
23902 break;
23903
23904 case DW_OP_plus_uconst:
23905 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23906 &bytes_read);
23907 i += bytes_read;
23908 break;
23909
23910 case DW_OP_minus:
23911 stack[stacki - 1] -= stack[stacki];
23912 stacki--;
23913 break;
23914
23915 case DW_OP_deref:
23916 /* If we're not the last op, then we definitely can't encode
23917 this using GDB's address_class enum. This is valid for partial
23918 global symbols, although the variable's address will be bogus
23919 in the psymtab. */
23920 if (i < size)
23921 {
23922 if (computed == nullptr)
23923 dwarf2_complex_location_expr_complaint ();
23924 else
23925 return 0;
23926 }
23927 break;
23928
23929 case DW_OP_GNU_push_tls_address:
23930 case DW_OP_form_tls_address:
23931 /* The top of the stack has the offset from the beginning
23932 of the thread control block at which the variable is located. */
23933 /* Nothing should follow this operator, so the top of stack would
23934 be returned. */
23935 /* This is valid for partial global symbols, but the variable's
23936 address will be bogus in the psymtab. Make it always at least
23937 non-zero to not look as a variable garbage collected by linker
23938 which have DW_OP_addr 0. */
23939 if (i < size)
23940 {
23941 if (computed == nullptr)
23942 dwarf2_complex_location_expr_complaint ();
23943 else
23944 return 0;
23945 }
23946 stack[stacki]++;
23947 break;
23948
23949 case DW_OP_GNU_uninit:
23950 if (computed != nullptr)
23951 return 0;
23952 break;
23953
23954 case DW_OP_addrx:
23955 case DW_OP_GNU_addr_index:
23956 case DW_OP_GNU_const_index:
23957 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23958 &bytes_read);
23959 i += bytes_read;
23960 break;
23961
23962 default:
23963 if (computed == nullptr)
23964 {
23965 const char *name = get_DW_OP_name (op);
23966
23967 if (name)
23968 complaint (_("unsupported stack op: '%s'"),
23969 name);
23970 else
23971 complaint (_("unsupported stack op: '%02x'"),
23972 op);
23973 }
23974
23975 return (stack[stacki]);
23976 }
23977
23978 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23979 outside of the allocated space. Also enforce minimum>0. */
23980 if (stacki >= ARRAY_SIZE (stack) - 1)
23981 {
23982 if (computed == nullptr)
23983 complaint (_("location description stack overflow"));
23984 return 0;
23985 }
23986
23987 if (stacki <= 0)
23988 {
23989 if (computed == nullptr)
23990 complaint (_("location description stack underflow"));
23991 return 0;
23992 }
23993 }
23994
23995 if (computed != nullptr)
23996 *computed = true;
23997 return (stack[stacki]);
23998 }
23999
24000 /* memory allocation interface */
24001
24002 static struct dwarf_block *
24003 dwarf_alloc_block (struct dwarf2_cu *cu)
24004 {
24005 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24006 }
24007
24008 static struct die_info *
24009 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24010 {
24011 struct die_info *die;
24012 size_t size = sizeof (struct die_info);
24013
24014 if (num_attrs > 1)
24015 size += (num_attrs - 1) * sizeof (struct attribute);
24016
24017 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24018 memset (die, 0, sizeof (struct die_info));
24019 return (die);
24020 }
24021
24022 \f
24023
24024 /* Macro support. */
24025
24026 /* An overload of dwarf_decode_macros that finds the correct section
24027 and ensures it is read in before calling the other overload. */
24028
24029 static void
24030 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24031 int section_is_gnu)
24032 {
24033 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24034 struct objfile *objfile = per_objfile->objfile;
24035 const struct line_header *lh = cu->line_header;
24036 unsigned int offset_size = cu->header.offset_size;
24037 struct dwarf2_section_info *section;
24038 const char *section_name;
24039
24040 if (cu->dwo_unit != nullptr)
24041 {
24042 if (section_is_gnu)
24043 {
24044 section = &cu->dwo_unit->dwo_file->sections.macro;
24045 section_name = ".debug_macro.dwo";
24046 }
24047 else
24048 {
24049 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24050 section_name = ".debug_macinfo.dwo";
24051 }
24052 }
24053 else
24054 {
24055 if (section_is_gnu)
24056 {
24057 section = &per_objfile->per_bfd->macro;
24058 section_name = ".debug_macro";
24059 }
24060 else
24061 {
24062 section = &per_objfile->per_bfd->macinfo;
24063 section_name = ".debug_macinfo";
24064 }
24065 }
24066
24067 section->read (objfile);
24068 if (section->buffer == nullptr)
24069 {
24070 complaint (_("missing %s section"), section_name);
24071 return;
24072 }
24073
24074 buildsym_compunit *builder = cu->get_builder ();
24075
24076 struct dwarf2_section_info *str_offsets_section;
24077 struct dwarf2_section_info *str_section;
24078 ULONGEST str_offsets_base;
24079
24080 if (cu->dwo_unit != nullptr)
24081 {
24082 str_offsets_section = &cu->dwo_unit->dwo_file
24083 ->sections.str_offsets;
24084 str_section = &cu->dwo_unit->dwo_file->sections.str;
24085 str_offsets_base = cu->header.addr_size;
24086 }
24087 else
24088 {
24089 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24090 str_section = &per_objfile->per_bfd->str;
24091 str_offsets_base = *cu->str_offsets_base;
24092 }
24093
24094 dwarf_decode_macros (per_objfile, builder, section, lh,
24095 offset_size, offset, str_section, str_offsets_section,
24096 str_offsets_base, section_is_gnu);
24097 }
24098
24099 /* Return the .debug_loc section to use for CU.
24100 For DWO files use .debug_loc.dwo. */
24101
24102 static struct dwarf2_section_info *
24103 cu_debug_loc_section (struct dwarf2_cu *cu)
24104 {
24105 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24106
24107 if (cu->dwo_unit)
24108 {
24109 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24110
24111 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24112 }
24113 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24114 : &per_objfile->per_bfd->loc);
24115 }
24116
24117 /* Return the .debug_rnglists section to use for CU. */
24118 static struct dwarf2_section_info *
24119 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24120 {
24121 if (cu->header.version < 5)
24122 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24123 cu->header.version);
24124 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24125
24126 /* Make sure we read the .debug_rnglists section from the file that
24127 contains the DW_AT_ranges attribute we are reading. Normally that
24128 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24129 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24130 program. */
24131 if (cu->dwo_unit != nullptr
24132 && tag != DW_TAG_compile_unit
24133 && tag != DW_TAG_skeleton_unit)
24134 {
24135 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24136
24137 if (sections->rnglists.size > 0)
24138 return &sections->rnglists;
24139 else
24140 error (_(".debug_rnglists section is missing from .dwo file."));
24141 }
24142 return &dwarf2_per_objfile->per_bfd->rnglists;
24143 }
24144
24145 /* A helper function that fills in a dwarf2_loclist_baton. */
24146
24147 static void
24148 fill_in_loclist_baton (struct dwarf2_cu *cu,
24149 struct dwarf2_loclist_baton *baton,
24150 const struct attribute *attr)
24151 {
24152 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24153 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24154
24155 section->read (per_objfile->objfile);
24156
24157 baton->per_objfile = per_objfile;
24158 baton->per_cu = cu->per_cu;
24159 gdb_assert (baton->per_cu);
24160 /* We don't know how long the location list is, but make sure we
24161 don't run off the edge of the section. */
24162 baton->size = section->size - attr->as_unsigned ();
24163 baton->data = section->buffer + attr->as_unsigned ();
24164 if (cu->base_address.has_value ())
24165 baton->base_address = *cu->base_address;
24166 else
24167 baton->base_address = 0;
24168 baton->from_dwo = cu->dwo_unit != NULL;
24169 }
24170
24171 static void
24172 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24173 struct dwarf2_cu *cu, int is_block)
24174 {
24175 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24176 struct objfile *objfile = per_objfile->objfile;
24177 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24178
24179 if (attr->form_is_section_offset ()
24180 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24181 the section. If so, fall through to the complaint in the
24182 other branch. */
24183 && attr->as_unsigned () < section->get_size (objfile))
24184 {
24185 struct dwarf2_loclist_baton *baton;
24186
24187 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24188
24189 fill_in_loclist_baton (cu, baton, attr);
24190
24191 if (!cu->base_address.has_value ())
24192 complaint (_("Location list used without "
24193 "specifying the CU base address."));
24194
24195 SYMBOL_ACLASS_INDEX (sym) = (is_block
24196 ? dwarf2_loclist_block_index
24197 : dwarf2_loclist_index);
24198 SYMBOL_LOCATION_BATON (sym) = baton;
24199 }
24200 else
24201 {
24202 struct dwarf2_locexpr_baton *baton;
24203
24204 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24205 baton->per_objfile = per_objfile;
24206 baton->per_cu = cu->per_cu;
24207 gdb_assert (baton->per_cu);
24208
24209 if (attr->form_is_block ())
24210 {
24211 /* Note that we're just copying the block's data pointer
24212 here, not the actual data. We're still pointing into the
24213 info_buffer for SYM's objfile; right now we never release
24214 that buffer, but when we do clean up properly this may
24215 need to change. */
24216 struct dwarf_block *block = attr->as_block ();
24217 baton->size = block->size;
24218 baton->data = block->data;
24219 }
24220 else
24221 {
24222 dwarf2_invalid_attrib_class_complaint ("location description",
24223 sym->natural_name ());
24224 baton->size = 0;
24225 }
24226
24227 SYMBOL_ACLASS_INDEX (sym) = (is_block
24228 ? dwarf2_locexpr_block_index
24229 : dwarf2_locexpr_index);
24230 SYMBOL_LOCATION_BATON (sym) = baton;
24231 }
24232 }
24233
24234 /* See read.h. */
24235
24236 const comp_unit_head *
24237 dwarf2_per_cu_data::get_header () const
24238 {
24239 if (!m_header_read_in)
24240 {
24241 const gdb_byte *info_ptr
24242 = this->section->buffer + to_underlying (this->sect_off);
24243
24244 memset (&m_header, 0, sizeof (m_header));
24245
24246 read_comp_unit_head (&m_header, info_ptr, this->section,
24247 rcuh_kind::COMPILE);
24248
24249 m_header_read_in = true;
24250 }
24251
24252 return &m_header;
24253 }
24254
24255 /* See read.h. */
24256
24257 int
24258 dwarf2_per_cu_data::addr_size () const
24259 {
24260 return this->get_header ()->addr_size;
24261 }
24262
24263 /* See read.h. */
24264
24265 int
24266 dwarf2_per_cu_data::offset_size () const
24267 {
24268 return this->get_header ()->offset_size;
24269 }
24270
24271 /* See read.h. */
24272
24273 int
24274 dwarf2_per_cu_data::ref_addr_size () const
24275 {
24276 const comp_unit_head *header = this->get_header ();
24277
24278 if (header->version == 2)
24279 return header->addr_size;
24280 else
24281 return header->offset_size;
24282 }
24283
24284 /* A helper function for dwarf2_find_containing_comp_unit that returns
24285 the index of the result, and that searches a vector. It will
24286 return a result even if the offset in question does not actually
24287 occur in any CU. This is separate so that it can be unit
24288 tested. */
24289
24290 static int
24291 dwarf2_find_containing_comp_unit
24292 (sect_offset sect_off,
24293 unsigned int offset_in_dwz,
24294 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
24295 {
24296 int low, high;
24297
24298 low = 0;
24299 high = all_comp_units.size () - 1;
24300 while (high > low)
24301 {
24302 struct dwarf2_per_cu_data *mid_cu;
24303 int mid = low + (high - low) / 2;
24304
24305 mid_cu = all_comp_units[mid].get ();
24306 if (mid_cu->is_dwz > offset_in_dwz
24307 || (mid_cu->is_dwz == offset_in_dwz
24308 && mid_cu->sect_off + mid_cu->length > sect_off))
24309 high = mid;
24310 else
24311 low = mid + 1;
24312 }
24313 gdb_assert (low == high);
24314 return low;
24315 }
24316
24317 /* Locate the .debug_info compilation unit from CU's objfile which contains
24318 the DIE at OFFSET. Raises an error on failure. */
24319
24320 static struct dwarf2_per_cu_data *
24321 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24322 unsigned int offset_in_dwz,
24323 dwarf2_per_objfile *per_objfile)
24324 {
24325 int low = dwarf2_find_containing_comp_unit
24326 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24327 dwarf2_per_cu_data *this_cu
24328 = per_objfile->per_bfd->all_comp_units[low].get ();
24329
24330 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24331 {
24332 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24333 error (_("Dwarf Error: could not find partial DIE containing "
24334 "offset %s [in module %s]"),
24335 sect_offset_str (sect_off),
24336 bfd_get_filename (per_objfile->objfile->obfd));
24337
24338 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24339 <= sect_off);
24340 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
24341 }
24342 else
24343 {
24344 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24345 && sect_off >= this_cu->sect_off + this_cu->length)
24346 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24347 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24348 return this_cu;
24349 }
24350 }
24351
24352 #if GDB_SELF_TEST
24353
24354 namespace selftests {
24355 namespace find_containing_comp_unit {
24356
24357 static void
24358 run_test ()
24359 {
24360 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
24361 dwarf2_per_cu_data *one_ptr = one.get ();
24362 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
24363 dwarf2_per_cu_data *two_ptr = two.get ();
24364 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
24365 dwarf2_per_cu_data *three_ptr = three.get ();
24366 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
24367 dwarf2_per_cu_data *four_ptr = four.get ();
24368
24369 one->length = 5;
24370 two->sect_off = sect_offset (one->length);
24371 two->length = 7;
24372
24373 three->length = 5;
24374 three->is_dwz = 1;
24375 four->sect_off = sect_offset (three->length);
24376 four->length = 7;
24377 four->is_dwz = 1;
24378
24379 std::vector<dwarf2_per_cu_data_up> units;
24380 units.push_back (std::move (one));
24381 units.push_back (std::move (two));
24382 units.push_back (std::move (three));
24383 units.push_back (std::move (four));
24384
24385 int result;
24386
24387 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24388 SELF_CHECK (units[result].get () == one_ptr);
24389 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24390 SELF_CHECK (units[result].get () == one_ptr);
24391 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24392 SELF_CHECK (units[result].get () == two_ptr);
24393
24394 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24395 SELF_CHECK (units[result].get () == three_ptr);
24396 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24397 SELF_CHECK (units[result].get () == three_ptr);
24398 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24399 SELF_CHECK (units[result].get () == four_ptr);
24400 }
24401
24402 }
24403 }
24404
24405 #endif /* GDB_SELF_TEST */
24406
24407 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24408
24409 static void
24410 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24411 enum language pretend_language)
24412 {
24413 struct attribute *attr;
24414
24415 /* Set the language we're debugging. */
24416 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24417 if (attr != nullptr)
24418 set_cu_language (attr->constant_value (0), cu);
24419 else
24420 {
24421 cu->language = pretend_language;
24422 cu->language_defn = language_def (cu->language);
24423 }
24424
24425 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24426 }
24427
24428 /* See read.h. */
24429
24430 dwarf2_cu *
24431 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24432 {
24433 auto it = m_dwarf2_cus.find (per_cu);
24434 if (it == m_dwarf2_cus.end ())
24435 return nullptr;
24436
24437 return it->second;
24438 }
24439
24440 /* See read.h. */
24441
24442 void
24443 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24444 {
24445 gdb_assert (this->get_cu (per_cu) == nullptr);
24446
24447 m_dwarf2_cus[per_cu] = cu;
24448 }
24449
24450 /* See read.h. */
24451
24452 void
24453 dwarf2_per_objfile::age_comp_units ()
24454 {
24455 dwarf_read_debug_printf_v ("running");
24456
24457 /* This is not expected to be called in the middle of CU expansion. There is
24458 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24459 loaded in memory. Calling age_comp_units while the queue is in use could
24460 make us free the DIEs for a CU that is in the queue and therefore break
24461 that invariant. */
24462 gdb_assert (!this->per_bfd->queue.has_value ());
24463
24464 /* Start by clearing all marks. */
24465 for (auto pair : m_dwarf2_cus)
24466 pair.second->clear_mark ();
24467
24468 /* Traverse all CUs, mark them and their dependencies if used recently
24469 enough. */
24470 for (auto pair : m_dwarf2_cus)
24471 {
24472 dwarf2_cu *cu = pair.second;
24473
24474 cu->last_used++;
24475 if (cu->last_used <= dwarf_max_cache_age)
24476 cu->mark ();
24477 }
24478
24479 /* Delete all CUs still not marked. */
24480 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24481 {
24482 dwarf2_cu *cu = it->second;
24483
24484 if (!cu->is_marked ())
24485 {
24486 dwarf_read_debug_printf_v ("deleting old CU %s",
24487 sect_offset_str (cu->per_cu->sect_off));
24488 delete cu;
24489 it = m_dwarf2_cus.erase (it);
24490 }
24491 else
24492 it++;
24493 }
24494 }
24495
24496 /* See read.h. */
24497
24498 void
24499 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24500 {
24501 auto it = m_dwarf2_cus.find (per_cu);
24502 if (it == m_dwarf2_cus.end ())
24503 return;
24504
24505 delete it->second;
24506
24507 m_dwarf2_cus.erase (it);
24508 }
24509
24510 dwarf2_per_objfile::~dwarf2_per_objfile ()
24511 {
24512 remove_all_cus ();
24513 }
24514
24515 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24516 We store these in a hash table separate from the DIEs, and preserve them
24517 when the DIEs are flushed out of cache.
24518
24519 The CU "per_cu" pointer is needed because offset alone is not enough to
24520 uniquely identify the type. A file may have multiple .debug_types sections,
24521 or the type may come from a DWO file. Furthermore, while it's more logical
24522 to use per_cu->section+offset, with Fission the section with the data is in
24523 the DWO file but we don't know that section at the point we need it.
24524 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24525 because we can enter the lookup routine, get_die_type_at_offset, from
24526 outside this file, and thus won't necessarily have PER_CU->cu.
24527 Fortunately, PER_CU is stable for the life of the objfile. */
24528
24529 struct dwarf2_per_cu_offset_and_type
24530 {
24531 const struct dwarf2_per_cu_data *per_cu;
24532 sect_offset sect_off;
24533 struct type *type;
24534 };
24535
24536 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24537
24538 static hashval_t
24539 per_cu_offset_and_type_hash (const void *item)
24540 {
24541 const struct dwarf2_per_cu_offset_and_type *ofs
24542 = (const struct dwarf2_per_cu_offset_and_type *) item;
24543
24544 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24545 }
24546
24547 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24548
24549 static int
24550 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24551 {
24552 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24553 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24554 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24555 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24556
24557 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24558 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24559 }
24560
24561 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24562 table if necessary. For convenience, return TYPE.
24563
24564 The DIEs reading must have careful ordering to:
24565 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24566 reading current DIE.
24567 * Not trying to dereference contents of still incompletely read in types
24568 while reading in other DIEs.
24569 * Enable referencing still incompletely read in types just by a pointer to
24570 the type without accessing its fields.
24571
24572 Therefore caller should follow these rules:
24573 * Try to fetch any prerequisite types we may need to build this DIE type
24574 before building the type and calling set_die_type.
24575 * After building type call set_die_type for current DIE as soon as
24576 possible before fetching more types to complete the current type.
24577 * Make the type as complete as possible before fetching more types. */
24578
24579 static struct type *
24580 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24581 bool skip_data_location)
24582 {
24583 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24584 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24585 struct objfile *objfile = per_objfile->objfile;
24586 struct attribute *attr;
24587 struct dynamic_prop prop;
24588
24589 /* For Ada types, make sure that the gnat-specific data is always
24590 initialized (if not already set). There are a few types where
24591 we should not be doing so, because the type-specific area is
24592 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24593 where the type-specific area is used to store the floatformat).
24594 But this is not a problem, because the gnat-specific information
24595 is actually not needed for these types. */
24596 if (need_gnat_info (cu)
24597 && type->code () != TYPE_CODE_FUNC
24598 && type->code () != TYPE_CODE_FLT
24599 && type->code () != TYPE_CODE_METHODPTR
24600 && type->code () != TYPE_CODE_MEMBERPTR
24601 && type->code () != TYPE_CODE_METHOD
24602 && type->code () != TYPE_CODE_FIXED_POINT
24603 && !HAVE_GNAT_AUX_INFO (type))
24604 INIT_GNAT_SPECIFIC (type);
24605
24606 /* Read DW_AT_allocated and set in type. */
24607 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24608 if (attr != NULL)
24609 {
24610 struct type *prop_type = cu->addr_sized_int_type (false);
24611 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24612 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24613 }
24614
24615 /* Read DW_AT_associated and set in type. */
24616 attr = dwarf2_attr (die, DW_AT_associated, cu);
24617 if (attr != NULL)
24618 {
24619 struct type *prop_type = cu->addr_sized_int_type (false);
24620 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24621 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24622 }
24623
24624 /* Read DW_AT_data_location and set in type. */
24625 if (!skip_data_location)
24626 {
24627 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24628 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24629 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24630 }
24631
24632 if (per_objfile->die_type_hash == NULL)
24633 per_objfile->die_type_hash
24634 = htab_up (htab_create_alloc (127,
24635 per_cu_offset_and_type_hash,
24636 per_cu_offset_and_type_eq,
24637 NULL, xcalloc, xfree));
24638
24639 ofs.per_cu = cu->per_cu;
24640 ofs.sect_off = die->sect_off;
24641 ofs.type = type;
24642 slot = (struct dwarf2_per_cu_offset_and_type **)
24643 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24644 if (*slot)
24645 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24646 sect_offset_str (die->sect_off));
24647 *slot = XOBNEW (&objfile->objfile_obstack,
24648 struct dwarf2_per_cu_offset_and_type);
24649 **slot = ofs;
24650 return type;
24651 }
24652
24653 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24654 or return NULL if the die does not have a saved type. */
24655
24656 static struct type *
24657 get_die_type_at_offset (sect_offset sect_off,
24658 dwarf2_per_cu_data *per_cu,
24659 dwarf2_per_objfile *per_objfile)
24660 {
24661 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24662
24663 if (per_objfile->die_type_hash == NULL)
24664 return NULL;
24665
24666 ofs.per_cu = per_cu;
24667 ofs.sect_off = sect_off;
24668 slot = ((struct dwarf2_per_cu_offset_and_type *)
24669 htab_find (per_objfile->die_type_hash.get (), &ofs));
24670 if (slot)
24671 return slot->type;
24672 else
24673 return NULL;
24674 }
24675
24676 /* Look up the type for DIE in CU in die_type_hash,
24677 or return NULL if DIE does not have a saved type. */
24678
24679 static struct type *
24680 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24681 {
24682 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24683 }
24684
24685 /* Trivial hash function for partial_die_info: the hash value of a DIE
24686 is its offset in .debug_info for this objfile. */
24687
24688 static hashval_t
24689 partial_die_hash (const void *item)
24690 {
24691 const struct partial_die_info *part_die
24692 = (const struct partial_die_info *) item;
24693
24694 return to_underlying (part_die->sect_off);
24695 }
24696
24697 /* Trivial comparison function for partial_die_info structures: two DIEs
24698 are equal if they have the same offset. */
24699
24700 static int
24701 partial_die_eq (const void *item_lhs, const void *item_rhs)
24702 {
24703 const struct partial_die_info *part_die_lhs
24704 = (const struct partial_die_info *) item_lhs;
24705 const struct partial_die_info *part_die_rhs
24706 = (const struct partial_die_info *) item_rhs;
24707
24708 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24709 }
24710
24711 struct cmd_list_element *set_dwarf_cmdlist;
24712 struct cmd_list_element *show_dwarf_cmdlist;
24713
24714 static void
24715 show_check_physname (struct ui_file *file, int from_tty,
24716 struct cmd_list_element *c, const char *value)
24717 {
24718 fprintf_filtered (file,
24719 _("Whether to check \"physname\" is %s.\n"),
24720 value);
24721 }
24722
24723 void _initialize_dwarf2_read ();
24724 void
24725 _initialize_dwarf2_read ()
24726 {
24727 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24728 Set DWARF specific variables.\n\
24729 Configure DWARF variables such as the cache size."),
24730 &set_dwarf_cmdlist,
24731 0/*allow-unknown*/, &maintenance_set_cmdlist);
24732
24733 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24734 Show DWARF specific variables.\n\
24735 Show DWARF variables such as the cache size."),
24736 &show_dwarf_cmdlist,
24737 0/*allow-unknown*/, &maintenance_show_cmdlist);
24738
24739 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24740 &dwarf_max_cache_age, _("\
24741 Set the upper bound on the age of cached DWARF compilation units."), _("\
24742 Show the upper bound on the age of cached DWARF compilation units."), _("\
24743 A higher limit means that cached compilation units will be stored\n\
24744 in memory longer, and more total memory will be used. Zero disables\n\
24745 caching, which can slow down startup."),
24746 NULL,
24747 show_dwarf_max_cache_age,
24748 &set_dwarf_cmdlist,
24749 &show_dwarf_cmdlist);
24750
24751 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24752 Set debugging of the DWARF reader."), _("\
24753 Show debugging of the DWARF reader."), _("\
24754 When enabled (non-zero), debugging messages are printed during DWARF\n\
24755 reading and symtab expansion. A value of 1 (one) provides basic\n\
24756 information. A value greater than 1 provides more verbose information."),
24757 NULL,
24758 NULL,
24759 &setdebuglist, &showdebuglist);
24760
24761 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24762 Set debugging of the DWARF DIE reader."), _("\
24763 Show debugging of the DWARF DIE reader."), _("\
24764 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24765 The value is the maximum depth to print."),
24766 NULL,
24767 NULL,
24768 &setdebuglist, &showdebuglist);
24769
24770 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24771 Set debugging of the dwarf line reader."), _("\
24772 Show debugging of the dwarf line reader."), _("\
24773 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24774 A value of 1 (one) provides basic information.\n\
24775 A value greater than 1 provides more verbose information."),
24776 NULL,
24777 NULL,
24778 &setdebuglist, &showdebuglist);
24779
24780 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24781 Set cross-checking of \"physname\" code against demangler."), _("\
24782 Show cross-checking of \"physname\" code against demangler."), _("\
24783 When enabled, GDB's internal \"physname\" code is checked against\n\
24784 the demangler."),
24785 NULL, show_check_physname,
24786 &setdebuglist, &showdebuglist);
24787
24788 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24789 no_class, &use_deprecated_index_sections, _("\
24790 Set whether to use deprecated gdb_index sections."), _("\
24791 Show whether to use deprecated gdb_index sections."), _("\
24792 When enabled, deprecated .gdb_index sections are used anyway.\n\
24793 Normally they are ignored either because of a missing feature or\n\
24794 performance issue.\n\
24795 Warning: This option must be enabled before gdb reads the file."),
24796 NULL,
24797 NULL,
24798 &setlist, &showlist);
24799
24800 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24801 &dwarf2_locexpr_funcs);
24802 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24803 &dwarf2_loclist_funcs);
24804
24805 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24806 &dwarf2_block_frame_base_locexpr_funcs);
24807 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24808 &dwarf2_block_frame_base_loclist_funcs);
24809
24810 #if GDB_SELF_TEST
24811 selftests::register_test ("dw2_expand_symtabs_matching",
24812 selftests::dw2_expand_symtabs_matching::run_test);
24813 selftests::register_test ("dwarf2_find_containing_comp_unit",
24814 selftests::find_containing_comp_unit::run_test);
24815 #endif
24816 }