gdb/testsuite: fix fission support in the Dwarf assembler
[deliverable/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.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/sect-names.h"
44 #include "dwarf2/stringify.h"
45 #include "dwarf2/public.h"
46 #include "bfd.h"
47 #include "elf-bfd.h"
48 #include "symtab.h"
49 #include "gdbtypes.h"
50 #include "objfiles.h"
51 #include "dwarf2.h"
52 #include "buildsym.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
92 /* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95 static unsigned int dwarf_read_debug = 0;
96
97 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
98
99 #define dwarf_read_debug_printf(fmt, ...) \
100 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
101 ##__VA_ARGS__)
102
103 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
104
105 #define dwarf_read_debug_printf_v(fmt, ...) \
106 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
107 ##__VA_ARGS__)
108
109 /* When non-zero, dump DIEs after they are read in. */
110 static unsigned int dwarf_die_debug = 0;
111
112 /* When non-zero, dump line number entries as they are read in. */
113 unsigned int dwarf_line_debug = 0;
114
115 /* When true, cross-check physname against demangler. */
116 static bool check_physname = false;
117
118 /* When true, do not reject deprecated .gdb_index sections. */
119 static bool use_deprecated_index_sections = false;
120
121 /* This is used to store the data that is always per objfile. */
122 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
123
124 /* These are used to store the dwarf2_per_bfd objects.
125
126 objfiles having the same BFD, which doesn't require relocations, are going to
127 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
128
129 Other objfiles are not going to share a dwarf2_per_bfd with any other
130 objfiles, so they'll have their own version kept in the _objfile_data_key
131 version. */
132 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
133 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
134
135 /* The "aclass" indices for various kinds of computed DWARF symbols. */
136
137 static int dwarf2_locexpr_index;
138 static int dwarf2_loclist_index;
139 static int dwarf2_locexpr_block_index;
140 static int dwarf2_loclist_block_index;
141
142 /* Size of .debug_loclists section header for 32-bit DWARF format. */
143 #define LOCLIST_HEADER_SIZE32 12
144
145 /* Size of .debug_loclists section header for 64-bit DWARF format. */
146 #define LOCLIST_HEADER_SIZE64 20
147
148 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
149 #define RNGLIST_HEADER_SIZE32 12
150
151 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
152 #define RNGLIST_HEADER_SIZE64 20
153
154 /* An index into a (C++) symbol name component in a symbol name as
155 recorded in the mapped_index's symbol table. For each C++ symbol
156 in the symbol table, we record one entry for the start of each
157 component in the symbol in a table of name components, and then
158 sort the table, in order to be able to binary search symbol names,
159 ignoring leading namespaces, both completion and regular look up.
160 For example, for symbol "A::B::C", we'll have an entry that points
161 to "A::B::C", another that points to "B::C", and another for "C".
162 Note that function symbols in GDB index have no parameter
163 information, just the function/method names. You can convert a
164 name_component to a "const char *" using the
165 'mapped_index::symbol_name_at(offset_type)' method. */
166
167 struct name_component
168 {
169 /* Offset in the symbol name where the component starts. Stored as
170 a (32-bit) offset instead of a pointer to save memory and improve
171 locality on 64-bit architectures. */
172 offset_type name_offset;
173
174 /* The symbol's index in the symbol and constant pool tables of a
175 mapped_index. */
176 offset_type idx;
177 };
178
179 /* Base class containing bits shared by both .gdb_index and
180 .debug_name indexes. */
181
182 struct mapped_index_base
183 {
184 mapped_index_base () = default;
185 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
186
187 /* The name_component table (a sorted vector). See name_component's
188 description above. */
189 std::vector<name_component> name_components;
190
191 /* How NAME_COMPONENTS is sorted. */
192 enum case_sensitivity name_components_casing;
193
194 /* Return the number of names in the symbol table. */
195 virtual size_t symbol_name_count () const = 0;
196
197 /* Get the name of the symbol at IDX in the symbol table. */
198 virtual const char *symbol_name_at
199 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
200
201 /* Return whether the name at IDX in the symbol table should be
202 ignored. */
203 virtual bool symbol_name_slot_invalid (offset_type idx) const
204 {
205 return false;
206 }
207
208 /* Build the symbol name component sorted vector, if we haven't
209 yet. */
210 void build_name_components (dwarf2_per_objfile *per_objfile);
211
212 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
213 possible matches for LN_NO_PARAMS in the name component
214 vector. */
215 std::pair<std::vector<name_component>::const_iterator,
216 std::vector<name_component>::const_iterator>
217 find_name_components_bounds (const lookup_name_info &ln_no_params,
218 enum language lang,
219 dwarf2_per_objfile *per_objfile) const;
220
221 /* Prevent deleting/destroying via a base class pointer. */
222 protected:
223 ~mapped_index_base() = default;
224 };
225
226 /* A description of the mapped index. The file format is described in
227 a comment by the code that writes the index. */
228 struct mapped_index final : public mapped_index_base
229 {
230 /* A slot/bucket in the symbol table hash. */
231 struct symbol_table_slot
232 {
233 const offset_type name;
234 const offset_type vec;
235 };
236
237 /* Index data format version. */
238 int version = 0;
239
240 /* The address table data. */
241 gdb::array_view<const gdb_byte> address_table;
242
243 /* The symbol table, implemented as a hash table. */
244 gdb::array_view<symbol_table_slot> symbol_table;
245
246 /* A pointer to the constant pool. */
247 const char *constant_pool = nullptr;
248
249 bool symbol_name_slot_invalid (offset_type idx) const override
250 {
251 const auto &bucket = this->symbol_table[idx];
252 return bucket.name == 0 && bucket.vec == 0;
253 }
254
255 /* Convenience method to get at the name of the symbol at IDX in the
256 symbol table. */
257 const char *symbol_name_at
258 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
259 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
260
261 size_t symbol_name_count () const override
262 { return this->symbol_table.size (); }
263 };
264
265 /* A description of the mapped .debug_names.
266 Uninitialized map has CU_COUNT 0. */
267 struct mapped_debug_names final : public mapped_index_base
268 {
269 bfd_endian dwarf5_byte_order;
270 bool dwarf5_is_dwarf64;
271 bool augmentation_is_gdb;
272 uint8_t offset_size;
273 uint32_t cu_count = 0;
274 uint32_t tu_count, bucket_count, name_count;
275 const gdb_byte *cu_table_reordered, *tu_table_reordered;
276 const uint32_t *bucket_table_reordered, *hash_table_reordered;
277 const gdb_byte *name_table_string_offs_reordered;
278 const gdb_byte *name_table_entry_offs_reordered;
279 const gdb_byte *entry_pool;
280
281 struct index_val
282 {
283 ULONGEST dwarf_tag;
284 struct attr
285 {
286 /* Attribute name DW_IDX_*. */
287 ULONGEST dw_idx;
288
289 /* Attribute form DW_FORM_*. */
290 ULONGEST form;
291
292 /* Value if FORM is DW_FORM_implicit_const. */
293 LONGEST implicit_const;
294 };
295 std::vector<attr> attr_vec;
296 };
297
298 std::unordered_map<ULONGEST, index_val> abbrev_map;
299
300 const char *namei_to_name
301 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
302
303 /* Implementation of the mapped_index_base virtual interface, for
304 the name_components cache. */
305
306 const char *symbol_name_at
307 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
308 { return namei_to_name (idx, per_objfile); }
309
310 size_t symbol_name_count () const override
311 { return this->name_count; }
312 };
313
314 /* See dwarf2read.h. */
315
316 dwarf2_per_objfile *
317 get_dwarf2_per_objfile (struct objfile *objfile)
318 {
319 return dwarf2_objfile_data_key.get (objfile);
320 }
321
322 /* Default names of the debugging sections. */
323
324 /* Note that if the debugging section has been compressed, it might
325 have a name like .zdebug_info. */
326
327 const struct dwarf2_debug_sections dwarf2_elf_names =
328 {
329 { ".debug_info", ".zdebug_info" },
330 { ".debug_abbrev", ".zdebug_abbrev" },
331 { ".debug_line", ".zdebug_line" },
332 { ".debug_loc", ".zdebug_loc" },
333 { ".debug_loclists", ".zdebug_loclists" },
334 { ".debug_macinfo", ".zdebug_macinfo" },
335 { ".debug_macro", ".zdebug_macro" },
336 { ".debug_str", ".zdebug_str" },
337 { ".debug_str_offsets", ".zdebug_str_offsets" },
338 { ".debug_line_str", ".zdebug_line_str" },
339 { ".debug_ranges", ".zdebug_ranges" },
340 { ".debug_rnglists", ".zdebug_rnglists" },
341 { ".debug_types", ".zdebug_types" },
342 { ".debug_addr", ".zdebug_addr" },
343 { ".debug_frame", ".zdebug_frame" },
344 { ".eh_frame", NULL },
345 { ".gdb_index", ".zgdb_index" },
346 { ".debug_names", ".zdebug_names" },
347 { ".debug_aranges", ".zdebug_aranges" },
348 23
349 };
350
351 /* List of DWO/DWP sections. */
352
353 static const struct dwop_section_names
354 {
355 struct dwarf2_section_names abbrev_dwo;
356 struct dwarf2_section_names info_dwo;
357 struct dwarf2_section_names line_dwo;
358 struct dwarf2_section_names loc_dwo;
359 struct dwarf2_section_names loclists_dwo;
360 struct dwarf2_section_names macinfo_dwo;
361 struct dwarf2_section_names macro_dwo;
362 struct dwarf2_section_names rnglists_dwo;
363 struct dwarf2_section_names str_dwo;
364 struct dwarf2_section_names str_offsets_dwo;
365 struct dwarf2_section_names types_dwo;
366 struct dwarf2_section_names cu_index;
367 struct dwarf2_section_names tu_index;
368 }
369 dwop_section_names =
370 {
371 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
372 { ".debug_info.dwo", ".zdebug_info.dwo" },
373 { ".debug_line.dwo", ".zdebug_line.dwo" },
374 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
375 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
376 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
377 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
378 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
379 { ".debug_str.dwo", ".zdebug_str.dwo" },
380 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
381 { ".debug_types.dwo", ".zdebug_types.dwo" },
382 { ".debug_cu_index", ".zdebug_cu_index" },
383 { ".debug_tu_index", ".zdebug_tu_index" },
384 };
385
386 /* local data types */
387
388 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
389 begin with a header, which contains the following information. */
390 struct loclists_rnglists_header
391 {
392 /* A 4-byte or 12-byte length containing the length of the
393 set of entries for this compilation unit, not including the
394 length field itself. */
395 unsigned int length;
396
397 /* A 2-byte version identifier. */
398 short version;
399
400 /* A 1-byte unsigned integer containing the size in bytes of an address on
401 the target system. */
402 unsigned char addr_size;
403
404 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
405 on the target system. */
406 unsigned char segment_collector_size;
407
408 /* A 4-byte count of the number of offsets that follow the header. */
409 unsigned int offset_entry_count;
410 };
411
412 /* Type used for delaying computation of method physnames.
413 See comments for compute_delayed_physnames. */
414 struct delayed_method_info
415 {
416 /* The type to which the method is attached, i.e., its parent class. */
417 struct type *type;
418
419 /* The index of the method in the type's function fieldlists. */
420 int fnfield_index;
421
422 /* The index of the method in the fieldlist. */
423 int index;
424
425 /* The name of the DIE. */
426 const char *name;
427
428 /* The DIE associated with this method. */
429 struct die_info *die;
430 };
431
432 /* Internal state when decoding a particular compilation unit. */
433 struct dwarf2_cu
434 {
435 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
436 dwarf2_per_objfile *per_objfile);
437
438 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
439
440 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
441 Create the set of symtabs used by this TU, or if this TU is sharing
442 symtabs with another TU and the symtabs have already been created
443 then restore those symtabs in the line header.
444 We don't need the pc/line-number mapping for type units. */
445 void setup_type_unit_groups (struct die_info *die);
446
447 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
448 buildsym_compunit constructor. */
449 struct compunit_symtab *start_symtab (const char *name,
450 const char *comp_dir,
451 CORE_ADDR low_pc);
452
453 /* Reset the builder. */
454 void reset_builder () { m_builder.reset (); }
455
456 /* Return a type that is a generic pointer type, the size of which
457 matches the address size given in the compilation unit header for
458 this CU. */
459 struct type *addr_type () const;
460
461 /* Find an integer type the same size as the address size given in
462 the compilation unit header for this CU. UNSIGNED_P controls if
463 the integer is unsigned or not. */
464 struct type *addr_sized_int_type (bool unsigned_p) const;
465
466 /* The header of the compilation unit. */
467 struct comp_unit_head header {};
468
469 /* Base address of this compilation unit. */
470 gdb::optional<CORE_ADDR> base_address;
471
472 /* The language we are debugging. */
473 enum language language = language_unknown;
474 const struct language_defn *language_defn = nullptr;
475
476 const char *producer = nullptr;
477
478 private:
479 /* The symtab builder for this CU. This is only non-NULL when full
480 symbols are being read. */
481 std::unique_ptr<buildsym_compunit> m_builder;
482
483 public:
484 /* The generic symbol table building routines have separate lists for
485 file scope symbols and all all other scopes (local scopes). So
486 we need to select the right one to pass to add_symbol_to_list().
487 We do it by keeping a pointer to the correct list in list_in_scope.
488
489 FIXME: The original dwarf code just treated the file scope as the
490 first local scope, and all other local scopes as nested local
491 scopes, and worked fine. Check to see if we really need to
492 distinguish these in buildsym.c. */
493 struct pending **list_in_scope = nullptr;
494
495 /* Hash table holding all the loaded partial DIEs
496 with partial_die->offset.SECT_OFF as hash. */
497 htab_t partial_dies = nullptr;
498
499 /* Storage for things with the same lifetime as this read-in compilation
500 unit, including partial DIEs. */
501 auto_obstack comp_unit_obstack;
502
503 /* Backlink to our per_cu entry. */
504 struct dwarf2_per_cu_data *per_cu;
505
506 /* The dwarf2_per_objfile that owns this. */
507 dwarf2_per_objfile *per_objfile;
508
509 /* How many compilation units ago was this CU last referenced? */
510 int last_used = 0;
511
512 /* A hash table of DIE cu_offset for following references with
513 die_info->offset.sect_off as hash. */
514 htab_t die_hash = nullptr;
515
516 /* Full DIEs if read in. */
517 struct die_info *dies = nullptr;
518
519 /* A set of pointers to dwarf2_per_cu_data objects for compilation
520 units referenced by this one. Only set during full symbol processing;
521 partial symbol tables do not have dependencies. */
522 htab_t dependencies = nullptr;
523
524 /* Header data from the line table, during full symbol processing. */
525 struct line_header *line_header = nullptr;
526 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
527 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
528 this is the DW_TAG_compile_unit die for this CU. We'll hold on
529 to the line header as long as this DIE is being processed. See
530 process_die_scope. */
531 die_info *line_header_die_owner = nullptr;
532
533 /* A list of methods which need to have physnames computed
534 after all type information has been read. */
535 std::vector<delayed_method_info> method_list;
536
537 /* To be copied to symtab->call_site_htab. */
538 htab_t call_site_htab = nullptr;
539
540 /* Non-NULL if this CU came from a DWO file.
541 There is an invariant here that is important to remember:
542 Except for attributes copied from the top level DIE in the "main"
543 (or "stub") file in preparation for reading the DWO file
544 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
545 Either there isn't a DWO file (in which case this is NULL and the point
546 is moot), or there is and either we're not going to read it (in which
547 case this is NULL) or there is and we are reading it (in which case this
548 is non-NULL). */
549 struct dwo_unit *dwo_unit = nullptr;
550
551 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
552 Note this value comes from the Fission stub CU/TU's DIE. */
553 gdb::optional<ULONGEST> addr_base;
554
555 /* The DW_AT_GNU_ranges_base attribute, if present.
556
557 This is only relevant in the context of pre-DWARF 5 split units. In this
558 context, there is a .debug_ranges section in the linked executable,
559 containing all the ranges data for all the compilation units. Each
560 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
561 indicates the base of its contribution to that section. The DW_AT_ranges
562 attributes in the split-unit are of the form DW_FORM_sec_offset and point
563 into the .debug_ranges section of the linked file. However, they are not
564 "true" DW_FORM_sec_offset, because they are relative to the base of their
565 compilation unit's contribution, rather than relative to the beginning of
566 the section. The DW_AT_GNU_ranges_base value must be added to it to make
567 it relative to the beginning of the section.
568
569 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
570 case, so this value can be added without needing to know whether we are in
571 this case or not.
572
573 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
574 skeleton/stub, it must not have the base added, as it already points to the
575 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
576 have a DW_AT_ranges attribute, we can use the
577
578 die->tag != DW_AT_compile_unit
579
580 to determine whether the base should be added or not. */
581 ULONGEST gnu_ranges_base = 0;
582
583 /* The DW_AT_rnglists_base attribute, if present.
584
585 This is used when processing attributes of form DW_FORM_rnglistx in
586 non-split units. Attributes of this form found in a split unit don't
587 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
588 section. */
589 ULONGEST rnglists_base = 0;
590
591 /* The DW_AT_loclists_base attribute if present. */
592 ULONGEST loclist_base = 0;
593
594 /* When reading debug info generated by older versions of rustc, we
595 have to rewrite some union types to be struct types with a
596 variant part. This rewriting must be done after the CU is fully
597 read in, because otherwise at the point of rewriting some struct
598 type might not have been fully processed. So, we keep a list of
599 all such types here and process them after expansion. */
600 std::vector<struct type *> rust_unions;
601
602 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
603 files, the value is implicitly zero. For DWARF 5 version DWO files, the
604 value is often implicit and is the size of the header of
605 .debug_str_offsets section (8 or 4, depending on the address size). */
606 gdb::optional<ULONGEST> str_offsets_base;
607
608 /* Mark used when releasing cached dies. */
609 bool mark : 1;
610
611 /* This CU references .debug_loc. See the symtab->locations_valid field.
612 This test is imperfect as there may exist optimized debug code not using
613 any location list and still facing inlining issues if handled as
614 unoptimized code. For a future better test see GCC PR other/32998. */
615 bool has_loclist : 1;
616
617 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
618 if all the producer_is_* fields are valid. This information is cached
619 because profiling CU expansion showed excessive time spent in
620 producer_is_gxx_lt_4_6. */
621 bool checked_producer : 1;
622 bool producer_is_gxx_lt_4_6 : 1;
623 bool producer_is_gcc_lt_4_3 : 1;
624 bool producer_is_icc : 1;
625 bool producer_is_icc_lt_14 : 1;
626 bool producer_is_codewarrior : 1;
627
628 /* When true, the file that we're processing is known to have
629 debugging info for C++ namespaces. GCC 3.3.x did not produce
630 this information, but later versions do. */
631
632 bool processing_has_namespace_info : 1;
633
634 struct partial_die_info *find_partial_die (sect_offset sect_off);
635
636 /* If this CU was inherited by another CU (via specification,
637 abstract_origin, etc), this is the ancestor CU. */
638 dwarf2_cu *ancestor;
639
640 /* Get the buildsym_compunit for this CU. */
641 buildsym_compunit *get_builder ()
642 {
643 /* If this CU has a builder associated with it, use that. */
644 if (m_builder != nullptr)
645 return m_builder.get ();
646
647 /* Otherwise, search ancestors for a valid builder. */
648 if (ancestor != nullptr)
649 return ancestor->get_builder ();
650
651 return nullptr;
652 }
653 };
654
655 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
656 This includes type_unit_group and quick_file_names. */
657
658 struct stmt_list_hash
659 {
660 /* The DWO unit this table is from or NULL if there is none. */
661 struct dwo_unit *dwo_unit;
662
663 /* Offset in .debug_line or .debug_line.dwo. */
664 sect_offset line_sect_off;
665 };
666
667 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
668 an object of this type. This contains elements of type unit groups
669 that can be shared across objfiles. The non-shareable parts are in
670 type_unit_group_unshareable. */
671
672 struct type_unit_group
673 {
674 /* dwarf2read.c's main "handle" on a TU symtab.
675 To simplify things we create an artificial CU that "includes" all the
676 type units using this stmt_list so that the rest of the code still has
677 a "per_cu" handle on the symtab. */
678 struct dwarf2_per_cu_data per_cu;
679
680 /* The TUs that share this DW_AT_stmt_list entry.
681 This is added to while parsing type units to build partial symtabs,
682 and is deleted afterwards and not used again. */
683 std::vector<signatured_type *> *tus;
684
685 /* The data used to construct the hash key. */
686 struct stmt_list_hash hash;
687 };
688
689 /* These sections are what may appear in a (real or virtual) DWO file. */
690
691 struct dwo_sections
692 {
693 struct dwarf2_section_info abbrev;
694 struct dwarf2_section_info line;
695 struct dwarf2_section_info loc;
696 struct dwarf2_section_info loclists;
697 struct dwarf2_section_info macinfo;
698 struct dwarf2_section_info macro;
699 struct dwarf2_section_info rnglists;
700 struct dwarf2_section_info str;
701 struct dwarf2_section_info str_offsets;
702 /* In the case of a virtual DWO file, these two are unused. */
703 struct dwarf2_section_info info;
704 std::vector<dwarf2_section_info> types;
705 };
706
707 /* CUs/TUs in DWP/DWO files. */
708
709 struct dwo_unit
710 {
711 /* Backlink to the containing struct dwo_file. */
712 struct dwo_file *dwo_file;
713
714 /* The "id" that distinguishes this CU/TU.
715 .debug_info calls this "dwo_id", .debug_types calls this "signature".
716 Since signatures came first, we stick with it for consistency. */
717 ULONGEST signature;
718
719 /* The section this CU/TU lives in, in the DWO file. */
720 struct dwarf2_section_info *section;
721
722 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
723 sect_offset sect_off;
724 unsigned int length;
725
726 /* For types, offset in the type's DIE of the type defined by this TU. */
727 cu_offset type_offset_in_tu;
728 };
729
730 /* include/dwarf2.h defines the DWP section codes.
731 It defines a max value but it doesn't define a min value, which we
732 use for error checking, so provide one. */
733
734 enum dwp_v2_section_ids
735 {
736 DW_SECT_MIN = 1
737 };
738
739 /* Data for one DWO file.
740
741 This includes virtual DWO files (a virtual DWO file is a DWO file as it
742 appears in a DWP file). DWP files don't really have DWO files per se -
743 comdat folding of types "loses" the DWO file they came from, and from
744 a high level view DWP files appear to contain a mass of random types.
745 However, to maintain consistency with the non-DWP case we pretend DWP
746 files contain virtual DWO files, and we assign each TU with one virtual
747 DWO file (generally based on the line and abbrev section offsets -
748 a heuristic that seems to work in practice). */
749
750 struct dwo_file
751 {
752 dwo_file () = default;
753 DISABLE_COPY_AND_ASSIGN (dwo_file);
754
755 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
756 For virtual DWO files the name is constructed from the section offsets
757 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
758 from related CU+TUs. */
759 const char *dwo_name = nullptr;
760
761 /* The DW_AT_comp_dir attribute. */
762 const char *comp_dir = nullptr;
763
764 /* The bfd, when the file is open. Otherwise this is NULL.
765 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
766 gdb_bfd_ref_ptr dbfd;
767
768 /* The sections that make up this DWO file.
769 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
770 sections (for lack of a better name). */
771 struct dwo_sections sections {};
772
773 /* The CUs in the file.
774 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
775 an extension to handle LLVM's Link Time Optimization output (where
776 multiple source files may be compiled into a single object/dwo pair). */
777 htab_up cus;
778
779 /* Table of TUs in the file.
780 Each element is a struct dwo_unit. */
781 htab_up tus;
782 };
783
784 /* These sections are what may appear in a DWP file. */
785
786 struct dwp_sections
787 {
788 /* These are used by all DWP versions (1, 2 and 5). */
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
792
793 /* These are only used by DWP version 2 and version 5 files.
794 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
795 sections are referenced by section number, and are not recorded here.
796 In DWP version 2 or 5 there is at most one copy of all these sections,
797 each section being (effectively) comprised of the concatenation of all of
798 the individual sections that exist in the version 1 format.
799 To keep the code simple we treat each of these concatenated pieces as a
800 section itself (a virtual section?). */
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info info;
803 struct dwarf2_section_info line;
804 struct dwarf2_section_info loc;
805 struct dwarf2_section_info loclists;
806 struct dwarf2_section_info macinfo;
807 struct dwarf2_section_info macro;
808 struct dwarf2_section_info rnglists;
809 struct dwarf2_section_info str_offsets;
810 struct dwarf2_section_info types;
811 };
812
813 /* These sections are what may appear in a virtual DWO file in DWP version 1.
814 A virtual DWO file is a DWO file as it appears in a DWP file. */
815
816 struct virtual_v1_dwo_sections
817 {
818 struct dwarf2_section_info abbrev;
819 struct dwarf2_section_info line;
820 struct dwarf2_section_info loc;
821 struct dwarf2_section_info macinfo;
822 struct dwarf2_section_info macro;
823 struct dwarf2_section_info str_offsets;
824 /* Each DWP hash table entry records one CU or one TU.
825 That is recorded here, and copied to dwo_unit.section. */
826 struct dwarf2_section_info info_or_types;
827 };
828
829 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
830 In version 2, the sections of the DWO files are concatenated together
831 and stored in one section of that name. Thus each ELF section contains
832 several "virtual" sections. */
833
834 struct virtual_v2_or_v5_dwo_sections
835 {
836 bfd_size_type abbrev_offset;
837 bfd_size_type abbrev_size;
838
839 bfd_size_type line_offset;
840 bfd_size_type line_size;
841
842 bfd_size_type loc_offset;
843 bfd_size_type loc_size;
844
845 bfd_size_type loclists_offset;
846 bfd_size_type loclists_size;
847
848 bfd_size_type macinfo_offset;
849 bfd_size_type macinfo_size;
850
851 bfd_size_type macro_offset;
852 bfd_size_type macro_size;
853
854 bfd_size_type rnglists_offset;
855 bfd_size_type rnglists_size;
856
857 bfd_size_type str_offsets_offset;
858 bfd_size_type str_offsets_size;
859
860 /* Each DWP hash table entry records one CU or one TU.
861 That is recorded here, and copied to dwo_unit.section. */
862 bfd_size_type info_or_types_offset;
863 bfd_size_type info_or_types_size;
864 };
865
866 /* Contents of DWP hash tables. */
867
868 struct dwp_hash_table
869 {
870 uint32_t version, nr_columns;
871 uint32_t nr_units, nr_slots;
872 const gdb_byte *hash_table, *unit_table;
873 union
874 {
875 struct
876 {
877 const gdb_byte *indices;
878 } v1;
879 struct
880 {
881 /* This is indexed by column number and gives the id of the section
882 in that column. */
883 #define MAX_NR_V2_DWO_SECTIONS \
884 (1 /* .debug_info or .debug_types */ \
885 + 1 /* .debug_abbrev */ \
886 + 1 /* .debug_line */ \
887 + 1 /* .debug_loc */ \
888 + 1 /* .debug_str_offsets */ \
889 + 1 /* .debug_macro or .debug_macinfo */)
890 int section_ids[MAX_NR_V2_DWO_SECTIONS];
891 const gdb_byte *offsets;
892 const gdb_byte *sizes;
893 } v2;
894 struct
895 {
896 /* This is indexed by column number and gives the id of the section
897 in that column. */
898 #define MAX_NR_V5_DWO_SECTIONS \
899 (1 /* .debug_info */ \
900 + 1 /* .debug_abbrev */ \
901 + 1 /* .debug_line */ \
902 + 1 /* .debug_loclists */ \
903 + 1 /* .debug_str_offsets */ \
904 + 1 /* .debug_macro */ \
905 + 1 /* .debug_rnglists */)
906 int section_ids[MAX_NR_V5_DWO_SECTIONS];
907 const gdb_byte *offsets;
908 const gdb_byte *sizes;
909 } v5;
910 } section_pool;
911 };
912
913 /* Data for one DWP file. */
914
915 struct dwp_file
916 {
917 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
918 : name (name_),
919 dbfd (std::move (abfd))
920 {
921 }
922
923 /* Name of the file. */
924 const char *name;
925
926 /* File format version. */
927 int version = 0;
928
929 /* The bfd. */
930 gdb_bfd_ref_ptr dbfd;
931
932 /* Section info for this file. */
933 struct dwp_sections sections {};
934
935 /* Table of CUs in the file. */
936 const struct dwp_hash_table *cus = nullptr;
937
938 /* Table of TUs in the file. */
939 const struct dwp_hash_table *tus = nullptr;
940
941 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
942 htab_up loaded_cus;
943 htab_up loaded_tus;
944
945 /* Table to map ELF section numbers to their sections.
946 This is only needed for the DWP V1 file format. */
947 unsigned int num_sections = 0;
948 asection **elf_sections = nullptr;
949 };
950
951 /* Struct used to pass misc. parameters to read_die_and_children, et
952 al. which are used for both .debug_info and .debug_types dies.
953 All parameters here are unchanging for the life of the call. This
954 struct exists to abstract away the constant parameters of die reading. */
955
956 struct die_reader_specs
957 {
958 /* The bfd of die_section. */
959 bfd *abfd;
960
961 /* The CU of the DIE we are parsing. */
962 struct dwarf2_cu *cu;
963
964 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
965 struct dwo_file *dwo_file;
966
967 /* The section the die comes from.
968 This is either .debug_info or .debug_types, or the .dwo variants. */
969 struct dwarf2_section_info *die_section;
970
971 /* die_section->buffer. */
972 const gdb_byte *buffer;
973
974 /* The end of the buffer. */
975 const gdb_byte *buffer_end;
976
977 /* The abbreviation table to use when reading the DIEs. */
978 struct abbrev_table *abbrev_table;
979 };
980
981 /* A subclass of die_reader_specs that holds storage and has complex
982 constructor and destructor behavior. */
983
984 class cutu_reader : public die_reader_specs
985 {
986 public:
987
988 cutu_reader (dwarf2_per_cu_data *this_cu,
989 dwarf2_per_objfile *per_objfile,
990 struct abbrev_table *abbrev_table,
991 dwarf2_cu *existing_cu,
992 bool skip_partial);
993
994 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
995 dwarf2_per_objfile *per_objfile,
996 struct dwarf2_cu *parent_cu = nullptr,
997 struct dwo_file *dwo_file = nullptr);
998
999 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1000
1001 const gdb_byte *info_ptr = nullptr;
1002 struct die_info *comp_unit_die = nullptr;
1003 bool dummy_p = false;
1004
1005 /* Release the new CU, putting it on the chain. This cannot be done
1006 for dummy CUs. */
1007 void keep ();
1008
1009 private:
1010 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1011 dwarf2_per_objfile *per_objfile,
1012 dwarf2_cu *existing_cu);
1013
1014 struct dwarf2_per_cu_data *m_this_cu;
1015 std::unique_ptr<dwarf2_cu> m_new_cu;
1016
1017 /* The ordinary abbreviation table. */
1018 abbrev_table_up m_abbrev_table_holder;
1019
1020 /* The DWO abbreviation table. */
1021 abbrev_table_up m_dwo_abbrev_table;
1022 };
1023
1024 /* When we construct a partial symbol table entry we only
1025 need this much information. */
1026 struct partial_die_info : public allocate_on_obstack
1027 {
1028 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
1029
1030 /* Disable assign but still keep copy ctor, which is needed
1031 load_partial_dies. */
1032 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1033
1034 /* Adjust the partial die before generating a symbol for it. This
1035 function may set the is_external flag or change the DIE's
1036 name. */
1037 void fixup (struct dwarf2_cu *cu);
1038
1039 /* Read a minimal amount of information into the minimal die
1040 structure. */
1041 const gdb_byte *read (const struct die_reader_specs *reader,
1042 const struct abbrev_info &abbrev,
1043 const gdb_byte *info_ptr);
1044
1045 /* Compute the name of this partial DIE. This memoizes the
1046 result, so it is safe to call multiple times. */
1047 const char *name (dwarf2_cu *cu);
1048
1049 /* Offset of this DIE. */
1050 const sect_offset sect_off;
1051
1052 /* DWARF-2 tag for this DIE. */
1053 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1054
1055 /* Assorted flags describing the data found in this DIE. */
1056 const unsigned int has_children : 1;
1057
1058 unsigned int is_external : 1;
1059 unsigned int is_declaration : 1;
1060 unsigned int has_type : 1;
1061 unsigned int has_specification : 1;
1062 unsigned int has_pc_info : 1;
1063 unsigned int may_be_inlined : 1;
1064
1065 /* This DIE has been marked DW_AT_main_subprogram. */
1066 unsigned int main_subprogram : 1;
1067
1068 /* Flag set if the SCOPE field of this structure has been
1069 computed. */
1070 unsigned int scope_set : 1;
1071
1072 /* Flag set if the DIE has a byte_size attribute. */
1073 unsigned int has_byte_size : 1;
1074
1075 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1076 unsigned int has_const_value : 1;
1077
1078 /* Flag set if any of the DIE's children are template arguments. */
1079 unsigned int has_template_arguments : 1;
1080
1081 /* Flag set if fixup has been called on this die. */
1082 unsigned int fixup_called : 1;
1083
1084 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1085 unsigned int is_dwz : 1;
1086
1087 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1088 unsigned int spec_is_dwz : 1;
1089
1090 unsigned int canonical_name : 1;
1091
1092 /* The name of this DIE. Normally the value of DW_AT_name, but
1093 sometimes a default name for unnamed DIEs. */
1094 const char *raw_name = nullptr;
1095
1096 /* The linkage name, if present. */
1097 const char *linkage_name = nullptr;
1098
1099 /* The scope to prepend to our children. This is generally
1100 allocated on the comp_unit_obstack, so will disappear
1101 when this compilation unit leaves the cache. */
1102 const char *scope = nullptr;
1103
1104 /* Some data associated with the partial DIE. The tag determines
1105 which field is live. */
1106 union
1107 {
1108 /* The location description associated with this DIE, if any. */
1109 struct dwarf_block *locdesc;
1110 /* The offset of an import, for DW_TAG_imported_unit. */
1111 sect_offset sect_off;
1112 } d {};
1113
1114 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1115 CORE_ADDR lowpc = 0;
1116 CORE_ADDR highpc = 0;
1117
1118 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1119 DW_AT_sibling, if any. */
1120 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1121 could return DW_AT_sibling values to its caller load_partial_dies. */
1122 const gdb_byte *sibling = nullptr;
1123
1124 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1125 DW_AT_specification (or DW_AT_abstract_origin or
1126 DW_AT_extension). */
1127 sect_offset spec_offset {};
1128
1129 /* Pointers to this DIE's parent, first child, and next sibling,
1130 if any. */
1131 struct partial_die_info *die_parent = nullptr;
1132 struct partial_die_info *die_child = nullptr;
1133 struct partial_die_info *die_sibling = nullptr;
1134
1135 friend struct partial_die_info *
1136 dwarf2_cu::find_partial_die (sect_offset sect_off);
1137
1138 private:
1139 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1140 partial_die_info (sect_offset sect_off)
1141 : partial_die_info (sect_off, DW_TAG_padding, 0)
1142 {
1143 }
1144
1145 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1146 int has_children_)
1147 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1148 {
1149 is_external = 0;
1150 is_declaration = 0;
1151 has_type = 0;
1152 has_specification = 0;
1153 has_pc_info = 0;
1154 may_be_inlined = 0;
1155 main_subprogram = 0;
1156 scope_set = 0;
1157 has_byte_size = 0;
1158 has_const_value = 0;
1159 has_template_arguments = 0;
1160 fixup_called = 0;
1161 is_dwz = 0;
1162 spec_is_dwz = 0;
1163 canonical_name = 0;
1164 }
1165 };
1166
1167 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1168 but this would require a corresponding change in unpack_field_as_long
1169 and friends. */
1170 static int bits_per_byte = 8;
1171
1172 struct variant_part_builder;
1173
1174 /* When reading a variant, we track a bit more information about the
1175 field, and store it in an object of this type. */
1176
1177 struct variant_field
1178 {
1179 int first_field = -1;
1180 int last_field = -1;
1181
1182 /* A variant can contain other variant parts. */
1183 std::vector<variant_part_builder> variant_parts;
1184
1185 /* If we see a DW_TAG_variant, then this will be set if this is the
1186 default branch. */
1187 bool default_branch = false;
1188 /* If we see a DW_AT_discr_value, then this will be the discriminant
1189 value. */
1190 ULONGEST discriminant_value = 0;
1191 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1192 data. */
1193 struct dwarf_block *discr_list_data = nullptr;
1194 };
1195
1196 /* This represents a DW_TAG_variant_part. */
1197
1198 struct variant_part_builder
1199 {
1200 /* The offset of the discriminant field. */
1201 sect_offset discriminant_offset {};
1202
1203 /* Variants that are direct children of this variant part. */
1204 std::vector<variant_field> variants;
1205
1206 /* True if we're currently reading a variant. */
1207 bool processing_variant = false;
1208 };
1209
1210 struct nextfield
1211 {
1212 int accessibility = 0;
1213 int virtuality = 0;
1214 /* Variant parts need to find the discriminant, which is a DIE
1215 reference. We track the section offset of each field to make
1216 this link. */
1217 sect_offset offset;
1218 struct field field {};
1219 };
1220
1221 struct fnfieldlist
1222 {
1223 const char *name = nullptr;
1224 std::vector<struct fn_field> fnfields;
1225 };
1226
1227 /* The routines that read and process dies for a C struct or C++ class
1228 pass lists of data member fields and lists of member function fields
1229 in an instance of a field_info structure, as defined below. */
1230 struct field_info
1231 {
1232 /* List of data member and baseclasses fields. */
1233 std::vector<struct nextfield> fields;
1234 std::vector<struct nextfield> baseclasses;
1235
1236 /* Set if the accessibility of one of the fields is not public. */
1237 bool non_public_fields = false;
1238
1239 /* Member function fieldlist array, contains name of possibly overloaded
1240 member function, number of overloaded member functions and a pointer
1241 to the head of the member function field chain. */
1242 std::vector<struct fnfieldlist> fnfieldlists;
1243
1244 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1245 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1246 std::vector<struct decl_field> typedef_field_list;
1247
1248 /* Nested types defined by this class and the number of elements in this
1249 list. */
1250 std::vector<struct decl_field> nested_types_list;
1251
1252 /* If non-null, this is the variant part we are currently
1253 reading. */
1254 variant_part_builder *current_variant_part = nullptr;
1255 /* This holds all the top-level variant parts attached to the type
1256 we're reading. */
1257 std::vector<variant_part_builder> variant_parts;
1258
1259 /* Return the total number of fields (including baseclasses). */
1260 int nfields () const
1261 {
1262 return fields.size () + baseclasses.size ();
1263 }
1264 };
1265
1266 /* Loaded secondary compilation units are kept in memory until they
1267 have not been referenced for the processing of this many
1268 compilation units. Set this to zero to disable caching. Cache
1269 sizes of up to at least twenty will improve startup time for
1270 typical inter-CU-reference binaries, at an obvious memory cost. */
1271 static int dwarf_max_cache_age = 5;
1272 static void
1273 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1274 struct cmd_list_element *c, const char *value)
1275 {
1276 fprintf_filtered (file, _("The upper bound on the age of cached "
1277 "DWARF compilation units is %s.\n"),
1278 value);
1279 }
1280 \f
1281 /* local function prototypes */
1282
1283 static void dwarf2_find_base_address (struct die_info *die,
1284 struct dwarf2_cu *cu);
1285
1286 static dwarf2_psymtab *create_partial_symtab
1287 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1288 const char *name);
1289
1290 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1291 const gdb_byte *info_ptr,
1292 struct die_info *type_unit_die);
1293
1294 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1295
1296 static void scan_partial_symbols (struct partial_die_info *,
1297 CORE_ADDR *, CORE_ADDR *,
1298 int, struct dwarf2_cu *);
1299
1300 static void add_partial_symbol (struct partial_die_info *,
1301 struct dwarf2_cu *);
1302
1303 static void add_partial_namespace (struct partial_die_info *pdi,
1304 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1305 int set_addrmap, struct dwarf2_cu *cu);
1306
1307 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1308 CORE_ADDR *highpc, int set_addrmap,
1309 struct dwarf2_cu *cu);
1310
1311 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1312 struct dwarf2_cu *cu);
1313
1314 static void add_partial_subprogram (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1316 int need_pc, struct dwarf2_cu *cu);
1317
1318 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1319
1320 static struct partial_die_info *load_partial_dies
1321 (const struct die_reader_specs *, const gdb_byte *, int);
1322
1323 /* A pair of partial_die_info and compilation unit. */
1324 struct cu_partial_die_info
1325 {
1326 /* The compilation unit of the partial_die_info. */
1327 struct dwarf2_cu *cu;
1328 /* A partial_die_info. */
1329 struct partial_die_info *pdi;
1330
1331 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1332 : cu (cu),
1333 pdi (pdi)
1334 { /* Nothing. */ }
1335
1336 private:
1337 cu_partial_die_info () = delete;
1338 };
1339
1340 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1341 struct dwarf2_cu *);
1342
1343 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1344 struct attribute *,
1345 const struct attr_abbrev *,
1346 const gdb_byte *);
1347
1348 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1349 struct attribute *attr, dwarf_tag tag);
1350
1351 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1352
1353 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1354 dwarf2_section_info *, sect_offset);
1355
1356 static const char *read_indirect_string
1357 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1358 const struct comp_unit_head *, unsigned int *);
1359
1360 static const char *read_indirect_string_at_offset
1361 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1362
1363 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1364 const gdb_byte *,
1365 unsigned int *);
1366
1367 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1368 ULONGEST str_index);
1369
1370 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1371 ULONGEST str_index);
1372
1373 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1374
1375 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1376 struct dwarf2_cu *);
1377
1378 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1379 struct dwarf2_cu *cu);
1380
1381 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1382
1383 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1384 struct dwarf2_cu *cu);
1385
1386 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1387
1388 static struct die_info *die_specification (struct die_info *die,
1389 struct dwarf2_cu **);
1390
1391 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1392 struct dwarf2_cu *cu);
1393
1394 static void dwarf_decode_lines (struct line_header *, const char *,
1395 struct dwarf2_cu *, dwarf2_psymtab *,
1396 CORE_ADDR, int decode_mapping);
1397
1398 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1399 const char *);
1400
1401 static struct symbol *new_symbol (struct die_info *, struct type *,
1402 struct dwarf2_cu *, struct symbol * = NULL);
1403
1404 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1405 struct dwarf2_cu *);
1406
1407 static void dwarf2_const_value_attr (const struct attribute *attr,
1408 struct type *type,
1409 const char *name,
1410 struct obstack *obstack,
1411 struct dwarf2_cu *cu, LONGEST *value,
1412 const gdb_byte **bytes,
1413 struct dwarf2_locexpr_baton **baton);
1414
1415 static struct type *read_subrange_index_type (struct die_info *die,
1416 struct dwarf2_cu *cu);
1417
1418 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1419
1420 static int need_gnat_info (struct dwarf2_cu *);
1421
1422 static struct type *die_descriptive_type (struct die_info *,
1423 struct dwarf2_cu *);
1424
1425 static void set_descriptive_type (struct type *, struct die_info *,
1426 struct dwarf2_cu *);
1427
1428 static struct type *die_containing_type (struct die_info *,
1429 struct dwarf2_cu *);
1430
1431 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1432 struct dwarf2_cu *);
1433
1434 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1435
1436 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1437
1438 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1439
1440 static char *typename_concat (struct obstack *obs, const char *prefix,
1441 const char *suffix, int physname,
1442 struct dwarf2_cu *cu);
1443
1444 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1445
1446 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1447
1448 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1449
1450 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1451
1452 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1453
1454 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1455
1456 /* Return the .debug_loclists section to use for cu. */
1457 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1458
1459 /* Return the .debug_rnglists section to use for cu. */
1460 static struct dwarf2_section_info *cu_debug_rnglists_section
1461 (struct dwarf2_cu *cu, dwarf_tag tag);
1462
1463 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1464 values. Keep the items ordered with increasing constraints compliance. */
1465 enum pc_bounds_kind
1466 {
1467 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1468 PC_BOUNDS_NOT_PRESENT,
1469
1470 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1471 were present but they do not form a valid range of PC addresses. */
1472 PC_BOUNDS_INVALID,
1473
1474 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1475 PC_BOUNDS_RANGES,
1476
1477 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1478 PC_BOUNDS_HIGH_LOW,
1479 };
1480
1481 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1482 CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *,
1484 dwarf2_psymtab *);
1485
1486 static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
1490 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1491 CORE_ADDR, struct dwarf2_cu *);
1492
1493 static void dwarf2_add_field (struct field_info *, struct die_info *,
1494 struct dwarf2_cu *);
1495
1496 static void dwarf2_attach_fields_to_type (struct field_info *,
1497 struct type *, struct dwarf2_cu *);
1498
1499 static void dwarf2_add_member_fn (struct field_info *,
1500 struct die_info *, struct type *,
1501 struct dwarf2_cu *);
1502
1503 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1504 struct type *,
1505 struct dwarf2_cu *);
1506
1507 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1508
1509 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1510
1511 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1512
1513 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
1515 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1516
1517 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1518
1519 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1520
1521 static struct type *read_module_type (struct die_info *die,
1522 struct dwarf2_cu *cu);
1523
1524 static const char *namespace_name (struct die_info *die,
1525 int *is_anonymous, struct dwarf2_cu *);
1526
1527 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1528
1529 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1530 bool * = nullptr);
1531
1532 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1533 struct dwarf2_cu *);
1534
1535 static struct die_info *read_die_and_siblings_1
1536 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1537 struct die_info *);
1538
1539 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1540 const gdb_byte *info_ptr,
1541 const gdb_byte **new_info_ptr,
1542 struct die_info *parent);
1543
1544 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1545 struct die_info **, const gdb_byte *,
1546 int);
1547
1548 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1549 struct die_info **, const gdb_byte *);
1550
1551 static void process_die (struct die_info *, struct dwarf2_cu *);
1552
1553 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1554 struct objfile *);
1555
1556 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1557
1558 static const char *dwarf2_full_name (const char *name,
1559 struct die_info *die,
1560 struct dwarf2_cu *cu);
1561
1562 static const char *dwarf2_physname (const char *name, struct die_info *die,
1563 struct dwarf2_cu *cu);
1564
1565 static struct die_info *dwarf2_extension (struct die_info *die,
1566 struct dwarf2_cu **);
1567
1568 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1569
1570 static void dump_die_for_error (struct die_info *);
1571
1572 static void dump_die_1 (struct ui_file *, int level, int max_level,
1573 struct die_info *);
1574
1575 /*static*/ void dump_die (struct die_info *, int max_level);
1576
1577 static void store_in_ref_table (struct die_info *,
1578 struct dwarf2_cu *);
1579
1580 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1581 const struct attribute *,
1582 struct dwarf2_cu **);
1583
1584 static struct die_info *follow_die_ref (struct die_info *,
1585 const struct attribute *,
1586 struct dwarf2_cu **);
1587
1588 static struct die_info *follow_die_sig (struct die_info *,
1589 const struct attribute *,
1590 struct dwarf2_cu **);
1591
1592 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1593 struct dwarf2_cu *);
1594
1595 static struct type *get_DW_AT_signature_type (struct die_info *,
1596 const struct attribute *,
1597 struct dwarf2_cu *);
1598
1599 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1600 dwarf2_per_objfile *per_objfile);
1601
1602 static void read_signatured_type (signatured_type *sig_type,
1603 dwarf2_per_objfile *per_objfile);
1604
1605 static int attr_to_dynamic_prop (const struct attribute *attr,
1606 struct die_info *die, struct dwarf2_cu *cu,
1607 struct dynamic_prop *prop, struct type *type);
1608
1609 /* memory allocation interface */
1610
1611 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1612
1613 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1614
1615 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1616
1617 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1618 struct dwarf2_loclist_baton *baton,
1619 const struct attribute *attr);
1620
1621 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1622 struct symbol *sym,
1623 struct dwarf2_cu *cu,
1624 int is_block);
1625
1626 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1627 const gdb_byte *info_ptr,
1628 const struct abbrev_info *abbrev);
1629
1630 static hashval_t partial_die_hash (const void *item);
1631
1632 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1633
1634 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1635 (sect_offset sect_off, unsigned int offset_in_dwz,
1636 dwarf2_per_objfile *per_objfile);
1637
1638 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1639 struct die_info *comp_unit_die,
1640 enum language pretend_language);
1641
1642 static struct type *set_die_type (struct die_info *, struct type *,
1643 struct dwarf2_cu *, bool = false);
1644
1645 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1646
1647 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1648
1649 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1650 dwarf2_per_objfile *per_objfile,
1651 dwarf2_cu *existing_cu,
1652 bool skip_partial,
1653 enum language pretend_language);
1654
1655 static void process_full_comp_unit (dwarf2_cu *cu,
1656 enum language pretend_language);
1657
1658 static void process_full_type_unit (dwarf2_cu *cu,
1659 enum language pretend_language);
1660
1661 static void dwarf2_add_dependence (struct dwarf2_cu *,
1662 struct dwarf2_per_cu_data *);
1663
1664 static void dwarf2_mark (struct dwarf2_cu *);
1665
1666 static struct type *get_die_type_at_offset (sect_offset,
1667 dwarf2_per_cu_data *per_cu,
1668 dwarf2_per_objfile *per_objfile);
1669
1670 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1671
1672 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1673 dwarf2_per_objfile *per_objfile,
1674 enum language pretend_language);
1675
1676 static void process_queue (dwarf2_per_objfile *per_objfile);
1677
1678 /* Class, the destructor of which frees all allocated queue entries. This
1679 will only have work to do if an error was thrown while processing the
1680 dwarf. If no error was thrown then the queue entries should have all
1681 been processed, and freed, as we went along. */
1682
1683 class dwarf2_queue_guard
1684 {
1685 public:
1686 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1687 : m_per_objfile (per_objfile)
1688 {
1689 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1690
1691 m_per_objfile->per_bfd->queue.emplace ();
1692 }
1693
1694 /* Free any entries remaining on the queue. There should only be
1695 entries left if we hit an error while processing the dwarf. */
1696 ~dwarf2_queue_guard ()
1697 {
1698 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1699
1700 m_per_objfile->per_bfd->queue.reset ();
1701 }
1702
1703 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1704
1705 private:
1706 dwarf2_per_objfile *m_per_objfile;
1707 };
1708
1709 dwarf2_queue_item::~dwarf2_queue_item ()
1710 {
1711 /* Anything still marked queued is likely to be in an
1712 inconsistent state, so discard it. */
1713 if (per_cu->queued)
1714 {
1715 per_objfile->remove_cu (per_cu);
1716 per_cu->queued = 0;
1717 }
1718 }
1719
1720 /* The return type of find_file_and_directory. Note, the enclosed
1721 string pointers are only valid while this object is valid. */
1722
1723 struct file_and_directory
1724 {
1725 /* The filename. This is never NULL. */
1726 const char *name;
1727
1728 /* The compilation directory. NULL if not known. If we needed to
1729 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1730 points directly to the DW_AT_comp_dir string attribute owned by
1731 the obstack that owns the DIE. */
1732 const char *comp_dir;
1733
1734 /* If we needed to build a new string for comp_dir, this is what
1735 owns the storage. */
1736 std::string comp_dir_storage;
1737 };
1738
1739 static file_and_directory find_file_and_directory (struct die_info *die,
1740 struct dwarf2_cu *cu);
1741
1742 static htab_up allocate_signatured_type_table ();
1743
1744 static htab_up allocate_dwo_unit_table ();
1745
1746 static struct dwo_unit *lookup_dwo_unit_in_dwp
1747 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1748 const char *comp_dir, ULONGEST signature, int is_debug_types);
1749
1750 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1751
1752 static struct dwo_unit *lookup_dwo_comp_unit
1753 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1754 ULONGEST signature);
1755
1756 static struct dwo_unit *lookup_dwo_type_unit
1757 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1758
1759 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1760
1761 /* A unique pointer to a dwo_file. */
1762
1763 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1764
1765 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1766
1767 static void check_producer (struct dwarf2_cu *cu);
1768
1769 static void free_line_header_voidp (void *arg);
1770 \f
1771 /* Various complaints about symbol reading that don't abort the process. */
1772
1773 static void
1774 dwarf2_debug_line_missing_file_complaint (void)
1775 {
1776 complaint (_(".debug_line section has line data without a file"));
1777 }
1778
1779 static void
1780 dwarf2_debug_line_missing_end_sequence_complaint (void)
1781 {
1782 complaint (_(".debug_line section has line "
1783 "program sequence without an end"));
1784 }
1785
1786 static void
1787 dwarf2_complex_location_expr_complaint (void)
1788 {
1789 complaint (_("location expression too complex"));
1790 }
1791
1792 static void
1793 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1794 int arg3)
1795 {
1796 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1797 arg1, arg2, arg3);
1798 }
1799
1800 static void
1801 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1802 {
1803 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1804 arg1, arg2);
1805 }
1806
1807 /* Hash function for line_header_hash. */
1808
1809 static hashval_t
1810 line_header_hash (const struct line_header *ofs)
1811 {
1812 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1813 }
1814
1815 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1816
1817 static hashval_t
1818 line_header_hash_voidp (const void *item)
1819 {
1820 const struct line_header *ofs = (const struct line_header *) item;
1821
1822 return line_header_hash (ofs);
1823 }
1824
1825 /* Equality function for line_header_hash. */
1826
1827 static int
1828 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1829 {
1830 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1831 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1832
1833 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1834 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1835 }
1836
1837 \f
1838
1839 /* See declaration. */
1840
1841 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1842 bool can_copy_)
1843 : obfd (obfd),
1844 can_copy (can_copy_)
1845 {
1846 if (names == NULL)
1847 names = &dwarf2_elf_names;
1848
1849 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1850 locate_sections (obfd, sec, *names);
1851 }
1852
1853 dwarf2_per_bfd::~dwarf2_per_bfd ()
1854 {
1855 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1856 per_cu->imported_symtabs_free ();
1857
1858 for (signatured_type *sig_type : all_type_units)
1859 sig_type->per_cu.imported_symtabs_free ();
1860
1861 /* Everything else should be on this->obstack. */
1862 }
1863
1864 /* See read.h. */
1865
1866 void
1867 dwarf2_per_objfile::remove_all_cus ()
1868 {
1869 gdb_assert (!this->per_bfd->queue.has_value ());
1870
1871 for (auto pair : m_dwarf2_cus)
1872 delete pair.second;
1873
1874 m_dwarf2_cus.clear ();
1875 }
1876
1877 /* A helper class that calls free_cached_comp_units on
1878 destruction. */
1879
1880 class free_cached_comp_units
1881 {
1882 public:
1883
1884 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1885 : m_per_objfile (per_objfile)
1886 {
1887 }
1888
1889 ~free_cached_comp_units ()
1890 {
1891 m_per_objfile->remove_all_cus ();
1892 }
1893
1894 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1895
1896 private:
1897
1898 dwarf2_per_objfile *m_per_objfile;
1899 };
1900
1901 /* See read.h. */
1902
1903 bool
1904 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1905 {
1906 gdb_assert (per_cu->index < this->m_symtabs.size ());
1907
1908 return this->m_symtabs[per_cu->index] != nullptr;
1909 }
1910
1911 /* See read.h. */
1912
1913 compunit_symtab *
1914 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1915 {
1916 gdb_assert (per_cu->index < this->m_symtabs.size ());
1917
1918 return this->m_symtabs[per_cu->index];
1919 }
1920
1921 /* See read.h. */
1922
1923 void
1924 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1925 compunit_symtab *symtab)
1926 {
1927 gdb_assert (per_cu->index < this->m_symtabs.size ());
1928 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1929
1930 this->m_symtabs[per_cu->index] = symtab;
1931 }
1932
1933 /* Try to locate the sections we need for DWARF 2 debugging
1934 information and return true if we have enough to do something.
1935 NAMES points to the dwarf2 section names, or is NULL if the standard
1936 ELF names are used. CAN_COPY is true for formats where symbol
1937 interposition is possible and so symbol values must follow copy
1938 relocation rules. */
1939
1940 int
1941 dwarf2_has_info (struct objfile *objfile,
1942 const struct dwarf2_debug_sections *names,
1943 bool can_copy)
1944 {
1945 if (objfile->flags & OBJF_READNEVER)
1946 return 0;
1947
1948 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1949
1950 if (per_objfile == NULL)
1951 {
1952 dwarf2_per_bfd *per_bfd;
1953
1954 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1955 BFD doesn't require relocations.
1956
1957 We don't share with objfiles for which -readnow was requested,
1958 because it would complicate things when loading the same BFD with
1959 -readnow and then without -readnow. */
1960 if (!gdb_bfd_requires_relocations (objfile->obfd)
1961 && (objfile->flags & OBJF_READNOW) == 0)
1962 {
1963 /* See if one has been created for this BFD yet. */
1964 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1965
1966 if (per_bfd == nullptr)
1967 {
1968 /* No, create it now. */
1969 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1970 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1971 }
1972 }
1973 else
1974 {
1975 /* No sharing possible, create one specifically for this objfile. */
1976 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1977 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1978 }
1979
1980 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1981 }
1982
1983 return (!per_objfile->per_bfd->info.is_virtual
1984 && per_objfile->per_bfd->info.s.section != NULL
1985 && !per_objfile->per_bfd->abbrev.is_virtual
1986 && per_objfile->per_bfd->abbrev.s.section != NULL);
1987 }
1988
1989 /* See declaration. */
1990
1991 void
1992 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1993 const dwarf2_debug_sections &names)
1994 {
1995 flagword aflag = bfd_section_flags (sectp);
1996
1997 if ((aflag & SEC_HAS_CONTENTS) == 0)
1998 {
1999 }
2000 else if (elf_section_data (sectp)->this_hdr.sh_size
2001 > bfd_get_file_size (abfd))
2002 {
2003 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2004 warning (_("Discarding section %s which has a section size (%s"
2005 ") larger than the file size [in module %s]"),
2006 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2007 bfd_get_filename (abfd));
2008 }
2009 else if (names.info.matches (sectp->name))
2010 {
2011 this->info.s.section = sectp;
2012 this->info.size = bfd_section_size (sectp);
2013 }
2014 else if (names.abbrev.matches (sectp->name))
2015 {
2016 this->abbrev.s.section = sectp;
2017 this->abbrev.size = bfd_section_size (sectp);
2018 }
2019 else if (names.line.matches (sectp->name))
2020 {
2021 this->line.s.section = sectp;
2022 this->line.size = bfd_section_size (sectp);
2023 }
2024 else if (names.loc.matches (sectp->name))
2025 {
2026 this->loc.s.section = sectp;
2027 this->loc.size = bfd_section_size (sectp);
2028 }
2029 else if (names.loclists.matches (sectp->name))
2030 {
2031 this->loclists.s.section = sectp;
2032 this->loclists.size = bfd_section_size (sectp);
2033 }
2034 else if (names.macinfo.matches (sectp->name))
2035 {
2036 this->macinfo.s.section = sectp;
2037 this->macinfo.size = bfd_section_size (sectp);
2038 }
2039 else if (names.macro.matches (sectp->name))
2040 {
2041 this->macro.s.section = sectp;
2042 this->macro.size = bfd_section_size (sectp);
2043 }
2044 else if (names.str.matches (sectp->name))
2045 {
2046 this->str.s.section = sectp;
2047 this->str.size = bfd_section_size (sectp);
2048 }
2049 else if (names.str_offsets.matches (sectp->name))
2050 {
2051 this->str_offsets.s.section = sectp;
2052 this->str_offsets.size = bfd_section_size (sectp);
2053 }
2054 else if (names.line_str.matches (sectp->name))
2055 {
2056 this->line_str.s.section = sectp;
2057 this->line_str.size = bfd_section_size (sectp);
2058 }
2059 else if (names.addr.matches (sectp->name))
2060 {
2061 this->addr.s.section = sectp;
2062 this->addr.size = bfd_section_size (sectp);
2063 }
2064 else if (names.frame.matches (sectp->name))
2065 {
2066 this->frame.s.section = sectp;
2067 this->frame.size = bfd_section_size (sectp);
2068 }
2069 else if (names.eh_frame.matches (sectp->name))
2070 {
2071 this->eh_frame.s.section = sectp;
2072 this->eh_frame.size = bfd_section_size (sectp);
2073 }
2074 else if (names.ranges.matches (sectp->name))
2075 {
2076 this->ranges.s.section = sectp;
2077 this->ranges.size = bfd_section_size (sectp);
2078 }
2079 else if (names.rnglists.matches (sectp->name))
2080 {
2081 this->rnglists.s.section = sectp;
2082 this->rnglists.size = bfd_section_size (sectp);
2083 }
2084 else if (names.types.matches (sectp->name))
2085 {
2086 struct dwarf2_section_info type_section;
2087
2088 memset (&type_section, 0, sizeof (type_section));
2089 type_section.s.section = sectp;
2090 type_section.size = bfd_section_size (sectp);
2091
2092 this->types.push_back (type_section);
2093 }
2094 else if (names.gdb_index.matches (sectp->name))
2095 {
2096 this->gdb_index.s.section = sectp;
2097 this->gdb_index.size = bfd_section_size (sectp);
2098 }
2099 else if (names.debug_names.matches (sectp->name))
2100 {
2101 this->debug_names.s.section = sectp;
2102 this->debug_names.size = bfd_section_size (sectp);
2103 }
2104 else if (names.debug_aranges.matches (sectp->name))
2105 {
2106 this->debug_aranges.s.section = sectp;
2107 this->debug_aranges.size = bfd_section_size (sectp);
2108 }
2109
2110 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2111 && bfd_section_vma (sectp) == 0)
2112 this->has_section_at_zero = true;
2113 }
2114
2115 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2116 SECTION_NAME. */
2117
2118 void
2119 dwarf2_get_section_info (struct objfile *objfile,
2120 enum dwarf2_section_enum sect,
2121 asection **sectp, const gdb_byte **bufp,
2122 bfd_size_type *sizep)
2123 {
2124 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2125 struct dwarf2_section_info *info;
2126
2127 /* We may see an objfile without any DWARF, in which case we just
2128 return nothing. */
2129 if (per_objfile == NULL)
2130 {
2131 *sectp = NULL;
2132 *bufp = NULL;
2133 *sizep = 0;
2134 return;
2135 }
2136 switch (sect)
2137 {
2138 case DWARF2_DEBUG_FRAME:
2139 info = &per_objfile->per_bfd->frame;
2140 break;
2141 case DWARF2_EH_FRAME:
2142 info = &per_objfile->per_bfd->eh_frame;
2143 break;
2144 default:
2145 gdb_assert_not_reached ("unexpected section");
2146 }
2147
2148 info->read (objfile);
2149
2150 *sectp = info->get_bfd_section ();
2151 *bufp = info->buffer;
2152 *sizep = info->size;
2153 }
2154
2155 \f
2156 /* DWARF quick_symbol_functions support. */
2157
2158 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2159 unique line tables, so we maintain a separate table of all .debug_line
2160 derived entries to support the sharing.
2161 All the quick functions need is the list of file names. We discard the
2162 line_header when we're done and don't need to record it here. */
2163 struct quick_file_names
2164 {
2165 /* The data used to construct the hash key. */
2166 struct stmt_list_hash hash;
2167
2168 /* The number of entries in file_names, real_names. */
2169 unsigned int num_file_names;
2170
2171 /* The file names from the line table, after being run through
2172 file_full_name. */
2173 const char **file_names;
2174
2175 /* The file names from the line table after being run through
2176 gdb_realpath. These are computed lazily. */
2177 const char **real_names;
2178 };
2179
2180 /* When using the index (and thus not using psymtabs), each CU has an
2181 object of this type. This is used to hold information needed by
2182 the various "quick" methods. */
2183 struct dwarf2_per_cu_quick_data
2184 {
2185 /* The file table. This can be NULL if there was no file table
2186 or it's currently not read in.
2187 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2188 struct quick_file_names *file_names;
2189
2190 /* A temporary mark bit used when iterating over all CUs in
2191 expand_symtabs_matching. */
2192 unsigned int mark : 1;
2193
2194 /* True if we've tried to read the file table and found there isn't one.
2195 There will be no point in trying to read it again next time. */
2196 unsigned int no_file_data : 1;
2197 };
2198
2199 /* A subclass of psymbol_functions that arranges to read the DWARF
2200 partial symbols when needed. */
2201 struct lazy_dwarf_reader : public psymbol_functions
2202 {
2203 using psymbol_functions::psymbol_functions;
2204
2205 bool can_lazily_read_symbols () override
2206 {
2207 return true;
2208 }
2209
2210 void read_partial_symbols (struct objfile *objfile) override
2211 {
2212 if (dwarf2_has_info (objfile, nullptr))
2213 dwarf2_build_psymtabs (objfile, this);
2214 }
2215 };
2216
2217 static quick_symbol_functions_up
2218 make_lazy_dwarf_reader ()
2219 {
2220 return quick_symbol_functions_up (new lazy_dwarf_reader);
2221 }
2222
2223 struct dwarf2_base_index_functions : public quick_symbol_functions
2224 {
2225 bool has_symbols (struct objfile *objfile) override;
2226
2227 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2228
2229 void forget_cached_source_info (struct objfile *objfile) override;
2230
2231 bool map_symtabs_matching_filename
2232 (struct objfile *objfile, const char *name, const char *real_path,
2233 gdb::function_view<bool (symtab *)> callback) override;
2234
2235 enum language lookup_global_symbol_language (struct objfile *objfile,
2236 const char *name,
2237 domain_enum domain,
2238 bool *symbol_found_p) override
2239 {
2240 *symbol_found_p = false;
2241 return language_unknown;
2242 }
2243
2244 void print_stats (struct objfile *objfile, bool print_bcache) override;
2245
2246 void expand_all_symtabs (struct objfile *objfile) override;
2247
2248 void expand_symtabs_with_fullname (struct objfile *objfile,
2249 const char *fullname) override;
2250
2251 struct compunit_symtab *find_pc_sect_compunit_symtab
2252 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2253 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2254
2255 struct compunit_symtab *find_compunit_symtab_by_address
2256 (struct objfile *objfile, CORE_ADDR address) override
2257 {
2258 return nullptr;
2259 }
2260
2261 void map_symbol_filenames (struct objfile *objfile,
2262 gdb::function_view<symbol_filename_ftype> fun,
2263 bool need_fullname) override;
2264 };
2265
2266 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2267 {
2268 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2269 block_enum block_index,
2270 const char *name,
2271 domain_enum domain) override;
2272
2273 void dump (struct objfile *objfile) override;
2274
2275 void expand_symtabs_for_function (struct objfile *objfile,
2276 const char *func_name) override;
2277
2278 void map_matching_symbols
2279 (struct objfile *,
2280 const lookup_name_info &lookup_name,
2281 domain_enum domain,
2282 int global,
2283 gdb::function_view<symbol_found_callback_ftype> callback,
2284 symbol_compare_ftype *ordered_compare) override;
2285
2286 void expand_symtabs_matching
2287 (struct objfile *objfile,
2288 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2289 const lookup_name_info *lookup_name,
2290 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2291 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2292 enum search_domain kind) override;
2293 };
2294
2295 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2296 {
2297 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2298 block_enum block_index,
2299 const char *name,
2300 domain_enum domain) override;
2301
2302 void dump (struct objfile *objfile) override;
2303
2304 void expand_symtabs_for_function (struct objfile *objfile,
2305 const char *func_name) override;
2306
2307 void map_matching_symbols
2308 (struct objfile *,
2309 const lookup_name_info &lookup_name,
2310 domain_enum domain,
2311 int global,
2312 gdb::function_view<symbol_found_callback_ftype> callback,
2313 symbol_compare_ftype *ordered_compare) override;
2314
2315 void expand_symtabs_matching
2316 (struct objfile *objfile,
2317 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2318 const lookup_name_info *lookup_name,
2319 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2320 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2321 enum search_domain kind) override;
2322 };
2323
2324 static quick_symbol_functions_up
2325 make_dwarf_gdb_index ()
2326 {
2327 return quick_symbol_functions_up (new dwarf2_gdb_index);
2328 }
2329
2330 static quick_symbol_functions_up
2331 make_dwarf_debug_names ()
2332 {
2333 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2334 }
2335
2336 /* Utility hash function for a stmt_list_hash. */
2337
2338 static hashval_t
2339 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2340 {
2341 hashval_t v = 0;
2342
2343 if (stmt_list_hash->dwo_unit != NULL)
2344 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2345 v += to_underlying (stmt_list_hash->line_sect_off);
2346 return v;
2347 }
2348
2349 /* Utility equality function for a stmt_list_hash. */
2350
2351 static int
2352 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2353 const struct stmt_list_hash *rhs)
2354 {
2355 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2356 return 0;
2357 if (lhs->dwo_unit != NULL
2358 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2359 return 0;
2360
2361 return lhs->line_sect_off == rhs->line_sect_off;
2362 }
2363
2364 /* Hash function for a quick_file_names. */
2365
2366 static hashval_t
2367 hash_file_name_entry (const void *e)
2368 {
2369 const struct quick_file_names *file_data
2370 = (const struct quick_file_names *) e;
2371
2372 return hash_stmt_list_entry (&file_data->hash);
2373 }
2374
2375 /* Equality function for a quick_file_names. */
2376
2377 static int
2378 eq_file_name_entry (const void *a, const void *b)
2379 {
2380 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2381 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2382
2383 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2384 }
2385
2386 /* Delete function for a quick_file_names. */
2387
2388 static void
2389 delete_file_name_entry (void *e)
2390 {
2391 struct quick_file_names *file_data = (struct quick_file_names *) e;
2392 int i;
2393
2394 for (i = 0; i < file_data->num_file_names; ++i)
2395 {
2396 xfree ((void*) file_data->file_names[i]);
2397 if (file_data->real_names)
2398 xfree ((void*) file_data->real_names[i]);
2399 }
2400
2401 /* The space for the struct itself lives on the obstack, so we don't
2402 free it here. */
2403 }
2404
2405 /* Create a quick_file_names hash table. */
2406
2407 static htab_up
2408 create_quick_file_names_table (unsigned int nr_initial_entries)
2409 {
2410 return htab_up (htab_create_alloc (nr_initial_entries,
2411 hash_file_name_entry, eq_file_name_entry,
2412 delete_file_name_entry, xcalloc, xfree));
2413 }
2414
2415 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2416 function is unrelated to symtabs, symtab would have to be created afterwards.
2417 You should call age_cached_comp_units after processing the CU. */
2418
2419 static dwarf2_cu *
2420 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2421 bool skip_partial)
2422 {
2423 if (per_cu->is_debug_types)
2424 load_full_type_unit (per_cu, per_objfile);
2425 else
2426 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2427 skip_partial, language_minimal);
2428
2429 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2430 if (cu == nullptr)
2431 return nullptr; /* Dummy CU. */
2432
2433 dwarf2_find_base_address (cu->dies, cu);
2434
2435 return cu;
2436 }
2437
2438 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2439
2440 static void
2441 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2442 dwarf2_per_objfile *per_objfile, bool skip_partial)
2443 {
2444 /* Skip type_unit_groups, reading the type units they contain
2445 is handled elsewhere. */
2446 if (per_cu->type_unit_group_p ())
2447 return;
2448
2449 {
2450 /* The destructor of dwarf2_queue_guard frees any entries left on
2451 the queue. After this point we're guaranteed to leave this function
2452 with the dwarf queue empty. */
2453 dwarf2_queue_guard q_guard (per_objfile);
2454
2455 if (!per_objfile->symtab_set_p (per_cu))
2456 {
2457 queue_comp_unit (per_cu, per_objfile, language_minimal);
2458 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2459
2460 /* If we just loaded a CU from a DWO, and we're working with an index
2461 that may badly handle TUs, load all the TUs in that DWO as well.
2462 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2463 if (!per_cu->is_debug_types
2464 && cu != NULL
2465 && cu->dwo_unit != NULL
2466 && per_objfile->per_bfd->index_table != NULL
2467 && per_objfile->per_bfd->index_table->version <= 7
2468 /* DWP files aren't supported yet. */
2469 && get_dwp_file (per_objfile) == NULL)
2470 queue_and_load_all_dwo_tus (cu);
2471 }
2472
2473 process_queue (per_objfile);
2474 }
2475
2476 /* Age the cache, releasing compilation units that have not
2477 been used recently. */
2478 per_objfile->age_comp_units ();
2479 }
2480
2481 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2482 the per-objfile for which this symtab is instantiated.
2483
2484 Returns the resulting symbol table. */
2485
2486 static struct compunit_symtab *
2487 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2488 dwarf2_per_objfile *per_objfile,
2489 bool skip_partial)
2490 {
2491 gdb_assert (per_objfile->per_bfd->using_index);
2492
2493 if (!per_objfile->symtab_set_p (per_cu))
2494 {
2495 free_cached_comp_units freer (per_objfile);
2496 scoped_restore decrementer = increment_reading_symtab ();
2497 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2498 process_cu_includes (per_objfile);
2499 }
2500
2501 return per_objfile->get_symtab (per_cu);
2502 }
2503
2504 /* See declaration. */
2505
2506 dwarf2_per_cu_data *
2507 dwarf2_per_bfd::get_cutu (int index)
2508 {
2509 if (index >= this->all_comp_units.size ())
2510 {
2511 index -= this->all_comp_units.size ();
2512 gdb_assert (index < this->all_type_units.size ());
2513 return &this->all_type_units[index]->per_cu;
2514 }
2515
2516 return this->all_comp_units[index];
2517 }
2518
2519 /* See declaration. */
2520
2521 dwarf2_per_cu_data *
2522 dwarf2_per_bfd::get_cu (int index)
2523 {
2524 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2525
2526 return this->all_comp_units[index];
2527 }
2528
2529 /* See declaration. */
2530
2531 signatured_type *
2532 dwarf2_per_bfd::get_tu (int index)
2533 {
2534 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2535
2536 return this->all_type_units[index];
2537 }
2538
2539 /* See read.h. */
2540
2541 dwarf2_per_cu_data *
2542 dwarf2_per_bfd::allocate_per_cu ()
2543 {
2544 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2545 result->per_bfd = this;
2546 result->index = m_num_psymtabs++;
2547 return result;
2548 }
2549
2550 /* See read.h. */
2551
2552 signatured_type *
2553 dwarf2_per_bfd::allocate_signatured_type ()
2554 {
2555 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2556 result->per_cu.per_bfd = this;
2557 result->per_cu.index = m_num_psymtabs++;
2558 return result;
2559 }
2560
2561 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2562 obstack, and constructed with the specified field values. */
2563
2564 static dwarf2_per_cu_data *
2565 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2566 struct dwarf2_section_info *section,
2567 int is_dwz,
2568 sect_offset sect_off, ULONGEST length)
2569 {
2570 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2571 the_cu->sect_off = sect_off;
2572 the_cu->length = length;
2573 the_cu->section = section;
2574 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2575 struct dwarf2_per_cu_quick_data);
2576 the_cu->is_dwz = is_dwz;
2577 return the_cu;
2578 }
2579
2580 /* A helper for create_cus_from_index that handles a given list of
2581 CUs. */
2582
2583 static void
2584 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2585 const gdb_byte *cu_list, offset_type n_elements,
2586 struct dwarf2_section_info *section,
2587 int is_dwz)
2588 {
2589 for (offset_type i = 0; i < n_elements; i += 2)
2590 {
2591 gdb_static_assert (sizeof (ULONGEST) >= 8);
2592
2593 sect_offset sect_off
2594 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2595 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2596 cu_list += 2 * 8;
2597
2598 dwarf2_per_cu_data *per_cu
2599 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2600 length);
2601 per_bfd->all_comp_units.push_back (per_cu);
2602 }
2603 }
2604
2605 /* Read the CU list from the mapped index, and use it to create all
2606 the CU objects for PER_BFD. */
2607
2608 static void
2609 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2610 const gdb_byte *cu_list, offset_type cu_list_elements,
2611 const gdb_byte *dwz_list, offset_type dwz_elements)
2612 {
2613 gdb_assert (per_bfd->all_comp_units.empty ());
2614 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2615
2616 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2617 &per_bfd->info, 0);
2618
2619 if (dwz_elements == 0)
2620 return;
2621
2622 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2623 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2624 &dwz->info, 1);
2625 }
2626
2627 /* Create the signatured type hash table from the index. */
2628
2629 static void
2630 create_signatured_type_table_from_index
2631 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2632 const gdb_byte *bytes, offset_type elements)
2633 {
2634 gdb_assert (per_bfd->all_type_units.empty ());
2635 per_bfd->all_type_units.reserve (elements / 3);
2636
2637 htab_up sig_types_hash = allocate_signatured_type_table ();
2638
2639 for (offset_type i = 0; i < elements; i += 3)
2640 {
2641 struct signatured_type *sig_type;
2642 ULONGEST signature;
2643 void **slot;
2644 cu_offset type_offset_in_tu;
2645
2646 gdb_static_assert (sizeof (ULONGEST) >= 8);
2647 sect_offset sect_off
2648 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2649 type_offset_in_tu
2650 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2651 BFD_ENDIAN_LITTLE);
2652 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2653 bytes += 3 * 8;
2654
2655 sig_type = per_bfd->allocate_signatured_type ();
2656 sig_type->signature = signature;
2657 sig_type->type_offset_in_tu = type_offset_in_tu;
2658 sig_type->per_cu.is_debug_types = 1;
2659 sig_type->per_cu.section = section;
2660 sig_type->per_cu.sect_off = sect_off;
2661 sig_type->per_cu.v.quick
2662 = OBSTACK_ZALLOC (&per_bfd->obstack,
2663 struct dwarf2_per_cu_quick_data);
2664
2665 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2666 *slot = sig_type;
2667
2668 per_bfd->all_type_units.push_back (sig_type);
2669 }
2670
2671 per_bfd->signatured_types = std::move (sig_types_hash);
2672 }
2673
2674 /* Create the signatured type hash table from .debug_names. */
2675
2676 static void
2677 create_signatured_type_table_from_debug_names
2678 (dwarf2_per_objfile *per_objfile,
2679 const mapped_debug_names &map,
2680 struct dwarf2_section_info *section,
2681 struct dwarf2_section_info *abbrev_section)
2682 {
2683 struct objfile *objfile = per_objfile->objfile;
2684
2685 section->read (objfile);
2686 abbrev_section->read (objfile);
2687
2688 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2689 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2690
2691 htab_up sig_types_hash = allocate_signatured_type_table ();
2692
2693 for (uint32_t i = 0; i < map.tu_count; ++i)
2694 {
2695 struct signatured_type *sig_type;
2696 void **slot;
2697
2698 sect_offset sect_off
2699 = (sect_offset) (extract_unsigned_integer
2700 (map.tu_table_reordered + i * map.offset_size,
2701 map.offset_size,
2702 map.dwarf5_byte_order));
2703
2704 comp_unit_head cu_header;
2705 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2706 abbrev_section,
2707 section->buffer + to_underlying (sect_off),
2708 rcuh_kind::TYPE);
2709
2710 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2711 sig_type->signature = cu_header.signature;
2712 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2713 sig_type->per_cu.is_debug_types = 1;
2714 sig_type->per_cu.section = section;
2715 sig_type->per_cu.sect_off = sect_off;
2716 sig_type->per_cu.v.quick
2717 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2718 struct dwarf2_per_cu_quick_data);
2719
2720 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2721 *slot = sig_type;
2722
2723 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2724 }
2725
2726 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2727 }
2728
2729 /* Read the address map data from the mapped index, and use it to
2730 populate the psymtabs_addrmap. */
2731
2732 static void
2733 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2734 struct mapped_index *index)
2735 {
2736 struct objfile *objfile = per_objfile->objfile;
2737 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2738 struct gdbarch *gdbarch = objfile->arch ();
2739 const gdb_byte *iter, *end;
2740 struct addrmap *mutable_map;
2741 CORE_ADDR baseaddr;
2742
2743 auto_obstack temp_obstack;
2744
2745 mutable_map = addrmap_create_mutable (&temp_obstack);
2746
2747 iter = index->address_table.data ();
2748 end = iter + index->address_table.size ();
2749
2750 baseaddr = objfile->text_section_offset ();
2751
2752 while (iter < end)
2753 {
2754 ULONGEST hi, lo, cu_index;
2755 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2756 iter += 8;
2757 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2758 iter += 8;
2759 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2760 iter += 4;
2761
2762 if (lo > hi)
2763 {
2764 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2765 hex_string (lo), hex_string (hi));
2766 continue;
2767 }
2768
2769 if (cu_index >= per_bfd->all_comp_units.size ())
2770 {
2771 complaint (_(".gdb_index address table has invalid CU number %u"),
2772 (unsigned) cu_index);
2773 continue;
2774 }
2775
2776 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2777 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2778 addrmap_set_empty (mutable_map, lo, hi - 1,
2779 per_bfd->get_cu (cu_index));
2780 }
2781
2782 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2783 &per_bfd->obstack);
2784 }
2785
2786 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2787 populate the psymtabs_addrmap. */
2788
2789 static void
2790 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2791 struct dwarf2_section_info *section)
2792 {
2793 struct objfile *objfile = per_objfile->objfile;
2794 bfd *abfd = objfile->obfd;
2795 struct gdbarch *gdbarch = objfile->arch ();
2796 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2797 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2798
2799 auto_obstack temp_obstack;
2800 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2801
2802 std::unordered_map<sect_offset,
2803 dwarf2_per_cu_data *,
2804 gdb::hash_enum<sect_offset>>
2805 debug_info_offset_to_per_cu;
2806 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
2807 {
2808 const auto insertpair
2809 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2810 if (!insertpair.second)
2811 {
2812 warning (_("Section .debug_aranges in %s has duplicate "
2813 "debug_info_offset %s, ignoring .debug_aranges."),
2814 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2815 return;
2816 }
2817 }
2818
2819 section->read (objfile);
2820
2821 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2822
2823 const gdb_byte *addr = section->buffer;
2824
2825 while (addr < section->buffer + section->size)
2826 {
2827 const gdb_byte *const entry_addr = addr;
2828 unsigned int bytes_read;
2829
2830 const LONGEST entry_length = read_initial_length (abfd, addr,
2831 &bytes_read);
2832 addr += bytes_read;
2833
2834 const gdb_byte *const entry_end = addr + entry_length;
2835 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2836 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2837 if (addr + entry_length > section->buffer + section->size)
2838 {
2839 warning (_("Section .debug_aranges in %s entry at offset %s "
2840 "length %s exceeds section length %s, "
2841 "ignoring .debug_aranges."),
2842 objfile_name (objfile),
2843 plongest (entry_addr - section->buffer),
2844 plongest (bytes_read + entry_length),
2845 pulongest (section->size));
2846 return;
2847 }
2848
2849 /* The version number. */
2850 const uint16_t version = read_2_bytes (abfd, addr);
2851 addr += 2;
2852 if (version != 2)
2853 {
2854 warning (_("Section .debug_aranges in %s entry at offset %s "
2855 "has unsupported version %d, ignoring .debug_aranges."),
2856 objfile_name (objfile),
2857 plongest (entry_addr - section->buffer), version);
2858 return;
2859 }
2860
2861 const uint64_t debug_info_offset
2862 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2863 addr += offset_size;
2864 const auto per_cu_it
2865 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2866 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2867 {
2868 warning (_("Section .debug_aranges in %s entry at offset %s "
2869 "debug_info_offset %s does not exists, "
2870 "ignoring .debug_aranges."),
2871 objfile_name (objfile),
2872 plongest (entry_addr - section->buffer),
2873 pulongest (debug_info_offset));
2874 return;
2875 }
2876 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2877
2878 const uint8_t address_size = *addr++;
2879 if (address_size < 1 || address_size > 8)
2880 {
2881 warning (_("Section .debug_aranges in %s entry at offset %s "
2882 "address_size %u is invalid, ignoring .debug_aranges."),
2883 objfile_name (objfile),
2884 plongest (entry_addr - section->buffer), address_size);
2885 return;
2886 }
2887
2888 const uint8_t segment_selector_size = *addr++;
2889 if (segment_selector_size != 0)
2890 {
2891 warning (_("Section .debug_aranges in %s entry at offset %s "
2892 "segment_selector_size %u is not supported, "
2893 "ignoring .debug_aranges."),
2894 objfile_name (objfile),
2895 plongest (entry_addr - section->buffer),
2896 segment_selector_size);
2897 return;
2898 }
2899
2900 /* Must pad to an alignment boundary that is twice the address
2901 size. It is undocumented by the DWARF standard but GCC does
2902 use it. */
2903 for (size_t padding = ((-(addr - section->buffer))
2904 & (2 * address_size - 1));
2905 padding > 0; padding--)
2906 if (*addr++ != 0)
2907 {
2908 warning (_("Section .debug_aranges in %s entry at offset %s "
2909 "padding is not zero, ignoring .debug_aranges."),
2910 objfile_name (objfile),
2911 plongest (entry_addr - section->buffer));
2912 return;
2913 }
2914
2915 for (;;)
2916 {
2917 if (addr + 2 * address_size > entry_end)
2918 {
2919 warning (_("Section .debug_aranges in %s entry at offset %s "
2920 "address list is not properly terminated, "
2921 "ignoring .debug_aranges."),
2922 objfile_name (objfile),
2923 plongest (entry_addr - section->buffer));
2924 return;
2925 }
2926 ULONGEST start = extract_unsigned_integer (addr, address_size,
2927 dwarf5_byte_order);
2928 addr += address_size;
2929 ULONGEST length = extract_unsigned_integer (addr, address_size,
2930 dwarf5_byte_order);
2931 addr += address_size;
2932 if (start == 0 && length == 0)
2933 break;
2934 if (start == 0 && !per_bfd->has_section_at_zero)
2935 {
2936 /* Symbol was eliminated due to a COMDAT group. */
2937 continue;
2938 }
2939 ULONGEST end = start + length;
2940 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2941 - baseaddr);
2942 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2943 - baseaddr);
2944 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2945 }
2946 }
2947
2948 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2949 &per_bfd->obstack);
2950 }
2951
2952 /* Find a slot in the mapped index INDEX for the object named NAME.
2953 If NAME is found, set *VEC_OUT to point to the CU vector in the
2954 constant pool and return true. If NAME cannot be found, return
2955 false. */
2956
2957 static bool
2958 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2959 offset_type **vec_out)
2960 {
2961 offset_type hash;
2962 offset_type slot, step;
2963 int (*cmp) (const char *, const char *);
2964
2965 gdb::unique_xmalloc_ptr<char> without_params;
2966 if (current_language->la_language == language_cplus
2967 || current_language->la_language == language_fortran
2968 || current_language->la_language == language_d)
2969 {
2970 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2971 not contain any. */
2972
2973 if (strchr (name, '(') != NULL)
2974 {
2975 without_params = cp_remove_params (name);
2976
2977 if (without_params != NULL)
2978 name = without_params.get ();
2979 }
2980 }
2981
2982 /* Index version 4 did not support case insensitive searches. But the
2983 indices for case insensitive languages are built in lowercase, therefore
2984 simulate our NAME being searched is also lowercased. */
2985 hash = mapped_index_string_hash ((index->version == 4
2986 && case_sensitivity == case_sensitive_off
2987 ? 5 : index->version),
2988 name);
2989
2990 slot = hash & (index->symbol_table.size () - 1);
2991 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2992 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2993
2994 for (;;)
2995 {
2996 const char *str;
2997
2998 const auto &bucket = index->symbol_table[slot];
2999 if (bucket.name == 0 && bucket.vec == 0)
3000 return false;
3001
3002 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3003 if (!cmp (name, str))
3004 {
3005 *vec_out = (offset_type *) (index->constant_pool
3006 + MAYBE_SWAP (bucket.vec));
3007 return true;
3008 }
3009
3010 slot = (slot + step) & (index->symbol_table.size () - 1);
3011 }
3012 }
3013
3014 /* A helper function that reads the .gdb_index from BUFFER and fills
3015 in MAP. FILENAME is the name of the file containing the data;
3016 it is used for error reporting. DEPRECATED_OK is true if it is
3017 ok to use deprecated sections.
3018
3019 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3020 out parameters that are filled in with information about the CU and
3021 TU lists in the section.
3022
3023 Returns true if all went well, false otherwise. */
3024
3025 static bool
3026 read_gdb_index_from_buffer (const char *filename,
3027 bool deprecated_ok,
3028 gdb::array_view<const gdb_byte> buffer,
3029 struct mapped_index *map,
3030 const gdb_byte **cu_list,
3031 offset_type *cu_list_elements,
3032 const gdb_byte **types_list,
3033 offset_type *types_list_elements)
3034 {
3035 const gdb_byte *addr = &buffer[0];
3036
3037 /* Version check. */
3038 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3039 /* Versions earlier than 3 emitted every copy of a psymbol. This
3040 causes the index to behave very poorly for certain requests. Version 3
3041 contained incomplete addrmap. So, it seems better to just ignore such
3042 indices. */
3043 if (version < 4)
3044 {
3045 static int warning_printed = 0;
3046 if (!warning_printed)
3047 {
3048 warning (_("Skipping obsolete .gdb_index section in %s."),
3049 filename);
3050 warning_printed = 1;
3051 }
3052 return 0;
3053 }
3054 /* Index version 4 uses a different hash function than index version
3055 5 and later.
3056
3057 Versions earlier than 6 did not emit psymbols for inlined
3058 functions. Using these files will cause GDB not to be able to
3059 set breakpoints on inlined functions by name, so we ignore these
3060 indices unless the user has done
3061 "set use-deprecated-index-sections on". */
3062 if (version < 6 && !deprecated_ok)
3063 {
3064 static int warning_printed = 0;
3065 if (!warning_printed)
3066 {
3067 warning (_("\
3068 Skipping deprecated .gdb_index section in %s.\n\
3069 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3070 to use the section anyway."),
3071 filename);
3072 warning_printed = 1;
3073 }
3074 return 0;
3075 }
3076 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3077 of the TU (for symbols coming from TUs),
3078 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3079 Plus gold-generated indices can have duplicate entries for global symbols,
3080 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3081 These are just performance bugs, and we can't distinguish gdb-generated
3082 indices from gold-generated ones, so issue no warning here. */
3083
3084 /* Indexes with higher version than the one supported by GDB may be no
3085 longer backward compatible. */
3086 if (version > 8)
3087 return 0;
3088
3089 map->version = version;
3090
3091 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3092
3093 int i = 0;
3094 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3095 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3096 / 8);
3097 ++i;
3098
3099 *types_list = addr + MAYBE_SWAP (metadata[i]);
3100 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3101 - MAYBE_SWAP (metadata[i]))
3102 / 8);
3103 ++i;
3104
3105 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3106 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3107 map->address_table
3108 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3109 ++i;
3110
3111 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3112 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3113 map->symbol_table
3114 = gdb::array_view<mapped_index::symbol_table_slot>
3115 ((mapped_index::symbol_table_slot *) symbol_table,
3116 (mapped_index::symbol_table_slot *) symbol_table_end);
3117
3118 ++i;
3119 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3120
3121 return 1;
3122 }
3123
3124 /* Callback types for dwarf2_read_gdb_index. */
3125
3126 typedef gdb::function_view
3127 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3128 get_gdb_index_contents_ftype;
3129 typedef gdb::function_view
3130 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3131 get_gdb_index_contents_dwz_ftype;
3132
3133 /* Read .gdb_index. If everything went ok, initialize the "quick"
3134 elements of all the CUs and return 1. Otherwise, return 0. */
3135
3136 static int
3137 dwarf2_read_gdb_index
3138 (dwarf2_per_objfile *per_objfile,
3139 get_gdb_index_contents_ftype get_gdb_index_contents,
3140 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3141 {
3142 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3143 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3144 struct dwz_file *dwz;
3145 struct objfile *objfile = per_objfile->objfile;
3146 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3147
3148 gdb::array_view<const gdb_byte> main_index_contents
3149 = get_gdb_index_contents (objfile, per_bfd);
3150
3151 if (main_index_contents.empty ())
3152 return 0;
3153
3154 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3155 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3156 use_deprecated_index_sections,
3157 main_index_contents, map.get (), &cu_list,
3158 &cu_list_elements, &types_list,
3159 &types_list_elements))
3160 return 0;
3161
3162 /* Don't use the index if it's empty. */
3163 if (map->symbol_table.empty ())
3164 return 0;
3165
3166 /* If there is a .dwz file, read it so we can get its CU list as
3167 well. */
3168 dwz = dwarf2_get_dwz_file (per_bfd);
3169 if (dwz != NULL)
3170 {
3171 struct mapped_index dwz_map;
3172 const gdb_byte *dwz_types_ignore;
3173 offset_type dwz_types_elements_ignore;
3174
3175 gdb::array_view<const gdb_byte> dwz_index_content
3176 = get_gdb_index_contents_dwz (objfile, dwz);
3177
3178 if (dwz_index_content.empty ())
3179 return 0;
3180
3181 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3182 1, dwz_index_content, &dwz_map,
3183 &dwz_list, &dwz_list_elements,
3184 &dwz_types_ignore,
3185 &dwz_types_elements_ignore))
3186 {
3187 warning (_("could not read '.gdb_index' section from %s; skipping"),
3188 bfd_get_filename (dwz->dwz_bfd.get ()));
3189 return 0;
3190 }
3191 }
3192
3193 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3194 dwz_list_elements);
3195
3196 if (types_list_elements)
3197 {
3198 /* We can only handle a single .debug_types when we have an
3199 index. */
3200 if (per_bfd->types.size () != 1)
3201 return 0;
3202
3203 dwarf2_section_info *section = &per_bfd->types[0];
3204
3205 create_signatured_type_table_from_index (per_bfd, section, types_list,
3206 types_list_elements);
3207 }
3208
3209 create_addrmap_from_index (per_objfile, map.get ());
3210
3211 per_bfd->index_table = std::move (map);
3212 per_bfd->using_index = 1;
3213 per_bfd->quick_file_names_table =
3214 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3215
3216 return 1;
3217 }
3218
3219 /* die_reader_func for dw2_get_file_names. */
3220
3221 static void
3222 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3223 struct die_info *comp_unit_die)
3224 {
3225 struct dwarf2_cu *cu = reader->cu;
3226 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3227 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3228 struct dwarf2_per_cu_data *lh_cu;
3229 struct attribute *attr;
3230 void **slot;
3231 struct quick_file_names *qfn;
3232
3233 gdb_assert (! this_cu->is_debug_types);
3234
3235 /* Our callers never want to match partial units -- instead they
3236 will match the enclosing full CU. */
3237 if (comp_unit_die->tag == DW_TAG_partial_unit)
3238 {
3239 this_cu->v.quick->no_file_data = 1;
3240 return;
3241 }
3242
3243 lh_cu = this_cu;
3244 slot = NULL;
3245
3246 line_header_up lh;
3247 sect_offset line_offset {};
3248
3249 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3250 if (attr != nullptr && attr->form_is_unsigned ())
3251 {
3252 struct quick_file_names find_entry;
3253
3254 line_offset = (sect_offset) attr->as_unsigned ();
3255
3256 /* We may have already read in this line header (TU line header sharing).
3257 If we have we're done. */
3258 find_entry.hash.dwo_unit = cu->dwo_unit;
3259 find_entry.hash.line_sect_off = line_offset;
3260 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3261 &find_entry, INSERT);
3262 if (*slot != NULL)
3263 {
3264 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3265 return;
3266 }
3267
3268 lh = dwarf_decode_line_header (line_offset, cu);
3269 }
3270 if (lh == NULL)
3271 {
3272 lh_cu->v.quick->no_file_data = 1;
3273 return;
3274 }
3275
3276 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3277 qfn->hash.dwo_unit = cu->dwo_unit;
3278 qfn->hash.line_sect_off = line_offset;
3279 gdb_assert (slot != NULL);
3280 *slot = qfn;
3281
3282 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3283
3284 int offset = 0;
3285 if (strcmp (fnd.name, "<unknown>") != 0)
3286 ++offset;
3287
3288 qfn->num_file_names = offset + lh->file_names_size ();
3289 qfn->file_names =
3290 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3291 qfn->num_file_names);
3292 if (offset != 0)
3293 qfn->file_names[0] = xstrdup (fnd.name);
3294 for (int i = 0; i < lh->file_names_size (); ++i)
3295 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3296 fnd.comp_dir).release ();
3297 qfn->real_names = NULL;
3298
3299 lh_cu->v.quick->file_names = qfn;
3300 }
3301
3302 /* A helper for the "quick" functions which attempts to read the line
3303 table for THIS_CU. */
3304
3305 static struct quick_file_names *
3306 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3307 dwarf2_per_objfile *per_objfile)
3308 {
3309 /* This should never be called for TUs. */
3310 gdb_assert (! this_cu->is_debug_types);
3311 /* Nor type unit groups. */
3312 gdb_assert (! this_cu->type_unit_group_p ());
3313
3314 if (this_cu->v.quick->file_names != NULL)
3315 return this_cu->v.quick->file_names;
3316 /* If we know there is no line data, no point in looking again. */
3317 if (this_cu->v.quick->no_file_data)
3318 return NULL;
3319
3320 cutu_reader reader (this_cu, per_objfile);
3321 if (!reader.dummy_p)
3322 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3323
3324 if (this_cu->v.quick->no_file_data)
3325 return NULL;
3326 return this_cu->v.quick->file_names;
3327 }
3328
3329 /* A helper for the "quick" functions which computes and caches the
3330 real path for a given file name from the line table. */
3331
3332 static const char *
3333 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3334 struct quick_file_names *qfn, int index)
3335 {
3336 if (qfn->real_names == NULL)
3337 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3338 qfn->num_file_names, const char *);
3339
3340 if (qfn->real_names[index] == NULL)
3341 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3342
3343 return qfn->real_names[index];
3344 }
3345
3346 struct symtab *
3347 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3348 {
3349 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3350 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3351 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3352
3353 if (cust == NULL)
3354 return NULL;
3355
3356 return compunit_primary_filetab (cust);
3357 }
3358
3359 /* Traversal function for dw2_forget_cached_source_info. */
3360
3361 static int
3362 dw2_free_cached_file_names (void **slot, void *info)
3363 {
3364 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3365
3366 if (file_data->real_names)
3367 {
3368 int i;
3369
3370 for (i = 0; i < file_data->num_file_names; ++i)
3371 {
3372 xfree ((void*) file_data->real_names[i]);
3373 file_data->real_names[i] = NULL;
3374 }
3375 }
3376
3377 return 1;
3378 }
3379
3380 void
3381 dwarf2_base_index_functions::forget_cached_source_info
3382 (struct objfile *objfile)
3383 {
3384 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3385
3386 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3387 dw2_free_cached_file_names, NULL);
3388 }
3389
3390 /* Helper function for dw2_map_symtabs_matching_filename that expands
3391 the symtabs and calls the iterator. */
3392
3393 static int
3394 dw2_map_expand_apply (struct objfile *objfile,
3395 struct dwarf2_per_cu_data *per_cu,
3396 const char *name, const char *real_path,
3397 gdb::function_view<bool (symtab *)> callback)
3398 {
3399 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3400
3401 /* Don't visit already-expanded CUs. */
3402 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3403 if (per_objfile->symtab_set_p (per_cu))
3404 return 0;
3405
3406 /* This may expand more than one symtab, and we want to iterate over
3407 all of them. */
3408 dw2_instantiate_symtab (per_cu, per_objfile, false);
3409
3410 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3411 last_made, callback);
3412 }
3413
3414 /* Implementation of the map_symtabs_matching_filename method. */
3415
3416 bool
3417 dwarf2_base_index_functions::map_symtabs_matching_filename
3418 (struct objfile *objfile, const char *name, const char *real_path,
3419 gdb::function_view<bool (symtab *)> callback)
3420 {
3421 const char *name_basename = lbasename (name);
3422 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3423
3424 /* The rule is CUs specify all the files, including those used by
3425 any TU, so there's no need to scan TUs here. */
3426
3427 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3428 {
3429 /* We only need to look at symtabs not already expanded. */
3430 if (per_objfile->symtab_set_p (per_cu))
3431 continue;
3432
3433 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3434 if (file_data == NULL)
3435 continue;
3436
3437 for (int j = 0; j < file_data->num_file_names; ++j)
3438 {
3439 const char *this_name = file_data->file_names[j];
3440 const char *this_real_name;
3441
3442 if (compare_filenames_for_search (this_name, name))
3443 {
3444 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3445 callback))
3446 return true;
3447 continue;
3448 }
3449
3450 /* Before we invoke realpath, which can get expensive when many
3451 files are involved, do a quick comparison of the basenames. */
3452 if (! basenames_may_differ
3453 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3454 continue;
3455
3456 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3457 if (compare_filenames_for_search (this_real_name, name))
3458 {
3459 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3460 callback))
3461 return true;
3462 continue;
3463 }
3464
3465 if (real_path != NULL)
3466 {
3467 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3468 gdb_assert (IS_ABSOLUTE_PATH (name));
3469 if (this_real_name != NULL
3470 && FILENAME_CMP (real_path, this_real_name) == 0)
3471 {
3472 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3473 callback))
3474 return true;
3475 continue;
3476 }
3477 }
3478 }
3479 }
3480
3481 return false;
3482 }
3483
3484 /* Struct used to manage iterating over all CUs looking for a symbol. */
3485
3486 struct dw2_symtab_iterator
3487 {
3488 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3489 dwarf2_per_objfile *per_objfile;
3490 /* If set, only look for symbols that match that block. Valid values are
3491 GLOBAL_BLOCK and STATIC_BLOCK. */
3492 gdb::optional<block_enum> block_index;
3493 /* The kind of symbol we're looking for. */
3494 domain_enum domain;
3495 /* The list of CUs from the index entry of the symbol,
3496 or NULL if not found. */
3497 offset_type *vec;
3498 /* The next element in VEC to look at. */
3499 int next;
3500 /* The number of elements in VEC, or zero if there is no match. */
3501 int length;
3502 /* Have we seen a global version of the symbol?
3503 If so we can ignore all further global instances.
3504 This is to work around gold/15646, inefficient gold-generated
3505 indices. */
3506 int global_seen;
3507 };
3508
3509 /* Initialize the index symtab iterator ITER, common part. */
3510
3511 static void
3512 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3513 dwarf2_per_objfile *per_objfile,
3514 gdb::optional<block_enum> block_index,
3515 domain_enum domain)
3516 {
3517 iter->per_objfile = per_objfile;
3518 iter->block_index = block_index;
3519 iter->domain = domain;
3520 iter->next = 0;
3521 iter->global_seen = 0;
3522 iter->vec = NULL;
3523 iter->length = 0;
3524 }
3525
3526 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3527
3528 static void
3529 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3530 dwarf2_per_objfile *per_objfile,
3531 gdb::optional<block_enum> block_index,
3532 domain_enum domain,
3533 const char *name)
3534 {
3535 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3536
3537 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3538 /* index is NULL if OBJF_READNOW. */
3539 if (index == NULL)
3540 return;
3541
3542 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3543 iter->length = MAYBE_SWAP (*iter->vec);
3544 }
3545
3546 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3547
3548 static void
3549 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3550 dwarf2_per_objfile *per_objfile,
3551 gdb::optional<block_enum> block_index,
3552 domain_enum domain, offset_type namei)
3553 {
3554 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3555
3556 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3557 /* index is NULL if OBJF_READNOW. */
3558 if (index == NULL)
3559 return;
3560
3561 gdb_assert (!index->symbol_name_slot_invalid (namei));
3562 const auto &bucket = index->symbol_table[namei];
3563
3564 iter->vec = (offset_type *) (index->constant_pool
3565 + MAYBE_SWAP (bucket.vec));
3566 iter->length = MAYBE_SWAP (*iter->vec);
3567 }
3568
3569 /* Return the next matching CU or NULL if there are no more. */
3570
3571 static struct dwarf2_per_cu_data *
3572 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3573 {
3574 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3575
3576 for ( ; iter->next < iter->length; ++iter->next)
3577 {
3578 offset_type cu_index_and_attrs =
3579 MAYBE_SWAP (iter->vec[iter->next + 1]);
3580 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3581 gdb_index_symbol_kind symbol_kind =
3582 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3583 /* Only check the symbol attributes if they're present.
3584 Indices prior to version 7 don't record them,
3585 and indices >= 7 may elide them for certain symbols
3586 (gold does this). */
3587 int attrs_valid =
3588 (per_objfile->per_bfd->index_table->version >= 7
3589 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3590
3591 /* Don't crash on bad data. */
3592 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3593 + per_objfile->per_bfd->all_type_units.size ()))
3594 {
3595 complaint (_(".gdb_index entry has bad CU index"
3596 " [in module %s]"), objfile_name (per_objfile->objfile));
3597 continue;
3598 }
3599
3600 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3601
3602 /* Skip if already read in. */
3603 if (per_objfile->symtab_set_p (per_cu))
3604 continue;
3605
3606 /* Check static vs global. */
3607 if (attrs_valid)
3608 {
3609 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3610
3611 if (iter->block_index.has_value ())
3612 {
3613 bool want_static = *iter->block_index == STATIC_BLOCK;
3614
3615 if (is_static != want_static)
3616 continue;
3617 }
3618
3619 /* Work around gold/15646. */
3620 if (!is_static
3621 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3622 {
3623 if (iter->global_seen)
3624 continue;
3625
3626 iter->global_seen = 1;
3627 }
3628 }
3629
3630 /* Only check the symbol's kind if it has one. */
3631 if (attrs_valid)
3632 {
3633 switch (iter->domain)
3634 {
3635 case VAR_DOMAIN:
3636 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3637 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3638 /* Some types are also in VAR_DOMAIN. */
3639 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3640 continue;
3641 break;
3642 case STRUCT_DOMAIN:
3643 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3644 continue;
3645 break;
3646 case LABEL_DOMAIN:
3647 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3648 continue;
3649 break;
3650 case MODULE_DOMAIN:
3651 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3652 continue;
3653 break;
3654 default:
3655 break;
3656 }
3657 }
3658
3659 ++iter->next;
3660 return per_cu;
3661 }
3662
3663 return NULL;
3664 }
3665
3666 struct compunit_symtab *
3667 dwarf2_gdb_index::lookup_symbol (struct objfile *objfile,
3668 block_enum block_index,
3669 const char *name, domain_enum domain)
3670 {
3671 struct compunit_symtab *stab_best = NULL;
3672 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3673
3674 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3675
3676 struct dw2_symtab_iterator iter;
3677 struct dwarf2_per_cu_data *per_cu;
3678
3679 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3680
3681 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3682 {
3683 struct symbol *sym, *with_opaque = NULL;
3684 struct compunit_symtab *stab
3685 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3686 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3687 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3688
3689 sym = block_find_symbol (block, name, domain,
3690 block_find_non_opaque_type_preferred,
3691 &with_opaque);
3692
3693 /* Some caution must be observed with overloaded functions
3694 and methods, since the index will not contain any overload
3695 information (but NAME might contain it). */
3696
3697 if (sym != NULL
3698 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3699 return stab;
3700 if (with_opaque != NULL
3701 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3702 stab_best = stab;
3703
3704 /* Keep looking through other CUs. */
3705 }
3706
3707 return stab_best;
3708 }
3709
3710 void
3711 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3712 bool print_bcache)
3713 {
3714 if (print_bcache)
3715 return;
3716
3717 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3718 int total = (per_objfile->per_bfd->all_comp_units.size ()
3719 + per_objfile->per_bfd->all_type_units.size ());
3720 int count = 0;
3721
3722 for (int i = 0; i < total; ++i)
3723 {
3724 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3725
3726 if (!per_objfile->symtab_set_p (per_cu))
3727 ++count;
3728 }
3729 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3730 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3731 }
3732
3733 /* This dumps minimal information about the index.
3734 It is called via "mt print objfiles".
3735 One use is to verify .gdb_index has been loaded by the
3736 gdb.dwarf2/gdb-index.exp testcase. */
3737
3738 void
3739 dwarf2_gdb_index::dump (struct objfile *objfile)
3740 {
3741 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3742
3743 gdb_assert (per_objfile->per_bfd->using_index);
3744 printf_filtered (".gdb_index:");
3745 if (per_objfile->per_bfd->index_table != NULL)
3746 {
3747 printf_filtered (" version %d\n",
3748 per_objfile->per_bfd->index_table->version);
3749 }
3750 else
3751 printf_filtered (" faked for \"readnow\"\n");
3752 printf_filtered ("\n");
3753 }
3754
3755 void
3756 dwarf2_gdb_index::expand_symtabs_for_function (struct objfile *objfile,
3757 const char *func_name)
3758 {
3759 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3760
3761 struct dw2_symtab_iterator iter;
3762 struct dwarf2_per_cu_data *per_cu;
3763
3764 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3765
3766 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3767 dw2_instantiate_symtab (per_cu, per_objfile, false);
3768
3769 }
3770
3771 void
3772 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3773 {
3774 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3775 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3776 + per_objfile->per_bfd->all_type_units.size ());
3777
3778 for (int i = 0; i < total_units; ++i)
3779 {
3780 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3781
3782 /* We don't want to directly expand a partial CU, because if we
3783 read it with the wrong language, then assertion failures can
3784 be triggered later on. See PR symtab/23010. So, tell
3785 dw2_instantiate_symtab to skip partial CUs -- any important
3786 partial CU will be read via DW_TAG_imported_unit anyway. */
3787 dw2_instantiate_symtab (per_cu, per_objfile, true);
3788 }
3789 }
3790
3791 void
3792 dwarf2_base_index_functions::expand_symtabs_with_fullname
3793 (struct objfile *objfile, const char *fullname)
3794 {
3795 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3796
3797 /* We don't need to consider type units here.
3798 This is only called for examining code, e.g. expand_line_sal.
3799 There can be an order of magnitude (or more) more type units
3800 than comp units, and we avoid them if we can. */
3801
3802 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3803 {
3804 /* We only need to look at symtabs not already expanded. */
3805 if (per_objfile->symtab_set_p (per_cu))
3806 continue;
3807
3808 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3809 if (file_data == NULL)
3810 continue;
3811
3812 for (int j = 0; j < file_data->num_file_names; ++j)
3813 {
3814 const char *this_fullname = file_data->file_names[j];
3815
3816 if (filename_cmp (this_fullname, fullname) == 0)
3817 {
3818 dw2_instantiate_symtab (per_cu, per_objfile, false);
3819 break;
3820 }
3821 }
3822 }
3823 }
3824
3825 static void
3826 dw2_expand_symtabs_matching_symbol
3827 (mapped_index_base &index,
3828 const lookup_name_info &lookup_name_in,
3829 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3830 gdb::function_view<bool (offset_type)> match_callback,
3831 dwarf2_per_objfile *per_objfile);
3832
3833 static void
3834 dw2_expand_symtabs_matching_one
3835 (dwarf2_per_cu_data *per_cu,
3836 dwarf2_per_objfile *per_objfile,
3837 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3838 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3839
3840 void
3841 dwarf2_gdb_index::map_matching_symbols
3842 (struct objfile *objfile,
3843 const lookup_name_info &name, domain_enum domain,
3844 int global,
3845 gdb::function_view<symbol_found_callback_ftype> callback,
3846 symbol_compare_ftype *ordered_compare)
3847 {
3848 /* Used for Ada. */
3849 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3850
3851 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3852
3853 if (per_objfile->per_bfd->index_table != nullptr)
3854 {
3855 mapped_index &index = *per_objfile->per_bfd->index_table;
3856
3857 const char *match_name = name.ada ().lookup_name ().c_str ();
3858 auto matcher = [&] (const char *symname)
3859 {
3860 if (ordered_compare == nullptr)
3861 return true;
3862 return ordered_compare (symname, match_name) == 0;
3863 };
3864
3865 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3866 [&] (offset_type namei)
3867 {
3868 struct dw2_symtab_iterator iter;
3869 struct dwarf2_per_cu_data *per_cu;
3870
3871 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3872 namei);
3873 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3874 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3875 nullptr);
3876 return true;
3877 }, per_objfile);
3878 }
3879 else
3880 {
3881 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3882 proceed assuming all symtabs have been read in. */
3883 }
3884
3885 for (compunit_symtab *cust : objfile->compunits ())
3886 {
3887 const struct block *block;
3888
3889 if (cust == NULL)
3890 continue;
3891 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3892 if (!iterate_over_symbols_terminated (block, name,
3893 domain, callback))
3894 return;
3895 }
3896 }
3897
3898 /* Starting from a search name, return the string that finds the upper
3899 bound of all strings that start with SEARCH_NAME in a sorted name
3900 list. Returns the empty string to indicate that the upper bound is
3901 the end of the list. */
3902
3903 static std::string
3904 make_sort_after_prefix_name (const char *search_name)
3905 {
3906 /* When looking to complete "func", we find the upper bound of all
3907 symbols that start with "func" by looking for where we'd insert
3908 the closest string that would follow "func" in lexicographical
3909 order. Usually, that's "func"-with-last-character-incremented,
3910 i.e. "fund". Mind non-ASCII characters, though. Usually those
3911 will be UTF-8 multi-byte sequences, but we can't be certain.
3912 Especially mind the 0xff character, which is a valid character in
3913 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3914 rule out compilers allowing it in identifiers. Note that
3915 conveniently, strcmp/strcasecmp are specified to compare
3916 characters interpreted as unsigned char. So what we do is treat
3917 the whole string as a base 256 number composed of a sequence of
3918 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3919 to 0, and carries 1 to the following more-significant position.
3920 If the very first character in SEARCH_NAME ends up incremented
3921 and carries/overflows, then the upper bound is the end of the
3922 list. The string after the empty string is also the empty
3923 string.
3924
3925 Some examples of this operation:
3926
3927 SEARCH_NAME => "+1" RESULT
3928
3929 "abc" => "abd"
3930 "ab\xff" => "ac"
3931 "\xff" "a" "\xff" => "\xff" "b"
3932 "\xff" => ""
3933 "\xff\xff" => ""
3934 "" => ""
3935
3936 Then, with these symbols for example:
3937
3938 func
3939 func1
3940 fund
3941
3942 completing "func" looks for symbols between "func" and
3943 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3944 which finds "func" and "func1", but not "fund".
3945
3946 And with:
3947
3948 funcÿ (Latin1 'ÿ' [0xff])
3949 funcÿ1
3950 fund
3951
3952 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3953 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3954
3955 And with:
3956
3957 ÿÿ (Latin1 'ÿ' [0xff])
3958 ÿÿ1
3959
3960 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3961 the end of the list.
3962 */
3963 std::string after = search_name;
3964 while (!after.empty () && (unsigned char) after.back () == 0xff)
3965 after.pop_back ();
3966 if (!after.empty ())
3967 after.back () = (unsigned char) after.back () + 1;
3968 return after;
3969 }
3970
3971 /* See declaration. */
3972
3973 std::pair<std::vector<name_component>::const_iterator,
3974 std::vector<name_component>::const_iterator>
3975 mapped_index_base::find_name_components_bounds
3976 (const lookup_name_info &lookup_name_without_params, language lang,
3977 dwarf2_per_objfile *per_objfile) const
3978 {
3979 auto *name_cmp
3980 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3981
3982 const char *lang_name
3983 = lookup_name_without_params.language_lookup_name (lang);
3984
3985 /* Comparison function object for lower_bound that matches against a
3986 given symbol name. */
3987 auto lookup_compare_lower = [&] (const name_component &elem,
3988 const char *name)
3989 {
3990 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3991 const char *elem_name = elem_qualified + elem.name_offset;
3992 return name_cmp (elem_name, name) < 0;
3993 };
3994
3995 /* Comparison function object for upper_bound that matches against a
3996 given symbol name. */
3997 auto lookup_compare_upper = [&] (const char *name,
3998 const name_component &elem)
3999 {
4000 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
4001 const char *elem_name = elem_qualified + elem.name_offset;
4002 return name_cmp (name, elem_name) < 0;
4003 };
4004
4005 auto begin = this->name_components.begin ();
4006 auto end = this->name_components.end ();
4007
4008 /* Find the lower bound. */
4009 auto lower = [&] ()
4010 {
4011 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4012 return begin;
4013 else
4014 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4015 } ();
4016
4017 /* Find the upper bound. */
4018 auto upper = [&] ()
4019 {
4020 if (lookup_name_without_params.completion_mode ())
4021 {
4022 /* In completion mode, we want UPPER to point past all
4023 symbols names that have the same prefix. I.e., with
4024 these symbols, and completing "func":
4025
4026 function << lower bound
4027 function1
4028 other_function << upper bound
4029
4030 We find the upper bound by looking for the insertion
4031 point of "func"-with-last-character-incremented,
4032 i.e. "fund". */
4033 std::string after = make_sort_after_prefix_name (lang_name);
4034 if (after.empty ())
4035 return end;
4036 return std::lower_bound (lower, end, after.c_str (),
4037 lookup_compare_lower);
4038 }
4039 else
4040 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4041 } ();
4042
4043 return {lower, upper};
4044 }
4045
4046 /* See declaration. */
4047
4048 void
4049 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4050 {
4051 if (!this->name_components.empty ())
4052 return;
4053
4054 this->name_components_casing = case_sensitivity;
4055 auto *name_cmp
4056 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4057
4058 /* The code below only knows how to break apart components of C++
4059 symbol names (and other languages that use '::' as
4060 namespace/module separator) and Ada symbol names. */
4061 auto count = this->symbol_name_count ();
4062 for (offset_type idx = 0; idx < count; idx++)
4063 {
4064 if (this->symbol_name_slot_invalid (idx))
4065 continue;
4066
4067 const char *name = this->symbol_name_at (idx, per_objfile);
4068
4069 /* Add each name component to the name component table. */
4070 unsigned int previous_len = 0;
4071
4072 if (strstr (name, "::") != nullptr)
4073 {
4074 for (unsigned int current_len = cp_find_first_component (name);
4075 name[current_len] != '\0';
4076 current_len += cp_find_first_component (name + current_len))
4077 {
4078 gdb_assert (name[current_len] == ':');
4079 this->name_components.push_back ({previous_len, idx});
4080 /* Skip the '::'. */
4081 current_len += 2;
4082 previous_len = current_len;
4083 }
4084 }
4085 else
4086 {
4087 /* Handle the Ada encoded (aka mangled) form here. */
4088 for (const char *iter = strstr (name, "__");
4089 iter != nullptr;
4090 iter = strstr (iter, "__"))
4091 {
4092 this->name_components.push_back ({previous_len, idx});
4093 iter += 2;
4094 previous_len = iter - name;
4095 }
4096 }
4097
4098 this->name_components.push_back ({previous_len, idx});
4099 }
4100
4101 /* Sort name_components elements by name. */
4102 auto name_comp_compare = [&] (const name_component &left,
4103 const name_component &right)
4104 {
4105 const char *left_qualified
4106 = this->symbol_name_at (left.idx, per_objfile);
4107 const char *right_qualified
4108 = this->symbol_name_at (right.idx, per_objfile);
4109
4110 const char *left_name = left_qualified + left.name_offset;
4111 const char *right_name = right_qualified + right.name_offset;
4112
4113 return name_cmp (left_name, right_name) < 0;
4114 };
4115
4116 std::sort (this->name_components.begin (),
4117 this->name_components.end (),
4118 name_comp_compare);
4119 }
4120
4121 /* Helper for dw2_expand_symtabs_matching that works with a
4122 mapped_index_base instead of the containing objfile. This is split
4123 to a separate function in order to be able to unit test the
4124 name_components matching using a mock mapped_index_base. For each
4125 symbol name that matches, calls MATCH_CALLBACK, passing it the
4126 symbol's index in the mapped_index_base symbol table. */
4127
4128 static void
4129 dw2_expand_symtabs_matching_symbol
4130 (mapped_index_base &index,
4131 const lookup_name_info &lookup_name_in,
4132 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4133 gdb::function_view<bool (offset_type)> match_callback,
4134 dwarf2_per_objfile *per_objfile)
4135 {
4136 lookup_name_info lookup_name_without_params
4137 = lookup_name_in.make_ignore_params ();
4138
4139 /* Build the symbol name component sorted vector, if we haven't
4140 yet. */
4141 index.build_name_components (per_objfile);
4142
4143 /* The same symbol may appear more than once in the range though.
4144 E.g., if we're looking for symbols that complete "w", and we have
4145 a symbol named "w1::w2", we'll find the two name components for
4146 that same symbol in the range. To be sure we only call the
4147 callback once per symbol, we first collect the symbol name
4148 indexes that matched in a temporary vector and ignore
4149 duplicates. */
4150 std::vector<offset_type> matches;
4151
4152 struct name_and_matcher
4153 {
4154 symbol_name_matcher_ftype *matcher;
4155 const char *name;
4156
4157 bool operator== (const name_and_matcher &other) const
4158 {
4159 return matcher == other.matcher && strcmp (name, other.name) == 0;
4160 }
4161 };
4162
4163 /* A vector holding all the different symbol name matchers, for all
4164 languages. */
4165 std::vector<name_and_matcher> matchers;
4166
4167 for (int i = 0; i < nr_languages; i++)
4168 {
4169 enum language lang_e = (enum language) i;
4170
4171 const language_defn *lang = language_def (lang_e);
4172 symbol_name_matcher_ftype *name_matcher
4173 = lang->get_symbol_name_matcher (lookup_name_without_params);
4174
4175 name_and_matcher key {
4176 name_matcher,
4177 lookup_name_without_params.language_lookup_name (lang_e)
4178 };
4179
4180 /* Don't insert the same comparison routine more than once.
4181 Note that we do this linear walk. This is not a problem in
4182 practice because the number of supported languages is
4183 low. */
4184 if (std::find (matchers.begin (), matchers.end (), key)
4185 != matchers.end ())
4186 continue;
4187 matchers.push_back (std::move (key));
4188
4189 auto bounds
4190 = index.find_name_components_bounds (lookup_name_without_params,
4191 lang_e, per_objfile);
4192
4193 /* Now for each symbol name in range, check to see if we have a name
4194 match, and if so, call the MATCH_CALLBACK callback. */
4195
4196 for (; bounds.first != bounds.second; ++bounds.first)
4197 {
4198 const char *qualified
4199 = index.symbol_name_at (bounds.first->idx, per_objfile);
4200
4201 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4202 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4203 continue;
4204
4205 matches.push_back (bounds.first->idx);
4206 }
4207 }
4208
4209 std::sort (matches.begin (), matches.end ());
4210
4211 /* Finally call the callback, once per match. */
4212 ULONGEST prev = -1;
4213 for (offset_type idx : matches)
4214 {
4215 if (prev != idx)
4216 {
4217 if (!match_callback (idx))
4218 break;
4219 prev = idx;
4220 }
4221 }
4222
4223 /* Above we use a type wider than idx's for 'prev', since 0 and
4224 (offset_type)-1 are both possible values. */
4225 static_assert (sizeof (prev) > sizeof (offset_type), "");
4226 }
4227
4228 #if GDB_SELF_TEST
4229
4230 namespace selftests { namespace dw2_expand_symtabs_matching {
4231
4232 /* A mock .gdb_index/.debug_names-like name index table, enough to
4233 exercise dw2_expand_symtabs_matching_symbol, which works with the
4234 mapped_index_base interface. Builds an index from the symbol list
4235 passed as parameter to the constructor. */
4236 class mock_mapped_index : public mapped_index_base
4237 {
4238 public:
4239 mock_mapped_index (gdb::array_view<const char *> symbols)
4240 : m_symbol_table (symbols)
4241 {}
4242
4243 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4244
4245 /* Return the number of names in the symbol table. */
4246 size_t symbol_name_count () const override
4247 {
4248 return m_symbol_table.size ();
4249 }
4250
4251 /* Get the name of the symbol at IDX in the symbol table. */
4252 const char *symbol_name_at
4253 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4254 {
4255 return m_symbol_table[idx];
4256 }
4257
4258 private:
4259 gdb::array_view<const char *> m_symbol_table;
4260 };
4261
4262 /* Convenience function that converts a NULL pointer to a "<null>"
4263 string, to pass to print routines. */
4264
4265 static const char *
4266 string_or_null (const char *str)
4267 {
4268 return str != NULL ? str : "<null>";
4269 }
4270
4271 /* Check if a lookup_name_info built from
4272 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4273 index. EXPECTED_LIST is the list of expected matches, in expected
4274 matching order. If no match expected, then an empty list is
4275 specified. Returns true on success. On failure prints a warning
4276 indicating the file:line that failed, and returns false. */
4277
4278 static bool
4279 check_match (const char *file, int line,
4280 mock_mapped_index &mock_index,
4281 const char *name, symbol_name_match_type match_type,
4282 bool completion_mode,
4283 std::initializer_list<const char *> expected_list,
4284 dwarf2_per_objfile *per_objfile)
4285 {
4286 lookup_name_info lookup_name (name, match_type, completion_mode);
4287
4288 bool matched = true;
4289
4290 auto mismatch = [&] (const char *expected_str,
4291 const char *got)
4292 {
4293 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4294 "expected=\"%s\", got=\"%s\"\n"),
4295 file, line,
4296 (match_type == symbol_name_match_type::FULL
4297 ? "FULL" : "WILD"),
4298 name, string_or_null (expected_str), string_or_null (got));
4299 matched = false;
4300 };
4301
4302 auto expected_it = expected_list.begin ();
4303 auto expected_end = expected_list.end ();
4304
4305 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4306 nullptr,
4307 [&] (offset_type idx)
4308 {
4309 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4310 const char *expected_str
4311 = expected_it == expected_end ? NULL : *expected_it++;
4312
4313 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4314 mismatch (expected_str, matched_name);
4315 return true;
4316 }, per_objfile);
4317
4318 const char *expected_str
4319 = expected_it == expected_end ? NULL : *expected_it++;
4320 if (expected_str != NULL)
4321 mismatch (expected_str, NULL);
4322
4323 return matched;
4324 }
4325
4326 /* The symbols added to the mock mapped_index for testing (in
4327 canonical form). */
4328 static const char *test_symbols[] = {
4329 "function",
4330 "std::bar",
4331 "std::zfunction",
4332 "std::zfunction2",
4333 "w1::w2",
4334 "ns::foo<char*>",
4335 "ns::foo<int>",
4336 "ns::foo<long>",
4337 "ns2::tmpl<int>::foo2",
4338 "(anonymous namespace)::A::B::C",
4339
4340 /* These are used to check that the increment-last-char in the
4341 matching algorithm for completion doesn't match "t1_fund" when
4342 completing "t1_func". */
4343 "t1_func",
4344 "t1_func1",
4345 "t1_fund",
4346 "t1_fund1",
4347
4348 /* A UTF-8 name with multi-byte sequences to make sure that
4349 cp-name-parser understands this as a single identifier ("função"
4350 is "function" in PT). */
4351 u8"u8função",
4352
4353 /* \377 (0xff) is Latin1 'ÿ'. */
4354 "yfunc\377",
4355
4356 /* \377 (0xff) is Latin1 'ÿ'. */
4357 "\377",
4358 "\377\377123",
4359
4360 /* A name with all sorts of complications. Starts with "z" to make
4361 it easier for the completion tests below. */
4362 #define Z_SYM_NAME \
4363 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4364 "::tuple<(anonymous namespace)::ui*, " \
4365 "std::default_delete<(anonymous namespace)::ui>, void>"
4366
4367 Z_SYM_NAME
4368 };
4369
4370 /* Returns true if the mapped_index_base::find_name_component_bounds
4371 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4372 in completion mode. */
4373
4374 static bool
4375 check_find_bounds_finds (mapped_index_base &index,
4376 const char *search_name,
4377 gdb::array_view<const char *> expected_syms,
4378 dwarf2_per_objfile *per_objfile)
4379 {
4380 lookup_name_info lookup_name (search_name,
4381 symbol_name_match_type::FULL, true);
4382
4383 auto bounds = index.find_name_components_bounds (lookup_name,
4384 language_cplus,
4385 per_objfile);
4386
4387 size_t distance = std::distance (bounds.first, bounds.second);
4388 if (distance != expected_syms.size ())
4389 return false;
4390
4391 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4392 {
4393 auto nc_elem = bounds.first + exp_elem;
4394 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4395 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4396 return false;
4397 }
4398
4399 return true;
4400 }
4401
4402 /* Test the lower-level mapped_index::find_name_component_bounds
4403 method. */
4404
4405 static void
4406 test_mapped_index_find_name_component_bounds ()
4407 {
4408 mock_mapped_index mock_index (test_symbols);
4409
4410 mock_index.build_name_components (NULL /* per_objfile */);
4411
4412 /* Test the lower-level mapped_index::find_name_component_bounds
4413 method in completion mode. */
4414 {
4415 static const char *expected_syms[] = {
4416 "t1_func",
4417 "t1_func1",
4418 };
4419
4420 SELF_CHECK (check_find_bounds_finds
4421 (mock_index, "t1_func", expected_syms,
4422 NULL /* per_objfile */));
4423 }
4424
4425 /* Check that the increment-last-char in the name matching algorithm
4426 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4427 {
4428 static const char *expected_syms1[] = {
4429 "\377",
4430 "\377\377123",
4431 };
4432 SELF_CHECK (check_find_bounds_finds
4433 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4434
4435 static const char *expected_syms2[] = {
4436 "\377\377123",
4437 };
4438 SELF_CHECK (check_find_bounds_finds
4439 (mock_index, "\377\377", expected_syms2,
4440 NULL /* per_objfile */));
4441 }
4442 }
4443
4444 /* Test dw2_expand_symtabs_matching_symbol. */
4445
4446 static void
4447 test_dw2_expand_symtabs_matching_symbol ()
4448 {
4449 mock_mapped_index mock_index (test_symbols);
4450
4451 /* We let all tests run until the end even if some fails, for debug
4452 convenience. */
4453 bool any_mismatch = false;
4454
4455 /* Create the expected symbols list (an initializer_list). Needed
4456 because lists have commas, and we need to pass them to CHECK,
4457 which is a macro. */
4458 #define EXPECT(...) { __VA_ARGS__ }
4459
4460 /* Wrapper for check_match that passes down the current
4461 __FILE__/__LINE__. */
4462 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4463 any_mismatch |= !check_match (__FILE__, __LINE__, \
4464 mock_index, \
4465 NAME, MATCH_TYPE, COMPLETION_MODE, \
4466 EXPECTED_LIST, NULL)
4467
4468 /* Identity checks. */
4469 for (const char *sym : test_symbols)
4470 {
4471 /* Should be able to match all existing symbols. */
4472 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4473 EXPECT (sym));
4474
4475 /* Should be able to match all existing symbols with
4476 parameters. */
4477 std::string with_params = std::string (sym) + "(int)";
4478 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4479 EXPECT (sym));
4480
4481 /* Should be able to match all existing symbols with
4482 parameters and qualifiers. */
4483 with_params = std::string (sym) + " ( int ) const";
4484 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4485 EXPECT (sym));
4486
4487 /* This should really find sym, but cp-name-parser.y doesn't
4488 know about lvalue/rvalue qualifiers yet. */
4489 with_params = std::string (sym) + " ( int ) &&";
4490 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4491 {});
4492 }
4493
4494 /* Check that the name matching algorithm for completion doesn't get
4495 confused with Latin1 'ÿ' / 0xff. */
4496 {
4497 static const char str[] = "\377";
4498 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4499 EXPECT ("\377", "\377\377123"));
4500 }
4501
4502 /* Check that the increment-last-char in the matching algorithm for
4503 completion doesn't match "t1_fund" when completing "t1_func". */
4504 {
4505 static const char str[] = "t1_func";
4506 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4507 EXPECT ("t1_func", "t1_func1"));
4508 }
4509
4510 /* Check that completion mode works at each prefix of the expected
4511 symbol name. */
4512 {
4513 static const char str[] = "function(int)";
4514 size_t len = strlen (str);
4515 std::string lookup;
4516
4517 for (size_t i = 1; i < len; i++)
4518 {
4519 lookup.assign (str, i);
4520 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4521 EXPECT ("function"));
4522 }
4523 }
4524
4525 /* While "w" is a prefix of both components, the match function
4526 should still only be called once. */
4527 {
4528 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4529 EXPECT ("w1::w2"));
4530 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4531 EXPECT ("w1::w2"));
4532 }
4533
4534 /* Same, with a "complicated" symbol. */
4535 {
4536 static const char str[] = Z_SYM_NAME;
4537 size_t len = strlen (str);
4538 std::string lookup;
4539
4540 for (size_t i = 1; i < len; i++)
4541 {
4542 lookup.assign (str, i);
4543 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4544 EXPECT (Z_SYM_NAME));
4545 }
4546 }
4547
4548 /* In FULL mode, an incomplete symbol doesn't match. */
4549 {
4550 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4551 {});
4552 }
4553
4554 /* A complete symbol with parameters matches any overload, since the
4555 index has no overload info. */
4556 {
4557 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4558 EXPECT ("std::zfunction", "std::zfunction2"));
4559 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4560 EXPECT ("std::zfunction", "std::zfunction2"));
4561 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4562 EXPECT ("std::zfunction", "std::zfunction2"));
4563 }
4564
4565 /* Check that whitespace is ignored appropriately. A symbol with a
4566 template argument list. */
4567 {
4568 static const char expected[] = "ns::foo<int>";
4569 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4570 EXPECT (expected));
4571 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4572 EXPECT (expected));
4573 }
4574
4575 /* Check that whitespace is ignored appropriately. A symbol with a
4576 template argument list that includes a pointer. */
4577 {
4578 static const char expected[] = "ns::foo<char*>";
4579 /* Try both completion and non-completion modes. */
4580 static const bool completion_mode[2] = {false, true};
4581 for (size_t i = 0; i < 2; i++)
4582 {
4583 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4584 completion_mode[i], EXPECT (expected));
4585 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4586 completion_mode[i], EXPECT (expected));
4587
4588 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4589 completion_mode[i], EXPECT (expected));
4590 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4591 completion_mode[i], EXPECT (expected));
4592 }
4593 }
4594
4595 {
4596 /* Check method qualifiers are ignored. */
4597 static const char expected[] = "ns::foo<char*>";
4598 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4599 symbol_name_match_type::FULL, true, EXPECT (expected));
4600 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4601 symbol_name_match_type::FULL, true, EXPECT (expected));
4602 CHECK_MATCH ("foo < char * > ( int ) const",
4603 symbol_name_match_type::WILD, true, EXPECT (expected));
4604 CHECK_MATCH ("foo < char * > ( int ) &&",
4605 symbol_name_match_type::WILD, true, EXPECT (expected));
4606 }
4607
4608 /* Test lookup names that don't match anything. */
4609 {
4610 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4611 {});
4612
4613 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4614 {});
4615 }
4616
4617 /* Some wild matching tests, exercising "(anonymous namespace)",
4618 which should not be confused with a parameter list. */
4619 {
4620 static const char *syms[] = {
4621 "A::B::C",
4622 "B::C",
4623 "C",
4624 "A :: B :: C ( int )",
4625 "B :: C ( int )",
4626 "C ( int )",
4627 };
4628
4629 for (const char *s : syms)
4630 {
4631 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4632 EXPECT ("(anonymous namespace)::A::B::C"));
4633 }
4634 }
4635
4636 {
4637 static const char expected[] = "ns2::tmpl<int>::foo2";
4638 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4639 EXPECT (expected));
4640 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4641 EXPECT (expected));
4642 }
4643
4644 SELF_CHECK (!any_mismatch);
4645
4646 #undef EXPECT
4647 #undef CHECK_MATCH
4648 }
4649
4650 static void
4651 run_test ()
4652 {
4653 test_mapped_index_find_name_component_bounds ();
4654 test_dw2_expand_symtabs_matching_symbol ();
4655 }
4656
4657 }} // namespace selftests::dw2_expand_symtabs_matching
4658
4659 #endif /* GDB_SELF_TEST */
4660
4661 /* If FILE_MATCHER is NULL or if PER_CU has
4662 dwarf2_per_cu_quick_data::MARK set (see
4663 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4664 EXPANSION_NOTIFY on it. */
4665
4666 static void
4667 dw2_expand_symtabs_matching_one
4668 (dwarf2_per_cu_data *per_cu,
4669 dwarf2_per_objfile *per_objfile,
4670 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4671 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4672 {
4673 if (file_matcher == NULL || per_cu->v.quick->mark)
4674 {
4675 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4676
4677 compunit_symtab *symtab
4678 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4679 gdb_assert (symtab != nullptr);
4680
4681 if (expansion_notify != NULL && symtab_was_null)
4682 expansion_notify (symtab);
4683 }
4684 }
4685
4686 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4687 matched, to expand corresponding CUs that were marked. IDX is the
4688 index of the symbol name that matched. */
4689
4690 static void
4691 dw2_expand_marked_cus
4692 (dwarf2_per_objfile *per_objfile, offset_type idx,
4693 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4694 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4695 search_domain kind)
4696 {
4697 offset_type *vec, vec_len, vec_idx;
4698 bool global_seen = false;
4699 mapped_index &index = *per_objfile->per_bfd->index_table;
4700
4701 vec = (offset_type *) (index.constant_pool
4702 + MAYBE_SWAP (index.symbol_table[idx].vec));
4703 vec_len = MAYBE_SWAP (vec[0]);
4704 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4705 {
4706 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4707 /* This value is only valid for index versions >= 7. */
4708 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4709 gdb_index_symbol_kind symbol_kind =
4710 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4711 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4712 /* Only check the symbol attributes if they're present.
4713 Indices prior to version 7 don't record them,
4714 and indices >= 7 may elide them for certain symbols
4715 (gold does this). */
4716 int attrs_valid =
4717 (index.version >= 7
4718 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4719
4720 /* Work around gold/15646. */
4721 if (attrs_valid
4722 && !is_static
4723 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4724 {
4725 if (global_seen)
4726 continue;
4727
4728 global_seen = true;
4729 }
4730
4731 /* Only check the symbol's kind if it has one. */
4732 if (attrs_valid)
4733 {
4734 switch (kind)
4735 {
4736 case VARIABLES_DOMAIN:
4737 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4738 continue;
4739 break;
4740 case FUNCTIONS_DOMAIN:
4741 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4742 continue;
4743 break;
4744 case TYPES_DOMAIN:
4745 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4746 continue;
4747 break;
4748 case MODULES_DOMAIN:
4749 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4750 continue;
4751 break;
4752 default:
4753 break;
4754 }
4755 }
4756
4757 /* Don't crash on bad data. */
4758 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4759 + per_objfile->per_bfd->all_type_units.size ()))
4760 {
4761 complaint (_(".gdb_index entry has bad CU index"
4762 " [in module %s]"), objfile_name (per_objfile->objfile));
4763 continue;
4764 }
4765
4766 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4767 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4768 expansion_notify);
4769 }
4770 }
4771
4772 /* If FILE_MATCHER is non-NULL, set all the
4773 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4774 that match FILE_MATCHER. */
4775
4776 static void
4777 dw_expand_symtabs_matching_file_matcher
4778 (dwarf2_per_objfile *per_objfile,
4779 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4780 {
4781 if (file_matcher == NULL)
4782 return;
4783
4784 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4785 htab_eq_pointer,
4786 NULL, xcalloc, xfree));
4787 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4788 htab_eq_pointer,
4789 NULL, xcalloc, xfree));
4790
4791 /* The rule is CUs specify all the files, including those used by
4792 any TU, so there's no need to scan TUs here. */
4793
4794 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4795 {
4796 QUIT;
4797
4798 per_cu->v.quick->mark = 0;
4799
4800 /* We only need to look at symtabs not already expanded. */
4801 if (per_objfile->symtab_set_p (per_cu))
4802 continue;
4803
4804 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4805 if (file_data == NULL)
4806 continue;
4807
4808 if (htab_find (visited_not_found.get (), file_data) != NULL)
4809 continue;
4810 else if (htab_find (visited_found.get (), file_data) != NULL)
4811 {
4812 per_cu->v.quick->mark = 1;
4813 continue;
4814 }
4815
4816 for (int j = 0; j < file_data->num_file_names; ++j)
4817 {
4818 const char *this_real_name;
4819
4820 if (file_matcher (file_data->file_names[j], false))
4821 {
4822 per_cu->v.quick->mark = 1;
4823 break;
4824 }
4825
4826 /* Before we invoke realpath, which can get expensive when many
4827 files are involved, do a quick comparison of the basenames. */
4828 if (!basenames_may_differ
4829 && !file_matcher (lbasename (file_data->file_names[j]),
4830 true))
4831 continue;
4832
4833 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4834 if (file_matcher (this_real_name, false))
4835 {
4836 per_cu->v.quick->mark = 1;
4837 break;
4838 }
4839 }
4840
4841 void **slot = htab_find_slot (per_cu->v.quick->mark
4842 ? visited_found.get ()
4843 : visited_not_found.get (),
4844 file_data, INSERT);
4845 *slot = file_data;
4846 }
4847 }
4848
4849 void
4850 dwarf2_gdb_index::expand_symtabs_matching
4851 (struct objfile *objfile,
4852 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4853 const lookup_name_info *lookup_name,
4854 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4855 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4856 enum search_domain kind)
4857 {
4858 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4859
4860 /* index_table is NULL if OBJF_READNOW. */
4861 if (!per_objfile->per_bfd->index_table)
4862 return;
4863
4864 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4865
4866 if (symbol_matcher == NULL && lookup_name == NULL)
4867 {
4868 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4869 {
4870 QUIT;
4871
4872 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4873 file_matcher, expansion_notify);
4874 }
4875 return;
4876 }
4877
4878 mapped_index &index = *per_objfile->per_bfd->index_table;
4879
4880 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4881 symbol_matcher,
4882 [&] (offset_type idx)
4883 {
4884 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4885 kind);
4886 return true;
4887 }, per_objfile);
4888 }
4889
4890 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4891 symtab. */
4892
4893 static struct compunit_symtab *
4894 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4895 CORE_ADDR pc)
4896 {
4897 int i;
4898
4899 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4900 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4901 return cust;
4902
4903 if (cust->includes == NULL)
4904 return NULL;
4905
4906 for (i = 0; cust->includes[i]; ++i)
4907 {
4908 struct compunit_symtab *s = cust->includes[i];
4909
4910 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4911 if (s != NULL)
4912 return s;
4913 }
4914
4915 return NULL;
4916 }
4917
4918 struct compunit_symtab *
4919 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4920 (struct objfile *objfile,
4921 struct bound_minimal_symbol msymbol,
4922 CORE_ADDR pc,
4923 struct obj_section *section,
4924 int warn_if_readin)
4925 {
4926 struct dwarf2_per_cu_data *data;
4927 struct compunit_symtab *result;
4928
4929 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4930 if (per_objfile->per_bfd->index_addrmap == nullptr)
4931 return NULL;
4932
4933 CORE_ADDR baseaddr = objfile->text_section_offset ();
4934 data = ((struct dwarf2_per_cu_data *)
4935 addrmap_find (per_objfile->per_bfd->index_addrmap,
4936 pc - baseaddr));
4937 if (!data)
4938 return NULL;
4939
4940 if (warn_if_readin && per_objfile->symtab_set_p (data))
4941 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4942 paddress (objfile->arch (), pc));
4943
4944 result = recursively_find_pc_sect_compunit_symtab
4945 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4946
4947 gdb_assert (result != NULL);
4948 return result;
4949 }
4950
4951 void
4952 dwarf2_base_index_functions::map_symbol_filenames
4953 (struct objfile *objfile,
4954 gdb::function_view<symbol_filename_ftype> fun,
4955 bool need_fullname)
4956 {
4957 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4958
4959 if (!per_objfile->per_bfd->filenames_cache)
4960 {
4961 per_objfile->per_bfd->filenames_cache.emplace ();
4962
4963 htab_up visited (htab_create_alloc (10,
4964 htab_hash_pointer, htab_eq_pointer,
4965 NULL, xcalloc, xfree));
4966
4967 /* The rule is CUs specify all the files, including those used
4968 by any TU, so there's no need to scan TUs here. We can
4969 ignore file names coming from already-expanded CUs. */
4970
4971 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4972 {
4973 if (per_objfile->symtab_set_p (per_cu))
4974 {
4975 void **slot = htab_find_slot (visited.get (),
4976 per_cu->v.quick->file_names,
4977 INSERT);
4978
4979 *slot = per_cu->v.quick->file_names;
4980 }
4981 }
4982
4983 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4984 {
4985 /* We only need to look at symtabs not already expanded. */
4986 if (per_objfile->symtab_set_p (per_cu))
4987 continue;
4988
4989 quick_file_names *file_data
4990 = dw2_get_file_names (per_cu, per_objfile);
4991 if (file_data == NULL)
4992 continue;
4993
4994 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4995 if (*slot)
4996 {
4997 /* Already visited. */
4998 continue;
4999 }
5000 *slot = file_data;
5001
5002 for (int j = 0; j < file_data->num_file_names; ++j)
5003 {
5004 const char *filename = file_data->file_names[j];
5005 per_objfile->per_bfd->filenames_cache->seen (filename);
5006 }
5007 }
5008 }
5009
5010 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
5011 {
5012 gdb::unique_xmalloc_ptr<char> this_real_name;
5013
5014 if (need_fullname)
5015 this_real_name = gdb_realpath (filename);
5016 fun (filename, this_real_name.get ());
5017 });
5018 }
5019
5020 bool
5021 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
5022 {
5023 return true;
5024 }
5025
5026 /* DWARF-5 debug_names reader. */
5027
5028 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5029 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5030
5031 /* A helper function that reads the .debug_names section in SECTION
5032 and fills in MAP. FILENAME is the name of the file containing the
5033 section; it is used for error reporting.
5034
5035 Returns true if all went well, false otherwise. */
5036
5037 static bool
5038 read_debug_names_from_section (struct objfile *objfile,
5039 const char *filename,
5040 struct dwarf2_section_info *section,
5041 mapped_debug_names &map)
5042 {
5043 if (section->empty ())
5044 return false;
5045
5046 /* Older elfutils strip versions could keep the section in the main
5047 executable while splitting it for the separate debug info file. */
5048 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5049 return false;
5050
5051 section->read (objfile);
5052
5053 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5054
5055 const gdb_byte *addr = section->buffer;
5056
5057 bfd *const abfd = section->get_bfd_owner ();
5058
5059 unsigned int bytes_read;
5060 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5061 addr += bytes_read;
5062
5063 map.dwarf5_is_dwarf64 = bytes_read != 4;
5064 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5065 if (bytes_read + length != section->size)
5066 {
5067 /* There may be multiple per-CU indices. */
5068 warning (_("Section .debug_names in %s length %s does not match "
5069 "section length %s, ignoring .debug_names."),
5070 filename, plongest (bytes_read + length),
5071 pulongest (section->size));
5072 return false;
5073 }
5074
5075 /* The version number. */
5076 uint16_t version = read_2_bytes (abfd, addr);
5077 addr += 2;
5078 if (version != 5)
5079 {
5080 warning (_("Section .debug_names in %s has unsupported version %d, "
5081 "ignoring .debug_names."),
5082 filename, version);
5083 return false;
5084 }
5085
5086 /* Padding. */
5087 uint16_t padding = read_2_bytes (abfd, addr);
5088 addr += 2;
5089 if (padding != 0)
5090 {
5091 warning (_("Section .debug_names in %s has unsupported padding %d, "
5092 "ignoring .debug_names."),
5093 filename, padding);
5094 return false;
5095 }
5096
5097 /* comp_unit_count - The number of CUs in the CU list. */
5098 map.cu_count = read_4_bytes (abfd, addr);
5099 addr += 4;
5100
5101 /* local_type_unit_count - The number of TUs in the local TU
5102 list. */
5103 map.tu_count = read_4_bytes (abfd, addr);
5104 addr += 4;
5105
5106 /* foreign_type_unit_count - The number of TUs in the foreign TU
5107 list. */
5108 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5109 addr += 4;
5110 if (foreign_tu_count != 0)
5111 {
5112 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5113 "ignoring .debug_names."),
5114 filename, static_cast<unsigned long> (foreign_tu_count));
5115 return false;
5116 }
5117
5118 /* bucket_count - The number of hash buckets in the hash lookup
5119 table. */
5120 map.bucket_count = read_4_bytes (abfd, addr);
5121 addr += 4;
5122
5123 /* name_count - The number of unique names in the index. */
5124 map.name_count = read_4_bytes (abfd, addr);
5125 addr += 4;
5126
5127 /* abbrev_table_size - The size in bytes of the abbreviations
5128 table. */
5129 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5130 addr += 4;
5131
5132 /* augmentation_string_size - The size in bytes of the augmentation
5133 string. This value is rounded up to a multiple of 4. */
5134 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5135 addr += 4;
5136 map.augmentation_is_gdb = ((augmentation_string_size
5137 == sizeof (dwarf5_augmentation))
5138 && memcmp (addr, dwarf5_augmentation,
5139 sizeof (dwarf5_augmentation)) == 0);
5140 augmentation_string_size += (-augmentation_string_size) & 3;
5141 addr += augmentation_string_size;
5142
5143 /* List of CUs */
5144 map.cu_table_reordered = addr;
5145 addr += map.cu_count * map.offset_size;
5146
5147 /* List of Local TUs */
5148 map.tu_table_reordered = addr;
5149 addr += map.tu_count * map.offset_size;
5150
5151 /* Hash Lookup Table */
5152 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5153 addr += map.bucket_count * 4;
5154 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5155 addr += map.name_count * 4;
5156
5157 /* Name Table */
5158 map.name_table_string_offs_reordered = addr;
5159 addr += map.name_count * map.offset_size;
5160 map.name_table_entry_offs_reordered = addr;
5161 addr += map.name_count * map.offset_size;
5162
5163 const gdb_byte *abbrev_table_start = addr;
5164 for (;;)
5165 {
5166 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5167 addr += bytes_read;
5168 if (index_num == 0)
5169 break;
5170
5171 const auto insertpair
5172 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5173 if (!insertpair.second)
5174 {
5175 warning (_("Section .debug_names in %s has duplicate index %s, "
5176 "ignoring .debug_names."),
5177 filename, pulongest (index_num));
5178 return false;
5179 }
5180 mapped_debug_names::index_val &indexval = insertpair.first->second;
5181 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5182 addr += bytes_read;
5183
5184 for (;;)
5185 {
5186 mapped_debug_names::index_val::attr attr;
5187 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5188 addr += bytes_read;
5189 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5190 addr += bytes_read;
5191 if (attr.form == DW_FORM_implicit_const)
5192 {
5193 attr.implicit_const = read_signed_leb128 (abfd, addr,
5194 &bytes_read);
5195 addr += bytes_read;
5196 }
5197 if (attr.dw_idx == 0 && attr.form == 0)
5198 break;
5199 indexval.attr_vec.push_back (std::move (attr));
5200 }
5201 }
5202 if (addr != abbrev_table_start + abbrev_table_size)
5203 {
5204 warning (_("Section .debug_names in %s has abbreviation_table "
5205 "of size %s vs. written as %u, ignoring .debug_names."),
5206 filename, plongest (addr - abbrev_table_start),
5207 abbrev_table_size);
5208 return false;
5209 }
5210 map.entry_pool = addr;
5211
5212 return true;
5213 }
5214
5215 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5216 list. */
5217
5218 static void
5219 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5220 const mapped_debug_names &map,
5221 dwarf2_section_info &section,
5222 bool is_dwz)
5223 {
5224 if (!map.augmentation_is_gdb)
5225 {
5226 for (uint32_t i = 0; i < map.cu_count; ++i)
5227 {
5228 sect_offset sect_off
5229 = (sect_offset) (extract_unsigned_integer
5230 (map.cu_table_reordered + i * map.offset_size,
5231 map.offset_size,
5232 map.dwarf5_byte_order));
5233 /* We don't know the length of the CU, because the CU list in a
5234 .debug_names index can be incomplete, so we can't use the start
5235 of the next CU as end of this CU. We create the CUs here with
5236 length 0, and in cutu_reader::cutu_reader we'll fill in the
5237 actual length. */
5238 dwarf2_per_cu_data *per_cu
5239 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5240 sect_off, 0);
5241 per_bfd->all_comp_units.push_back (per_cu);
5242 }
5243 return;
5244 }
5245
5246 sect_offset sect_off_prev;
5247 for (uint32_t i = 0; i <= map.cu_count; ++i)
5248 {
5249 sect_offset sect_off_next;
5250 if (i < map.cu_count)
5251 {
5252 sect_off_next
5253 = (sect_offset) (extract_unsigned_integer
5254 (map.cu_table_reordered + i * map.offset_size,
5255 map.offset_size,
5256 map.dwarf5_byte_order));
5257 }
5258 else
5259 sect_off_next = (sect_offset) section.size;
5260 if (i >= 1)
5261 {
5262 const ULONGEST length = sect_off_next - sect_off_prev;
5263 dwarf2_per_cu_data *per_cu
5264 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5265 sect_off_prev, length);
5266 per_bfd->all_comp_units.push_back (per_cu);
5267 }
5268 sect_off_prev = sect_off_next;
5269 }
5270 }
5271
5272 /* Read the CU list from the mapped index, and use it to create all
5273 the CU objects for this dwarf2_per_objfile. */
5274
5275 static void
5276 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5277 const mapped_debug_names &map,
5278 const mapped_debug_names &dwz_map)
5279 {
5280 gdb_assert (per_bfd->all_comp_units.empty ());
5281 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5282
5283 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5284 false /* is_dwz */);
5285
5286 if (dwz_map.cu_count == 0)
5287 return;
5288
5289 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5290 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5291 true /* is_dwz */);
5292 }
5293
5294 /* Read .debug_names. If everything went ok, initialize the "quick"
5295 elements of all the CUs and return true. Otherwise, return false. */
5296
5297 static bool
5298 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5299 {
5300 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5301 mapped_debug_names dwz_map;
5302 struct objfile *objfile = per_objfile->objfile;
5303 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5304
5305 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5306 &per_bfd->debug_names, *map))
5307 return false;
5308
5309 /* Don't use the index if it's empty. */
5310 if (map->name_count == 0)
5311 return false;
5312
5313 /* If there is a .dwz file, read it so we can get its CU list as
5314 well. */
5315 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5316 if (dwz != NULL)
5317 {
5318 if (!read_debug_names_from_section (objfile,
5319 bfd_get_filename (dwz->dwz_bfd.get ()),
5320 &dwz->debug_names, dwz_map))
5321 {
5322 warning (_("could not read '.debug_names' section from %s; skipping"),
5323 bfd_get_filename (dwz->dwz_bfd.get ()));
5324 return false;
5325 }
5326 }
5327
5328 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5329
5330 if (map->tu_count != 0)
5331 {
5332 /* We can only handle a single .debug_types when we have an
5333 index. */
5334 if (per_bfd->types.size () != 1)
5335 return false;
5336
5337 dwarf2_section_info *section = &per_bfd->types[0];
5338
5339 create_signatured_type_table_from_debug_names
5340 (per_objfile, *map, section, &per_bfd->abbrev);
5341 }
5342
5343 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5344
5345 per_bfd->debug_names_table = std::move (map);
5346 per_bfd->using_index = 1;
5347 per_bfd->quick_file_names_table =
5348 create_quick_file_names_table (per_bfd->all_comp_units.size ());
5349
5350 return true;
5351 }
5352
5353 /* Type used to manage iterating over all CUs looking for a symbol for
5354 .debug_names. */
5355
5356 class dw2_debug_names_iterator
5357 {
5358 public:
5359 dw2_debug_names_iterator (const mapped_debug_names &map,
5360 gdb::optional<block_enum> block_index,
5361 domain_enum domain,
5362 const char *name, dwarf2_per_objfile *per_objfile)
5363 : m_map (map), m_block_index (block_index), m_domain (domain),
5364 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5365 m_per_objfile (per_objfile)
5366 {}
5367
5368 dw2_debug_names_iterator (const mapped_debug_names &map,
5369 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5370 : m_map (map),
5371 m_search (search),
5372 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5373 m_per_objfile (per_objfile)
5374 {}
5375
5376 dw2_debug_names_iterator (const mapped_debug_names &map,
5377 block_enum block_index, domain_enum domain,
5378 uint32_t namei, dwarf2_per_objfile *per_objfile)
5379 : m_map (map), m_block_index (block_index), m_domain (domain),
5380 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5381 m_per_objfile (per_objfile)
5382 {}
5383
5384 /* Return the next matching CU or NULL if there are no more. */
5385 dwarf2_per_cu_data *next ();
5386
5387 private:
5388 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5389 const char *name,
5390 dwarf2_per_objfile *per_objfile);
5391 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5392 uint32_t namei,
5393 dwarf2_per_objfile *per_objfile);
5394
5395 /* The internalized form of .debug_names. */
5396 const mapped_debug_names &m_map;
5397
5398 /* If set, only look for symbols that match that block. Valid values are
5399 GLOBAL_BLOCK and STATIC_BLOCK. */
5400 const gdb::optional<block_enum> m_block_index;
5401
5402 /* The kind of symbol we're looking for. */
5403 const domain_enum m_domain = UNDEF_DOMAIN;
5404 const search_domain m_search = ALL_DOMAIN;
5405
5406 /* The list of CUs from the index entry of the symbol, or NULL if
5407 not found. */
5408 const gdb_byte *m_addr;
5409
5410 dwarf2_per_objfile *m_per_objfile;
5411 };
5412
5413 const char *
5414 mapped_debug_names::namei_to_name
5415 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5416 {
5417 const ULONGEST namei_string_offs
5418 = extract_unsigned_integer ((name_table_string_offs_reordered
5419 + namei * offset_size),
5420 offset_size,
5421 dwarf5_byte_order);
5422 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5423 }
5424
5425 /* Find a slot in .debug_names for the object named NAME. If NAME is
5426 found, return pointer to its pool data. If NAME cannot be found,
5427 return NULL. */
5428
5429 const gdb_byte *
5430 dw2_debug_names_iterator::find_vec_in_debug_names
5431 (const mapped_debug_names &map, const char *name,
5432 dwarf2_per_objfile *per_objfile)
5433 {
5434 int (*cmp) (const char *, const char *);
5435
5436 gdb::unique_xmalloc_ptr<char> without_params;
5437 if (current_language->la_language == language_cplus
5438 || current_language->la_language == language_fortran
5439 || current_language->la_language == language_d)
5440 {
5441 /* NAME is already canonical. Drop any qualifiers as
5442 .debug_names does not contain any. */
5443
5444 if (strchr (name, '(') != NULL)
5445 {
5446 without_params = cp_remove_params (name);
5447 if (without_params != NULL)
5448 name = without_params.get ();
5449 }
5450 }
5451
5452 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5453
5454 const uint32_t full_hash = dwarf5_djb_hash (name);
5455 uint32_t namei
5456 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5457 (map.bucket_table_reordered
5458 + (full_hash % map.bucket_count)), 4,
5459 map.dwarf5_byte_order);
5460 if (namei == 0)
5461 return NULL;
5462 --namei;
5463 if (namei >= map.name_count)
5464 {
5465 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5466 "[in module %s]"),
5467 namei, map.name_count,
5468 objfile_name (per_objfile->objfile));
5469 return NULL;
5470 }
5471
5472 for (;;)
5473 {
5474 const uint32_t namei_full_hash
5475 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5476 (map.hash_table_reordered + namei), 4,
5477 map.dwarf5_byte_order);
5478 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5479 return NULL;
5480
5481 if (full_hash == namei_full_hash)
5482 {
5483 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5484
5485 #if 0 /* An expensive sanity check. */
5486 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5487 {
5488 complaint (_("Wrong .debug_names hash for string at index %u "
5489 "[in module %s]"),
5490 namei, objfile_name (dwarf2_per_objfile->objfile));
5491 return NULL;
5492 }
5493 #endif
5494
5495 if (cmp (namei_string, name) == 0)
5496 {
5497 const ULONGEST namei_entry_offs
5498 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5499 + namei * map.offset_size),
5500 map.offset_size, map.dwarf5_byte_order);
5501 return map.entry_pool + namei_entry_offs;
5502 }
5503 }
5504
5505 ++namei;
5506 if (namei >= map.name_count)
5507 return NULL;
5508 }
5509 }
5510
5511 const gdb_byte *
5512 dw2_debug_names_iterator::find_vec_in_debug_names
5513 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5514 {
5515 if (namei >= map.name_count)
5516 {
5517 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5518 "[in module %s]"),
5519 namei, map.name_count,
5520 objfile_name (per_objfile->objfile));
5521 return NULL;
5522 }
5523
5524 const ULONGEST namei_entry_offs
5525 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5526 + namei * map.offset_size),
5527 map.offset_size, map.dwarf5_byte_order);
5528 return map.entry_pool + namei_entry_offs;
5529 }
5530
5531 /* See dw2_debug_names_iterator. */
5532
5533 dwarf2_per_cu_data *
5534 dw2_debug_names_iterator::next ()
5535 {
5536 if (m_addr == NULL)
5537 return NULL;
5538
5539 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5540 struct objfile *objfile = m_per_objfile->objfile;
5541 bfd *const abfd = objfile->obfd;
5542
5543 again:
5544
5545 unsigned int bytes_read;
5546 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5547 m_addr += bytes_read;
5548 if (abbrev == 0)
5549 return NULL;
5550
5551 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5552 if (indexval_it == m_map.abbrev_map.cend ())
5553 {
5554 complaint (_("Wrong .debug_names undefined abbrev code %s "
5555 "[in module %s]"),
5556 pulongest (abbrev), objfile_name (objfile));
5557 return NULL;
5558 }
5559 const mapped_debug_names::index_val &indexval = indexval_it->second;
5560 enum class symbol_linkage {
5561 unknown,
5562 static_,
5563 extern_,
5564 } symbol_linkage_ = symbol_linkage::unknown;
5565 dwarf2_per_cu_data *per_cu = NULL;
5566 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5567 {
5568 ULONGEST ull;
5569 switch (attr.form)
5570 {
5571 case DW_FORM_implicit_const:
5572 ull = attr.implicit_const;
5573 break;
5574 case DW_FORM_flag_present:
5575 ull = 1;
5576 break;
5577 case DW_FORM_udata:
5578 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5579 m_addr += bytes_read;
5580 break;
5581 case DW_FORM_ref4:
5582 ull = read_4_bytes (abfd, m_addr);
5583 m_addr += 4;
5584 break;
5585 case DW_FORM_ref8:
5586 ull = read_8_bytes (abfd, m_addr);
5587 m_addr += 8;
5588 break;
5589 case DW_FORM_ref_sig8:
5590 ull = read_8_bytes (abfd, m_addr);
5591 m_addr += 8;
5592 break;
5593 default:
5594 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5595 dwarf_form_name (attr.form),
5596 objfile_name (objfile));
5597 return NULL;
5598 }
5599 switch (attr.dw_idx)
5600 {
5601 case DW_IDX_compile_unit:
5602 /* Don't crash on bad data. */
5603 if (ull >= per_bfd->all_comp_units.size ())
5604 {
5605 complaint (_(".debug_names entry has bad CU index %s"
5606 " [in module %s]"),
5607 pulongest (ull),
5608 objfile_name (objfile));
5609 continue;
5610 }
5611 per_cu = per_bfd->get_cutu (ull);
5612 break;
5613 case DW_IDX_type_unit:
5614 /* Don't crash on bad data. */
5615 if (ull >= per_bfd->all_type_units.size ())
5616 {
5617 complaint (_(".debug_names entry has bad TU index %s"
5618 " [in module %s]"),
5619 pulongest (ull),
5620 objfile_name (objfile));
5621 continue;
5622 }
5623 per_cu = &per_bfd->get_tu (ull)->per_cu;
5624 break;
5625 case DW_IDX_die_offset:
5626 /* In a per-CU index (as opposed to a per-module index), index
5627 entries without CU attribute implicitly refer to the single CU. */
5628 if (per_cu == NULL)
5629 per_cu = per_bfd->get_cu (0);
5630 break;
5631 case DW_IDX_GNU_internal:
5632 if (!m_map.augmentation_is_gdb)
5633 break;
5634 symbol_linkage_ = symbol_linkage::static_;
5635 break;
5636 case DW_IDX_GNU_external:
5637 if (!m_map.augmentation_is_gdb)
5638 break;
5639 symbol_linkage_ = symbol_linkage::extern_;
5640 break;
5641 }
5642 }
5643
5644 /* Skip if already read in. */
5645 if (m_per_objfile->symtab_set_p (per_cu))
5646 goto again;
5647
5648 /* Check static vs global. */
5649 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5650 {
5651 const bool want_static = *m_block_index == STATIC_BLOCK;
5652 const bool symbol_is_static =
5653 symbol_linkage_ == symbol_linkage::static_;
5654 if (want_static != symbol_is_static)
5655 goto again;
5656 }
5657
5658 /* Match dw2_symtab_iter_next, symbol_kind
5659 and debug_names::psymbol_tag. */
5660 switch (m_domain)
5661 {
5662 case VAR_DOMAIN:
5663 switch (indexval.dwarf_tag)
5664 {
5665 case DW_TAG_variable:
5666 case DW_TAG_subprogram:
5667 /* Some types are also in VAR_DOMAIN. */
5668 case DW_TAG_typedef:
5669 case DW_TAG_structure_type:
5670 break;
5671 default:
5672 goto again;
5673 }
5674 break;
5675 case STRUCT_DOMAIN:
5676 switch (indexval.dwarf_tag)
5677 {
5678 case DW_TAG_typedef:
5679 case DW_TAG_structure_type:
5680 break;
5681 default:
5682 goto again;
5683 }
5684 break;
5685 case LABEL_DOMAIN:
5686 switch (indexval.dwarf_tag)
5687 {
5688 case 0:
5689 case DW_TAG_variable:
5690 break;
5691 default:
5692 goto again;
5693 }
5694 break;
5695 case MODULE_DOMAIN:
5696 switch (indexval.dwarf_tag)
5697 {
5698 case DW_TAG_module:
5699 break;
5700 default:
5701 goto again;
5702 }
5703 break;
5704 default:
5705 break;
5706 }
5707
5708 /* Match dw2_expand_symtabs_matching, symbol_kind and
5709 debug_names::psymbol_tag. */
5710 switch (m_search)
5711 {
5712 case VARIABLES_DOMAIN:
5713 switch (indexval.dwarf_tag)
5714 {
5715 case DW_TAG_variable:
5716 break;
5717 default:
5718 goto again;
5719 }
5720 break;
5721 case FUNCTIONS_DOMAIN:
5722 switch (indexval.dwarf_tag)
5723 {
5724 case DW_TAG_subprogram:
5725 break;
5726 default:
5727 goto again;
5728 }
5729 break;
5730 case TYPES_DOMAIN:
5731 switch (indexval.dwarf_tag)
5732 {
5733 case DW_TAG_typedef:
5734 case DW_TAG_structure_type:
5735 break;
5736 default:
5737 goto again;
5738 }
5739 break;
5740 case MODULES_DOMAIN:
5741 switch (indexval.dwarf_tag)
5742 {
5743 case DW_TAG_module:
5744 break;
5745 default:
5746 goto again;
5747 }
5748 default:
5749 break;
5750 }
5751
5752 return per_cu;
5753 }
5754
5755 struct compunit_symtab *
5756 dwarf2_debug_names_index::lookup_symbol
5757 (struct objfile *objfile, block_enum block_index,
5758 const char *name, domain_enum domain)
5759 {
5760 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5761
5762 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5763 if (!mapp)
5764 {
5765 /* index is NULL if OBJF_READNOW. */
5766 return NULL;
5767 }
5768 const auto &map = *mapp;
5769
5770 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5771
5772 struct compunit_symtab *stab_best = NULL;
5773 struct dwarf2_per_cu_data *per_cu;
5774 while ((per_cu = iter.next ()) != NULL)
5775 {
5776 struct symbol *sym, *with_opaque = NULL;
5777 compunit_symtab *stab
5778 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5779 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5780 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5781
5782 sym = block_find_symbol (block, name, domain,
5783 block_find_non_opaque_type_preferred,
5784 &with_opaque);
5785
5786 /* Some caution must be observed with overloaded functions and
5787 methods, since the index will not contain any overload
5788 information (but NAME might contain it). */
5789
5790 if (sym != NULL
5791 && strcmp_iw (sym->search_name (), name) == 0)
5792 return stab;
5793 if (with_opaque != NULL
5794 && strcmp_iw (with_opaque->search_name (), name) == 0)
5795 stab_best = stab;
5796
5797 /* Keep looking through other CUs. */
5798 }
5799
5800 return stab_best;
5801 }
5802
5803 /* This dumps minimal information about .debug_names. It is called
5804 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5805 uses this to verify that .debug_names has been loaded. */
5806
5807 void
5808 dwarf2_debug_names_index::dump (struct objfile *objfile)
5809 {
5810 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5811
5812 gdb_assert (per_objfile->per_bfd->using_index);
5813 printf_filtered (".debug_names:");
5814 if (per_objfile->per_bfd->debug_names_table)
5815 printf_filtered (" exists\n");
5816 else
5817 printf_filtered (" faked for \"readnow\"\n");
5818 printf_filtered ("\n");
5819 }
5820
5821 void
5822 dwarf2_debug_names_index::expand_symtabs_for_function
5823 (struct objfile *objfile, const char *func_name)
5824 {
5825 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5826
5827 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5828 if (per_objfile->per_bfd->debug_names_table)
5829 {
5830 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5831
5832 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5833 per_objfile);
5834
5835 struct dwarf2_per_cu_data *per_cu;
5836 while ((per_cu = iter.next ()) != NULL)
5837 dw2_instantiate_symtab (per_cu, per_objfile, false);
5838 }
5839 }
5840
5841 void
5842 dwarf2_debug_names_index::map_matching_symbols
5843 (struct objfile *objfile,
5844 const lookup_name_info &name, domain_enum domain,
5845 int global,
5846 gdb::function_view<symbol_found_callback_ftype> callback,
5847 symbol_compare_ftype *ordered_compare)
5848 {
5849 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5850
5851 /* debug_names_table is NULL if OBJF_READNOW. */
5852 if (!per_objfile->per_bfd->debug_names_table)
5853 return;
5854
5855 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5856 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5857
5858 const char *match_name = name.ada ().lookup_name ().c_str ();
5859 auto matcher = [&] (const char *symname)
5860 {
5861 if (ordered_compare == nullptr)
5862 return true;
5863 return ordered_compare (symname, match_name) == 0;
5864 };
5865
5866 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5867 [&] (offset_type namei)
5868 {
5869 /* The name was matched, now expand corresponding CUs that were
5870 marked. */
5871 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5872 per_objfile);
5873
5874 struct dwarf2_per_cu_data *per_cu;
5875 while ((per_cu = iter.next ()) != NULL)
5876 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5877 nullptr);
5878 return true;
5879 }, per_objfile);
5880
5881 /* It's a shame we couldn't do this inside the
5882 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5883 that have already been expanded. Instead, this loop matches what
5884 the psymtab code does. */
5885 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5886 {
5887 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5888 if (symtab != nullptr)
5889 {
5890 const struct block *block
5891 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5892 if (!iterate_over_symbols_terminated (block, name,
5893 domain, callback))
5894 break;
5895 }
5896 }
5897 }
5898
5899 void
5900 dwarf2_debug_names_index::expand_symtabs_matching
5901 (struct objfile *objfile,
5902 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5903 const lookup_name_info *lookup_name,
5904 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5905 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5906 enum search_domain kind)
5907 {
5908 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5909
5910 /* debug_names_table is NULL if OBJF_READNOW. */
5911 if (!per_objfile->per_bfd->debug_names_table)
5912 return;
5913
5914 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5915
5916 if (symbol_matcher == NULL && lookup_name == NULL)
5917 {
5918 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5919 {
5920 QUIT;
5921
5922 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5923 expansion_notify);
5924 }
5925 return;
5926 }
5927
5928 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5929
5930 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5931 symbol_matcher,
5932 [&] (offset_type namei)
5933 {
5934 /* The name was matched, now expand corresponding CUs that were
5935 marked. */
5936 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5937
5938 struct dwarf2_per_cu_data *per_cu;
5939 while ((per_cu = iter.next ()) != NULL)
5940 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5941 expansion_notify);
5942 return true;
5943 }, per_objfile);
5944 }
5945
5946 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5947 to either a dwarf2_per_bfd or dwz_file object. */
5948
5949 template <typename T>
5950 static gdb::array_view<const gdb_byte>
5951 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5952 {
5953 dwarf2_section_info *section = &section_owner->gdb_index;
5954
5955 if (section->empty ())
5956 return {};
5957
5958 /* Older elfutils strip versions could keep the section in the main
5959 executable while splitting it for the separate debug info file. */
5960 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5961 return {};
5962
5963 section->read (obj);
5964
5965 /* dwarf2_section_info::size is a bfd_size_type, while
5966 gdb::array_view works with size_t. On 32-bit hosts, with
5967 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5968 is 32-bit. So we need an explicit narrowing conversion here.
5969 This is fine, because it's impossible to allocate or mmap an
5970 array/buffer larger than what size_t can represent. */
5971 return gdb::make_array_view (section->buffer, section->size);
5972 }
5973
5974 /* Lookup the index cache for the contents of the index associated to
5975 DWARF2_OBJ. */
5976
5977 static gdb::array_view<const gdb_byte>
5978 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5979 {
5980 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5981 if (build_id == nullptr)
5982 return {};
5983
5984 return global_index_cache.lookup_gdb_index (build_id,
5985 &dwarf2_per_bfd->index_cache_res);
5986 }
5987
5988 /* Same as the above, but for DWZ. */
5989
5990 static gdb::array_view<const gdb_byte>
5991 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5992 {
5993 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5994 if (build_id == nullptr)
5995 return {};
5996
5997 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5998 }
5999
6000 /* See dwarf2/public.h. */
6001
6002 void
6003 dwarf2_initialize_objfile (struct objfile *objfile)
6004 {
6005 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6006 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6007
6008 dwarf_read_debug_printf ("called");
6009
6010 /* If we're about to read full symbols, don't bother with the
6011 indices. In this case we also don't care if some other debug
6012 format is making psymtabs, because they are all about to be
6013 expanded anyway. */
6014 if ((objfile->flags & OBJF_READNOW))
6015 {
6016 dwarf_read_debug_printf ("readnow requested");
6017
6018 /* When using READNOW, the using_index flag (set below) indicates that
6019 PER_BFD was already initialized, when we loaded some other objfile. */
6020 if (per_bfd->using_index)
6021 {
6022 dwarf_read_debug_printf ("using_index already set");
6023 per_objfile->resize_symtabs ();
6024 objfile->qf.push_front (make_dwarf_gdb_index ());
6025 return;
6026 }
6027
6028 per_bfd->using_index = 1;
6029 create_all_comp_units (per_objfile);
6030 create_all_type_units (per_objfile);
6031 per_bfd->quick_file_names_table
6032 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6033 per_objfile->resize_symtabs ();
6034
6035 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6036 + per_bfd->all_type_units.size ()); ++i)
6037 {
6038 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6039
6040 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6041 struct dwarf2_per_cu_quick_data);
6042 }
6043
6044 /* Arrange for gdb to see the "quick" functions. However, these
6045 functions will be no-ops because we will have expanded all
6046 symtabs. */
6047 objfile->qf.push_front (make_dwarf_gdb_index ());
6048 return;
6049 }
6050
6051 /* Was a debug names index already read when we processed an objfile sharing
6052 PER_BFD? */
6053 if (per_bfd->debug_names_table != nullptr)
6054 {
6055 dwarf_read_debug_printf ("re-using shared debug names table");
6056 per_objfile->resize_symtabs ();
6057 objfile->qf.push_front (make_dwarf_debug_names ());
6058 return;
6059 }
6060
6061 /* Was a GDB index already read when we processed an objfile sharing
6062 PER_BFD? */
6063 if (per_bfd->index_table != nullptr)
6064 {
6065 dwarf_read_debug_printf ("re-using shared index table");
6066 per_objfile->resize_symtabs ();
6067 objfile->qf.push_front (make_dwarf_gdb_index ());
6068 return;
6069 }
6070
6071 /* There might already be partial symtabs built for this BFD. This happens
6072 when loading the same binary twice with the index-cache enabled. If so,
6073 don't try to read an index. The objfile / per_objfile initialization will
6074 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6075 code path. */
6076 if (per_bfd->partial_symtabs != nullptr)
6077 {
6078 dwarf_read_debug_printf ("re-using shared partial symtabs");
6079 objfile->qf.push_front (make_lazy_dwarf_reader ());
6080 return;
6081 }
6082
6083 if (dwarf2_read_debug_names (per_objfile))
6084 {
6085 dwarf_read_debug_printf ("found debug names");
6086 per_objfile->resize_symtabs ();
6087 objfile->qf.push_front (make_dwarf_debug_names ());
6088 return;
6089 }
6090
6091 if (dwarf2_read_gdb_index (per_objfile,
6092 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6093 get_gdb_index_contents_from_section<dwz_file>))
6094 {
6095 dwarf_read_debug_printf ("found gdb index from file");
6096 per_objfile->resize_symtabs ();
6097 objfile->qf.push_front (make_dwarf_gdb_index ());
6098 return;
6099 }
6100
6101 /* ... otherwise, try to find the index in the index cache. */
6102 if (dwarf2_read_gdb_index (per_objfile,
6103 get_gdb_index_contents_from_cache,
6104 get_gdb_index_contents_from_cache_dwz))
6105 {
6106 dwarf_read_debug_printf ("found gdb index from cache");
6107 global_index_cache.hit ();
6108 per_objfile->resize_symtabs ();
6109 objfile->qf.push_front (make_dwarf_gdb_index ());
6110 return;
6111 }
6112
6113 global_index_cache.miss ();
6114 objfile->qf.push_front (make_lazy_dwarf_reader ());
6115 }
6116
6117 \f
6118
6119 /* Build a partial symbol table. */
6120
6121 void
6122 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
6123 {
6124 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6125 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6126
6127 if (per_bfd->partial_symtabs != nullptr)
6128 {
6129 /* Partial symbols were already read, so now we can simply
6130 attach them. */
6131 if (psf == nullptr)
6132 {
6133 psf = new psymbol_functions (per_bfd->partial_symtabs);
6134 objfile->qf.emplace_front (psf);
6135 }
6136 else
6137 psf->set_partial_symtabs (per_bfd->partial_symtabs);
6138 per_objfile->resize_symtabs ();
6139 return;
6140 }
6141
6142 if (psf == nullptr)
6143 {
6144 psf = new psymbol_functions;
6145 objfile->qf.emplace_front (psf);
6146 }
6147 const std::shared_ptr<psymtab_storage> &partial_symtabs
6148 = psf->get_partial_symtabs ();
6149
6150 /* Set the local reference to partial symtabs, so that we don't try
6151 to read them again if reading another objfile with the same BFD.
6152 If we can't in fact share, this won't make a difference anyway as
6153 the dwarf2_per_bfd object won't be shared. */
6154 per_bfd->partial_symtabs = partial_symtabs;
6155
6156 try
6157 {
6158 /* This isn't really ideal: all the data we allocate on the
6159 objfile's obstack is still uselessly kept around. However,
6160 freeing it seems unsafe. */
6161 psymtab_discarder psymtabs (partial_symtabs.get ());
6162 dwarf2_build_psymtabs_hard (per_objfile);
6163 psymtabs.keep ();
6164
6165 per_objfile->resize_symtabs ();
6166
6167 /* (maybe) store an index in the cache. */
6168 global_index_cache.store (per_objfile);
6169 }
6170 catch (const gdb_exception_error &except)
6171 {
6172 exception_print (gdb_stderr, except);
6173 }
6174 }
6175
6176 /* Find the base address of the compilation unit for range lists and
6177 location lists. It will normally be specified by DW_AT_low_pc.
6178 In DWARF-3 draft 4, the base address could be overridden by
6179 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6180 compilation units with discontinuous ranges. */
6181
6182 static void
6183 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6184 {
6185 struct attribute *attr;
6186
6187 cu->base_address.reset ();
6188
6189 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6190 if (attr != nullptr)
6191 cu->base_address = attr->as_address ();
6192 else
6193 {
6194 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6195 if (attr != nullptr)
6196 cu->base_address = attr->as_address ();
6197 }
6198 }
6199
6200 /* Helper function that returns the proper abbrev section for
6201 THIS_CU. */
6202
6203 static struct dwarf2_section_info *
6204 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6205 {
6206 struct dwarf2_section_info *abbrev;
6207 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6208
6209 if (this_cu->is_dwz)
6210 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
6211 else
6212 abbrev = &per_bfd->abbrev;
6213
6214 return abbrev;
6215 }
6216
6217 /* Fetch the abbreviation table offset from a comp or type unit header. */
6218
6219 static sect_offset
6220 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6221 struct dwarf2_section_info *section,
6222 sect_offset sect_off)
6223 {
6224 bfd *abfd = section->get_bfd_owner ();
6225 const gdb_byte *info_ptr;
6226 unsigned int initial_length_size, offset_size;
6227 uint16_t version;
6228
6229 section->read (per_objfile->objfile);
6230 info_ptr = section->buffer + to_underlying (sect_off);
6231 read_initial_length (abfd, info_ptr, &initial_length_size);
6232 offset_size = initial_length_size == 4 ? 4 : 8;
6233 info_ptr += initial_length_size;
6234
6235 version = read_2_bytes (abfd, info_ptr);
6236 info_ptr += 2;
6237 if (version >= 5)
6238 {
6239 /* Skip unit type and address size. */
6240 info_ptr += 2;
6241 }
6242
6243 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6244 }
6245
6246 /* A partial symtab that is used only for include files. */
6247 struct dwarf2_include_psymtab : public partial_symtab
6248 {
6249 dwarf2_include_psymtab (const char *filename,
6250 psymtab_storage *partial_symtabs,
6251 objfile_per_bfd_storage *objfile_per_bfd)
6252 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
6253 {
6254 }
6255
6256 void read_symtab (struct objfile *objfile) override
6257 {
6258 /* It's an include file, no symbols to read for it.
6259 Everything is in the includer symtab. */
6260
6261 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6262 expansion of the includer psymtab. We use the dependencies[0] field to
6263 model the includer. But if we go the regular route of calling
6264 expand_psymtab here, and having expand_psymtab call expand_dependencies
6265 to expand the includer, we'll only use expand_psymtab on the includer
6266 (making it a non-toplevel psymtab), while if we expand the includer via
6267 another path, we'll use read_symtab (making it a toplevel psymtab).
6268 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6269 psymtab, and trigger read_symtab on the includer here directly. */
6270 includer ()->read_symtab (objfile);
6271 }
6272
6273 void expand_psymtab (struct objfile *objfile) override
6274 {
6275 /* This is not called by read_symtab, and should not be called by any
6276 expand_dependencies. */
6277 gdb_assert (false);
6278 }
6279
6280 bool readin_p (struct objfile *objfile) const override
6281 {
6282 return includer ()->readin_p (objfile);
6283 }
6284
6285 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6286 {
6287 return nullptr;
6288 }
6289
6290 private:
6291 partial_symtab *includer () const
6292 {
6293 /* An include psymtab has exactly one dependency: the psymtab that
6294 includes it. */
6295 gdb_assert (this->number_of_dependencies == 1);
6296 return this->dependencies[0];
6297 }
6298 };
6299
6300 /* Allocate a new partial symtab for file named NAME and mark this new
6301 partial symtab as being an include of PST. */
6302
6303 static void
6304 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
6305 const char *name,
6306 dwarf2_psymtab *pst,
6307 psymtab_storage *partial_symtabs,
6308 objfile_per_bfd_storage *objfile_per_bfd)
6309 {
6310 dwarf2_include_psymtab *subpst
6311 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
6312
6313 if (!IS_ABSOLUTE_PATH (subpst->filename))
6314 subpst->dirname = pst->dirname;
6315
6316 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
6317 subpst->dependencies[0] = pst;
6318 subpst->number_of_dependencies = 1;
6319 }
6320
6321 /* Read the Line Number Program data and extract the list of files
6322 included by the source file represented by PST. Build an include
6323 partial symtab for each of these included files. */
6324
6325 static void
6326 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6327 struct die_info *die,
6328 dwarf2_psymtab *pst)
6329 {
6330 line_header_up lh;
6331 struct attribute *attr;
6332
6333 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6334 if (attr != nullptr && attr->form_is_unsigned ())
6335 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6336 if (lh == NULL)
6337 return; /* No linetable, so no includes. */
6338
6339 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6340 that we pass in the raw text_low here; that is ok because we're
6341 only decoding the line table to make include partial symtabs, and
6342 so the addresses aren't really used. */
6343 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6344 pst->raw_text_low (), 1);
6345 }
6346
6347 static hashval_t
6348 hash_signatured_type (const void *item)
6349 {
6350 const struct signatured_type *sig_type
6351 = (const struct signatured_type *) item;
6352
6353 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6354 return sig_type->signature;
6355 }
6356
6357 static int
6358 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6359 {
6360 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6361 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6362
6363 return lhs->signature == rhs->signature;
6364 }
6365
6366 /* Allocate a hash table for signatured types. */
6367
6368 static htab_up
6369 allocate_signatured_type_table ()
6370 {
6371 return htab_up (htab_create_alloc (41,
6372 hash_signatured_type,
6373 eq_signatured_type,
6374 NULL, xcalloc, xfree));
6375 }
6376
6377 /* A helper function to add a signatured type CU to a table. */
6378
6379 static int
6380 add_signatured_type_cu_to_table (void **slot, void *datum)
6381 {
6382 struct signatured_type *sigt = (struct signatured_type *) *slot;
6383 std::vector<signatured_type *> *all_type_units
6384 = (std::vector<signatured_type *> *) datum;
6385
6386 all_type_units->push_back (sigt);
6387
6388 return 1;
6389 }
6390
6391 /* A helper for create_debug_types_hash_table. Read types from SECTION
6392 and fill them into TYPES_HTAB. It will process only type units,
6393 therefore DW_UT_type. */
6394
6395 static void
6396 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6397 struct dwo_file *dwo_file,
6398 dwarf2_section_info *section, htab_up &types_htab,
6399 rcuh_kind section_kind)
6400 {
6401 struct objfile *objfile = per_objfile->objfile;
6402 struct dwarf2_section_info *abbrev_section;
6403 bfd *abfd;
6404 const gdb_byte *info_ptr, *end_ptr;
6405
6406 abbrev_section = (dwo_file != NULL
6407 ? &dwo_file->sections.abbrev
6408 : &per_objfile->per_bfd->abbrev);
6409
6410 dwarf_read_debug_printf ("Reading %s for %s",
6411 section->get_name (),
6412 abbrev_section->get_file_name ());
6413
6414 section->read (objfile);
6415 info_ptr = section->buffer;
6416
6417 if (info_ptr == NULL)
6418 return;
6419
6420 /* We can't set abfd until now because the section may be empty or
6421 not present, in which case the bfd is unknown. */
6422 abfd = section->get_bfd_owner ();
6423
6424 /* We don't use cutu_reader here because we don't need to read
6425 any dies: the signature is in the header. */
6426
6427 end_ptr = info_ptr + section->size;
6428 while (info_ptr < end_ptr)
6429 {
6430 struct signatured_type *sig_type;
6431 struct dwo_unit *dwo_tu;
6432 void **slot;
6433 const gdb_byte *ptr = info_ptr;
6434 struct comp_unit_head header;
6435 unsigned int length;
6436
6437 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6438
6439 /* Initialize it due to a false compiler warning. */
6440 header.signature = -1;
6441 header.type_cu_offset_in_tu = (cu_offset) -1;
6442
6443 /* We need to read the type's signature in order to build the hash
6444 table, but we don't need anything else just yet. */
6445
6446 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6447 abbrev_section, ptr, section_kind);
6448
6449 length = header.get_length ();
6450
6451 /* Skip dummy type units. */
6452 if (ptr >= info_ptr + length
6453 || peek_abbrev_code (abfd, ptr) == 0
6454 || (header.unit_type != DW_UT_type
6455 && header.unit_type != DW_UT_split_type))
6456 {
6457 info_ptr += length;
6458 continue;
6459 }
6460
6461 if (types_htab == NULL)
6462 {
6463 if (dwo_file)
6464 types_htab = allocate_dwo_unit_table ();
6465 else
6466 types_htab = allocate_signatured_type_table ();
6467 }
6468
6469 if (dwo_file)
6470 {
6471 sig_type = NULL;
6472 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6473 dwo_tu->dwo_file = dwo_file;
6474 dwo_tu->signature = header.signature;
6475 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6476 dwo_tu->section = section;
6477 dwo_tu->sect_off = sect_off;
6478 dwo_tu->length = length;
6479 }
6480 else
6481 {
6482 /* N.B.: type_offset is not usable if this type uses a DWO file.
6483 The real type_offset is in the DWO file. */
6484 dwo_tu = NULL;
6485 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6486 sig_type->signature = header.signature;
6487 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6488 sig_type->per_cu.is_debug_types = 1;
6489 sig_type->per_cu.section = section;
6490 sig_type->per_cu.sect_off = sect_off;
6491 sig_type->per_cu.length = length;
6492 }
6493
6494 slot = htab_find_slot (types_htab.get (),
6495 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6496 INSERT);
6497 gdb_assert (slot != NULL);
6498 if (*slot != NULL)
6499 {
6500 sect_offset dup_sect_off;
6501
6502 if (dwo_file)
6503 {
6504 const struct dwo_unit *dup_tu
6505 = (const struct dwo_unit *) *slot;
6506
6507 dup_sect_off = dup_tu->sect_off;
6508 }
6509 else
6510 {
6511 const struct signatured_type *dup_tu
6512 = (const struct signatured_type *) *slot;
6513
6514 dup_sect_off = dup_tu->per_cu.sect_off;
6515 }
6516
6517 complaint (_("debug type entry at offset %s is duplicate to"
6518 " the entry at offset %s, signature %s"),
6519 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6520 hex_string (header.signature));
6521 }
6522 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6523
6524 dwarf_read_debug_printf_v (" offset %s, signature %s",
6525 sect_offset_str (sect_off),
6526 hex_string (header.signature));
6527
6528 info_ptr += length;
6529 }
6530 }
6531
6532 /* Create the hash table of all entries in the .debug_types
6533 (or .debug_types.dwo) section(s).
6534 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6535 otherwise it is NULL.
6536
6537 The result is a pointer to the hash table or NULL if there are no types.
6538
6539 Note: This function processes DWO files only, not DWP files. */
6540
6541 static void
6542 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6543 struct dwo_file *dwo_file,
6544 gdb::array_view<dwarf2_section_info> type_sections,
6545 htab_up &types_htab)
6546 {
6547 for (dwarf2_section_info &section : type_sections)
6548 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6549 rcuh_kind::TYPE);
6550 }
6551
6552 /* Create the hash table of all entries in the .debug_types section,
6553 and initialize all_type_units.
6554 The result is zero if there is an error (e.g. missing .debug_types section),
6555 otherwise non-zero. */
6556
6557 static int
6558 create_all_type_units (dwarf2_per_objfile *per_objfile)
6559 {
6560 htab_up types_htab;
6561
6562 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6563 types_htab, rcuh_kind::COMPILE);
6564 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6565 types_htab);
6566 if (types_htab == NULL)
6567 {
6568 per_objfile->per_bfd->signatured_types = NULL;
6569 return 0;
6570 }
6571
6572 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6573
6574 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6575 per_objfile->per_bfd->all_type_units.reserve
6576 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6577
6578 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6579 add_signatured_type_cu_to_table,
6580 &per_objfile->per_bfd->all_type_units);
6581
6582 return 1;
6583 }
6584
6585 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6586 If SLOT is non-NULL, it is the entry to use in the hash table.
6587 Otherwise we find one. */
6588
6589 static struct signatured_type *
6590 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6591 {
6592 if (per_objfile->per_bfd->all_type_units.size ()
6593 == per_objfile->per_bfd->all_type_units.capacity ())
6594 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6595
6596 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6597
6598 per_objfile->resize_symtabs ();
6599
6600 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6601 sig_type->signature = sig;
6602 sig_type->per_cu.is_debug_types = 1;
6603 if (per_objfile->per_bfd->using_index)
6604 {
6605 sig_type->per_cu.v.quick =
6606 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6607 struct dwarf2_per_cu_quick_data);
6608 }
6609
6610 if (slot == NULL)
6611 {
6612 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6613 sig_type, INSERT);
6614 }
6615 gdb_assert (*slot == NULL);
6616 *slot = sig_type;
6617 /* The rest of sig_type must be filled in by the caller. */
6618 return sig_type;
6619 }
6620
6621 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6622 Fill in SIG_ENTRY with DWO_ENTRY. */
6623
6624 static void
6625 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6626 struct signatured_type *sig_entry,
6627 struct dwo_unit *dwo_entry)
6628 {
6629 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6630
6631 /* Make sure we're not clobbering something we don't expect to. */
6632 gdb_assert (! sig_entry->per_cu.queued);
6633 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6634 if (per_bfd->using_index)
6635 {
6636 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6637 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6638 }
6639 else
6640 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6641 gdb_assert (sig_entry->signature == dwo_entry->signature);
6642 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6643 gdb_assert (sig_entry->type_unit_group == NULL);
6644 gdb_assert (sig_entry->dwo_unit == NULL);
6645
6646 sig_entry->per_cu.section = dwo_entry->section;
6647 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6648 sig_entry->per_cu.length = dwo_entry->length;
6649 sig_entry->per_cu.reading_dwo_directly = 1;
6650 sig_entry->per_cu.per_bfd = per_bfd;
6651 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6652 sig_entry->dwo_unit = dwo_entry;
6653 }
6654
6655 /* Subroutine of lookup_signatured_type.
6656 If we haven't read the TU yet, create the signatured_type data structure
6657 for a TU to be read in directly from a DWO file, bypassing the stub.
6658 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6659 using .gdb_index, then when reading a CU we want to stay in the DWO file
6660 containing that CU. Otherwise we could end up reading several other DWO
6661 files (due to comdat folding) to process the transitive closure of all the
6662 mentioned TUs, and that can be slow. The current DWO file will have every
6663 type signature that it needs.
6664 We only do this for .gdb_index because in the psymtab case we already have
6665 to read all the DWOs to build the type unit groups. */
6666
6667 static struct signatured_type *
6668 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6669 {
6670 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6671 struct dwo_file *dwo_file;
6672 struct dwo_unit find_dwo_entry, *dwo_entry;
6673 struct signatured_type find_sig_entry, *sig_entry;
6674 void **slot;
6675
6676 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6677
6678 /* If TU skeletons have been removed then we may not have read in any
6679 TUs yet. */
6680 if (per_objfile->per_bfd->signatured_types == NULL)
6681 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6682
6683 /* We only ever need to read in one copy of a signatured type.
6684 Use the global signatured_types array to do our own comdat-folding
6685 of types. If this is the first time we're reading this TU, and
6686 the TU has an entry in .gdb_index, replace the recorded data from
6687 .gdb_index with this TU. */
6688
6689 find_sig_entry.signature = sig;
6690 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6691 &find_sig_entry, INSERT);
6692 sig_entry = (struct signatured_type *) *slot;
6693
6694 /* We can get here with the TU already read, *or* in the process of being
6695 read. Don't reassign the global entry to point to this DWO if that's
6696 the case. Also note that if the TU is already being read, it may not
6697 have come from a DWO, the program may be a mix of Fission-compiled
6698 code and non-Fission-compiled code. */
6699
6700 /* Have we already tried to read this TU?
6701 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6702 needn't exist in the global table yet). */
6703 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6704 return sig_entry;
6705
6706 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6707 dwo_unit of the TU itself. */
6708 dwo_file = cu->dwo_unit->dwo_file;
6709
6710 /* Ok, this is the first time we're reading this TU. */
6711 if (dwo_file->tus == NULL)
6712 return NULL;
6713 find_dwo_entry.signature = sig;
6714 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6715 &find_dwo_entry);
6716 if (dwo_entry == NULL)
6717 return NULL;
6718
6719 /* If the global table doesn't have an entry for this TU, add one. */
6720 if (sig_entry == NULL)
6721 sig_entry = add_type_unit (per_objfile, sig, slot);
6722
6723 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6724 sig_entry->per_cu.tu_read = 1;
6725 return sig_entry;
6726 }
6727
6728 /* Subroutine of lookup_signatured_type.
6729 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6730 then try the DWP file. If the TU stub (skeleton) has been removed then
6731 it won't be in .gdb_index. */
6732
6733 static struct signatured_type *
6734 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6735 {
6736 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6737 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6738 struct dwo_unit *dwo_entry;
6739 struct signatured_type find_sig_entry, *sig_entry;
6740 void **slot;
6741
6742 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6743 gdb_assert (dwp_file != NULL);
6744
6745 /* If TU skeletons have been removed then we may not have read in any
6746 TUs yet. */
6747 if (per_objfile->per_bfd->signatured_types == NULL)
6748 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6749
6750 find_sig_entry.signature = sig;
6751 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6752 &find_sig_entry, INSERT);
6753 sig_entry = (struct signatured_type *) *slot;
6754
6755 /* Have we already tried to read this TU?
6756 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6757 needn't exist in the global table yet). */
6758 if (sig_entry != NULL)
6759 return sig_entry;
6760
6761 if (dwp_file->tus == NULL)
6762 return NULL;
6763 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6764 1 /* is_debug_types */);
6765 if (dwo_entry == NULL)
6766 return NULL;
6767
6768 sig_entry = add_type_unit (per_objfile, sig, slot);
6769 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6770
6771 return sig_entry;
6772 }
6773
6774 /* Lookup a signature based type for DW_FORM_ref_sig8.
6775 Returns NULL if signature SIG is not present in the table.
6776 It is up to the caller to complain about this. */
6777
6778 static struct signatured_type *
6779 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6780 {
6781 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6782
6783 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6784 {
6785 /* We're in a DWO/DWP file, and we're using .gdb_index.
6786 These cases require special processing. */
6787 if (get_dwp_file (per_objfile) == NULL)
6788 return lookup_dwo_signatured_type (cu, sig);
6789 else
6790 return lookup_dwp_signatured_type (cu, sig);
6791 }
6792 else
6793 {
6794 struct signatured_type find_entry, *entry;
6795
6796 if (per_objfile->per_bfd->signatured_types == NULL)
6797 return NULL;
6798 find_entry.signature = sig;
6799 entry = ((struct signatured_type *)
6800 htab_find (per_objfile->per_bfd->signatured_types.get (),
6801 &find_entry));
6802 return entry;
6803 }
6804 }
6805
6806 /* Low level DIE reading support. */
6807
6808 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6809
6810 static void
6811 init_cu_die_reader (struct die_reader_specs *reader,
6812 struct dwarf2_cu *cu,
6813 struct dwarf2_section_info *section,
6814 struct dwo_file *dwo_file,
6815 struct abbrev_table *abbrev_table)
6816 {
6817 gdb_assert (section->readin && section->buffer != NULL);
6818 reader->abfd = section->get_bfd_owner ();
6819 reader->cu = cu;
6820 reader->dwo_file = dwo_file;
6821 reader->die_section = section;
6822 reader->buffer = section->buffer;
6823 reader->buffer_end = section->buffer + section->size;
6824 reader->abbrev_table = abbrev_table;
6825 }
6826
6827 /* Subroutine of cutu_reader to simplify it.
6828 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6829 There's just a lot of work to do, and cutu_reader is big enough
6830 already.
6831
6832 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6833 from it to the DIE in the DWO. If NULL we are skipping the stub.
6834 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6835 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6836 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6837 STUB_COMP_DIR may be non-NULL.
6838 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6839 are filled in with the info of the DIE from the DWO file.
6840 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6841 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6842 kept around for at least as long as *RESULT_READER.
6843
6844 The result is non-zero if a valid (non-dummy) DIE was found. */
6845
6846 static int
6847 read_cutu_die_from_dwo (dwarf2_cu *cu,
6848 struct dwo_unit *dwo_unit,
6849 struct die_info *stub_comp_unit_die,
6850 const char *stub_comp_dir,
6851 struct die_reader_specs *result_reader,
6852 const gdb_byte **result_info_ptr,
6853 struct die_info **result_comp_unit_die,
6854 abbrev_table_up *result_dwo_abbrev_table)
6855 {
6856 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6857 dwarf2_per_cu_data *per_cu = cu->per_cu;
6858 struct objfile *objfile = per_objfile->objfile;
6859 bfd *abfd;
6860 const gdb_byte *begin_info_ptr, *info_ptr;
6861 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6862 int i,num_extra_attrs;
6863 struct dwarf2_section_info *dwo_abbrev_section;
6864 struct die_info *comp_unit_die;
6865
6866 /* At most one of these may be provided. */
6867 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6868
6869 /* These attributes aren't processed until later:
6870 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6871 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6872 referenced later. However, these attributes are found in the stub
6873 which we won't have later. In order to not impose this complication
6874 on the rest of the code, we read them here and copy them to the
6875 DWO CU/TU die. */
6876
6877 stmt_list = NULL;
6878 low_pc = NULL;
6879 high_pc = NULL;
6880 ranges = NULL;
6881 comp_dir = NULL;
6882
6883 if (stub_comp_unit_die != NULL)
6884 {
6885 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6886 DWO file. */
6887 if (!per_cu->is_debug_types)
6888 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6889 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6890 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6891 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6892 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6893
6894 cu->addr_base = stub_comp_unit_die->addr_base ();
6895
6896 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6897 We need the value before we can process DW_AT_ranges values from the
6898 DWO. */
6899 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6900
6901 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6902 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6903 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6904 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6905 section. */
6906 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6907 }
6908 else if (stub_comp_dir != NULL)
6909 {
6910 /* Reconstruct the comp_dir attribute to simplify the code below. */
6911 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6912 comp_dir->name = DW_AT_comp_dir;
6913 comp_dir->form = DW_FORM_string;
6914 comp_dir->set_string_noncanonical (stub_comp_dir);
6915 }
6916
6917 /* Set up for reading the DWO CU/TU. */
6918 cu->dwo_unit = dwo_unit;
6919 dwarf2_section_info *section = dwo_unit->section;
6920 section->read (objfile);
6921 abfd = section->get_bfd_owner ();
6922 begin_info_ptr = info_ptr = (section->buffer
6923 + to_underlying (dwo_unit->sect_off));
6924 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6925
6926 if (per_cu->is_debug_types)
6927 {
6928 signatured_type *sig_type = (struct signatured_type *) per_cu;
6929
6930 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6931 section, dwo_abbrev_section,
6932 info_ptr, rcuh_kind::TYPE);
6933 /* This is not an assert because it can be caused by bad debug info. */
6934 if (sig_type->signature != cu->header.signature)
6935 {
6936 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6937 " TU at offset %s [in module %s]"),
6938 hex_string (sig_type->signature),
6939 hex_string (cu->header.signature),
6940 sect_offset_str (dwo_unit->sect_off),
6941 bfd_get_filename (abfd));
6942 }
6943 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6944 /* For DWOs coming from DWP files, we don't know the CU length
6945 nor the type's offset in the TU until now. */
6946 dwo_unit->length = cu->header.get_length ();
6947 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6948
6949 /* Establish the type offset that can be used to lookup the type.
6950 For DWO files, we don't know it until now. */
6951 sig_type->type_offset_in_section
6952 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6953 }
6954 else
6955 {
6956 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6957 section, dwo_abbrev_section,
6958 info_ptr, rcuh_kind::COMPILE);
6959 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6960 /* For DWOs coming from DWP files, we don't know the CU length
6961 until now. */
6962 dwo_unit->length = cu->header.get_length ();
6963 }
6964
6965 dwo_abbrev_section->read (objfile);
6966 *result_dwo_abbrev_table
6967 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6968 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6969 result_dwo_abbrev_table->get ());
6970
6971 /* Read in the die, but leave space to copy over the attributes
6972 from the stub. This has the benefit of simplifying the rest of
6973 the code - all the work to maintain the illusion of a single
6974 DW_TAG_{compile,type}_unit DIE is done here. */
6975 num_extra_attrs = ((stmt_list != NULL)
6976 + (low_pc != NULL)
6977 + (high_pc != NULL)
6978 + (ranges != NULL)
6979 + (comp_dir != NULL));
6980 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6981 num_extra_attrs);
6982
6983 /* Copy over the attributes from the stub to the DIE we just read in. */
6984 comp_unit_die = *result_comp_unit_die;
6985 i = comp_unit_die->num_attrs;
6986 if (stmt_list != NULL)
6987 comp_unit_die->attrs[i++] = *stmt_list;
6988 if (low_pc != NULL)
6989 comp_unit_die->attrs[i++] = *low_pc;
6990 if (high_pc != NULL)
6991 comp_unit_die->attrs[i++] = *high_pc;
6992 if (ranges != NULL)
6993 comp_unit_die->attrs[i++] = *ranges;
6994 if (comp_dir != NULL)
6995 comp_unit_die->attrs[i++] = *comp_dir;
6996 comp_unit_die->num_attrs += num_extra_attrs;
6997
6998 if (dwarf_die_debug)
6999 {
7000 fprintf_unfiltered (gdb_stdlog,
7001 "Read die from %s@0x%x of %s:\n",
7002 section->get_name (),
7003 (unsigned) (begin_info_ptr - section->buffer),
7004 bfd_get_filename (abfd));
7005 dump_die (comp_unit_die, dwarf_die_debug);
7006 }
7007
7008 /* Skip dummy compilation units. */
7009 if (info_ptr >= begin_info_ptr + dwo_unit->length
7010 || peek_abbrev_code (abfd, info_ptr) == 0)
7011 return 0;
7012
7013 *result_info_ptr = info_ptr;
7014 return 1;
7015 }
7016
7017 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7018 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7019 signature is part of the header. */
7020 static gdb::optional<ULONGEST>
7021 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7022 {
7023 if (cu->header.version >= 5)
7024 return cu->header.signature;
7025 struct attribute *attr;
7026 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7027 if (attr == nullptr || !attr->form_is_unsigned ())
7028 return gdb::optional<ULONGEST> ();
7029 return attr->as_unsigned ();
7030 }
7031
7032 /* Subroutine of cutu_reader to simplify it.
7033 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7034 Returns NULL if the specified DWO unit cannot be found. */
7035
7036 static struct dwo_unit *
7037 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7038 {
7039 dwarf2_per_cu_data *per_cu = cu->per_cu;
7040 struct dwo_unit *dwo_unit;
7041 const char *comp_dir;
7042
7043 gdb_assert (cu != NULL);
7044
7045 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7046 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7047 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7048
7049 if (per_cu->is_debug_types)
7050 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7051 else
7052 {
7053 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7054
7055 if (!signature.has_value ())
7056 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7057 " [in module %s]"),
7058 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7059
7060 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7061 }
7062
7063 return dwo_unit;
7064 }
7065
7066 /* Subroutine of cutu_reader to simplify it.
7067 See it for a description of the parameters.
7068 Read a TU directly from a DWO file, bypassing the stub. */
7069
7070 void
7071 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7072 dwarf2_per_objfile *per_objfile,
7073 dwarf2_cu *existing_cu)
7074 {
7075 struct signatured_type *sig_type;
7076
7077 /* Verify we can do the following downcast, and that we have the
7078 data we need. */
7079 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7080 sig_type = (struct signatured_type *) this_cu;
7081 gdb_assert (sig_type->dwo_unit != NULL);
7082
7083 dwarf2_cu *cu;
7084
7085 if (existing_cu != nullptr)
7086 {
7087 cu = existing_cu;
7088 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7089 /* There's no need to do the rereading_dwo_cu handling that
7090 cutu_reader does since we don't read the stub. */
7091 }
7092 else
7093 {
7094 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7095 in per_objfile yet. */
7096 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7097 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7098 cu = m_new_cu.get ();
7099 }
7100
7101 /* A future optimization, if needed, would be to use an existing
7102 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7103 could share abbrev tables. */
7104
7105 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7106 NULL /* stub_comp_unit_die */,
7107 sig_type->dwo_unit->dwo_file->comp_dir,
7108 this, &info_ptr,
7109 &comp_unit_die,
7110 &m_dwo_abbrev_table) == 0)
7111 {
7112 /* Dummy die. */
7113 dummy_p = true;
7114 }
7115 }
7116
7117 /* Initialize a CU (or TU) and read its DIEs.
7118 If the CU defers to a DWO file, read the DWO file as well.
7119
7120 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7121 Otherwise the table specified in the comp unit header is read in and used.
7122 This is an optimization for when we already have the abbrev table.
7123
7124 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7125 allocated. */
7126
7127 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7128 dwarf2_per_objfile *per_objfile,
7129 struct abbrev_table *abbrev_table,
7130 dwarf2_cu *existing_cu,
7131 bool skip_partial)
7132 : die_reader_specs {},
7133 m_this_cu (this_cu)
7134 {
7135 struct objfile *objfile = per_objfile->objfile;
7136 struct dwarf2_section_info *section = this_cu->section;
7137 bfd *abfd = section->get_bfd_owner ();
7138 const gdb_byte *begin_info_ptr;
7139 struct signatured_type *sig_type = NULL;
7140 struct dwarf2_section_info *abbrev_section;
7141 /* Non-zero if CU currently points to a DWO file and we need to
7142 reread it. When this happens we need to reread the skeleton die
7143 before we can reread the DWO file (this only applies to CUs, not TUs). */
7144 int rereading_dwo_cu = 0;
7145
7146 if (dwarf_die_debug)
7147 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7148 this_cu->is_debug_types ? "type" : "comp",
7149 sect_offset_str (this_cu->sect_off));
7150
7151 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7152 file (instead of going through the stub), short-circuit all of this. */
7153 if (this_cu->reading_dwo_directly)
7154 {
7155 /* Narrow down the scope of possibilities to have to understand. */
7156 gdb_assert (this_cu->is_debug_types);
7157 gdb_assert (abbrev_table == NULL);
7158 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7159 return;
7160 }
7161
7162 /* This is cheap if the section is already read in. */
7163 section->read (objfile);
7164
7165 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7166
7167 abbrev_section = get_abbrev_section_for_cu (this_cu);
7168
7169 dwarf2_cu *cu;
7170
7171 if (existing_cu != nullptr)
7172 {
7173 cu = existing_cu;
7174 /* If this CU is from a DWO file we need to start over, we need to
7175 refetch the attributes from the skeleton CU.
7176 This could be optimized by retrieving those attributes from when we
7177 were here the first time: the previous comp_unit_die was stored in
7178 comp_unit_obstack. But there's no data yet that we need this
7179 optimization. */
7180 if (cu->dwo_unit != NULL)
7181 rereading_dwo_cu = 1;
7182 }
7183 else
7184 {
7185 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7186 in per_objfile yet. */
7187 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7188 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7189 cu = m_new_cu.get ();
7190 }
7191
7192 /* Get the header. */
7193 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7194 {
7195 /* We already have the header, there's no need to read it in again. */
7196 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7197 }
7198 else
7199 {
7200 if (this_cu->is_debug_types)
7201 {
7202 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7203 section, abbrev_section,
7204 info_ptr, rcuh_kind::TYPE);
7205
7206 /* Since per_cu is the first member of struct signatured_type,
7207 we can go from a pointer to one to a pointer to the other. */
7208 sig_type = (struct signatured_type *) this_cu;
7209 gdb_assert (sig_type->signature == cu->header.signature);
7210 gdb_assert (sig_type->type_offset_in_tu
7211 == cu->header.type_cu_offset_in_tu);
7212 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7213
7214 /* LENGTH has not been set yet for type units if we're
7215 using .gdb_index. */
7216 this_cu->length = cu->header.get_length ();
7217
7218 /* Establish the type offset that can be used to lookup the type. */
7219 sig_type->type_offset_in_section =
7220 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7221
7222 this_cu->dwarf_version = cu->header.version;
7223 }
7224 else
7225 {
7226 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7227 section, abbrev_section,
7228 info_ptr,
7229 rcuh_kind::COMPILE);
7230
7231 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7232 if (this_cu->length == 0)
7233 this_cu->length = cu->header.get_length ();
7234 else
7235 gdb_assert (this_cu->length == cu->header.get_length ());
7236 this_cu->dwarf_version = cu->header.version;
7237 }
7238 }
7239
7240 /* Skip dummy compilation units. */
7241 if (info_ptr >= begin_info_ptr + this_cu->length
7242 || peek_abbrev_code (abfd, info_ptr) == 0)
7243 {
7244 dummy_p = true;
7245 return;
7246 }
7247
7248 /* If we don't have them yet, read the abbrevs for this compilation unit.
7249 And if we need to read them now, make sure they're freed when we're
7250 done. */
7251 if (abbrev_table != NULL)
7252 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7253 else
7254 {
7255 abbrev_section->read (objfile);
7256 m_abbrev_table_holder
7257 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7258 abbrev_table = m_abbrev_table_holder.get ();
7259 }
7260
7261 /* Read the top level CU/TU die. */
7262 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7263 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7264
7265 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7266 {
7267 dummy_p = true;
7268 return;
7269 }
7270
7271 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7272 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7273 table from the DWO file and pass the ownership over to us. It will be
7274 referenced from READER, so we must make sure to free it after we're done
7275 with READER.
7276
7277 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7278 DWO CU, that this test will fail (the attribute will not be present). */
7279 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7280 if (dwo_name != nullptr)
7281 {
7282 struct dwo_unit *dwo_unit;
7283 struct die_info *dwo_comp_unit_die;
7284
7285 if (comp_unit_die->has_children)
7286 {
7287 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7288 " has children (offset %s) [in module %s]"),
7289 sect_offset_str (this_cu->sect_off),
7290 bfd_get_filename (abfd));
7291 }
7292 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7293 if (dwo_unit != NULL)
7294 {
7295 if (read_cutu_die_from_dwo (cu, dwo_unit,
7296 comp_unit_die, NULL,
7297 this, &info_ptr,
7298 &dwo_comp_unit_die,
7299 &m_dwo_abbrev_table) == 0)
7300 {
7301 /* Dummy die. */
7302 dummy_p = true;
7303 return;
7304 }
7305 comp_unit_die = dwo_comp_unit_die;
7306 }
7307 else
7308 {
7309 /* Yikes, we couldn't find the rest of the DIE, we only have
7310 the stub. A complaint has already been logged. There's
7311 not much more we can do except pass on the stub DIE to
7312 die_reader_func. We don't want to throw an error on bad
7313 debug info. */
7314 }
7315 }
7316 }
7317
7318 void
7319 cutu_reader::keep ()
7320 {
7321 /* Done, clean up. */
7322 gdb_assert (!dummy_p);
7323 if (m_new_cu != NULL)
7324 {
7325 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7326 now. */
7327 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7328 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7329 }
7330 }
7331
7332 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7333 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7334 assumed to have already done the lookup to find the DWO file).
7335
7336 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7337 THIS_CU->is_debug_types, but nothing else.
7338
7339 We fill in THIS_CU->length.
7340
7341 THIS_CU->cu is always freed when done.
7342 This is done in order to not leave THIS_CU->cu in a state where we have
7343 to care whether it refers to the "main" CU or the DWO CU.
7344
7345 When parent_cu is passed, it is used to provide a default value for
7346 str_offsets_base and addr_base from the parent. */
7347
7348 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7349 dwarf2_per_objfile *per_objfile,
7350 struct dwarf2_cu *parent_cu,
7351 struct dwo_file *dwo_file)
7352 : die_reader_specs {},
7353 m_this_cu (this_cu)
7354 {
7355 struct objfile *objfile = per_objfile->objfile;
7356 struct dwarf2_section_info *section = this_cu->section;
7357 bfd *abfd = section->get_bfd_owner ();
7358 struct dwarf2_section_info *abbrev_section;
7359 const gdb_byte *begin_info_ptr, *info_ptr;
7360
7361 if (dwarf_die_debug)
7362 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7363 this_cu->is_debug_types ? "type" : "comp",
7364 sect_offset_str (this_cu->sect_off));
7365
7366 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7367
7368 abbrev_section = (dwo_file != NULL
7369 ? &dwo_file->sections.abbrev
7370 : get_abbrev_section_for_cu (this_cu));
7371
7372 /* This is cheap if the section is already read in. */
7373 section->read (objfile);
7374
7375 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7376
7377 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7378 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7379 section, abbrev_section, info_ptr,
7380 (this_cu->is_debug_types
7381 ? rcuh_kind::TYPE
7382 : rcuh_kind::COMPILE));
7383
7384 if (parent_cu != nullptr)
7385 {
7386 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7387 m_new_cu->addr_base = parent_cu->addr_base;
7388 }
7389 this_cu->length = m_new_cu->header.get_length ();
7390
7391 /* Skip dummy compilation units. */
7392 if (info_ptr >= begin_info_ptr + this_cu->length
7393 || peek_abbrev_code (abfd, info_ptr) == 0)
7394 {
7395 dummy_p = true;
7396 return;
7397 }
7398
7399 abbrev_section->read (objfile);
7400 m_abbrev_table_holder
7401 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7402
7403 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7404 m_abbrev_table_holder.get ());
7405 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7406 }
7407
7408 \f
7409 /* Type Unit Groups.
7410
7411 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7412 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7413 so that all types coming from the same compilation (.o file) are grouped
7414 together. A future step could be to put the types in the same symtab as
7415 the CU the types ultimately came from. */
7416
7417 static hashval_t
7418 hash_type_unit_group (const void *item)
7419 {
7420 const struct type_unit_group *tu_group
7421 = (const struct type_unit_group *) item;
7422
7423 return hash_stmt_list_entry (&tu_group->hash);
7424 }
7425
7426 static int
7427 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7428 {
7429 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7430 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7431
7432 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7433 }
7434
7435 /* Allocate a hash table for type unit groups. */
7436
7437 static htab_up
7438 allocate_type_unit_groups_table ()
7439 {
7440 return htab_up (htab_create_alloc (3,
7441 hash_type_unit_group,
7442 eq_type_unit_group,
7443 NULL, xcalloc, xfree));
7444 }
7445
7446 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7447 partial symtabs. We combine several TUs per psymtab to not let the size
7448 of any one psymtab grow too big. */
7449 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7450 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7451
7452 /* Helper routine for get_type_unit_group.
7453 Create the type_unit_group object used to hold one or more TUs. */
7454
7455 static struct type_unit_group *
7456 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7457 {
7458 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7459 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7460 struct dwarf2_per_cu_data *per_cu;
7461 struct type_unit_group *tu_group;
7462
7463 tu_group = OBSTACK_ZALLOC (&per_bfd->obstack, type_unit_group);
7464 per_cu = &tu_group->per_cu;
7465 per_cu->per_bfd = per_bfd;
7466
7467 if (per_bfd->using_index)
7468 {
7469 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7470 struct dwarf2_per_cu_quick_data);
7471 }
7472 else
7473 {
7474 unsigned int line_offset = to_underlying (line_offset_struct);
7475 dwarf2_psymtab *pst;
7476 std::string name;
7477
7478 /* Give the symtab a useful name for debug purposes. */
7479 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7480 name = string_printf ("<type_units_%d>",
7481 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7482 else
7483 name = string_printf ("<type_units_at_0x%x>", line_offset);
7484
7485 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7486 pst->anonymous = true;
7487 }
7488
7489 tu_group->hash.dwo_unit = cu->dwo_unit;
7490 tu_group->hash.line_sect_off = line_offset_struct;
7491
7492 return tu_group;
7493 }
7494
7495 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7496 STMT_LIST is a DW_AT_stmt_list attribute. */
7497
7498 static struct type_unit_group *
7499 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7500 {
7501 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7502 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7503 struct type_unit_group *tu_group;
7504 void **slot;
7505 unsigned int line_offset;
7506 struct type_unit_group type_unit_group_for_lookup;
7507
7508 if (per_objfile->per_bfd->type_unit_groups == NULL)
7509 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7510
7511 /* Do we need to create a new group, or can we use an existing one? */
7512
7513 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7514 {
7515 line_offset = stmt_list->as_unsigned ();
7516 ++tu_stats->nr_symtab_sharers;
7517 }
7518 else
7519 {
7520 /* Ugh, no stmt_list. Rare, but we have to handle it.
7521 We can do various things here like create one group per TU or
7522 spread them over multiple groups to split up the expansion work.
7523 To avoid worst case scenarios (too many groups or too large groups)
7524 we, umm, group them in bunches. */
7525 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7526 | (tu_stats->nr_stmt_less_type_units
7527 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7528 ++tu_stats->nr_stmt_less_type_units;
7529 }
7530
7531 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7532 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7533 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7534 &type_unit_group_for_lookup, INSERT);
7535 if (*slot != NULL)
7536 {
7537 tu_group = (struct type_unit_group *) *slot;
7538 gdb_assert (tu_group != NULL);
7539 }
7540 else
7541 {
7542 sect_offset line_offset_struct = (sect_offset) line_offset;
7543 tu_group = create_type_unit_group (cu, line_offset_struct);
7544 *slot = tu_group;
7545 ++tu_stats->nr_symtabs;
7546 }
7547
7548 return tu_group;
7549 }
7550 \f
7551 /* Partial symbol tables. */
7552
7553 /* Create a psymtab named NAME and assign it to PER_CU.
7554
7555 The caller must fill in the following details:
7556 dirname, textlow, texthigh. */
7557
7558 static dwarf2_psymtab *
7559 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7560 dwarf2_per_objfile *per_objfile,
7561 const char *name)
7562 {
7563 dwarf2_psymtab *pst
7564 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7565 per_objfile->objfile->per_bfd, per_cu);
7566
7567 pst->psymtabs_addrmap_supported = true;
7568
7569 /* This is the glue that links PST into GDB's symbol API. */
7570 per_cu->v.psymtab = pst;
7571
7572 return pst;
7573 }
7574
7575 /* DIE reader function for process_psymtab_comp_unit. */
7576
7577 static void
7578 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7579 const gdb_byte *info_ptr,
7580 struct die_info *comp_unit_die,
7581 enum language pretend_language)
7582 {
7583 struct dwarf2_cu *cu = reader->cu;
7584 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7585 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7586 struct objfile *objfile = per_objfile->objfile;
7587 struct gdbarch *gdbarch = objfile->arch ();
7588 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7589 CORE_ADDR baseaddr;
7590 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7591 dwarf2_psymtab *pst;
7592 enum pc_bounds_kind cu_bounds_kind;
7593 const char *filename;
7594
7595 gdb_assert (! per_cu->is_debug_types);
7596
7597 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7598
7599 /* Allocate a new partial symbol table structure. */
7600 gdb::unique_xmalloc_ptr<char> debug_filename;
7601 static const char artificial[] = "<artificial>";
7602 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7603 if (filename == NULL)
7604 filename = "";
7605 else if (strcmp (filename, artificial) == 0)
7606 {
7607 debug_filename.reset (concat (artificial, "@",
7608 sect_offset_str (per_cu->sect_off),
7609 (char *) NULL));
7610 filename = debug_filename.get ();
7611 }
7612
7613 pst = create_partial_symtab (per_cu, per_objfile, filename);
7614
7615 /* This must be done before calling dwarf2_build_include_psymtabs. */
7616 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7617
7618 baseaddr = objfile->text_section_offset ();
7619
7620 dwarf2_find_base_address (comp_unit_die, cu);
7621
7622 /* Possibly set the default values of LOWPC and HIGHPC from
7623 `DW_AT_ranges'. */
7624 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7625 &best_highpc, cu, pst);
7626 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7627 {
7628 CORE_ADDR low
7629 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7630 - baseaddr);
7631 CORE_ADDR high
7632 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7633 - baseaddr - 1);
7634 /* Store the contiguous range if it is not empty; it can be
7635 empty for CUs with no code. */
7636 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7637 low, high, pst);
7638 }
7639
7640 /* Check if comp unit has_children.
7641 If so, read the rest of the partial symbols from this comp unit.
7642 If not, there's no more debug_info for this comp unit. */
7643 if (comp_unit_die->has_children)
7644 {
7645 struct partial_die_info *first_die;
7646 CORE_ADDR lowpc, highpc;
7647
7648 lowpc = ((CORE_ADDR) -1);
7649 highpc = ((CORE_ADDR) 0);
7650
7651 first_die = load_partial_dies (reader, info_ptr, 1);
7652
7653 scan_partial_symbols (first_die, &lowpc, &highpc,
7654 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7655
7656 /* If we didn't find a lowpc, set it to highpc to avoid
7657 complaints from `maint check'. */
7658 if (lowpc == ((CORE_ADDR) -1))
7659 lowpc = highpc;
7660
7661 /* If the compilation unit didn't have an explicit address range,
7662 then use the information extracted from its child dies. */
7663 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7664 {
7665 best_lowpc = lowpc;
7666 best_highpc = highpc;
7667 }
7668 }
7669 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7670 best_lowpc + baseaddr)
7671 - baseaddr);
7672 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7673 best_highpc + baseaddr)
7674 - baseaddr);
7675
7676 pst->end ();
7677
7678 if (!cu->per_cu->imported_symtabs_empty ())
7679 {
7680 int i;
7681 int len = cu->per_cu->imported_symtabs_size ();
7682
7683 /* Fill in 'dependencies' here; we fill in 'users' in a
7684 post-pass. */
7685 pst->number_of_dependencies = len;
7686 pst->dependencies
7687 = per_bfd->partial_symtabs->allocate_dependencies (len);
7688 for (i = 0; i < len; ++i)
7689 {
7690 pst->dependencies[i]
7691 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7692 }
7693
7694 cu->per_cu->imported_symtabs_free ();
7695 }
7696
7697 /* Get the list of files included in the current compilation unit,
7698 and build a psymtab for each of them. */
7699 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7700
7701 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7702 ", %d global, %d static syms",
7703 per_cu->is_debug_types ? "type" : "comp",
7704 sect_offset_str (per_cu->sect_off),
7705 paddress (gdbarch, pst->text_low (objfile)),
7706 paddress (gdbarch, pst->text_high (objfile)),
7707 (int) pst->global_psymbols.size (),
7708 (int) pst->static_psymbols.size ());
7709 }
7710
7711 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7712 Process compilation unit THIS_CU for a psymtab. */
7713
7714 static void
7715 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7716 dwarf2_per_objfile *per_objfile,
7717 bool want_partial_unit,
7718 enum language pretend_language)
7719 {
7720 /* If this compilation unit was already read in, free the
7721 cached copy in order to read it in again. This is
7722 necessary because we skipped some symbols when we first
7723 read in the compilation unit (see load_partial_dies).
7724 This problem could be avoided, but the benefit is unclear. */
7725 per_objfile->remove_cu (this_cu);
7726
7727 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7728
7729 switch (reader.comp_unit_die->tag)
7730 {
7731 case DW_TAG_compile_unit:
7732 this_cu->unit_type = DW_UT_compile;
7733 break;
7734 case DW_TAG_partial_unit:
7735 this_cu->unit_type = DW_UT_partial;
7736 break;
7737 case DW_TAG_type_unit:
7738 this_cu->unit_type = DW_UT_type;
7739 break;
7740 default:
7741 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7742 dwarf_tag_name (reader.comp_unit_die->tag),
7743 sect_offset_str (reader.cu->per_cu->sect_off),
7744 objfile_name (per_objfile->objfile));
7745 }
7746
7747 if (reader.dummy_p)
7748 {
7749 /* Nothing. */
7750 }
7751 else if (this_cu->is_debug_types)
7752 build_type_psymtabs_reader (&reader, reader.info_ptr,
7753 reader.comp_unit_die);
7754 else if (want_partial_unit
7755 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7756 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7757 reader.comp_unit_die,
7758 pretend_language);
7759
7760 this_cu->lang = reader.cu->language;
7761
7762 /* Age out any secondary CUs. */
7763 per_objfile->age_comp_units ();
7764 }
7765
7766 /* Reader function for build_type_psymtabs. */
7767
7768 static void
7769 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7770 const gdb_byte *info_ptr,
7771 struct die_info *type_unit_die)
7772 {
7773 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7774 struct dwarf2_cu *cu = reader->cu;
7775 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7776 struct signatured_type *sig_type;
7777 struct type_unit_group *tu_group;
7778 struct attribute *attr;
7779 struct partial_die_info *first_die;
7780 CORE_ADDR lowpc, highpc;
7781 dwarf2_psymtab *pst;
7782
7783 gdb_assert (per_cu->is_debug_types);
7784 sig_type = (struct signatured_type *) per_cu;
7785
7786 if (! type_unit_die->has_children)
7787 return;
7788
7789 attr = type_unit_die->attr (DW_AT_stmt_list);
7790 tu_group = get_type_unit_group (cu, attr);
7791
7792 if (tu_group->tus == nullptr)
7793 tu_group->tus = new std::vector<signatured_type *>;
7794 tu_group->tus->push_back (sig_type);
7795
7796 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7797 pst = create_partial_symtab (per_cu, per_objfile, "");
7798 pst->anonymous = true;
7799
7800 first_die = load_partial_dies (reader, info_ptr, 1);
7801
7802 lowpc = (CORE_ADDR) -1;
7803 highpc = (CORE_ADDR) 0;
7804 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7805
7806 pst->end ();
7807 }
7808
7809 /* Struct used to sort TUs by their abbreviation table offset. */
7810
7811 struct tu_abbrev_offset
7812 {
7813 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7814 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7815 {}
7816
7817 signatured_type *sig_type;
7818 sect_offset abbrev_offset;
7819 };
7820
7821 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7822
7823 static bool
7824 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7825 const struct tu_abbrev_offset &b)
7826 {
7827 return a.abbrev_offset < b.abbrev_offset;
7828 }
7829
7830 /* Efficiently read all the type units.
7831 This does the bulk of the work for build_type_psymtabs.
7832
7833 The efficiency is because we sort TUs by the abbrev table they use and
7834 only read each abbrev table once. In one program there are 200K TUs
7835 sharing 8K abbrev tables.
7836
7837 The main purpose of this function is to support building the
7838 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7839 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7840 can collapse the search space by grouping them by stmt_list.
7841 The savings can be significant, in the same program from above the 200K TUs
7842 share 8K stmt_list tables.
7843
7844 FUNC is expected to call get_type_unit_group, which will create the
7845 struct type_unit_group if necessary and add it to
7846 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7847
7848 static void
7849 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7850 {
7851 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7852 abbrev_table_up abbrev_table;
7853 sect_offset abbrev_offset;
7854
7855 /* It's up to the caller to not call us multiple times. */
7856 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7857
7858 if (per_objfile->per_bfd->all_type_units.empty ())
7859 return;
7860
7861 /* TUs typically share abbrev tables, and there can be way more TUs than
7862 abbrev tables. Sort by abbrev table to reduce the number of times we
7863 read each abbrev table in.
7864 Alternatives are to punt or to maintain a cache of abbrev tables.
7865 This is simpler and efficient enough for now.
7866
7867 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7868 symtab to use). Typically TUs with the same abbrev offset have the same
7869 stmt_list value too so in practice this should work well.
7870
7871 The basic algorithm here is:
7872
7873 sort TUs by abbrev table
7874 for each TU with same abbrev table:
7875 read abbrev table if first user
7876 read TU top level DIE
7877 [IWBN if DWO skeletons had DW_AT_stmt_list]
7878 call FUNC */
7879
7880 dwarf_read_debug_printf ("Building type unit groups ...");
7881
7882 /* Sort in a separate table to maintain the order of all_type_units
7883 for .gdb_index: TU indices directly index all_type_units. */
7884 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7885 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7886
7887 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7888 sorted_by_abbrev.emplace_back
7889 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7890 sig_type->per_cu.sect_off));
7891
7892 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7893 sort_tu_by_abbrev_offset);
7894
7895 abbrev_offset = (sect_offset) ~(unsigned) 0;
7896
7897 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7898 {
7899 /* Switch to the next abbrev table if necessary. */
7900 if (abbrev_table == NULL
7901 || tu.abbrev_offset != abbrev_offset)
7902 {
7903 abbrev_offset = tu.abbrev_offset;
7904 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7905 abbrev_table =
7906 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7907 ++tu_stats->nr_uniq_abbrev_tables;
7908 }
7909
7910 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7911 abbrev_table.get (), nullptr, false);
7912 if (!reader.dummy_p)
7913 build_type_psymtabs_reader (&reader, reader.info_ptr,
7914 reader.comp_unit_die);
7915 }
7916 }
7917
7918 /* Print collected type unit statistics. */
7919
7920 static void
7921 print_tu_stats (dwarf2_per_objfile *per_objfile)
7922 {
7923 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7924
7925 dwarf_read_debug_printf ("Type unit statistics:");
7926 dwarf_read_debug_printf (" %zu TUs",
7927 per_objfile->per_bfd->all_type_units.size ());
7928 dwarf_read_debug_printf (" %d uniq abbrev tables",
7929 tu_stats->nr_uniq_abbrev_tables);
7930 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7931 tu_stats->nr_symtabs);
7932 dwarf_read_debug_printf (" %d symtab sharers",
7933 tu_stats->nr_symtab_sharers);
7934 dwarf_read_debug_printf (" %d type units without a stmt_list",
7935 tu_stats->nr_stmt_less_type_units);
7936 dwarf_read_debug_printf (" %d all_type_units reallocs",
7937 tu_stats->nr_all_type_units_reallocs);
7938 }
7939
7940 /* Traversal function for build_type_psymtabs. */
7941
7942 static int
7943 build_type_psymtab_dependencies (void **slot, void *info)
7944 {
7945 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7946 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7947 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7948 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7949 dwarf2_psymtab *pst = per_cu->v.psymtab;
7950 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7951 int i;
7952
7953 gdb_assert (len > 0);
7954 gdb_assert (per_cu->type_unit_group_p ());
7955
7956 pst->number_of_dependencies = len;
7957 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7958 for (i = 0; i < len; ++i)
7959 {
7960 struct signatured_type *iter = tu_group->tus->at (i);
7961 gdb_assert (iter->per_cu.is_debug_types);
7962 pst->dependencies[i] = iter->per_cu.v.psymtab;
7963 iter->type_unit_group = tu_group;
7964 }
7965
7966 delete tu_group->tus;
7967 tu_group->tus = nullptr;
7968
7969 return 1;
7970 }
7971
7972 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7973 Build partial symbol tables for the .debug_types comp-units. */
7974
7975 static void
7976 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7977 {
7978 if (! create_all_type_units (per_objfile))
7979 return;
7980
7981 build_type_psymtabs_1 (per_objfile);
7982 }
7983
7984 /* Traversal function for process_skeletonless_type_unit.
7985 Read a TU in a DWO file and build partial symbols for it. */
7986
7987 static int
7988 process_skeletonless_type_unit (void **slot, void *info)
7989 {
7990 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7991 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7992 struct signatured_type find_entry, *entry;
7993
7994 /* If this TU doesn't exist in the global table, add it and read it in. */
7995
7996 if (per_objfile->per_bfd->signatured_types == NULL)
7997 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7998
7999 find_entry.signature = dwo_unit->signature;
8000 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
8001 &find_entry, INSERT);
8002 /* If we've already seen this type there's nothing to do. What's happening
8003 is we're doing our own version of comdat-folding here. */
8004 if (*slot != NULL)
8005 return 1;
8006
8007 /* This does the job that create_all_type_units would have done for
8008 this TU. */
8009 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8010 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
8011 *slot = entry;
8012
8013 /* This does the job that build_type_psymtabs_1 would have done. */
8014 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
8015 if (!reader.dummy_p)
8016 build_type_psymtabs_reader (&reader, reader.info_ptr,
8017 reader.comp_unit_die);
8018
8019 return 1;
8020 }
8021
8022 /* Traversal function for process_skeletonless_type_units. */
8023
8024 static int
8025 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8026 {
8027 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8028
8029 if (dwo_file->tus != NULL)
8030 htab_traverse_noresize (dwo_file->tus.get (),
8031 process_skeletonless_type_unit, info);
8032
8033 return 1;
8034 }
8035
8036 /* Scan all TUs of DWO files, verifying we've processed them.
8037 This is needed in case a TU was emitted without its skeleton.
8038 Note: This can't be done until we know what all the DWO files are. */
8039
8040 static void
8041 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8042 {
8043 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8044 if (get_dwp_file (per_objfile) == NULL
8045 && per_objfile->per_bfd->dwo_files != NULL)
8046 {
8047 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8048 process_dwo_file_for_skeletonless_type_units,
8049 per_objfile);
8050 }
8051 }
8052
8053 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8054
8055 static void
8056 set_partial_user (dwarf2_per_objfile *per_objfile)
8057 {
8058 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8059 {
8060 dwarf2_psymtab *pst = per_cu->v.psymtab;
8061
8062 if (pst == NULL)
8063 continue;
8064
8065 for (int j = 0; j < pst->number_of_dependencies; ++j)
8066 {
8067 /* Set the 'user' field only if it is not already set. */
8068 if (pst->dependencies[j]->user == NULL)
8069 pst->dependencies[j]->user = pst;
8070 }
8071 }
8072 }
8073
8074 /* Build the partial symbol table by doing a quick pass through the
8075 .debug_info and .debug_abbrev sections. */
8076
8077 static void
8078 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8079 {
8080 struct objfile *objfile = per_objfile->objfile;
8081 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
8082
8083 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8084 objfile_name (objfile));
8085
8086 scoped_restore restore_reading_psyms
8087 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
8088
8089 per_bfd->info.read (objfile);
8090
8091 /* Any cached compilation units will be linked by the per-objfile
8092 read_in_chain. Make sure to free them when we're done. */
8093 free_cached_comp_units freer (per_objfile);
8094
8095 build_type_psymtabs (per_objfile);
8096
8097 create_all_comp_units (per_objfile);
8098
8099 /* Create a temporary address map on a temporary obstack. We later
8100 copy this to the final obstack. */
8101 auto_obstack temp_obstack;
8102
8103 scoped_restore save_psymtabs_addrmap
8104 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
8105 addrmap_create_mutable (&temp_obstack));
8106
8107 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
8108 {
8109 if (per_cu->v.psymtab != NULL)
8110 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8111 continue;
8112 process_psymtab_comp_unit (per_cu, per_objfile, false,
8113 language_minimal);
8114 }
8115
8116 /* This has to wait until we read the CUs, we need the list of DWOs. */
8117 process_skeletonless_type_units (per_objfile);
8118
8119 /* Now that all TUs have been processed we can fill in the dependencies. */
8120 if (per_bfd->type_unit_groups != NULL)
8121 {
8122 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
8123 build_type_psymtab_dependencies, per_objfile);
8124 }
8125
8126 if (dwarf_read_debug > 0)
8127 print_tu_stats (per_objfile);
8128
8129 set_partial_user (per_objfile);
8130
8131 per_bfd->partial_symtabs->psymtabs_addrmap
8132 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
8133 per_bfd->partial_symtabs->obstack ());
8134 /* At this point we want to keep the address map. */
8135 save_psymtabs_addrmap.release ();
8136
8137 dwarf_read_debug_printf ("Done building psymtabs of %s",
8138 objfile_name (objfile));
8139 }
8140
8141 /* Load the partial DIEs for a secondary CU into memory.
8142 This is also used when rereading a primary CU with load_all_dies. */
8143
8144 static void
8145 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8146 dwarf2_per_objfile *per_objfile,
8147 dwarf2_cu *existing_cu)
8148 {
8149 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8150
8151 if (!reader.dummy_p)
8152 {
8153 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8154 language_minimal);
8155
8156 /* Check if comp unit has_children.
8157 If so, read the rest of the partial symbols from this comp unit.
8158 If not, there's no more debug_info for this comp unit. */
8159 if (reader.comp_unit_die->has_children)
8160 load_partial_dies (&reader, reader.info_ptr, 0);
8161
8162 reader.keep ();
8163 }
8164 }
8165
8166 static void
8167 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8168 struct dwarf2_section_info *section,
8169 struct dwarf2_section_info *abbrev_section,
8170 unsigned int is_dwz)
8171 {
8172 const gdb_byte *info_ptr;
8173 struct objfile *objfile = per_objfile->objfile;
8174
8175 dwarf_read_debug_printf ("Reading %s for %s",
8176 section->get_name (),
8177 section->get_file_name ());
8178
8179 section->read (objfile);
8180
8181 info_ptr = section->buffer;
8182
8183 while (info_ptr < section->buffer + section->size)
8184 {
8185 struct dwarf2_per_cu_data *this_cu;
8186
8187 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8188
8189 comp_unit_head cu_header;
8190 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8191 abbrev_section, info_ptr,
8192 rcuh_kind::COMPILE);
8193
8194 /* Save the compilation unit for later lookup. */
8195 if (cu_header.unit_type != DW_UT_type)
8196 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8197 else
8198 {
8199 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8200 sig_type->signature = cu_header.signature;
8201 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8202 this_cu = &sig_type->per_cu;
8203 }
8204 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8205 this_cu->sect_off = sect_off;
8206 this_cu->length = cu_header.length + cu_header.initial_length_size;
8207 this_cu->is_dwz = is_dwz;
8208 this_cu->section = section;
8209
8210 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8211
8212 info_ptr = info_ptr + this_cu->length;
8213 }
8214 }
8215
8216 /* Create a list of all compilation units in OBJFILE.
8217 This is only done for -readnow and building partial symtabs. */
8218
8219 static void
8220 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8221 {
8222 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8223 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8224 &per_objfile->per_bfd->abbrev, 0);
8225
8226 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8227 if (dwz != NULL)
8228 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8229 }
8230
8231 /* Process all loaded DIEs for compilation unit CU, starting at
8232 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8233 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8234 DW_AT_ranges). See the comments of add_partial_subprogram on how
8235 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8236
8237 static void
8238 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8239 CORE_ADDR *highpc, int set_addrmap,
8240 struct dwarf2_cu *cu)
8241 {
8242 struct partial_die_info *pdi;
8243
8244 /* Now, march along the PDI's, descending into ones which have
8245 interesting children but skipping the children of the other ones,
8246 until we reach the end of the compilation unit. */
8247
8248 pdi = first_die;
8249
8250 while (pdi != NULL)
8251 {
8252 pdi->fixup (cu);
8253
8254 /* Anonymous namespaces or modules have no name but have interesting
8255 children, so we need to look at them. Ditto for anonymous
8256 enums. */
8257
8258 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8259 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8260 || pdi->tag == DW_TAG_imported_unit
8261 || pdi->tag == DW_TAG_inlined_subroutine)
8262 {
8263 switch (pdi->tag)
8264 {
8265 case DW_TAG_subprogram:
8266 case DW_TAG_inlined_subroutine:
8267 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8268 if (cu->language == language_cplus)
8269 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8270 set_addrmap, cu);
8271 break;
8272 case DW_TAG_constant:
8273 case DW_TAG_variable:
8274 case DW_TAG_typedef:
8275 case DW_TAG_union_type:
8276 if (!pdi->is_declaration
8277 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8278 {
8279 add_partial_symbol (pdi, cu);
8280 }
8281 break;
8282 case DW_TAG_class_type:
8283 case DW_TAG_interface_type:
8284 case DW_TAG_structure_type:
8285 if (!pdi->is_declaration)
8286 {
8287 add_partial_symbol (pdi, cu);
8288 }
8289 if ((cu->language == language_rust
8290 || cu->language == language_cplus) && pdi->has_children)
8291 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8292 set_addrmap, cu);
8293 break;
8294 case DW_TAG_enumeration_type:
8295 if (!pdi->is_declaration)
8296 add_partial_enumeration (pdi, cu);
8297 break;
8298 case DW_TAG_base_type:
8299 case DW_TAG_subrange_type:
8300 /* File scope base type definitions are added to the partial
8301 symbol table. */
8302 add_partial_symbol (pdi, cu);
8303 break;
8304 case DW_TAG_namespace:
8305 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8306 break;
8307 case DW_TAG_module:
8308 if (!pdi->is_declaration)
8309 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8310 break;
8311 case DW_TAG_imported_unit:
8312 {
8313 struct dwarf2_per_cu_data *per_cu;
8314
8315 /* For now we don't handle imported units in type units. */
8316 if (cu->per_cu->is_debug_types)
8317 {
8318 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8319 " supported in type units [in module %s]"),
8320 objfile_name (cu->per_objfile->objfile));
8321 }
8322
8323 per_cu = dwarf2_find_containing_comp_unit
8324 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8325
8326 /* Go read the partial unit, if needed. */
8327 if (per_cu->v.psymtab == NULL)
8328 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8329 cu->language);
8330
8331 cu->per_cu->imported_symtabs_push (per_cu);
8332 }
8333 break;
8334 case DW_TAG_imported_declaration:
8335 add_partial_symbol (pdi, cu);
8336 break;
8337 default:
8338 break;
8339 }
8340 }
8341
8342 /* If the die has a sibling, skip to the sibling. */
8343
8344 pdi = pdi->die_sibling;
8345 }
8346 }
8347
8348 /* Functions used to compute the fully scoped name of a partial DIE.
8349
8350 Normally, this is simple. For C++, the parent DIE's fully scoped
8351 name is concatenated with "::" and the partial DIE's name.
8352 Enumerators are an exception; they use the scope of their parent
8353 enumeration type, i.e. the name of the enumeration type is not
8354 prepended to the enumerator.
8355
8356 There are two complexities. One is DW_AT_specification; in this
8357 case "parent" means the parent of the target of the specification,
8358 instead of the direct parent of the DIE. The other is compilers
8359 which do not emit DW_TAG_namespace; in this case we try to guess
8360 the fully qualified name of structure types from their members'
8361 linkage names. This must be done using the DIE's children rather
8362 than the children of any DW_AT_specification target. We only need
8363 to do this for structures at the top level, i.e. if the target of
8364 any DW_AT_specification (if any; otherwise the DIE itself) does not
8365 have a parent. */
8366
8367 /* Compute the scope prefix associated with PDI's parent, in
8368 compilation unit CU. The result will be allocated on CU's
8369 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8370 field. NULL is returned if no prefix is necessary. */
8371 static const char *
8372 partial_die_parent_scope (struct partial_die_info *pdi,
8373 struct dwarf2_cu *cu)
8374 {
8375 const char *grandparent_scope;
8376 struct partial_die_info *parent, *real_pdi;
8377
8378 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8379 then this means the parent of the specification DIE. */
8380
8381 real_pdi = pdi;
8382 while (real_pdi->has_specification)
8383 {
8384 auto res = find_partial_die (real_pdi->spec_offset,
8385 real_pdi->spec_is_dwz, cu);
8386 real_pdi = res.pdi;
8387 cu = res.cu;
8388 }
8389
8390 parent = real_pdi->die_parent;
8391 if (parent == NULL)
8392 return NULL;
8393
8394 if (parent->scope_set)
8395 return parent->scope;
8396
8397 parent->fixup (cu);
8398
8399 grandparent_scope = partial_die_parent_scope (parent, cu);
8400
8401 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8402 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8403 Work around this problem here. */
8404 if (cu->language == language_cplus
8405 && parent->tag == DW_TAG_namespace
8406 && strcmp (parent->name (cu), "::") == 0
8407 && grandparent_scope == NULL)
8408 {
8409 parent->scope = NULL;
8410 parent->scope_set = 1;
8411 return NULL;
8412 }
8413
8414 /* Nested subroutines in Fortran get a prefix. */
8415 if (pdi->tag == DW_TAG_enumerator)
8416 /* Enumerators should not get the name of the enumeration as a prefix. */
8417 parent->scope = grandparent_scope;
8418 else if (parent->tag == DW_TAG_namespace
8419 || parent->tag == DW_TAG_module
8420 || parent->tag == DW_TAG_structure_type
8421 || parent->tag == DW_TAG_class_type
8422 || parent->tag == DW_TAG_interface_type
8423 || parent->tag == DW_TAG_union_type
8424 || parent->tag == DW_TAG_enumeration_type
8425 || (cu->language == language_fortran
8426 && parent->tag == DW_TAG_subprogram
8427 && pdi->tag == DW_TAG_subprogram))
8428 {
8429 if (grandparent_scope == NULL)
8430 parent->scope = parent->name (cu);
8431 else
8432 parent->scope = typename_concat (&cu->comp_unit_obstack,
8433 grandparent_scope,
8434 parent->name (cu), 0, cu);
8435 }
8436 else
8437 {
8438 /* FIXME drow/2004-04-01: What should we be doing with
8439 function-local names? For partial symbols, we should probably be
8440 ignoring them. */
8441 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8442 dwarf_tag_name (parent->tag),
8443 sect_offset_str (pdi->sect_off));
8444 parent->scope = grandparent_scope;
8445 }
8446
8447 parent->scope_set = 1;
8448 return parent->scope;
8449 }
8450
8451 /* Return the fully scoped name associated with PDI, from compilation unit
8452 CU. The result will be allocated with malloc. */
8453
8454 static gdb::unique_xmalloc_ptr<char>
8455 partial_die_full_name (struct partial_die_info *pdi,
8456 struct dwarf2_cu *cu)
8457 {
8458 const char *parent_scope;
8459
8460 /* If this is a template instantiation, we can not work out the
8461 template arguments from partial DIEs. So, unfortunately, we have
8462 to go through the full DIEs. At least any work we do building
8463 types here will be reused if full symbols are loaded later. */
8464 if (pdi->has_template_arguments)
8465 {
8466 pdi->fixup (cu);
8467
8468 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8469 {
8470 struct die_info *die;
8471 struct attribute attr;
8472 struct dwarf2_cu *ref_cu = cu;
8473
8474 /* DW_FORM_ref_addr is using section offset. */
8475 attr.name = (enum dwarf_attribute) 0;
8476 attr.form = DW_FORM_ref_addr;
8477 attr.u.unsnd = to_underlying (pdi->sect_off);
8478 die = follow_die_ref (NULL, &attr, &ref_cu);
8479
8480 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8481 }
8482 }
8483
8484 parent_scope = partial_die_parent_scope (pdi, cu);
8485 if (parent_scope == NULL)
8486 return NULL;
8487 else
8488 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8489 pdi->name (cu),
8490 0, cu));
8491 }
8492
8493 static void
8494 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8495 {
8496 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8497 struct objfile *objfile = per_objfile->objfile;
8498 struct gdbarch *gdbarch = objfile->arch ();
8499 CORE_ADDR addr = 0;
8500 const char *actual_name = NULL;
8501 CORE_ADDR baseaddr;
8502
8503 baseaddr = objfile->text_section_offset ();
8504
8505 gdb::unique_xmalloc_ptr<char> built_actual_name
8506 = partial_die_full_name (pdi, cu);
8507 if (built_actual_name != NULL)
8508 actual_name = built_actual_name.get ();
8509
8510 if (actual_name == NULL)
8511 actual_name = pdi->name (cu);
8512
8513 partial_symbol psymbol;
8514 memset (&psymbol, 0, sizeof (psymbol));
8515 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8516 psymbol.ginfo.set_section_index (-1);
8517
8518 /* The code below indicates that the psymbol should be installed by
8519 setting this. */
8520 gdb::optional<psymbol_placement> where;
8521
8522 switch (pdi->tag)
8523 {
8524 case DW_TAG_inlined_subroutine:
8525 case DW_TAG_subprogram:
8526 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8527 - baseaddr);
8528 if (pdi->is_external
8529 || cu->language == language_ada
8530 || (cu->language == language_fortran
8531 && pdi->die_parent != NULL
8532 && pdi->die_parent->tag == DW_TAG_subprogram))
8533 {
8534 /* Normally, only "external" DIEs are part of the global scope.
8535 But in Ada and Fortran, we want to be able to access nested
8536 procedures globally. So all Ada and Fortran subprograms are
8537 stored in the global scope. */
8538 where = psymbol_placement::GLOBAL;
8539 }
8540 else
8541 where = psymbol_placement::STATIC;
8542
8543 psymbol.domain = VAR_DOMAIN;
8544 psymbol.aclass = LOC_BLOCK;
8545 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8546 psymbol.ginfo.value.address = addr;
8547
8548 if (pdi->main_subprogram && actual_name != NULL)
8549 set_objfile_main_name (objfile, actual_name, cu->language);
8550 break;
8551 case DW_TAG_constant:
8552 psymbol.domain = VAR_DOMAIN;
8553 psymbol.aclass = LOC_STATIC;
8554 where = (pdi->is_external
8555 ? psymbol_placement::GLOBAL
8556 : psymbol_placement::STATIC);
8557 break;
8558 case DW_TAG_variable:
8559 if (pdi->d.locdesc)
8560 addr = decode_locdesc (pdi->d.locdesc, cu);
8561
8562 if (pdi->d.locdesc
8563 && addr == 0
8564 && !per_objfile->per_bfd->has_section_at_zero)
8565 {
8566 /* A global or static variable may also have been stripped
8567 out by the linker if unused, in which case its address
8568 will be nullified; do not add such variables into partial
8569 symbol table then. */
8570 }
8571 else if (pdi->is_external)
8572 {
8573 /* Global Variable.
8574 Don't enter into the minimal symbol tables as there is
8575 a minimal symbol table entry from the ELF symbols already.
8576 Enter into partial symbol table if it has a location
8577 descriptor or a type.
8578 If the location descriptor is missing, new_symbol will create
8579 a LOC_UNRESOLVED symbol, the address of the variable will then
8580 be determined from the minimal symbol table whenever the variable
8581 is referenced.
8582 The address for the partial symbol table entry is not
8583 used by GDB, but it comes in handy for debugging partial symbol
8584 table building. */
8585
8586 if (pdi->d.locdesc || pdi->has_type)
8587 {
8588 psymbol.domain = VAR_DOMAIN;
8589 psymbol.aclass = LOC_STATIC;
8590 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8591 psymbol.ginfo.value.address = addr;
8592 where = psymbol_placement::GLOBAL;
8593 }
8594 }
8595 else
8596 {
8597 int has_loc = pdi->d.locdesc != NULL;
8598
8599 /* Static Variable. Skip symbols whose value we cannot know (those
8600 without location descriptors or constant values). */
8601 if (!has_loc && !pdi->has_const_value)
8602 return;
8603
8604 psymbol.domain = VAR_DOMAIN;
8605 psymbol.aclass = LOC_STATIC;
8606 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8607 if (has_loc)
8608 psymbol.ginfo.value.address = addr;
8609 where = psymbol_placement::STATIC;
8610 }
8611 break;
8612 case DW_TAG_array_type:
8613 case DW_TAG_typedef:
8614 case DW_TAG_base_type:
8615 case DW_TAG_subrange_type:
8616 psymbol.domain = VAR_DOMAIN;
8617 psymbol.aclass = LOC_TYPEDEF;
8618 where = psymbol_placement::STATIC;
8619 break;
8620 case DW_TAG_imported_declaration:
8621 case DW_TAG_namespace:
8622 psymbol.domain = VAR_DOMAIN;
8623 psymbol.aclass = LOC_TYPEDEF;
8624 where = psymbol_placement::GLOBAL;
8625 break;
8626 case DW_TAG_module:
8627 /* With Fortran 77 there might be a "BLOCK DATA" module
8628 available without any name. If so, we skip the module as it
8629 doesn't bring any value. */
8630 if (actual_name != nullptr)
8631 {
8632 psymbol.domain = MODULE_DOMAIN;
8633 psymbol.aclass = LOC_TYPEDEF;
8634 where = psymbol_placement::GLOBAL;
8635 }
8636 break;
8637 case DW_TAG_class_type:
8638 case DW_TAG_interface_type:
8639 case DW_TAG_structure_type:
8640 case DW_TAG_union_type:
8641 case DW_TAG_enumeration_type:
8642 /* Skip external references. The DWARF standard says in the section
8643 about "Structure, Union, and Class Type Entries": "An incomplete
8644 structure, union or class type is represented by a structure,
8645 union or class entry that does not have a byte size attribute
8646 and that has a DW_AT_declaration attribute." */
8647 if (!pdi->has_byte_size && pdi->is_declaration)
8648 return;
8649
8650 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8651 static vs. global. */
8652 psymbol.domain = STRUCT_DOMAIN;
8653 psymbol.aclass = LOC_TYPEDEF;
8654 where = (cu->language == language_cplus
8655 ? psymbol_placement::GLOBAL
8656 : psymbol_placement::STATIC);
8657 break;
8658 case DW_TAG_enumerator:
8659 psymbol.domain = VAR_DOMAIN;
8660 psymbol.aclass = LOC_CONST;
8661 where = (cu->language == language_cplus
8662 ? psymbol_placement::GLOBAL
8663 : psymbol_placement::STATIC);
8664 break;
8665 default:
8666 break;
8667 }
8668
8669 if (where.has_value ())
8670 {
8671 if (built_actual_name != nullptr)
8672 actual_name = objfile->intern (actual_name);
8673 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8674 psymbol.ginfo.set_linkage_name (actual_name);
8675 else
8676 {
8677 psymbol.ginfo.set_demangled_name (actual_name,
8678 &objfile->objfile_obstack);
8679 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8680 }
8681 cu->per_cu->v.psymtab->add_psymbol
8682 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8683 objfile);
8684 }
8685 }
8686
8687 /* Read a partial die corresponding to a namespace; also, add a symbol
8688 corresponding to that namespace to the symbol table. NAMESPACE is
8689 the name of the enclosing namespace. */
8690
8691 static void
8692 add_partial_namespace (struct partial_die_info *pdi,
8693 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8694 int set_addrmap, struct dwarf2_cu *cu)
8695 {
8696 /* Add a symbol for the namespace. */
8697
8698 add_partial_symbol (pdi, cu);
8699
8700 /* Now scan partial symbols in that namespace. */
8701
8702 if (pdi->has_children)
8703 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8704 }
8705
8706 /* Read a partial die corresponding to a Fortran module. */
8707
8708 static void
8709 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8710 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8711 {
8712 /* Add a symbol for the namespace. */
8713
8714 add_partial_symbol (pdi, cu);
8715
8716 /* Now scan partial symbols in that module. */
8717
8718 if (pdi->has_children)
8719 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8720 }
8721
8722 /* Read a partial die corresponding to a subprogram or an inlined
8723 subprogram and create a partial symbol for that subprogram.
8724 When the CU language allows it, this routine also defines a partial
8725 symbol for each nested subprogram that this subprogram contains.
8726 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8727 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8728
8729 PDI may also be a lexical block, in which case we simply search
8730 recursively for subprograms defined inside that lexical block.
8731 Again, this is only performed when the CU language allows this
8732 type of definitions. */
8733
8734 static void
8735 add_partial_subprogram (struct partial_die_info *pdi,
8736 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8737 int set_addrmap, struct dwarf2_cu *cu)
8738 {
8739 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8740 {
8741 if (pdi->has_pc_info)
8742 {
8743 if (pdi->lowpc < *lowpc)
8744 *lowpc = pdi->lowpc;
8745 if (pdi->highpc > *highpc)
8746 *highpc = pdi->highpc;
8747 if (set_addrmap)
8748 {
8749 struct objfile *objfile = cu->per_objfile->objfile;
8750 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8751 struct gdbarch *gdbarch = objfile->arch ();
8752 CORE_ADDR baseaddr;
8753 CORE_ADDR this_highpc;
8754 CORE_ADDR this_lowpc;
8755
8756 baseaddr = objfile->text_section_offset ();
8757 this_lowpc
8758 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8759 pdi->lowpc + baseaddr)
8760 - baseaddr);
8761 this_highpc
8762 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8763 pdi->highpc + baseaddr)
8764 - baseaddr);
8765 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8766 this_lowpc, this_highpc - 1,
8767 cu->per_cu->v.psymtab);
8768 }
8769 }
8770
8771 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8772 {
8773 if (!pdi->is_declaration)
8774 /* Ignore subprogram DIEs that do not have a name, they are
8775 illegal. Do not emit a complaint at this point, we will
8776 do so when we convert this psymtab into a symtab. */
8777 if (pdi->name (cu))
8778 add_partial_symbol (pdi, cu);
8779 }
8780 }
8781
8782 if (! pdi->has_children)
8783 return;
8784
8785 if (cu->language == language_ada || cu->language == language_fortran)
8786 {
8787 pdi = pdi->die_child;
8788 while (pdi != NULL)
8789 {
8790 pdi->fixup (cu);
8791 if (pdi->tag == DW_TAG_subprogram
8792 || pdi->tag == DW_TAG_inlined_subroutine
8793 || pdi->tag == DW_TAG_lexical_block)
8794 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8795 pdi = pdi->die_sibling;
8796 }
8797 }
8798 }
8799
8800 /* Read a partial die corresponding to an enumeration type. */
8801
8802 static void
8803 add_partial_enumeration (struct partial_die_info *enum_pdi,
8804 struct dwarf2_cu *cu)
8805 {
8806 struct partial_die_info *pdi;
8807
8808 if (enum_pdi->name (cu) != NULL)
8809 add_partial_symbol (enum_pdi, cu);
8810
8811 pdi = enum_pdi->die_child;
8812 while (pdi)
8813 {
8814 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8815 complaint (_("malformed enumerator DIE ignored"));
8816 else
8817 add_partial_symbol (pdi, cu);
8818 pdi = pdi->die_sibling;
8819 }
8820 }
8821
8822 /* Return the initial uleb128 in the die at INFO_PTR. */
8823
8824 static unsigned int
8825 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8826 {
8827 unsigned int bytes_read;
8828
8829 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8830 }
8831
8832 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8833 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8834
8835 Return the corresponding abbrev, or NULL if the number is zero (indicating
8836 an empty DIE). In either case *BYTES_READ will be set to the length of
8837 the initial number. */
8838
8839 static const struct abbrev_info *
8840 peek_die_abbrev (const die_reader_specs &reader,
8841 const gdb_byte *info_ptr, unsigned int *bytes_read)
8842 {
8843 dwarf2_cu *cu = reader.cu;
8844 bfd *abfd = reader.abfd;
8845 unsigned int abbrev_number
8846 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8847
8848 if (abbrev_number == 0)
8849 return NULL;
8850
8851 const abbrev_info *abbrev
8852 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8853 if (!abbrev)
8854 {
8855 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8856 " at offset %s [in module %s]"),
8857 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8858 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8859 }
8860
8861 return abbrev;
8862 }
8863
8864 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8865 Returns a pointer to the end of a series of DIEs, terminated by an empty
8866 DIE. Any children of the skipped DIEs will also be skipped. */
8867
8868 static const gdb_byte *
8869 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8870 {
8871 while (1)
8872 {
8873 unsigned int bytes_read;
8874 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8875 &bytes_read);
8876
8877 if (abbrev == NULL)
8878 return info_ptr + bytes_read;
8879 else
8880 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8881 }
8882 }
8883
8884 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8885 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8886 abbrev corresponding to that skipped uleb128 should be passed in
8887 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8888 children. */
8889
8890 static const gdb_byte *
8891 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8892 const struct abbrev_info *abbrev)
8893 {
8894 unsigned int bytes_read;
8895 struct attribute attr;
8896 bfd *abfd = reader->abfd;
8897 struct dwarf2_cu *cu = reader->cu;
8898 const gdb_byte *buffer = reader->buffer;
8899 const gdb_byte *buffer_end = reader->buffer_end;
8900 unsigned int form, i;
8901
8902 for (i = 0; i < abbrev->num_attrs; i++)
8903 {
8904 /* The only abbrev we care about is DW_AT_sibling. */
8905 if (abbrev->attrs[i].name == DW_AT_sibling)
8906 {
8907 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8908 if (attr.form == DW_FORM_ref_addr)
8909 complaint (_("ignoring absolute DW_AT_sibling"));
8910 else
8911 {
8912 sect_offset off = attr.get_ref_die_offset ();
8913 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8914
8915 if (sibling_ptr < info_ptr)
8916 complaint (_("DW_AT_sibling points backwards"));
8917 else if (sibling_ptr > reader->buffer_end)
8918 reader->die_section->overflow_complaint ();
8919 else
8920 return sibling_ptr;
8921 }
8922 }
8923
8924 /* If it isn't DW_AT_sibling, skip this attribute. */
8925 form = abbrev->attrs[i].form;
8926 skip_attribute:
8927 switch (form)
8928 {
8929 case DW_FORM_ref_addr:
8930 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8931 and later it is offset sized. */
8932 if (cu->header.version == 2)
8933 info_ptr += cu->header.addr_size;
8934 else
8935 info_ptr += cu->header.offset_size;
8936 break;
8937 case DW_FORM_GNU_ref_alt:
8938 info_ptr += cu->header.offset_size;
8939 break;
8940 case DW_FORM_addr:
8941 info_ptr += cu->header.addr_size;
8942 break;
8943 case DW_FORM_data1:
8944 case DW_FORM_ref1:
8945 case DW_FORM_flag:
8946 case DW_FORM_strx1:
8947 info_ptr += 1;
8948 break;
8949 case DW_FORM_flag_present:
8950 case DW_FORM_implicit_const:
8951 break;
8952 case DW_FORM_data2:
8953 case DW_FORM_ref2:
8954 case DW_FORM_strx2:
8955 info_ptr += 2;
8956 break;
8957 case DW_FORM_strx3:
8958 info_ptr += 3;
8959 break;
8960 case DW_FORM_data4:
8961 case DW_FORM_ref4:
8962 case DW_FORM_strx4:
8963 info_ptr += 4;
8964 break;
8965 case DW_FORM_data8:
8966 case DW_FORM_ref8:
8967 case DW_FORM_ref_sig8:
8968 info_ptr += 8;
8969 break;
8970 case DW_FORM_data16:
8971 info_ptr += 16;
8972 break;
8973 case DW_FORM_string:
8974 read_direct_string (abfd, info_ptr, &bytes_read);
8975 info_ptr += bytes_read;
8976 break;
8977 case DW_FORM_sec_offset:
8978 case DW_FORM_strp:
8979 case DW_FORM_GNU_strp_alt:
8980 info_ptr += cu->header.offset_size;
8981 break;
8982 case DW_FORM_exprloc:
8983 case DW_FORM_block:
8984 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8985 info_ptr += bytes_read;
8986 break;
8987 case DW_FORM_block1:
8988 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8989 break;
8990 case DW_FORM_block2:
8991 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8992 break;
8993 case DW_FORM_block4:
8994 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8995 break;
8996 case DW_FORM_addrx:
8997 case DW_FORM_strx:
8998 case DW_FORM_sdata:
8999 case DW_FORM_udata:
9000 case DW_FORM_ref_udata:
9001 case DW_FORM_GNU_addr_index:
9002 case DW_FORM_GNU_str_index:
9003 case DW_FORM_rnglistx:
9004 case DW_FORM_loclistx:
9005 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9006 break;
9007 case DW_FORM_indirect:
9008 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9009 info_ptr += bytes_read;
9010 /* We need to continue parsing from here, so just go back to
9011 the top. */
9012 goto skip_attribute;
9013
9014 default:
9015 error (_("Dwarf Error: Cannot handle %s "
9016 "in DWARF reader [in module %s]"),
9017 dwarf_form_name (form),
9018 bfd_get_filename (abfd));
9019 }
9020 }
9021
9022 if (abbrev->has_children)
9023 return skip_children (reader, info_ptr);
9024 else
9025 return info_ptr;
9026 }
9027
9028 /* Locate ORIG_PDI's sibling.
9029 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9030
9031 static const gdb_byte *
9032 locate_pdi_sibling (const struct die_reader_specs *reader,
9033 struct partial_die_info *orig_pdi,
9034 const gdb_byte *info_ptr)
9035 {
9036 /* Do we know the sibling already? */
9037
9038 if (orig_pdi->sibling)
9039 return orig_pdi->sibling;
9040
9041 /* Are there any children to deal with? */
9042
9043 if (!orig_pdi->has_children)
9044 return info_ptr;
9045
9046 /* Skip the children the long way. */
9047
9048 return skip_children (reader, info_ptr);
9049 }
9050
9051 /* Expand this partial symbol table into a full symbol table. SELF is
9052 not NULL. */
9053
9054 void
9055 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9056 {
9057 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9058
9059 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9060
9061 /* If this psymtab is constructed from a debug-only objfile, the
9062 has_section_at_zero flag will not necessarily be correct. We
9063 can get the correct value for this flag by looking at the data
9064 associated with the (presumably stripped) associated objfile. */
9065 if (objfile->separate_debug_objfile_backlink)
9066 {
9067 dwarf2_per_objfile *per_objfile_backlink
9068 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9069
9070 per_objfile->per_bfd->has_section_at_zero
9071 = per_objfile_backlink->per_bfd->has_section_at_zero;
9072 }
9073
9074 expand_psymtab (objfile);
9075
9076 process_cu_includes (per_objfile);
9077 }
9078 \f
9079 /* Reading in full CUs. */
9080
9081 /* Add PER_CU to the queue. */
9082
9083 static void
9084 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9085 dwarf2_per_objfile *per_objfile,
9086 enum language pretend_language)
9087 {
9088 per_cu->queued = 1;
9089
9090 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9091 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
9092 }
9093
9094 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9095
9096 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9097 dependency.
9098
9099 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9100 DIEs, false otherwise.
9101
9102 Explanation: there is an invariant that if a CU is queued for expansion
9103 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9104 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9105 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9106 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9107 invariant is respected.
9108
9109 The caller is therefore not required to load the CU's DIEs (we return false)
9110 if:
9111
9112 - the CU is already expanded, and therefore does not get enqueued
9113 - the CU gets enqueued for expansion, but its DIEs are already loaded
9114
9115 Note that the caller should not use this function's return value as an
9116 indicator of whether the CU's DIEs are loaded right now, it should check
9117 that by calling `dwarf2_per_objfile::get_cu` instead. */
9118
9119 static int
9120 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9121 dwarf2_per_cu_data *per_cu,
9122 dwarf2_per_objfile *per_objfile,
9123 enum language pretend_language)
9124 {
9125 /* We may arrive here during partial symbol reading, if we need full
9126 DIEs to process an unusual case (e.g. template arguments). Do
9127 not queue PER_CU, just tell our caller to load its DIEs. */
9128 if (per_cu->per_bfd->reading_partial_symbols)
9129 {
9130 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9131
9132 if (cu == NULL || cu->dies == NULL)
9133 return 1;
9134 return 0;
9135 }
9136
9137 /* Mark the dependence relation so that we don't flush PER_CU
9138 too early. */
9139 if (dependent_cu != NULL)
9140 dwarf2_add_dependence (dependent_cu, per_cu);
9141
9142 /* If it's already on the queue, we have nothing to do. */
9143 if (per_cu->queued)
9144 {
9145 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9146 loaded. */
9147 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9148
9149 /* If the CU is queued for expansion, it should not already be
9150 expanded. */
9151 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9152
9153 /* The DIEs are already loaded, the caller doesn't need to do it. */
9154 return 0;
9155 }
9156
9157 bool queued = false;
9158 if (!per_objfile->symtab_set_p (per_cu))
9159 {
9160 /* Add it to the queue. */
9161 queue_comp_unit (per_cu, per_objfile, pretend_language);
9162 queued = true;
9163 }
9164
9165 /* If the compilation unit is already loaded, just mark it as
9166 used. */
9167 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9168 if (cu != nullptr)
9169 cu->last_used = 0;
9170
9171 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9172 and the DIEs are not already loaded. */
9173 return queued && cu == nullptr;
9174 }
9175
9176 /* Process the queue. */
9177
9178 static void
9179 process_queue (dwarf2_per_objfile *per_objfile)
9180 {
9181 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9182 objfile_name (per_objfile->objfile));
9183
9184 /* The queue starts out with one item, but following a DIE reference
9185 may load a new CU, adding it to the end of the queue. */
9186 while (!per_objfile->per_bfd->queue->empty ())
9187 {
9188 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
9189 dwarf2_per_cu_data *per_cu = item.per_cu;
9190
9191 if (!per_objfile->symtab_set_p (per_cu))
9192 {
9193 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9194
9195 /* Skip dummy CUs. */
9196 if (cu != nullptr)
9197 {
9198 unsigned int debug_print_threshold;
9199 char buf[100];
9200
9201 if (per_cu->is_debug_types)
9202 {
9203 struct signatured_type *sig_type =
9204 (struct signatured_type *) per_cu;
9205
9206 sprintf (buf, "TU %s at offset %s",
9207 hex_string (sig_type->signature),
9208 sect_offset_str (per_cu->sect_off));
9209 /* There can be 100s of TUs.
9210 Only print them in verbose mode. */
9211 debug_print_threshold = 2;
9212 }
9213 else
9214 {
9215 sprintf (buf, "CU at offset %s",
9216 sect_offset_str (per_cu->sect_off));
9217 debug_print_threshold = 1;
9218 }
9219
9220 if (dwarf_read_debug >= debug_print_threshold)
9221 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9222
9223 if (per_cu->is_debug_types)
9224 process_full_type_unit (cu, item.pretend_language);
9225 else
9226 process_full_comp_unit (cu, item.pretend_language);
9227
9228 if (dwarf_read_debug >= debug_print_threshold)
9229 dwarf_read_debug_printf ("Done expanding %s", buf);
9230 }
9231 }
9232
9233 per_cu->queued = 0;
9234 per_objfile->per_bfd->queue->pop ();
9235 }
9236
9237 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9238 objfile_name (per_objfile->objfile));
9239 }
9240
9241 /* Read in full symbols for PST, and anything it depends on. */
9242
9243 void
9244 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9245 {
9246 gdb_assert (!readin_p (objfile));
9247
9248 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9249 free_cached_comp_units freer (per_objfile);
9250 expand_dependencies (objfile);
9251
9252 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9253 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9254 }
9255
9256 /* See psympriv.h. */
9257
9258 bool
9259 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9260 {
9261 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9262 return per_objfile->symtab_set_p (per_cu_data);
9263 }
9264
9265 /* See psympriv.h. */
9266
9267 compunit_symtab *
9268 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9269 {
9270 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9271 return per_objfile->get_symtab (per_cu_data);
9272 }
9273
9274 /* Trivial hash function for die_info: the hash value of a DIE
9275 is its offset in .debug_info for this objfile. */
9276
9277 static hashval_t
9278 die_hash (const void *item)
9279 {
9280 const struct die_info *die = (const struct die_info *) item;
9281
9282 return to_underlying (die->sect_off);
9283 }
9284
9285 /* Trivial comparison function for die_info structures: two DIEs
9286 are equal if they have the same offset. */
9287
9288 static int
9289 die_eq (const void *item_lhs, const void *item_rhs)
9290 {
9291 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9292 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9293
9294 return die_lhs->sect_off == die_rhs->sect_off;
9295 }
9296
9297 /* Load the DIEs associated with PER_CU into memory.
9298
9299 In some cases, the caller, while reading partial symbols, will need to load
9300 the full symbols for the CU for some reason. It will already have a
9301 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9302 rather than creating a new one. */
9303
9304 static void
9305 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9306 dwarf2_per_objfile *per_objfile,
9307 dwarf2_cu *existing_cu,
9308 bool skip_partial,
9309 enum language pretend_language)
9310 {
9311 gdb_assert (! this_cu->is_debug_types);
9312
9313 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9314 if (reader.dummy_p)
9315 return;
9316
9317 struct dwarf2_cu *cu = reader.cu;
9318 const gdb_byte *info_ptr = reader.info_ptr;
9319
9320 gdb_assert (cu->die_hash == NULL);
9321 cu->die_hash =
9322 htab_create_alloc_ex (cu->header.length / 12,
9323 die_hash,
9324 die_eq,
9325 NULL,
9326 &cu->comp_unit_obstack,
9327 hashtab_obstack_allocate,
9328 dummy_obstack_deallocate);
9329
9330 if (reader.comp_unit_die->has_children)
9331 reader.comp_unit_die->child
9332 = read_die_and_siblings (&reader, reader.info_ptr,
9333 &info_ptr, reader.comp_unit_die);
9334 cu->dies = reader.comp_unit_die;
9335 /* comp_unit_die is not stored in die_hash, no need. */
9336
9337 /* We try not to read any attributes in this function, because not
9338 all CUs needed for references have been loaded yet, and symbol
9339 table processing isn't initialized. But we have to set the CU language,
9340 or we won't be able to build types correctly.
9341 Similarly, if we do not read the producer, we can not apply
9342 producer-specific interpretation. */
9343 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9344
9345 reader.keep ();
9346 }
9347
9348 /* Add a DIE to the delayed physname list. */
9349
9350 static void
9351 add_to_method_list (struct type *type, int fnfield_index, int index,
9352 const char *name, struct die_info *die,
9353 struct dwarf2_cu *cu)
9354 {
9355 struct delayed_method_info mi;
9356 mi.type = type;
9357 mi.fnfield_index = fnfield_index;
9358 mi.index = index;
9359 mi.name = name;
9360 mi.die = die;
9361 cu->method_list.push_back (mi);
9362 }
9363
9364 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9365 "const" / "volatile". If so, decrements LEN by the length of the
9366 modifier and return true. Otherwise return false. */
9367
9368 template<size_t N>
9369 static bool
9370 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9371 {
9372 size_t mod_len = sizeof (mod) - 1;
9373 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9374 {
9375 len -= mod_len;
9376 return true;
9377 }
9378 return false;
9379 }
9380
9381 /* Compute the physnames of any methods on the CU's method list.
9382
9383 The computation of method physnames is delayed in order to avoid the
9384 (bad) condition that one of the method's formal parameters is of an as yet
9385 incomplete type. */
9386
9387 static void
9388 compute_delayed_physnames (struct dwarf2_cu *cu)
9389 {
9390 /* Only C++ delays computing physnames. */
9391 if (cu->method_list.empty ())
9392 return;
9393 gdb_assert (cu->language == language_cplus);
9394
9395 for (const delayed_method_info &mi : cu->method_list)
9396 {
9397 const char *physname;
9398 struct fn_fieldlist *fn_flp
9399 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9400 physname = dwarf2_physname (mi.name, mi.die, cu);
9401 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9402 = physname ? physname : "";
9403
9404 /* Since there's no tag to indicate whether a method is a
9405 const/volatile overload, extract that information out of the
9406 demangled name. */
9407 if (physname != NULL)
9408 {
9409 size_t len = strlen (physname);
9410
9411 while (1)
9412 {
9413 if (physname[len] == ')') /* shortcut */
9414 break;
9415 else if (check_modifier (physname, len, " const"))
9416 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9417 else if (check_modifier (physname, len, " volatile"))
9418 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9419 else
9420 break;
9421 }
9422 }
9423 }
9424
9425 /* The list is no longer needed. */
9426 cu->method_list.clear ();
9427 }
9428
9429 /* Go objects should be embedded in a DW_TAG_module DIE,
9430 and it's not clear if/how imported objects will appear.
9431 To keep Go support simple until that's worked out,
9432 go back through what we've read and create something usable.
9433 We could do this while processing each DIE, and feels kinda cleaner,
9434 but that way is more invasive.
9435 This is to, for example, allow the user to type "p var" or "b main"
9436 without having to specify the package name, and allow lookups
9437 of module.object to work in contexts that use the expression
9438 parser. */
9439
9440 static void
9441 fixup_go_packaging (struct dwarf2_cu *cu)
9442 {
9443 gdb::unique_xmalloc_ptr<char> package_name;
9444 struct pending *list;
9445 int i;
9446
9447 for (list = *cu->get_builder ()->get_global_symbols ();
9448 list != NULL;
9449 list = list->next)
9450 {
9451 for (i = 0; i < list->nsyms; ++i)
9452 {
9453 struct symbol *sym = list->symbol[i];
9454
9455 if (sym->language () == language_go
9456 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9457 {
9458 gdb::unique_xmalloc_ptr<char> this_package_name
9459 (go_symbol_package_name (sym));
9460
9461 if (this_package_name == NULL)
9462 continue;
9463 if (package_name == NULL)
9464 package_name = std::move (this_package_name);
9465 else
9466 {
9467 struct objfile *objfile = cu->per_objfile->objfile;
9468 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9469 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9470 (symbol_symtab (sym) != NULL
9471 ? symtab_to_filename_for_display
9472 (symbol_symtab (sym))
9473 : objfile_name (objfile)),
9474 this_package_name.get (), package_name.get ());
9475 }
9476 }
9477 }
9478 }
9479
9480 if (package_name != NULL)
9481 {
9482 struct objfile *objfile = cu->per_objfile->objfile;
9483 const char *saved_package_name = objfile->intern (package_name.get ());
9484 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9485 saved_package_name);
9486 struct symbol *sym;
9487
9488 sym = new (&objfile->objfile_obstack) symbol;
9489 sym->set_language (language_go, &objfile->objfile_obstack);
9490 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9491 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9492 e.g., "main" finds the "main" module and not C's main(). */
9493 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9494 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9495 SYMBOL_TYPE (sym) = type;
9496
9497 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9498 }
9499 }
9500
9501 /* Allocate a fully-qualified name consisting of the two parts on the
9502 obstack. */
9503
9504 static const char *
9505 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9506 {
9507 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9508 }
9509
9510 /* A helper that allocates a variant part to attach to a Rust enum
9511 type. OBSTACK is where the results should be allocated. TYPE is
9512 the type we're processing. DISCRIMINANT_INDEX is the index of the
9513 discriminant. It must be the index of one of the fields of TYPE,
9514 or -1 to mean there is no discriminant (univariant enum).
9515 DEFAULT_INDEX is the index of the default field; or -1 if there is
9516 no default. RANGES is indexed by "effective" field number (the
9517 field index, but omitting the discriminant and default fields) and
9518 must hold the discriminant values used by the variants. Note that
9519 RANGES must have a lifetime at least as long as OBSTACK -- either
9520 already allocated on it, or static. */
9521
9522 static void
9523 alloc_rust_variant (struct obstack *obstack, struct type *type,
9524 int discriminant_index, int default_index,
9525 gdb::array_view<discriminant_range> ranges)
9526 {
9527 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9528 gdb_assert (discriminant_index == -1
9529 || (discriminant_index >= 0
9530 && discriminant_index < type->num_fields ()));
9531 gdb_assert (default_index == -1
9532 || (default_index >= 0 && default_index < type->num_fields ()));
9533
9534 /* We have one variant for each non-discriminant field. */
9535 int n_variants = type->num_fields ();
9536 if (discriminant_index != -1)
9537 --n_variants;
9538
9539 variant *variants = new (obstack) variant[n_variants];
9540 int var_idx = 0;
9541 int range_idx = 0;
9542 for (int i = 0; i < type->num_fields (); ++i)
9543 {
9544 if (i == discriminant_index)
9545 continue;
9546
9547 variants[var_idx].first_field = i;
9548 variants[var_idx].last_field = i + 1;
9549
9550 /* The default field does not need a range, but other fields do.
9551 We skipped the discriminant above. */
9552 if (i != default_index)
9553 {
9554 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9555 ++range_idx;
9556 }
9557
9558 ++var_idx;
9559 }
9560
9561 gdb_assert (range_idx == ranges.size ());
9562 gdb_assert (var_idx == n_variants);
9563
9564 variant_part *part = new (obstack) variant_part;
9565 part->discriminant_index = discriminant_index;
9566 /* If there is no discriminant, then whether it is signed is of no
9567 consequence. */
9568 part->is_unsigned
9569 = (discriminant_index == -1
9570 ? false
9571 : type->field (discriminant_index).type ()->is_unsigned ());
9572 part->variants = gdb::array_view<variant> (variants, n_variants);
9573
9574 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9575 gdb::array_view<variant_part> *prop_value
9576 = new (storage) gdb::array_view<variant_part> (part, 1);
9577
9578 struct dynamic_prop prop;
9579 prop.set_variant_parts (prop_value);
9580
9581 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9582 }
9583
9584 /* Some versions of rustc emitted enums in an unusual way.
9585
9586 Ordinary enums were emitted as unions. The first element of each
9587 structure in the union was named "RUST$ENUM$DISR". This element
9588 held the discriminant.
9589
9590 These versions of Rust also implemented the "non-zero"
9591 optimization. When the enum had two values, and one is empty and
9592 the other holds a pointer that cannot be zero, the pointer is used
9593 as the discriminant, with a zero value meaning the empty variant.
9594 Here, the union's first member is of the form
9595 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9596 where the fieldnos are the indices of the fields that should be
9597 traversed in order to find the field (which may be several fields deep)
9598 and the variantname is the name of the variant of the case when the
9599 field is zero.
9600
9601 This function recognizes whether TYPE is of one of these forms,
9602 and, if so, smashes it to be a variant type. */
9603
9604 static void
9605 quirk_rust_enum (struct type *type, struct objfile *objfile)
9606 {
9607 gdb_assert (type->code () == TYPE_CODE_UNION);
9608
9609 /* We don't need to deal with empty enums. */
9610 if (type->num_fields () == 0)
9611 return;
9612
9613 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9614 if (type->num_fields () == 1
9615 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9616 {
9617 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9618
9619 /* Decode the field name to find the offset of the
9620 discriminant. */
9621 ULONGEST bit_offset = 0;
9622 struct type *field_type = type->field (0).type ();
9623 while (name[0] >= '0' && name[0] <= '9')
9624 {
9625 char *tail;
9626 unsigned long index = strtoul (name, &tail, 10);
9627 name = tail;
9628 if (*name != '$'
9629 || index >= field_type->num_fields ()
9630 || (TYPE_FIELD_LOC_KIND (field_type, index)
9631 != FIELD_LOC_KIND_BITPOS))
9632 {
9633 complaint (_("Could not parse Rust enum encoding string \"%s\""
9634 "[in module %s]"),
9635 TYPE_FIELD_NAME (type, 0),
9636 objfile_name (objfile));
9637 return;
9638 }
9639 ++name;
9640
9641 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9642 field_type = field_type->field (index).type ();
9643 }
9644
9645 /* Smash this type to be a structure type. We have to do this
9646 because the type has already been recorded. */
9647 type->set_code (TYPE_CODE_STRUCT);
9648 type->set_num_fields (3);
9649 /* Save the field we care about. */
9650 struct field saved_field = type->field (0);
9651 type->set_fields
9652 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9653
9654 /* Put the discriminant at index 0. */
9655 type->field (0).set_type (field_type);
9656 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9657 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9658 SET_FIELD_BITPOS (type->field (0), bit_offset);
9659
9660 /* The order of fields doesn't really matter, so put the real
9661 field at index 1 and the data-less field at index 2. */
9662 type->field (1) = saved_field;
9663 TYPE_FIELD_NAME (type, 1)
9664 = rust_last_path_segment (type->field (1).type ()->name ());
9665 type->field (1).type ()->set_name
9666 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9667 TYPE_FIELD_NAME (type, 1)));
9668
9669 const char *dataless_name
9670 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9671 name);
9672 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9673 dataless_name);
9674 type->field (2).set_type (dataless_type);
9675 /* NAME points into the original discriminant name, which
9676 already has the correct lifetime. */
9677 TYPE_FIELD_NAME (type, 2) = name;
9678 SET_FIELD_BITPOS (type->field (2), 0);
9679
9680 /* Indicate that this is a variant type. */
9681 static discriminant_range ranges[1] = { { 0, 0 } };
9682 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9683 }
9684 /* A union with a single anonymous field is probably an old-style
9685 univariant enum. */
9686 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9687 {
9688 /* Smash this type to be a structure type. We have to do this
9689 because the type has already been recorded. */
9690 type->set_code (TYPE_CODE_STRUCT);
9691
9692 struct type *field_type = type->field (0).type ();
9693 const char *variant_name
9694 = rust_last_path_segment (field_type->name ());
9695 TYPE_FIELD_NAME (type, 0) = variant_name;
9696 field_type->set_name
9697 (rust_fully_qualify (&objfile->objfile_obstack,
9698 type->name (), variant_name));
9699
9700 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9701 }
9702 else
9703 {
9704 struct type *disr_type = nullptr;
9705 for (int i = 0; i < type->num_fields (); ++i)
9706 {
9707 disr_type = type->field (i).type ();
9708
9709 if (disr_type->code () != TYPE_CODE_STRUCT)
9710 {
9711 /* All fields of a true enum will be structs. */
9712 return;
9713 }
9714 else if (disr_type->num_fields () == 0)
9715 {
9716 /* Could be data-less variant, so keep going. */
9717 disr_type = nullptr;
9718 }
9719 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9720 "RUST$ENUM$DISR") != 0)
9721 {
9722 /* Not a Rust enum. */
9723 return;
9724 }
9725 else
9726 {
9727 /* Found one. */
9728 break;
9729 }
9730 }
9731
9732 /* If we got here without a discriminant, then it's probably
9733 just a union. */
9734 if (disr_type == nullptr)
9735 return;
9736
9737 /* Smash this type to be a structure type. We have to do this
9738 because the type has already been recorded. */
9739 type->set_code (TYPE_CODE_STRUCT);
9740
9741 /* Make space for the discriminant field. */
9742 struct field *disr_field = &disr_type->field (0);
9743 field *new_fields
9744 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9745 * sizeof (struct field)));
9746 memcpy (new_fields + 1, type->fields (),
9747 type->num_fields () * sizeof (struct field));
9748 type->set_fields (new_fields);
9749 type->set_num_fields (type->num_fields () + 1);
9750
9751 /* Install the discriminant at index 0 in the union. */
9752 type->field (0) = *disr_field;
9753 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9754 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9755
9756 /* We need a way to find the correct discriminant given a
9757 variant name. For convenience we build a map here. */
9758 struct type *enum_type = disr_field->type ();
9759 std::unordered_map<std::string, ULONGEST> discriminant_map;
9760 for (int i = 0; i < enum_type->num_fields (); ++i)
9761 {
9762 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9763 {
9764 const char *name
9765 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9766 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9767 }
9768 }
9769
9770 int n_fields = type->num_fields ();
9771 /* We don't need a range entry for the discriminant, but we do
9772 need one for every other field, as there is no default
9773 variant. */
9774 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9775 discriminant_range,
9776 n_fields - 1);
9777 /* Skip the discriminant here. */
9778 for (int i = 1; i < n_fields; ++i)
9779 {
9780 /* Find the final word in the name of this variant's type.
9781 That name can be used to look up the correct
9782 discriminant. */
9783 const char *variant_name
9784 = rust_last_path_segment (type->field (i).type ()->name ());
9785
9786 auto iter = discriminant_map.find (variant_name);
9787 if (iter != discriminant_map.end ())
9788 {
9789 ranges[i - 1].low = iter->second;
9790 ranges[i - 1].high = iter->second;
9791 }
9792
9793 /* In Rust, each element should have the size of the
9794 enclosing enum. */
9795 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9796
9797 /* Remove the discriminant field, if it exists. */
9798 struct type *sub_type = type->field (i).type ();
9799 if (sub_type->num_fields () > 0)
9800 {
9801 sub_type->set_num_fields (sub_type->num_fields () - 1);
9802 sub_type->set_fields (sub_type->fields () + 1);
9803 }
9804 TYPE_FIELD_NAME (type, i) = variant_name;
9805 sub_type->set_name
9806 (rust_fully_qualify (&objfile->objfile_obstack,
9807 type->name (), variant_name));
9808 }
9809
9810 /* Indicate that this is a variant type. */
9811 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9812 gdb::array_view<discriminant_range> (ranges,
9813 n_fields - 1));
9814 }
9815 }
9816
9817 /* Rewrite some Rust unions to be structures with variants parts. */
9818
9819 static void
9820 rust_union_quirks (struct dwarf2_cu *cu)
9821 {
9822 gdb_assert (cu->language == language_rust);
9823 for (type *type_ : cu->rust_unions)
9824 quirk_rust_enum (type_, cu->per_objfile->objfile);
9825 /* We don't need this any more. */
9826 cu->rust_unions.clear ();
9827 }
9828
9829 /* See read.h. */
9830
9831 type_unit_group_unshareable *
9832 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9833 {
9834 auto iter = this->m_type_units.find (tu_group);
9835 if (iter != this->m_type_units.end ())
9836 return iter->second.get ();
9837
9838 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9839 type_unit_group_unshareable *result = uniq.get ();
9840 this->m_type_units[tu_group] = std::move (uniq);
9841 return result;
9842 }
9843
9844 struct type *
9845 dwarf2_per_objfile::get_type_for_signatured_type
9846 (signatured_type *sig_type) const
9847 {
9848 auto iter = this->m_type_map.find (sig_type);
9849 if (iter == this->m_type_map.end ())
9850 return nullptr;
9851
9852 return iter->second;
9853 }
9854
9855 void dwarf2_per_objfile::set_type_for_signatured_type
9856 (signatured_type *sig_type, struct type *type)
9857 {
9858 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9859
9860 this->m_type_map[sig_type] = type;
9861 }
9862
9863 /* A helper function for computing the list of all symbol tables
9864 included by PER_CU. */
9865
9866 static void
9867 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9868 htab_t all_children, htab_t all_type_symtabs,
9869 dwarf2_per_cu_data *per_cu,
9870 dwarf2_per_objfile *per_objfile,
9871 struct compunit_symtab *immediate_parent)
9872 {
9873 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9874 if (*slot != NULL)
9875 {
9876 /* This inclusion and its children have been processed. */
9877 return;
9878 }
9879
9880 *slot = per_cu;
9881
9882 /* Only add a CU if it has a symbol table. */
9883 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9884 if (cust != NULL)
9885 {
9886 /* If this is a type unit only add its symbol table if we haven't
9887 seen it yet (type unit per_cu's can share symtabs). */
9888 if (per_cu->is_debug_types)
9889 {
9890 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9891 if (*slot == NULL)
9892 {
9893 *slot = cust;
9894 result->push_back (cust);
9895 if (cust->user == NULL)
9896 cust->user = immediate_parent;
9897 }
9898 }
9899 else
9900 {
9901 result->push_back (cust);
9902 if (cust->user == NULL)
9903 cust->user = immediate_parent;
9904 }
9905 }
9906
9907 if (!per_cu->imported_symtabs_empty ())
9908 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9909 {
9910 recursively_compute_inclusions (result, all_children,
9911 all_type_symtabs, ptr, per_objfile,
9912 cust);
9913 }
9914 }
9915
9916 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9917 PER_CU. */
9918
9919 static void
9920 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9921 dwarf2_per_objfile *per_objfile)
9922 {
9923 gdb_assert (! per_cu->is_debug_types);
9924
9925 if (!per_cu->imported_symtabs_empty ())
9926 {
9927 int len;
9928 std::vector<compunit_symtab *> result_symtabs;
9929 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9930
9931 /* If we don't have a symtab, we can just skip this case. */
9932 if (cust == NULL)
9933 return;
9934
9935 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9936 htab_eq_pointer,
9937 NULL, xcalloc, xfree));
9938 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9939 htab_eq_pointer,
9940 NULL, xcalloc, xfree));
9941
9942 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9943 {
9944 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9945 all_type_symtabs.get (), ptr,
9946 per_objfile, cust);
9947 }
9948
9949 /* Now we have a transitive closure of all the included symtabs. */
9950 len = result_symtabs.size ();
9951 cust->includes
9952 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9953 struct compunit_symtab *, len + 1);
9954 memcpy (cust->includes, result_symtabs.data (),
9955 len * sizeof (compunit_symtab *));
9956 cust->includes[len] = NULL;
9957 }
9958 }
9959
9960 /* Compute the 'includes' field for the symtabs of all the CUs we just
9961 read. */
9962
9963 static void
9964 process_cu_includes (dwarf2_per_objfile *per_objfile)
9965 {
9966 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9967 {
9968 if (! iter->is_debug_types)
9969 compute_compunit_symtab_includes (iter, per_objfile);
9970 }
9971
9972 per_objfile->per_bfd->just_read_cus.clear ();
9973 }
9974
9975 /* Generate full symbol information for CU, whose DIEs have
9976 already been loaded into memory. */
9977
9978 static void
9979 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9980 {
9981 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9982 struct objfile *objfile = per_objfile->objfile;
9983 struct gdbarch *gdbarch = objfile->arch ();
9984 CORE_ADDR lowpc, highpc;
9985 struct compunit_symtab *cust;
9986 CORE_ADDR baseaddr;
9987 struct block *static_block;
9988 CORE_ADDR addr;
9989
9990 baseaddr = objfile->text_section_offset ();
9991
9992 /* Clear the list here in case something was left over. */
9993 cu->method_list.clear ();
9994
9995 cu->language = pretend_language;
9996 cu->language_defn = language_def (cu->language);
9997
9998 dwarf2_find_base_address (cu->dies, cu);
9999
10000 /* Before we start reading the top-level DIE, ensure it has a valid tag
10001 type. */
10002 switch (cu->dies->tag)
10003 {
10004 case DW_TAG_compile_unit:
10005 case DW_TAG_partial_unit:
10006 case DW_TAG_type_unit:
10007 break;
10008 default:
10009 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
10010 dwarf_tag_name (cu->dies->tag),
10011 sect_offset_str (cu->per_cu->sect_off),
10012 objfile_name (per_objfile->objfile));
10013 }
10014
10015 /* Do line number decoding in read_file_scope () */
10016 process_die (cu->dies, cu);
10017
10018 /* For now fudge the Go package. */
10019 if (cu->language == language_go)
10020 fixup_go_packaging (cu);
10021
10022 /* Now that we have processed all the DIEs in the CU, all the types
10023 should be complete, and it should now be safe to compute all of the
10024 physnames. */
10025 compute_delayed_physnames (cu);
10026
10027 if (cu->language == language_rust)
10028 rust_union_quirks (cu);
10029
10030 /* Some compilers don't define a DW_AT_high_pc attribute for the
10031 compilation unit. If the DW_AT_high_pc is missing, synthesize
10032 it, by scanning the DIE's below the compilation unit. */
10033 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10034
10035 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10036 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10037
10038 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10039 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10040 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10041 addrmap to help ensure it has an accurate map of pc values belonging to
10042 this comp unit. */
10043 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10044
10045 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10046 SECT_OFF_TEXT (objfile),
10047 0);
10048
10049 if (cust != NULL)
10050 {
10051 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10052
10053 /* Set symtab language to language from DW_AT_language. If the
10054 compilation is from a C file generated by language preprocessors, do
10055 not set the language if it was already deduced by start_subfile. */
10056 if (!(cu->language == language_c
10057 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10058 COMPUNIT_FILETABS (cust)->language = cu->language;
10059
10060 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10061 produce DW_AT_location with location lists but it can be possibly
10062 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10063 there were bugs in prologue debug info, fixed later in GCC-4.5
10064 by "unwind info for epilogues" patch (which is not directly related).
10065
10066 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10067 needed, it would be wrong due to missing DW_AT_producer there.
10068
10069 Still one can confuse GDB by using non-standard GCC compilation
10070 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10071 */
10072 if (cu->has_loclist && gcc_4_minor >= 5)
10073 cust->locations_valid = 1;
10074
10075 if (gcc_4_minor >= 5)
10076 cust->epilogue_unwind_valid = 1;
10077
10078 cust->call_site_htab = cu->call_site_htab;
10079 }
10080
10081 per_objfile->set_symtab (cu->per_cu, cust);
10082
10083 /* Push it for inclusion processing later. */
10084 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10085
10086 /* Not needed any more. */
10087 cu->reset_builder ();
10088 }
10089
10090 /* Generate full symbol information for type unit CU, whose DIEs have
10091 already been loaded into memory. */
10092
10093 static void
10094 process_full_type_unit (dwarf2_cu *cu,
10095 enum language pretend_language)
10096 {
10097 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10098 struct objfile *objfile = per_objfile->objfile;
10099 struct compunit_symtab *cust;
10100 struct signatured_type *sig_type;
10101
10102 gdb_assert (cu->per_cu->is_debug_types);
10103 sig_type = (struct signatured_type *) cu->per_cu;
10104
10105 /* Clear the list here in case something was left over. */
10106 cu->method_list.clear ();
10107
10108 cu->language = pretend_language;
10109 cu->language_defn = language_def (cu->language);
10110
10111 /* The symbol tables are set up in read_type_unit_scope. */
10112 process_die (cu->dies, cu);
10113
10114 /* For now fudge the Go package. */
10115 if (cu->language == language_go)
10116 fixup_go_packaging (cu);
10117
10118 /* Now that we have processed all the DIEs in the CU, all the types
10119 should be complete, and it should now be safe to compute all of the
10120 physnames. */
10121 compute_delayed_physnames (cu);
10122
10123 if (cu->language == language_rust)
10124 rust_union_quirks (cu);
10125
10126 /* TUs share symbol tables.
10127 If this is the first TU to use this symtab, complete the construction
10128 of it with end_expandable_symtab. Otherwise, complete the addition of
10129 this TU's symbols to the existing symtab. */
10130 type_unit_group_unshareable *tug_unshare =
10131 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10132 if (tug_unshare->compunit_symtab == NULL)
10133 {
10134 buildsym_compunit *builder = cu->get_builder ();
10135 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10136 tug_unshare->compunit_symtab = cust;
10137
10138 if (cust != NULL)
10139 {
10140 /* Set symtab language to language from DW_AT_language. If the
10141 compilation is from a C file generated by language preprocessors,
10142 do not set the language if it was already deduced by
10143 start_subfile. */
10144 if (!(cu->language == language_c
10145 && COMPUNIT_FILETABS (cust)->language != language_c))
10146 COMPUNIT_FILETABS (cust)->language = cu->language;
10147 }
10148 }
10149 else
10150 {
10151 cu->get_builder ()->augment_type_symtab ();
10152 cust = tug_unshare->compunit_symtab;
10153 }
10154
10155 per_objfile->set_symtab (cu->per_cu, cust);
10156
10157 /* Not needed any more. */
10158 cu->reset_builder ();
10159 }
10160
10161 /* Process an imported unit DIE. */
10162
10163 static void
10164 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10165 {
10166 struct attribute *attr;
10167
10168 /* For now we don't handle imported units in type units. */
10169 if (cu->per_cu->is_debug_types)
10170 {
10171 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10172 " supported in type units [in module %s]"),
10173 objfile_name (cu->per_objfile->objfile));
10174 }
10175
10176 attr = dwarf2_attr (die, DW_AT_import, cu);
10177 if (attr != NULL)
10178 {
10179 sect_offset sect_off = attr->get_ref_die_offset ();
10180 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10181 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10182 dwarf2_per_cu_data *per_cu
10183 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10184
10185 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10186 into another compilation unit, at root level. Regard this as a hint,
10187 and ignore it. */
10188 if (die->parent && die->parent->parent == NULL
10189 && per_cu->unit_type == DW_UT_compile
10190 && per_cu->lang == language_cplus)
10191 return;
10192
10193 /* If necessary, add it to the queue and load its DIEs. */
10194 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10195 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10196 false, cu->language);
10197
10198 cu->per_cu->imported_symtabs_push (per_cu);
10199 }
10200 }
10201
10202 /* RAII object that represents a process_die scope: i.e.,
10203 starts/finishes processing a DIE. */
10204 class process_die_scope
10205 {
10206 public:
10207 process_die_scope (die_info *die, dwarf2_cu *cu)
10208 : m_die (die), m_cu (cu)
10209 {
10210 /* We should only be processing DIEs not already in process. */
10211 gdb_assert (!m_die->in_process);
10212 m_die->in_process = true;
10213 }
10214
10215 ~process_die_scope ()
10216 {
10217 m_die->in_process = false;
10218
10219 /* If we're done processing the DIE for the CU that owns the line
10220 header, we don't need the line header anymore. */
10221 if (m_cu->line_header_die_owner == m_die)
10222 {
10223 delete m_cu->line_header;
10224 m_cu->line_header = NULL;
10225 m_cu->line_header_die_owner = NULL;
10226 }
10227 }
10228
10229 private:
10230 die_info *m_die;
10231 dwarf2_cu *m_cu;
10232 };
10233
10234 /* Process a die and its children. */
10235
10236 static void
10237 process_die (struct die_info *die, struct dwarf2_cu *cu)
10238 {
10239 process_die_scope scope (die, cu);
10240
10241 switch (die->tag)
10242 {
10243 case DW_TAG_padding:
10244 break;
10245 case DW_TAG_compile_unit:
10246 case DW_TAG_partial_unit:
10247 read_file_scope (die, cu);
10248 break;
10249 case DW_TAG_type_unit:
10250 read_type_unit_scope (die, cu);
10251 break;
10252 case DW_TAG_subprogram:
10253 /* Nested subprograms in Fortran get a prefix. */
10254 if (cu->language == language_fortran
10255 && die->parent != NULL
10256 && die->parent->tag == DW_TAG_subprogram)
10257 cu->processing_has_namespace_info = true;
10258 /* Fall through. */
10259 case DW_TAG_inlined_subroutine:
10260 read_func_scope (die, cu);
10261 break;
10262 case DW_TAG_lexical_block:
10263 case DW_TAG_try_block:
10264 case DW_TAG_catch_block:
10265 read_lexical_block_scope (die, cu);
10266 break;
10267 case DW_TAG_call_site:
10268 case DW_TAG_GNU_call_site:
10269 read_call_site_scope (die, cu);
10270 break;
10271 case DW_TAG_class_type:
10272 case DW_TAG_interface_type:
10273 case DW_TAG_structure_type:
10274 case DW_TAG_union_type:
10275 process_structure_scope (die, cu);
10276 break;
10277 case DW_TAG_enumeration_type:
10278 process_enumeration_scope (die, cu);
10279 break;
10280
10281 /* These dies have a type, but processing them does not create
10282 a symbol or recurse to process the children. Therefore we can
10283 read them on-demand through read_type_die. */
10284 case DW_TAG_subroutine_type:
10285 case DW_TAG_set_type:
10286 case DW_TAG_pointer_type:
10287 case DW_TAG_ptr_to_member_type:
10288 case DW_TAG_reference_type:
10289 case DW_TAG_rvalue_reference_type:
10290 case DW_TAG_string_type:
10291 break;
10292
10293 case DW_TAG_array_type:
10294 /* We only need to handle this case for Ada -- in other
10295 languages, it's normal for the compiler to emit a typedef
10296 instead. */
10297 if (cu->language != language_ada)
10298 break;
10299 /* FALLTHROUGH */
10300 case DW_TAG_base_type:
10301 case DW_TAG_subrange_type:
10302 case DW_TAG_typedef:
10303 /* Add a typedef symbol for the type definition, if it has a
10304 DW_AT_name. */
10305 new_symbol (die, read_type_die (die, cu), cu);
10306 break;
10307 case DW_TAG_common_block:
10308 read_common_block (die, cu);
10309 break;
10310 case DW_TAG_common_inclusion:
10311 break;
10312 case DW_TAG_namespace:
10313 cu->processing_has_namespace_info = true;
10314 read_namespace (die, cu);
10315 break;
10316 case DW_TAG_module:
10317 cu->processing_has_namespace_info = true;
10318 read_module (die, cu);
10319 break;
10320 case DW_TAG_imported_declaration:
10321 cu->processing_has_namespace_info = true;
10322 if (read_namespace_alias (die, cu))
10323 break;
10324 /* The declaration is not a global namespace alias. */
10325 /* Fall through. */
10326 case DW_TAG_imported_module:
10327 cu->processing_has_namespace_info = true;
10328 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10329 || cu->language != language_fortran))
10330 complaint (_("Tag '%s' has unexpected children"),
10331 dwarf_tag_name (die->tag));
10332 read_import_statement (die, cu);
10333 break;
10334
10335 case DW_TAG_imported_unit:
10336 process_imported_unit_die (die, cu);
10337 break;
10338
10339 case DW_TAG_variable:
10340 read_variable (die, cu);
10341 break;
10342
10343 default:
10344 new_symbol (die, NULL, cu);
10345 break;
10346 }
10347 }
10348 \f
10349 /* DWARF name computation. */
10350
10351 /* A helper function for dwarf2_compute_name which determines whether DIE
10352 needs to have the name of the scope prepended to the name listed in the
10353 die. */
10354
10355 static int
10356 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10357 {
10358 struct attribute *attr;
10359
10360 switch (die->tag)
10361 {
10362 case DW_TAG_namespace:
10363 case DW_TAG_typedef:
10364 case DW_TAG_class_type:
10365 case DW_TAG_interface_type:
10366 case DW_TAG_structure_type:
10367 case DW_TAG_union_type:
10368 case DW_TAG_enumeration_type:
10369 case DW_TAG_enumerator:
10370 case DW_TAG_subprogram:
10371 case DW_TAG_inlined_subroutine:
10372 case DW_TAG_member:
10373 case DW_TAG_imported_declaration:
10374 return 1;
10375
10376 case DW_TAG_variable:
10377 case DW_TAG_constant:
10378 /* We only need to prefix "globally" visible variables. These include
10379 any variable marked with DW_AT_external or any variable that
10380 lives in a namespace. [Variables in anonymous namespaces
10381 require prefixing, but they are not DW_AT_external.] */
10382
10383 if (dwarf2_attr (die, DW_AT_specification, cu))
10384 {
10385 struct dwarf2_cu *spec_cu = cu;
10386
10387 return die_needs_namespace (die_specification (die, &spec_cu),
10388 spec_cu);
10389 }
10390
10391 attr = dwarf2_attr (die, DW_AT_external, cu);
10392 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10393 && die->parent->tag != DW_TAG_module)
10394 return 0;
10395 /* A variable in a lexical block of some kind does not need a
10396 namespace, even though in C++ such variables may be external
10397 and have a mangled name. */
10398 if (die->parent->tag == DW_TAG_lexical_block
10399 || die->parent->tag == DW_TAG_try_block
10400 || die->parent->tag == DW_TAG_catch_block
10401 || die->parent->tag == DW_TAG_subprogram)
10402 return 0;
10403 return 1;
10404
10405 default:
10406 return 0;
10407 }
10408 }
10409
10410 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10411 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10412 defined for the given DIE. */
10413
10414 static struct attribute *
10415 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10416 {
10417 struct attribute *attr;
10418
10419 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10420 if (attr == NULL)
10421 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10422
10423 return attr;
10424 }
10425
10426 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10427 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10428 defined for the given DIE. */
10429
10430 static const char *
10431 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10432 {
10433 const char *linkage_name;
10434
10435 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10436 if (linkage_name == NULL)
10437 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10438
10439 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10440 See https://github.com/rust-lang/rust/issues/32925. */
10441 if (cu->language == language_rust && linkage_name != NULL
10442 && strchr (linkage_name, '{') != NULL)
10443 linkage_name = NULL;
10444
10445 return linkage_name;
10446 }
10447
10448 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10449 compute the physname for the object, which include a method's:
10450 - formal parameters (C++),
10451 - receiver type (Go),
10452
10453 The term "physname" is a bit confusing.
10454 For C++, for example, it is the demangled name.
10455 For Go, for example, it's the mangled name.
10456
10457 For Ada, return the DIE's linkage name rather than the fully qualified
10458 name. PHYSNAME is ignored..
10459
10460 The result is allocated on the objfile->per_bfd's obstack and
10461 canonicalized. */
10462
10463 static const char *
10464 dwarf2_compute_name (const char *name,
10465 struct die_info *die, struct dwarf2_cu *cu,
10466 int physname)
10467 {
10468 struct objfile *objfile = cu->per_objfile->objfile;
10469
10470 if (name == NULL)
10471 name = dwarf2_name (die, cu);
10472
10473 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10474 but otherwise compute it by typename_concat inside GDB.
10475 FIXME: Actually this is not really true, or at least not always true.
10476 It's all very confusing. compute_and_set_names doesn't try to demangle
10477 Fortran names because there is no mangling standard. So new_symbol
10478 will set the demangled name to the result of dwarf2_full_name, and it is
10479 the demangled name that GDB uses if it exists. */
10480 if (cu->language == language_ada
10481 || (cu->language == language_fortran && physname))
10482 {
10483 /* For Ada unit, we prefer the linkage name over the name, as
10484 the former contains the exported name, which the user expects
10485 to be able to reference. Ideally, we want the user to be able
10486 to reference this entity using either natural or linkage name,
10487 but we haven't started looking at this enhancement yet. */
10488 const char *linkage_name = dw2_linkage_name (die, cu);
10489
10490 if (linkage_name != NULL)
10491 return linkage_name;
10492 }
10493
10494 /* These are the only languages we know how to qualify names in. */
10495 if (name != NULL
10496 && (cu->language == language_cplus
10497 || cu->language == language_fortran || cu->language == language_d
10498 || cu->language == language_rust))
10499 {
10500 if (die_needs_namespace (die, cu))
10501 {
10502 const char *prefix;
10503 const char *canonical_name = NULL;
10504
10505 string_file buf;
10506
10507 prefix = determine_prefix (die, cu);
10508 if (*prefix != '\0')
10509 {
10510 gdb::unique_xmalloc_ptr<char> prefixed_name
10511 (typename_concat (NULL, prefix, name, physname, cu));
10512
10513 buf.puts (prefixed_name.get ());
10514 }
10515 else
10516 buf.puts (name);
10517
10518 /* Template parameters may be specified in the DIE's DW_AT_name, or
10519 as children with DW_TAG_template_type_param or
10520 DW_TAG_value_type_param. If the latter, add them to the name
10521 here. If the name already has template parameters, then
10522 skip this step; some versions of GCC emit both, and
10523 it is more efficient to use the pre-computed name.
10524
10525 Something to keep in mind about this process: it is very
10526 unlikely, or in some cases downright impossible, to produce
10527 something that will match the mangled name of a function.
10528 If the definition of the function has the same debug info,
10529 we should be able to match up with it anyway. But fallbacks
10530 using the minimal symbol, for instance to find a method
10531 implemented in a stripped copy of libstdc++, will not work.
10532 If we do not have debug info for the definition, we will have to
10533 match them up some other way.
10534
10535 When we do name matching there is a related problem with function
10536 templates; two instantiated function templates are allowed to
10537 differ only by their return types, which we do not add here. */
10538
10539 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10540 {
10541 struct attribute *attr;
10542 struct die_info *child;
10543 int first = 1;
10544 const language_defn *cplus_lang = language_def (cu->language);
10545
10546 die->building_fullname = 1;
10547
10548 for (child = die->child; child != NULL; child = child->sibling)
10549 {
10550 struct type *type;
10551 LONGEST value;
10552 const gdb_byte *bytes;
10553 struct dwarf2_locexpr_baton *baton;
10554 struct value *v;
10555
10556 if (child->tag != DW_TAG_template_type_param
10557 && child->tag != DW_TAG_template_value_param)
10558 continue;
10559
10560 if (first)
10561 {
10562 buf.puts ("<");
10563 first = 0;
10564 }
10565 else
10566 buf.puts (", ");
10567
10568 attr = dwarf2_attr (child, DW_AT_type, cu);
10569 if (attr == NULL)
10570 {
10571 complaint (_("template parameter missing DW_AT_type"));
10572 buf.puts ("UNKNOWN_TYPE");
10573 continue;
10574 }
10575 type = die_type (child, cu);
10576
10577 if (child->tag == DW_TAG_template_type_param)
10578 {
10579 cplus_lang->print_type (type, "", &buf, -1, 0,
10580 &type_print_raw_options);
10581 continue;
10582 }
10583
10584 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10585 if (attr == NULL)
10586 {
10587 complaint (_("template parameter missing "
10588 "DW_AT_const_value"));
10589 buf.puts ("UNKNOWN_VALUE");
10590 continue;
10591 }
10592
10593 dwarf2_const_value_attr (attr, type, name,
10594 &cu->comp_unit_obstack, cu,
10595 &value, &bytes, &baton);
10596
10597 if (type->has_no_signedness ())
10598 /* GDB prints characters as NUMBER 'CHAR'. If that's
10599 changed, this can use value_print instead. */
10600 cplus_lang->printchar (value, type, &buf);
10601 else
10602 {
10603 struct value_print_options opts;
10604
10605 if (baton != NULL)
10606 v = dwarf2_evaluate_loc_desc (type, NULL,
10607 baton->data,
10608 baton->size,
10609 baton->per_cu,
10610 baton->per_objfile);
10611 else if (bytes != NULL)
10612 {
10613 v = allocate_value (type);
10614 memcpy (value_contents_writeable (v), bytes,
10615 TYPE_LENGTH (type));
10616 }
10617 else
10618 v = value_from_longest (type, value);
10619
10620 /* Specify decimal so that we do not depend on
10621 the radix. */
10622 get_formatted_print_options (&opts, 'd');
10623 opts.raw = 1;
10624 value_print (v, &buf, &opts);
10625 release_value (v);
10626 }
10627 }
10628
10629 die->building_fullname = 0;
10630
10631 if (!first)
10632 {
10633 /* Close the argument list, with a space if necessary
10634 (nested templates). */
10635 if (!buf.empty () && buf.string ().back () == '>')
10636 buf.puts (" >");
10637 else
10638 buf.puts (">");
10639 }
10640 }
10641
10642 /* For C++ methods, append formal parameter type
10643 information, if PHYSNAME. */
10644
10645 if (physname && die->tag == DW_TAG_subprogram
10646 && cu->language == language_cplus)
10647 {
10648 struct type *type = read_type_die (die, cu);
10649
10650 c_type_print_args (type, &buf, 1, cu->language,
10651 &type_print_raw_options);
10652
10653 if (cu->language == language_cplus)
10654 {
10655 /* Assume that an artificial first parameter is
10656 "this", but do not crash if it is not. RealView
10657 marks unnamed (and thus unused) parameters as
10658 artificial; there is no way to differentiate
10659 the two cases. */
10660 if (type->num_fields () > 0
10661 && TYPE_FIELD_ARTIFICIAL (type, 0)
10662 && type->field (0).type ()->code () == TYPE_CODE_PTR
10663 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10664 buf.puts (" const");
10665 }
10666 }
10667
10668 const std::string &intermediate_name = buf.string ();
10669
10670 if (cu->language == language_cplus)
10671 canonical_name
10672 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10673 objfile);
10674
10675 /* If we only computed INTERMEDIATE_NAME, or if
10676 INTERMEDIATE_NAME is already canonical, then we need to
10677 intern it. */
10678 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10679 name = objfile->intern (intermediate_name);
10680 else
10681 name = canonical_name;
10682 }
10683 }
10684
10685 return name;
10686 }
10687
10688 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10689 If scope qualifiers are appropriate they will be added. The result
10690 will be allocated on the storage_obstack, or NULL if the DIE does
10691 not have a name. NAME may either be from a previous call to
10692 dwarf2_name or NULL.
10693
10694 The output string will be canonicalized (if C++). */
10695
10696 static const char *
10697 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10698 {
10699 return dwarf2_compute_name (name, die, cu, 0);
10700 }
10701
10702 /* Construct a physname for the given DIE in CU. NAME may either be
10703 from a previous call to dwarf2_name or NULL. The result will be
10704 allocated on the objfile_objstack or NULL if the DIE does not have a
10705 name.
10706
10707 The output string will be canonicalized (if C++). */
10708
10709 static const char *
10710 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10711 {
10712 struct objfile *objfile = cu->per_objfile->objfile;
10713 const char *retval, *mangled = NULL, *canon = NULL;
10714 int need_copy = 1;
10715
10716 /* In this case dwarf2_compute_name is just a shortcut not building anything
10717 on its own. */
10718 if (!die_needs_namespace (die, cu))
10719 return dwarf2_compute_name (name, die, cu, 1);
10720
10721 if (cu->language != language_rust)
10722 mangled = dw2_linkage_name (die, cu);
10723
10724 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10725 has computed. */
10726 gdb::unique_xmalloc_ptr<char> demangled;
10727 if (mangled != NULL)
10728 {
10729
10730 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10731 {
10732 /* Do nothing (do not demangle the symbol name). */
10733 }
10734 else
10735 {
10736 /* Use DMGL_RET_DROP for C++ template functions to suppress
10737 their return type. It is easier for GDB users to search
10738 for such functions as `name(params)' than `long name(params)'.
10739 In such case the minimal symbol names do not match the full
10740 symbol names but for template functions there is never a need
10741 to look up their definition from their declaration so
10742 the only disadvantage remains the minimal symbol variant
10743 `long name(params)' does not have the proper inferior type. */
10744 demangled.reset (gdb_demangle (mangled,
10745 (DMGL_PARAMS | DMGL_ANSI
10746 | DMGL_RET_DROP)));
10747 }
10748 if (demangled)
10749 canon = demangled.get ();
10750 else
10751 {
10752 canon = mangled;
10753 need_copy = 0;
10754 }
10755 }
10756
10757 if (canon == NULL || check_physname)
10758 {
10759 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10760
10761 if (canon != NULL && strcmp (physname, canon) != 0)
10762 {
10763 /* It may not mean a bug in GDB. The compiler could also
10764 compute DW_AT_linkage_name incorrectly. But in such case
10765 GDB would need to be bug-to-bug compatible. */
10766
10767 complaint (_("Computed physname <%s> does not match demangled <%s> "
10768 "(from linkage <%s>) - DIE at %s [in module %s]"),
10769 physname, canon, mangled, sect_offset_str (die->sect_off),
10770 objfile_name (objfile));
10771
10772 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10773 is available here - over computed PHYSNAME. It is safer
10774 against both buggy GDB and buggy compilers. */
10775
10776 retval = canon;
10777 }
10778 else
10779 {
10780 retval = physname;
10781 need_copy = 0;
10782 }
10783 }
10784 else
10785 retval = canon;
10786
10787 if (need_copy)
10788 retval = objfile->intern (retval);
10789
10790 return retval;
10791 }
10792
10793 /* Inspect DIE in CU for a namespace alias. If one exists, record
10794 a new symbol for it.
10795
10796 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10797
10798 static int
10799 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10800 {
10801 struct attribute *attr;
10802
10803 /* If the die does not have a name, this is not a namespace
10804 alias. */
10805 attr = dwarf2_attr (die, DW_AT_name, cu);
10806 if (attr != NULL)
10807 {
10808 int num;
10809 struct die_info *d = die;
10810 struct dwarf2_cu *imported_cu = cu;
10811
10812 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10813 keep inspecting DIEs until we hit the underlying import. */
10814 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10815 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10816 {
10817 attr = dwarf2_attr (d, DW_AT_import, cu);
10818 if (attr == NULL)
10819 break;
10820
10821 d = follow_die_ref (d, attr, &imported_cu);
10822 if (d->tag != DW_TAG_imported_declaration)
10823 break;
10824 }
10825
10826 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10827 {
10828 complaint (_("DIE at %s has too many recursively imported "
10829 "declarations"), sect_offset_str (d->sect_off));
10830 return 0;
10831 }
10832
10833 if (attr != NULL)
10834 {
10835 struct type *type;
10836 sect_offset sect_off = attr->get_ref_die_offset ();
10837
10838 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10839 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10840 {
10841 /* This declaration is a global namespace alias. Add
10842 a symbol for it whose type is the aliased namespace. */
10843 new_symbol (die, type, cu);
10844 return 1;
10845 }
10846 }
10847 }
10848
10849 return 0;
10850 }
10851
10852 /* Return the using directives repository (global or local?) to use in the
10853 current context for CU.
10854
10855 For Ada, imported declarations can materialize renamings, which *may* be
10856 global. However it is impossible (for now?) in DWARF to distinguish
10857 "external" imported declarations and "static" ones. As all imported
10858 declarations seem to be static in all other languages, make them all CU-wide
10859 global only in Ada. */
10860
10861 static struct using_direct **
10862 using_directives (struct dwarf2_cu *cu)
10863 {
10864 if (cu->language == language_ada
10865 && cu->get_builder ()->outermost_context_p ())
10866 return cu->get_builder ()->get_global_using_directives ();
10867 else
10868 return cu->get_builder ()->get_local_using_directives ();
10869 }
10870
10871 /* Read the import statement specified by the given die and record it. */
10872
10873 static void
10874 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10875 {
10876 struct objfile *objfile = cu->per_objfile->objfile;
10877 struct attribute *import_attr;
10878 struct die_info *imported_die, *child_die;
10879 struct dwarf2_cu *imported_cu;
10880 const char *imported_name;
10881 const char *imported_name_prefix;
10882 const char *canonical_name;
10883 const char *import_alias;
10884 const char *imported_declaration = NULL;
10885 const char *import_prefix;
10886 std::vector<const char *> excludes;
10887
10888 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10889 if (import_attr == NULL)
10890 {
10891 complaint (_("Tag '%s' has no DW_AT_import"),
10892 dwarf_tag_name (die->tag));
10893 return;
10894 }
10895
10896 imported_cu = cu;
10897 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10898 imported_name = dwarf2_name (imported_die, imported_cu);
10899 if (imported_name == NULL)
10900 {
10901 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10902
10903 The import in the following code:
10904 namespace A
10905 {
10906 typedef int B;
10907 }
10908
10909 int main ()
10910 {
10911 using A::B;
10912 B b;
10913 return b;
10914 }
10915
10916 ...
10917 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10918 <52> DW_AT_decl_file : 1
10919 <53> DW_AT_decl_line : 6
10920 <54> DW_AT_import : <0x75>
10921 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10922 <59> DW_AT_name : B
10923 <5b> DW_AT_decl_file : 1
10924 <5c> DW_AT_decl_line : 2
10925 <5d> DW_AT_type : <0x6e>
10926 ...
10927 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10928 <76> DW_AT_byte_size : 4
10929 <77> DW_AT_encoding : 5 (signed)
10930
10931 imports the wrong die ( 0x75 instead of 0x58 ).
10932 This case will be ignored until the gcc bug is fixed. */
10933 return;
10934 }
10935
10936 /* Figure out the local name after import. */
10937 import_alias = dwarf2_name (die, cu);
10938
10939 /* Figure out where the statement is being imported to. */
10940 import_prefix = determine_prefix (die, cu);
10941
10942 /* Figure out what the scope of the imported die is and prepend it
10943 to the name of the imported die. */
10944 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10945
10946 if (imported_die->tag != DW_TAG_namespace
10947 && imported_die->tag != DW_TAG_module)
10948 {
10949 imported_declaration = imported_name;
10950 canonical_name = imported_name_prefix;
10951 }
10952 else if (strlen (imported_name_prefix) > 0)
10953 canonical_name = obconcat (&objfile->objfile_obstack,
10954 imported_name_prefix,
10955 (cu->language == language_d ? "." : "::"),
10956 imported_name, (char *) NULL);
10957 else
10958 canonical_name = imported_name;
10959
10960 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10961 for (child_die = die->child; child_die && child_die->tag;
10962 child_die = child_die->sibling)
10963 {
10964 /* DWARF-4: A Fortran use statement with a “rename list” may be
10965 represented by an imported module entry with an import attribute
10966 referring to the module and owned entries corresponding to those
10967 entities that are renamed as part of being imported. */
10968
10969 if (child_die->tag != DW_TAG_imported_declaration)
10970 {
10971 complaint (_("child DW_TAG_imported_declaration expected "
10972 "- DIE at %s [in module %s]"),
10973 sect_offset_str (child_die->sect_off),
10974 objfile_name (objfile));
10975 continue;
10976 }
10977
10978 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10979 if (import_attr == NULL)
10980 {
10981 complaint (_("Tag '%s' has no DW_AT_import"),
10982 dwarf_tag_name (child_die->tag));
10983 continue;
10984 }
10985
10986 imported_cu = cu;
10987 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10988 &imported_cu);
10989 imported_name = dwarf2_name (imported_die, imported_cu);
10990 if (imported_name == NULL)
10991 {
10992 complaint (_("child DW_TAG_imported_declaration has unknown "
10993 "imported name - DIE at %s [in module %s]"),
10994 sect_offset_str (child_die->sect_off),
10995 objfile_name (objfile));
10996 continue;
10997 }
10998
10999 excludes.push_back (imported_name);
11000
11001 process_die (child_die, cu);
11002 }
11003
11004 add_using_directive (using_directives (cu),
11005 import_prefix,
11006 canonical_name,
11007 import_alias,
11008 imported_declaration,
11009 excludes,
11010 0,
11011 &objfile->objfile_obstack);
11012 }
11013
11014 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11015 types, but gives them a size of zero. Starting with version 14,
11016 ICC is compatible with GCC. */
11017
11018 static bool
11019 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11020 {
11021 if (!cu->checked_producer)
11022 check_producer (cu);
11023
11024 return cu->producer_is_icc_lt_14;
11025 }
11026
11027 /* ICC generates a DW_AT_type for C void functions. This was observed on
11028 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11029 which says that void functions should not have a DW_AT_type. */
11030
11031 static bool
11032 producer_is_icc (struct dwarf2_cu *cu)
11033 {
11034 if (!cu->checked_producer)
11035 check_producer (cu);
11036
11037 return cu->producer_is_icc;
11038 }
11039
11040 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11041 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11042 this, it was first present in GCC release 4.3.0. */
11043
11044 static bool
11045 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11046 {
11047 if (!cu->checked_producer)
11048 check_producer (cu);
11049
11050 return cu->producer_is_gcc_lt_4_3;
11051 }
11052
11053 static file_and_directory
11054 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11055 {
11056 file_and_directory res;
11057
11058 /* Find the filename. Do not use dwarf2_name here, since the filename
11059 is not a source language identifier. */
11060 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11061 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11062
11063 if (res.comp_dir == NULL
11064 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11065 && IS_ABSOLUTE_PATH (res.name))
11066 {
11067 res.comp_dir_storage = ldirname (res.name);
11068 if (!res.comp_dir_storage.empty ())
11069 res.comp_dir = res.comp_dir_storage.c_str ();
11070 }
11071 if (res.comp_dir != NULL)
11072 {
11073 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11074 directory, get rid of it. */
11075 const char *cp = strchr (res.comp_dir, ':');
11076
11077 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11078 res.comp_dir = cp + 1;
11079 }
11080
11081 if (res.name == NULL)
11082 res.name = "<unknown>";
11083
11084 return res;
11085 }
11086
11087 /* Handle DW_AT_stmt_list for a compilation unit.
11088 DIE is the DW_TAG_compile_unit die for CU.
11089 COMP_DIR is the compilation directory. LOWPC is passed to
11090 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11091
11092 static void
11093 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11094 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11095 {
11096 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11097 struct attribute *attr;
11098 struct line_header line_header_local;
11099 hashval_t line_header_local_hash;
11100 void **slot;
11101 int decode_mapping;
11102
11103 gdb_assert (! cu->per_cu->is_debug_types);
11104
11105 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11106 if (attr == NULL || !attr->form_is_unsigned ())
11107 return;
11108
11109 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11110
11111 /* The line header hash table is only created if needed (it exists to
11112 prevent redundant reading of the line table for partial_units).
11113 If we're given a partial_unit, we'll need it. If we're given a
11114 compile_unit, then use the line header hash table if it's already
11115 created, but don't create one just yet. */
11116
11117 if (per_objfile->line_header_hash == NULL
11118 && die->tag == DW_TAG_partial_unit)
11119 {
11120 per_objfile->line_header_hash
11121 .reset (htab_create_alloc (127, line_header_hash_voidp,
11122 line_header_eq_voidp,
11123 free_line_header_voidp,
11124 xcalloc, xfree));
11125 }
11126
11127 line_header_local.sect_off = line_offset;
11128 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11129 line_header_local_hash = line_header_hash (&line_header_local);
11130 if (per_objfile->line_header_hash != NULL)
11131 {
11132 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11133 &line_header_local,
11134 line_header_local_hash, NO_INSERT);
11135
11136 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11137 is not present in *SLOT (since if there is something in *SLOT then
11138 it will be for a partial_unit). */
11139 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11140 {
11141 gdb_assert (*slot != NULL);
11142 cu->line_header = (struct line_header *) *slot;
11143 return;
11144 }
11145 }
11146
11147 /* dwarf_decode_line_header does not yet provide sufficient information.
11148 We always have to call also dwarf_decode_lines for it. */
11149 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11150 if (lh == NULL)
11151 return;
11152
11153 cu->line_header = lh.release ();
11154 cu->line_header_die_owner = die;
11155
11156 if (per_objfile->line_header_hash == NULL)
11157 slot = NULL;
11158 else
11159 {
11160 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11161 &line_header_local,
11162 line_header_local_hash, INSERT);
11163 gdb_assert (slot != NULL);
11164 }
11165 if (slot != NULL && *slot == NULL)
11166 {
11167 /* This newly decoded line number information unit will be owned
11168 by line_header_hash hash table. */
11169 *slot = cu->line_header;
11170 cu->line_header_die_owner = NULL;
11171 }
11172 else
11173 {
11174 /* We cannot free any current entry in (*slot) as that struct line_header
11175 may be already used by multiple CUs. Create only temporary decoded
11176 line_header for this CU - it may happen at most once for each line
11177 number information unit. And if we're not using line_header_hash
11178 then this is what we want as well. */
11179 gdb_assert (die->tag != DW_TAG_partial_unit);
11180 }
11181 decode_mapping = (die->tag != DW_TAG_partial_unit);
11182 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11183 decode_mapping);
11184
11185 }
11186
11187 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11188
11189 static void
11190 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11191 {
11192 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11193 struct objfile *objfile = per_objfile->objfile;
11194 struct gdbarch *gdbarch = objfile->arch ();
11195 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11196 CORE_ADDR highpc = ((CORE_ADDR) 0);
11197 struct attribute *attr;
11198 struct die_info *child_die;
11199 CORE_ADDR baseaddr;
11200
11201 prepare_one_comp_unit (cu, die, cu->language);
11202 baseaddr = objfile->text_section_offset ();
11203
11204 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11205
11206 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11207 from finish_block. */
11208 if (lowpc == ((CORE_ADDR) -1))
11209 lowpc = highpc;
11210 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11211
11212 file_and_directory fnd = find_file_and_directory (die, cu);
11213
11214 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11215 standardised yet. As a workaround for the language detection we fall
11216 back to the DW_AT_producer string. */
11217 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11218 cu->language = language_opencl;
11219
11220 /* Similar hack for Go. */
11221 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11222 set_cu_language (DW_LANG_Go, cu);
11223
11224 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11225
11226 /* Decode line number information if present. We do this before
11227 processing child DIEs, so that the line header table is available
11228 for DW_AT_decl_file. */
11229 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11230
11231 /* Process all dies in compilation unit. */
11232 if (die->child != NULL)
11233 {
11234 child_die = die->child;
11235 while (child_die && child_die->tag)
11236 {
11237 process_die (child_die, cu);
11238 child_die = child_die->sibling;
11239 }
11240 }
11241
11242 /* Decode macro information, if present. Dwarf 2 macro information
11243 refers to information in the line number info statement program
11244 header, so we can only read it if we've read the header
11245 successfully. */
11246 attr = dwarf2_attr (die, DW_AT_macros, cu);
11247 if (attr == NULL)
11248 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11249 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11250 {
11251 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11252 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11253
11254 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11255 }
11256 else
11257 {
11258 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11259 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11260 {
11261 unsigned int macro_offset = attr->as_unsigned ();
11262
11263 dwarf_decode_macros (cu, macro_offset, 0);
11264 }
11265 }
11266 }
11267
11268 void
11269 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11270 {
11271 struct type_unit_group *tu_group;
11272 int first_time;
11273 struct attribute *attr;
11274 unsigned int i;
11275 struct signatured_type *sig_type;
11276
11277 gdb_assert (per_cu->is_debug_types);
11278 sig_type = (struct signatured_type *) per_cu;
11279
11280 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11281
11282 /* If we're using .gdb_index (includes -readnow) then
11283 per_cu->type_unit_group may not have been set up yet. */
11284 if (sig_type->type_unit_group == NULL)
11285 sig_type->type_unit_group = get_type_unit_group (this, attr);
11286 tu_group = sig_type->type_unit_group;
11287
11288 /* If we've already processed this stmt_list there's no real need to
11289 do it again, we could fake it and just recreate the part we need
11290 (file name,index -> symtab mapping). If data shows this optimization
11291 is useful we can do it then. */
11292 type_unit_group_unshareable *tug_unshare
11293 = per_objfile->get_type_unit_group_unshareable (tu_group);
11294 first_time = tug_unshare->compunit_symtab == NULL;
11295
11296 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11297 debug info. */
11298 line_header_up lh;
11299 if (attr != NULL && attr->form_is_unsigned ())
11300 {
11301 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11302 lh = dwarf_decode_line_header (line_offset, this);
11303 }
11304 if (lh == NULL)
11305 {
11306 if (first_time)
11307 start_symtab ("", NULL, 0);
11308 else
11309 {
11310 gdb_assert (tug_unshare->symtabs == NULL);
11311 gdb_assert (m_builder == nullptr);
11312 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11313 m_builder.reset (new struct buildsym_compunit
11314 (COMPUNIT_OBJFILE (cust), "",
11315 COMPUNIT_DIRNAME (cust),
11316 compunit_language (cust),
11317 0, cust));
11318 list_in_scope = get_builder ()->get_file_symbols ();
11319 }
11320 return;
11321 }
11322
11323 line_header = lh.release ();
11324 line_header_die_owner = die;
11325
11326 if (first_time)
11327 {
11328 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11329
11330 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11331 still initializing it, and our caller (a few levels up)
11332 process_full_type_unit still needs to know if this is the first
11333 time. */
11334
11335 tug_unshare->symtabs
11336 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11337 struct symtab *, line_header->file_names_size ());
11338
11339 auto &file_names = line_header->file_names ();
11340 for (i = 0; i < file_names.size (); ++i)
11341 {
11342 file_entry &fe = file_names[i];
11343 dwarf2_start_subfile (this, fe.name,
11344 fe.include_dir (line_header));
11345 buildsym_compunit *b = get_builder ();
11346 if (b->get_current_subfile ()->symtab == NULL)
11347 {
11348 /* NOTE: start_subfile will recognize when it's been
11349 passed a file it has already seen. So we can't
11350 assume there's a simple mapping from
11351 cu->line_header->file_names to subfiles, plus
11352 cu->line_header->file_names may contain dups. */
11353 b->get_current_subfile ()->symtab
11354 = allocate_symtab (cust, b->get_current_subfile ()->name);
11355 }
11356
11357 fe.symtab = b->get_current_subfile ()->symtab;
11358 tug_unshare->symtabs[i] = fe.symtab;
11359 }
11360 }
11361 else
11362 {
11363 gdb_assert (m_builder == nullptr);
11364 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11365 m_builder.reset (new struct buildsym_compunit
11366 (COMPUNIT_OBJFILE (cust), "",
11367 COMPUNIT_DIRNAME (cust),
11368 compunit_language (cust),
11369 0, cust));
11370 list_in_scope = get_builder ()->get_file_symbols ();
11371
11372 auto &file_names = line_header->file_names ();
11373 for (i = 0; i < file_names.size (); ++i)
11374 {
11375 file_entry &fe = file_names[i];
11376 fe.symtab = tug_unshare->symtabs[i];
11377 }
11378 }
11379
11380 /* The main symtab is allocated last. Type units don't have DW_AT_name
11381 so they don't have a "real" (so to speak) symtab anyway.
11382 There is later code that will assign the main symtab to all symbols
11383 that don't have one. We need to handle the case of a symbol with a
11384 missing symtab (DW_AT_decl_file) anyway. */
11385 }
11386
11387 /* Process DW_TAG_type_unit.
11388 For TUs we want to skip the first top level sibling if it's not the
11389 actual type being defined by this TU. In this case the first top
11390 level sibling is there to provide context only. */
11391
11392 static void
11393 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11394 {
11395 struct die_info *child_die;
11396
11397 prepare_one_comp_unit (cu, die, language_minimal);
11398
11399 /* Initialize (or reinitialize) the machinery for building symtabs.
11400 We do this before processing child DIEs, so that the line header table
11401 is available for DW_AT_decl_file. */
11402 cu->setup_type_unit_groups (die);
11403
11404 if (die->child != NULL)
11405 {
11406 child_die = die->child;
11407 while (child_die && child_die->tag)
11408 {
11409 process_die (child_die, cu);
11410 child_die = child_die->sibling;
11411 }
11412 }
11413 }
11414 \f
11415 /* DWO/DWP files.
11416
11417 http://gcc.gnu.org/wiki/DebugFission
11418 http://gcc.gnu.org/wiki/DebugFissionDWP
11419
11420 To simplify handling of both DWO files ("object" files with the DWARF info)
11421 and DWP files (a file with the DWOs packaged up into one file), we treat
11422 DWP files as having a collection of virtual DWO files. */
11423
11424 static hashval_t
11425 hash_dwo_file (const void *item)
11426 {
11427 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11428 hashval_t hash;
11429
11430 hash = htab_hash_string (dwo_file->dwo_name);
11431 if (dwo_file->comp_dir != NULL)
11432 hash += htab_hash_string (dwo_file->comp_dir);
11433 return hash;
11434 }
11435
11436 static int
11437 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11438 {
11439 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11440 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11441
11442 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11443 return 0;
11444 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11445 return lhs->comp_dir == rhs->comp_dir;
11446 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11447 }
11448
11449 /* Allocate a hash table for DWO files. */
11450
11451 static htab_up
11452 allocate_dwo_file_hash_table ()
11453 {
11454 auto delete_dwo_file = [] (void *item)
11455 {
11456 struct dwo_file *dwo_file = (struct dwo_file *) item;
11457
11458 delete dwo_file;
11459 };
11460
11461 return htab_up (htab_create_alloc (41,
11462 hash_dwo_file,
11463 eq_dwo_file,
11464 delete_dwo_file,
11465 xcalloc, xfree));
11466 }
11467
11468 /* Lookup DWO file DWO_NAME. */
11469
11470 static void **
11471 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11472 const char *dwo_name,
11473 const char *comp_dir)
11474 {
11475 struct dwo_file find_entry;
11476 void **slot;
11477
11478 if (per_objfile->per_bfd->dwo_files == NULL)
11479 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11480
11481 find_entry.dwo_name = dwo_name;
11482 find_entry.comp_dir = comp_dir;
11483 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11484 INSERT);
11485
11486 return slot;
11487 }
11488
11489 static hashval_t
11490 hash_dwo_unit (const void *item)
11491 {
11492 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11493
11494 /* This drops the top 32 bits of the id, but is ok for a hash. */
11495 return dwo_unit->signature;
11496 }
11497
11498 static int
11499 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11500 {
11501 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11502 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11503
11504 /* The signature is assumed to be unique within the DWO file.
11505 So while object file CU dwo_id's always have the value zero,
11506 that's OK, assuming each object file DWO file has only one CU,
11507 and that's the rule for now. */
11508 return lhs->signature == rhs->signature;
11509 }
11510
11511 /* Allocate a hash table for DWO CUs,TUs.
11512 There is one of these tables for each of CUs,TUs for each DWO file. */
11513
11514 static htab_up
11515 allocate_dwo_unit_table ()
11516 {
11517 /* Start out with a pretty small number.
11518 Generally DWO files contain only one CU and maybe some TUs. */
11519 return htab_up (htab_create_alloc (3,
11520 hash_dwo_unit,
11521 eq_dwo_unit,
11522 NULL, xcalloc, xfree));
11523 }
11524
11525 /* die_reader_func for create_dwo_cu. */
11526
11527 static void
11528 create_dwo_cu_reader (const struct die_reader_specs *reader,
11529 const gdb_byte *info_ptr,
11530 struct die_info *comp_unit_die,
11531 struct dwo_file *dwo_file,
11532 struct dwo_unit *dwo_unit)
11533 {
11534 struct dwarf2_cu *cu = reader->cu;
11535 sect_offset sect_off = cu->per_cu->sect_off;
11536 struct dwarf2_section_info *section = cu->per_cu->section;
11537
11538 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11539 if (!signature.has_value ())
11540 {
11541 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11542 " its dwo_id [in module %s]"),
11543 sect_offset_str (sect_off), dwo_file->dwo_name);
11544 return;
11545 }
11546
11547 dwo_unit->dwo_file = dwo_file;
11548 dwo_unit->signature = *signature;
11549 dwo_unit->section = section;
11550 dwo_unit->sect_off = sect_off;
11551 dwo_unit->length = cu->per_cu->length;
11552
11553 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11554 sect_offset_str (sect_off),
11555 hex_string (dwo_unit->signature));
11556 }
11557
11558 /* Create the dwo_units for the CUs in a DWO_FILE.
11559 Note: This function processes DWO files only, not DWP files. */
11560
11561 static void
11562 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11563 dwarf2_cu *cu, struct dwo_file &dwo_file,
11564 dwarf2_section_info &section, htab_up &cus_htab)
11565 {
11566 struct objfile *objfile = per_objfile->objfile;
11567 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11568 const gdb_byte *info_ptr, *end_ptr;
11569
11570 section.read (objfile);
11571 info_ptr = section.buffer;
11572
11573 if (info_ptr == NULL)
11574 return;
11575
11576 dwarf_read_debug_printf ("Reading %s for %s:",
11577 section.get_name (),
11578 section.get_file_name ());
11579
11580 end_ptr = info_ptr + section.size;
11581 while (info_ptr < end_ptr)
11582 {
11583 struct dwarf2_per_cu_data per_cu;
11584 struct dwo_unit read_unit {};
11585 struct dwo_unit *dwo_unit;
11586 void **slot;
11587 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11588
11589 memset (&per_cu, 0, sizeof (per_cu));
11590 per_cu.per_bfd = per_bfd;
11591 per_cu.is_debug_types = 0;
11592 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11593 per_cu.section = &section;
11594
11595 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11596 if (!reader.dummy_p)
11597 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11598 &dwo_file, &read_unit);
11599 info_ptr += per_cu.length;
11600
11601 // If the unit could not be parsed, skip it.
11602 if (read_unit.dwo_file == NULL)
11603 continue;
11604
11605 if (cus_htab == NULL)
11606 cus_htab = allocate_dwo_unit_table ();
11607
11608 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11609 struct dwo_unit);
11610 *dwo_unit = read_unit;
11611 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11612 gdb_assert (slot != NULL);
11613 if (*slot != NULL)
11614 {
11615 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11616 sect_offset dup_sect_off = dup_cu->sect_off;
11617
11618 complaint (_("debug cu entry at offset %s is duplicate to"
11619 " the entry at offset %s, signature %s"),
11620 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11621 hex_string (dwo_unit->signature));
11622 }
11623 *slot = (void *)dwo_unit;
11624 }
11625 }
11626
11627 /* DWP file .debug_{cu,tu}_index section format:
11628 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11629 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11630
11631 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11632 officially standard DWP format was published with DWARF v5 and is called
11633 Version 5. There are no versions 3 or 4.
11634
11635 DWP Version 1:
11636
11637 Both index sections have the same format, and serve to map a 64-bit
11638 signature to a set of section numbers. Each section begins with a header,
11639 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11640 indexes, and a pool of 32-bit section numbers. The index sections will be
11641 aligned at 8-byte boundaries in the file.
11642
11643 The index section header consists of:
11644
11645 V, 32 bit version number
11646 -, 32 bits unused
11647 N, 32 bit number of compilation units or type units in the index
11648 M, 32 bit number of slots in the hash table
11649
11650 Numbers are recorded using the byte order of the application binary.
11651
11652 The hash table begins at offset 16 in the section, and consists of an array
11653 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11654 order of the application binary). Unused slots in the hash table are 0.
11655 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11656
11657 The parallel table begins immediately after the hash table
11658 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11659 array of 32-bit indexes (using the byte order of the application binary),
11660 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11661 table contains a 32-bit index into the pool of section numbers. For unused
11662 hash table slots, the corresponding entry in the parallel table will be 0.
11663
11664 The pool of section numbers begins immediately following the hash table
11665 (at offset 16 + 12 * M from the beginning of the section). The pool of
11666 section numbers consists of an array of 32-bit words (using the byte order
11667 of the application binary). Each item in the array is indexed starting
11668 from 0. The hash table entry provides the index of the first section
11669 number in the set. Additional section numbers in the set follow, and the
11670 set is terminated by a 0 entry (section number 0 is not used in ELF).
11671
11672 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11673 section must be the first entry in the set, and the .debug_abbrev.dwo must
11674 be the second entry. Other members of the set may follow in any order.
11675
11676 ---
11677
11678 DWP Versions 2 and 5:
11679
11680 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11681 and the entries in the index tables are now offsets into these sections.
11682 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11683 section.
11684
11685 Index Section Contents:
11686 Header
11687 Hash Table of Signatures dwp_hash_table.hash_table
11688 Parallel Table of Indices dwp_hash_table.unit_table
11689 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11690 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11691
11692 The index section header consists of:
11693
11694 V, 32 bit version number
11695 L, 32 bit number of columns in the table of section offsets
11696 N, 32 bit number of compilation units or type units in the index
11697 M, 32 bit number of slots in the hash table
11698
11699 Numbers are recorded using the byte order of the application binary.
11700
11701 The hash table has the same format as version 1.
11702 The parallel table of indices has the same format as version 1,
11703 except that the entries are origin-1 indices into the table of sections
11704 offsets and the table of section sizes.
11705
11706 The table of offsets begins immediately following the parallel table
11707 (at offset 16 + 12 * M from the beginning of the section). The table is
11708 a two-dimensional array of 32-bit words (using the byte order of the
11709 application binary), with L columns and N+1 rows, in row-major order.
11710 Each row in the array is indexed starting from 0. The first row provides
11711 a key to the remaining rows: each column in this row provides an identifier
11712 for a debug section, and the offsets in the same column of subsequent rows
11713 refer to that section. The section identifiers for Version 2 are:
11714
11715 DW_SECT_INFO 1 .debug_info.dwo
11716 DW_SECT_TYPES 2 .debug_types.dwo
11717 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11718 DW_SECT_LINE 4 .debug_line.dwo
11719 DW_SECT_LOC 5 .debug_loc.dwo
11720 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11721 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11722 DW_SECT_MACRO 8 .debug_macro.dwo
11723
11724 The section identifiers for Version 5 are:
11725
11726 DW_SECT_INFO_V5 1 .debug_info.dwo
11727 DW_SECT_RESERVED_V5 2 --
11728 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11729 DW_SECT_LINE_V5 4 .debug_line.dwo
11730 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11731 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11732 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11733 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11734
11735 The offsets provided by the CU and TU index sections are the base offsets
11736 for the contributions made by each CU or TU to the corresponding section
11737 in the package file. Each CU and TU header contains an abbrev_offset
11738 field, used to find the abbreviations table for that CU or TU within the
11739 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11740 be interpreted as relative to the base offset given in the index section.
11741 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11742 should be interpreted as relative to the base offset for .debug_line.dwo,
11743 and offsets into other debug sections obtained from DWARF attributes should
11744 also be interpreted as relative to the corresponding base offset.
11745
11746 The table of sizes begins immediately following the table of offsets.
11747 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11748 with L columns and N rows, in row-major order. Each row in the array is
11749 indexed starting from 1 (row 0 is shared by the two tables).
11750
11751 ---
11752
11753 Hash table lookup is handled the same in version 1 and 2:
11754
11755 We assume that N and M will not exceed 2^32 - 1.
11756 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11757
11758 Given a 64-bit compilation unit signature or a type signature S, an entry
11759 in the hash table is located as follows:
11760
11761 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11762 the low-order k bits all set to 1.
11763
11764 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11765
11766 3) If the hash table entry at index H matches the signature, use that
11767 entry. If the hash table entry at index H is unused (all zeroes),
11768 terminate the search: the signature is not present in the table.
11769
11770 4) Let H = (H + H') modulo M. Repeat at Step 3.
11771
11772 Because M > N and H' and M are relatively prime, the search is guaranteed
11773 to stop at an unused slot or find the match. */
11774
11775 /* Create a hash table to map DWO IDs to their CU/TU entry in
11776 .debug_{info,types}.dwo in DWP_FILE.
11777 Returns NULL if there isn't one.
11778 Note: This function processes DWP files only, not DWO files. */
11779
11780 static struct dwp_hash_table *
11781 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11782 struct dwp_file *dwp_file, int is_debug_types)
11783 {
11784 struct objfile *objfile = per_objfile->objfile;
11785 bfd *dbfd = dwp_file->dbfd.get ();
11786 const gdb_byte *index_ptr, *index_end;
11787 struct dwarf2_section_info *index;
11788 uint32_t version, nr_columns, nr_units, nr_slots;
11789 struct dwp_hash_table *htab;
11790
11791 if (is_debug_types)
11792 index = &dwp_file->sections.tu_index;
11793 else
11794 index = &dwp_file->sections.cu_index;
11795
11796 if (index->empty ())
11797 return NULL;
11798 index->read (objfile);
11799
11800 index_ptr = index->buffer;
11801 index_end = index_ptr + index->size;
11802
11803 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11804 For now it's safe to just read 4 bytes (particularly as it's difficult to
11805 tell if you're dealing with Version 5 before you've read the version). */
11806 version = read_4_bytes (dbfd, index_ptr);
11807 index_ptr += 4;
11808 if (version == 2 || version == 5)
11809 nr_columns = read_4_bytes (dbfd, index_ptr);
11810 else
11811 nr_columns = 0;
11812 index_ptr += 4;
11813 nr_units = read_4_bytes (dbfd, index_ptr);
11814 index_ptr += 4;
11815 nr_slots = read_4_bytes (dbfd, index_ptr);
11816 index_ptr += 4;
11817
11818 if (version != 1 && version != 2 && version != 5)
11819 {
11820 error (_("Dwarf Error: unsupported DWP file version (%s)"
11821 " [in module %s]"),
11822 pulongest (version), dwp_file->name);
11823 }
11824 if (nr_slots != (nr_slots & -nr_slots))
11825 {
11826 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11827 " is not power of 2 [in module %s]"),
11828 pulongest (nr_slots), dwp_file->name);
11829 }
11830
11831 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11832 htab->version = version;
11833 htab->nr_columns = nr_columns;
11834 htab->nr_units = nr_units;
11835 htab->nr_slots = nr_slots;
11836 htab->hash_table = index_ptr;
11837 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11838
11839 /* Exit early if the table is empty. */
11840 if (nr_slots == 0 || nr_units == 0
11841 || (version == 2 && nr_columns == 0)
11842 || (version == 5 && nr_columns == 0))
11843 {
11844 /* All must be zero. */
11845 if (nr_slots != 0 || nr_units != 0
11846 || (version == 2 && nr_columns != 0)
11847 || (version == 5 && nr_columns != 0))
11848 {
11849 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11850 " all zero [in modules %s]"),
11851 dwp_file->name);
11852 }
11853 return htab;
11854 }
11855
11856 if (version == 1)
11857 {
11858 htab->section_pool.v1.indices =
11859 htab->unit_table + sizeof (uint32_t) * nr_slots;
11860 /* It's harder to decide whether the section is too small in v1.
11861 V1 is deprecated anyway so we punt. */
11862 }
11863 else if (version == 2)
11864 {
11865 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11866 int *ids = htab->section_pool.v2.section_ids;
11867 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11868 /* Reverse map for error checking. */
11869 int ids_seen[DW_SECT_MAX + 1];
11870 int i;
11871
11872 if (nr_columns < 2)
11873 {
11874 error (_("Dwarf Error: bad DWP hash table, too few columns"
11875 " in section table [in module %s]"),
11876 dwp_file->name);
11877 }
11878 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11879 {
11880 error (_("Dwarf Error: bad DWP hash table, too many columns"
11881 " in section table [in module %s]"),
11882 dwp_file->name);
11883 }
11884 memset (ids, 255, sizeof_ids);
11885 memset (ids_seen, 255, sizeof (ids_seen));
11886 for (i = 0; i < nr_columns; ++i)
11887 {
11888 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11889
11890 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11891 {
11892 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11893 " in section table [in module %s]"),
11894 id, dwp_file->name);
11895 }
11896 if (ids_seen[id] != -1)
11897 {
11898 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11899 " id %d in section table [in module %s]"),
11900 id, dwp_file->name);
11901 }
11902 ids_seen[id] = i;
11903 ids[i] = id;
11904 }
11905 /* Must have exactly one info or types section. */
11906 if (((ids_seen[DW_SECT_INFO] != -1)
11907 + (ids_seen[DW_SECT_TYPES] != -1))
11908 != 1)
11909 {
11910 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11911 " DWO info/types section [in module %s]"),
11912 dwp_file->name);
11913 }
11914 /* Must have an abbrev section. */
11915 if (ids_seen[DW_SECT_ABBREV] == -1)
11916 {
11917 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11918 " section [in module %s]"),
11919 dwp_file->name);
11920 }
11921 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11922 htab->section_pool.v2.sizes =
11923 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11924 * nr_units * nr_columns);
11925 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11926 * nr_units * nr_columns))
11927 > index_end)
11928 {
11929 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11930 " [in module %s]"),
11931 dwp_file->name);
11932 }
11933 }
11934 else /* version == 5 */
11935 {
11936 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11937 int *ids = htab->section_pool.v5.section_ids;
11938 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11939 /* Reverse map for error checking. */
11940 int ids_seen[DW_SECT_MAX_V5 + 1];
11941
11942 if (nr_columns < 2)
11943 {
11944 error (_("Dwarf Error: bad DWP hash table, too few columns"
11945 " in section table [in module %s]"),
11946 dwp_file->name);
11947 }
11948 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11949 {
11950 error (_("Dwarf Error: bad DWP hash table, too many columns"
11951 " in section table [in module %s]"),
11952 dwp_file->name);
11953 }
11954 memset (ids, 255, sizeof_ids);
11955 memset (ids_seen, 255, sizeof (ids_seen));
11956 for (int i = 0; i < nr_columns; ++i)
11957 {
11958 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11959
11960 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11961 {
11962 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11963 " in section table [in module %s]"),
11964 id, dwp_file->name);
11965 }
11966 if (ids_seen[id] != -1)
11967 {
11968 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11969 " id %d in section table [in module %s]"),
11970 id, dwp_file->name);
11971 }
11972 ids_seen[id] = i;
11973 ids[i] = id;
11974 }
11975 /* Must have seen an info section. */
11976 if (ids_seen[DW_SECT_INFO_V5] == -1)
11977 {
11978 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11979 " DWO info/types section [in module %s]"),
11980 dwp_file->name);
11981 }
11982 /* Must have an abbrev section. */
11983 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11984 {
11985 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11986 " section [in module %s]"),
11987 dwp_file->name);
11988 }
11989 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11990 htab->section_pool.v5.sizes
11991 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11992 * nr_units * nr_columns);
11993 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11994 * nr_units * nr_columns))
11995 > index_end)
11996 {
11997 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11998 " [in module %s]"),
11999 dwp_file->name);
12000 }
12001 }
12002
12003 return htab;
12004 }
12005
12006 /* Update SECTIONS with the data from SECTP.
12007
12008 This function is like the other "locate" section routines, but in
12009 this context the sections to read comes from the DWP V1 hash table,
12010 not the full ELF section table.
12011
12012 The result is non-zero for success, or zero if an error was found. */
12013
12014 static int
12015 locate_v1_virtual_dwo_sections (asection *sectp,
12016 struct virtual_v1_dwo_sections *sections)
12017 {
12018 const struct dwop_section_names *names = &dwop_section_names;
12019
12020 if (names->abbrev_dwo.matches (sectp->name))
12021 {
12022 /* There can be only one. */
12023 if (sections->abbrev.s.section != NULL)
12024 return 0;
12025 sections->abbrev.s.section = sectp;
12026 sections->abbrev.size = bfd_section_size (sectp);
12027 }
12028 else if (names->info_dwo.matches (sectp->name)
12029 || names->types_dwo.matches (sectp->name))
12030 {
12031 /* There can be only one. */
12032 if (sections->info_or_types.s.section != NULL)
12033 return 0;
12034 sections->info_or_types.s.section = sectp;
12035 sections->info_or_types.size = bfd_section_size (sectp);
12036 }
12037 else if (names->line_dwo.matches (sectp->name))
12038 {
12039 /* There can be only one. */
12040 if (sections->line.s.section != NULL)
12041 return 0;
12042 sections->line.s.section = sectp;
12043 sections->line.size = bfd_section_size (sectp);
12044 }
12045 else if (names->loc_dwo.matches (sectp->name))
12046 {
12047 /* There can be only one. */
12048 if (sections->loc.s.section != NULL)
12049 return 0;
12050 sections->loc.s.section = sectp;
12051 sections->loc.size = bfd_section_size (sectp);
12052 }
12053 else if (names->macinfo_dwo.matches (sectp->name))
12054 {
12055 /* There can be only one. */
12056 if (sections->macinfo.s.section != NULL)
12057 return 0;
12058 sections->macinfo.s.section = sectp;
12059 sections->macinfo.size = bfd_section_size (sectp);
12060 }
12061 else if (names->macro_dwo.matches (sectp->name))
12062 {
12063 /* There can be only one. */
12064 if (sections->macro.s.section != NULL)
12065 return 0;
12066 sections->macro.s.section = sectp;
12067 sections->macro.size = bfd_section_size (sectp);
12068 }
12069 else if (names->str_offsets_dwo.matches (sectp->name))
12070 {
12071 /* There can be only one. */
12072 if (sections->str_offsets.s.section != NULL)
12073 return 0;
12074 sections->str_offsets.s.section = sectp;
12075 sections->str_offsets.size = bfd_section_size (sectp);
12076 }
12077 else
12078 {
12079 /* No other kind of section is valid. */
12080 return 0;
12081 }
12082
12083 return 1;
12084 }
12085
12086 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12087 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12088 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12089 This is for DWP version 1 files. */
12090
12091 static struct dwo_unit *
12092 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12093 struct dwp_file *dwp_file,
12094 uint32_t unit_index,
12095 const char *comp_dir,
12096 ULONGEST signature, int is_debug_types)
12097 {
12098 const struct dwp_hash_table *dwp_htab =
12099 is_debug_types ? dwp_file->tus : dwp_file->cus;
12100 bfd *dbfd = dwp_file->dbfd.get ();
12101 const char *kind = is_debug_types ? "TU" : "CU";
12102 struct dwo_file *dwo_file;
12103 struct dwo_unit *dwo_unit;
12104 struct virtual_v1_dwo_sections sections;
12105 void **dwo_file_slot;
12106 int i;
12107
12108 gdb_assert (dwp_file->version == 1);
12109
12110 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12111 kind, pulongest (unit_index), hex_string (signature),
12112 dwp_file->name);
12113
12114 /* Fetch the sections of this DWO unit.
12115 Put a limit on the number of sections we look for so that bad data
12116 doesn't cause us to loop forever. */
12117
12118 #define MAX_NR_V1_DWO_SECTIONS \
12119 (1 /* .debug_info or .debug_types */ \
12120 + 1 /* .debug_abbrev */ \
12121 + 1 /* .debug_line */ \
12122 + 1 /* .debug_loc */ \
12123 + 1 /* .debug_str_offsets */ \
12124 + 1 /* .debug_macro or .debug_macinfo */ \
12125 + 1 /* trailing zero */)
12126
12127 memset (&sections, 0, sizeof (sections));
12128
12129 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12130 {
12131 asection *sectp;
12132 uint32_t section_nr =
12133 read_4_bytes (dbfd,
12134 dwp_htab->section_pool.v1.indices
12135 + (unit_index + i) * sizeof (uint32_t));
12136
12137 if (section_nr == 0)
12138 break;
12139 if (section_nr >= dwp_file->num_sections)
12140 {
12141 error (_("Dwarf Error: bad DWP hash table, section number too large"
12142 " [in module %s]"),
12143 dwp_file->name);
12144 }
12145
12146 sectp = dwp_file->elf_sections[section_nr];
12147 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12148 {
12149 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12150 " [in module %s]"),
12151 dwp_file->name);
12152 }
12153 }
12154
12155 if (i < 2
12156 || sections.info_or_types.empty ()
12157 || sections.abbrev.empty ())
12158 {
12159 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12160 " [in module %s]"),
12161 dwp_file->name);
12162 }
12163 if (i == MAX_NR_V1_DWO_SECTIONS)
12164 {
12165 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12166 " [in module %s]"),
12167 dwp_file->name);
12168 }
12169
12170 /* It's easier for the rest of the code if we fake a struct dwo_file and
12171 have dwo_unit "live" in that. At least for now.
12172
12173 The DWP file can be made up of a random collection of CUs and TUs.
12174 However, for each CU + set of TUs that came from the same original DWO
12175 file, we can combine them back into a virtual DWO file to save space
12176 (fewer struct dwo_file objects to allocate). Remember that for really
12177 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12178
12179 std::string virtual_dwo_name =
12180 string_printf ("virtual-dwo/%d-%d-%d-%d",
12181 sections.abbrev.get_id (),
12182 sections.line.get_id (),
12183 sections.loc.get_id (),
12184 sections.str_offsets.get_id ());
12185 /* Can we use an existing virtual DWO file? */
12186 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12187 comp_dir);
12188 /* Create one if necessary. */
12189 if (*dwo_file_slot == NULL)
12190 {
12191 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12192 virtual_dwo_name.c_str ());
12193
12194 dwo_file = new struct dwo_file;
12195 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12196 dwo_file->comp_dir = comp_dir;
12197 dwo_file->sections.abbrev = sections.abbrev;
12198 dwo_file->sections.line = sections.line;
12199 dwo_file->sections.loc = sections.loc;
12200 dwo_file->sections.macinfo = sections.macinfo;
12201 dwo_file->sections.macro = sections.macro;
12202 dwo_file->sections.str_offsets = sections.str_offsets;
12203 /* The "str" section is global to the entire DWP file. */
12204 dwo_file->sections.str = dwp_file->sections.str;
12205 /* The info or types section is assigned below to dwo_unit,
12206 there's no need to record it in dwo_file.
12207 Also, we can't simply record type sections in dwo_file because
12208 we record a pointer into the vector in dwo_unit. As we collect more
12209 types we'll grow the vector and eventually have to reallocate space
12210 for it, invalidating all copies of pointers into the previous
12211 contents. */
12212 *dwo_file_slot = dwo_file;
12213 }
12214 else
12215 {
12216 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12217 virtual_dwo_name.c_str ());
12218
12219 dwo_file = (struct dwo_file *) *dwo_file_slot;
12220 }
12221
12222 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12223 dwo_unit->dwo_file = dwo_file;
12224 dwo_unit->signature = signature;
12225 dwo_unit->section =
12226 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12227 *dwo_unit->section = sections.info_or_types;
12228 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12229
12230 return dwo_unit;
12231 }
12232
12233 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12234 simplify them. Given a pointer to the containing section SECTION, and
12235 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12236 virtual section of just that piece. */
12237
12238 static struct dwarf2_section_info
12239 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12240 struct dwarf2_section_info *section,
12241 bfd_size_type offset, bfd_size_type size)
12242 {
12243 struct dwarf2_section_info result;
12244 asection *sectp;
12245
12246 gdb_assert (section != NULL);
12247 gdb_assert (!section->is_virtual);
12248
12249 memset (&result, 0, sizeof (result));
12250 result.s.containing_section = section;
12251 result.is_virtual = true;
12252
12253 if (size == 0)
12254 return result;
12255
12256 sectp = section->get_bfd_section ();
12257
12258 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12259 bounds of the real section. This is a pretty-rare event, so just
12260 flag an error (easier) instead of a warning and trying to cope. */
12261 if (sectp == NULL
12262 || offset + size > bfd_section_size (sectp))
12263 {
12264 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12265 " in section %s [in module %s]"),
12266 sectp ? bfd_section_name (sectp) : "<unknown>",
12267 objfile_name (per_objfile->objfile));
12268 }
12269
12270 result.virtual_offset = offset;
12271 result.size = size;
12272 return result;
12273 }
12274
12275 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12276 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12277 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12278 This is for DWP version 2 files. */
12279
12280 static struct dwo_unit *
12281 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12282 struct dwp_file *dwp_file,
12283 uint32_t unit_index,
12284 const char *comp_dir,
12285 ULONGEST signature, int is_debug_types)
12286 {
12287 const struct dwp_hash_table *dwp_htab =
12288 is_debug_types ? dwp_file->tus : dwp_file->cus;
12289 bfd *dbfd = dwp_file->dbfd.get ();
12290 const char *kind = is_debug_types ? "TU" : "CU";
12291 struct dwo_file *dwo_file;
12292 struct dwo_unit *dwo_unit;
12293 struct virtual_v2_or_v5_dwo_sections sections;
12294 void **dwo_file_slot;
12295 int i;
12296
12297 gdb_assert (dwp_file->version == 2);
12298
12299 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12300 kind, pulongest (unit_index), hex_string (signature),
12301 dwp_file->name);
12302
12303 /* Fetch the section offsets of this DWO unit. */
12304
12305 memset (&sections, 0, sizeof (sections));
12306
12307 for (i = 0; i < dwp_htab->nr_columns; ++i)
12308 {
12309 uint32_t offset = read_4_bytes (dbfd,
12310 dwp_htab->section_pool.v2.offsets
12311 + (((unit_index - 1) * dwp_htab->nr_columns
12312 + i)
12313 * sizeof (uint32_t)));
12314 uint32_t size = read_4_bytes (dbfd,
12315 dwp_htab->section_pool.v2.sizes
12316 + (((unit_index - 1) * dwp_htab->nr_columns
12317 + i)
12318 * sizeof (uint32_t)));
12319
12320 switch (dwp_htab->section_pool.v2.section_ids[i])
12321 {
12322 case DW_SECT_INFO:
12323 case DW_SECT_TYPES:
12324 sections.info_or_types_offset = offset;
12325 sections.info_or_types_size = size;
12326 break;
12327 case DW_SECT_ABBREV:
12328 sections.abbrev_offset = offset;
12329 sections.abbrev_size = size;
12330 break;
12331 case DW_SECT_LINE:
12332 sections.line_offset = offset;
12333 sections.line_size = size;
12334 break;
12335 case DW_SECT_LOC:
12336 sections.loc_offset = offset;
12337 sections.loc_size = size;
12338 break;
12339 case DW_SECT_STR_OFFSETS:
12340 sections.str_offsets_offset = offset;
12341 sections.str_offsets_size = size;
12342 break;
12343 case DW_SECT_MACINFO:
12344 sections.macinfo_offset = offset;
12345 sections.macinfo_size = size;
12346 break;
12347 case DW_SECT_MACRO:
12348 sections.macro_offset = offset;
12349 sections.macro_size = size;
12350 break;
12351 }
12352 }
12353
12354 /* It's easier for the rest of the code if we fake a struct dwo_file and
12355 have dwo_unit "live" in that. At least for now.
12356
12357 The DWP file can be made up of a random collection of CUs and TUs.
12358 However, for each CU + set of TUs that came from the same original DWO
12359 file, we can combine them back into a virtual DWO file to save space
12360 (fewer struct dwo_file objects to allocate). Remember that for really
12361 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12362
12363 std::string virtual_dwo_name =
12364 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12365 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12366 (long) (sections.line_size ? sections.line_offset : 0),
12367 (long) (sections.loc_size ? sections.loc_offset : 0),
12368 (long) (sections.str_offsets_size
12369 ? sections.str_offsets_offset : 0));
12370 /* Can we use an existing virtual DWO file? */
12371 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12372 comp_dir);
12373 /* Create one if necessary. */
12374 if (*dwo_file_slot == NULL)
12375 {
12376 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12377 virtual_dwo_name.c_str ());
12378
12379 dwo_file = new struct dwo_file;
12380 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12381 dwo_file->comp_dir = comp_dir;
12382 dwo_file->sections.abbrev =
12383 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12384 sections.abbrev_offset,
12385 sections.abbrev_size);
12386 dwo_file->sections.line =
12387 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12388 sections.line_offset,
12389 sections.line_size);
12390 dwo_file->sections.loc =
12391 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12392 sections.loc_offset, sections.loc_size);
12393 dwo_file->sections.macinfo =
12394 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12395 sections.macinfo_offset,
12396 sections.macinfo_size);
12397 dwo_file->sections.macro =
12398 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12399 sections.macro_offset,
12400 sections.macro_size);
12401 dwo_file->sections.str_offsets =
12402 create_dwp_v2_or_v5_section (per_objfile,
12403 &dwp_file->sections.str_offsets,
12404 sections.str_offsets_offset,
12405 sections.str_offsets_size);
12406 /* The "str" section is global to the entire DWP file. */
12407 dwo_file->sections.str = dwp_file->sections.str;
12408 /* The info or types section is assigned below to dwo_unit,
12409 there's no need to record it in dwo_file.
12410 Also, we can't simply record type sections in dwo_file because
12411 we record a pointer into the vector in dwo_unit. As we collect more
12412 types we'll grow the vector and eventually have to reallocate space
12413 for it, invalidating all copies of pointers into the previous
12414 contents. */
12415 *dwo_file_slot = dwo_file;
12416 }
12417 else
12418 {
12419 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12420 virtual_dwo_name.c_str ());
12421
12422 dwo_file = (struct dwo_file *) *dwo_file_slot;
12423 }
12424
12425 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12426 dwo_unit->dwo_file = dwo_file;
12427 dwo_unit->signature = signature;
12428 dwo_unit->section =
12429 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12430 *dwo_unit->section = create_dwp_v2_or_v5_section
12431 (per_objfile,
12432 is_debug_types
12433 ? &dwp_file->sections.types
12434 : &dwp_file->sections.info,
12435 sections.info_or_types_offset,
12436 sections.info_or_types_size);
12437 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12438
12439 return dwo_unit;
12440 }
12441
12442 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12443 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12444 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12445 This is for DWP version 5 files. */
12446
12447 static struct dwo_unit *
12448 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12449 struct dwp_file *dwp_file,
12450 uint32_t unit_index,
12451 const char *comp_dir,
12452 ULONGEST signature, int is_debug_types)
12453 {
12454 const struct dwp_hash_table *dwp_htab
12455 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12456 bfd *dbfd = dwp_file->dbfd.get ();
12457 const char *kind = is_debug_types ? "TU" : "CU";
12458 struct dwo_file *dwo_file;
12459 struct dwo_unit *dwo_unit;
12460 struct virtual_v2_or_v5_dwo_sections sections {};
12461 void **dwo_file_slot;
12462
12463 gdb_assert (dwp_file->version == 5);
12464
12465 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12466 kind, pulongest (unit_index), hex_string (signature),
12467 dwp_file->name);
12468
12469 /* Fetch the section offsets of this DWO unit. */
12470
12471 /* memset (&sections, 0, sizeof (sections)); */
12472
12473 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12474 {
12475 uint32_t offset = read_4_bytes (dbfd,
12476 dwp_htab->section_pool.v5.offsets
12477 + (((unit_index - 1)
12478 * dwp_htab->nr_columns
12479 + i)
12480 * sizeof (uint32_t)));
12481 uint32_t size = read_4_bytes (dbfd,
12482 dwp_htab->section_pool.v5.sizes
12483 + (((unit_index - 1) * dwp_htab->nr_columns
12484 + i)
12485 * sizeof (uint32_t)));
12486
12487 switch (dwp_htab->section_pool.v5.section_ids[i])
12488 {
12489 case DW_SECT_ABBREV_V5:
12490 sections.abbrev_offset = offset;
12491 sections.abbrev_size = size;
12492 break;
12493 case DW_SECT_INFO_V5:
12494 sections.info_or_types_offset = offset;
12495 sections.info_or_types_size = size;
12496 break;
12497 case DW_SECT_LINE_V5:
12498 sections.line_offset = offset;
12499 sections.line_size = size;
12500 break;
12501 case DW_SECT_LOCLISTS_V5:
12502 sections.loclists_offset = offset;
12503 sections.loclists_size = size;
12504 break;
12505 case DW_SECT_MACRO_V5:
12506 sections.macro_offset = offset;
12507 sections.macro_size = size;
12508 break;
12509 case DW_SECT_RNGLISTS_V5:
12510 sections.rnglists_offset = offset;
12511 sections.rnglists_size = size;
12512 break;
12513 case DW_SECT_STR_OFFSETS_V5:
12514 sections.str_offsets_offset = offset;
12515 sections.str_offsets_size = size;
12516 break;
12517 case DW_SECT_RESERVED_V5:
12518 default:
12519 break;
12520 }
12521 }
12522
12523 /* It's easier for the rest of the code if we fake a struct dwo_file and
12524 have dwo_unit "live" in that. At least for now.
12525
12526 The DWP file can be made up of a random collection of CUs and TUs.
12527 However, for each CU + set of TUs that came from the same original DWO
12528 file, we can combine them back into a virtual DWO file to save space
12529 (fewer struct dwo_file objects to allocate). Remember that for really
12530 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12531
12532 std::string virtual_dwo_name =
12533 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12534 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12535 (long) (sections.line_size ? sections.line_offset : 0),
12536 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12537 (long) (sections.str_offsets_size
12538 ? sections.str_offsets_offset : 0),
12539 (long) (sections.macro_size ? sections.macro_offset : 0),
12540 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12541 /* Can we use an existing virtual DWO file? */
12542 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12543 virtual_dwo_name.c_str (),
12544 comp_dir);
12545 /* Create one if necessary. */
12546 if (*dwo_file_slot == NULL)
12547 {
12548 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12549 virtual_dwo_name.c_str ());
12550
12551 dwo_file = new struct dwo_file;
12552 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12553 dwo_file->comp_dir = comp_dir;
12554 dwo_file->sections.abbrev =
12555 create_dwp_v2_or_v5_section (per_objfile,
12556 &dwp_file->sections.abbrev,
12557 sections.abbrev_offset,
12558 sections.abbrev_size);
12559 dwo_file->sections.line =
12560 create_dwp_v2_or_v5_section (per_objfile,
12561 &dwp_file->sections.line,
12562 sections.line_offset, sections.line_size);
12563 dwo_file->sections.macro =
12564 create_dwp_v2_or_v5_section (per_objfile,
12565 &dwp_file->sections.macro,
12566 sections.macro_offset,
12567 sections.macro_size);
12568 dwo_file->sections.loclists =
12569 create_dwp_v2_or_v5_section (per_objfile,
12570 &dwp_file->sections.loclists,
12571 sections.loclists_offset,
12572 sections.loclists_size);
12573 dwo_file->sections.rnglists =
12574 create_dwp_v2_or_v5_section (per_objfile,
12575 &dwp_file->sections.rnglists,
12576 sections.rnglists_offset,
12577 sections.rnglists_size);
12578 dwo_file->sections.str_offsets =
12579 create_dwp_v2_or_v5_section (per_objfile,
12580 &dwp_file->sections.str_offsets,
12581 sections.str_offsets_offset,
12582 sections.str_offsets_size);
12583 /* The "str" section is global to the entire DWP file. */
12584 dwo_file->sections.str = dwp_file->sections.str;
12585 /* The info or types section is assigned below to dwo_unit,
12586 there's no need to record it in dwo_file.
12587 Also, we can't simply record type sections in dwo_file because
12588 we record a pointer into the vector in dwo_unit. As we collect more
12589 types we'll grow the vector and eventually have to reallocate space
12590 for it, invalidating all copies of pointers into the previous
12591 contents. */
12592 *dwo_file_slot = dwo_file;
12593 }
12594 else
12595 {
12596 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12597 virtual_dwo_name.c_str ());
12598
12599 dwo_file = (struct dwo_file *) *dwo_file_slot;
12600 }
12601
12602 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12603 dwo_unit->dwo_file = dwo_file;
12604 dwo_unit->signature = signature;
12605 dwo_unit->section
12606 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12607 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12608 &dwp_file->sections.info,
12609 sections.info_or_types_offset,
12610 sections.info_or_types_size);
12611 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12612
12613 return dwo_unit;
12614 }
12615
12616 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12617 Returns NULL if the signature isn't found. */
12618
12619 static struct dwo_unit *
12620 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12621 struct dwp_file *dwp_file, const char *comp_dir,
12622 ULONGEST signature, int is_debug_types)
12623 {
12624 const struct dwp_hash_table *dwp_htab =
12625 is_debug_types ? dwp_file->tus : dwp_file->cus;
12626 bfd *dbfd = dwp_file->dbfd.get ();
12627 uint32_t mask = dwp_htab->nr_slots - 1;
12628 uint32_t hash = signature & mask;
12629 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12630 unsigned int i;
12631 void **slot;
12632 struct dwo_unit find_dwo_cu;
12633
12634 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12635 find_dwo_cu.signature = signature;
12636 slot = htab_find_slot (is_debug_types
12637 ? dwp_file->loaded_tus.get ()
12638 : dwp_file->loaded_cus.get (),
12639 &find_dwo_cu, INSERT);
12640
12641 if (*slot != NULL)
12642 return (struct dwo_unit *) *slot;
12643
12644 /* Use a for loop so that we don't loop forever on bad debug info. */
12645 for (i = 0; i < dwp_htab->nr_slots; ++i)
12646 {
12647 ULONGEST signature_in_table;
12648
12649 signature_in_table =
12650 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12651 if (signature_in_table == signature)
12652 {
12653 uint32_t unit_index =
12654 read_4_bytes (dbfd,
12655 dwp_htab->unit_table + hash * sizeof (uint32_t));
12656
12657 if (dwp_file->version == 1)
12658 {
12659 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12660 unit_index, comp_dir,
12661 signature, is_debug_types);
12662 }
12663 else if (dwp_file->version == 2)
12664 {
12665 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12666 unit_index, comp_dir,
12667 signature, is_debug_types);
12668 }
12669 else /* version == 5 */
12670 {
12671 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12672 unit_index, comp_dir,
12673 signature, is_debug_types);
12674 }
12675 return (struct dwo_unit *) *slot;
12676 }
12677 if (signature_in_table == 0)
12678 return NULL;
12679 hash = (hash + hash2) & mask;
12680 }
12681
12682 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12683 " [in module %s]"),
12684 dwp_file->name);
12685 }
12686
12687 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12688 Open the file specified by FILE_NAME and hand it off to BFD for
12689 preliminary analysis. Return a newly initialized bfd *, which
12690 includes a canonicalized copy of FILE_NAME.
12691 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12692 SEARCH_CWD is true if the current directory is to be searched.
12693 It will be searched before debug-file-directory.
12694 If successful, the file is added to the bfd include table of the
12695 objfile's bfd (see gdb_bfd_record_inclusion).
12696 If unable to find/open the file, return NULL.
12697 NOTE: This function is derived from symfile_bfd_open. */
12698
12699 static gdb_bfd_ref_ptr
12700 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12701 const char *file_name, int is_dwp, int search_cwd)
12702 {
12703 int desc;
12704 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12705 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12706 to debug_file_directory. */
12707 const char *search_path;
12708 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12709
12710 gdb::unique_xmalloc_ptr<char> search_path_holder;
12711 if (search_cwd)
12712 {
12713 if (*debug_file_directory != '\0')
12714 {
12715 search_path_holder.reset (concat (".", dirname_separator_string,
12716 debug_file_directory,
12717 (char *) NULL));
12718 search_path = search_path_holder.get ();
12719 }
12720 else
12721 search_path = ".";
12722 }
12723 else
12724 search_path = debug_file_directory;
12725
12726 openp_flags flags = OPF_RETURN_REALPATH;
12727 if (is_dwp)
12728 flags |= OPF_SEARCH_IN_PATH;
12729
12730 gdb::unique_xmalloc_ptr<char> absolute_name;
12731 desc = openp (search_path, flags, file_name,
12732 O_RDONLY | O_BINARY, &absolute_name);
12733 if (desc < 0)
12734 return NULL;
12735
12736 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12737 gnutarget, desc));
12738 if (sym_bfd == NULL)
12739 return NULL;
12740 bfd_set_cacheable (sym_bfd.get (), 1);
12741
12742 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12743 return NULL;
12744
12745 /* Success. Record the bfd as having been included by the objfile's bfd.
12746 This is important because things like demangled_names_hash lives in the
12747 objfile's per_bfd space and may have references to things like symbol
12748 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12749 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12750
12751 return sym_bfd;
12752 }
12753
12754 /* Try to open DWO file FILE_NAME.
12755 COMP_DIR is the DW_AT_comp_dir attribute.
12756 The result is the bfd handle of the file.
12757 If there is a problem finding or opening the file, return NULL.
12758 Upon success, the canonicalized path of the file is stored in the bfd,
12759 same as symfile_bfd_open. */
12760
12761 static gdb_bfd_ref_ptr
12762 open_dwo_file (dwarf2_per_objfile *per_objfile,
12763 const char *file_name, const char *comp_dir)
12764 {
12765 if (IS_ABSOLUTE_PATH (file_name))
12766 return try_open_dwop_file (per_objfile, file_name,
12767 0 /*is_dwp*/, 0 /*search_cwd*/);
12768
12769 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12770
12771 if (comp_dir != NULL)
12772 {
12773 gdb::unique_xmalloc_ptr<char> path_to_try
12774 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12775
12776 /* NOTE: If comp_dir is a relative path, this will also try the
12777 search path, which seems useful. */
12778 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12779 0 /*is_dwp*/,
12780 1 /*search_cwd*/));
12781 if (abfd != NULL)
12782 return abfd;
12783 }
12784
12785 /* That didn't work, try debug-file-directory, which, despite its name,
12786 is a list of paths. */
12787
12788 if (*debug_file_directory == '\0')
12789 return NULL;
12790
12791 return try_open_dwop_file (per_objfile, file_name,
12792 0 /*is_dwp*/, 1 /*search_cwd*/);
12793 }
12794
12795 /* This function is mapped across the sections and remembers the offset and
12796 size of each of the DWO debugging sections we are interested in. */
12797
12798 static void
12799 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12800 dwo_sections *dwo_sections)
12801 {
12802 const struct dwop_section_names *names = &dwop_section_names;
12803
12804 if (names->abbrev_dwo.matches (sectp->name))
12805 {
12806 dwo_sections->abbrev.s.section = sectp;
12807 dwo_sections->abbrev.size = bfd_section_size (sectp);
12808 }
12809 else if (names->info_dwo.matches (sectp->name))
12810 {
12811 dwo_sections->info.s.section = sectp;
12812 dwo_sections->info.size = bfd_section_size (sectp);
12813 }
12814 else if (names->line_dwo.matches (sectp->name))
12815 {
12816 dwo_sections->line.s.section = sectp;
12817 dwo_sections->line.size = bfd_section_size (sectp);
12818 }
12819 else if (names->loc_dwo.matches (sectp->name))
12820 {
12821 dwo_sections->loc.s.section = sectp;
12822 dwo_sections->loc.size = bfd_section_size (sectp);
12823 }
12824 else if (names->loclists_dwo.matches (sectp->name))
12825 {
12826 dwo_sections->loclists.s.section = sectp;
12827 dwo_sections->loclists.size = bfd_section_size (sectp);
12828 }
12829 else if (names->macinfo_dwo.matches (sectp->name))
12830 {
12831 dwo_sections->macinfo.s.section = sectp;
12832 dwo_sections->macinfo.size = bfd_section_size (sectp);
12833 }
12834 else if (names->macro_dwo.matches (sectp->name))
12835 {
12836 dwo_sections->macro.s.section = sectp;
12837 dwo_sections->macro.size = bfd_section_size (sectp);
12838 }
12839 else if (names->rnglists_dwo.matches (sectp->name))
12840 {
12841 dwo_sections->rnglists.s.section = sectp;
12842 dwo_sections->rnglists.size = bfd_section_size (sectp);
12843 }
12844 else if (names->str_dwo.matches (sectp->name))
12845 {
12846 dwo_sections->str.s.section = sectp;
12847 dwo_sections->str.size = bfd_section_size (sectp);
12848 }
12849 else if (names->str_offsets_dwo.matches (sectp->name))
12850 {
12851 dwo_sections->str_offsets.s.section = sectp;
12852 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12853 }
12854 else if (names->types_dwo.matches (sectp->name))
12855 {
12856 struct dwarf2_section_info type_section;
12857
12858 memset (&type_section, 0, sizeof (type_section));
12859 type_section.s.section = sectp;
12860 type_section.size = bfd_section_size (sectp);
12861 dwo_sections->types.push_back (type_section);
12862 }
12863 }
12864
12865 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12866 by PER_CU. This is for the non-DWP case.
12867 The result is NULL if DWO_NAME can't be found. */
12868
12869 static struct dwo_file *
12870 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12871 const char *comp_dir)
12872 {
12873 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12874
12875 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12876 if (dbfd == NULL)
12877 {
12878 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12879
12880 return NULL;
12881 }
12882
12883 dwo_file_up dwo_file (new struct dwo_file);
12884 dwo_file->dwo_name = dwo_name;
12885 dwo_file->comp_dir = comp_dir;
12886 dwo_file->dbfd = std::move (dbfd);
12887
12888 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12889 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12890 &dwo_file->sections);
12891
12892 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12893 dwo_file->cus);
12894
12895 if (cu->per_cu->dwarf_version < 5)
12896 {
12897 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12898 dwo_file->sections.types, dwo_file->tus);
12899 }
12900 else
12901 {
12902 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12903 &dwo_file->sections.info, dwo_file->tus,
12904 rcuh_kind::COMPILE);
12905 }
12906
12907 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12908
12909 return dwo_file.release ();
12910 }
12911
12912 /* This function is mapped across the sections and remembers the offset and
12913 size of each of the DWP debugging sections common to version 1 and 2 that
12914 we are interested in. */
12915
12916 static void
12917 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12918 dwp_file *dwp_file)
12919 {
12920 const struct dwop_section_names *names = &dwop_section_names;
12921 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12922
12923 /* Record the ELF section number for later lookup: this is what the
12924 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12925 gdb_assert (elf_section_nr < dwp_file->num_sections);
12926 dwp_file->elf_sections[elf_section_nr] = sectp;
12927
12928 /* Look for specific sections that we need. */
12929 if (names->str_dwo.matches (sectp->name))
12930 {
12931 dwp_file->sections.str.s.section = sectp;
12932 dwp_file->sections.str.size = bfd_section_size (sectp);
12933 }
12934 else if (names->cu_index.matches (sectp->name))
12935 {
12936 dwp_file->sections.cu_index.s.section = sectp;
12937 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12938 }
12939 else if (names->tu_index.matches (sectp->name))
12940 {
12941 dwp_file->sections.tu_index.s.section = sectp;
12942 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12943 }
12944 }
12945
12946 /* This function is mapped across the sections and remembers the offset and
12947 size of each of the DWP version 2 debugging sections that we are interested
12948 in. This is split into a separate function because we don't know if we
12949 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12950
12951 static void
12952 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12953 {
12954 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12955 const struct dwop_section_names *names = &dwop_section_names;
12956 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12957
12958 /* Record the ELF section number for later lookup: this is what the
12959 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12960 gdb_assert (elf_section_nr < dwp_file->num_sections);
12961 dwp_file->elf_sections[elf_section_nr] = sectp;
12962
12963 /* Look for specific sections that we need. */
12964 if (names->abbrev_dwo.matches (sectp->name))
12965 {
12966 dwp_file->sections.abbrev.s.section = sectp;
12967 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12968 }
12969 else if (names->info_dwo.matches (sectp->name))
12970 {
12971 dwp_file->sections.info.s.section = sectp;
12972 dwp_file->sections.info.size = bfd_section_size (sectp);
12973 }
12974 else if (names->line_dwo.matches (sectp->name))
12975 {
12976 dwp_file->sections.line.s.section = sectp;
12977 dwp_file->sections.line.size = bfd_section_size (sectp);
12978 }
12979 else if (names->loc_dwo.matches (sectp->name))
12980 {
12981 dwp_file->sections.loc.s.section = sectp;
12982 dwp_file->sections.loc.size = bfd_section_size (sectp);
12983 }
12984 else if (names->macinfo_dwo.matches (sectp->name))
12985 {
12986 dwp_file->sections.macinfo.s.section = sectp;
12987 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12988 }
12989 else if (names->macro_dwo.matches (sectp->name))
12990 {
12991 dwp_file->sections.macro.s.section = sectp;
12992 dwp_file->sections.macro.size = bfd_section_size (sectp);
12993 }
12994 else if (names->str_offsets_dwo.matches (sectp->name))
12995 {
12996 dwp_file->sections.str_offsets.s.section = sectp;
12997 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12998 }
12999 else if (names->types_dwo.matches (sectp->name))
13000 {
13001 dwp_file->sections.types.s.section = sectp;
13002 dwp_file->sections.types.size = bfd_section_size (sectp);
13003 }
13004 }
13005
13006 /* This function is mapped across the sections and remembers the offset and
13007 size of each of the DWP version 5 debugging sections that we are interested
13008 in. This is split into a separate function because we don't know if we
13009 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13010
13011 static void
13012 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13013 {
13014 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13015 const struct dwop_section_names *names = &dwop_section_names;
13016 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13017
13018 /* Record the ELF section number for later lookup: this is what the
13019 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13020 gdb_assert (elf_section_nr < dwp_file->num_sections);
13021 dwp_file->elf_sections[elf_section_nr] = sectp;
13022
13023 /* Look for specific sections that we need. */
13024 if (names->abbrev_dwo.matches (sectp->name))
13025 {
13026 dwp_file->sections.abbrev.s.section = sectp;
13027 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13028 }
13029 else if (names->info_dwo.matches (sectp->name))
13030 {
13031 dwp_file->sections.info.s.section = sectp;
13032 dwp_file->sections.info.size = bfd_section_size (sectp);
13033 }
13034 else if (names->line_dwo.matches (sectp->name))
13035 {
13036 dwp_file->sections.line.s.section = sectp;
13037 dwp_file->sections.line.size = bfd_section_size (sectp);
13038 }
13039 else if (names->loclists_dwo.matches (sectp->name))
13040 {
13041 dwp_file->sections.loclists.s.section = sectp;
13042 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13043 }
13044 else if (names->macro_dwo.matches (sectp->name))
13045 {
13046 dwp_file->sections.macro.s.section = sectp;
13047 dwp_file->sections.macro.size = bfd_section_size (sectp);
13048 }
13049 else if (names->rnglists_dwo.matches (sectp->name))
13050 {
13051 dwp_file->sections.rnglists.s.section = sectp;
13052 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13053 }
13054 else if (names->str_offsets_dwo.matches (sectp->name))
13055 {
13056 dwp_file->sections.str_offsets.s.section = sectp;
13057 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13058 }
13059 }
13060
13061 /* Hash function for dwp_file loaded CUs/TUs. */
13062
13063 static hashval_t
13064 hash_dwp_loaded_cutus (const void *item)
13065 {
13066 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13067
13068 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13069 return dwo_unit->signature;
13070 }
13071
13072 /* Equality function for dwp_file loaded CUs/TUs. */
13073
13074 static int
13075 eq_dwp_loaded_cutus (const void *a, const void *b)
13076 {
13077 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13078 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13079
13080 return dua->signature == dub->signature;
13081 }
13082
13083 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13084
13085 static htab_up
13086 allocate_dwp_loaded_cutus_table ()
13087 {
13088 return htab_up (htab_create_alloc (3,
13089 hash_dwp_loaded_cutus,
13090 eq_dwp_loaded_cutus,
13091 NULL, xcalloc, xfree));
13092 }
13093
13094 /* Try to open DWP file FILE_NAME.
13095 The result is the bfd handle of the file.
13096 If there is a problem finding or opening the file, return NULL.
13097 Upon success, the canonicalized path of the file is stored in the bfd,
13098 same as symfile_bfd_open. */
13099
13100 static gdb_bfd_ref_ptr
13101 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13102 {
13103 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13104 1 /*is_dwp*/,
13105 1 /*search_cwd*/));
13106 if (abfd != NULL)
13107 return abfd;
13108
13109 /* Work around upstream bug 15652.
13110 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13111 [Whether that's a "bug" is debatable, but it is getting in our way.]
13112 We have no real idea where the dwp file is, because gdb's realpath-ing
13113 of the executable's path may have discarded the needed info.
13114 [IWBN if the dwp file name was recorded in the executable, akin to
13115 .gnu_debuglink, but that doesn't exist yet.]
13116 Strip the directory from FILE_NAME and search again. */
13117 if (*debug_file_directory != '\0')
13118 {
13119 /* Don't implicitly search the current directory here.
13120 If the user wants to search "." to handle this case,
13121 it must be added to debug-file-directory. */
13122 return try_open_dwop_file (per_objfile, lbasename (file_name),
13123 1 /*is_dwp*/,
13124 0 /*search_cwd*/);
13125 }
13126
13127 return NULL;
13128 }
13129
13130 /* Initialize the use of the DWP file for the current objfile.
13131 By convention the name of the DWP file is ${objfile}.dwp.
13132 The result is NULL if it can't be found. */
13133
13134 static std::unique_ptr<struct dwp_file>
13135 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13136 {
13137 struct objfile *objfile = per_objfile->objfile;
13138
13139 /* Try to find first .dwp for the binary file before any symbolic links
13140 resolving. */
13141
13142 /* If the objfile is a debug file, find the name of the real binary
13143 file and get the name of dwp file from there. */
13144 std::string dwp_name;
13145 if (objfile->separate_debug_objfile_backlink != NULL)
13146 {
13147 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13148 const char *backlink_basename = lbasename (backlink->original_name);
13149
13150 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13151 }
13152 else
13153 dwp_name = objfile->original_name;
13154
13155 dwp_name += ".dwp";
13156
13157 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13158 if (dbfd == NULL
13159 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13160 {
13161 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13162 dwp_name = objfile_name (objfile);
13163 dwp_name += ".dwp";
13164 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13165 }
13166
13167 if (dbfd == NULL)
13168 {
13169 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13170
13171 return std::unique_ptr<dwp_file> ();
13172 }
13173
13174 const char *name = bfd_get_filename (dbfd.get ());
13175 std::unique_ptr<struct dwp_file> dwp_file
13176 (new struct dwp_file (name, std::move (dbfd)));
13177
13178 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13179 dwp_file->elf_sections =
13180 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13181 dwp_file->num_sections, asection *);
13182
13183 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13184 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13185 dwp_file.get ());
13186
13187 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13188
13189 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13190
13191 /* The DWP file version is stored in the hash table. Oh well. */
13192 if (dwp_file->cus && dwp_file->tus
13193 && dwp_file->cus->version != dwp_file->tus->version)
13194 {
13195 /* Technically speaking, we should try to limp along, but this is
13196 pretty bizarre. We use pulongest here because that's the established
13197 portability solution (e.g, we cannot use %u for uint32_t). */
13198 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13199 " TU version %s [in DWP file %s]"),
13200 pulongest (dwp_file->cus->version),
13201 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13202 }
13203
13204 if (dwp_file->cus)
13205 dwp_file->version = dwp_file->cus->version;
13206 else if (dwp_file->tus)
13207 dwp_file->version = dwp_file->tus->version;
13208 else
13209 dwp_file->version = 2;
13210
13211 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13212 {
13213 if (dwp_file->version == 2)
13214 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13215 dwp_file.get ());
13216 else
13217 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13218 dwp_file.get ());
13219 }
13220
13221 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13222 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13223
13224 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13225 dwarf_read_debug_printf (" %s CUs, %s TUs",
13226 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13227 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13228
13229 return dwp_file;
13230 }
13231
13232 /* Wrapper around open_and_init_dwp_file, only open it once. */
13233
13234 static struct dwp_file *
13235 get_dwp_file (dwarf2_per_objfile *per_objfile)
13236 {
13237 if (!per_objfile->per_bfd->dwp_checked)
13238 {
13239 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13240 per_objfile->per_bfd->dwp_checked = 1;
13241 }
13242 return per_objfile->per_bfd->dwp_file.get ();
13243 }
13244
13245 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13246 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13247 or in the DWP file for the objfile, referenced by THIS_UNIT.
13248 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13249 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13250
13251 This is called, for example, when wanting to read a variable with a
13252 complex location. Therefore we don't want to do file i/o for every call.
13253 Therefore we don't want to look for a DWO file on every call.
13254 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13255 then we check if we've already seen DWO_NAME, and only THEN do we check
13256 for a DWO file.
13257
13258 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13259 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13260
13261 static struct dwo_unit *
13262 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13263 ULONGEST signature, int is_debug_types)
13264 {
13265 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13266 struct objfile *objfile = per_objfile->objfile;
13267 const char *kind = is_debug_types ? "TU" : "CU";
13268 void **dwo_file_slot;
13269 struct dwo_file *dwo_file;
13270 struct dwp_file *dwp_file;
13271
13272 /* First see if there's a DWP file.
13273 If we have a DWP file but didn't find the DWO inside it, don't
13274 look for the original DWO file. It makes gdb behave differently
13275 depending on whether one is debugging in the build tree. */
13276
13277 dwp_file = get_dwp_file (per_objfile);
13278 if (dwp_file != NULL)
13279 {
13280 const struct dwp_hash_table *dwp_htab =
13281 is_debug_types ? dwp_file->tus : dwp_file->cus;
13282
13283 if (dwp_htab != NULL)
13284 {
13285 struct dwo_unit *dwo_cutu =
13286 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13287 is_debug_types);
13288
13289 if (dwo_cutu != NULL)
13290 {
13291 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13292 kind, hex_string (signature),
13293 host_address_to_string (dwo_cutu));
13294
13295 return dwo_cutu;
13296 }
13297 }
13298 }
13299 else
13300 {
13301 /* No DWP file, look for the DWO file. */
13302
13303 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13304 if (*dwo_file_slot == NULL)
13305 {
13306 /* Read in the file and build a table of the CUs/TUs it contains. */
13307 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13308 }
13309 /* NOTE: This will be NULL if unable to open the file. */
13310 dwo_file = (struct dwo_file *) *dwo_file_slot;
13311
13312 if (dwo_file != NULL)
13313 {
13314 struct dwo_unit *dwo_cutu = NULL;
13315
13316 if (is_debug_types && dwo_file->tus)
13317 {
13318 struct dwo_unit find_dwo_cutu;
13319
13320 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13321 find_dwo_cutu.signature = signature;
13322 dwo_cutu
13323 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13324 &find_dwo_cutu);
13325 }
13326 else if (!is_debug_types && dwo_file->cus)
13327 {
13328 struct dwo_unit find_dwo_cutu;
13329
13330 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13331 find_dwo_cutu.signature = signature;
13332 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13333 &find_dwo_cutu);
13334 }
13335
13336 if (dwo_cutu != NULL)
13337 {
13338 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13339 kind, dwo_name, hex_string (signature),
13340 host_address_to_string (dwo_cutu));
13341
13342 return dwo_cutu;
13343 }
13344 }
13345 }
13346
13347 /* We didn't find it. This could mean a dwo_id mismatch, or
13348 someone deleted the DWO/DWP file, or the search path isn't set up
13349 correctly to find the file. */
13350
13351 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13352 kind, dwo_name, hex_string (signature));
13353
13354 /* This is a warning and not a complaint because it can be caused by
13355 pilot error (e.g., user accidentally deleting the DWO). */
13356 {
13357 /* Print the name of the DWP file if we looked there, helps the user
13358 better diagnose the problem. */
13359 std::string dwp_text;
13360
13361 if (dwp_file != NULL)
13362 dwp_text = string_printf (" [in DWP file %s]",
13363 lbasename (dwp_file->name));
13364
13365 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13366 " [in module %s]"),
13367 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13368 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13369 }
13370 return NULL;
13371 }
13372
13373 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13374 See lookup_dwo_cutu_unit for details. */
13375
13376 static struct dwo_unit *
13377 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13378 ULONGEST signature)
13379 {
13380 gdb_assert (!cu->per_cu->is_debug_types);
13381
13382 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13383 }
13384
13385 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13386 See lookup_dwo_cutu_unit for details. */
13387
13388 static struct dwo_unit *
13389 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13390 {
13391 gdb_assert (cu->per_cu->is_debug_types);
13392
13393 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13394
13395 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13396 }
13397
13398 /* Traversal function for queue_and_load_all_dwo_tus. */
13399
13400 static int
13401 queue_and_load_dwo_tu (void **slot, void *info)
13402 {
13403 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13404 dwarf2_cu *cu = (dwarf2_cu *) info;
13405 ULONGEST signature = dwo_unit->signature;
13406 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13407
13408 if (sig_type != NULL)
13409 {
13410 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13411
13412 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13413 a real dependency of PER_CU on SIG_TYPE. That is detected later
13414 while processing PER_CU. */
13415 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13416 load_full_type_unit (sig_cu, cu->per_objfile);
13417 cu->per_cu->imported_symtabs_push (sig_cu);
13418 }
13419
13420 return 1;
13421 }
13422
13423 /* Queue all TUs contained in the DWO of CU to be read in.
13424 The DWO may have the only definition of the type, though it may not be
13425 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13426 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13427
13428 static void
13429 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13430 {
13431 struct dwo_unit *dwo_unit;
13432 struct dwo_file *dwo_file;
13433
13434 gdb_assert (cu != nullptr);
13435 gdb_assert (!cu->per_cu->is_debug_types);
13436 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13437
13438 dwo_unit = cu->dwo_unit;
13439 gdb_assert (dwo_unit != NULL);
13440
13441 dwo_file = dwo_unit->dwo_file;
13442 if (dwo_file->tus != NULL)
13443 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13444 }
13445
13446 /* Read in various DIEs. */
13447
13448 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13449 Inherit only the children of the DW_AT_abstract_origin DIE not being
13450 already referenced by DW_AT_abstract_origin from the children of the
13451 current DIE. */
13452
13453 static void
13454 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13455 {
13456 struct die_info *child_die;
13457 sect_offset *offsetp;
13458 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13459 struct die_info *origin_die;
13460 /* Iterator of the ORIGIN_DIE children. */
13461 struct die_info *origin_child_die;
13462 struct attribute *attr;
13463 struct dwarf2_cu *origin_cu;
13464 struct pending **origin_previous_list_in_scope;
13465
13466 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13467 if (!attr)
13468 return;
13469
13470 /* Note that following die references may follow to a die in a
13471 different cu. */
13472
13473 origin_cu = cu;
13474 origin_die = follow_die_ref (die, attr, &origin_cu);
13475
13476 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13477 symbols in. */
13478 origin_previous_list_in_scope = origin_cu->list_in_scope;
13479 origin_cu->list_in_scope = cu->list_in_scope;
13480
13481 if (die->tag != origin_die->tag
13482 && !(die->tag == DW_TAG_inlined_subroutine
13483 && origin_die->tag == DW_TAG_subprogram))
13484 complaint (_("DIE %s and its abstract origin %s have different tags"),
13485 sect_offset_str (die->sect_off),
13486 sect_offset_str (origin_die->sect_off));
13487
13488 std::vector<sect_offset> offsets;
13489
13490 for (child_die = die->child;
13491 child_die && child_die->tag;
13492 child_die = child_die->sibling)
13493 {
13494 struct die_info *child_origin_die;
13495 struct dwarf2_cu *child_origin_cu;
13496
13497 /* We are trying to process concrete instance entries:
13498 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13499 it's not relevant to our analysis here. i.e. detecting DIEs that are
13500 present in the abstract instance but not referenced in the concrete
13501 one. */
13502 if (child_die->tag == DW_TAG_call_site
13503 || child_die->tag == DW_TAG_GNU_call_site)
13504 continue;
13505
13506 /* For each CHILD_DIE, find the corresponding child of
13507 ORIGIN_DIE. If there is more than one layer of
13508 DW_AT_abstract_origin, follow them all; there shouldn't be,
13509 but GCC versions at least through 4.4 generate this (GCC PR
13510 40573). */
13511 child_origin_die = child_die;
13512 child_origin_cu = cu;
13513 while (1)
13514 {
13515 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13516 child_origin_cu);
13517 if (attr == NULL)
13518 break;
13519 child_origin_die = follow_die_ref (child_origin_die, attr,
13520 &child_origin_cu);
13521 }
13522
13523 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13524 counterpart may exist. */
13525 if (child_origin_die != child_die)
13526 {
13527 if (child_die->tag != child_origin_die->tag
13528 && !(child_die->tag == DW_TAG_inlined_subroutine
13529 && child_origin_die->tag == DW_TAG_subprogram))
13530 complaint (_("Child DIE %s and its abstract origin %s have "
13531 "different tags"),
13532 sect_offset_str (child_die->sect_off),
13533 sect_offset_str (child_origin_die->sect_off));
13534 if (child_origin_die->parent != origin_die)
13535 complaint (_("Child DIE %s and its abstract origin %s have "
13536 "different parents"),
13537 sect_offset_str (child_die->sect_off),
13538 sect_offset_str (child_origin_die->sect_off));
13539 else
13540 offsets.push_back (child_origin_die->sect_off);
13541 }
13542 }
13543 std::sort (offsets.begin (), offsets.end ());
13544 sect_offset *offsets_end = offsets.data () + offsets.size ();
13545 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13546 if (offsetp[-1] == *offsetp)
13547 complaint (_("Multiple children of DIE %s refer "
13548 "to DIE %s as their abstract origin"),
13549 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13550
13551 offsetp = offsets.data ();
13552 origin_child_die = origin_die->child;
13553 while (origin_child_die && origin_child_die->tag)
13554 {
13555 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13556 while (offsetp < offsets_end
13557 && *offsetp < origin_child_die->sect_off)
13558 offsetp++;
13559 if (offsetp >= offsets_end
13560 || *offsetp > origin_child_die->sect_off)
13561 {
13562 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13563 Check whether we're already processing ORIGIN_CHILD_DIE.
13564 This can happen with mutually referenced abstract_origins.
13565 PR 16581. */
13566 if (!origin_child_die->in_process)
13567 process_die (origin_child_die, origin_cu);
13568 }
13569 origin_child_die = origin_child_die->sibling;
13570 }
13571 origin_cu->list_in_scope = origin_previous_list_in_scope;
13572
13573 if (cu != origin_cu)
13574 compute_delayed_physnames (origin_cu);
13575 }
13576
13577 static void
13578 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13579 {
13580 struct objfile *objfile = cu->per_objfile->objfile;
13581 struct gdbarch *gdbarch = objfile->arch ();
13582 struct context_stack *newobj;
13583 CORE_ADDR lowpc;
13584 CORE_ADDR highpc;
13585 struct die_info *child_die;
13586 struct attribute *attr, *call_line, *call_file;
13587 const char *name;
13588 CORE_ADDR baseaddr;
13589 struct block *block;
13590 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13591 std::vector<struct symbol *> template_args;
13592 struct template_symbol *templ_func = NULL;
13593
13594 if (inlined_func)
13595 {
13596 /* If we do not have call site information, we can't show the
13597 caller of this inlined function. That's too confusing, so
13598 only use the scope for local variables. */
13599 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13600 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13601 if (call_line == NULL || call_file == NULL)
13602 {
13603 read_lexical_block_scope (die, cu);
13604 return;
13605 }
13606 }
13607
13608 baseaddr = objfile->text_section_offset ();
13609
13610 name = dwarf2_name (die, cu);
13611
13612 /* Ignore functions with missing or empty names. These are actually
13613 illegal according to the DWARF standard. */
13614 if (name == NULL)
13615 {
13616 complaint (_("missing name for subprogram DIE at %s"),
13617 sect_offset_str (die->sect_off));
13618 return;
13619 }
13620
13621 /* Ignore functions with missing or invalid low and high pc attributes. */
13622 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13623 <= PC_BOUNDS_INVALID)
13624 {
13625 attr = dwarf2_attr (die, DW_AT_external, cu);
13626 if (attr == nullptr || !attr->as_boolean ())
13627 complaint (_("cannot get low and high bounds "
13628 "for subprogram DIE at %s"),
13629 sect_offset_str (die->sect_off));
13630 return;
13631 }
13632
13633 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13634 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13635
13636 /* If we have any template arguments, then we must allocate a
13637 different sort of symbol. */
13638 for (child_die = die->child; child_die; child_die = child_die->sibling)
13639 {
13640 if (child_die->tag == DW_TAG_template_type_param
13641 || child_die->tag == DW_TAG_template_value_param)
13642 {
13643 templ_func = new (&objfile->objfile_obstack) template_symbol;
13644 templ_func->subclass = SYMBOL_TEMPLATE;
13645 break;
13646 }
13647 }
13648
13649 gdb_assert (cu->get_builder () != nullptr);
13650 newobj = cu->get_builder ()->push_context (0, lowpc);
13651 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13652 (struct symbol *) templ_func);
13653
13654 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13655 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13656 cu->language);
13657
13658 /* If there is a location expression for DW_AT_frame_base, record
13659 it. */
13660 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13661 if (attr != nullptr)
13662 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13663
13664 /* If there is a location for the static link, record it. */
13665 newobj->static_link = NULL;
13666 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13667 if (attr != nullptr)
13668 {
13669 newobj->static_link
13670 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13671 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13672 cu->addr_type ());
13673 }
13674
13675 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13676
13677 if (die->child != NULL)
13678 {
13679 child_die = die->child;
13680 while (child_die && child_die->tag)
13681 {
13682 if (child_die->tag == DW_TAG_template_type_param
13683 || child_die->tag == DW_TAG_template_value_param)
13684 {
13685 struct symbol *arg = new_symbol (child_die, NULL, cu);
13686
13687 if (arg != NULL)
13688 template_args.push_back (arg);
13689 }
13690 else
13691 process_die (child_die, cu);
13692 child_die = child_die->sibling;
13693 }
13694 }
13695
13696 inherit_abstract_dies (die, cu);
13697
13698 /* If we have a DW_AT_specification, we might need to import using
13699 directives from the context of the specification DIE. See the
13700 comment in determine_prefix. */
13701 if (cu->language == language_cplus
13702 && dwarf2_attr (die, DW_AT_specification, cu))
13703 {
13704 struct dwarf2_cu *spec_cu = cu;
13705 struct die_info *spec_die = die_specification (die, &spec_cu);
13706
13707 while (spec_die)
13708 {
13709 child_die = spec_die->child;
13710 while (child_die && child_die->tag)
13711 {
13712 if (child_die->tag == DW_TAG_imported_module)
13713 process_die (child_die, spec_cu);
13714 child_die = child_die->sibling;
13715 }
13716
13717 /* In some cases, GCC generates specification DIEs that
13718 themselves contain DW_AT_specification attributes. */
13719 spec_die = die_specification (spec_die, &spec_cu);
13720 }
13721 }
13722
13723 struct context_stack cstk = cu->get_builder ()->pop_context ();
13724 /* Make a block for the local symbols within. */
13725 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13726 cstk.static_link, lowpc, highpc);
13727
13728 /* For C++, set the block's scope. */
13729 if ((cu->language == language_cplus
13730 || cu->language == language_fortran
13731 || cu->language == language_d
13732 || cu->language == language_rust)
13733 && cu->processing_has_namespace_info)
13734 block_set_scope (block, determine_prefix (die, cu),
13735 &objfile->objfile_obstack);
13736
13737 /* If we have address ranges, record them. */
13738 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13739
13740 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13741
13742 /* Attach template arguments to function. */
13743 if (!template_args.empty ())
13744 {
13745 gdb_assert (templ_func != NULL);
13746
13747 templ_func->n_template_arguments = template_args.size ();
13748 templ_func->template_arguments
13749 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13750 templ_func->n_template_arguments);
13751 memcpy (templ_func->template_arguments,
13752 template_args.data (),
13753 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13754
13755 /* Make sure that the symtab is set on the new symbols. Even
13756 though they don't appear in this symtab directly, other parts
13757 of gdb assume that symbols do, and this is reasonably
13758 true. */
13759 for (symbol *sym : template_args)
13760 symbol_set_symtab (sym, symbol_symtab (templ_func));
13761 }
13762
13763 /* In C++, we can have functions nested inside functions (e.g., when
13764 a function declares a class that has methods). This means that
13765 when we finish processing a function scope, we may need to go
13766 back to building a containing block's symbol lists. */
13767 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13768 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13769
13770 /* If we've finished processing a top-level function, subsequent
13771 symbols go in the file symbol list. */
13772 if (cu->get_builder ()->outermost_context_p ())
13773 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13774 }
13775
13776 /* Process all the DIES contained within a lexical block scope. Start
13777 a new scope, process the dies, and then close the scope. */
13778
13779 static void
13780 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13781 {
13782 struct objfile *objfile = cu->per_objfile->objfile;
13783 struct gdbarch *gdbarch = objfile->arch ();
13784 CORE_ADDR lowpc, highpc;
13785 struct die_info *child_die;
13786 CORE_ADDR baseaddr;
13787
13788 baseaddr = objfile->text_section_offset ();
13789
13790 /* Ignore blocks with missing or invalid low and high pc attributes. */
13791 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13792 as multiple lexical blocks? Handling children in a sane way would
13793 be nasty. Might be easier to properly extend generic blocks to
13794 describe ranges. */
13795 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13796 {
13797 case PC_BOUNDS_NOT_PRESENT:
13798 /* DW_TAG_lexical_block has no attributes, process its children as if
13799 there was no wrapping by that DW_TAG_lexical_block.
13800 GCC does no longer produces such DWARF since GCC r224161. */
13801 for (child_die = die->child;
13802 child_die != NULL && child_die->tag;
13803 child_die = child_die->sibling)
13804 {
13805 /* We might already be processing this DIE. This can happen
13806 in an unusual circumstance -- where a subroutine A
13807 appears lexically in another subroutine B, but A actually
13808 inlines B. The recursion is broken here, rather than in
13809 inherit_abstract_dies, because it seems better to simply
13810 drop concrete children here. */
13811 if (!child_die->in_process)
13812 process_die (child_die, cu);
13813 }
13814 return;
13815 case PC_BOUNDS_INVALID:
13816 return;
13817 }
13818 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13819 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13820
13821 cu->get_builder ()->push_context (0, lowpc);
13822 if (die->child != NULL)
13823 {
13824 child_die = die->child;
13825 while (child_die && child_die->tag)
13826 {
13827 process_die (child_die, cu);
13828 child_die = child_die->sibling;
13829 }
13830 }
13831 inherit_abstract_dies (die, cu);
13832 struct context_stack cstk = cu->get_builder ()->pop_context ();
13833
13834 if (*cu->get_builder ()->get_local_symbols () != NULL
13835 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13836 {
13837 struct block *block
13838 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13839 cstk.start_addr, highpc);
13840
13841 /* Note that recording ranges after traversing children, as we
13842 do here, means that recording a parent's ranges entails
13843 walking across all its children's ranges as they appear in
13844 the address map, which is quadratic behavior.
13845
13846 It would be nicer to record the parent's ranges before
13847 traversing its children, simply overriding whatever you find
13848 there. But since we don't even decide whether to create a
13849 block until after we've traversed its children, that's hard
13850 to do. */
13851 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13852 }
13853 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13854 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13855 }
13856
13857 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13858
13859 static void
13860 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13861 {
13862 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13863 struct objfile *objfile = per_objfile->objfile;
13864 struct gdbarch *gdbarch = objfile->arch ();
13865 CORE_ADDR pc, baseaddr;
13866 struct attribute *attr;
13867 struct call_site *call_site, call_site_local;
13868 void **slot;
13869 int nparams;
13870 struct die_info *child_die;
13871
13872 baseaddr = objfile->text_section_offset ();
13873
13874 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13875 if (attr == NULL)
13876 {
13877 /* This was a pre-DWARF-5 GNU extension alias
13878 for DW_AT_call_return_pc. */
13879 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13880 }
13881 if (!attr)
13882 {
13883 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13884 "DIE %s [in module %s]"),
13885 sect_offset_str (die->sect_off), objfile_name (objfile));
13886 return;
13887 }
13888 pc = attr->as_address () + baseaddr;
13889 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13890
13891 if (cu->call_site_htab == NULL)
13892 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13893 NULL, &objfile->objfile_obstack,
13894 hashtab_obstack_allocate, NULL);
13895 call_site_local.pc = pc;
13896 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13897 if (*slot != NULL)
13898 {
13899 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13900 "DIE %s [in module %s]"),
13901 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13902 objfile_name (objfile));
13903 return;
13904 }
13905
13906 /* Count parameters at the caller. */
13907
13908 nparams = 0;
13909 for (child_die = die->child; child_die && child_die->tag;
13910 child_die = child_die->sibling)
13911 {
13912 if (child_die->tag != DW_TAG_call_site_parameter
13913 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13914 {
13915 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13916 "DW_TAG_call_site child DIE %s [in module %s]"),
13917 child_die->tag, sect_offset_str (child_die->sect_off),
13918 objfile_name (objfile));
13919 continue;
13920 }
13921
13922 nparams++;
13923 }
13924
13925 call_site
13926 = ((struct call_site *)
13927 obstack_alloc (&objfile->objfile_obstack,
13928 sizeof (*call_site)
13929 + (sizeof (*call_site->parameter) * (nparams - 1))));
13930 *slot = call_site;
13931 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13932 call_site->pc = pc;
13933
13934 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13935 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13936 {
13937 struct die_info *func_die;
13938
13939 /* Skip also over DW_TAG_inlined_subroutine. */
13940 for (func_die = die->parent;
13941 func_die && func_die->tag != DW_TAG_subprogram
13942 && func_die->tag != DW_TAG_subroutine_type;
13943 func_die = func_die->parent);
13944
13945 /* DW_AT_call_all_calls is a superset
13946 of DW_AT_call_all_tail_calls. */
13947 if (func_die
13948 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13949 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13950 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13951 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13952 {
13953 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13954 not complete. But keep CALL_SITE for look ups via call_site_htab,
13955 both the initial caller containing the real return address PC and
13956 the final callee containing the current PC of a chain of tail
13957 calls do not need to have the tail call list complete. But any
13958 function candidate for a virtual tail call frame searched via
13959 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13960 determined unambiguously. */
13961 }
13962 else
13963 {
13964 struct type *func_type = NULL;
13965
13966 if (func_die)
13967 func_type = get_die_type (func_die, cu);
13968 if (func_type != NULL)
13969 {
13970 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13971
13972 /* Enlist this call site to the function. */
13973 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13974 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13975 }
13976 else
13977 complaint (_("Cannot find function owning DW_TAG_call_site "
13978 "DIE %s [in module %s]"),
13979 sect_offset_str (die->sect_off), objfile_name (objfile));
13980 }
13981 }
13982
13983 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13984 if (attr == NULL)
13985 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13986 if (attr == NULL)
13987 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13988 if (attr == NULL)
13989 {
13990 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13991 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13992 }
13993 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13994 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13995 /* Keep NULL DWARF_BLOCK. */;
13996 else if (attr->form_is_block ())
13997 {
13998 struct dwarf2_locexpr_baton *dlbaton;
13999 struct dwarf_block *block = attr->as_block ();
14000
14001 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14002 dlbaton->data = block->data;
14003 dlbaton->size = block->size;
14004 dlbaton->per_objfile = per_objfile;
14005 dlbaton->per_cu = cu->per_cu;
14006
14007 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14008 }
14009 else if (attr->form_is_ref ())
14010 {
14011 struct dwarf2_cu *target_cu = cu;
14012 struct die_info *target_die;
14013
14014 target_die = follow_die_ref (die, attr, &target_cu);
14015 gdb_assert (target_cu->per_objfile->objfile == objfile);
14016 if (die_is_declaration (target_die, target_cu))
14017 {
14018 const char *target_physname;
14019
14020 /* Prefer the mangled name; otherwise compute the demangled one. */
14021 target_physname = dw2_linkage_name (target_die, target_cu);
14022 if (target_physname == NULL)
14023 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14024 if (target_physname == NULL)
14025 complaint (_("DW_AT_call_target target DIE has invalid "
14026 "physname, for referencing DIE %s [in module %s]"),
14027 sect_offset_str (die->sect_off), objfile_name (objfile));
14028 else
14029 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14030 }
14031 else
14032 {
14033 CORE_ADDR lowpc;
14034
14035 /* DW_AT_entry_pc should be preferred. */
14036 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14037 <= PC_BOUNDS_INVALID)
14038 complaint (_("DW_AT_call_target target DIE has invalid "
14039 "low pc, for referencing DIE %s [in module %s]"),
14040 sect_offset_str (die->sect_off), objfile_name (objfile));
14041 else
14042 {
14043 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14044 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14045 }
14046 }
14047 }
14048 else
14049 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14050 "block nor reference, for DIE %s [in module %s]"),
14051 sect_offset_str (die->sect_off), objfile_name (objfile));
14052
14053 call_site->per_cu = cu->per_cu;
14054 call_site->per_objfile = per_objfile;
14055
14056 for (child_die = die->child;
14057 child_die && child_die->tag;
14058 child_die = child_die->sibling)
14059 {
14060 struct call_site_parameter *parameter;
14061 struct attribute *loc, *origin;
14062
14063 if (child_die->tag != DW_TAG_call_site_parameter
14064 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14065 {
14066 /* Already printed the complaint above. */
14067 continue;
14068 }
14069
14070 gdb_assert (call_site->parameter_count < nparams);
14071 parameter = &call_site->parameter[call_site->parameter_count];
14072
14073 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14074 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14075 register is contained in DW_AT_call_value. */
14076
14077 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14078 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14079 if (origin == NULL)
14080 {
14081 /* This was a pre-DWARF-5 GNU extension alias
14082 for DW_AT_call_parameter. */
14083 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14084 }
14085 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14086 {
14087 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14088
14089 sect_offset sect_off = origin->get_ref_die_offset ();
14090 if (!cu->header.offset_in_cu_p (sect_off))
14091 {
14092 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14093 binding can be done only inside one CU. Such referenced DIE
14094 therefore cannot be even moved to DW_TAG_partial_unit. */
14095 complaint (_("DW_AT_call_parameter offset is not in CU for "
14096 "DW_TAG_call_site child DIE %s [in module %s]"),
14097 sect_offset_str (child_die->sect_off),
14098 objfile_name (objfile));
14099 continue;
14100 }
14101 parameter->u.param_cu_off
14102 = (cu_offset) (sect_off - cu->header.sect_off);
14103 }
14104 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14105 {
14106 complaint (_("No DW_FORM_block* DW_AT_location for "
14107 "DW_TAG_call_site child DIE %s [in module %s]"),
14108 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14109 continue;
14110 }
14111 else
14112 {
14113 struct dwarf_block *block = loc->as_block ();
14114
14115 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14116 (block->data, &block->data[block->size]);
14117 if (parameter->u.dwarf_reg != -1)
14118 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14119 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14120 &block->data[block->size],
14121 &parameter->u.fb_offset))
14122 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14123 else
14124 {
14125 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14126 "for DW_FORM_block* DW_AT_location is supported for "
14127 "DW_TAG_call_site child DIE %s "
14128 "[in module %s]"),
14129 sect_offset_str (child_die->sect_off),
14130 objfile_name (objfile));
14131 continue;
14132 }
14133 }
14134
14135 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14136 if (attr == NULL)
14137 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14138 if (attr == NULL || !attr->form_is_block ())
14139 {
14140 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14141 "DW_TAG_call_site child DIE %s [in module %s]"),
14142 sect_offset_str (child_die->sect_off),
14143 objfile_name (objfile));
14144 continue;
14145 }
14146
14147 struct dwarf_block *block = attr->as_block ();
14148 parameter->value = block->data;
14149 parameter->value_size = block->size;
14150
14151 /* Parameters are not pre-cleared by memset above. */
14152 parameter->data_value = NULL;
14153 parameter->data_value_size = 0;
14154 call_site->parameter_count++;
14155
14156 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14157 if (attr == NULL)
14158 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14159 if (attr != nullptr)
14160 {
14161 if (!attr->form_is_block ())
14162 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14163 "DW_TAG_call_site child DIE %s [in module %s]"),
14164 sect_offset_str (child_die->sect_off),
14165 objfile_name (objfile));
14166 else
14167 {
14168 block = attr->as_block ();
14169 parameter->data_value = block->data;
14170 parameter->data_value_size = block->size;
14171 }
14172 }
14173 }
14174 }
14175
14176 /* Helper function for read_variable. If DIE represents a virtual
14177 table, then return the type of the concrete object that is
14178 associated with the virtual table. Otherwise, return NULL. */
14179
14180 static struct type *
14181 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14182 {
14183 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14184 if (attr == NULL)
14185 return NULL;
14186
14187 /* Find the type DIE. */
14188 struct die_info *type_die = NULL;
14189 struct dwarf2_cu *type_cu = cu;
14190
14191 if (attr->form_is_ref ())
14192 type_die = follow_die_ref (die, attr, &type_cu);
14193 if (type_die == NULL)
14194 return NULL;
14195
14196 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14197 return NULL;
14198 return die_containing_type (type_die, type_cu);
14199 }
14200
14201 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14202
14203 static void
14204 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14205 {
14206 struct rust_vtable_symbol *storage = NULL;
14207
14208 if (cu->language == language_rust)
14209 {
14210 struct type *containing_type = rust_containing_type (die, cu);
14211
14212 if (containing_type != NULL)
14213 {
14214 struct objfile *objfile = cu->per_objfile->objfile;
14215
14216 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14217 storage->concrete_type = containing_type;
14218 storage->subclass = SYMBOL_RUST_VTABLE;
14219 }
14220 }
14221
14222 struct symbol *res = new_symbol (die, NULL, cu, storage);
14223 struct attribute *abstract_origin
14224 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14225 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14226 if (res == NULL && loc && abstract_origin)
14227 {
14228 /* We have a variable without a name, but with a location and an abstract
14229 origin. This may be a concrete instance of an abstract variable
14230 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14231 later. */
14232 struct dwarf2_cu *origin_cu = cu;
14233 struct die_info *origin_die
14234 = follow_die_ref (die, abstract_origin, &origin_cu);
14235 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14236 per_objfile->per_bfd->abstract_to_concrete
14237 [origin_die->sect_off].push_back (die->sect_off);
14238 }
14239 }
14240
14241 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14242 reading .debug_rnglists.
14243 Callback's type should be:
14244 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14245 Return true if the attributes are present and valid, otherwise,
14246 return false. */
14247
14248 template <typename Callback>
14249 static bool
14250 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14251 dwarf_tag tag, Callback &&callback)
14252 {
14253 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14254 struct objfile *objfile = per_objfile->objfile;
14255 bfd *obfd = objfile->obfd;
14256 /* Base address selection entry. */
14257 gdb::optional<CORE_ADDR> base;
14258 const gdb_byte *buffer;
14259 CORE_ADDR baseaddr;
14260 bool overflow = false;
14261 ULONGEST addr_index;
14262 struct dwarf2_section_info *rnglists_section;
14263
14264 base = cu->base_address;
14265 rnglists_section = cu_debug_rnglists_section (cu, tag);
14266 rnglists_section->read (objfile);
14267
14268 if (offset >= rnglists_section->size)
14269 {
14270 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14271 offset);
14272 return false;
14273 }
14274 buffer = rnglists_section->buffer + offset;
14275
14276 baseaddr = objfile->text_section_offset ();
14277
14278 while (1)
14279 {
14280 /* Initialize it due to a false compiler warning. */
14281 CORE_ADDR range_beginning = 0, range_end = 0;
14282 const gdb_byte *buf_end = (rnglists_section->buffer
14283 + rnglists_section->size);
14284 unsigned int bytes_read;
14285
14286 if (buffer == buf_end)
14287 {
14288 overflow = true;
14289 break;
14290 }
14291 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14292 switch (rlet)
14293 {
14294 case DW_RLE_end_of_list:
14295 break;
14296 case DW_RLE_base_address:
14297 if (buffer + cu->header.addr_size > buf_end)
14298 {
14299 overflow = true;
14300 break;
14301 }
14302 base = cu->header.read_address (obfd, buffer, &bytes_read);
14303 buffer += bytes_read;
14304 break;
14305 case DW_RLE_base_addressx:
14306 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14307 buffer += bytes_read;
14308 base = read_addr_index (cu, addr_index);
14309 break;
14310 case DW_RLE_start_length:
14311 if (buffer + cu->header.addr_size > buf_end)
14312 {
14313 overflow = true;
14314 break;
14315 }
14316 range_beginning = cu->header.read_address (obfd, buffer,
14317 &bytes_read);
14318 buffer += bytes_read;
14319 range_end = (range_beginning
14320 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14321 buffer += bytes_read;
14322 if (buffer > buf_end)
14323 {
14324 overflow = true;
14325 break;
14326 }
14327 break;
14328 case DW_RLE_startx_length:
14329 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14330 buffer += bytes_read;
14331 range_beginning = read_addr_index (cu, addr_index);
14332 if (buffer > buf_end)
14333 {
14334 overflow = true;
14335 break;
14336 }
14337 range_end = (range_beginning
14338 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14339 buffer += bytes_read;
14340 break;
14341 case DW_RLE_offset_pair:
14342 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14343 buffer += bytes_read;
14344 if (buffer > buf_end)
14345 {
14346 overflow = true;
14347 break;
14348 }
14349 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14350 buffer += bytes_read;
14351 if (buffer > buf_end)
14352 {
14353 overflow = true;
14354 break;
14355 }
14356 break;
14357 case DW_RLE_start_end:
14358 if (buffer + 2 * cu->header.addr_size > buf_end)
14359 {
14360 overflow = true;
14361 break;
14362 }
14363 range_beginning = cu->header.read_address (obfd, buffer,
14364 &bytes_read);
14365 buffer += bytes_read;
14366 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14367 buffer += bytes_read;
14368 break;
14369 case DW_RLE_startx_endx:
14370 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14371 buffer += bytes_read;
14372 range_beginning = read_addr_index (cu, addr_index);
14373 if (buffer > buf_end)
14374 {
14375 overflow = true;
14376 break;
14377 }
14378 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14379 buffer += bytes_read;
14380 range_end = read_addr_index (cu, addr_index);
14381 break;
14382 default:
14383 complaint (_("Invalid .debug_rnglists data (no base address)"));
14384 return false;
14385 }
14386 if (rlet == DW_RLE_end_of_list || overflow)
14387 break;
14388 if (rlet == DW_RLE_base_address)
14389 continue;
14390
14391 if (range_beginning > range_end)
14392 {
14393 /* Inverted range entries are invalid. */
14394 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14395 return false;
14396 }
14397
14398 /* Empty range entries have no effect. */
14399 if (range_beginning == range_end)
14400 continue;
14401
14402 /* Only DW_RLE_offset_pair needs the base address added. */
14403 if (rlet == DW_RLE_offset_pair)
14404 {
14405 if (!base.has_value ())
14406 {
14407 /* We have no valid base address for the DW_RLE_offset_pair. */
14408 complaint (_("Invalid .debug_rnglists data (no base address for "
14409 "DW_RLE_offset_pair)"));
14410 return false;
14411 }
14412
14413 range_beginning += *base;
14414 range_end += *base;
14415 }
14416
14417 /* A not-uncommon case of bad debug info.
14418 Don't pollute the addrmap with bad data. */
14419 if (range_beginning + baseaddr == 0
14420 && !per_objfile->per_bfd->has_section_at_zero)
14421 {
14422 complaint (_(".debug_rnglists entry has start address of zero"
14423 " [in module %s]"), objfile_name (objfile));
14424 continue;
14425 }
14426
14427 callback (range_beginning, range_end);
14428 }
14429
14430 if (overflow)
14431 {
14432 complaint (_("Offset %d is not terminated "
14433 "for DW_AT_ranges attribute"),
14434 offset);
14435 return false;
14436 }
14437
14438 return true;
14439 }
14440
14441 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14442 Callback's type should be:
14443 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14444 Return 1 if the attributes are present and valid, otherwise, return 0. */
14445
14446 template <typename Callback>
14447 static int
14448 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14449 Callback &&callback)
14450 {
14451 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14452 struct objfile *objfile = per_objfile->objfile;
14453 struct comp_unit_head *cu_header = &cu->header;
14454 bfd *obfd = objfile->obfd;
14455 unsigned int addr_size = cu_header->addr_size;
14456 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14457 /* Base address selection entry. */
14458 gdb::optional<CORE_ADDR> base;
14459 unsigned int dummy;
14460 const gdb_byte *buffer;
14461 CORE_ADDR baseaddr;
14462
14463 if (cu_header->version >= 5)
14464 return dwarf2_rnglists_process (offset, cu, tag, callback);
14465
14466 base = cu->base_address;
14467
14468 per_objfile->per_bfd->ranges.read (objfile);
14469 if (offset >= per_objfile->per_bfd->ranges.size)
14470 {
14471 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14472 offset);
14473 return 0;
14474 }
14475 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14476
14477 baseaddr = objfile->text_section_offset ();
14478
14479 while (1)
14480 {
14481 CORE_ADDR range_beginning, range_end;
14482
14483 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14484 buffer += addr_size;
14485 range_end = cu->header.read_address (obfd, buffer, &dummy);
14486 buffer += addr_size;
14487 offset += 2 * addr_size;
14488
14489 /* An end of list marker is a pair of zero addresses. */
14490 if (range_beginning == 0 && range_end == 0)
14491 /* Found the end of list entry. */
14492 break;
14493
14494 /* Each base address selection entry is a pair of 2 values.
14495 The first is the largest possible address, the second is
14496 the base address. Check for a base address here. */
14497 if ((range_beginning & mask) == mask)
14498 {
14499 /* If we found the largest possible address, then we already
14500 have the base address in range_end. */
14501 base = range_end;
14502 continue;
14503 }
14504
14505 if (!base.has_value ())
14506 {
14507 /* We have no valid base address for the ranges
14508 data. */
14509 complaint (_("Invalid .debug_ranges data (no base address)"));
14510 return 0;
14511 }
14512
14513 if (range_beginning > range_end)
14514 {
14515 /* Inverted range entries are invalid. */
14516 complaint (_("Invalid .debug_ranges data (inverted range)"));
14517 return 0;
14518 }
14519
14520 /* Empty range entries have no effect. */
14521 if (range_beginning == range_end)
14522 continue;
14523
14524 range_beginning += *base;
14525 range_end += *base;
14526
14527 /* A not-uncommon case of bad debug info.
14528 Don't pollute the addrmap with bad data. */
14529 if (range_beginning + baseaddr == 0
14530 && !per_objfile->per_bfd->has_section_at_zero)
14531 {
14532 complaint (_(".debug_ranges entry has start address of zero"
14533 " [in module %s]"), objfile_name (objfile));
14534 continue;
14535 }
14536
14537 callback (range_beginning, range_end);
14538 }
14539
14540 return 1;
14541 }
14542
14543 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14544 Return 1 if the attributes are present and valid, otherwise, return 0.
14545 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14546
14547 static int
14548 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14549 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14550 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14551 {
14552 struct objfile *objfile = cu->per_objfile->objfile;
14553 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14554 struct gdbarch *gdbarch = objfile->arch ();
14555 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14556 int low_set = 0;
14557 CORE_ADDR low = 0;
14558 CORE_ADDR high = 0;
14559 int retval;
14560
14561 retval = dwarf2_ranges_process (offset, cu, tag,
14562 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14563 {
14564 if (ranges_pst != NULL)
14565 {
14566 CORE_ADDR lowpc;
14567 CORE_ADDR highpc;
14568
14569 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14570 range_beginning + baseaddr)
14571 - baseaddr);
14572 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14573 range_end + baseaddr)
14574 - baseaddr);
14575 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14576 lowpc, highpc - 1, ranges_pst);
14577 }
14578
14579 /* FIXME: This is recording everything as a low-high
14580 segment of consecutive addresses. We should have a
14581 data structure for discontiguous block ranges
14582 instead. */
14583 if (! low_set)
14584 {
14585 low = range_beginning;
14586 high = range_end;
14587 low_set = 1;
14588 }
14589 else
14590 {
14591 if (range_beginning < low)
14592 low = range_beginning;
14593 if (range_end > high)
14594 high = range_end;
14595 }
14596 });
14597 if (!retval)
14598 return 0;
14599
14600 if (! low_set)
14601 /* If the first entry is an end-of-list marker, the range
14602 describes an empty scope, i.e. no instructions. */
14603 return 0;
14604
14605 if (low_return)
14606 *low_return = low;
14607 if (high_return)
14608 *high_return = high;
14609 return 1;
14610 }
14611
14612 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14613 definition for the return value. *LOWPC and *HIGHPC are set iff
14614 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14615
14616 static enum pc_bounds_kind
14617 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14618 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14619 dwarf2_psymtab *pst)
14620 {
14621 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14622 struct attribute *attr;
14623 struct attribute *attr_high;
14624 CORE_ADDR low = 0;
14625 CORE_ADDR high = 0;
14626 enum pc_bounds_kind ret;
14627
14628 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14629 if (attr_high)
14630 {
14631 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14632 if (attr != nullptr)
14633 {
14634 low = attr->as_address ();
14635 high = attr_high->as_address ();
14636 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14637 high += low;
14638 }
14639 else
14640 /* Found high w/o low attribute. */
14641 return PC_BOUNDS_INVALID;
14642
14643 /* Found consecutive range of addresses. */
14644 ret = PC_BOUNDS_HIGH_LOW;
14645 }
14646 else
14647 {
14648 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14649 if (attr != nullptr && attr->form_is_unsigned ())
14650 {
14651 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14652 on DWARF version). */
14653 ULONGEST ranges_offset = attr->as_unsigned ();
14654
14655 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14656 this value. */
14657 if (die->tag != DW_TAG_compile_unit)
14658 ranges_offset += cu->gnu_ranges_base;
14659
14660 /* Value of the DW_AT_ranges attribute is the offset in the
14661 .debug_ranges section. */
14662 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14663 die->tag))
14664 return PC_BOUNDS_INVALID;
14665 /* Found discontinuous range of addresses. */
14666 ret = PC_BOUNDS_RANGES;
14667 }
14668 else
14669 return PC_BOUNDS_NOT_PRESENT;
14670 }
14671
14672 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14673 if (high <= low)
14674 return PC_BOUNDS_INVALID;
14675
14676 /* When using the GNU linker, .gnu.linkonce. sections are used to
14677 eliminate duplicate copies of functions and vtables and such.
14678 The linker will arbitrarily choose one and discard the others.
14679 The AT_*_pc values for such functions refer to local labels in
14680 these sections. If the section from that file was discarded, the
14681 labels are not in the output, so the relocs get a value of 0.
14682 If this is a discarded function, mark the pc bounds as invalid,
14683 so that GDB will ignore it. */
14684 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14685 return PC_BOUNDS_INVALID;
14686
14687 *lowpc = low;
14688 if (highpc)
14689 *highpc = high;
14690 return ret;
14691 }
14692
14693 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14694 its low and high PC addresses. Do nothing if these addresses could not
14695 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14696 and HIGHPC to the high address if greater than HIGHPC. */
14697
14698 static void
14699 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14700 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14701 struct dwarf2_cu *cu)
14702 {
14703 CORE_ADDR low, high;
14704 struct die_info *child = die->child;
14705
14706 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14707 {
14708 *lowpc = std::min (*lowpc, low);
14709 *highpc = std::max (*highpc, high);
14710 }
14711
14712 /* If the language does not allow nested subprograms (either inside
14713 subprograms or lexical blocks), we're done. */
14714 if (cu->language != language_ada)
14715 return;
14716
14717 /* Check all the children of the given DIE. If it contains nested
14718 subprograms, then check their pc bounds. Likewise, we need to
14719 check lexical blocks as well, as they may also contain subprogram
14720 definitions. */
14721 while (child && child->tag)
14722 {
14723 if (child->tag == DW_TAG_subprogram
14724 || child->tag == DW_TAG_lexical_block)
14725 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14726 child = child->sibling;
14727 }
14728 }
14729
14730 /* Get the low and high pc's represented by the scope DIE, and store
14731 them in *LOWPC and *HIGHPC. If the correct values can't be
14732 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14733
14734 static void
14735 get_scope_pc_bounds (struct die_info *die,
14736 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14737 struct dwarf2_cu *cu)
14738 {
14739 CORE_ADDR best_low = (CORE_ADDR) -1;
14740 CORE_ADDR best_high = (CORE_ADDR) 0;
14741 CORE_ADDR current_low, current_high;
14742
14743 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14744 >= PC_BOUNDS_RANGES)
14745 {
14746 best_low = current_low;
14747 best_high = current_high;
14748 }
14749 else
14750 {
14751 struct die_info *child = die->child;
14752
14753 while (child && child->tag)
14754 {
14755 switch (child->tag) {
14756 case DW_TAG_subprogram:
14757 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14758 break;
14759 case DW_TAG_namespace:
14760 case DW_TAG_module:
14761 /* FIXME: carlton/2004-01-16: Should we do this for
14762 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14763 that current GCC's always emit the DIEs corresponding
14764 to definitions of methods of classes as children of a
14765 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14766 the DIEs giving the declarations, which could be
14767 anywhere). But I don't see any reason why the
14768 standards says that they have to be there. */
14769 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14770
14771 if (current_low != ((CORE_ADDR) -1))
14772 {
14773 best_low = std::min (best_low, current_low);
14774 best_high = std::max (best_high, current_high);
14775 }
14776 break;
14777 default:
14778 /* Ignore. */
14779 break;
14780 }
14781
14782 child = child->sibling;
14783 }
14784 }
14785
14786 *lowpc = best_low;
14787 *highpc = best_high;
14788 }
14789
14790 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14791 in DIE. */
14792
14793 static void
14794 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14795 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14796 {
14797 struct objfile *objfile = cu->per_objfile->objfile;
14798 struct gdbarch *gdbarch = objfile->arch ();
14799 struct attribute *attr;
14800 struct attribute *attr_high;
14801
14802 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14803 if (attr_high)
14804 {
14805 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14806 if (attr != nullptr)
14807 {
14808 CORE_ADDR low = attr->as_address ();
14809 CORE_ADDR high = attr_high->as_address ();
14810
14811 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14812 high += low;
14813
14814 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14815 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14816 cu->get_builder ()->record_block_range (block, low, high - 1);
14817 }
14818 }
14819
14820 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14821 if (attr != nullptr && attr->form_is_unsigned ())
14822 {
14823 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14824 on DWARF version). */
14825 ULONGEST ranges_offset = attr->as_unsigned ();
14826
14827 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14828 this value. */
14829 if (die->tag != DW_TAG_compile_unit)
14830 ranges_offset += cu->gnu_ranges_base;
14831
14832 std::vector<blockrange> blockvec;
14833 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14834 [&] (CORE_ADDR start, CORE_ADDR end)
14835 {
14836 start += baseaddr;
14837 end += baseaddr;
14838 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14839 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14840 cu->get_builder ()->record_block_range (block, start, end - 1);
14841 blockvec.emplace_back (start, end);
14842 });
14843
14844 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14845 }
14846 }
14847
14848 /* Check whether the producer field indicates either of GCC < 4.6, or the
14849 Intel C/C++ compiler, and cache the result in CU. */
14850
14851 static void
14852 check_producer (struct dwarf2_cu *cu)
14853 {
14854 int major, minor;
14855
14856 if (cu->producer == NULL)
14857 {
14858 /* For unknown compilers expect their behavior is DWARF version
14859 compliant.
14860
14861 GCC started to support .debug_types sections by -gdwarf-4 since
14862 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14863 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14864 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14865 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14866 }
14867 else if (producer_is_gcc (cu->producer, &major, &minor))
14868 {
14869 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14870 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14871 }
14872 else if (producer_is_icc (cu->producer, &major, &minor))
14873 {
14874 cu->producer_is_icc = true;
14875 cu->producer_is_icc_lt_14 = major < 14;
14876 }
14877 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14878 cu->producer_is_codewarrior = true;
14879 else
14880 {
14881 /* For other non-GCC compilers, expect their behavior is DWARF version
14882 compliant. */
14883 }
14884
14885 cu->checked_producer = true;
14886 }
14887
14888 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14889 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14890 during 4.6.0 experimental. */
14891
14892 static bool
14893 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14894 {
14895 if (!cu->checked_producer)
14896 check_producer (cu);
14897
14898 return cu->producer_is_gxx_lt_4_6;
14899 }
14900
14901
14902 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14903 with incorrect is_stmt attributes. */
14904
14905 static bool
14906 producer_is_codewarrior (struct dwarf2_cu *cu)
14907 {
14908 if (!cu->checked_producer)
14909 check_producer (cu);
14910
14911 return cu->producer_is_codewarrior;
14912 }
14913
14914 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14915 If that attribute is not available, return the appropriate
14916 default. */
14917
14918 static enum dwarf_access_attribute
14919 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14920 {
14921 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14922 if (attr != nullptr)
14923 {
14924 LONGEST value = attr->constant_value (-1);
14925 if (value == DW_ACCESS_public
14926 || value == DW_ACCESS_protected
14927 || value == DW_ACCESS_private)
14928 return (dwarf_access_attribute) value;
14929 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14930 plongest (value));
14931 }
14932
14933 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14934 {
14935 /* The default DWARF 2 accessibility for members is public, the default
14936 accessibility for inheritance is private. */
14937
14938 if (die->tag != DW_TAG_inheritance)
14939 return DW_ACCESS_public;
14940 else
14941 return DW_ACCESS_private;
14942 }
14943 else
14944 {
14945 /* DWARF 3+ defines the default accessibility a different way. The same
14946 rules apply now for DW_TAG_inheritance as for the members and it only
14947 depends on the container kind. */
14948
14949 if (die->parent->tag == DW_TAG_class_type)
14950 return DW_ACCESS_private;
14951 else
14952 return DW_ACCESS_public;
14953 }
14954 }
14955
14956 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14957 offset. If the attribute was not found return 0, otherwise return
14958 1. If it was found but could not properly be handled, set *OFFSET
14959 to 0. */
14960
14961 static int
14962 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14963 LONGEST *offset)
14964 {
14965 struct attribute *attr;
14966
14967 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14968 if (attr != NULL)
14969 {
14970 *offset = 0;
14971
14972 /* Note that we do not check for a section offset first here.
14973 This is because DW_AT_data_member_location is new in DWARF 4,
14974 so if we see it, we can assume that a constant form is really
14975 a constant and not a section offset. */
14976 if (attr->form_is_constant ())
14977 *offset = attr->constant_value (0);
14978 else if (attr->form_is_section_offset ())
14979 dwarf2_complex_location_expr_complaint ();
14980 else if (attr->form_is_block ())
14981 *offset = decode_locdesc (attr->as_block (), cu);
14982 else
14983 dwarf2_complex_location_expr_complaint ();
14984
14985 return 1;
14986 }
14987
14988 return 0;
14989 }
14990
14991 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14992
14993 static void
14994 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14995 struct field *field)
14996 {
14997 struct attribute *attr;
14998
14999 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15000 if (attr != NULL)
15001 {
15002 if (attr->form_is_constant ())
15003 {
15004 LONGEST offset = attr->constant_value (0);
15005 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15006 }
15007 else if (attr->form_is_section_offset ())
15008 dwarf2_complex_location_expr_complaint ();
15009 else if (attr->form_is_block ())
15010 {
15011 bool handled;
15012 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
15013 if (handled)
15014 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15015 else
15016 {
15017 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15018 struct objfile *objfile = per_objfile->objfile;
15019 struct dwarf2_locexpr_baton *dlbaton
15020 = XOBNEW (&objfile->objfile_obstack,
15021 struct dwarf2_locexpr_baton);
15022 dlbaton->data = attr->as_block ()->data;
15023 dlbaton->size = attr->as_block ()->size;
15024 /* When using this baton, we want to compute the address
15025 of the field, not the value. This is why
15026 is_reference is set to false here. */
15027 dlbaton->is_reference = false;
15028 dlbaton->per_objfile = per_objfile;
15029 dlbaton->per_cu = cu->per_cu;
15030
15031 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15032 }
15033 }
15034 else
15035 dwarf2_complex_location_expr_complaint ();
15036 }
15037 }
15038
15039 /* Add an aggregate field to the field list. */
15040
15041 static void
15042 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15043 struct dwarf2_cu *cu)
15044 {
15045 struct objfile *objfile = cu->per_objfile->objfile;
15046 struct gdbarch *gdbarch = objfile->arch ();
15047 struct nextfield *new_field;
15048 struct attribute *attr;
15049 struct field *fp;
15050 const char *fieldname = "";
15051
15052 if (die->tag == DW_TAG_inheritance)
15053 {
15054 fip->baseclasses.emplace_back ();
15055 new_field = &fip->baseclasses.back ();
15056 }
15057 else
15058 {
15059 fip->fields.emplace_back ();
15060 new_field = &fip->fields.back ();
15061 }
15062
15063 new_field->offset = die->sect_off;
15064
15065 new_field->accessibility = dwarf2_access_attribute (die, cu);
15066 if (new_field->accessibility != DW_ACCESS_public)
15067 fip->non_public_fields = true;
15068
15069 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15070 if (attr != nullptr)
15071 new_field->virtuality = attr->as_virtuality ();
15072 else
15073 new_field->virtuality = DW_VIRTUALITY_none;
15074
15075 fp = &new_field->field;
15076
15077 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15078 {
15079 /* Data member other than a C++ static data member. */
15080
15081 /* Get type of field. */
15082 fp->set_type (die_type (die, cu));
15083
15084 SET_FIELD_BITPOS (*fp, 0);
15085
15086 /* Get bit size of field (zero if none). */
15087 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15088 if (attr != nullptr)
15089 {
15090 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15091 }
15092 else
15093 {
15094 FIELD_BITSIZE (*fp) = 0;
15095 }
15096
15097 /* Get bit offset of field. */
15098 handle_data_member_location (die, cu, fp);
15099 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15100 if (attr != nullptr && attr->form_is_constant ())
15101 {
15102 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15103 {
15104 /* For big endian bits, the DW_AT_bit_offset gives the
15105 additional bit offset from the MSB of the containing
15106 anonymous object to the MSB of the field. We don't
15107 have to do anything special since we don't need to
15108 know the size of the anonymous object. */
15109 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15110 + attr->constant_value (0)));
15111 }
15112 else
15113 {
15114 /* For little endian bits, compute the bit offset to the
15115 MSB of the anonymous object, subtract off the number of
15116 bits from the MSB of the field to the MSB of the
15117 object, and then subtract off the number of bits of
15118 the field itself. The result is the bit offset of
15119 the LSB of the field. */
15120 int anonymous_size;
15121 int bit_offset = attr->constant_value (0);
15122
15123 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15124 if (attr != nullptr && attr->form_is_constant ())
15125 {
15126 /* The size of the anonymous object containing
15127 the bit field is explicit, so use the
15128 indicated size (in bytes). */
15129 anonymous_size = attr->constant_value (0);
15130 }
15131 else
15132 {
15133 /* The size of the anonymous object containing
15134 the bit field must be inferred from the type
15135 attribute of the data member containing the
15136 bit field. */
15137 anonymous_size = TYPE_LENGTH (fp->type ());
15138 }
15139 SET_FIELD_BITPOS (*fp,
15140 (FIELD_BITPOS (*fp)
15141 + anonymous_size * bits_per_byte
15142 - bit_offset - FIELD_BITSIZE (*fp)));
15143 }
15144 }
15145 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15146 if (attr != NULL)
15147 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15148 + attr->constant_value (0)));
15149
15150 /* Get name of field. */
15151 fieldname = dwarf2_name (die, cu);
15152 if (fieldname == NULL)
15153 fieldname = "";
15154
15155 /* The name is already allocated along with this objfile, so we don't
15156 need to duplicate it for the type. */
15157 fp->name = fieldname;
15158
15159 /* Change accessibility for artificial fields (e.g. virtual table
15160 pointer or virtual base class pointer) to private. */
15161 if (dwarf2_attr (die, DW_AT_artificial, cu))
15162 {
15163 FIELD_ARTIFICIAL (*fp) = 1;
15164 new_field->accessibility = DW_ACCESS_private;
15165 fip->non_public_fields = true;
15166 }
15167 }
15168 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15169 {
15170 /* C++ static member. */
15171
15172 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15173 is a declaration, but all versions of G++ as of this writing
15174 (so through at least 3.2.1) incorrectly generate
15175 DW_TAG_variable tags. */
15176
15177 const char *physname;
15178
15179 /* Get name of field. */
15180 fieldname = dwarf2_name (die, cu);
15181 if (fieldname == NULL)
15182 return;
15183
15184 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15185 if (attr
15186 /* Only create a symbol if this is an external value.
15187 new_symbol checks this and puts the value in the global symbol
15188 table, which we want. If it is not external, new_symbol
15189 will try to put the value in cu->list_in_scope which is wrong. */
15190 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15191 {
15192 /* A static const member, not much different than an enum as far as
15193 we're concerned, except that we can support more types. */
15194 new_symbol (die, NULL, cu);
15195 }
15196
15197 /* Get physical name. */
15198 physname = dwarf2_physname (fieldname, die, cu);
15199
15200 /* The name is already allocated along with this objfile, so we don't
15201 need to duplicate it for the type. */
15202 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15203 fp->set_type (die_type (die, cu));
15204 FIELD_NAME (*fp) = fieldname;
15205 }
15206 else if (die->tag == DW_TAG_inheritance)
15207 {
15208 /* C++ base class field. */
15209 handle_data_member_location (die, cu, fp);
15210 FIELD_BITSIZE (*fp) = 0;
15211 fp->set_type (die_type (die, cu));
15212 FIELD_NAME (*fp) = fp->type ()->name ();
15213 }
15214 else
15215 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15216 }
15217
15218 /* Can the type given by DIE define another type? */
15219
15220 static bool
15221 type_can_define_types (const struct die_info *die)
15222 {
15223 switch (die->tag)
15224 {
15225 case DW_TAG_typedef:
15226 case DW_TAG_class_type:
15227 case DW_TAG_structure_type:
15228 case DW_TAG_union_type:
15229 case DW_TAG_enumeration_type:
15230 return true;
15231
15232 default:
15233 return false;
15234 }
15235 }
15236
15237 /* Add a type definition defined in the scope of the FIP's class. */
15238
15239 static void
15240 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15241 struct dwarf2_cu *cu)
15242 {
15243 struct decl_field fp;
15244 memset (&fp, 0, sizeof (fp));
15245
15246 gdb_assert (type_can_define_types (die));
15247
15248 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15249 fp.name = dwarf2_name (die, cu);
15250 fp.type = read_type_die (die, cu);
15251
15252 /* Save accessibility. */
15253 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15254 switch (accessibility)
15255 {
15256 case DW_ACCESS_public:
15257 /* The assumed value if neither private nor protected. */
15258 break;
15259 case DW_ACCESS_private:
15260 fp.is_private = 1;
15261 break;
15262 case DW_ACCESS_protected:
15263 fp.is_protected = 1;
15264 break;
15265 }
15266
15267 if (die->tag == DW_TAG_typedef)
15268 fip->typedef_field_list.push_back (fp);
15269 else
15270 fip->nested_types_list.push_back (fp);
15271 }
15272
15273 /* A convenience typedef that's used when finding the discriminant
15274 field for a variant part. */
15275 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15276 offset_map_type;
15277
15278 /* Compute the discriminant range for a given variant. OBSTACK is
15279 where the results will be stored. VARIANT is the variant to
15280 process. IS_UNSIGNED indicates whether the discriminant is signed
15281 or unsigned. */
15282
15283 static const gdb::array_view<discriminant_range>
15284 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15285 bool is_unsigned)
15286 {
15287 std::vector<discriminant_range> ranges;
15288
15289 if (variant.default_branch)
15290 return {};
15291
15292 if (variant.discr_list_data == nullptr)
15293 {
15294 discriminant_range r
15295 = {variant.discriminant_value, variant.discriminant_value};
15296 ranges.push_back (r);
15297 }
15298 else
15299 {
15300 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15301 variant.discr_list_data->size);
15302 while (!data.empty ())
15303 {
15304 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15305 {
15306 complaint (_("invalid discriminant marker: %d"), data[0]);
15307 break;
15308 }
15309 bool is_range = data[0] == DW_DSC_range;
15310 data = data.slice (1);
15311
15312 ULONGEST low, high;
15313 unsigned int bytes_read;
15314
15315 if (data.empty ())
15316 {
15317 complaint (_("DW_AT_discr_list missing low value"));
15318 break;
15319 }
15320 if (is_unsigned)
15321 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15322 else
15323 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15324 &bytes_read);
15325 data = data.slice (bytes_read);
15326
15327 if (is_range)
15328 {
15329 if (data.empty ())
15330 {
15331 complaint (_("DW_AT_discr_list missing high value"));
15332 break;
15333 }
15334 if (is_unsigned)
15335 high = read_unsigned_leb128 (nullptr, data.data (),
15336 &bytes_read);
15337 else
15338 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15339 &bytes_read);
15340 data = data.slice (bytes_read);
15341 }
15342 else
15343 high = low;
15344
15345 ranges.push_back ({ low, high });
15346 }
15347 }
15348
15349 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15350 ranges.size ());
15351 std::copy (ranges.begin (), ranges.end (), result);
15352 return gdb::array_view<discriminant_range> (result, ranges.size ());
15353 }
15354
15355 static const gdb::array_view<variant_part> create_variant_parts
15356 (struct obstack *obstack,
15357 const offset_map_type &offset_map,
15358 struct field_info *fi,
15359 const std::vector<variant_part_builder> &variant_parts);
15360
15361 /* Fill in a "struct variant" for a given variant field. RESULT is
15362 the variant to fill in. OBSTACK is where any needed allocations
15363 will be done. OFFSET_MAP holds the mapping from section offsets to
15364 fields for the type. FI describes the fields of the type we're
15365 processing. FIELD is the variant field we're converting. */
15366
15367 static void
15368 create_one_variant (variant &result, struct obstack *obstack,
15369 const offset_map_type &offset_map,
15370 struct field_info *fi, const variant_field &field)
15371 {
15372 result.discriminants = convert_variant_range (obstack, field, false);
15373 result.first_field = field.first_field + fi->baseclasses.size ();
15374 result.last_field = field.last_field + fi->baseclasses.size ();
15375 result.parts = create_variant_parts (obstack, offset_map, fi,
15376 field.variant_parts);
15377 }
15378
15379 /* Fill in a "struct variant_part" for a given variant part. RESULT
15380 is the variant part to fill in. OBSTACK is where any needed
15381 allocations will be done. OFFSET_MAP holds the mapping from
15382 section offsets to fields for the type. FI describes the fields of
15383 the type we're processing. BUILDER is the variant part to be
15384 converted. */
15385
15386 static void
15387 create_one_variant_part (variant_part &result,
15388 struct obstack *obstack,
15389 const offset_map_type &offset_map,
15390 struct field_info *fi,
15391 const variant_part_builder &builder)
15392 {
15393 auto iter = offset_map.find (builder.discriminant_offset);
15394 if (iter == offset_map.end ())
15395 {
15396 result.discriminant_index = -1;
15397 /* Doesn't matter. */
15398 result.is_unsigned = false;
15399 }
15400 else
15401 {
15402 result.discriminant_index = iter->second;
15403 result.is_unsigned
15404 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15405 }
15406
15407 size_t n = builder.variants.size ();
15408 variant *output = new (obstack) variant[n];
15409 for (size_t i = 0; i < n; ++i)
15410 create_one_variant (output[i], obstack, offset_map, fi,
15411 builder.variants[i]);
15412
15413 result.variants = gdb::array_view<variant> (output, n);
15414 }
15415
15416 /* Create a vector of variant parts that can be attached to a type.
15417 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15418 holds the mapping from section offsets to fields for the type. FI
15419 describes the fields of the type we're processing. VARIANT_PARTS
15420 is the vector to convert. */
15421
15422 static const gdb::array_view<variant_part>
15423 create_variant_parts (struct obstack *obstack,
15424 const offset_map_type &offset_map,
15425 struct field_info *fi,
15426 const std::vector<variant_part_builder> &variant_parts)
15427 {
15428 if (variant_parts.empty ())
15429 return {};
15430
15431 size_t n = variant_parts.size ();
15432 variant_part *result = new (obstack) variant_part[n];
15433 for (size_t i = 0; i < n; ++i)
15434 create_one_variant_part (result[i], obstack, offset_map, fi,
15435 variant_parts[i]);
15436
15437 return gdb::array_view<variant_part> (result, n);
15438 }
15439
15440 /* Compute the variant part vector for FIP, attaching it to TYPE when
15441 done. */
15442
15443 static void
15444 add_variant_property (struct field_info *fip, struct type *type,
15445 struct dwarf2_cu *cu)
15446 {
15447 /* Map section offsets of fields to their field index. Note the
15448 field index here does not take the number of baseclasses into
15449 account. */
15450 offset_map_type offset_map;
15451 for (int i = 0; i < fip->fields.size (); ++i)
15452 offset_map[fip->fields[i].offset] = i;
15453
15454 struct objfile *objfile = cu->per_objfile->objfile;
15455 gdb::array_view<variant_part> parts
15456 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15457 fip->variant_parts);
15458
15459 struct dynamic_prop prop;
15460 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15461 obstack_copy (&objfile->objfile_obstack, &parts,
15462 sizeof (parts)));
15463
15464 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15465 }
15466
15467 /* Create the vector of fields, and attach it to the type. */
15468
15469 static void
15470 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15471 struct dwarf2_cu *cu)
15472 {
15473 int nfields = fip->nfields ();
15474
15475 /* Record the field count, allocate space for the array of fields,
15476 and create blank accessibility bitfields if necessary. */
15477 type->set_num_fields (nfields);
15478 type->set_fields
15479 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15480
15481 if (fip->non_public_fields && cu->language != language_ada)
15482 {
15483 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15484
15485 TYPE_FIELD_PRIVATE_BITS (type) =
15486 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15487 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15488
15489 TYPE_FIELD_PROTECTED_BITS (type) =
15490 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15491 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15492
15493 TYPE_FIELD_IGNORE_BITS (type) =
15494 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15495 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15496 }
15497
15498 /* If the type has baseclasses, allocate and clear a bit vector for
15499 TYPE_FIELD_VIRTUAL_BITS. */
15500 if (!fip->baseclasses.empty () && cu->language != language_ada)
15501 {
15502 int num_bytes = B_BYTES (fip->baseclasses.size ());
15503 unsigned char *pointer;
15504
15505 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15506 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15507 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15508 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15509 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15510 }
15511
15512 if (!fip->variant_parts.empty ())
15513 add_variant_property (fip, type, cu);
15514
15515 /* Copy the saved-up fields into the field vector. */
15516 for (int i = 0; i < nfields; ++i)
15517 {
15518 struct nextfield &field
15519 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15520 : fip->fields[i - fip->baseclasses.size ()]);
15521
15522 type->field (i) = field.field;
15523 switch (field.accessibility)
15524 {
15525 case DW_ACCESS_private:
15526 if (cu->language != language_ada)
15527 SET_TYPE_FIELD_PRIVATE (type, i);
15528 break;
15529
15530 case DW_ACCESS_protected:
15531 if (cu->language != language_ada)
15532 SET_TYPE_FIELD_PROTECTED (type, i);
15533 break;
15534
15535 case DW_ACCESS_public:
15536 break;
15537
15538 default:
15539 /* Unknown accessibility. Complain and treat it as public. */
15540 {
15541 complaint (_("unsupported accessibility %d"),
15542 field.accessibility);
15543 }
15544 break;
15545 }
15546 if (i < fip->baseclasses.size ())
15547 {
15548 switch (field.virtuality)
15549 {
15550 case DW_VIRTUALITY_virtual:
15551 case DW_VIRTUALITY_pure_virtual:
15552 if (cu->language == language_ada)
15553 error (_("unexpected virtuality in component of Ada type"));
15554 SET_TYPE_FIELD_VIRTUAL (type, i);
15555 break;
15556 }
15557 }
15558 }
15559 }
15560
15561 /* Return true if this member function is a constructor, false
15562 otherwise. */
15563
15564 static int
15565 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15566 {
15567 const char *fieldname;
15568 const char *type_name;
15569 int len;
15570
15571 if (die->parent == NULL)
15572 return 0;
15573
15574 if (die->parent->tag != DW_TAG_structure_type
15575 && die->parent->tag != DW_TAG_union_type
15576 && die->parent->tag != DW_TAG_class_type)
15577 return 0;
15578
15579 fieldname = dwarf2_name (die, cu);
15580 type_name = dwarf2_name (die->parent, cu);
15581 if (fieldname == NULL || type_name == NULL)
15582 return 0;
15583
15584 len = strlen (fieldname);
15585 return (strncmp (fieldname, type_name, len) == 0
15586 && (type_name[len] == '\0' || type_name[len] == '<'));
15587 }
15588
15589 /* Add a member function to the proper fieldlist. */
15590
15591 static void
15592 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15593 struct type *type, struct dwarf2_cu *cu)
15594 {
15595 struct objfile *objfile = cu->per_objfile->objfile;
15596 struct attribute *attr;
15597 int i;
15598 struct fnfieldlist *flp = nullptr;
15599 struct fn_field *fnp;
15600 const char *fieldname;
15601 struct type *this_type;
15602
15603 if (cu->language == language_ada)
15604 error (_("unexpected member function in Ada type"));
15605
15606 /* Get name of member function. */
15607 fieldname = dwarf2_name (die, cu);
15608 if (fieldname == NULL)
15609 return;
15610
15611 /* Look up member function name in fieldlist. */
15612 for (i = 0; i < fip->fnfieldlists.size (); i++)
15613 {
15614 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15615 {
15616 flp = &fip->fnfieldlists[i];
15617 break;
15618 }
15619 }
15620
15621 /* Create a new fnfieldlist if necessary. */
15622 if (flp == nullptr)
15623 {
15624 fip->fnfieldlists.emplace_back ();
15625 flp = &fip->fnfieldlists.back ();
15626 flp->name = fieldname;
15627 i = fip->fnfieldlists.size () - 1;
15628 }
15629
15630 /* Create a new member function field and add it to the vector of
15631 fnfieldlists. */
15632 flp->fnfields.emplace_back ();
15633 fnp = &flp->fnfields.back ();
15634
15635 /* Delay processing of the physname until later. */
15636 if (cu->language == language_cplus)
15637 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15638 die, cu);
15639 else
15640 {
15641 const char *physname = dwarf2_physname (fieldname, die, cu);
15642 fnp->physname = physname ? physname : "";
15643 }
15644
15645 fnp->type = alloc_type (objfile);
15646 this_type = read_type_die (die, cu);
15647 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15648 {
15649 int nparams = this_type->num_fields ();
15650
15651 /* TYPE is the domain of this method, and THIS_TYPE is the type
15652 of the method itself (TYPE_CODE_METHOD). */
15653 smash_to_method_type (fnp->type, type,
15654 TYPE_TARGET_TYPE (this_type),
15655 this_type->fields (),
15656 this_type->num_fields (),
15657 this_type->has_varargs ());
15658
15659 /* Handle static member functions.
15660 Dwarf2 has no clean way to discern C++ static and non-static
15661 member functions. G++ helps GDB by marking the first
15662 parameter for non-static member functions (which is the this
15663 pointer) as artificial. We obtain this information from
15664 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15665 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15666 fnp->voffset = VOFFSET_STATIC;
15667 }
15668 else
15669 complaint (_("member function type missing for '%s'"),
15670 dwarf2_full_name (fieldname, die, cu));
15671
15672 /* Get fcontext from DW_AT_containing_type if present. */
15673 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15674 fnp->fcontext = die_containing_type (die, cu);
15675
15676 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15677 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15678
15679 /* Get accessibility. */
15680 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15681 switch (accessibility)
15682 {
15683 case DW_ACCESS_private:
15684 fnp->is_private = 1;
15685 break;
15686 case DW_ACCESS_protected:
15687 fnp->is_protected = 1;
15688 break;
15689 }
15690
15691 /* Check for artificial methods. */
15692 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15693 if (attr && attr->as_boolean ())
15694 fnp->is_artificial = 1;
15695
15696 /* Check for defaulted methods. */
15697 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15698 if (attr != nullptr)
15699 fnp->defaulted = attr->defaulted ();
15700
15701 /* Check for deleted methods. */
15702 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15703 if (attr != nullptr && attr->as_boolean ())
15704 fnp->is_deleted = 1;
15705
15706 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15707
15708 /* Get index in virtual function table if it is a virtual member
15709 function. For older versions of GCC, this is an offset in the
15710 appropriate virtual table, as specified by DW_AT_containing_type.
15711 For everyone else, it is an expression to be evaluated relative
15712 to the object address. */
15713
15714 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15715 if (attr != nullptr)
15716 {
15717 if (attr->form_is_block () && attr->as_block ()->size > 0)
15718 {
15719 struct dwarf_block *block = attr->as_block ();
15720
15721 if (block->data[0] == DW_OP_constu)
15722 {
15723 /* Old-style GCC. */
15724 fnp->voffset = decode_locdesc (block, cu) + 2;
15725 }
15726 else if (block->data[0] == DW_OP_deref
15727 || (block->size > 1
15728 && block->data[0] == DW_OP_deref_size
15729 && block->data[1] == cu->header.addr_size))
15730 {
15731 fnp->voffset = decode_locdesc (block, cu);
15732 if ((fnp->voffset % cu->header.addr_size) != 0)
15733 dwarf2_complex_location_expr_complaint ();
15734 else
15735 fnp->voffset /= cu->header.addr_size;
15736 fnp->voffset += 2;
15737 }
15738 else
15739 dwarf2_complex_location_expr_complaint ();
15740
15741 if (!fnp->fcontext)
15742 {
15743 /* If there is no `this' field and no DW_AT_containing_type,
15744 we cannot actually find a base class context for the
15745 vtable! */
15746 if (this_type->num_fields () == 0
15747 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15748 {
15749 complaint (_("cannot determine context for virtual member "
15750 "function \"%s\" (offset %s)"),
15751 fieldname, sect_offset_str (die->sect_off));
15752 }
15753 else
15754 {
15755 fnp->fcontext
15756 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15757 }
15758 }
15759 }
15760 else if (attr->form_is_section_offset ())
15761 {
15762 dwarf2_complex_location_expr_complaint ();
15763 }
15764 else
15765 {
15766 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15767 fieldname);
15768 }
15769 }
15770 else
15771 {
15772 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15773 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15774 {
15775 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15776 complaint (_("Member function \"%s\" (offset %s) is virtual "
15777 "but the vtable offset is not specified"),
15778 fieldname, sect_offset_str (die->sect_off));
15779 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15780 TYPE_CPLUS_DYNAMIC (type) = 1;
15781 }
15782 }
15783 }
15784
15785 /* Create the vector of member function fields, and attach it to the type. */
15786
15787 static void
15788 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15789 struct dwarf2_cu *cu)
15790 {
15791 if (cu->language == language_ada)
15792 error (_("unexpected member functions in Ada type"));
15793
15794 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15795 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15796 TYPE_ALLOC (type,
15797 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15798
15799 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15800 {
15801 struct fnfieldlist &nf = fip->fnfieldlists[i];
15802 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15803
15804 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15805 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15806 fn_flp->fn_fields = (struct fn_field *)
15807 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15808
15809 for (int k = 0; k < nf.fnfields.size (); ++k)
15810 fn_flp->fn_fields[k] = nf.fnfields[k];
15811 }
15812
15813 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15814 }
15815
15816 /* Returns non-zero if NAME is the name of a vtable member in CU's
15817 language, zero otherwise. */
15818 static int
15819 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15820 {
15821 static const char vptr[] = "_vptr";
15822
15823 /* Look for the C++ form of the vtable. */
15824 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15825 return 1;
15826
15827 return 0;
15828 }
15829
15830 /* GCC outputs unnamed structures that are really pointers to member
15831 functions, with the ABI-specified layout. If TYPE describes
15832 such a structure, smash it into a member function type.
15833
15834 GCC shouldn't do this; it should just output pointer to member DIEs.
15835 This is GCC PR debug/28767. */
15836
15837 static void
15838 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15839 {
15840 struct type *pfn_type, *self_type, *new_type;
15841
15842 /* Check for a structure with no name and two children. */
15843 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15844 return;
15845
15846 /* Check for __pfn and __delta members. */
15847 if (TYPE_FIELD_NAME (type, 0) == NULL
15848 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15849 || TYPE_FIELD_NAME (type, 1) == NULL
15850 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15851 return;
15852
15853 /* Find the type of the method. */
15854 pfn_type = type->field (0).type ();
15855 if (pfn_type == NULL
15856 || pfn_type->code () != TYPE_CODE_PTR
15857 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15858 return;
15859
15860 /* Look for the "this" argument. */
15861 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15862 if (pfn_type->num_fields () == 0
15863 /* || pfn_type->field (0).type () == NULL */
15864 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15865 return;
15866
15867 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15868 new_type = alloc_type (objfile);
15869 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15870 pfn_type->fields (), pfn_type->num_fields (),
15871 pfn_type->has_varargs ());
15872 smash_to_methodptr_type (type, new_type);
15873 }
15874
15875 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15876 requires rewriting, then copy it and return the updated copy.
15877 Otherwise return nullptr. */
15878
15879 static struct type *
15880 rewrite_array_type (struct type *type)
15881 {
15882 if (type->code () != TYPE_CODE_ARRAY)
15883 return nullptr;
15884
15885 struct type *index_type = type->index_type ();
15886 range_bounds *current_bounds = index_type->bounds ();
15887
15888 /* Handle multi-dimensional arrays. */
15889 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15890 if (new_target == nullptr)
15891 {
15892 /* Maybe we don't need to rewrite this array. */
15893 if (current_bounds->low.kind () == PROP_CONST
15894 && current_bounds->high.kind () == PROP_CONST)
15895 return nullptr;
15896 }
15897
15898 /* Either the target type was rewritten, or the bounds have to be
15899 updated. Either way we want to copy the type and update
15900 everything. */
15901 struct type *copy = copy_type (type);
15902 int nfields = copy->num_fields ();
15903 field *new_fields
15904 = ((struct field *) TYPE_ZALLOC (copy,
15905 nfields * sizeof (struct field)));
15906 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15907 copy->set_fields (new_fields);
15908 if (new_target != nullptr)
15909 TYPE_TARGET_TYPE (copy) = new_target;
15910
15911 struct type *index_copy = copy_type (index_type);
15912 range_bounds *bounds
15913 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15914 sizeof (range_bounds));
15915 *bounds = *current_bounds;
15916 bounds->low.set_const_val (1);
15917 bounds->high.set_const_val (0);
15918 index_copy->set_bounds (bounds);
15919 copy->set_index_type (index_copy);
15920
15921 return copy;
15922 }
15923
15924 /* While some versions of GCC will generate complicated DWARF for an
15925 array (see quirk_ada_thick_pointer), more recent versions were
15926 modified to emit an explicit thick pointer structure. However, in
15927 this case, the array still has DWARF expressions for its ranges,
15928 and these must be ignored. */
15929
15930 static void
15931 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15932 struct type *type)
15933 {
15934 gdb_assert (cu->language == language_ada);
15935
15936 /* Check for a structure with two children. */
15937 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15938 return;
15939
15940 /* Check for P_ARRAY and P_BOUNDS members. */
15941 if (TYPE_FIELD_NAME (type, 0) == NULL
15942 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15943 || TYPE_FIELD_NAME (type, 1) == NULL
15944 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15945 return;
15946
15947 /* Make sure we're looking at a pointer to an array. */
15948 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15949 return;
15950
15951 /* The Ada code already knows how to handle these types, so all that
15952 we need to do is turn the bounds into static bounds. However, we
15953 don't want to rewrite existing array or index types in-place,
15954 because those may be referenced in other contexts where this
15955 rewriting is undesirable. */
15956 struct type *new_ary_type
15957 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15958 if (new_ary_type != nullptr)
15959 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15960 }
15961
15962 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15963 appropriate error checking and issuing complaints if there is a
15964 problem. */
15965
15966 static ULONGEST
15967 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15968 {
15969 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15970
15971 if (attr == nullptr)
15972 return 0;
15973
15974 if (!attr->form_is_constant ())
15975 {
15976 complaint (_("DW_AT_alignment must have constant form"
15977 " - DIE at %s [in module %s]"),
15978 sect_offset_str (die->sect_off),
15979 objfile_name (cu->per_objfile->objfile));
15980 return 0;
15981 }
15982
15983 LONGEST val = attr->constant_value (0);
15984 if (val < 0)
15985 {
15986 complaint (_("DW_AT_alignment value must not be negative"
15987 " - DIE at %s [in module %s]"),
15988 sect_offset_str (die->sect_off),
15989 objfile_name (cu->per_objfile->objfile));
15990 return 0;
15991 }
15992 ULONGEST align = val;
15993
15994 if (align == 0)
15995 {
15996 complaint (_("DW_AT_alignment value must not be zero"
15997 " - DIE at %s [in module %s]"),
15998 sect_offset_str (die->sect_off),
15999 objfile_name (cu->per_objfile->objfile));
16000 return 0;
16001 }
16002 if ((align & (align - 1)) != 0)
16003 {
16004 complaint (_("DW_AT_alignment value must be a power of 2"
16005 " - DIE at %s [in module %s]"),
16006 sect_offset_str (die->sect_off),
16007 objfile_name (cu->per_objfile->objfile));
16008 return 0;
16009 }
16010
16011 return align;
16012 }
16013
16014 /* If the DIE has a DW_AT_alignment attribute, use its value to set
16015 the alignment for TYPE. */
16016
16017 static void
16018 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16019 struct type *type)
16020 {
16021 if (!set_type_align (type, get_alignment (cu, die)))
16022 complaint (_("DW_AT_alignment value too large"
16023 " - DIE at %s [in module %s]"),
16024 sect_offset_str (die->sect_off),
16025 objfile_name (cu->per_objfile->objfile));
16026 }
16027
16028 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16029 constant for a type, according to DWARF5 spec, Table 5.5. */
16030
16031 static bool
16032 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16033 {
16034 switch (value)
16035 {
16036 case DW_CC_normal:
16037 case DW_CC_pass_by_reference:
16038 case DW_CC_pass_by_value:
16039 return true;
16040
16041 default:
16042 complaint (_("unrecognized DW_AT_calling_convention value "
16043 "(%s) for a type"), pulongest (value));
16044 return false;
16045 }
16046 }
16047
16048 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16049 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16050 also according to GNU-specific values (see include/dwarf2.h). */
16051
16052 static bool
16053 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16054 {
16055 switch (value)
16056 {
16057 case DW_CC_normal:
16058 case DW_CC_program:
16059 case DW_CC_nocall:
16060 return true;
16061
16062 case DW_CC_GNU_renesas_sh:
16063 case DW_CC_GNU_borland_fastcall_i386:
16064 case DW_CC_GDB_IBM_OpenCL:
16065 return true;
16066
16067 default:
16068 complaint (_("unrecognized DW_AT_calling_convention value "
16069 "(%s) for a subroutine"), pulongest (value));
16070 return false;
16071 }
16072 }
16073
16074 /* Called when we find the DIE that starts a structure or union scope
16075 (definition) to create a type for the structure or union. Fill in
16076 the type's name and general properties; the members will not be
16077 processed until process_structure_scope. A symbol table entry for
16078 the type will also not be done until process_structure_scope (assuming
16079 the type has a name).
16080
16081 NOTE: we need to call these functions regardless of whether or not the
16082 DIE has a DW_AT_name attribute, since it might be an anonymous
16083 structure or union. This gets the type entered into our set of
16084 user defined types. */
16085
16086 static struct type *
16087 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16088 {
16089 struct objfile *objfile = cu->per_objfile->objfile;
16090 struct type *type;
16091 struct attribute *attr;
16092 const char *name;
16093
16094 /* If the definition of this type lives in .debug_types, read that type.
16095 Don't follow DW_AT_specification though, that will take us back up
16096 the chain and we want to go down. */
16097 attr = die->attr (DW_AT_signature);
16098 if (attr != nullptr)
16099 {
16100 type = get_DW_AT_signature_type (die, attr, cu);
16101
16102 /* The type's CU may not be the same as CU.
16103 Ensure TYPE is recorded with CU in die_type_hash. */
16104 return set_die_type (die, type, cu);
16105 }
16106
16107 type = alloc_type (objfile);
16108 INIT_CPLUS_SPECIFIC (type);
16109
16110 name = dwarf2_name (die, cu);
16111 if (name != NULL)
16112 {
16113 if (cu->language == language_cplus
16114 || cu->language == language_d
16115 || cu->language == language_rust)
16116 {
16117 const char *full_name = dwarf2_full_name (name, die, cu);
16118
16119 /* dwarf2_full_name might have already finished building the DIE's
16120 type. If so, there is no need to continue. */
16121 if (get_die_type (die, cu) != NULL)
16122 return get_die_type (die, cu);
16123
16124 type->set_name (full_name);
16125 }
16126 else
16127 {
16128 /* The name is already allocated along with this objfile, so
16129 we don't need to duplicate it for the type. */
16130 type->set_name (name);
16131 }
16132 }
16133
16134 if (die->tag == DW_TAG_structure_type)
16135 {
16136 type->set_code (TYPE_CODE_STRUCT);
16137 }
16138 else if (die->tag == DW_TAG_union_type)
16139 {
16140 type->set_code (TYPE_CODE_UNION);
16141 }
16142 else
16143 {
16144 type->set_code (TYPE_CODE_STRUCT);
16145 }
16146
16147 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16148 type->set_is_declared_class (true);
16149
16150 /* Store the calling convention in the type if it's available in
16151 the die. Otherwise the calling convention remains set to
16152 the default value DW_CC_normal. */
16153 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16154 if (attr != nullptr
16155 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16156 {
16157 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16158 TYPE_CPLUS_CALLING_CONVENTION (type)
16159 = (enum dwarf_calling_convention) (attr->constant_value (0));
16160 }
16161
16162 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16163 if (attr != nullptr)
16164 {
16165 if (attr->form_is_constant ())
16166 TYPE_LENGTH (type) = attr->constant_value (0);
16167 else
16168 {
16169 struct dynamic_prop prop;
16170 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16171 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16172 TYPE_LENGTH (type) = 0;
16173 }
16174 }
16175 else
16176 {
16177 TYPE_LENGTH (type) = 0;
16178 }
16179
16180 maybe_set_alignment (cu, die, type);
16181
16182 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16183 {
16184 /* ICC<14 does not output the required DW_AT_declaration on
16185 incomplete types, but gives them a size of zero. */
16186 type->set_is_stub (true);
16187 }
16188 else
16189 type->set_stub_is_supported (true);
16190
16191 if (die_is_declaration (die, cu))
16192 type->set_is_stub (true);
16193 else if (attr == NULL && die->child == NULL
16194 && producer_is_realview (cu->producer))
16195 /* RealView does not output the required DW_AT_declaration
16196 on incomplete types. */
16197 type->set_is_stub (true);
16198
16199 /* We need to add the type field to the die immediately so we don't
16200 infinitely recurse when dealing with pointers to the structure
16201 type within the structure itself. */
16202 set_die_type (die, type, cu);
16203
16204 /* set_die_type should be already done. */
16205 set_descriptive_type (type, die, cu);
16206
16207 return type;
16208 }
16209
16210 static void handle_struct_member_die
16211 (struct die_info *child_die,
16212 struct type *type,
16213 struct field_info *fi,
16214 std::vector<struct symbol *> *template_args,
16215 struct dwarf2_cu *cu);
16216
16217 /* A helper for handle_struct_member_die that handles
16218 DW_TAG_variant_part. */
16219
16220 static void
16221 handle_variant_part (struct die_info *die, struct type *type,
16222 struct field_info *fi,
16223 std::vector<struct symbol *> *template_args,
16224 struct dwarf2_cu *cu)
16225 {
16226 variant_part_builder *new_part;
16227 if (fi->current_variant_part == nullptr)
16228 {
16229 fi->variant_parts.emplace_back ();
16230 new_part = &fi->variant_parts.back ();
16231 }
16232 else if (!fi->current_variant_part->processing_variant)
16233 {
16234 complaint (_("nested DW_TAG_variant_part seen "
16235 "- DIE at %s [in module %s]"),
16236 sect_offset_str (die->sect_off),
16237 objfile_name (cu->per_objfile->objfile));
16238 return;
16239 }
16240 else
16241 {
16242 variant_field &current = fi->current_variant_part->variants.back ();
16243 current.variant_parts.emplace_back ();
16244 new_part = &current.variant_parts.back ();
16245 }
16246
16247 /* When we recurse, we want callees to add to this new variant
16248 part. */
16249 scoped_restore save_current_variant_part
16250 = make_scoped_restore (&fi->current_variant_part, new_part);
16251
16252 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16253 if (discr == NULL)
16254 {
16255 /* It's a univariant form, an extension we support. */
16256 }
16257 else if (discr->form_is_ref ())
16258 {
16259 struct dwarf2_cu *target_cu = cu;
16260 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16261
16262 new_part->discriminant_offset = target_die->sect_off;
16263 }
16264 else
16265 {
16266 complaint (_("DW_AT_discr does not have DIE reference form"
16267 " - DIE at %s [in module %s]"),
16268 sect_offset_str (die->sect_off),
16269 objfile_name (cu->per_objfile->objfile));
16270 }
16271
16272 for (die_info *child_die = die->child;
16273 child_die != NULL;
16274 child_die = child_die->sibling)
16275 handle_struct_member_die (child_die, type, fi, template_args, cu);
16276 }
16277
16278 /* A helper for handle_struct_member_die that handles
16279 DW_TAG_variant. */
16280
16281 static void
16282 handle_variant (struct die_info *die, struct type *type,
16283 struct field_info *fi,
16284 std::vector<struct symbol *> *template_args,
16285 struct dwarf2_cu *cu)
16286 {
16287 if (fi->current_variant_part == nullptr)
16288 {
16289 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16290 "- DIE at %s [in module %s]"),
16291 sect_offset_str (die->sect_off),
16292 objfile_name (cu->per_objfile->objfile));
16293 return;
16294 }
16295 if (fi->current_variant_part->processing_variant)
16296 {
16297 complaint (_("nested DW_TAG_variant seen "
16298 "- DIE at %s [in module %s]"),
16299 sect_offset_str (die->sect_off),
16300 objfile_name (cu->per_objfile->objfile));
16301 return;
16302 }
16303
16304 scoped_restore save_processing_variant
16305 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16306 true);
16307
16308 fi->current_variant_part->variants.emplace_back ();
16309 variant_field &variant = fi->current_variant_part->variants.back ();
16310 variant.first_field = fi->fields.size ();
16311
16312 /* In a variant we want to get the discriminant and also add a
16313 field for our sole member child. */
16314 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16315 if (discr == nullptr || !discr->form_is_constant ())
16316 {
16317 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16318 if (discr == nullptr || discr->as_block ()->size == 0)
16319 variant.default_branch = true;
16320 else
16321 variant.discr_list_data = discr->as_block ();
16322 }
16323 else
16324 variant.discriminant_value = discr->constant_value (0);
16325
16326 for (die_info *variant_child = die->child;
16327 variant_child != NULL;
16328 variant_child = variant_child->sibling)
16329 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16330
16331 variant.last_field = fi->fields.size ();
16332 }
16333
16334 /* A helper for process_structure_scope that handles a single member
16335 DIE. */
16336
16337 static void
16338 handle_struct_member_die (struct die_info *child_die, struct type *type,
16339 struct field_info *fi,
16340 std::vector<struct symbol *> *template_args,
16341 struct dwarf2_cu *cu)
16342 {
16343 if (child_die->tag == DW_TAG_member
16344 || child_die->tag == DW_TAG_variable)
16345 {
16346 /* NOTE: carlton/2002-11-05: A C++ static data member
16347 should be a DW_TAG_member that is a declaration, but
16348 all versions of G++ as of this writing (so through at
16349 least 3.2.1) incorrectly generate DW_TAG_variable
16350 tags for them instead. */
16351 dwarf2_add_field (fi, child_die, cu);
16352 }
16353 else if (child_die->tag == DW_TAG_subprogram)
16354 {
16355 /* Rust doesn't have member functions in the C++ sense.
16356 However, it does emit ordinary functions as children
16357 of a struct DIE. */
16358 if (cu->language == language_rust)
16359 read_func_scope (child_die, cu);
16360 else
16361 {
16362 /* C++ member function. */
16363 dwarf2_add_member_fn (fi, child_die, type, cu);
16364 }
16365 }
16366 else if (child_die->tag == DW_TAG_inheritance)
16367 {
16368 /* C++ base class field. */
16369 dwarf2_add_field (fi, child_die, cu);
16370 }
16371 else if (type_can_define_types (child_die))
16372 dwarf2_add_type_defn (fi, child_die, cu);
16373 else if (child_die->tag == DW_TAG_template_type_param
16374 || child_die->tag == DW_TAG_template_value_param)
16375 {
16376 struct symbol *arg = new_symbol (child_die, NULL, cu);
16377
16378 if (arg != NULL)
16379 template_args->push_back (arg);
16380 }
16381 else if (child_die->tag == DW_TAG_variant_part)
16382 handle_variant_part (child_die, type, fi, template_args, cu);
16383 else if (child_die->tag == DW_TAG_variant)
16384 handle_variant (child_die, type, fi, template_args, cu);
16385 }
16386
16387 /* Finish creating a structure or union type, including filling in
16388 its members and creating a symbol for it. */
16389
16390 static void
16391 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16392 {
16393 struct objfile *objfile = cu->per_objfile->objfile;
16394 struct die_info *child_die;
16395 struct type *type;
16396
16397 type = get_die_type (die, cu);
16398 if (type == NULL)
16399 type = read_structure_type (die, cu);
16400
16401 bool has_template_parameters = false;
16402 if (die->child != NULL && ! die_is_declaration (die, cu))
16403 {
16404 struct field_info fi;
16405 std::vector<struct symbol *> template_args;
16406
16407 child_die = die->child;
16408
16409 while (child_die && child_die->tag)
16410 {
16411 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16412 child_die = child_die->sibling;
16413 }
16414
16415 /* Attach template arguments to type. */
16416 if (!template_args.empty ())
16417 {
16418 has_template_parameters = true;
16419 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16420 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16421 TYPE_TEMPLATE_ARGUMENTS (type)
16422 = XOBNEWVEC (&objfile->objfile_obstack,
16423 struct symbol *,
16424 TYPE_N_TEMPLATE_ARGUMENTS (type));
16425 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16426 template_args.data (),
16427 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16428 * sizeof (struct symbol *)));
16429 }
16430
16431 /* Attach fields and member functions to the type. */
16432 if (fi.nfields () > 0)
16433 dwarf2_attach_fields_to_type (&fi, type, cu);
16434 if (!fi.fnfieldlists.empty ())
16435 {
16436 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16437
16438 /* Get the type which refers to the base class (possibly this
16439 class itself) which contains the vtable pointer for the current
16440 class from the DW_AT_containing_type attribute. This use of
16441 DW_AT_containing_type is a GNU extension. */
16442
16443 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16444 {
16445 struct type *t = die_containing_type (die, cu);
16446
16447 set_type_vptr_basetype (type, t);
16448 if (type == t)
16449 {
16450 int i;
16451
16452 /* Our own class provides vtbl ptr. */
16453 for (i = t->num_fields () - 1;
16454 i >= TYPE_N_BASECLASSES (t);
16455 --i)
16456 {
16457 const char *fieldname = TYPE_FIELD_NAME (t, i);
16458
16459 if (is_vtable_name (fieldname, cu))
16460 {
16461 set_type_vptr_fieldno (type, i);
16462 break;
16463 }
16464 }
16465
16466 /* Complain if virtual function table field not found. */
16467 if (i < TYPE_N_BASECLASSES (t))
16468 complaint (_("virtual function table pointer "
16469 "not found when defining class '%s'"),
16470 type->name () ? type->name () : "");
16471 }
16472 else
16473 {
16474 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16475 }
16476 }
16477 else if (cu->producer
16478 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16479 {
16480 /* The IBM XLC compiler does not provide direct indication
16481 of the containing type, but the vtable pointer is
16482 always named __vfp. */
16483
16484 int i;
16485
16486 for (i = type->num_fields () - 1;
16487 i >= TYPE_N_BASECLASSES (type);
16488 --i)
16489 {
16490 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16491 {
16492 set_type_vptr_fieldno (type, i);
16493 set_type_vptr_basetype (type, type);
16494 break;
16495 }
16496 }
16497 }
16498 }
16499
16500 /* Copy fi.typedef_field_list linked list elements content into the
16501 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16502 if (!fi.typedef_field_list.empty ())
16503 {
16504 int count = fi.typedef_field_list.size ();
16505
16506 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16507 TYPE_TYPEDEF_FIELD_ARRAY (type)
16508 = ((struct decl_field *)
16509 TYPE_ALLOC (type,
16510 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16511 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16512
16513 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16514 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16515 }
16516
16517 /* Copy fi.nested_types_list linked list elements content into the
16518 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16519 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16520 {
16521 int count = fi.nested_types_list.size ();
16522
16523 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16524 TYPE_NESTED_TYPES_ARRAY (type)
16525 = ((struct decl_field *)
16526 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16527 TYPE_NESTED_TYPES_COUNT (type) = count;
16528
16529 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16530 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16531 }
16532 }
16533
16534 quirk_gcc_member_function_pointer (type, objfile);
16535 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16536 cu->rust_unions.push_back (type);
16537 else if (cu->language == language_ada)
16538 quirk_ada_thick_pointer_struct (die, cu, type);
16539
16540 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16541 snapshots) has been known to create a die giving a declaration
16542 for a class that has, as a child, a die giving a definition for a
16543 nested class. So we have to process our children even if the
16544 current die is a declaration. Normally, of course, a declaration
16545 won't have any children at all. */
16546
16547 child_die = die->child;
16548
16549 while (child_die != NULL && child_die->tag)
16550 {
16551 if (child_die->tag == DW_TAG_member
16552 || child_die->tag == DW_TAG_variable
16553 || child_die->tag == DW_TAG_inheritance
16554 || child_die->tag == DW_TAG_template_value_param
16555 || child_die->tag == DW_TAG_template_type_param)
16556 {
16557 /* Do nothing. */
16558 }
16559 else
16560 process_die (child_die, cu);
16561
16562 child_die = child_die->sibling;
16563 }
16564
16565 /* Do not consider external references. According to the DWARF standard,
16566 these DIEs are identified by the fact that they have no byte_size
16567 attribute, and a declaration attribute. */
16568 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16569 || !die_is_declaration (die, cu)
16570 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16571 {
16572 struct symbol *sym = new_symbol (die, type, cu);
16573
16574 if (has_template_parameters)
16575 {
16576 struct symtab *symtab;
16577 if (sym != nullptr)
16578 symtab = symbol_symtab (sym);
16579 else if (cu->line_header != nullptr)
16580 {
16581 /* Any related symtab will do. */
16582 symtab
16583 = cu->line_header->file_names ()[0].symtab;
16584 }
16585 else
16586 {
16587 symtab = nullptr;
16588 complaint (_("could not find suitable "
16589 "symtab for template parameter"
16590 " - DIE at %s [in module %s]"),
16591 sect_offset_str (die->sect_off),
16592 objfile_name (objfile));
16593 }
16594
16595 if (symtab != nullptr)
16596 {
16597 /* Make sure that the symtab is set on the new symbols.
16598 Even though they don't appear in this symtab directly,
16599 other parts of gdb assume that symbols do, and this is
16600 reasonably true. */
16601 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16602 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16603 }
16604 }
16605 }
16606 }
16607
16608 /* Assuming DIE is an enumeration type, and TYPE is its associated
16609 type, update TYPE using some information only available in DIE's
16610 children. In particular, the fields are computed. */
16611
16612 static void
16613 update_enumeration_type_from_children (struct die_info *die,
16614 struct type *type,
16615 struct dwarf2_cu *cu)
16616 {
16617 struct die_info *child_die;
16618 int unsigned_enum = 1;
16619 int flag_enum = 1;
16620
16621 auto_obstack obstack;
16622 std::vector<struct field> fields;
16623
16624 for (child_die = die->child;
16625 child_die != NULL && child_die->tag;
16626 child_die = child_die->sibling)
16627 {
16628 struct attribute *attr;
16629 LONGEST value;
16630 const gdb_byte *bytes;
16631 struct dwarf2_locexpr_baton *baton;
16632 const char *name;
16633
16634 if (child_die->tag != DW_TAG_enumerator)
16635 continue;
16636
16637 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16638 if (attr == NULL)
16639 continue;
16640
16641 name = dwarf2_name (child_die, cu);
16642 if (name == NULL)
16643 name = "<anonymous enumerator>";
16644
16645 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16646 &value, &bytes, &baton);
16647 if (value < 0)
16648 {
16649 unsigned_enum = 0;
16650 flag_enum = 0;
16651 }
16652 else
16653 {
16654 if (count_one_bits_ll (value) >= 2)
16655 flag_enum = 0;
16656 }
16657
16658 fields.emplace_back ();
16659 struct field &field = fields.back ();
16660 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16661 SET_FIELD_ENUMVAL (field, value);
16662 }
16663
16664 if (!fields.empty ())
16665 {
16666 type->set_num_fields (fields.size ());
16667 type->set_fields
16668 ((struct field *)
16669 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16670 memcpy (type->fields (), fields.data (),
16671 sizeof (struct field) * fields.size ());
16672 }
16673
16674 if (unsigned_enum)
16675 type->set_is_unsigned (true);
16676
16677 if (flag_enum)
16678 type->set_is_flag_enum (true);
16679 }
16680
16681 /* Given a DW_AT_enumeration_type die, set its type. We do not
16682 complete the type's fields yet, or create any symbols. */
16683
16684 static struct type *
16685 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16686 {
16687 struct objfile *objfile = cu->per_objfile->objfile;
16688 struct type *type;
16689 struct attribute *attr;
16690 const char *name;
16691
16692 /* If the definition of this type lives in .debug_types, read that type.
16693 Don't follow DW_AT_specification though, that will take us back up
16694 the chain and we want to go down. */
16695 attr = die->attr (DW_AT_signature);
16696 if (attr != nullptr)
16697 {
16698 type = get_DW_AT_signature_type (die, attr, cu);
16699
16700 /* The type's CU may not be the same as CU.
16701 Ensure TYPE is recorded with CU in die_type_hash. */
16702 return set_die_type (die, type, cu);
16703 }
16704
16705 type = alloc_type (objfile);
16706
16707 type->set_code (TYPE_CODE_ENUM);
16708 name = dwarf2_full_name (NULL, die, cu);
16709 if (name != NULL)
16710 type->set_name (name);
16711
16712 attr = dwarf2_attr (die, DW_AT_type, cu);
16713 if (attr != NULL)
16714 {
16715 struct type *underlying_type = die_type (die, cu);
16716
16717 TYPE_TARGET_TYPE (type) = underlying_type;
16718 }
16719
16720 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16721 if (attr != nullptr)
16722 {
16723 TYPE_LENGTH (type) = attr->constant_value (0);
16724 }
16725 else
16726 {
16727 TYPE_LENGTH (type) = 0;
16728 }
16729
16730 maybe_set_alignment (cu, die, type);
16731
16732 /* The enumeration DIE can be incomplete. In Ada, any type can be
16733 declared as private in the package spec, and then defined only
16734 inside the package body. Such types are known as Taft Amendment
16735 Types. When another package uses such a type, an incomplete DIE
16736 may be generated by the compiler. */
16737 if (die_is_declaration (die, cu))
16738 type->set_is_stub (true);
16739
16740 /* If this type has an underlying type that is not a stub, then we
16741 may use its attributes. We always use the "unsigned" attribute
16742 in this situation, because ordinarily we guess whether the type
16743 is unsigned -- but the guess can be wrong and the underlying type
16744 can tell us the reality. However, we defer to a local size
16745 attribute if one exists, because this lets the compiler override
16746 the underlying type if needed. */
16747 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16748 {
16749 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16750 underlying_type = check_typedef (underlying_type);
16751
16752 type->set_is_unsigned (underlying_type->is_unsigned ());
16753
16754 if (TYPE_LENGTH (type) == 0)
16755 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16756
16757 if (TYPE_RAW_ALIGN (type) == 0
16758 && TYPE_RAW_ALIGN (underlying_type) != 0)
16759 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16760 }
16761
16762 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16763
16764 set_die_type (die, type, cu);
16765
16766 /* Finish the creation of this type by using the enum's children.
16767 Note that, as usual, this must come after set_die_type to avoid
16768 infinite recursion when trying to compute the names of the
16769 enumerators. */
16770 update_enumeration_type_from_children (die, type, cu);
16771
16772 return type;
16773 }
16774
16775 /* Given a pointer to a die which begins an enumeration, process all
16776 the dies that define the members of the enumeration, and create the
16777 symbol for the enumeration type.
16778
16779 NOTE: We reverse the order of the element list. */
16780
16781 static void
16782 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16783 {
16784 struct type *this_type;
16785
16786 this_type = get_die_type (die, cu);
16787 if (this_type == NULL)
16788 this_type = read_enumeration_type (die, cu);
16789
16790 if (die->child != NULL)
16791 {
16792 struct die_info *child_die;
16793 const char *name;
16794
16795 child_die = die->child;
16796 while (child_die && child_die->tag)
16797 {
16798 if (child_die->tag != DW_TAG_enumerator)
16799 {
16800 process_die (child_die, cu);
16801 }
16802 else
16803 {
16804 name = dwarf2_name (child_die, cu);
16805 if (name)
16806 new_symbol (child_die, this_type, cu);
16807 }
16808
16809 child_die = child_die->sibling;
16810 }
16811 }
16812
16813 /* If we are reading an enum from a .debug_types unit, and the enum
16814 is a declaration, and the enum is not the signatured type in the
16815 unit, then we do not want to add a symbol for it. Adding a
16816 symbol would in some cases obscure the true definition of the
16817 enum, giving users an incomplete type when the definition is
16818 actually available. Note that we do not want to do this for all
16819 enums which are just declarations, because C++0x allows forward
16820 enum declarations. */
16821 if (cu->per_cu->is_debug_types
16822 && die_is_declaration (die, cu))
16823 {
16824 struct signatured_type *sig_type;
16825
16826 sig_type = (struct signatured_type *) cu->per_cu;
16827 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16828 if (sig_type->type_offset_in_section != die->sect_off)
16829 return;
16830 }
16831
16832 new_symbol (die, this_type, cu);
16833 }
16834
16835 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16836 expression for an index type and finds the corresponding field
16837 offset in the hidden "P_BOUNDS" structure. Returns true on success
16838 and updates *FIELD, false if it fails to recognize an
16839 expression. */
16840
16841 static bool
16842 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16843 int *bounds_offset, struct field *field,
16844 struct dwarf2_cu *cu)
16845 {
16846 struct attribute *attr = dwarf2_attr (die, name, cu);
16847 if (attr == nullptr || !attr->form_is_block ())
16848 return false;
16849
16850 const struct dwarf_block *block = attr->as_block ();
16851 const gdb_byte *start = block->data;
16852 const gdb_byte *end = block->data + block->size;
16853
16854 /* The expression to recognize generally looks like:
16855
16856 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16857 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16858
16859 However, the second "plus_uconst" may be missing:
16860
16861 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16862 DW_OP_deref_size: 4)
16863
16864 This happens when the field is at the start of the structure.
16865
16866 Also, the final deref may not be sized:
16867
16868 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16869 DW_OP_deref)
16870
16871 This happens when the size of the index type happens to be the
16872 same as the architecture's word size. This can occur with or
16873 without the second plus_uconst. */
16874
16875 if (end - start < 2)
16876 return false;
16877 if (*start++ != DW_OP_push_object_address)
16878 return false;
16879 if (*start++ != DW_OP_plus_uconst)
16880 return false;
16881
16882 uint64_t this_bound_off;
16883 start = gdb_read_uleb128 (start, end, &this_bound_off);
16884 if (start == nullptr || (int) this_bound_off != this_bound_off)
16885 return false;
16886 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16887 is consistent among all bounds. */
16888 if (*bounds_offset == -1)
16889 *bounds_offset = this_bound_off;
16890 else if (*bounds_offset != this_bound_off)
16891 return false;
16892
16893 if (start == end || *start++ != DW_OP_deref)
16894 return false;
16895
16896 int offset = 0;
16897 if (start ==end)
16898 return false;
16899 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16900 {
16901 /* This means an offset of 0. */
16902 }
16903 else if (*start++ != DW_OP_plus_uconst)
16904 return false;
16905 else
16906 {
16907 /* The size is the parameter to DW_OP_plus_uconst. */
16908 uint64_t val;
16909 start = gdb_read_uleb128 (start, end, &val);
16910 if (start == nullptr)
16911 return false;
16912 if ((int) val != val)
16913 return false;
16914 offset = val;
16915 }
16916
16917 if (start == end)
16918 return false;
16919
16920 uint64_t size;
16921 if (*start == DW_OP_deref_size)
16922 {
16923 start = gdb_read_uleb128 (start + 1, end, &size);
16924 if (start == nullptr)
16925 return false;
16926 }
16927 else if (*start == DW_OP_deref)
16928 {
16929 size = cu->header.addr_size;
16930 ++start;
16931 }
16932 else
16933 return false;
16934
16935 SET_FIELD_BITPOS (*field, 8 * offset);
16936 if (size != TYPE_LENGTH (field->type ()))
16937 FIELD_BITSIZE (*field) = 8 * size;
16938
16939 return true;
16940 }
16941
16942 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16943 some kinds of Ada arrays:
16944
16945 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16946 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16947 <11e0> DW_AT_data_location: 2 byte block: 97 6
16948 (DW_OP_push_object_address; DW_OP_deref)
16949 <11e3> DW_AT_type : <0x1173>
16950 <11e7> DW_AT_sibling : <0x1201>
16951 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16952 <11ec> DW_AT_type : <0x1206>
16953 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16954 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16955 DW_OP_deref_size: 4)
16956 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16957 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16958 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16959
16960 This actually represents a "thick pointer", which is a structure
16961 with two elements: one that is a pointer to the array data, and one
16962 that is a pointer to another structure; this second structure holds
16963 the array bounds.
16964
16965 This returns a new type on success, or nullptr if this didn't
16966 recognize the type. */
16967
16968 static struct type *
16969 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16970 struct type *type)
16971 {
16972 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16973 /* So far we've only seen this with block form. */
16974 if (attr == nullptr || !attr->form_is_block ())
16975 return nullptr;
16976
16977 /* Note that this will fail if the structure layout is changed by
16978 the compiler. However, we have no good way to recognize some
16979 other layout, because we don't know what expression the compiler
16980 might choose to emit should this happen. */
16981 struct dwarf_block *blk = attr->as_block ();
16982 if (blk->size != 2
16983 || blk->data[0] != DW_OP_push_object_address
16984 || blk->data[1] != DW_OP_deref)
16985 return nullptr;
16986
16987 int bounds_offset = -1;
16988 int max_align = -1;
16989 std::vector<struct field> range_fields;
16990 for (struct die_info *child_die = die->child;
16991 child_die;
16992 child_die = child_die->sibling)
16993 {
16994 if (child_die->tag == DW_TAG_subrange_type)
16995 {
16996 struct type *underlying = read_subrange_index_type (child_die, cu);
16997
16998 int this_align = type_align (underlying);
16999 if (this_align > max_align)
17000 max_align = this_align;
17001
17002 range_fields.emplace_back ();
17003 range_fields.emplace_back ();
17004
17005 struct field &lower = range_fields[range_fields.size () - 2];
17006 struct field &upper = range_fields[range_fields.size () - 1];
17007
17008 lower.set_type (underlying);
17009 FIELD_ARTIFICIAL (lower) = 1;
17010
17011 upper.set_type (underlying);
17012 FIELD_ARTIFICIAL (upper) = 1;
17013
17014 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17015 &bounds_offset, &lower, cu)
17016 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17017 &bounds_offset, &upper, cu))
17018 return nullptr;
17019 }
17020 }
17021
17022 /* This shouldn't really happen, but double-check that we found
17023 where the bounds are stored. */
17024 if (bounds_offset == -1)
17025 return nullptr;
17026
17027 struct objfile *objfile = cu->per_objfile->objfile;
17028 for (int i = 0; i < range_fields.size (); i += 2)
17029 {
17030 char name[20];
17031
17032 /* Set the name of each field in the bounds. */
17033 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17034 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17035 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17036 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17037 }
17038
17039 struct type *bounds = alloc_type (objfile);
17040 bounds->set_code (TYPE_CODE_STRUCT);
17041
17042 bounds->set_num_fields (range_fields.size ());
17043 bounds->set_fields
17044 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17045 * sizeof (struct field))));
17046 memcpy (bounds->fields (), range_fields.data (),
17047 bounds->num_fields () * sizeof (struct field));
17048
17049 int last_fieldno = range_fields.size () - 1;
17050 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17051 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17052 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17053
17054 /* Rewrite the existing array type in place. Specifically, we
17055 remove any dynamic properties we might have read, and we replace
17056 the index types. */
17057 struct type *iter = type;
17058 for (int i = 0; i < range_fields.size (); i += 2)
17059 {
17060 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17061 iter->main_type->dyn_prop_list = nullptr;
17062 iter->set_index_type
17063 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17064 iter = TYPE_TARGET_TYPE (iter);
17065 }
17066
17067 struct type *result = alloc_type (objfile);
17068 result->set_code (TYPE_CODE_STRUCT);
17069
17070 result->set_num_fields (2);
17071 result->set_fields
17072 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17073 * sizeof (struct field))));
17074
17075 /* The names are chosen to coincide with what the compiler does with
17076 -fgnat-encodings=all, which the Ada code in gdb already
17077 understands. */
17078 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17079 result->field (0).set_type (lookup_pointer_type (type));
17080
17081 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17082 result->field (1).set_type (lookup_pointer_type (bounds));
17083 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17084
17085 result->set_name (type->name ());
17086 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17087 + TYPE_LENGTH (result->field (1).type ()));
17088
17089 return result;
17090 }
17091
17092 /* Extract all information from a DW_TAG_array_type DIE and put it in
17093 the DIE's type field. For now, this only handles one dimensional
17094 arrays. */
17095
17096 static struct type *
17097 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17098 {
17099 struct objfile *objfile = cu->per_objfile->objfile;
17100 struct die_info *child_die;
17101 struct type *type;
17102 struct type *element_type, *range_type, *index_type;
17103 struct attribute *attr;
17104 const char *name;
17105 struct dynamic_prop *byte_stride_prop = NULL;
17106 unsigned int bit_stride = 0;
17107
17108 element_type = die_type (die, cu);
17109
17110 /* The die_type call above may have already set the type for this DIE. */
17111 type = get_die_type (die, cu);
17112 if (type)
17113 return type;
17114
17115 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17116 if (attr != NULL)
17117 {
17118 int stride_ok;
17119 struct type *prop_type = cu->addr_sized_int_type (false);
17120
17121 byte_stride_prop
17122 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17123 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17124 prop_type);
17125 if (!stride_ok)
17126 {
17127 complaint (_("unable to read array DW_AT_byte_stride "
17128 " - DIE at %s [in module %s]"),
17129 sect_offset_str (die->sect_off),
17130 objfile_name (cu->per_objfile->objfile));
17131 /* Ignore this attribute. We will likely not be able to print
17132 arrays of this type correctly, but there is little we can do
17133 to help if we cannot read the attribute's value. */
17134 byte_stride_prop = NULL;
17135 }
17136 }
17137
17138 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17139 if (attr != NULL)
17140 bit_stride = attr->constant_value (0);
17141
17142 /* Irix 6.2 native cc creates array types without children for
17143 arrays with unspecified length. */
17144 if (die->child == NULL)
17145 {
17146 index_type = objfile_type (objfile)->builtin_int;
17147 range_type = create_static_range_type (NULL, index_type, 0, -1);
17148 type = create_array_type_with_stride (NULL, element_type, range_type,
17149 byte_stride_prop, bit_stride);
17150 return set_die_type (die, type, cu);
17151 }
17152
17153 std::vector<struct type *> range_types;
17154 child_die = die->child;
17155 while (child_die && child_die->tag)
17156 {
17157 if (child_die->tag == DW_TAG_subrange_type)
17158 {
17159 struct type *child_type = read_type_die (child_die, cu);
17160
17161 if (child_type != NULL)
17162 {
17163 /* The range type was succesfully read. Save it for the
17164 array type creation. */
17165 range_types.push_back (child_type);
17166 }
17167 }
17168 child_die = child_die->sibling;
17169 }
17170
17171 if (range_types.empty ())
17172 {
17173 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17174 sect_offset_str (die->sect_off),
17175 objfile_name (cu->per_objfile->objfile));
17176 return NULL;
17177 }
17178
17179 /* Dwarf2 dimensions are output from left to right, create the
17180 necessary array types in backwards order. */
17181
17182 type = element_type;
17183
17184 if (read_array_order (die, cu) == DW_ORD_col_major)
17185 {
17186 int i = 0;
17187
17188 while (i < range_types.size ())
17189 {
17190 type = create_array_type_with_stride (NULL, type, range_types[i++],
17191 byte_stride_prop, bit_stride);
17192 bit_stride = 0;
17193 byte_stride_prop = nullptr;
17194 }
17195 }
17196 else
17197 {
17198 size_t ndim = range_types.size ();
17199 while (ndim-- > 0)
17200 {
17201 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17202 byte_stride_prop, bit_stride);
17203 bit_stride = 0;
17204 byte_stride_prop = nullptr;
17205 }
17206 }
17207
17208 gdb_assert (type != element_type);
17209
17210 /* Understand Dwarf2 support for vector types (like they occur on
17211 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17212 array type. This is not part of the Dwarf2/3 standard yet, but a
17213 custom vendor extension. The main difference between a regular
17214 array and the vector variant is that vectors are passed by value
17215 to functions. */
17216 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17217 if (attr != nullptr)
17218 make_vector_type (type);
17219
17220 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17221 implementation may choose to implement triple vectors using this
17222 attribute. */
17223 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17224 if (attr != nullptr && attr->form_is_unsigned ())
17225 {
17226 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17227 TYPE_LENGTH (type) = attr->as_unsigned ();
17228 else
17229 complaint (_("DW_AT_byte_size for array type smaller "
17230 "than the total size of elements"));
17231 }
17232
17233 name = dwarf2_name (die, cu);
17234 if (name)
17235 type->set_name (name);
17236
17237 maybe_set_alignment (cu, die, type);
17238
17239 struct type *replacement_type = nullptr;
17240 if (cu->language == language_ada)
17241 {
17242 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17243 if (replacement_type != nullptr)
17244 type = replacement_type;
17245 }
17246
17247 /* Install the type in the die. */
17248 set_die_type (die, type, cu, replacement_type != nullptr);
17249
17250 /* set_die_type should be already done. */
17251 set_descriptive_type (type, die, cu);
17252
17253 return type;
17254 }
17255
17256 static enum dwarf_array_dim_ordering
17257 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17258 {
17259 struct attribute *attr;
17260
17261 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17262
17263 if (attr != nullptr)
17264 {
17265 LONGEST val = attr->constant_value (-1);
17266 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17267 return (enum dwarf_array_dim_ordering) val;
17268 }
17269
17270 /* GNU F77 is a special case, as at 08/2004 array type info is the
17271 opposite order to the dwarf2 specification, but data is still
17272 laid out as per normal fortran.
17273
17274 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17275 version checking. */
17276
17277 if (cu->language == language_fortran
17278 && cu->producer && strstr (cu->producer, "GNU F77"))
17279 {
17280 return DW_ORD_row_major;
17281 }
17282
17283 switch (cu->language_defn->array_ordering ())
17284 {
17285 case array_column_major:
17286 return DW_ORD_col_major;
17287 case array_row_major:
17288 default:
17289 return DW_ORD_row_major;
17290 };
17291 }
17292
17293 /* Extract all information from a DW_TAG_set_type DIE and put it in
17294 the DIE's type field. */
17295
17296 static struct type *
17297 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17298 {
17299 struct type *domain_type, *set_type;
17300 struct attribute *attr;
17301
17302 domain_type = die_type (die, cu);
17303
17304 /* The die_type call above may have already set the type for this DIE. */
17305 set_type = get_die_type (die, cu);
17306 if (set_type)
17307 return set_type;
17308
17309 set_type = create_set_type (NULL, domain_type);
17310
17311 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17312 if (attr != nullptr && attr->form_is_unsigned ())
17313 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17314
17315 maybe_set_alignment (cu, die, set_type);
17316
17317 return set_die_type (die, set_type, cu);
17318 }
17319
17320 /* A helper for read_common_block that creates a locexpr baton.
17321 SYM is the symbol which we are marking as computed.
17322 COMMON_DIE is the DIE for the common block.
17323 COMMON_LOC is the location expression attribute for the common
17324 block itself.
17325 MEMBER_LOC is the location expression attribute for the particular
17326 member of the common block that we are processing.
17327 CU is the CU from which the above come. */
17328
17329 static void
17330 mark_common_block_symbol_computed (struct symbol *sym,
17331 struct die_info *common_die,
17332 struct attribute *common_loc,
17333 struct attribute *member_loc,
17334 struct dwarf2_cu *cu)
17335 {
17336 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17337 struct objfile *objfile = per_objfile->objfile;
17338 struct dwarf2_locexpr_baton *baton;
17339 gdb_byte *ptr;
17340 unsigned int cu_off;
17341 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17342 LONGEST offset = 0;
17343
17344 gdb_assert (common_loc && member_loc);
17345 gdb_assert (common_loc->form_is_block ());
17346 gdb_assert (member_loc->form_is_block ()
17347 || member_loc->form_is_constant ());
17348
17349 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17350 baton->per_objfile = per_objfile;
17351 baton->per_cu = cu->per_cu;
17352 gdb_assert (baton->per_cu);
17353
17354 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17355
17356 if (member_loc->form_is_constant ())
17357 {
17358 offset = member_loc->constant_value (0);
17359 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17360 }
17361 else
17362 baton->size += member_loc->as_block ()->size;
17363
17364 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17365 baton->data = ptr;
17366
17367 *ptr++ = DW_OP_call4;
17368 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17369 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17370 ptr += 4;
17371
17372 if (member_loc->form_is_constant ())
17373 {
17374 *ptr++ = DW_OP_addr;
17375 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17376 ptr += cu->header.addr_size;
17377 }
17378 else
17379 {
17380 /* We have to copy the data here, because DW_OP_call4 will only
17381 use a DW_AT_location attribute. */
17382 struct dwarf_block *block = member_loc->as_block ();
17383 memcpy (ptr, block->data, block->size);
17384 ptr += block->size;
17385 }
17386
17387 *ptr++ = DW_OP_plus;
17388 gdb_assert (ptr - baton->data == baton->size);
17389
17390 SYMBOL_LOCATION_BATON (sym) = baton;
17391 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17392 }
17393
17394 /* Create appropriate locally-scoped variables for all the
17395 DW_TAG_common_block entries. Also create a struct common_block
17396 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17397 is used to separate the common blocks name namespace from regular
17398 variable names. */
17399
17400 static void
17401 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17402 {
17403 struct attribute *attr;
17404
17405 attr = dwarf2_attr (die, DW_AT_location, cu);
17406 if (attr != nullptr)
17407 {
17408 /* Support the .debug_loc offsets. */
17409 if (attr->form_is_block ())
17410 {
17411 /* Ok. */
17412 }
17413 else if (attr->form_is_section_offset ())
17414 {
17415 dwarf2_complex_location_expr_complaint ();
17416 attr = NULL;
17417 }
17418 else
17419 {
17420 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17421 "common block member");
17422 attr = NULL;
17423 }
17424 }
17425
17426 if (die->child != NULL)
17427 {
17428 struct objfile *objfile = cu->per_objfile->objfile;
17429 struct die_info *child_die;
17430 size_t n_entries = 0, size;
17431 struct common_block *common_block;
17432 struct symbol *sym;
17433
17434 for (child_die = die->child;
17435 child_die && child_die->tag;
17436 child_die = child_die->sibling)
17437 ++n_entries;
17438
17439 size = (sizeof (struct common_block)
17440 + (n_entries - 1) * sizeof (struct symbol *));
17441 common_block
17442 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17443 size);
17444 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17445 common_block->n_entries = 0;
17446
17447 for (child_die = die->child;
17448 child_die && child_die->tag;
17449 child_die = child_die->sibling)
17450 {
17451 /* Create the symbol in the DW_TAG_common_block block in the current
17452 symbol scope. */
17453 sym = new_symbol (child_die, NULL, cu);
17454 if (sym != NULL)
17455 {
17456 struct attribute *member_loc;
17457
17458 common_block->contents[common_block->n_entries++] = sym;
17459
17460 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17461 cu);
17462 if (member_loc)
17463 {
17464 /* GDB has handled this for a long time, but it is
17465 not specified by DWARF. It seems to have been
17466 emitted by gfortran at least as recently as:
17467 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17468 complaint (_("Variable in common block has "
17469 "DW_AT_data_member_location "
17470 "- DIE at %s [in module %s]"),
17471 sect_offset_str (child_die->sect_off),
17472 objfile_name (objfile));
17473
17474 if (member_loc->form_is_section_offset ())
17475 dwarf2_complex_location_expr_complaint ();
17476 else if (member_loc->form_is_constant ()
17477 || member_loc->form_is_block ())
17478 {
17479 if (attr != nullptr)
17480 mark_common_block_symbol_computed (sym, die, attr,
17481 member_loc, cu);
17482 }
17483 else
17484 dwarf2_complex_location_expr_complaint ();
17485 }
17486 }
17487 }
17488
17489 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17490 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17491 }
17492 }
17493
17494 /* Create a type for a C++ namespace. */
17495
17496 static struct type *
17497 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17498 {
17499 struct objfile *objfile = cu->per_objfile->objfile;
17500 const char *previous_prefix, *name;
17501 int is_anonymous;
17502 struct type *type;
17503
17504 /* For extensions, reuse the type of the original namespace. */
17505 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17506 {
17507 struct die_info *ext_die;
17508 struct dwarf2_cu *ext_cu = cu;
17509
17510 ext_die = dwarf2_extension (die, &ext_cu);
17511 type = read_type_die (ext_die, ext_cu);
17512
17513 /* EXT_CU may not be the same as CU.
17514 Ensure TYPE is recorded with CU in die_type_hash. */
17515 return set_die_type (die, type, cu);
17516 }
17517
17518 name = namespace_name (die, &is_anonymous, cu);
17519
17520 /* Now build the name of the current namespace. */
17521
17522 previous_prefix = determine_prefix (die, cu);
17523 if (previous_prefix[0] != '\0')
17524 name = typename_concat (&objfile->objfile_obstack,
17525 previous_prefix, name, 0, cu);
17526
17527 /* Create the type. */
17528 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17529
17530 return set_die_type (die, type, cu);
17531 }
17532
17533 /* Read a namespace scope. */
17534
17535 static void
17536 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17537 {
17538 struct objfile *objfile = cu->per_objfile->objfile;
17539 int is_anonymous;
17540
17541 /* Add a symbol associated to this if we haven't seen the namespace
17542 before. Also, add a using directive if it's an anonymous
17543 namespace. */
17544
17545 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17546 {
17547 struct type *type;
17548
17549 type = read_type_die (die, cu);
17550 new_symbol (die, type, cu);
17551
17552 namespace_name (die, &is_anonymous, cu);
17553 if (is_anonymous)
17554 {
17555 const char *previous_prefix = determine_prefix (die, cu);
17556
17557 std::vector<const char *> excludes;
17558 add_using_directive (using_directives (cu),
17559 previous_prefix, type->name (), NULL,
17560 NULL, excludes, 0, &objfile->objfile_obstack);
17561 }
17562 }
17563
17564 if (die->child != NULL)
17565 {
17566 struct die_info *child_die = die->child;
17567
17568 while (child_die && child_die->tag)
17569 {
17570 process_die (child_die, cu);
17571 child_die = child_die->sibling;
17572 }
17573 }
17574 }
17575
17576 /* Read a Fortran module as type. This DIE can be only a declaration used for
17577 imported module. Still we need that type as local Fortran "use ... only"
17578 declaration imports depend on the created type in determine_prefix. */
17579
17580 static struct type *
17581 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17582 {
17583 struct objfile *objfile = cu->per_objfile->objfile;
17584 const char *module_name;
17585 struct type *type;
17586
17587 module_name = dwarf2_name (die, cu);
17588 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17589
17590 return set_die_type (die, type, cu);
17591 }
17592
17593 /* Read a Fortran module. */
17594
17595 static void
17596 read_module (struct die_info *die, struct dwarf2_cu *cu)
17597 {
17598 struct die_info *child_die = die->child;
17599 struct type *type;
17600
17601 type = read_type_die (die, cu);
17602 new_symbol (die, type, cu);
17603
17604 while (child_die && child_die->tag)
17605 {
17606 process_die (child_die, cu);
17607 child_die = child_die->sibling;
17608 }
17609 }
17610
17611 /* Return the name of the namespace represented by DIE. Set
17612 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17613 namespace. */
17614
17615 static const char *
17616 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17617 {
17618 struct die_info *current_die;
17619 const char *name = NULL;
17620
17621 /* Loop through the extensions until we find a name. */
17622
17623 for (current_die = die;
17624 current_die != NULL;
17625 current_die = dwarf2_extension (die, &cu))
17626 {
17627 /* We don't use dwarf2_name here so that we can detect the absence
17628 of a name -> anonymous namespace. */
17629 name = dwarf2_string_attr (die, DW_AT_name, cu);
17630
17631 if (name != NULL)
17632 break;
17633 }
17634
17635 /* Is it an anonymous namespace? */
17636
17637 *is_anonymous = (name == NULL);
17638 if (*is_anonymous)
17639 name = CP_ANONYMOUS_NAMESPACE_STR;
17640
17641 return name;
17642 }
17643
17644 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17645 the user defined type vector. */
17646
17647 static struct type *
17648 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17649 {
17650 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17651 struct comp_unit_head *cu_header = &cu->header;
17652 struct type *type;
17653 struct attribute *attr_byte_size;
17654 struct attribute *attr_address_class;
17655 int byte_size, addr_class;
17656 struct type *target_type;
17657
17658 target_type = die_type (die, cu);
17659
17660 /* The die_type call above may have already set the type for this DIE. */
17661 type = get_die_type (die, cu);
17662 if (type)
17663 return type;
17664
17665 type = lookup_pointer_type (target_type);
17666
17667 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17668 if (attr_byte_size)
17669 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17670 else
17671 byte_size = cu_header->addr_size;
17672
17673 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17674 if (attr_address_class)
17675 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17676 else
17677 addr_class = DW_ADDR_none;
17678
17679 ULONGEST alignment = get_alignment (cu, die);
17680
17681 /* If the pointer size, alignment, or address class is different
17682 than the default, create a type variant marked as such and set
17683 the length accordingly. */
17684 if (TYPE_LENGTH (type) != byte_size
17685 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17686 && alignment != TYPE_RAW_ALIGN (type))
17687 || addr_class != DW_ADDR_none)
17688 {
17689 if (gdbarch_address_class_type_flags_p (gdbarch))
17690 {
17691 type_instance_flags type_flags
17692 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17693 addr_class);
17694 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17695 == 0);
17696 type = make_type_with_address_space (type, type_flags);
17697 }
17698 else if (TYPE_LENGTH (type) != byte_size)
17699 {
17700 complaint (_("invalid pointer size %d"), byte_size);
17701 }
17702 else if (TYPE_RAW_ALIGN (type) != alignment)
17703 {
17704 complaint (_("Invalid DW_AT_alignment"
17705 " - DIE at %s [in module %s]"),
17706 sect_offset_str (die->sect_off),
17707 objfile_name (cu->per_objfile->objfile));
17708 }
17709 else
17710 {
17711 /* Should we also complain about unhandled address classes? */
17712 }
17713 }
17714
17715 TYPE_LENGTH (type) = byte_size;
17716 set_type_align (type, alignment);
17717 return set_die_type (die, type, cu);
17718 }
17719
17720 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17721 the user defined type vector. */
17722
17723 static struct type *
17724 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17725 {
17726 struct type *type;
17727 struct type *to_type;
17728 struct type *domain;
17729
17730 to_type = die_type (die, cu);
17731 domain = die_containing_type (die, cu);
17732
17733 /* The calls above may have already set the type for this DIE. */
17734 type = get_die_type (die, cu);
17735 if (type)
17736 return type;
17737
17738 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17739 type = lookup_methodptr_type (to_type);
17740 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17741 {
17742 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17743
17744 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17745 to_type->fields (), to_type->num_fields (),
17746 to_type->has_varargs ());
17747 type = lookup_methodptr_type (new_type);
17748 }
17749 else
17750 type = lookup_memberptr_type (to_type, domain);
17751
17752 return set_die_type (die, type, cu);
17753 }
17754
17755 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17756 the user defined type vector. */
17757
17758 static struct type *
17759 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17760 enum type_code refcode)
17761 {
17762 struct comp_unit_head *cu_header = &cu->header;
17763 struct type *type, *target_type;
17764 struct attribute *attr;
17765
17766 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17767
17768 target_type = die_type (die, cu);
17769
17770 /* The die_type call above may have already set the type for this DIE. */
17771 type = get_die_type (die, cu);
17772 if (type)
17773 return type;
17774
17775 type = lookup_reference_type (target_type, refcode);
17776 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17777 if (attr != nullptr)
17778 {
17779 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17780 }
17781 else
17782 {
17783 TYPE_LENGTH (type) = cu_header->addr_size;
17784 }
17785 maybe_set_alignment (cu, die, type);
17786 return set_die_type (die, type, cu);
17787 }
17788
17789 /* Add the given cv-qualifiers to the element type of the array. GCC
17790 outputs DWARF type qualifiers that apply to an array, not the
17791 element type. But GDB relies on the array element type to carry
17792 the cv-qualifiers. This mimics section 6.7.3 of the C99
17793 specification. */
17794
17795 static struct type *
17796 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17797 struct type *base_type, int cnst, int voltl)
17798 {
17799 struct type *el_type, *inner_array;
17800
17801 base_type = copy_type (base_type);
17802 inner_array = base_type;
17803
17804 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17805 {
17806 TYPE_TARGET_TYPE (inner_array) =
17807 copy_type (TYPE_TARGET_TYPE (inner_array));
17808 inner_array = TYPE_TARGET_TYPE (inner_array);
17809 }
17810
17811 el_type = TYPE_TARGET_TYPE (inner_array);
17812 cnst |= TYPE_CONST (el_type);
17813 voltl |= TYPE_VOLATILE (el_type);
17814 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17815
17816 return set_die_type (die, base_type, cu);
17817 }
17818
17819 static struct type *
17820 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17821 {
17822 struct type *base_type, *cv_type;
17823
17824 base_type = die_type (die, cu);
17825
17826 /* The die_type call above may have already set the type for this DIE. */
17827 cv_type = get_die_type (die, cu);
17828 if (cv_type)
17829 return cv_type;
17830
17831 /* In case the const qualifier is applied to an array type, the element type
17832 is so qualified, not the array type (section 6.7.3 of C99). */
17833 if (base_type->code () == TYPE_CODE_ARRAY)
17834 return add_array_cv_type (die, cu, base_type, 1, 0);
17835
17836 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17837 return set_die_type (die, cv_type, cu);
17838 }
17839
17840 static struct type *
17841 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17842 {
17843 struct type *base_type, *cv_type;
17844
17845 base_type = die_type (die, cu);
17846
17847 /* The die_type call above may have already set the type for this DIE. */
17848 cv_type = get_die_type (die, cu);
17849 if (cv_type)
17850 return cv_type;
17851
17852 /* In case the volatile qualifier is applied to an array type, the
17853 element type is so qualified, not the array type (section 6.7.3
17854 of C99). */
17855 if (base_type->code () == TYPE_CODE_ARRAY)
17856 return add_array_cv_type (die, cu, base_type, 0, 1);
17857
17858 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17859 return set_die_type (die, cv_type, cu);
17860 }
17861
17862 /* Handle DW_TAG_restrict_type. */
17863
17864 static struct type *
17865 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17866 {
17867 struct type *base_type, *cv_type;
17868
17869 base_type = die_type (die, cu);
17870
17871 /* The die_type call above may have already set the type for this DIE. */
17872 cv_type = get_die_type (die, cu);
17873 if (cv_type)
17874 return cv_type;
17875
17876 cv_type = make_restrict_type (base_type);
17877 return set_die_type (die, cv_type, cu);
17878 }
17879
17880 /* Handle DW_TAG_atomic_type. */
17881
17882 static struct type *
17883 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17884 {
17885 struct type *base_type, *cv_type;
17886
17887 base_type = die_type (die, cu);
17888
17889 /* The die_type call above may have already set the type for this DIE. */
17890 cv_type = get_die_type (die, cu);
17891 if (cv_type)
17892 return cv_type;
17893
17894 cv_type = make_atomic_type (base_type);
17895 return set_die_type (die, cv_type, cu);
17896 }
17897
17898 /* Extract all information from a DW_TAG_string_type DIE and add to
17899 the user defined type vector. It isn't really a user defined type,
17900 but it behaves like one, with other DIE's using an AT_user_def_type
17901 attribute to reference it. */
17902
17903 static struct type *
17904 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17905 {
17906 struct objfile *objfile = cu->per_objfile->objfile;
17907 struct gdbarch *gdbarch = objfile->arch ();
17908 struct type *type, *range_type, *index_type, *char_type;
17909 struct attribute *attr;
17910 struct dynamic_prop prop;
17911 bool length_is_constant = true;
17912 LONGEST length;
17913
17914 /* There are a couple of places where bit sizes might be made use of
17915 when parsing a DW_TAG_string_type, however, no producer that we know
17916 of make use of these. Handling bit sizes that are a multiple of the
17917 byte size is easy enough, but what about other bit sizes? Lets deal
17918 with that problem when we have to. Warn about these attributes being
17919 unsupported, then parse the type and ignore them like we always
17920 have. */
17921 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17922 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17923 {
17924 static bool warning_printed = false;
17925 if (!warning_printed)
17926 {
17927 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17928 "currently supported on DW_TAG_string_type."));
17929 warning_printed = true;
17930 }
17931 }
17932
17933 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17934 if (attr != nullptr && !attr->form_is_constant ())
17935 {
17936 /* The string length describes the location at which the length of
17937 the string can be found. The size of the length field can be
17938 specified with one of the attributes below. */
17939 struct type *prop_type;
17940 struct attribute *len
17941 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17942 if (len == nullptr)
17943 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17944 if (len != nullptr && len->form_is_constant ())
17945 {
17946 /* Pass 0 as the default as we know this attribute is constant
17947 and the default value will not be returned. */
17948 LONGEST sz = len->constant_value (0);
17949 prop_type = cu->per_objfile->int_type (sz, true);
17950 }
17951 else
17952 {
17953 /* If the size is not specified then we assume it is the size of
17954 an address on this target. */
17955 prop_type = cu->addr_sized_int_type (true);
17956 }
17957
17958 /* Convert the attribute into a dynamic property. */
17959 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17960 length = 1;
17961 else
17962 length_is_constant = false;
17963 }
17964 else if (attr != nullptr)
17965 {
17966 /* This DW_AT_string_length just contains the length with no
17967 indirection. There's no need to create a dynamic property in this
17968 case. Pass 0 for the default value as we know it will not be
17969 returned in this case. */
17970 length = attr->constant_value (0);
17971 }
17972 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17973 {
17974 /* We don't currently support non-constant byte sizes for strings. */
17975 length = attr->constant_value (1);
17976 }
17977 else
17978 {
17979 /* Use 1 as a fallback length if we have nothing else. */
17980 length = 1;
17981 }
17982
17983 index_type = objfile_type (objfile)->builtin_int;
17984 if (length_is_constant)
17985 range_type = create_static_range_type (NULL, index_type, 1, length);
17986 else
17987 {
17988 struct dynamic_prop low_bound;
17989
17990 low_bound.set_const_val (1);
17991 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17992 }
17993 char_type = language_string_char_type (cu->language_defn, gdbarch);
17994 type = create_string_type (NULL, char_type, range_type);
17995
17996 return set_die_type (die, type, cu);
17997 }
17998
17999 /* Assuming that DIE corresponds to a function, returns nonzero
18000 if the function is prototyped. */
18001
18002 static int
18003 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18004 {
18005 struct attribute *attr;
18006
18007 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
18008 if (attr && attr->as_boolean ())
18009 return 1;
18010
18011 /* The DWARF standard implies that the DW_AT_prototyped attribute
18012 is only meaningful for C, but the concept also extends to other
18013 languages that allow unprototyped functions (Eg: Objective C).
18014 For all other languages, assume that functions are always
18015 prototyped. */
18016 if (cu->language != language_c
18017 && cu->language != language_objc
18018 && cu->language != language_opencl)
18019 return 1;
18020
18021 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18022 prototyped and unprototyped functions; default to prototyped,
18023 since that is more common in modern code (and RealView warns
18024 about unprototyped functions). */
18025 if (producer_is_realview (cu->producer))
18026 return 1;
18027
18028 return 0;
18029 }
18030
18031 /* Handle DIES due to C code like:
18032
18033 struct foo
18034 {
18035 int (*funcp)(int a, long l);
18036 int b;
18037 };
18038
18039 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18040
18041 static struct type *
18042 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18043 {
18044 struct objfile *objfile = cu->per_objfile->objfile;
18045 struct type *type; /* Type that this function returns. */
18046 struct type *ftype; /* Function that returns above type. */
18047 struct attribute *attr;
18048
18049 type = die_type (die, cu);
18050
18051 /* The die_type call above may have already set the type for this DIE. */
18052 ftype = get_die_type (die, cu);
18053 if (ftype)
18054 return ftype;
18055
18056 ftype = lookup_function_type (type);
18057
18058 if (prototyped_function_p (die, cu))
18059 ftype->set_is_prototyped (true);
18060
18061 /* Store the calling convention in the type if it's available in
18062 the subroutine die. Otherwise set the calling convention to
18063 the default value DW_CC_normal. */
18064 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18065 if (attr != nullptr
18066 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18067 TYPE_CALLING_CONVENTION (ftype)
18068 = (enum dwarf_calling_convention) attr->constant_value (0);
18069 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18070 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18071 else
18072 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18073
18074 /* Record whether the function returns normally to its caller or not
18075 if the DWARF producer set that information. */
18076 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18077 if (attr && attr->as_boolean ())
18078 TYPE_NO_RETURN (ftype) = 1;
18079
18080 /* We need to add the subroutine type to the die immediately so
18081 we don't infinitely recurse when dealing with parameters
18082 declared as the same subroutine type. */
18083 set_die_type (die, ftype, cu);
18084
18085 if (die->child != NULL)
18086 {
18087 struct type *void_type = objfile_type (objfile)->builtin_void;
18088 struct die_info *child_die;
18089 int nparams, iparams;
18090
18091 /* Count the number of parameters.
18092 FIXME: GDB currently ignores vararg functions, but knows about
18093 vararg member functions. */
18094 nparams = 0;
18095 child_die = die->child;
18096 while (child_die && child_die->tag)
18097 {
18098 if (child_die->tag == DW_TAG_formal_parameter)
18099 nparams++;
18100 else if (child_die->tag == DW_TAG_unspecified_parameters)
18101 ftype->set_has_varargs (true);
18102
18103 child_die = child_die->sibling;
18104 }
18105
18106 /* Allocate storage for parameters and fill them in. */
18107 ftype->set_num_fields (nparams);
18108 ftype->set_fields
18109 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18110
18111 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18112 even if we error out during the parameters reading below. */
18113 for (iparams = 0; iparams < nparams; iparams++)
18114 ftype->field (iparams).set_type (void_type);
18115
18116 iparams = 0;
18117 child_die = die->child;
18118 while (child_die && child_die->tag)
18119 {
18120 if (child_die->tag == DW_TAG_formal_parameter)
18121 {
18122 struct type *arg_type;
18123
18124 /* DWARF version 2 has no clean way to discern C++
18125 static and non-static member functions. G++ helps
18126 GDB by marking the first parameter for non-static
18127 member functions (which is the this pointer) as
18128 artificial. We pass this information to
18129 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18130
18131 DWARF version 3 added DW_AT_object_pointer, which GCC
18132 4.5 does not yet generate. */
18133 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18134 if (attr != nullptr)
18135 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18136 else
18137 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18138 arg_type = die_type (child_die, cu);
18139
18140 /* RealView does not mark THIS as const, which the testsuite
18141 expects. GCC marks THIS as const in method definitions,
18142 but not in the class specifications (GCC PR 43053). */
18143 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18144 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18145 {
18146 int is_this = 0;
18147 struct dwarf2_cu *arg_cu = cu;
18148 const char *name = dwarf2_name (child_die, cu);
18149
18150 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18151 if (attr != nullptr)
18152 {
18153 /* If the compiler emits this, use it. */
18154 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18155 is_this = 1;
18156 }
18157 else if (name && strcmp (name, "this") == 0)
18158 /* Function definitions will have the argument names. */
18159 is_this = 1;
18160 else if (name == NULL && iparams == 0)
18161 /* Declarations may not have the names, so like
18162 elsewhere in GDB, assume an artificial first
18163 argument is "this". */
18164 is_this = 1;
18165
18166 if (is_this)
18167 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18168 arg_type, 0);
18169 }
18170
18171 ftype->field (iparams).set_type (arg_type);
18172 iparams++;
18173 }
18174 child_die = child_die->sibling;
18175 }
18176 }
18177
18178 return ftype;
18179 }
18180
18181 static struct type *
18182 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18183 {
18184 struct objfile *objfile = cu->per_objfile->objfile;
18185 const char *name = NULL;
18186 struct type *this_type, *target_type;
18187
18188 name = dwarf2_full_name (NULL, die, cu);
18189 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18190 this_type->set_target_is_stub (true);
18191 set_die_type (die, this_type, cu);
18192 target_type = die_type (die, cu);
18193 if (target_type != this_type)
18194 TYPE_TARGET_TYPE (this_type) = target_type;
18195 else
18196 {
18197 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18198 spec and cause infinite loops in GDB. */
18199 complaint (_("Self-referential DW_TAG_typedef "
18200 "- DIE at %s [in module %s]"),
18201 sect_offset_str (die->sect_off), objfile_name (objfile));
18202 TYPE_TARGET_TYPE (this_type) = NULL;
18203 }
18204 if (name == NULL)
18205 {
18206 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18207 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18208 Handle these by just returning the target type, rather than
18209 constructing an anonymous typedef type and trying to handle this
18210 elsewhere. */
18211 set_die_type (die, target_type, cu);
18212 return target_type;
18213 }
18214 return this_type;
18215 }
18216
18217 /* Helper for get_dwarf2_rational_constant that computes the value of
18218 a given gmp_mpz given an attribute. */
18219
18220 static void
18221 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18222 {
18223 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18224 location expression that pushes an implicit value. */
18225 if (attr->form == DW_FORM_exprloc)
18226 {
18227 dwarf_block *blk = attr->as_block ();
18228 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18229 {
18230 uint64_t len;
18231 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18232 blk->data + blk->size,
18233 &len);
18234 if (ptr - blk->data + len <= blk->size)
18235 {
18236 mpz_import (value->val, len,
18237 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18238 1, 0, 0, ptr);
18239 return;
18240 }
18241 }
18242
18243 /* On failure set it to 1. */
18244 *value = gdb_mpz (1);
18245 }
18246 else if (attr->form_is_block ())
18247 {
18248 dwarf_block *blk = attr->as_block ();
18249 mpz_import (value->val, blk->size,
18250 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18251 1, 0, 0, blk->data);
18252 }
18253 else
18254 *value = gdb_mpz (attr->constant_value (1));
18255 }
18256
18257 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18258 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18259
18260 If the numerator and/or numerator attribute is missing,
18261 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18262 untouched. */
18263
18264 static void
18265 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18266 gdb_mpz *numerator, gdb_mpz *denominator)
18267 {
18268 struct attribute *num_attr, *denom_attr;
18269
18270 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18271 if (num_attr == nullptr)
18272 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18273 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18274
18275 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18276 if (denom_attr == nullptr)
18277 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18278 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18279
18280 if (num_attr == nullptr || denom_attr == nullptr)
18281 return;
18282
18283 get_mpz (cu, numerator, num_attr);
18284 get_mpz (cu, denominator, denom_attr);
18285 }
18286
18287 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18288 rational constant, rather than a signed one.
18289
18290 If the rational constant has a negative value, a complaint
18291 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18292
18293 static void
18294 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18295 struct dwarf2_cu *cu,
18296 gdb_mpz *numerator,
18297 gdb_mpz *denominator)
18298 {
18299 gdb_mpz num (1);
18300 gdb_mpz denom (1);
18301
18302 get_dwarf2_rational_constant (die, cu, &num, &denom);
18303 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18304 {
18305 mpz_neg (num.val, num.val);
18306 mpz_neg (denom.val, denom.val);
18307 }
18308 else if (mpz_sgn (num.val) == -1)
18309 {
18310 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18311 " in DIE at %s"),
18312 sect_offset_str (die->sect_off));
18313 return;
18314 }
18315 else if (mpz_sgn (denom.val) == -1)
18316 {
18317 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18318 " in DIE at %s"),
18319 sect_offset_str (die->sect_off));
18320 return;
18321 }
18322
18323 *numerator = std::move (num);
18324 *denominator = std::move (denom);
18325 }
18326
18327 /* Assuming that ENCODING is a string whose contents starting at the
18328 K'th character is "_nn" where "nn" is a decimal number, scan that
18329 number and set RESULT to the value. K is updated to point to the
18330 character immediately following the number.
18331
18332 If the string does not conform to the format described above, false
18333 is returned, and K may or may not be changed. */
18334
18335 static bool
18336 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18337 {
18338 /* The next character should be an underscore ('_') followed
18339 by a digit. */
18340 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18341 return false;
18342
18343 /* Skip the underscore. */
18344 k++;
18345 int start = k;
18346
18347 /* Determine the number of digits for our number. */
18348 while (isdigit (encoding[k]))
18349 k++;
18350 if (k == start)
18351 return false;
18352
18353 std::string copy (&encoding[start], k - start);
18354 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18355 return false;
18356
18357 return true;
18358 }
18359
18360 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18361 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18362 DENOM, update OFFSET, and return true on success. Return false on
18363 failure. */
18364
18365 static bool
18366 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18367 gdb_mpz *num, gdb_mpz *denom)
18368 {
18369 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18370 return false;
18371 return ada_get_gnat_encoded_number (encoding, offset, denom);
18372 }
18373
18374 /* Assuming DIE corresponds to a fixed point type, finish the creation
18375 of the corresponding TYPE by setting its type-specific data. CU is
18376 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18377 encodings. It is nullptr if the GNAT encoding should be
18378 ignored. */
18379
18380 static void
18381 finish_fixed_point_type (struct type *type, const char *suffix,
18382 struct die_info *die, struct dwarf2_cu *cu)
18383 {
18384 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18385 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18386
18387 /* If GNAT encodings are preferred, don't examine the
18388 attributes. */
18389 struct attribute *attr = nullptr;
18390 if (suffix == nullptr)
18391 {
18392 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18393 if (attr == nullptr)
18394 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18395 if (attr == nullptr)
18396 attr = dwarf2_attr (die, DW_AT_small, cu);
18397 }
18398
18399 /* Numerator and denominator of our fixed-point type's scaling factor.
18400 The default is a scaling factor of 1, which we use as a fallback
18401 when we are not able to decode it (problem with the debugging info,
18402 unsupported forms, bug in GDB, etc...). Using that as the default
18403 allows us to at least print the unscaled value, which might still
18404 be useful to a user. */
18405 gdb_mpz scale_num (1);
18406 gdb_mpz scale_denom (1);
18407
18408 if (attr == nullptr)
18409 {
18410 int offset = 0;
18411 if (suffix != nullptr
18412 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18413 &scale_denom)
18414 /* The number might be encoded as _nn_dd_nn_dd, where the
18415 second ratio is the 'small value. In this situation, we
18416 want the second value. */
18417 && (suffix[offset] != '_'
18418 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18419 &scale_denom)))
18420 {
18421 /* Found it. */
18422 }
18423 else
18424 {
18425 /* Scaling factor not found. Assume a scaling factor of 1,
18426 and hope for the best. At least the user will be able to
18427 see the encoded value. */
18428 scale_num = 1;
18429 scale_denom = 1;
18430 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18431 sect_offset_str (die->sect_off));
18432 }
18433 }
18434 else if (attr->name == DW_AT_binary_scale)
18435 {
18436 LONGEST scale_exp = attr->constant_value (0);
18437 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18438
18439 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18440 }
18441 else if (attr->name == DW_AT_decimal_scale)
18442 {
18443 LONGEST scale_exp = attr->constant_value (0);
18444 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18445
18446 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18447 }
18448 else if (attr->name == DW_AT_small)
18449 {
18450 struct die_info *scale_die;
18451 struct dwarf2_cu *scale_cu = cu;
18452
18453 scale_die = follow_die_ref (die, attr, &scale_cu);
18454 if (scale_die->tag == DW_TAG_constant)
18455 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18456 &scale_num, &scale_denom);
18457 else
18458 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18459 " (DIE at %s)"),
18460 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18461 }
18462 else
18463 {
18464 complaint (_("unsupported scale attribute %s for fixed-point type"
18465 " (DIE at %s)"),
18466 dwarf_attr_name (attr->name),
18467 sect_offset_str (die->sect_off));
18468 }
18469
18470 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18471 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18472 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18473 mpq_canonicalize (scaling_factor.val);
18474 }
18475
18476 /* The gnat-encoding suffix for fixed point. */
18477
18478 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18479
18480 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18481 "XF" suffix of the name. The text after this is what encodes the
18482 'small and 'delta information. Otherwise, return nullptr. */
18483
18484 static const char *
18485 gnat_encoded_fixed_point_type_info (const char *name)
18486 {
18487 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18488 }
18489
18490 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18491 (which may be different from NAME) to the architecture back-end to allow
18492 it to guess the correct format if necessary. */
18493
18494 static struct type *
18495 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18496 const char *name_hint, enum bfd_endian byte_order)
18497 {
18498 struct gdbarch *gdbarch = objfile->arch ();
18499 const struct floatformat **format;
18500 struct type *type;
18501
18502 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18503 if (format)
18504 type = init_float_type (objfile, bits, name, format, byte_order);
18505 else
18506 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18507
18508 return type;
18509 }
18510
18511 /* Allocate an integer type of size BITS and name NAME. */
18512
18513 static struct type *
18514 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18515 int bits, int unsigned_p, const char *name)
18516 {
18517 struct type *type;
18518
18519 /* Versions of Intel's C Compiler generate an integer type called "void"
18520 instead of using DW_TAG_unspecified_type. This has been seen on
18521 at least versions 14, 17, and 18. */
18522 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18523 && strcmp (name, "void") == 0)
18524 type = objfile_type (objfile)->builtin_void;
18525 else
18526 type = init_integer_type (objfile, bits, unsigned_p, name);
18527
18528 return type;
18529 }
18530
18531 /* Return true if DIE has a DW_AT_small attribute whose value is
18532 a constant rational, where both the numerator and denominator
18533 are equal to zero.
18534
18535 CU is the DIE's Compilation Unit. */
18536
18537 static bool
18538 has_zero_over_zero_small_attribute (struct die_info *die,
18539 struct dwarf2_cu *cu)
18540 {
18541 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18542 if (attr == nullptr)
18543 return false;
18544
18545 struct dwarf2_cu *scale_cu = cu;
18546 struct die_info *scale_die
18547 = follow_die_ref (die, attr, &scale_cu);
18548
18549 if (scale_die->tag != DW_TAG_constant)
18550 return false;
18551
18552 gdb_mpz num (1), denom (1);
18553 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18554 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18555 }
18556
18557 /* Initialise and return a floating point type of size BITS suitable for
18558 use as a component of a complex number. The NAME_HINT is passed through
18559 when initialising the floating point type and is the name of the complex
18560 type.
18561
18562 As DWARF doesn't currently provide an explicit name for the components
18563 of a complex number, but it can be helpful to have these components
18564 named, we try to select a suitable name based on the size of the
18565 component. */
18566 static struct type *
18567 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18568 struct objfile *objfile,
18569 int bits, const char *name_hint,
18570 enum bfd_endian byte_order)
18571 {
18572 gdbarch *gdbarch = objfile->arch ();
18573 struct type *tt = nullptr;
18574
18575 /* Try to find a suitable floating point builtin type of size BITS.
18576 We're going to use the name of this type as the name for the complex
18577 target type that we are about to create. */
18578 switch (cu->language)
18579 {
18580 case language_fortran:
18581 switch (bits)
18582 {
18583 case 32:
18584 tt = builtin_f_type (gdbarch)->builtin_real;
18585 break;
18586 case 64:
18587 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18588 break;
18589 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18590 case 128:
18591 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18592 break;
18593 }
18594 break;
18595 default:
18596 switch (bits)
18597 {
18598 case 32:
18599 tt = builtin_type (gdbarch)->builtin_float;
18600 break;
18601 case 64:
18602 tt = builtin_type (gdbarch)->builtin_double;
18603 break;
18604 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18605 case 128:
18606 tt = builtin_type (gdbarch)->builtin_long_double;
18607 break;
18608 }
18609 break;
18610 }
18611
18612 /* If the type we found doesn't match the size we were looking for, then
18613 pretend we didn't find a type at all, the complex target type we
18614 create will then be nameless. */
18615 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18616 tt = nullptr;
18617
18618 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18619 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18620 }
18621
18622 /* Find a representation of a given base type and install
18623 it in the TYPE field of the die. */
18624
18625 static struct type *
18626 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18627 {
18628 struct objfile *objfile = cu->per_objfile->objfile;
18629 struct type *type;
18630 struct attribute *attr;
18631 int encoding = 0, bits = 0;
18632 const char *name;
18633 gdbarch *arch;
18634
18635 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18636 if (attr != nullptr && attr->form_is_constant ())
18637 encoding = attr->constant_value (0);
18638 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18639 if (attr != nullptr)
18640 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18641 name = dwarf2_name (die, cu);
18642 if (!name)
18643 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18644
18645 arch = objfile->arch ();
18646 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18647
18648 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18649 if (attr != nullptr && attr->form_is_constant ())
18650 {
18651 int endianity = attr->constant_value (0);
18652
18653 switch (endianity)
18654 {
18655 case DW_END_big:
18656 byte_order = BFD_ENDIAN_BIG;
18657 break;
18658 case DW_END_little:
18659 byte_order = BFD_ENDIAN_LITTLE;
18660 break;
18661 default:
18662 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18663 break;
18664 }
18665 }
18666
18667 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18668 && cu->language == language_ada
18669 && has_zero_over_zero_small_attribute (die, cu))
18670 {
18671 /* brobecker/2018-02-24: This is a fixed point type for which
18672 the scaling factor is represented as fraction whose value
18673 does not make sense (zero divided by zero), so we should
18674 normally never see these. However, there is a small category
18675 of fixed point types for which GNAT is unable to provide
18676 the scaling factor via the standard DWARF mechanisms, and
18677 for which the info is provided via the GNAT encodings instead.
18678 This is likely what this DIE is about. */
18679 encoding = (encoding == DW_ATE_signed_fixed
18680 ? DW_ATE_signed
18681 : DW_ATE_unsigned);
18682 }
18683
18684 /* With GNAT encodings, fixed-point information will be encoded in
18685 the type name. Note that this can also occur with the above
18686 zero-over-zero case, which is why this is a separate "if" rather
18687 than an "else if". */
18688 const char *gnat_encoding_suffix = nullptr;
18689 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18690 && cu->language == language_ada
18691 && name != nullptr)
18692 {
18693 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18694 if (gnat_encoding_suffix != nullptr)
18695 {
18696 gdb_assert (startswith (gnat_encoding_suffix,
18697 GNAT_FIXED_POINT_SUFFIX));
18698 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18699 name, gnat_encoding_suffix - name);
18700 /* Use -1 here so that SUFFIX points at the "_" after the
18701 "XF". */
18702 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18703
18704 encoding = (encoding == DW_ATE_signed
18705 ? DW_ATE_signed_fixed
18706 : DW_ATE_unsigned_fixed);
18707 }
18708 }
18709
18710 switch (encoding)
18711 {
18712 case DW_ATE_address:
18713 /* Turn DW_ATE_address into a void * pointer. */
18714 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18715 type = init_pointer_type (objfile, bits, name, type);
18716 break;
18717 case DW_ATE_boolean:
18718 type = init_boolean_type (objfile, bits, 1, name);
18719 break;
18720 case DW_ATE_complex_float:
18721 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18722 byte_order);
18723 if (type->code () == TYPE_CODE_ERROR)
18724 {
18725 if (name == nullptr)
18726 {
18727 struct obstack *obstack
18728 = &cu->per_objfile->objfile->objfile_obstack;
18729 name = obconcat (obstack, "_Complex ", type->name (),
18730 nullptr);
18731 }
18732 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18733 }
18734 else
18735 type = init_complex_type (name, type);
18736 break;
18737 case DW_ATE_decimal_float:
18738 type = init_decfloat_type (objfile, bits, name);
18739 break;
18740 case DW_ATE_float:
18741 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18742 break;
18743 case DW_ATE_signed:
18744 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18745 break;
18746 case DW_ATE_unsigned:
18747 if (cu->language == language_fortran
18748 && name
18749 && startswith (name, "character("))
18750 type = init_character_type (objfile, bits, 1, name);
18751 else
18752 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18753 break;
18754 case DW_ATE_signed_char:
18755 if (cu->language == language_ada || cu->language == language_m2
18756 || cu->language == language_pascal
18757 || cu->language == language_fortran)
18758 type = init_character_type (objfile, bits, 0, name);
18759 else
18760 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18761 break;
18762 case DW_ATE_unsigned_char:
18763 if (cu->language == language_ada || cu->language == language_m2
18764 || cu->language == language_pascal
18765 || cu->language == language_fortran
18766 || cu->language == language_rust)
18767 type = init_character_type (objfile, bits, 1, name);
18768 else
18769 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18770 break;
18771 case DW_ATE_UTF:
18772 {
18773 if (bits == 16)
18774 type = builtin_type (arch)->builtin_char16;
18775 else if (bits == 32)
18776 type = builtin_type (arch)->builtin_char32;
18777 else
18778 {
18779 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18780 bits);
18781 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18782 }
18783 return set_die_type (die, type, cu);
18784 }
18785 break;
18786 case DW_ATE_signed_fixed:
18787 type = init_fixed_point_type (objfile, bits, 0, name);
18788 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18789 break;
18790 case DW_ATE_unsigned_fixed:
18791 type = init_fixed_point_type (objfile, bits, 1, name);
18792 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18793 break;
18794
18795 default:
18796 complaint (_("unsupported DW_AT_encoding: '%s'"),
18797 dwarf_type_encoding_name (encoding));
18798 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18799 break;
18800 }
18801
18802 if (name && strcmp (name, "char") == 0)
18803 type->set_has_no_signedness (true);
18804
18805 maybe_set_alignment (cu, die, type);
18806
18807 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18808
18809 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18810 {
18811 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18812 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18813 {
18814 unsigned real_bit_size = attr->as_unsigned ();
18815 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18816 /* Only use the attributes if they make sense together. */
18817 if (attr == nullptr
18818 || (attr->as_unsigned () + real_bit_size
18819 <= 8 * TYPE_LENGTH (type)))
18820 {
18821 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18822 = real_bit_size;
18823 if (attr != nullptr)
18824 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18825 = attr->as_unsigned ();
18826 }
18827 }
18828 }
18829
18830 return set_die_type (die, type, cu);
18831 }
18832
18833 /* Parse dwarf attribute if it's a block, reference or constant and put the
18834 resulting value of the attribute into struct bound_prop.
18835 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18836
18837 static int
18838 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18839 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18840 struct type *default_type)
18841 {
18842 struct dwarf2_property_baton *baton;
18843 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18844 struct objfile *objfile = per_objfile->objfile;
18845 struct obstack *obstack = &objfile->objfile_obstack;
18846
18847 gdb_assert (default_type != NULL);
18848
18849 if (attr == NULL || prop == NULL)
18850 return 0;
18851
18852 if (attr->form_is_block ())
18853 {
18854 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18855 baton->property_type = default_type;
18856 baton->locexpr.per_cu = cu->per_cu;
18857 baton->locexpr.per_objfile = per_objfile;
18858
18859 struct dwarf_block *block = attr->as_block ();
18860 baton->locexpr.size = block->size;
18861 baton->locexpr.data = block->data;
18862 switch (attr->name)
18863 {
18864 case DW_AT_string_length:
18865 baton->locexpr.is_reference = true;
18866 break;
18867 default:
18868 baton->locexpr.is_reference = false;
18869 break;
18870 }
18871
18872 prop->set_locexpr (baton);
18873 gdb_assert (prop->baton () != NULL);
18874 }
18875 else if (attr->form_is_ref ())
18876 {
18877 struct dwarf2_cu *target_cu = cu;
18878 struct die_info *target_die;
18879 struct attribute *target_attr;
18880
18881 target_die = follow_die_ref (die, attr, &target_cu);
18882 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18883 if (target_attr == NULL)
18884 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18885 target_cu);
18886 if (target_attr == NULL)
18887 return 0;
18888
18889 switch (target_attr->name)
18890 {
18891 case DW_AT_location:
18892 if (target_attr->form_is_section_offset ())
18893 {
18894 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18895 baton->property_type = die_type (target_die, target_cu);
18896 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18897 prop->set_loclist (baton);
18898 gdb_assert (prop->baton () != NULL);
18899 }
18900 else if (target_attr->form_is_block ())
18901 {
18902 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18903 baton->property_type = die_type (target_die, target_cu);
18904 baton->locexpr.per_cu = cu->per_cu;
18905 baton->locexpr.per_objfile = per_objfile;
18906 struct dwarf_block *block = target_attr->as_block ();
18907 baton->locexpr.size = block->size;
18908 baton->locexpr.data = block->data;
18909 baton->locexpr.is_reference = true;
18910 prop->set_locexpr (baton);
18911 gdb_assert (prop->baton () != NULL);
18912 }
18913 else
18914 {
18915 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18916 "dynamic property");
18917 return 0;
18918 }
18919 break;
18920 case DW_AT_data_member_location:
18921 {
18922 LONGEST offset;
18923
18924 if (!handle_data_member_location (target_die, target_cu,
18925 &offset))
18926 return 0;
18927
18928 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18929 baton->property_type = read_type_die (target_die->parent,
18930 target_cu);
18931 baton->offset_info.offset = offset;
18932 baton->offset_info.type = die_type (target_die, target_cu);
18933 prop->set_addr_offset (baton);
18934 break;
18935 }
18936 }
18937 }
18938 else if (attr->form_is_constant ())
18939 prop->set_const_val (attr->constant_value (0));
18940 else
18941 {
18942 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18943 dwarf2_name (die, cu));
18944 return 0;
18945 }
18946
18947 return 1;
18948 }
18949
18950 /* See read.h. */
18951
18952 struct type *
18953 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18954 {
18955 struct type *int_type;
18956
18957 /* Helper macro to examine the various builtin types. */
18958 #define TRY_TYPE(F) \
18959 int_type = (unsigned_p \
18960 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18961 : objfile_type (objfile)->builtin_ ## F); \
18962 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18963 return int_type
18964
18965 TRY_TYPE (char);
18966 TRY_TYPE (short);
18967 TRY_TYPE (int);
18968 TRY_TYPE (long);
18969 TRY_TYPE (long_long);
18970
18971 #undef TRY_TYPE
18972
18973 gdb_assert_not_reached ("unable to find suitable integer type");
18974 }
18975
18976 /* See read.h. */
18977
18978 struct type *
18979 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18980 {
18981 int addr_size = this->per_cu->addr_size ();
18982 return this->per_objfile->int_type (addr_size, unsigned_p);
18983 }
18984
18985 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18986 present (which is valid) then compute the default type based on the
18987 compilation units address size. */
18988
18989 static struct type *
18990 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18991 {
18992 struct type *index_type = die_type (die, cu);
18993
18994 /* Dwarf-2 specifications explicitly allows to create subrange types
18995 without specifying a base type.
18996 In that case, the base type must be set to the type of
18997 the lower bound, upper bound or count, in that order, if any of these
18998 three attributes references an object that has a type.
18999 If no base type is found, the Dwarf-2 specifications say that
19000 a signed integer type of size equal to the size of an address should
19001 be used.
19002 For the following C code: `extern char gdb_int [];'
19003 GCC produces an empty range DIE.
19004 FIXME: muller/2010-05-28: Possible references to object for low bound,
19005 high bound or count are not yet handled by this code. */
19006 if (index_type->code () == TYPE_CODE_VOID)
19007 index_type = cu->addr_sized_int_type (false);
19008
19009 return index_type;
19010 }
19011
19012 /* Read the given DW_AT_subrange DIE. */
19013
19014 static struct type *
19015 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
19016 {
19017 struct type *base_type, *orig_base_type;
19018 struct type *range_type;
19019 struct attribute *attr;
19020 struct dynamic_prop low, high;
19021 int low_default_is_valid;
19022 int high_bound_is_count = 0;
19023 const char *name;
19024 ULONGEST negative_mask;
19025
19026 orig_base_type = read_subrange_index_type (die, cu);
19027
19028 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
19029 whereas the real type might be. So, we use ORIG_BASE_TYPE when
19030 creating the range type, but we use the result of check_typedef
19031 when examining properties of the type. */
19032 base_type = check_typedef (orig_base_type);
19033
19034 /* The die_type call above may have already set the type for this DIE. */
19035 range_type = get_die_type (die, cu);
19036 if (range_type)
19037 return range_type;
19038
19039 high.set_const_val (0);
19040
19041 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19042 omitting DW_AT_lower_bound. */
19043 switch (cu->language)
19044 {
19045 case language_c:
19046 case language_cplus:
19047 low.set_const_val (0);
19048 low_default_is_valid = 1;
19049 break;
19050 case language_fortran:
19051 low.set_const_val (1);
19052 low_default_is_valid = 1;
19053 break;
19054 case language_d:
19055 case language_objc:
19056 case language_rust:
19057 low.set_const_val (0);
19058 low_default_is_valid = (cu->header.version >= 4);
19059 break;
19060 case language_ada:
19061 case language_m2:
19062 case language_pascal:
19063 low.set_const_val (1);
19064 low_default_is_valid = (cu->header.version >= 4);
19065 break;
19066 default:
19067 low.set_const_val (0);
19068 low_default_is_valid = 0;
19069 break;
19070 }
19071
19072 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
19073 if (attr != nullptr)
19074 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
19075 else if (!low_default_is_valid)
19076 complaint (_("Missing DW_AT_lower_bound "
19077 "- DIE at %s [in module %s]"),
19078 sect_offset_str (die->sect_off),
19079 objfile_name (cu->per_objfile->objfile));
19080
19081 struct attribute *attr_ub, *attr_count;
19082 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
19083 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19084 {
19085 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
19086 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19087 {
19088 /* If bounds are constant do the final calculation here. */
19089 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19090 high.set_const_val (low.const_val () + high.const_val () - 1);
19091 else
19092 high_bound_is_count = 1;
19093 }
19094 else
19095 {
19096 if (attr_ub != NULL)
19097 complaint (_("Unresolved DW_AT_upper_bound "
19098 "- DIE at %s [in module %s]"),
19099 sect_offset_str (die->sect_off),
19100 objfile_name (cu->per_objfile->objfile));
19101 if (attr_count != NULL)
19102 complaint (_("Unresolved DW_AT_count "
19103 "- DIE at %s [in module %s]"),
19104 sect_offset_str (die->sect_off),
19105 objfile_name (cu->per_objfile->objfile));
19106 }
19107 }
19108
19109 LONGEST bias = 0;
19110 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
19111 if (bias_attr != nullptr && bias_attr->form_is_constant ())
19112 bias = bias_attr->constant_value (0);
19113
19114 /* Normally, the DWARF producers are expected to use a signed
19115 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19116 But this is unfortunately not always the case, as witnessed
19117 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19118 is used instead. To work around that ambiguity, we treat
19119 the bounds as signed, and thus sign-extend their values, when
19120 the base type is signed. */
19121 negative_mask =
19122 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19123 if (low.kind () == PROP_CONST
19124 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19125 low.set_const_val (low.const_val () | negative_mask);
19126 if (high.kind () == PROP_CONST
19127 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19128 high.set_const_val (high.const_val () | negative_mask);
19129
19130 /* Check for bit and byte strides. */
19131 struct dynamic_prop byte_stride_prop;
19132 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19133 if (attr_byte_stride != nullptr)
19134 {
19135 struct type *prop_type = cu->addr_sized_int_type (false);
19136 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19137 prop_type);
19138 }
19139
19140 struct dynamic_prop bit_stride_prop;
19141 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19142 if (attr_bit_stride != nullptr)
19143 {
19144 /* It only makes sense to have either a bit or byte stride. */
19145 if (attr_byte_stride != nullptr)
19146 {
19147 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19148 "- DIE at %s [in module %s]"),
19149 sect_offset_str (die->sect_off),
19150 objfile_name (cu->per_objfile->objfile));
19151 attr_bit_stride = nullptr;
19152 }
19153 else
19154 {
19155 struct type *prop_type = cu->addr_sized_int_type (false);
19156 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19157 prop_type);
19158 }
19159 }
19160
19161 if (attr_byte_stride != nullptr
19162 || attr_bit_stride != nullptr)
19163 {
19164 bool byte_stride_p = (attr_byte_stride != nullptr);
19165 struct dynamic_prop *stride
19166 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19167
19168 range_type
19169 = create_range_type_with_stride (NULL, orig_base_type, &low,
19170 &high, bias, stride, byte_stride_p);
19171 }
19172 else
19173 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19174
19175 if (high_bound_is_count)
19176 range_type->bounds ()->flag_upper_bound_is_count = 1;
19177
19178 /* Ada expects an empty array on no boundary attributes. */
19179 if (attr == NULL && cu->language != language_ada)
19180 range_type->bounds ()->high.set_undefined ();
19181
19182 name = dwarf2_name (die, cu);
19183 if (name)
19184 range_type->set_name (name);
19185
19186 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19187 if (attr != nullptr)
19188 TYPE_LENGTH (range_type) = attr->constant_value (0);
19189
19190 maybe_set_alignment (cu, die, range_type);
19191
19192 set_die_type (die, range_type, cu);
19193
19194 /* set_die_type should be already done. */
19195 set_descriptive_type (range_type, die, cu);
19196
19197 return range_type;
19198 }
19199
19200 static struct type *
19201 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19202 {
19203 struct type *type;
19204
19205 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19206 type->set_name (dwarf2_name (die, cu));
19207
19208 /* In Ada, an unspecified type is typically used when the description
19209 of the type is deferred to a different unit. When encountering
19210 such a type, we treat it as a stub, and try to resolve it later on,
19211 when needed. */
19212 if (cu->language == language_ada)
19213 type->set_is_stub (true);
19214
19215 return set_die_type (die, type, cu);
19216 }
19217
19218 /* Read a single die and all its descendents. Set the die's sibling
19219 field to NULL; set other fields in the die correctly, and set all
19220 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19221 location of the info_ptr after reading all of those dies. PARENT
19222 is the parent of the die in question. */
19223
19224 static struct die_info *
19225 read_die_and_children (const struct die_reader_specs *reader,
19226 const gdb_byte *info_ptr,
19227 const gdb_byte **new_info_ptr,
19228 struct die_info *parent)
19229 {
19230 struct die_info *die;
19231 const gdb_byte *cur_ptr;
19232
19233 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19234 if (die == NULL)
19235 {
19236 *new_info_ptr = cur_ptr;
19237 return NULL;
19238 }
19239 store_in_ref_table (die, reader->cu);
19240
19241 if (die->has_children)
19242 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19243 else
19244 {
19245 die->child = NULL;
19246 *new_info_ptr = cur_ptr;
19247 }
19248
19249 die->sibling = NULL;
19250 die->parent = parent;
19251 return die;
19252 }
19253
19254 /* Read a die, all of its descendents, and all of its siblings; set
19255 all of the fields of all of the dies correctly. Arguments are as
19256 in read_die_and_children. */
19257
19258 static struct die_info *
19259 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19260 const gdb_byte *info_ptr,
19261 const gdb_byte **new_info_ptr,
19262 struct die_info *parent)
19263 {
19264 struct die_info *first_die, *last_sibling;
19265 const gdb_byte *cur_ptr;
19266
19267 cur_ptr = info_ptr;
19268 first_die = last_sibling = NULL;
19269
19270 while (1)
19271 {
19272 struct die_info *die
19273 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19274
19275 if (die == NULL)
19276 {
19277 *new_info_ptr = cur_ptr;
19278 return first_die;
19279 }
19280
19281 if (!first_die)
19282 first_die = die;
19283 else
19284 last_sibling->sibling = die;
19285
19286 last_sibling = die;
19287 }
19288 }
19289
19290 /* Read a die, all of its descendents, and all of its siblings; set
19291 all of the fields of all of the dies correctly. Arguments are as
19292 in read_die_and_children.
19293 This the main entry point for reading a DIE and all its children. */
19294
19295 static struct die_info *
19296 read_die_and_siblings (const struct die_reader_specs *reader,
19297 const gdb_byte *info_ptr,
19298 const gdb_byte **new_info_ptr,
19299 struct die_info *parent)
19300 {
19301 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19302 new_info_ptr, parent);
19303
19304 if (dwarf_die_debug)
19305 {
19306 fprintf_unfiltered (gdb_stdlog,
19307 "Read die from %s@0x%x of %s:\n",
19308 reader->die_section->get_name (),
19309 (unsigned) (info_ptr - reader->die_section->buffer),
19310 bfd_get_filename (reader->abfd));
19311 dump_die (die, dwarf_die_debug);
19312 }
19313
19314 return die;
19315 }
19316
19317 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19318 attributes.
19319 The caller is responsible for filling in the extra attributes
19320 and updating (*DIEP)->num_attrs.
19321 Set DIEP to point to a newly allocated die with its information,
19322 except for its child, sibling, and parent fields. */
19323
19324 static const gdb_byte *
19325 read_full_die_1 (const struct die_reader_specs *reader,
19326 struct die_info **diep, const gdb_byte *info_ptr,
19327 int num_extra_attrs)
19328 {
19329 unsigned int abbrev_number, bytes_read, i;
19330 const struct abbrev_info *abbrev;
19331 struct die_info *die;
19332 struct dwarf2_cu *cu = reader->cu;
19333 bfd *abfd = reader->abfd;
19334
19335 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19336 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19337 info_ptr += bytes_read;
19338 if (!abbrev_number)
19339 {
19340 *diep = NULL;
19341 return info_ptr;
19342 }
19343
19344 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19345 if (!abbrev)
19346 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19347 abbrev_number,
19348 bfd_get_filename (abfd));
19349
19350 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19351 die->sect_off = sect_off;
19352 die->tag = abbrev->tag;
19353 die->abbrev = abbrev_number;
19354 die->has_children = abbrev->has_children;
19355
19356 /* Make the result usable.
19357 The caller needs to update num_attrs after adding the extra
19358 attributes. */
19359 die->num_attrs = abbrev->num_attrs;
19360
19361 bool any_need_reprocess = false;
19362 for (i = 0; i < abbrev->num_attrs; ++i)
19363 {
19364 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19365 info_ptr);
19366 if (die->attrs[i].requires_reprocessing_p ())
19367 any_need_reprocess = true;
19368 }
19369
19370 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19371 if (attr != nullptr && attr->form_is_unsigned ())
19372 cu->str_offsets_base = attr->as_unsigned ();
19373
19374 attr = die->attr (DW_AT_loclists_base);
19375 if (attr != nullptr)
19376 cu->loclist_base = attr->as_unsigned ();
19377
19378 auto maybe_addr_base = die->addr_base ();
19379 if (maybe_addr_base.has_value ())
19380 cu->addr_base = *maybe_addr_base;
19381
19382 attr = die->attr (DW_AT_rnglists_base);
19383 if (attr != nullptr)
19384 cu->rnglists_base = attr->as_unsigned ();
19385
19386 if (any_need_reprocess)
19387 {
19388 for (i = 0; i < abbrev->num_attrs; ++i)
19389 {
19390 if (die->attrs[i].requires_reprocessing_p ())
19391 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19392 }
19393 }
19394 *diep = die;
19395 return info_ptr;
19396 }
19397
19398 /* Read a die and all its attributes.
19399 Set DIEP to point to a newly allocated die with its information,
19400 except for its child, sibling, and parent fields. */
19401
19402 static const gdb_byte *
19403 read_full_die (const struct die_reader_specs *reader,
19404 struct die_info **diep, const gdb_byte *info_ptr)
19405 {
19406 const gdb_byte *result;
19407
19408 result = read_full_die_1 (reader, diep, info_ptr, 0);
19409
19410 if (dwarf_die_debug)
19411 {
19412 fprintf_unfiltered (gdb_stdlog,
19413 "Read die from %s@0x%x of %s:\n",
19414 reader->die_section->get_name (),
19415 (unsigned) (info_ptr - reader->die_section->buffer),
19416 bfd_get_filename (reader->abfd));
19417 dump_die (*diep, dwarf_die_debug);
19418 }
19419
19420 return result;
19421 }
19422 \f
19423
19424 /* Returns nonzero if TAG represents a type that we might generate a partial
19425 symbol for. */
19426
19427 static int
19428 is_type_tag_for_partial (int tag, enum language lang)
19429 {
19430 switch (tag)
19431 {
19432 #if 0
19433 /* Some types that would be reasonable to generate partial symbols for,
19434 that we don't at present. Note that normally this does not
19435 matter, mainly because C compilers don't give names to these
19436 types, but instead emit DW_TAG_typedef. */
19437 case DW_TAG_file_type:
19438 case DW_TAG_ptr_to_member_type:
19439 case DW_TAG_set_type:
19440 case DW_TAG_string_type:
19441 case DW_TAG_subroutine_type:
19442 #endif
19443
19444 /* GNAT may emit an array with a name, but no typedef, so we
19445 need to make a symbol in this case. */
19446 case DW_TAG_array_type:
19447 return lang == language_ada;
19448
19449 case DW_TAG_base_type:
19450 case DW_TAG_class_type:
19451 case DW_TAG_interface_type:
19452 case DW_TAG_enumeration_type:
19453 case DW_TAG_structure_type:
19454 case DW_TAG_subrange_type:
19455 case DW_TAG_typedef:
19456 case DW_TAG_union_type:
19457 return 1;
19458 default:
19459 return 0;
19460 }
19461 }
19462
19463 /* Load all DIEs that are interesting for partial symbols into memory. */
19464
19465 static struct partial_die_info *
19466 load_partial_dies (const struct die_reader_specs *reader,
19467 const gdb_byte *info_ptr, int building_psymtab)
19468 {
19469 struct dwarf2_cu *cu = reader->cu;
19470 struct objfile *objfile = cu->per_objfile->objfile;
19471 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19472 unsigned int bytes_read;
19473 unsigned int load_all = 0;
19474 int nesting_level = 1;
19475
19476 parent_die = NULL;
19477 last_die = NULL;
19478
19479 gdb_assert (cu->per_cu != NULL);
19480 if (cu->per_cu->load_all_dies)
19481 load_all = 1;
19482
19483 cu->partial_dies
19484 = htab_create_alloc_ex (cu->header.length / 12,
19485 partial_die_hash,
19486 partial_die_eq,
19487 NULL,
19488 &cu->comp_unit_obstack,
19489 hashtab_obstack_allocate,
19490 dummy_obstack_deallocate);
19491
19492 while (1)
19493 {
19494 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19495 &bytes_read);
19496
19497 /* A NULL abbrev means the end of a series of children. */
19498 if (abbrev == NULL)
19499 {
19500 if (--nesting_level == 0)
19501 return first_die;
19502
19503 info_ptr += bytes_read;
19504 last_die = parent_die;
19505 parent_die = parent_die->die_parent;
19506 continue;
19507 }
19508
19509 /* Check for template arguments. We never save these; if
19510 they're seen, we just mark the parent, and go on our way. */
19511 if (parent_die != NULL
19512 && cu->language == language_cplus
19513 && (abbrev->tag == DW_TAG_template_type_param
19514 || abbrev->tag == DW_TAG_template_value_param))
19515 {
19516 parent_die->has_template_arguments = 1;
19517
19518 if (!load_all)
19519 {
19520 /* We don't need a partial DIE for the template argument. */
19521 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19522 continue;
19523 }
19524 }
19525
19526 /* We only recurse into c++ subprograms looking for template arguments.
19527 Skip their other children. */
19528 if (!load_all
19529 && cu->language == language_cplus
19530 && parent_die != NULL
19531 && parent_die->tag == DW_TAG_subprogram
19532 && abbrev->tag != DW_TAG_inlined_subroutine)
19533 {
19534 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19535 continue;
19536 }
19537
19538 /* Check whether this DIE is interesting enough to save. Normally
19539 we would not be interested in members here, but there may be
19540 later variables referencing them via DW_AT_specification (for
19541 static members). */
19542 if (!load_all
19543 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19544 && abbrev->tag != DW_TAG_constant
19545 && abbrev->tag != DW_TAG_enumerator
19546 && abbrev->tag != DW_TAG_subprogram
19547 && abbrev->tag != DW_TAG_inlined_subroutine
19548 && abbrev->tag != DW_TAG_lexical_block
19549 && abbrev->tag != DW_TAG_variable
19550 && abbrev->tag != DW_TAG_namespace
19551 && abbrev->tag != DW_TAG_module
19552 && abbrev->tag != DW_TAG_member
19553 && abbrev->tag != DW_TAG_imported_unit
19554 && abbrev->tag != DW_TAG_imported_declaration)
19555 {
19556 /* Otherwise we skip to the next sibling, if any. */
19557 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19558 continue;
19559 }
19560
19561 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19562 abbrev);
19563
19564 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19565
19566 /* This two-pass algorithm for processing partial symbols has a
19567 high cost in cache pressure. Thus, handle some simple cases
19568 here which cover the majority of C partial symbols. DIEs
19569 which neither have specification tags in them, nor could have
19570 specification tags elsewhere pointing at them, can simply be
19571 processed and discarded.
19572
19573 This segment is also optional; scan_partial_symbols and
19574 add_partial_symbol will handle these DIEs if we chain
19575 them in normally. When compilers which do not emit large
19576 quantities of duplicate debug information are more common,
19577 this code can probably be removed. */
19578
19579 /* Any complete simple types at the top level (pretty much all
19580 of them, for a language without namespaces), can be processed
19581 directly. */
19582 if (parent_die == NULL
19583 && pdi.has_specification == 0
19584 && pdi.is_declaration == 0
19585 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19586 || pdi.tag == DW_TAG_base_type
19587 || pdi.tag == DW_TAG_array_type
19588 || pdi.tag == DW_TAG_subrange_type))
19589 {
19590 if (building_psymtab && pdi.raw_name != NULL)
19591 add_partial_symbol (&pdi, cu);
19592
19593 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19594 continue;
19595 }
19596
19597 /* The exception for DW_TAG_typedef with has_children above is
19598 a workaround of GCC PR debug/47510. In the case of this complaint
19599 type_name_or_error will error on such types later.
19600
19601 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19602 it could not find the child DIEs referenced later, this is checked
19603 above. In correct DWARF DW_TAG_typedef should have no children. */
19604
19605 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19606 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19607 "- DIE at %s [in module %s]"),
19608 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19609
19610 /* If we're at the second level, and we're an enumerator, and
19611 our parent has no specification (meaning possibly lives in a
19612 namespace elsewhere), then we can add the partial symbol now
19613 instead of queueing it. */
19614 if (pdi.tag == DW_TAG_enumerator
19615 && parent_die != NULL
19616 && parent_die->die_parent == NULL
19617 && parent_die->tag == DW_TAG_enumeration_type
19618 && parent_die->has_specification == 0)
19619 {
19620 if (pdi.raw_name == NULL)
19621 complaint (_("malformed enumerator DIE ignored"));
19622 else if (building_psymtab)
19623 add_partial_symbol (&pdi, cu);
19624
19625 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19626 continue;
19627 }
19628
19629 struct partial_die_info *part_die
19630 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19631
19632 /* We'll save this DIE so link it in. */
19633 part_die->die_parent = parent_die;
19634 part_die->die_sibling = NULL;
19635 part_die->die_child = NULL;
19636
19637 if (last_die && last_die == parent_die)
19638 last_die->die_child = part_die;
19639 else if (last_die)
19640 last_die->die_sibling = part_die;
19641
19642 last_die = part_die;
19643
19644 if (first_die == NULL)
19645 first_die = part_die;
19646
19647 /* Maybe add the DIE to the hash table. Not all DIEs that we
19648 find interesting need to be in the hash table, because we
19649 also have the parent/sibling/child chains; only those that we
19650 might refer to by offset later during partial symbol reading.
19651
19652 For now this means things that might have be the target of a
19653 DW_AT_specification, DW_AT_abstract_origin, or
19654 DW_AT_extension. DW_AT_extension will refer only to
19655 namespaces; DW_AT_abstract_origin refers to functions (and
19656 many things under the function DIE, but we do not recurse
19657 into function DIEs during partial symbol reading) and
19658 possibly variables as well; DW_AT_specification refers to
19659 declarations. Declarations ought to have the DW_AT_declaration
19660 flag. It happens that GCC forgets to put it in sometimes, but
19661 only for functions, not for types.
19662
19663 Adding more things than necessary to the hash table is harmless
19664 except for the performance cost. Adding too few will result in
19665 wasted time in find_partial_die, when we reread the compilation
19666 unit with load_all_dies set. */
19667
19668 if (load_all
19669 || abbrev->tag == DW_TAG_constant
19670 || abbrev->tag == DW_TAG_subprogram
19671 || abbrev->tag == DW_TAG_variable
19672 || abbrev->tag == DW_TAG_namespace
19673 || part_die->is_declaration)
19674 {
19675 void **slot;
19676
19677 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19678 to_underlying (part_die->sect_off),
19679 INSERT);
19680 *slot = part_die;
19681 }
19682
19683 /* For some DIEs we want to follow their children (if any). For C
19684 we have no reason to follow the children of structures; for other
19685 languages we have to, so that we can get at method physnames
19686 to infer fully qualified class names, for DW_AT_specification,
19687 and for C++ template arguments. For C++, we also look one level
19688 inside functions to find template arguments (if the name of the
19689 function does not already contain the template arguments).
19690
19691 For Ada and Fortran, we need to scan the children of subprograms
19692 and lexical blocks as well because these languages allow the
19693 definition of nested entities that could be interesting for the
19694 debugger, such as nested subprograms for instance. */
19695 if (last_die->has_children
19696 && (load_all
19697 || last_die->tag == DW_TAG_namespace
19698 || last_die->tag == DW_TAG_module
19699 || last_die->tag == DW_TAG_enumeration_type
19700 || (cu->language == language_cplus
19701 && last_die->tag == DW_TAG_subprogram
19702 && (last_die->raw_name == NULL
19703 || strchr (last_die->raw_name, '<') == NULL))
19704 || (cu->language != language_c
19705 && (last_die->tag == DW_TAG_class_type
19706 || last_die->tag == DW_TAG_interface_type
19707 || last_die->tag == DW_TAG_structure_type
19708 || last_die->tag == DW_TAG_union_type))
19709 || ((cu->language == language_ada
19710 || cu->language == language_fortran)
19711 && (last_die->tag == DW_TAG_subprogram
19712 || last_die->tag == DW_TAG_lexical_block))))
19713 {
19714 nesting_level++;
19715 parent_die = last_die;
19716 continue;
19717 }
19718
19719 /* Otherwise we skip to the next sibling, if any. */
19720 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19721
19722 /* Back to the top, do it again. */
19723 }
19724 }
19725
19726 partial_die_info::partial_die_info (sect_offset sect_off_,
19727 const struct abbrev_info *abbrev)
19728 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19729 {
19730 }
19731
19732 /* See class definition. */
19733
19734 const char *
19735 partial_die_info::name (dwarf2_cu *cu)
19736 {
19737 if (!canonical_name && raw_name != nullptr)
19738 {
19739 struct objfile *objfile = cu->per_objfile->objfile;
19740 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19741 canonical_name = 1;
19742 }
19743
19744 return raw_name;
19745 }
19746
19747 /* Read a minimal amount of information into the minimal die structure.
19748 INFO_PTR should point just after the initial uleb128 of a DIE. */
19749
19750 const gdb_byte *
19751 partial_die_info::read (const struct die_reader_specs *reader,
19752 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19753 {
19754 struct dwarf2_cu *cu = reader->cu;
19755 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19756 unsigned int i;
19757 int has_low_pc_attr = 0;
19758 int has_high_pc_attr = 0;
19759 int high_pc_relative = 0;
19760
19761 for (i = 0; i < abbrev.num_attrs; ++i)
19762 {
19763 attribute attr;
19764 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19765 /* String and address offsets that need to do the reprocessing have
19766 already been read at this point, so there is no need to wait until
19767 the loop terminates to do the reprocessing. */
19768 if (attr.requires_reprocessing_p ())
19769 read_attribute_reprocess (reader, &attr, tag);
19770 /* Store the data if it is of an attribute we want to keep in a
19771 partial symbol table. */
19772 switch (attr.name)
19773 {
19774 case DW_AT_name:
19775 switch (tag)
19776 {
19777 case DW_TAG_compile_unit:
19778 case DW_TAG_partial_unit:
19779 case DW_TAG_type_unit:
19780 /* Compilation units have a DW_AT_name that is a filename, not
19781 a source language identifier. */
19782 case DW_TAG_enumeration_type:
19783 case DW_TAG_enumerator:
19784 /* These tags always have simple identifiers already; no need
19785 to canonicalize them. */
19786 canonical_name = 1;
19787 raw_name = attr.as_string ();
19788 break;
19789 default:
19790 canonical_name = 0;
19791 raw_name = attr.as_string ();
19792 break;
19793 }
19794 break;
19795 case DW_AT_linkage_name:
19796 case DW_AT_MIPS_linkage_name:
19797 /* Note that both forms of linkage name might appear. We
19798 assume they will be the same, and we only store the last
19799 one we see. */
19800 linkage_name = attr.as_string ();
19801 break;
19802 case DW_AT_low_pc:
19803 has_low_pc_attr = 1;
19804 lowpc = attr.as_address ();
19805 break;
19806 case DW_AT_high_pc:
19807 has_high_pc_attr = 1;
19808 highpc = attr.as_address ();
19809 if (cu->header.version >= 4 && attr.form_is_constant ())
19810 high_pc_relative = 1;
19811 break;
19812 case DW_AT_location:
19813 /* Support the .debug_loc offsets. */
19814 if (attr.form_is_block ())
19815 {
19816 d.locdesc = attr.as_block ();
19817 }
19818 else if (attr.form_is_section_offset ())
19819 {
19820 dwarf2_complex_location_expr_complaint ();
19821 }
19822 else
19823 {
19824 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19825 "partial symbol information");
19826 }
19827 break;
19828 case DW_AT_external:
19829 is_external = attr.as_boolean ();
19830 break;
19831 case DW_AT_declaration:
19832 is_declaration = attr.as_boolean ();
19833 break;
19834 case DW_AT_type:
19835 has_type = 1;
19836 break;
19837 case DW_AT_abstract_origin:
19838 case DW_AT_specification:
19839 case DW_AT_extension:
19840 has_specification = 1;
19841 spec_offset = attr.get_ref_die_offset ();
19842 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19843 || cu->per_cu->is_dwz);
19844 break;
19845 case DW_AT_sibling:
19846 /* Ignore absolute siblings, they might point outside of
19847 the current compile unit. */
19848 if (attr.form == DW_FORM_ref_addr)
19849 complaint (_("ignoring absolute DW_AT_sibling"));
19850 else
19851 {
19852 const gdb_byte *buffer = reader->buffer;
19853 sect_offset off = attr.get_ref_die_offset ();
19854 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19855
19856 if (sibling_ptr < info_ptr)
19857 complaint (_("DW_AT_sibling points backwards"));
19858 else if (sibling_ptr > reader->buffer_end)
19859 reader->die_section->overflow_complaint ();
19860 else
19861 sibling = sibling_ptr;
19862 }
19863 break;
19864 case DW_AT_byte_size:
19865 has_byte_size = 1;
19866 break;
19867 case DW_AT_const_value:
19868 has_const_value = 1;
19869 break;
19870 case DW_AT_calling_convention:
19871 /* DWARF doesn't provide a way to identify a program's source-level
19872 entry point. DW_AT_calling_convention attributes are only meant
19873 to describe functions' calling conventions.
19874
19875 However, because it's a necessary piece of information in
19876 Fortran, and before DWARF 4 DW_CC_program was the only
19877 piece of debugging information whose definition refers to
19878 a 'main program' at all, several compilers marked Fortran
19879 main programs with DW_CC_program --- even when those
19880 functions use the standard calling conventions.
19881
19882 Although DWARF now specifies a way to provide this
19883 information, we support this practice for backward
19884 compatibility. */
19885 if (attr.constant_value (0) == DW_CC_program
19886 && cu->language == language_fortran)
19887 main_subprogram = 1;
19888 break;
19889 case DW_AT_inline:
19890 {
19891 LONGEST value = attr.constant_value (-1);
19892 if (value == DW_INL_inlined
19893 || value == DW_INL_declared_inlined)
19894 may_be_inlined = 1;
19895 }
19896 break;
19897
19898 case DW_AT_import:
19899 if (tag == DW_TAG_imported_unit)
19900 {
19901 d.sect_off = attr.get_ref_die_offset ();
19902 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19903 || cu->per_cu->is_dwz);
19904 }
19905 break;
19906
19907 case DW_AT_main_subprogram:
19908 main_subprogram = attr.as_boolean ();
19909 break;
19910
19911 case DW_AT_ranges:
19912 {
19913 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19914 on DWARF version). */
19915 ULONGEST ranges_offset = attr.as_unsigned ();
19916
19917 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19918 this value. */
19919 if (tag != DW_TAG_compile_unit)
19920 ranges_offset += cu->gnu_ranges_base;
19921
19922 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19923 nullptr, tag))
19924 has_pc_info = 1;
19925 }
19926 break;
19927
19928 default:
19929 break;
19930 }
19931 }
19932
19933 /* For Ada, if both the name and the linkage name appear, we prefer
19934 the latter. This lets "catch exception" work better, regardless
19935 of the order in which the name and linkage name were emitted.
19936 Really, though, this is just a workaround for the fact that gdb
19937 doesn't store both the name and the linkage name. */
19938 if (cu->language == language_ada && linkage_name != nullptr)
19939 raw_name = linkage_name;
19940
19941 if (high_pc_relative)
19942 highpc += lowpc;
19943
19944 if (has_low_pc_attr && has_high_pc_attr)
19945 {
19946 /* When using the GNU linker, .gnu.linkonce. sections are used to
19947 eliminate duplicate copies of functions and vtables and such.
19948 The linker will arbitrarily choose one and discard the others.
19949 The AT_*_pc values for such functions refer to local labels in
19950 these sections. If the section from that file was discarded, the
19951 labels are not in the output, so the relocs get a value of 0.
19952 If this is a discarded function, mark the pc bounds as invalid,
19953 so that GDB will ignore it. */
19954 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19955 {
19956 struct objfile *objfile = per_objfile->objfile;
19957 struct gdbarch *gdbarch = objfile->arch ();
19958
19959 complaint (_("DW_AT_low_pc %s is zero "
19960 "for DIE at %s [in module %s]"),
19961 paddress (gdbarch, lowpc),
19962 sect_offset_str (sect_off),
19963 objfile_name (objfile));
19964 }
19965 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19966 else if (lowpc >= highpc)
19967 {
19968 struct objfile *objfile = per_objfile->objfile;
19969 struct gdbarch *gdbarch = objfile->arch ();
19970
19971 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19972 "for DIE at %s [in module %s]"),
19973 paddress (gdbarch, lowpc),
19974 paddress (gdbarch, highpc),
19975 sect_offset_str (sect_off),
19976 objfile_name (objfile));
19977 }
19978 else
19979 has_pc_info = 1;
19980 }
19981
19982 return info_ptr;
19983 }
19984
19985 /* Find a cached partial DIE at OFFSET in CU. */
19986
19987 struct partial_die_info *
19988 dwarf2_cu::find_partial_die (sect_offset sect_off)
19989 {
19990 struct partial_die_info *lookup_die = NULL;
19991 struct partial_die_info part_die (sect_off);
19992
19993 lookup_die = ((struct partial_die_info *)
19994 htab_find_with_hash (partial_dies, &part_die,
19995 to_underlying (sect_off)));
19996
19997 return lookup_die;
19998 }
19999
20000 /* Find a partial DIE at OFFSET, which may or may not be in CU,
20001 except in the case of .debug_types DIEs which do not reference
20002 outside their CU (they do however referencing other types via
20003 DW_FORM_ref_sig8). */
20004
20005 static const struct cu_partial_die_info
20006 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
20007 {
20008 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20009 struct objfile *objfile = per_objfile->objfile;
20010 struct partial_die_info *pd = NULL;
20011
20012 if (offset_in_dwz == cu->per_cu->is_dwz
20013 && cu->header.offset_in_cu_p (sect_off))
20014 {
20015 pd = cu->find_partial_die (sect_off);
20016 if (pd != NULL)
20017 return { cu, pd };
20018 /* We missed recording what we needed.
20019 Load all dies and try again. */
20020 }
20021 else
20022 {
20023 /* TUs don't reference other CUs/TUs (except via type signatures). */
20024 if (cu->per_cu->is_debug_types)
20025 {
20026 error (_("Dwarf Error: Type Unit at offset %s contains"
20027 " external reference to offset %s [in module %s].\n"),
20028 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
20029 bfd_get_filename (objfile->obfd));
20030 }
20031 dwarf2_per_cu_data *per_cu
20032 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20033 per_objfile);
20034
20035 cu = per_objfile->get_cu (per_cu);
20036 if (cu == NULL || cu->partial_dies == NULL)
20037 load_partial_comp_unit (per_cu, per_objfile, nullptr);
20038
20039 cu = per_objfile->get_cu (per_cu);
20040
20041 cu->last_used = 0;
20042 pd = cu->find_partial_die (sect_off);
20043 }
20044
20045 /* If we didn't find it, and not all dies have been loaded,
20046 load them all and try again. */
20047
20048 if (pd == NULL && cu->per_cu->load_all_dies == 0)
20049 {
20050 cu->per_cu->load_all_dies = 1;
20051
20052 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20053 THIS_CU->cu may already be in use. So we can't just free it and
20054 replace its DIEs with the ones we read in. Instead, we leave those
20055 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20056 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20057 set. */
20058 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
20059
20060 pd = cu->find_partial_die (sect_off);
20061 }
20062
20063 if (pd == NULL)
20064 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
20065 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
20066 return { cu, pd };
20067 }
20068
20069 /* See if we can figure out if the class lives in a namespace. We do
20070 this by looking for a member function; its demangled name will
20071 contain namespace info, if there is any. */
20072
20073 static void
20074 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20075 struct dwarf2_cu *cu)
20076 {
20077 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20078 what template types look like, because the demangler
20079 frequently doesn't give the same name as the debug info. We
20080 could fix this by only using the demangled name to get the
20081 prefix (but see comment in read_structure_type). */
20082
20083 struct partial_die_info *real_pdi;
20084 struct partial_die_info *child_pdi;
20085
20086 /* If this DIE (this DIE's specification, if any) has a parent, then
20087 we should not do this. We'll prepend the parent's fully qualified
20088 name when we create the partial symbol. */
20089
20090 real_pdi = struct_pdi;
20091 while (real_pdi->has_specification)
20092 {
20093 auto res = find_partial_die (real_pdi->spec_offset,
20094 real_pdi->spec_is_dwz, cu);
20095 real_pdi = res.pdi;
20096 cu = res.cu;
20097 }
20098
20099 if (real_pdi->die_parent != NULL)
20100 return;
20101
20102 for (child_pdi = struct_pdi->die_child;
20103 child_pdi != NULL;
20104 child_pdi = child_pdi->die_sibling)
20105 {
20106 if (child_pdi->tag == DW_TAG_subprogram
20107 && child_pdi->linkage_name != NULL)
20108 {
20109 gdb::unique_xmalloc_ptr<char> actual_class_name
20110 (cu->language_defn->class_name_from_physname
20111 (child_pdi->linkage_name));
20112 if (actual_class_name != NULL)
20113 {
20114 struct objfile *objfile = cu->per_objfile->objfile;
20115 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20116 struct_pdi->canonical_name = 1;
20117 }
20118 break;
20119 }
20120 }
20121 }
20122
20123 /* Return true if a DIE with TAG may have the DW_AT_const_value
20124 attribute. */
20125
20126 static bool
20127 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20128 {
20129 switch (tag)
20130 {
20131 case DW_TAG_constant:
20132 case DW_TAG_enumerator:
20133 case DW_TAG_formal_parameter:
20134 case DW_TAG_template_value_param:
20135 case DW_TAG_variable:
20136 return true;
20137 }
20138
20139 return false;
20140 }
20141
20142 void
20143 partial_die_info::fixup (struct dwarf2_cu *cu)
20144 {
20145 /* Once we've fixed up a die, there's no point in doing so again.
20146 This also avoids a memory leak if we were to call
20147 guess_partial_die_structure_name multiple times. */
20148 if (fixup_called)
20149 return;
20150
20151 /* If we found a reference attribute and the DIE has no name, try
20152 to find a name in the referred to DIE. */
20153
20154 if (raw_name == NULL && has_specification)
20155 {
20156 struct partial_die_info *spec_die;
20157
20158 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20159 spec_die = res.pdi;
20160 cu = res.cu;
20161
20162 spec_die->fixup (cu);
20163
20164 if (spec_die->raw_name)
20165 {
20166 raw_name = spec_die->raw_name;
20167 canonical_name = spec_die->canonical_name;
20168
20169 /* Copy DW_AT_external attribute if it is set. */
20170 if (spec_die->is_external)
20171 is_external = spec_die->is_external;
20172 }
20173 }
20174
20175 if (!has_const_value && has_specification
20176 && can_have_DW_AT_const_value_p (tag))
20177 {
20178 struct partial_die_info *spec_die;
20179
20180 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20181 spec_die = res.pdi;
20182 cu = res.cu;
20183
20184 spec_die->fixup (cu);
20185
20186 if (spec_die->has_const_value)
20187 {
20188 /* Copy DW_AT_const_value attribute if it is set. */
20189 has_const_value = spec_die->has_const_value;
20190 }
20191 }
20192
20193 /* Set default names for some unnamed DIEs. */
20194
20195 if (raw_name == NULL && tag == DW_TAG_namespace)
20196 {
20197 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20198 canonical_name = 1;
20199 }
20200
20201 /* If there is no parent die to provide a namespace, and there are
20202 children, see if we can determine the namespace from their linkage
20203 name. */
20204 if (cu->language == language_cplus
20205 && !cu->per_objfile->per_bfd->types.empty ()
20206 && die_parent == NULL
20207 && has_children
20208 && (tag == DW_TAG_class_type
20209 || tag == DW_TAG_structure_type
20210 || tag == DW_TAG_union_type))
20211 guess_partial_die_structure_name (this, cu);
20212
20213 /* GCC might emit a nameless struct or union that has a linkage
20214 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20215 if (raw_name == NULL
20216 && (tag == DW_TAG_class_type
20217 || tag == DW_TAG_interface_type
20218 || tag == DW_TAG_structure_type
20219 || tag == DW_TAG_union_type)
20220 && linkage_name != NULL)
20221 {
20222 gdb::unique_xmalloc_ptr<char> demangled
20223 (gdb_demangle (linkage_name, DMGL_TYPES));
20224 if (demangled != nullptr)
20225 {
20226 const char *base;
20227
20228 /* Strip any leading namespaces/classes, keep only the base name.
20229 DW_AT_name for named DIEs does not contain the prefixes. */
20230 base = strrchr (demangled.get (), ':');
20231 if (base && base > demangled.get () && base[-1] == ':')
20232 base++;
20233 else
20234 base = demangled.get ();
20235
20236 struct objfile *objfile = cu->per_objfile->objfile;
20237 raw_name = objfile->intern (base);
20238 canonical_name = 1;
20239 }
20240 }
20241
20242 fixup_called = 1;
20243 }
20244
20245 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20246 contents from the given SECTION in the HEADER.
20247
20248 HEADER_OFFSET is the offset of the header in the section. */
20249 static void
20250 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20251 struct dwarf2_section_info *section,
20252 sect_offset header_offset)
20253 {
20254 unsigned int bytes_read;
20255 bfd *abfd = section->get_bfd_owner ();
20256 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20257
20258 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20259 info_ptr += bytes_read;
20260
20261 header->version = read_2_bytes (abfd, info_ptr);
20262 info_ptr += 2;
20263
20264 header->addr_size = read_1_byte (abfd, info_ptr);
20265 info_ptr += 1;
20266
20267 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20268 info_ptr += 1;
20269
20270 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20271 }
20272
20273 /* Return the DW_AT_loclists_base value for the CU. */
20274 static ULONGEST
20275 lookup_loclist_base (struct dwarf2_cu *cu)
20276 {
20277 /* For the .dwo unit, the loclist_base points to the first offset following
20278 the header. The header consists of the following entities-
20279 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20280 bit format)
20281 2. version (2 bytes)
20282 3. address size (1 byte)
20283 4. segment selector size (1 byte)
20284 5. offset entry count (4 bytes)
20285 These sizes are derived as per the DWARFv5 standard. */
20286 if (cu->dwo_unit != nullptr)
20287 {
20288 if (cu->header.initial_length_size == 4)
20289 return LOCLIST_HEADER_SIZE32;
20290 return LOCLIST_HEADER_SIZE64;
20291 }
20292 return cu->loclist_base;
20293 }
20294
20295 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20296 array of offsets in the .debug_loclists section. */
20297
20298 static sect_offset
20299 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20300 {
20301 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20302 struct objfile *objfile = per_objfile->objfile;
20303 bfd *abfd = objfile->obfd;
20304 ULONGEST loclist_header_size =
20305 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20306 : LOCLIST_HEADER_SIZE64);
20307 ULONGEST loclist_base = lookup_loclist_base (cu);
20308
20309 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20310 ULONGEST start_offset =
20311 loclist_base + loclist_index * cu->header.offset_size;
20312
20313 /* Get loclists section. */
20314 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20315
20316 /* Read the loclists section content. */
20317 section->read (objfile);
20318 if (section->buffer == NULL)
20319 error (_("DW_FORM_loclistx used without .debug_loclists "
20320 "section [in module %s]"), objfile_name (objfile));
20321
20322 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20323 so if loclist_base is smaller than the header size, we have a problem. */
20324 if (loclist_base < loclist_header_size)
20325 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20326 objfile_name (objfile));
20327
20328 /* Read the header of the loclists contribution. */
20329 struct loclists_rnglists_header header;
20330 read_loclists_rnglists_header (&header, section,
20331 (sect_offset) (loclist_base - loclist_header_size));
20332
20333 /* Verify the loclist index is valid. */
20334 if (loclist_index >= header.offset_entry_count)
20335 error (_("DW_FORM_loclistx pointing outside of "
20336 ".debug_loclists offset array [in module %s]"),
20337 objfile_name (objfile));
20338
20339 /* Validate that reading won't go beyond the end of the section. */
20340 if (start_offset + cu->header.offset_size > section->size)
20341 error (_("Reading DW_FORM_loclistx index beyond end of"
20342 ".debug_loclists section [in module %s]"),
20343 objfile_name (objfile));
20344
20345 const gdb_byte *info_ptr = section->buffer + start_offset;
20346
20347 if (cu->header.offset_size == 4)
20348 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20349 else
20350 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20351 }
20352
20353 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20354 array of offsets in the .debug_rnglists section. */
20355
20356 static sect_offset
20357 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20358 dwarf_tag tag)
20359 {
20360 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20361 struct objfile *objfile = dwarf2_per_objfile->objfile;
20362 bfd *abfd = objfile->obfd;
20363 ULONGEST rnglist_header_size =
20364 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20365 : RNGLIST_HEADER_SIZE64);
20366
20367 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20368 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20369 doesn't apply. */
20370 ULONGEST rnglist_base =
20371 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20372
20373 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20374 ULONGEST start_offset =
20375 rnglist_base + rnglist_index * cu->header.offset_size;
20376
20377 /* Get rnglists section. */
20378 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20379
20380 /* Read the rnglists section content. */
20381 section->read (objfile);
20382 if (section->buffer == nullptr)
20383 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20384 "[in module %s]"),
20385 objfile_name (objfile));
20386
20387 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20388 so if rnglist_base is smaller than the header size, we have a problem. */
20389 if (rnglist_base < rnglist_header_size)
20390 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20391 objfile_name (objfile));
20392
20393 /* Read the header of the rnglists contribution. */
20394 struct loclists_rnglists_header header;
20395 read_loclists_rnglists_header (&header, section,
20396 (sect_offset) (rnglist_base - rnglist_header_size));
20397
20398 /* Verify the rnglist index is valid. */
20399 if (rnglist_index >= header.offset_entry_count)
20400 error (_("DW_FORM_rnglistx index pointing outside of "
20401 ".debug_rnglists offset array [in module %s]"),
20402 objfile_name (objfile));
20403
20404 /* Validate that reading won't go beyond the end of the section. */
20405 if (start_offset + cu->header.offset_size > section->size)
20406 error (_("Reading DW_FORM_rnglistx index beyond end of"
20407 ".debug_rnglists section [in module %s]"),
20408 objfile_name (objfile));
20409
20410 const gdb_byte *info_ptr = section->buffer + start_offset;
20411
20412 if (cu->header.offset_size == 4)
20413 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20414 else
20415 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20416 }
20417
20418 /* Process the attributes that had to be skipped in the first round. These
20419 attributes are the ones that need str_offsets_base or addr_base attributes.
20420 They could not have been processed in the first round, because at the time
20421 the values of str_offsets_base or addr_base may not have been known. */
20422 static void
20423 read_attribute_reprocess (const struct die_reader_specs *reader,
20424 struct attribute *attr, dwarf_tag tag)
20425 {
20426 struct dwarf2_cu *cu = reader->cu;
20427 switch (attr->form)
20428 {
20429 case DW_FORM_addrx:
20430 case DW_FORM_GNU_addr_index:
20431 attr->set_address (read_addr_index (cu,
20432 attr->as_unsigned_reprocess ()));
20433 break;
20434 case DW_FORM_loclistx:
20435 {
20436 sect_offset loclists_sect_off
20437 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20438
20439 attr->set_unsigned (to_underlying (loclists_sect_off));
20440 }
20441 break;
20442 case DW_FORM_rnglistx:
20443 {
20444 sect_offset rnglists_sect_off
20445 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20446
20447 attr->set_unsigned (to_underlying (rnglists_sect_off));
20448 }
20449 break;
20450 case DW_FORM_strx:
20451 case DW_FORM_strx1:
20452 case DW_FORM_strx2:
20453 case DW_FORM_strx3:
20454 case DW_FORM_strx4:
20455 case DW_FORM_GNU_str_index:
20456 {
20457 unsigned int str_index = attr->as_unsigned_reprocess ();
20458 gdb_assert (!attr->canonical_string_p ());
20459 if (reader->dwo_file != NULL)
20460 attr->set_string_noncanonical (read_dwo_str_index (reader,
20461 str_index));
20462 else
20463 attr->set_string_noncanonical (read_stub_str_index (cu,
20464 str_index));
20465 break;
20466 }
20467 default:
20468 gdb_assert_not_reached (_("Unexpected DWARF form."));
20469 }
20470 }
20471
20472 /* Read an attribute value described by an attribute form. */
20473
20474 static const gdb_byte *
20475 read_attribute_value (const struct die_reader_specs *reader,
20476 struct attribute *attr, unsigned form,
20477 LONGEST implicit_const, const gdb_byte *info_ptr)
20478 {
20479 struct dwarf2_cu *cu = reader->cu;
20480 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20481 struct objfile *objfile = per_objfile->objfile;
20482 bfd *abfd = reader->abfd;
20483 struct comp_unit_head *cu_header = &cu->header;
20484 unsigned int bytes_read;
20485 struct dwarf_block *blk;
20486
20487 attr->form = (enum dwarf_form) form;
20488 switch (form)
20489 {
20490 case DW_FORM_ref_addr:
20491 if (cu_header->version == 2)
20492 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20493 &bytes_read));
20494 else
20495 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20496 &bytes_read));
20497 info_ptr += bytes_read;
20498 break;
20499 case DW_FORM_GNU_ref_alt:
20500 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20501 &bytes_read));
20502 info_ptr += bytes_read;
20503 break;
20504 case DW_FORM_addr:
20505 {
20506 struct gdbarch *gdbarch = objfile->arch ();
20507 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20508 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20509 attr->set_address (addr);
20510 info_ptr += bytes_read;
20511 }
20512 break;
20513 case DW_FORM_block2:
20514 blk = dwarf_alloc_block (cu);
20515 blk->size = read_2_bytes (abfd, info_ptr);
20516 info_ptr += 2;
20517 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20518 info_ptr += blk->size;
20519 attr->set_block (blk);
20520 break;
20521 case DW_FORM_block4:
20522 blk = dwarf_alloc_block (cu);
20523 blk->size = read_4_bytes (abfd, info_ptr);
20524 info_ptr += 4;
20525 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20526 info_ptr += blk->size;
20527 attr->set_block (blk);
20528 break;
20529 case DW_FORM_data2:
20530 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20531 info_ptr += 2;
20532 break;
20533 case DW_FORM_data4:
20534 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20535 info_ptr += 4;
20536 break;
20537 case DW_FORM_data8:
20538 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20539 info_ptr += 8;
20540 break;
20541 case DW_FORM_data16:
20542 blk = dwarf_alloc_block (cu);
20543 blk->size = 16;
20544 blk->data = read_n_bytes (abfd, info_ptr, 16);
20545 info_ptr += 16;
20546 attr->set_block (blk);
20547 break;
20548 case DW_FORM_sec_offset:
20549 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20550 &bytes_read));
20551 info_ptr += bytes_read;
20552 break;
20553 case DW_FORM_loclistx:
20554 {
20555 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20556 &bytes_read));
20557 info_ptr += bytes_read;
20558 }
20559 break;
20560 case DW_FORM_string:
20561 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20562 &bytes_read));
20563 info_ptr += bytes_read;
20564 break;
20565 case DW_FORM_strp:
20566 if (!cu->per_cu->is_dwz)
20567 {
20568 attr->set_string_noncanonical
20569 (read_indirect_string (per_objfile,
20570 abfd, info_ptr, cu_header,
20571 &bytes_read));
20572 info_ptr += bytes_read;
20573 break;
20574 }
20575 /* FALLTHROUGH */
20576 case DW_FORM_line_strp:
20577 if (!cu->per_cu->is_dwz)
20578 {
20579 attr->set_string_noncanonical
20580 (per_objfile->read_line_string (info_ptr, cu_header,
20581 &bytes_read));
20582 info_ptr += bytes_read;
20583 break;
20584 }
20585 /* FALLTHROUGH */
20586 case DW_FORM_GNU_strp_alt:
20587 {
20588 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20589 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20590 &bytes_read);
20591
20592 attr->set_string_noncanonical
20593 (dwz->read_string (objfile, str_offset));
20594 info_ptr += bytes_read;
20595 }
20596 break;
20597 case DW_FORM_exprloc:
20598 case DW_FORM_block:
20599 blk = dwarf_alloc_block (cu);
20600 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20601 info_ptr += bytes_read;
20602 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20603 info_ptr += blk->size;
20604 attr->set_block (blk);
20605 break;
20606 case DW_FORM_block1:
20607 blk = dwarf_alloc_block (cu);
20608 blk->size = read_1_byte (abfd, info_ptr);
20609 info_ptr += 1;
20610 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20611 info_ptr += blk->size;
20612 attr->set_block (blk);
20613 break;
20614 case DW_FORM_data1:
20615 case DW_FORM_flag:
20616 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20617 info_ptr += 1;
20618 break;
20619 case DW_FORM_flag_present:
20620 attr->set_unsigned (1);
20621 break;
20622 case DW_FORM_sdata:
20623 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20624 info_ptr += bytes_read;
20625 break;
20626 case DW_FORM_rnglistx:
20627 {
20628 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20629 &bytes_read));
20630 info_ptr += bytes_read;
20631 }
20632 break;
20633 case DW_FORM_udata:
20634 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20635 info_ptr += bytes_read;
20636 break;
20637 case DW_FORM_ref1:
20638 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20639 + read_1_byte (abfd, info_ptr)));
20640 info_ptr += 1;
20641 break;
20642 case DW_FORM_ref2:
20643 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20644 + read_2_bytes (abfd, info_ptr)));
20645 info_ptr += 2;
20646 break;
20647 case DW_FORM_ref4:
20648 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20649 + read_4_bytes (abfd, info_ptr)));
20650 info_ptr += 4;
20651 break;
20652 case DW_FORM_ref8:
20653 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20654 + read_8_bytes (abfd, info_ptr)));
20655 info_ptr += 8;
20656 break;
20657 case DW_FORM_ref_sig8:
20658 attr->set_signature (read_8_bytes (abfd, info_ptr));
20659 info_ptr += 8;
20660 break;
20661 case DW_FORM_ref_udata:
20662 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20663 + read_unsigned_leb128 (abfd, info_ptr,
20664 &bytes_read)));
20665 info_ptr += bytes_read;
20666 break;
20667 case DW_FORM_indirect:
20668 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20669 info_ptr += bytes_read;
20670 if (form == DW_FORM_implicit_const)
20671 {
20672 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20673 info_ptr += bytes_read;
20674 }
20675 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20676 info_ptr);
20677 break;
20678 case DW_FORM_implicit_const:
20679 attr->set_signed (implicit_const);
20680 break;
20681 case DW_FORM_addrx:
20682 case DW_FORM_GNU_addr_index:
20683 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20684 &bytes_read));
20685 info_ptr += bytes_read;
20686 break;
20687 case DW_FORM_strx:
20688 case DW_FORM_strx1:
20689 case DW_FORM_strx2:
20690 case DW_FORM_strx3:
20691 case DW_FORM_strx4:
20692 case DW_FORM_GNU_str_index:
20693 {
20694 ULONGEST str_index;
20695 if (form == DW_FORM_strx1)
20696 {
20697 str_index = read_1_byte (abfd, info_ptr);
20698 info_ptr += 1;
20699 }
20700 else if (form == DW_FORM_strx2)
20701 {
20702 str_index = read_2_bytes (abfd, info_ptr);
20703 info_ptr += 2;
20704 }
20705 else if (form == DW_FORM_strx3)
20706 {
20707 str_index = read_3_bytes (abfd, info_ptr);
20708 info_ptr += 3;
20709 }
20710 else if (form == DW_FORM_strx4)
20711 {
20712 str_index = read_4_bytes (abfd, info_ptr);
20713 info_ptr += 4;
20714 }
20715 else
20716 {
20717 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20718 info_ptr += bytes_read;
20719 }
20720 attr->set_unsigned_reprocess (str_index);
20721 }
20722 break;
20723 default:
20724 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20725 dwarf_form_name (form),
20726 bfd_get_filename (abfd));
20727 }
20728
20729 /* Super hack. */
20730 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20731 attr->form = DW_FORM_GNU_ref_alt;
20732
20733 /* We have seen instances where the compiler tried to emit a byte
20734 size attribute of -1 which ended up being encoded as an unsigned
20735 0xffffffff. Although 0xffffffff is technically a valid size value,
20736 an object of this size seems pretty unlikely so we can relatively
20737 safely treat these cases as if the size attribute was invalid and
20738 treat them as zero by default. */
20739 if (attr->name == DW_AT_byte_size
20740 && form == DW_FORM_data4
20741 && attr->as_unsigned () >= 0xffffffff)
20742 {
20743 complaint
20744 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20745 hex_string (attr->as_unsigned ()));
20746 attr->set_unsigned (0);
20747 }
20748
20749 return info_ptr;
20750 }
20751
20752 /* Read an attribute described by an abbreviated attribute. */
20753
20754 static const gdb_byte *
20755 read_attribute (const struct die_reader_specs *reader,
20756 struct attribute *attr, const struct attr_abbrev *abbrev,
20757 const gdb_byte *info_ptr)
20758 {
20759 attr->name = abbrev->name;
20760 attr->string_is_canonical = 0;
20761 attr->requires_reprocessing = 0;
20762 return read_attribute_value (reader, attr, abbrev->form,
20763 abbrev->implicit_const, info_ptr);
20764 }
20765
20766 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20767
20768 static const char *
20769 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20770 LONGEST str_offset)
20771 {
20772 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20773 str_offset, "DW_FORM_strp");
20774 }
20775
20776 /* Return pointer to string at .debug_str offset as read from BUF.
20777 BUF is assumed to be in a compilation unit described by CU_HEADER.
20778 Return *BYTES_READ_PTR count of bytes read from BUF. */
20779
20780 static const char *
20781 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20782 const gdb_byte *buf,
20783 const struct comp_unit_head *cu_header,
20784 unsigned int *bytes_read_ptr)
20785 {
20786 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20787
20788 return read_indirect_string_at_offset (per_objfile, str_offset);
20789 }
20790
20791 /* See read.h. */
20792
20793 const char *
20794 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20795 const struct comp_unit_head *cu_header,
20796 unsigned int *bytes_read_ptr)
20797 {
20798 bfd *abfd = objfile->obfd;
20799 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20800
20801 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20802 }
20803
20804 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20805 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20806 ADDR_SIZE is the size of addresses from the CU header. */
20807
20808 static CORE_ADDR
20809 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20810 gdb::optional<ULONGEST> addr_base, int addr_size)
20811 {
20812 struct objfile *objfile = per_objfile->objfile;
20813 bfd *abfd = objfile->obfd;
20814 const gdb_byte *info_ptr;
20815 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20816
20817 per_objfile->per_bfd->addr.read (objfile);
20818 if (per_objfile->per_bfd->addr.buffer == NULL)
20819 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20820 objfile_name (objfile));
20821 if (addr_base_or_zero + addr_index * addr_size
20822 >= per_objfile->per_bfd->addr.size)
20823 error (_("DW_FORM_addr_index pointing outside of "
20824 ".debug_addr section [in module %s]"),
20825 objfile_name (objfile));
20826 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20827 + addr_index * addr_size);
20828 if (addr_size == 4)
20829 return bfd_get_32 (abfd, info_ptr);
20830 else
20831 return bfd_get_64 (abfd, info_ptr);
20832 }
20833
20834 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20835
20836 static CORE_ADDR
20837 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20838 {
20839 return read_addr_index_1 (cu->per_objfile, addr_index,
20840 cu->addr_base, cu->header.addr_size);
20841 }
20842
20843 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20844
20845 static CORE_ADDR
20846 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20847 unsigned int *bytes_read)
20848 {
20849 bfd *abfd = cu->per_objfile->objfile->obfd;
20850 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20851
20852 return read_addr_index (cu, addr_index);
20853 }
20854
20855 /* See read.h. */
20856
20857 CORE_ADDR
20858 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20859 dwarf2_per_objfile *per_objfile,
20860 unsigned int addr_index)
20861 {
20862 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20863 gdb::optional<ULONGEST> addr_base;
20864 int addr_size;
20865
20866 /* We need addr_base and addr_size.
20867 If we don't have PER_CU->cu, we have to get it.
20868 Nasty, but the alternative is storing the needed info in PER_CU,
20869 which at this point doesn't seem justified: it's not clear how frequently
20870 it would get used and it would increase the size of every PER_CU.
20871 Entry points like dwarf2_per_cu_addr_size do a similar thing
20872 so we're not in uncharted territory here.
20873 Alas we need to be a bit more complicated as addr_base is contained
20874 in the DIE.
20875
20876 We don't need to read the entire CU(/TU).
20877 We just need the header and top level die.
20878
20879 IWBN to use the aging mechanism to let us lazily later discard the CU.
20880 For now we skip this optimization. */
20881
20882 if (cu != NULL)
20883 {
20884 addr_base = cu->addr_base;
20885 addr_size = cu->header.addr_size;
20886 }
20887 else
20888 {
20889 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20890 addr_base = reader.cu->addr_base;
20891 addr_size = reader.cu->header.addr_size;
20892 }
20893
20894 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20895 }
20896
20897 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20898 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20899 DWO file. */
20900
20901 static const char *
20902 read_str_index (struct dwarf2_cu *cu,
20903 struct dwarf2_section_info *str_section,
20904 struct dwarf2_section_info *str_offsets_section,
20905 ULONGEST str_offsets_base, ULONGEST str_index)
20906 {
20907 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20908 struct objfile *objfile = per_objfile->objfile;
20909 const char *objf_name = objfile_name (objfile);
20910 bfd *abfd = objfile->obfd;
20911 const gdb_byte *info_ptr;
20912 ULONGEST str_offset;
20913 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20914
20915 str_section->read (objfile);
20916 str_offsets_section->read (objfile);
20917 if (str_section->buffer == NULL)
20918 error (_("%s used without %s section"
20919 " in CU at offset %s [in module %s]"),
20920 form_name, str_section->get_name (),
20921 sect_offset_str (cu->header.sect_off), objf_name);
20922 if (str_offsets_section->buffer == NULL)
20923 error (_("%s used without %s section"
20924 " in CU at offset %s [in module %s]"),
20925 form_name, str_section->get_name (),
20926 sect_offset_str (cu->header.sect_off), objf_name);
20927 info_ptr = (str_offsets_section->buffer
20928 + str_offsets_base
20929 + str_index * cu->header.offset_size);
20930 if (cu->header.offset_size == 4)
20931 str_offset = bfd_get_32 (abfd, info_ptr);
20932 else
20933 str_offset = bfd_get_64 (abfd, info_ptr);
20934 if (str_offset >= str_section->size)
20935 error (_("Offset from %s pointing outside of"
20936 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20937 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20938 return (const char *) (str_section->buffer + str_offset);
20939 }
20940
20941 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20942
20943 static const char *
20944 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20945 {
20946 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20947 ? reader->cu->header.addr_size : 0;
20948 return read_str_index (reader->cu,
20949 &reader->dwo_file->sections.str,
20950 &reader->dwo_file->sections.str_offsets,
20951 str_offsets_base, str_index);
20952 }
20953
20954 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20955
20956 static const char *
20957 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20958 {
20959 struct objfile *objfile = cu->per_objfile->objfile;
20960 const char *objf_name = objfile_name (objfile);
20961 static const char form_name[] = "DW_FORM_GNU_str_index";
20962 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20963
20964 if (!cu->str_offsets_base.has_value ())
20965 error (_("%s used in Fission stub without %s"
20966 " in CU at offset 0x%lx [in module %s]"),
20967 form_name, str_offsets_attr_name,
20968 (long) cu->header.offset_size, objf_name);
20969
20970 return read_str_index (cu,
20971 &cu->per_objfile->per_bfd->str,
20972 &cu->per_objfile->per_bfd->str_offsets,
20973 *cu->str_offsets_base, str_index);
20974 }
20975
20976 /* Return the length of an LEB128 number in BUF. */
20977
20978 static int
20979 leb128_size (const gdb_byte *buf)
20980 {
20981 const gdb_byte *begin = buf;
20982 gdb_byte byte;
20983
20984 while (1)
20985 {
20986 byte = *buf++;
20987 if ((byte & 128) == 0)
20988 return buf - begin;
20989 }
20990 }
20991
20992 static void
20993 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20994 {
20995 switch (lang)
20996 {
20997 case DW_LANG_C89:
20998 case DW_LANG_C99:
20999 case DW_LANG_C11:
21000 case DW_LANG_C:
21001 case DW_LANG_UPC:
21002 cu->language = language_c;
21003 break;
21004 case DW_LANG_Java:
21005 case DW_LANG_C_plus_plus:
21006 case DW_LANG_C_plus_plus_11:
21007 case DW_LANG_C_plus_plus_14:
21008 cu->language = language_cplus;
21009 break;
21010 case DW_LANG_D:
21011 cu->language = language_d;
21012 break;
21013 case DW_LANG_Fortran77:
21014 case DW_LANG_Fortran90:
21015 case DW_LANG_Fortran95:
21016 case DW_LANG_Fortran03:
21017 case DW_LANG_Fortran08:
21018 cu->language = language_fortran;
21019 break;
21020 case DW_LANG_Go:
21021 cu->language = language_go;
21022 break;
21023 case DW_LANG_Mips_Assembler:
21024 cu->language = language_asm;
21025 break;
21026 case DW_LANG_Ada83:
21027 case DW_LANG_Ada95:
21028 cu->language = language_ada;
21029 break;
21030 case DW_LANG_Modula2:
21031 cu->language = language_m2;
21032 break;
21033 case DW_LANG_Pascal83:
21034 cu->language = language_pascal;
21035 break;
21036 case DW_LANG_ObjC:
21037 cu->language = language_objc;
21038 break;
21039 case DW_LANG_Rust:
21040 case DW_LANG_Rust_old:
21041 cu->language = language_rust;
21042 break;
21043 case DW_LANG_Cobol74:
21044 case DW_LANG_Cobol85:
21045 default:
21046 cu->language = language_minimal;
21047 break;
21048 }
21049 cu->language_defn = language_def (cu->language);
21050 }
21051
21052 /* Return the named attribute or NULL if not there. */
21053
21054 static struct attribute *
21055 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21056 {
21057 for (;;)
21058 {
21059 unsigned int i;
21060 struct attribute *spec = NULL;
21061
21062 for (i = 0; i < die->num_attrs; ++i)
21063 {
21064 if (die->attrs[i].name == name)
21065 return &die->attrs[i];
21066 if (die->attrs[i].name == DW_AT_specification
21067 || die->attrs[i].name == DW_AT_abstract_origin)
21068 spec = &die->attrs[i];
21069 }
21070
21071 if (!spec)
21072 break;
21073
21074 die = follow_die_ref (die, spec, &cu);
21075 }
21076
21077 return NULL;
21078 }
21079
21080 /* Return the string associated with a string-typed attribute, or NULL if it
21081 is either not found or is of an incorrect type. */
21082
21083 static const char *
21084 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21085 {
21086 struct attribute *attr;
21087 const char *str = NULL;
21088
21089 attr = dwarf2_attr (die, name, cu);
21090
21091 if (attr != NULL)
21092 {
21093 str = attr->as_string ();
21094 if (str == nullptr)
21095 complaint (_("string type expected for attribute %s for "
21096 "DIE at %s in module %s"),
21097 dwarf_attr_name (name), sect_offset_str (die->sect_off),
21098 objfile_name (cu->per_objfile->objfile));
21099 }
21100
21101 return str;
21102 }
21103
21104 /* Return the dwo name or NULL if not present. If present, it is in either
21105 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
21106 static const char *
21107 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21108 {
21109 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21110 if (dwo_name == nullptr)
21111 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21112 return dwo_name;
21113 }
21114
21115 /* Return non-zero iff the attribute NAME is defined for the given DIE,
21116 and holds a non-zero value. This function should only be used for
21117 DW_FORM_flag or DW_FORM_flag_present attributes. */
21118
21119 static int
21120 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21121 {
21122 struct attribute *attr = dwarf2_attr (die, name, cu);
21123
21124 return attr != nullptr && attr->as_boolean ();
21125 }
21126
21127 static int
21128 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
21129 {
21130 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21131 which value is non-zero. However, we have to be careful with
21132 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21133 (via dwarf2_flag_true_p) follows this attribute. So we may
21134 end up accidently finding a declaration attribute that belongs
21135 to a different DIE referenced by the specification attribute,
21136 even though the given DIE does not have a declaration attribute. */
21137 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21138 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
21139 }
21140
21141 /* Return the die giving the specification for DIE, if there is
21142 one. *SPEC_CU is the CU containing DIE on input, and the CU
21143 containing the return value on output. If there is no
21144 specification, but there is an abstract origin, that is
21145 returned. */
21146
21147 static struct die_info *
21148 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
21149 {
21150 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21151 *spec_cu);
21152
21153 if (spec_attr == NULL)
21154 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21155
21156 if (spec_attr == NULL)
21157 return NULL;
21158 else
21159 return follow_die_ref (die, spec_attr, spec_cu);
21160 }
21161
21162 /* Stub for free_line_header to match void * callback types. */
21163
21164 static void
21165 free_line_header_voidp (void *arg)
21166 {
21167 struct line_header *lh = (struct line_header *) arg;
21168
21169 delete lh;
21170 }
21171
21172 /* A convenience function to find the proper .debug_line section for a CU. */
21173
21174 static struct dwarf2_section_info *
21175 get_debug_line_section (struct dwarf2_cu *cu)
21176 {
21177 struct dwarf2_section_info *section;
21178 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21179
21180 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21181 DWO file. */
21182 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21183 section = &cu->dwo_unit->dwo_file->sections.line;
21184 else if (cu->per_cu->is_dwz)
21185 {
21186 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
21187
21188 section = &dwz->line;
21189 }
21190 else
21191 section = &per_objfile->per_bfd->line;
21192
21193 return section;
21194 }
21195
21196 /* Read the statement program header starting at OFFSET in
21197 .debug_line, or .debug_line.dwo. Return a pointer
21198 to a struct line_header, allocated using xmalloc.
21199 Returns NULL if there is a problem reading the header, e.g., if it
21200 has a version we don't understand.
21201
21202 NOTE: the strings in the include directory and file name tables of
21203 the returned object point into the dwarf line section buffer,
21204 and must not be freed. */
21205
21206 static line_header_up
21207 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21208 {
21209 struct dwarf2_section_info *section;
21210 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21211
21212 section = get_debug_line_section (cu);
21213 section->read (per_objfile->objfile);
21214 if (section->buffer == NULL)
21215 {
21216 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21217 complaint (_("missing .debug_line.dwo section"));
21218 else
21219 complaint (_("missing .debug_line section"));
21220 return 0;
21221 }
21222
21223 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21224 per_objfile, section, &cu->header);
21225 }
21226
21227 /* Subroutine of dwarf_decode_lines to simplify it.
21228 Return the file name of the psymtab for the given file_entry.
21229 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21230 If space for the result is malloc'd, *NAME_HOLDER will be set.
21231 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21232
21233 static const char *
21234 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21235 const dwarf2_psymtab *pst,
21236 const char *comp_dir,
21237 gdb::unique_xmalloc_ptr<char> *name_holder)
21238 {
21239 const char *include_name = fe.name;
21240 const char *include_name_to_compare = include_name;
21241 const char *pst_filename;
21242 int file_is_pst;
21243
21244 const char *dir_name = fe.include_dir (lh);
21245
21246 gdb::unique_xmalloc_ptr<char> hold_compare;
21247 if (!IS_ABSOLUTE_PATH (include_name)
21248 && (dir_name != NULL || comp_dir != NULL))
21249 {
21250 /* Avoid creating a duplicate psymtab for PST.
21251 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21252 Before we do the comparison, however, we need to account
21253 for DIR_NAME and COMP_DIR.
21254 First prepend dir_name (if non-NULL). If we still don't
21255 have an absolute path prepend comp_dir (if non-NULL).
21256 However, the directory we record in the include-file's
21257 psymtab does not contain COMP_DIR (to match the
21258 corresponding symtab(s)).
21259
21260 Example:
21261
21262 bash$ cd /tmp
21263 bash$ gcc -g ./hello.c
21264 include_name = "hello.c"
21265 dir_name = "."
21266 DW_AT_comp_dir = comp_dir = "/tmp"
21267 DW_AT_name = "./hello.c"
21268
21269 */
21270
21271 if (dir_name != NULL)
21272 {
21273 name_holder->reset (concat (dir_name, SLASH_STRING,
21274 include_name, (char *) NULL));
21275 include_name = name_holder->get ();
21276 include_name_to_compare = include_name;
21277 }
21278 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21279 {
21280 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21281 include_name, (char *) NULL));
21282 include_name_to_compare = hold_compare.get ();
21283 }
21284 }
21285
21286 pst_filename = pst->filename;
21287 gdb::unique_xmalloc_ptr<char> copied_name;
21288 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21289 {
21290 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21291 pst_filename, (char *) NULL));
21292 pst_filename = copied_name.get ();
21293 }
21294
21295 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21296
21297 if (file_is_pst)
21298 return NULL;
21299 return include_name;
21300 }
21301
21302 /* State machine to track the state of the line number program. */
21303
21304 class lnp_state_machine
21305 {
21306 public:
21307 /* Initialize a machine state for the start of a line number
21308 program. */
21309 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21310 bool record_lines_p);
21311
21312 file_entry *current_file ()
21313 {
21314 /* lh->file_names is 0-based, but the file name numbers in the
21315 statement program are 1-based. */
21316 return m_line_header->file_name_at (m_file);
21317 }
21318
21319 /* Record the line in the state machine. END_SEQUENCE is true if
21320 we're processing the end of a sequence. */
21321 void record_line (bool end_sequence);
21322
21323 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21324 nop-out rest of the lines in this sequence. */
21325 void check_line_address (struct dwarf2_cu *cu,
21326 const gdb_byte *line_ptr,
21327 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21328
21329 void handle_set_discriminator (unsigned int discriminator)
21330 {
21331 m_discriminator = discriminator;
21332 m_line_has_non_zero_discriminator |= discriminator != 0;
21333 }
21334
21335 /* Handle DW_LNE_set_address. */
21336 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21337 {
21338 m_op_index = 0;
21339 address += baseaddr;
21340 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21341 }
21342
21343 /* Handle DW_LNS_advance_pc. */
21344 void handle_advance_pc (CORE_ADDR adjust);
21345
21346 /* Handle a special opcode. */
21347 void handle_special_opcode (unsigned char op_code);
21348
21349 /* Handle DW_LNS_advance_line. */
21350 void handle_advance_line (int line_delta)
21351 {
21352 advance_line (line_delta);
21353 }
21354
21355 /* Handle DW_LNS_set_file. */
21356 void handle_set_file (file_name_index file);
21357
21358 /* Handle DW_LNS_negate_stmt. */
21359 void handle_negate_stmt ()
21360 {
21361 m_is_stmt = !m_is_stmt;
21362 }
21363
21364 /* Handle DW_LNS_const_add_pc. */
21365 void handle_const_add_pc ();
21366
21367 /* Handle DW_LNS_fixed_advance_pc. */
21368 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21369 {
21370 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21371 m_op_index = 0;
21372 }
21373
21374 /* Handle DW_LNS_copy. */
21375 void handle_copy ()
21376 {
21377 record_line (false);
21378 m_discriminator = 0;
21379 }
21380
21381 /* Handle DW_LNE_end_sequence. */
21382 void handle_end_sequence ()
21383 {
21384 m_currently_recording_lines = true;
21385 }
21386
21387 private:
21388 /* Advance the line by LINE_DELTA. */
21389 void advance_line (int line_delta)
21390 {
21391 m_line += line_delta;
21392
21393 if (line_delta != 0)
21394 m_line_has_non_zero_discriminator = m_discriminator != 0;
21395 }
21396
21397 struct dwarf2_cu *m_cu;
21398
21399 gdbarch *m_gdbarch;
21400
21401 /* True if we're recording lines.
21402 Otherwise we're building partial symtabs and are just interested in
21403 finding include files mentioned by the line number program. */
21404 bool m_record_lines_p;
21405
21406 /* The line number header. */
21407 line_header *m_line_header;
21408
21409 /* These are part of the standard DWARF line number state machine,
21410 and initialized according to the DWARF spec. */
21411
21412 unsigned char m_op_index = 0;
21413 /* The line table index of the current file. */
21414 file_name_index m_file = 1;
21415 unsigned int m_line = 1;
21416
21417 /* These are initialized in the constructor. */
21418
21419 CORE_ADDR m_address;
21420 bool m_is_stmt;
21421 unsigned int m_discriminator;
21422
21423 /* Additional bits of state we need to track. */
21424
21425 /* The last file that we called dwarf2_start_subfile for.
21426 This is only used for TLLs. */
21427 unsigned int m_last_file = 0;
21428 /* The last file a line number was recorded for. */
21429 struct subfile *m_last_subfile = NULL;
21430
21431 /* The address of the last line entry. */
21432 CORE_ADDR m_last_address;
21433
21434 /* Set to true when a previous line at the same address (using
21435 m_last_address) had m_is_stmt true. This is reset to false when a
21436 line entry at a new address (m_address different to m_last_address) is
21437 processed. */
21438 bool m_stmt_at_address = false;
21439
21440 /* When true, record the lines we decode. */
21441 bool m_currently_recording_lines = false;
21442
21443 /* The last line number that was recorded, used to coalesce
21444 consecutive entries for the same line. This can happen, for
21445 example, when discriminators are present. PR 17276. */
21446 unsigned int m_last_line = 0;
21447 bool m_line_has_non_zero_discriminator = false;
21448 };
21449
21450 void
21451 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21452 {
21453 CORE_ADDR addr_adj = (((m_op_index + adjust)
21454 / m_line_header->maximum_ops_per_instruction)
21455 * m_line_header->minimum_instruction_length);
21456 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21457 m_op_index = ((m_op_index + adjust)
21458 % m_line_header->maximum_ops_per_instruction);
21459 }
21460
21461 void
21462 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21463 {
21464 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21465 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21466 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21467 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21468 / m_line_header->maximum_ops_per_instruction)
21469 * m_line_header->minimum_instruction_length);
21470 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21471 m_op_index = ((m_op_index + adj_opcode_d)
21472 % m_line_header->maximum_ops_per_instruction);
21473
21474 int line_delta = m_line_header->line_base + adj_opcode_r;
21475 advance_line (line_delta);
21476 record_line (false);
21477 m_discriminator = 0;
21478 }
21479
21480 void
21481 lnp_state_machine::handle_set_file (file_name_index file)
21482 {
21483 m_file = file;
21484
21485 const file_entry *fe = current_file ();
21486 if (fe == NULL)
21487 dwarf2_debug_line_missing_file_complaint ();
21488 else if (m_record_lines_p)
21489 {
21490 const char *dir = fe->include_dir (m_line_header);
21491
21492 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21493 m_line_has_non_zero_discriminator = m_discriminator != 0;
21494 dwarf2_start_subfile (m_cu, fe->name, dir);
21495 }
21496 }
21497
21498 void
21499 lnp_state_machine::handle_const_add_pc ()
21500 {
21501 CORE_ADDR adjust
21502 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21503
21504 CORE_ADDR addr_adj
21505 = (((m_op_index + adjust)
21506 / m_line_header->maximum_ops_per_instruction)
21507 * m_line_header->minimum_instruction_length);
21508
21509 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21510 m_op_index = ((m_op_index + adjust)
21511 % m_line_header->maximum_ops_per_instruction);
21512 }
21513
21514 /* Return non-zero if we should add LINE to the line number table.
21515 LINE is the line to add, LAST_LINE is the last line that was added,
21516 LAST_SUBFILE is the subfile for LAST_LINE.
21517 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21518 had a non-zero discriminator.
21519
21520 We have to be careful in the presence of discriminators.
21521 E.g., for this line:
21522
21523 for (i = 0; i < 100000; i++);
21524
21525 clang can emit four line number entries for that one line,
21526 each with a different discriminator.
21527 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21528
21529 However, we want gdb to coalesce all four entries into one.
21530 Otherwise the user could stepi into the middle of the line and
21531 gdb would get confused about whether the pc really was in the
21532 middle of the line.
21533
21534 Things are further complicated by the fact that two consecutive
21535 line number entries for the same line is a heuristic used by gcc
21536 to denote the end of the prologue. So we can't just discard duplicate
21537 entries, we have to be selective about it. The heuristic we use is
21538 that we only collapse consecutive entries for the same line if at least
21539 one of those entries has a non-zero discriminator. PR 17276.
21540
21541 Note: Addresses in the line number state machine can never go backwards
21542 within one sequence, thus this coalescing is ok. */
21543
21544 static int
21545 dwarf_record_line_p (struct dwarf2_cu *cu,
21546 unsigned int line, unsigned int last_line,
21547 int line_has_non_zero_discriminator,
21548 struct subfile *last_subfile)
21549 {
21550 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21551 return 1;
21552 if (line != last_line)
21553 return 1;
21554 /* Same line for the same file that we've seen already.
21555 As a last check, for pr 17276, only record the line if the line
21556 has never had a non-zero discriminator. */
21557 if (!line_has_non_zero_discriminator)
21558 return 1;
21559 return 0;
21560 }
21561
21562 /* Use the CU's builder to record line number LINE beginning at
21563 address ADDRESS in the line table of subfile SUBFILE. */
21564
21565 static void
21566 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21567 unsigned int line, CORE_ADDR address, bool is_stmt,
21568 struct dwarf2_cu *cu)
21569 {
21570 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21571
21572 if (dwarf_line_debug)
21573 {
21574 fprintf_unfiltered (gdb_stdlog,
21575 "Recording line %u, file %s, address %s\n",
21576 line, lbasename (subfile->name),
21577 paddress (gdbarch, address));
21578 }
21579
21580 if (cu != nullptr)
21581 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21582 }
21583
21584 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21585 Mark the end of a set of line number records.
21586 The arguments are the same as for dwarf_record_line_1.
21587 If SUBFILE is NULL the request is ignored. */
21588
21589 static void
21590 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21591 CORE_ADDR address, struct dwarf2_cu *cu)
21592 {
21593 if (subfile == NULL)
21594 return;
21595
21596 if (dwarf_line_debug)
21597 {
21598 fprintf_unfiltered (gdb_stdlog,
21599 "Finishing current line, file %s, address %s\n",
21600 lbasename (subfile->name),
21601 paddress (gdbarch, address));
21602 }
21603
21604 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21605 }
21606
21607 void
21608 lnp_state_machine::record_line (bool end_sequence)
21609 {
21610 if (dwarf_line_debug)
21611 {
21612 fprintf_unfiltered (gdb_stdlog,
21613 "Processing actual line %u: file %u,"
21614 " address %s, is_stmt %u, discrim %u%s\n",
21615 m_line, m_file,
21616 paddress (m_gdbarch, m_address),
21617 m_is_stmt, m_discriminator,
21618 (end_sequence ? "\t(end sequence)" : ""));
21619 }
21620
21621 file_entry *fe = current_file ();
21622
21623 if (fe == NULL)
21624 dwarf2_debug_line_missing_file_complaint ();
21625 /* For now we ignore lines not starting on an instruction boundary.
21626 But not when processing end_sequence for compatibility with the
21627 previous version of the code. */
21628 else if (m_op_index == 0 || end_sequence)
21629 {
21630 fe->included_p = 1;
21631 if (m_record_lines_p)
21632 {
21633 /* When we switch files we insert an end maker in the first file,
21634 switch to the second file and add a new line entry. The
21635 problem is that the end marker inserted in the first file will
21636 discard any previous line entries at the same address. If the
21637 line entries in the first file are marked as is-stmt, while
21638 the new line in the second file is non-stmt, then this means
21639 the end marker will discard is-stmt lines so we can have a
21640 non-stmt line. This means that there are less addresses at
21641 which the user can insert a breakpoint.
21642
21643 To improve this we track the last address in m_last_address,
21644 and whether we have seen an is-stmt at this address. Then
21645 when switching files, if we have seen a stmt at the current
21646 address, and we are switching to create a non-stmt line, then
21647 discard the new line. */
21648 bool file_changed
21649 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21650 bool ignore_this_line
21651 = ((file_changed && !end_sequence && m_last_address == m_address
21652 && !m_is_stmt && m_stmt_at_address)
21653 || (!end_sequence && m_line == 0));
21654
21655 if ((file_changed && !ignore_this_line) || end_sequence)
21656 {
21657 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21658 m_currently_recording_lines ? m_cu : nullptr);
21659 }
21660
21661 if (!end_sequence && !ignore_this_line)
21662 {
21663 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21664
21665 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21666 m_line_has_non_zero_discriminator,
21667 m_last_subfile))
21668 {
21669 buildsym_compunit *builder = m_cu->get_builder ();
21670 dwarf_record_line_1 (m_gdbarch,
21671 builder->get_current_subfile (),
21672 m_line, m_address, is_stmt,
21673 m_currently_recording_lines ? m_cu : nullptr);
21674 }
21675 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21676 m_last_line = m_line;
21677 }
21678 }
21679 }
21680
21681 /* Track whether we have seen any m_is_stmt true at m_address in case we
21682 have multiple line table entries all at m_address. */
21683 if (m_last_address != m_address)
21684 {
21685 m_stmt_at_address = false;
21686 m_last_address = m_address;
21687 }
21688 m_stmt_at_address |= m_is_stmt;
21689 }
21690
21691 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21692 line_header *lh, bool record_lines_p)
21693 {
21694 m_cu = cu;
21695 m_gdbarch = arch;
21696 m_record_lines_p = record_lines_p;
21697 m_line_header = lh;
21698
21699 m_currently_recording_lines = true;
21700
21701 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21702 was a line entry for it so that the backend has a chance to adjust it
21703 and also record it in case it needs it. This is currently used by MIPS
21704 code, cf. `mips_adjust_dwarf2_line'. */
21705 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21706 m_is_stmt = lh->default_is_stmt;
21707 m_discriminator = 0;
21708
21709 m_last_address = m_address;
21710 m_stmt_at_address = false;
21711 }
21712
21713 void
21714 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21715 const gdb_byte *line_ptr,
21716 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21717 {
21718 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21719 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21720 located at 0x0. In this case, additionally check that if
21721 ADDRESS < UNRELOCATED_LOWPC. */
21722
21723 if ((address == 0 && address < unrelocated_lowpc)
21724 || address == (CORE_ADDR) -1)
21725 {
21726 /* This line table is for a function which has been
21727 GCd by the linker. Ignore it. PR gdb/12528 */
21728
21729 struct objfile *objfile = cu->per_objfile->objfile;
21730 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21731
21732 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21733 line_offset, objfile_name (objfile));
21734 m_currently_recording_lines = false;
21735 /* Note: m_currently_recording_lines is left as false until we see
21736 DW_LNE_end_sequence. */
21737 }
21738 }
21739
21740 /* Subroutine of dwarf_decode_lines to simplify it.
21741 Process the line number information in LH.
21742 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21743 program in order to set included_p for every referenced header. */
21744
21745 static void
21746 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21747 const int decode_for_pst_p, CORE_ADDR lowpc)
21748 {
21749 const gdb_byte *line_ptr, *extended_end;
21750 const gdb_byte *line_end;
21751 unsigned int bytes_read, extended_len;
21752 unsigned char op_code, extended_op;
21753 CORE_ADDR baseaddr;
21754 struct objfile *objfile = cu->per_objfile->objfile;
21755 bfd *abfd = objfile->obfd;
21756 struct gdbarch *gdbarch = objfile->arch ();
21757 /* True if we're recording line info (as opposed to building partial
21758 symtabs and just interested in finding include files mentioned by
21759 the line number program). */
21760 bool record_lines_p = !decode_for_pst_p;
21761
21762 baseaddr = objfile->text_section_offset ();
21763
21764 line_ptr = lh->statement_program_start;
21765 line_end = lh->statement_program_end;
21766
21767 /* Read the statement sequences until there's nothing left. */
21768 while (line_ptr < line_end)
21769 {
21770 /* The DWARF line number program state machine. Reset the state
21771 machine at the start of each sequence. */
21772 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21773 bool end_sequence = false;
21774
21775 if (record_lines_p)
21776 {
21777 /* Start a subfile for the current file of the state
21778 machine. */
21779 const file_entry *fe = state_machine.current_file ();
21780
21781 if (fe != NULL)
21782 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21783 }
21784
21785 /* Decode the table. */
21786 while (line_ptr < line_end && !end_sequence)
21787 {
21788 op_code = read_1_byte (abfd, line_ptr);
21789 line_ptr += 1;
21790
21791 if (op_code >= lh->opcode_base)
21792 {
21793 /* Special opcode. */
21794 state_machine.handle_special_opcode (op_code);
21795 }
21796 else switch (op_code)
21797 {
21798 case DW_LNS_extended_op:
21799 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21800 &bytes_read);
21801 line_ptr += bytes_read;
21802 extended_end = line_ptr + extended_len;
21803 extended_op = read_1_byte (abfd, line_ptr);
21804 line_ptr += 1;
21805 if (DW_LNE_lo_user <= extended_op
21806 && extended_op <= DW_LNE_hi_user)
21807 {
21808 /* Vendor extension, ignore. */
21809 line_ptr = extended_end;
21810 break;
21811 }
21812 switch (extended_op)
21813 {
21814 case DW_LNE_end_sequence:
21815 state_machine.handle_end_sequence ();
21816 end_sequence = true;
21817 break;
21818 case DW_LNE_set_address:
21819 {
21820 CORE_ADDR address
21821 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21822 line_ptr += bytes_read;
21823
21824 state_machine.check_line_address (cu, line_ptr,
21825 lowpc - baseaddr, address);
21826 state_machine.handle_set_address (baseaddr, address);
21827 }
21828 break;
21829 case DW_LNE_define_file:
21830 {
21831 const char *cur_file;
21832 unsigned int mod_time, length;
21833 dir_index dindex;
21834
21835 cur_file = read_direct_string (abfd, line_ptr,
21836 &bytes_read);
21837 line_ptr += bytes_read;
21838 dindex = (dir_index)
21839 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21840 line_ptr += bytes_read;
21841 mod_time =
21842 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21843 line_ptr += bytes_read;
21844 length =
21845 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21846 line_ptr += bytes_read;
21847 lh->add_file_name (cur_file, dindex, mod_time, length);
21848 }
21849 break;
21850 case DW_LNE_set_discriminator:
21851 {
21852 /* The discriminator is not interesting to the
21853 debugger; just ignore it. We still need to
21854 check its value though:
21855 if there are consecutive entries for the same
21856 (non-prologue) line we want to coalesce them.
21857 PR 17276. */
21858 unsigned int discr
21859 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21860 line_ptr += bytes_read;
21861
21862 state_machine.handle_set_discriminator (discr);
21863 }
21864 break;
21865 default:
21866 complaint (_("mangled .debug_line section"));
21867 return;
21868 }
21869 /* Make sure that we parsed the extended op correctly. If e.g.
21870 we expected a different address size than the producer used,
21871 we may have read the wrong number of bytes. */
21872 if (line_ptr != extended_end)
21873 {
21874 complaint (_("mangled .debug_line section"));
21875 return;
21876 }
21877 break;
21878 case DW_LNS_copy:
21879 state_machine.handle_copy ();
21880 break;
21881 case DW_LNS_advance_pc:
21882 {
21883 CORE_ADDR adjust
21884 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21885 line_ptr += bytes_read;
21886
21887 state_machine.handle_advance_pc (adjust);
21888 }
21889 break;
21890 case DW_LNS_advance_line:
21891 {
21892 int line_delta
21893 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21894 line_ptr += bytes_read;
21895
21896 state_machine.handle_advance_line (line_delta);
21897 }
21898 break;
21899 case DW_LNS_set_file:
21900 {
21901 file_name_index file
21902 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21903 &bytes_read);
21904 line_ptr += bytes_read;
21905
21906 state_machine.handle_set_file (file);
21907 }
21908 break;
21909 case DW_LNS_set_column:
21910 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21911 line_ptr += bytes_read;
21912 break;
21913 case DW_LNS_negate_stmt:
21914 state_machine.handle_negate_stmt ();
21915 break;
21916 case DW_LNS_set_basic_block:
21917 break;
21918 /* Add to the address register of the state machine the
21919 address increment value corresponding to special opcode
21920 255. I.e., this value is scaled by the minimum
21921 instruction length since special opcode 255 would have
21922 scaled the increment. */
21923 case DW_LNS_const_add_pc:
21924 state_machine.handle_const_add_pc ();
21925 break;
21926 case DW_LNS_fixed_advance_pc:
21927 {
21928 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21929 line_ptr += 2;
21930
21931 state_machine.handle_fixed_advance_pc (addr_adj);
21932 }
21933 break;
21934 default:
21935 {
21936 /* Unknown standard opcode, ignore it. */
21937 int i;
21938
21939 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21940 {
21941 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21942 line_ptr += bytes_read;
21943 }
21944 }
21945 }
21946 }
21947
21948 if (!end_sequence)
21949 dwarf2_debug_line_missing_end_sequence_complaint ();
21950
21951 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21952 in which case we still finish recording the last line). */
21953 state_machine.record_line (true);
21954 }
21955 }
21956
21957 /* Decode the Line Number Program (LNP) for the given line_header
21958 structure and CU. The actual information extracted and the type
21959 of structures created from the LNP depends on the value of PST.
21960
21961 1. If PST is NULL, then this procedure uses the data from the program
21962 to create all necessary symbol tables, and their linetables.
21963
21964 2. If PST is not NULL, this procedure reads the program to determine
21965 the list of files included by the unit represented by PST, and
21966 builds all the associated partial symbol tables.
21967
21968 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21969 It is used for relative paths in the line table.
21970 NOTE: When processing partial symtabs (pst != NULL),
21971 comp_dir == pst->dirname.
21972
21973 NOTE: It is important that psymtabs have the same file name (via strcmp)
21974 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21975 symtab we don't use it in the name of the psymtabs we create.
21976 E.g. expand_line_sal requires this when finding psymtabs to expand.
21977 A good testcase for this is mb-inline.exp.
21978
21979 LOWPC is the lowest address in CU (or 0 if not known).
21980
21981 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21982 for its PC<->lines mapping information. Otherwise only the filename
21983 table is read in. */
21984
21985 static void
21986 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21987 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21988 CORE_ADDR lowpc, int decode_mapping)
21989 {
21990 struct objfile *objfile = cu->per_objfile->objfile;
21991 const int decode_for_pst_p = (pst != NULL);
21992
21993 if (decode_mapping)
21994 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21995
21996 if (decode_for_pst_p)
21997 {
21998 /* Now that we're done scanning the Line Header Program, we can
21999 create the psymtab of each included file. */
22000 for (auto &file_entry : lh->file_names ())
22001 if (file_entry.included_p == 1)
22002 {
22003 gdb::unique_xmalloc_ptr<char> name_holder;
22004 const char *include_name =
22005 psymtab_include_file_name (lh, file_entry, pst,
22006 comp_dir, &name_holder);
22007 if (include_name != NULL)
22008 dwarf2_create_include_psymtab
22009 (cu->per_objfile->per_bfd, include_name, pst,
22010 cu->per_objfile->per_bfd->partial_symtabs.get (),
22011 objfile->per_bfd);
22012 }
22013 }
22014 else
22015 {
22016 /* Make sure a symtab is created for every file, even files
22017 which contain only variables (i.e. no code with associated
22018 line numbers). */
22019 buildsym_compunit *builder = cu->get_builder ();
22020 struct compunit_symtab *cust = builder->get_compunit_symtab ();
22021
22022 for (auto &fe : lh->file_names ())
22023 {
22024 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
22025 if (builder->get_current_subfile ()->symtab == NULL)
22026 {
22027 builder->get_current_subfile ()->symtab
22028 = allocate_symtab (cust,
22029 builder->get_current_subfile ()->name);
22030 }
22031 fe.symtab = builder->get_current_subfile ()->symtab;
22032 }
22033 }
22034 }
22035
22036 /* Start a subfile for DWARF. FILENAME is the name of the file and
22037 DIRNAME the name of the source directory which contains FILENAME
22038 or NULL if not known.
22039 This routine tries to keep line numbers from identical absolute and
22040 relative file names in a common subfile.
22041
22042 Using the `list' example from the GDB testsuite, which resides in
22043 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22044 of /srcdir/list0.c yields the following debugging information for list0.c:
22045
22046 DW_AT_name: /srcdir/list0.c
22047 DW_AT_comp_dir: /compdir
22048 files.files[0].name: list0.h
22049 files.files[0].dir: /srcdir
22050 files.files[1].name: list0.c
22051 files.files[1].dir: /srcdir
22052
22053 The line number information for list0.c has to end up in a single
22054 subfile, so that `break /srcdir/list0.c:1' works as expected.
22055 start_subfile will ensure that this happens provided that we pass the
22056 concatenation of files.files[1].dir and files.files[1].name as the
22057 subfile's name. */
22058
22059 static void
22060 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22061 const char *dirname)
22062 {
22063 gdb::unique_xmalloc_ptr<char> copy;
22064
22065 /* In order not to lose the line information directory,
22066 we concatenate it to the filename when it makes sense.
22067 Note that the Dwarf3 standard says (speaking of filenames in line
22068 information): ``The directory index is ignored for file names
22069 that represent full path names''. Thus ignoring dirname in the
22070 `else' branch below isn't an issue. */
22071
22072 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
22073 {
22074 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22075 filename = copy.get ();
22076 }
22077
22078 cu->get_builder ()->start_subfile (filename);
22079 }
22080
22081 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22082 buildsym_compunit constructor. */
22083
22084 struct compunit_symtab *
22085 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22086 CORE_ADDR low_pc)
22087 {
22088 gdb_assert (m_builder == nullptr);
22089
22090 m_builder.reset (new struct buildsym_compunit
22091 (this->per_objfile->objfile,
22092 name, comp_dir, language, low_pc));
22093
22094 list_in_scope = get_builder ()->get_file_symbols ();
22095
22096 get_builder ()->record_debugformat ("DWARF 2");
22097 get_builder ()->record_producer (producer);
22098
22099 processing_has_namespace_info = false;
22100
22101 return get_builder ()->get_compunit_symtab ();
22102 }
22103
22104 static void
22105 var_decode_location (struct attribute *attr, struct symbol *sym,
22106 struct dwarf2_cu *cu)
22107 {
22108 struct objfile *objfile = cu->per_objfile->objfile;
22109 struct comp_unit_head *cu_header = &cu->header;
22110
22111 /* NOTE drow/2003-01-30: There used to be a comment and some special
22112 code here to turn a symbol with DW_AT_external and a
22113 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22114 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22115 with some versions of binutils) where shared libraries could have
22116 relocations against symbols in their debug information - the
22117 minimal symbol would have the right address, but the debug info
22118 would not. It's no longer necessary, because we will explicitly
22119 apply relocations when we read in the debug information now. */
22120
22121 /* A DW_AT_location attribute with no contents indicates that a
22122 variable has been optimized away. */
22123 if (attr->form_is_block () && attr->as_block ()->size == 0)
22124 {
22125 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22126 return;
22127 }
22128
22129 /* Handle one degenerate form of location expression specially, to
22130 preserve GDB's previous behavior when section offsets are
22131 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22132 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
22133
22134 if (attr->form_is_block ())
22135 {
22136 struct dwarf_block *block = attr->as_block ();
22137
22138 if ((block->data[0] == DW_OP_addr
22139 && block->size == 1 + cu_header->addr_size)
22140 || ((block->data[0] == DW_OP_GNU_addr_index
22141 || block->data[0] == DW_OP_addrx)
22142 && (block->size
22143 == 1 + leb128_size (&block->data[1]))))
22144 {
22145 unsigned int dummy;
22146
22147 if (block->data[0] == DW_OP_addr)
22148 SET_SYMBOL_VALUE_ADDRESS
22149 (sym, cu->header.read_address (objfile->obfd,
22150 block->data + 1,
22151 &dummy));
22152 else
22153 SET_SYMBOL_VALUE_ADDRESS
22154 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22155 &dummy));
22156 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22157 fixup_symbol_section (sym, objfile);
22158 SET_SYMBOL_VALUE_ADDRESS
22159 (sym,
22160 SYMBOL_VALUE_ADDRESS (sym)
22161 + objfile->section_offsets[sym->section_index ()]);
22162 return;
22163 }
22164 }
22165
22166 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22167 expression evaluator, and use LOC_COMPUTED only when necessary
22168 (i.e. when the value of a register or memory location is
22169 referenced, or a thread-local block, etc.). Then again, it might
22170 not be worthwhile. I'm assuming that it isn't unless performance
22171 or memory numbers show me otherwise. */
22172
22173 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22174
22175 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22176 cu->has_loclist = true;
22177 }
22178
22179 /* Given a pointer to a DWARF information entry, figure out if we need
22180 to make a symbol table entry for it, and if so, create a new entry
22181 and return a pointer to it.
22182 If TYPE is NULL, determine symbol type from the die, otherwise
22183 used the passed type.
22184 If SPACE is not NULL, use it to hold the new symbol. If it is
22185 NULL, allocate a new symbol on the objfile's obstack. */
22186
22187 static struct symbol *
22188 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22189 struct symbol *space)
22190 {
22191 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22192 struct objfile *objfile = per_objfile->objfile;
22193 struct gdbarch *gdbarch = objfile->arch ();
22194 struct symbol *sym = NULL;
22195 const char *name;
22196 struct attribute *attr = NULL;
22197 struct attribute *attr2 = NULL;
22198 CORE_ADDR baseaddr;
22199 struct pending **list_to_add = NULL;
22200
22201 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22202
22203 baseaddr = objfile->text_section_offset ();
22204
22205 name = dwarf2_name (die, cu);
22206 if (name)
22207 {
22208 int suppress_add = 0;
22209
22210 if (space)
22211 sym = space;
22212 else
22213 sym = new (&objfile->objfile_obstack) symbol;
22214 OBJSTAT (objfile, n_syms++);
22215
22216 /* Cache this symbol's name and the name's demangled form (if any). */
22217 sym->set_language (cu->language, &objfile->objfile_obstack);
22218 /* Fortran does not have mangling standard and the mangling does differ
22219 between gfortran, iFort etc. */
22220 const char *physname
22221 = (cu->language == language_fortran
22222 ? dwarf2_full_name (name, die, cu)
22223 : dwarf2_physname (name, die, cu));
22224 const char *linkagename = dw2_linkage_name (die, cu);
22225
22226 if (linkagename == nullptr || cu->language == language_ada)
22227 sym->set_linkage_name (physname);
22228 else
22229 {
22230 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22231 sym->set_linkage_name (linkagename);
22232 }
22233
22234 /* Default assumptions.
22235 Use the passed type or decode it from the die. */
22236 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22237 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22238 if (type != NULL)
22239 SYMBOL_TYPE (sym) = type;
22240 else
22241 SYMBOL_TYPE (sym) = die_type (die, cu);
22242 attr = dwarf2_attr (die,
22243 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22244 cu);
22245 if (attr != nullptr)
22246 SYMBOL_LINE (sym) = attr->constant_value (0);
22247
22248 attr = dwarf2_attr (die,
22249 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22250 cu);
22251 if (attr != nullptr && attr->is_nonnegative ())
22252 {
22253 file_name_index file_index
22254 = (file_name_index) attr->as_nonnegative ();
22255 struct file_entry *fe;
22256
22257 if (cu->line_header != NULL)
22258 fe = cu->line_header->file_name_at (file_index);
22259 else
22260 fe = NULL;
22261
22262 if (fe == NULL)
22263 complaint (_("file index out of range"));
22264 else
22265 symbol_set_symtab (sym, fe->symtab);
22266 }
22267
22268 switch (die->tag)
22269 {
22270 case DW_TAG_label:
22271 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22272 if (attr != nullptr)
22273 {
22274 CORE_ADDR addr;
22275
22276 addr = attr->as_address ();
22277 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22278 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22279 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22280 }
22281 else
22282 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22283 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22284 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22285 add_symbol_to_list (sym, cu->list_in_scope);
22286 break;
22287 case DW_TAG_subprogram:
22288 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22289 finish_block. */
22290 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22291 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22292 if ((attr2 != nullptr && attr2->as_boolean ())
22293 || cu->language == language_ada
22294 || cu->language == language_fortran)
22295 {
22296 /* Subprograms marked external are stored as a global symbol.
22297 Ada and Fortran subprograms, whether marked external or
22298 not, are always stored as a global symbol, because we want
22299 to be able to access them globally. For instance, we want
22300 to be able to break on a nested subprogram without having
22301 to specify the context. */
22302 list_to_add = cu->get_builder ()->get_global_symbols ();
22303 }
22304 else
22305 {
22306 list_to_add = cu->list_in_scope;
22307 }
22308 break;
22309 case DW_TAG_inlined_subroutine:
22310 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22311 finish_block. */
22312 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22313 SYMBOL_INLINED (sym) = 1;
22314 list_to_add = cu->list_in_scope;
22315 break;
22316 case DW_TAG_template_value_param:
22317 suppress_add = 1;
22318 /* Fall through. */
22319 case DW_TAG_constant:
22320 case DW_TAG_variable:
22321 case DW_TAG_member:
22322 /* Compilation with minimal debug info may result in
22323 variables with missing type entries. Change the
22324 misleading `void' type to something sensible. */
22325 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22326 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22327
22328 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22329 /* In the case of DW_TAG_member, we should only be called for
22330 static const members. */
22331 if (die->tag == DW_TAG_member)
22332 {
22333 /* dwarf2_add_field uses die_is_declaration,
22334 so we do the same. */
22335 gdb_assert (die_is_declaration (die, cu));
22336 gdb_assert (attr);
22337 }
22338 if (attr != nullptr)
22339 {
22340 dwarf2_const_value (attr, sym, cu);
22341 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22342 if (!suppress_add)
22343 {
22344 if (attr2 != nullptr && attr2->as_boolean ())
22345 list_to_add = cu->get_builder ()->get_global_symbols ();
22346 else
22347 list_to_add = cu->list_in_scope;
22348 }
22349 break;
22350 }
22351 attr = dwarf2_attr (die, DW_AT_location, cu);
22352 if (attr != nullptr)
22353 {
22354 var_decode_location (attr, sym, cu);
22355 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22356
22357 /* Fortran explicitly imports any global symbols to the local
22358 scope by DW_TAG_common_block. */
22359 if (cu->language == language_fortran && die->parent
22360 && die->parent->tag == DW_TAG_common_block)
22361 attr2 = NULL;
22362
22363 if (SYMBOL_CLASS (sym) == LOC_STATIC
22364 && SYMBOL_VALUE_ADDRESS (sym) == 0
22365 && !per_objfile->per_bfd->has_section_at_zero)
22366 {
22367 /* When a static variable is eliminated by the linker,
22368 the corresponding debug information is not stripped
22369 out, but the variable address is set to null;
22370 do not add such variables into symbol table. */
22371 }
22372 else if (attr2 != nullptr && attr2->as_boolean ())
22373 {
22374 if (SYMBOL_CLASS (sym) == LOC_STATIC
22375 && (objfile->flags & OBJF_MAINLINE) == 0
22376 && per_objfile->per_bfd->can_copy)
22377 {
22378 /* A global static variable might be subject to
22379 copy relocation. We first check for a local
22380 minsym, though, because maybe the symbol was
22381 marked hidden, in which case this would not
22382 apply. */
22383 bound_minimal_symbol found
22384 = (lookup_minimal_symbol_linkage
22385 (sym->linkage_name (), objfile));
22386 if (found.minsym != nullptr)
22387 sym->maybe_copied = 1;
22388 }
22389
22390 /* A variable with DW_AT_external is never static,
22391 but it may be block-scoped. */
22392 list_to_add
22393 = ((cu->list_in_scope
22394 == cu->get_builder ()->get_file_symbols ())
22395 ? cu->get_builder ()->get_global_symbols ()
22396 : cu->list_in_scope);
22397 }
22398 else
22399 list_to_add = cu->list_in_scope;
22400 }
22401 else
22402 {
22403 /* We do not know the address of this symbol.
22404 If it is an external symbol and we have type information
22405 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22406 The address of the variable will then be determined from
22407 the minimal symbol table whenever the variable is
22408 referenced. */
22409 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22410
22411 /* Fortran explicitly imports any global symbols to the local
22412 scope by DW_TAG_common_block. */
22413 if (cu->language == language_fortran && die->parent
22414 && die->parent->tag == DW_TAG_common_block)
22415 {
22416 /* SYMBOL_CLASS doesn't matter here because
22417 read_common_block is going to reset it. */
22418 if (!suppress_add)
22419 list_to_add = cu->list_in_scope;
22420 }
22421 else if (attr2 != nullptr && attr2->as_boolean ()
22422 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22423 {
22424 /* A variable with DW_AT_external is never static, but it
22425 may be block-scoped. */
22426 list_to_add
22427 = ((cu->list_in_scope
22428 == cu->get_builder ()->get_file_symbols ())
22429 ? cu->get_builder ()->get_global_symbols ()
22430 : cu->list_in_scope);
22431
22432 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22433 }
22434 else if (!die_is_declaration (die, cu))
22435 {
22436 /* Use the default LOC_OPTIMIZED_OUT class. */
22437 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22438 if (!suppress_add)
22439 list_to_add = cu->list_in_scope;
22440 }
22441 }
22442 break;
22443 case DW_TAG_formal_parameter:
22444 {
22445 /* If we are inside a function, mark this as an argument. If
22446 not, we might be looking at an argument to an inlined function
22447 when we do not have enough information to show inlined frames;
22448 pretend it's a local variable in that case so that the user can
22449 still see it. */
22450 struct context_stack *curr
22451 = cu->get_builder ()->get_current_context_stack ();
22452 if (curr != nullptr && curr->name != nullptr)
22453 SYMBOL_IS_ARGUMENT (sym) = 1;
22454 attr = dwarf2_attr (die, DW_AT_location, cu);
22455 if (attr != nullptr)
22456 {
22457 var_decode_location (attr, sym, cu);
22458 }
22459 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22460 if (attr != nullptr)
22461 {
22462 dwarf2_const_value (attr, sym, cu);
22463 }
22464
22465 list_to_add = cu->list_in_scope;
22466 }
22467 break;
22468 case DW_TAG_unspecified_parameters:
22469 /* From varargs functions; gdb doesn't seem to have any
22470 interest in this information, so just ignore it for now.
22471 (FIXME?) */
22472 break;
22473 case DW_TAG_template_type_param:
22474 suppress_add = 1;
22475 /* Fall through. */
22476 case DW_TAG_class_type:
22477 case DW_TAG_interface_type:
22478 case DW_TAG_structure_type:
22479 case DW_TAG_union_type:
22480 case DW_TAG_set_type:
22481 case DW_TAG_enumeration_type:
22482 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22483 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22484
22485 {
22486 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22487 really ever be static objects: otherwise, if you try
22488 to, say, break of a class's method and you're in a file
22489 which doesn't mention that class, it won't work unless
22490 the check for all static symbols in lookup_symbol_aux
22491 saves you. See the OtherFileClass tests in
22492 gdb.c++/namespace.exp. */
22493
22494 if (!suppress_add)
22495 {
22496 buildsym_compunit *builder = cu->get_builder ();
22497 list_to_add
22498 = (cu->list_in_scope == builder->get_file_symbols ()
22499 && cu->language == language_cplus
22500 ? builder->get_global_symbols ()
22501 : cu->list_in_scope);
22502
22503 /* The semantics of C++ state that "struct foo {
22504 ... }" also defines a typedef for "foo". */
22505 if (cu->language == language_cplus
22506 || cu->language == language_ada
22507 || cu->language == language_d
22508 || cu->language == language_rust)
22509 {
22510 /* The symbol's name is already allocated along
22511 with this objfile, so we don't need to
22512 duplicate it for the type. */
22513 if (SYMBOL_TYPE (sym)->name () == 0)
22514 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22515 }
22516 }
22517 }
22518 break;
22519 case DW_TAG_typedef:
22520 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22521 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22522 list_to_add = cu->list_in_scope;
22523 break;
22524 case DW_TAG_array_type:
22525 case DW_TAG_base_type:
22526 case DW_TAG_subrange_type:
22527 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22528 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22529 list_to_add = cu->list_in_scope;
22530 break;
22531 case DW_TAG_enumerator:
22532 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22533 if (attr != nullptr)
22534 {
22535 dwarf2_const_value (attr, sym, cu);
22536 }
22537 {
22538 /* NOTE: carlton/2003-11-10: See comment above in the
22539 DW_TAG_class_type, etc. block. */
22540
22541 list_to_add
22542 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22543 && cu->language == language_cplus
22544 ? cu->get_builder ()->get_global_symbols ()
22545 : cu->list_in_scope);
22546 }
22547 break;
22548 case DW_TAG_imported_declaration:
22549 case DW_TAG_namespace:
22550 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22551 list_to_add = cu->get_builder ()->get_global_symbols ();
22552 break;
22553 case DW_TAG_module:
22554 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22555 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22556 list_to_add = cu->get_builder ()->get_global_symbols ();
22557 break;
22558 case DW_TAG_common_block:
22559 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22560 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22561 add_symbol_to_list (sym, cu->list_in_scope);
22562 break;
22563 default:
22564 /* Not a tag we recognize. Hopefully we aren't processing
22565 trash data, but since we must specifically ignore things
22566 we don't recognize, there is nothing else we should do at
22567 this point. */
22568 complaint (_("unsupported tag: '%s'"),
22569 dwarf_tag_name (die->tag));
22570 break;
22571 }
22572
22573 if (suppress_add)
22574 {
22575 sym->hash_next = objfile->template_symbols;
22576 objfile->template_symbols = sym;
22577 list_to_add = NULL;
22578 }
22579
22580 if (list_to_add != NULL)
22581 add_symbol_to_list (sym, list_to_add);
22582
22583 /* For the benefit of old versions of GCC, check for anonymous
22584 namespaces based on the demangled name. */
22585 if (!cu->processing_has_namespace_info
22586 && cu->language == language_cplus)
22587 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22588 }
22589 return (sym);
22590 }
22591
22592 /* Given an attr with a DW_FORM_dataN value in host byte order,
22593 zero-extend it as appropriate for the symbol's type. The DWARF
22594 standard (v4) is not entirely clear about the meaning of using
22595 DW_FORM_dataN for a constant with a signed type, where the type is
22596 wider than the data. The conclusion of a discussion on the DWARF
22597 list was that this is unspecified. We choose to always zero-extend
22598 because that is the interpretation long in use by GCC. */
22599
22600 static gdb_byte *
22601 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22602 struct dwarf2_cu *cu, LONGEST *value, int bits)
22603 {
22604 struct objfile *objfile = cu->per_objfile->objfile;
22605 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22606 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22607 LONGEST l = attr->constant_value (0);
22608
22609 if (bits < sizeof (*value) * 8)
22610 {
22611 l &= ((LONGEST) 1 << bits) - 1;
22612 *value = l;
22613 }
22614 else if (bits == sizeof (*value) * 8)
22615 *value = l;
22616 else
22617 {
22618 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22619 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22620 return bytes;
22621 }
22622
22623 return NULL;
22624 }
22625
22626 /* Read a constant value from an attribute. Either set *VALUE, or if
22627 the value does not fit in *VALUE, set *BYTES - either already
22628 allocated on the objfile obstack, or newly allocated on OBSTACK,
22629 or, set *BATON, if we translated the constant to a location
22630 expression. */
22631
22632 static void
22633 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22634 const char *name, struct obstack *obstack,
22635 struct dwarf2_cu *cu,
22636 LONGEST *value, const gdb_byte **bytes,
22637 struct dwarf2_locexpr_baton **baton)
22638 {
22639 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22640 struct objfile *objfile = per_objfile->objfile;
22641 struct comp_unit_head *cu_header = &cu->header;
22642 struct dwarf_block *blk;
22643 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22644 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22645
22646 *value = 0;
22647 *bytes = NULL;
22648 *baton = NULL;
22649
22650 switch (attr->form)
22651 {
22652 case DW_FORM_addr:
22653 case DW_FORM_addrx:
22654 case DW_FORM_GNU_addr_index:
22655 {
22656 gdb_byte *data;
22657
22658 if (TYPE_LENGTH (type) != cu_header->addr_size)
22659 dwarf2_const_value_length_mismatch_complaint (name,
22660 cu_header->addr_size,
22661 TYPE_LENGTH (type));
22662 /* Symbols of this form are reasonably rare, so we just
22663 piggyback on the existing location code rather than writing
22664 a new implementation of symbol_computed_ops. */
22665 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22666 (*baton)->per_objfile = per_objfile;
22667 (*baton)->per_cu = cu->per_cu;
22668 gdb_assert ((*baton)->per_cu);
22669
22670 (*baton)->size = 2 + cu_header->addr_size;
22671 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22672 (*baton)->data = data;
22673
22674 data[0] = DW_OP_addr;
22675 store_unsigned_integer (&data[1], cu_header->addr_size,
22676 byte_order, attr->as_address ());
22677 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22678 }
22679 break;
22680 case DW_FORM_string:
22681 case DW_FORM_strp:
22682 case DW_FORM_strx:
22683 case DW_FORM_GNU_str_index:
22684 case DW_FORM_GNU_strp_alt:
22685 /* The string is already allocated on the objfile obstack, point
22686 directly to it. */
22687 *bytes = (const gdb_byte *) attr->as_string ();
22688 break;
22689 case DW_FORM_block1:
22690 case DW_FORM_block2:
22691 case DW_FORM_block4:
22692 case DW_FORM_block:
22693 case DW_FORM_exprloc:
22694 case DW_FORM_data16:
22695 blk = attr->as_block ();
22696 if (TYPE_LENGTH (type) != blk->size)
22697 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22698 TYPE_LENGTH (type));
22699 *bytes = blk->data;
22700 break;
22701
22702 /* The DW_AT_const_value attributes are supposed to carry the
22703 symbol's value "represented as it would be on the target
22704 architecture." By the time we get here, it's already been
22705 converted to host endianness, so we just need to sign- or
22706 zero-extend it as appropriate. */
22707 case DW_FORM_data1:
22708 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22709 break;
22710 case DW_FORM_data2:
22711 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22712 break;
22713 case DW_FORM_data4:
22714 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22715 break;
22716 case DW_FORM_data8:
22717 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22718 break;
22719
22720 case DW_FORM_sdata:
22721 case DW_FORM_implicit_const:
22722 *value = attr->as_signed ();
22723 break;
22724
22725 case DW_FORM_udata:
22726 *value = attr->as_unsigned ();
22727 break;
22728
22729 default:
22730 complaint (_("unsupported const value attribute form: '%s'"),
22731 dwarf_form_name (attr->form));
22732 *value = 0;
22733 break;
22734 }
22735 }
22736
22737
22738 /* Copy constant value from an attribute to a symbol. */
22739
22740 static void
22741 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22742 struct dwarf2_cu *cu)
22743 {
22744 struct objfile *objfile = cu->per_objfile->objfile;
22745 LONGEST value;
22746 const gdb_byte *bytes;
22747 struct dwarf2_locexpr_baton *baton;
22748
22749 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22750 sym->print_name (),
22751 &objfile->objfile_obstack, cu,
22752 &value, &bytes, &baton);
22753
22754 if (baton != NULL)
22755 {
22756 SYMBOL_LOCATION_BATON (sym) = baton;
22757 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22758 }
22759 else if (bytes != NULL)
22760 {
22761 SYMBOL_VALUE_BYTES (sym) = bytes;
22762 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22763 }
22764 else
22765 {
22766 SYMBOL_VALUE (sym) = value;
22767 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22768 }
22769 }
22770
22771 /* Return the type of the die in question using its DW_AT_type attribute. */
22772
22773 static struct type *
22774 die_type (struct die_info *die, struct dwarf2_cu *cu)
22775 {
22776 struct attribute *type_attr;
22777
22778 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22779 if (!type_attr)
22780 {
22781 struct objfile *objfile = cu->per_objfile->objfile;
22782 /* A missing DW_AT_type represents a void type. */
22783 return objfile_type (objfile)->builtin_void;
22784 }
22785
22786 return lookup_die_type (die, type_attr, cu);
22787 }
22788
22789 /* True iff CU's producer generates GNAT Ada auxiliary information
22790 that allows to find parallel types through that information instead
22791 of having to do expensive parallel lookups by type name. */
22792
22793 static int
22794 need_gnat_info (struct dwarf2_cu *cu)
22795 {
22796 /* Assume that the Ada compiler was GNAT, which always produces
22797 the auxiliary information. */
22798 return (cu->language == language_ada);
22799 }
22800
22801 /* Return the auxiliary type of the die in question using its
22802 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22803 attribute is not present. */
22804
22805 static struct type *
22806 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22807 {
22808 struct attribute *type_attr;
22809
22810 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22811 if (!type_attr)
22812 return NULL;
22813
22814 return lookup_die_type (die, type_attr, cu);
22815 }
22816
22817 /* If DIE has a descriptive_type attribute, then set the TYPE's
22818 descriptive type accordingly. */
22819
22820 static void
22821 set_descriptive_type (struct type *type, struct die_info *die,
22822 struct dwarf2_cu *cu)
22823 {
22824 struct type *descriptive_type = die_descriptive_type (die, cu);
22825
22826 if (descriptive_type)
22827 {
22828 ALLOCATE_GNAT_AUX_TYPE (type);
22829 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22830 }
22831 }
22832
22833 /* Return the containing type of the die in question using its
22834 DW_AT_containing_type attribute. */
22835
22836 static struct type *
22837 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22838 {
22839 struct attribute *type_attr;
22840 struct objfile *objfile = cu->per_objfile->objfile;
22841
22842 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22843 if (!type_attr)
22844 error (_("Dwarf Error: Problem turning containing type into gdb type "
22845 "[in module %s]"), objfile_name (objfile));
22846
22847 return lookup_die_type (die, type_attr, cu);
22848 }
22849
22850 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22851
22852 static struct type *
22853 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22854 {
22855 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22856 struct objfile *objfile = per_objfile->objfile;
22857 char *saved;
22858
22859 std::string message
22860 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22861 objfile_name (objfile),
22862 sect_offset_str (cu->header.sect_off),
22863 sect_offset_str (die->sect_off));
22864 saved = obstack_strdup (&objfile->objfile_obstack, message);
22865
22866 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22867 }
22868
22869 /* Look up the type of DIE in CU using its type attribute ATTR.
22870 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22871 DW_AT_containing_type.
22872 If there is no type substitute an error marker. */
22873
22874 static struct type *
22875 lookup_die_type (struct die_info *die, const struct attribute *attr,
22876 struct dwarf2_cu *cu)
22877 {
22878 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22879 struct objfile *objfile = per_objfile->objfile;
22880 struct type *this_type;
22881
22882 gdb_assert (attr->name == DW_AT_type
22883 || attr->name == DW_AT_GNAT_descriptive_type
22884 || attr->name == DW_AT_containing_type);
22885
22886 /* First see if we have it cached. */
22887
22888 if (attr->form == DW_FORM_GNU_ref_alt)
22889 {
22890 struct dwarf2_per_cu_data *per_cu;
22891 sect_offset sect_off = attr->get_ref_die_offset ();
22892
22893 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22894 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22895 }
22896 else if (attr->form_is_ref ())
22897 {
22898 sect_offset sect_off = attr->get_ref_die_offset ();
22899
22900 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22901 }
22902 else if (attr->form == DW_FORM_ref_sig8)
22903 {
22904 ULONGEST signature = attr->as_signature ();
22905
22906 return get_signatured_type (die, signature, cu);
22907 }
22908 else
22909 {
22910 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22911 " at %s [in module %s]"),
22912 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22913 objfile_name (objfile));
22914 return build_error_marker_type (cu, die);
22915 }
22916
22917 /* If not cached we need to read it in. */
22918
22919 if (this_type == NULL)
22920 {
22921 struct die_info *type_die = NULL;
22922 struct dwarf2_cu *type_cu = cu;
22923
22924 if (attr->form_is_ref ())
22925 type_die = follow_die_ref (die, attr, &type_cu);
22926 if (type_die == NULL)
22927 return build_error_marker_type (cu, die);
22928 /* If we find the type now, it's probably because the type came
22929 from an inter-CU reference and the type's CU got expanded before
22930 ours. */
22931 this_type = read_type_die (type_die, type_cu);
22932 }
22933
22934 /* If we still don't have a type use an error marker. */
22935
22936 if (this_type == NULL)
22937 return build_error_marker_type (cu, die);
22938
22939 return this_type;
22940 }
22941
22942 /* Return the type in DIE, CU.
22943 Returns NULL for invalid types.
22944
22945 This first does a lookup in die_type_hash,
22946 and only reads the die in if necessary.
22947
22948 NOTE: This can be called when reading in partial or full symbols. */
22949
22950 static struct type *
22951 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22952 {
22953 struct type *this_type;
22954
22955 this_type = get_die_type (die, cu);
22956 if (this_type)
22957 return this_type;
22958
22959 return read_type_die_1 (die, cu);
22960 }
22961
22962 /* Read the type in DIE, CU.
22963 Returns NULL for invalid types. */
22964
22965 static struct type *
22966 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22967 {
22968 struct type *this_type = NULL;
22969
22970 switch (die->tag)
22971 {
22972 case DW_TAG_class_type:
22973 case DW_TAG_interface_type:
22974 case DW_TAG_structure_type:
22975 case DW_TAG_union_type:
22976 this_type = read_structure_type (die, cu);
22977 break;
22978 case DW_TAG_enumeration_type:
22979 this_type = read_enumeration_type (die, cu);
22980 break;
22981 case DW_TAG_subprogram:
22982 case DW_TAG_subroutine_type:
22983 case DW_TAG_inlined_subroutine:
22984 this_type = read_subroutine_type (die, cu);
22985 break;
22986 case DW_TAG_array_type:
22987 this_type = read_array_type (die, cu);
22988 break;
22989 case DW_TAG_set_type:
22990 this_type = read_set_type (die, cu);
22991 break;
22992 case DW_TAG_pointer_type:
22993 this_type = read_tag_pointer_type (die, cu);
22994 break;
22995 case DW_TAG_ptr_to_member_type:
22996 this_type = read_tag_ptr_to_member_type (die, cu);
22997 break;
22998 case DW_TAG_reference_type:
22999 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
23000 break;
23001 case DW_TAG_rvalue_reference_type:
23002 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
23003 break;
23004 case DW_TAG_const_type:
23005 this_type = read_tag_const_type (die, cu);
23006 break;
23007 case DW_TAG_volatile_type:
23008 this_type = read_tag_volatile_type (die, cu);
23009 break;
23010 case DW_TAG_restrict_type:
23011 this_type = read_tag_restrict_type (die, cu);
23012 break;
23013 case DW_TAG_string_type:
23014 this_type = read_tag_string_type (die, cu);
23015 break;
23016 case DW_TAG_typedef:
23017 this_type = read_typedef (die, cu);
23018 break;
23019 case DW_TAG_subrange_type:
23020 this_type = read_subrange_type (die, cu);
23021 break;
23022 case DW_TAG_base_type:
23023 this_type = read_base_type (die, cu);
23024 break;
23025 case DW_TAG_unspecified_type:
23026 this_type = read_unspecified_type (die, cu);
23027 break;
23028 case DW_TAG_namespace:
23029 this_type = read_namespace_type (die, cu);
23030 break;
23031 case DW_TAG_module:
23032 this_type = read_module_type (die, cu);
23033 break;
23034 case DW_TAG_atomic_type:
23035 this_type = read_tag_atomic_type (die, cu);
23036 break;
23037 default:
23038 complaint (_("unexpected tag in read_type_die: '%s'"),
23039 dwarf_tag_name (die->tag));
23040 break;
23041 }
23042
23043 return this_type;
23044 }
23045
23046 /* See if we can figure out if the class lives in a namespace. We do
23047 this by looking for a member function; its demangled name will
23048 contain namespace info, if there is any.
23049 Return the computed name or NULL.
23050 Space for the result is allocated on the objfile's obstack.
23051 This is the full-die version of guess_partial_die_structure_name.
23052 In this case we know DIE has no useful parent. */
23053
23054 static const char *
23055 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23056 {
23057 struct die_info *spec_die;
23058 struct dwarf2_cu *spec_cu;
23059 struct die_info *child;
23060 struct objfile *objfile = cu->per_objfile->objfile;
23061
23062 spec_cu = cu;
23063 spec_die = die_specification (die, &spec_cu);
23064 if (spec_die != NULL)
23065 {
23066 die = spec_die;
23067 cu = spec_cu;
23068 }
23069
23070 for (child = die->child;
23071 child != NULL;
23072 child = child->sibling)
23073 {
23074 if (child->tag == DW_TAG_subprogram)
23075 {
23076 const char *linkage_name = dw2_linkage_name (child, cu);
23077
23078 if (linkage_name != NULL)
23079 {
23080 gdb::unique_xmalloc_ptr<char> actual_name
23081 (cu->language_defn->class_name_from_physname (linkage_name));
23082 const char *name = NULL;
23083
23084 if (actual_name != NULL)
23085 {
23086 const char *die_name = dwarf2_name (die, cu);
23087
23088 if (die_name != NULL
23089 && strcmp (die_name, actual_name.get ()) != 0)
23090 {
23091 /* Strip off the class name from the full name.
23092 We want the prefix. */
23093 int die_name_len = strlen (die_name);
23094 int actual_name_len = strlen (actual_name.get ());
23095 const char *ptr = actual_name.get ();
23096
23097 /* Test for '::' as a sanity check. */
23098 if (actual_name_len > die_name_len + 2
23099 && ptr[actual_name_len - die_name_len - 1] == ':')
23100 name = obstack_strndup (
23101 &objfile->per_bfd->storage_obstack,
23102 ptr, actual_name_len - die_name_len - 2);
23103 }
23104 }
23105 return name;
23106 }
23107 }
23108 }
23109
23110 return NULL;
23111 }
23112
23113 /* GCC might emit a nameless typedef that has a linkage name. Determine the
23114 prefix part in such case. See
23115 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23116
23117 static const char *
23118 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23119 {
23120 struct attribute *attr;
23121 const char *base;
23122
23123 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23124 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23125 return NULL;
23126
23127 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
23128 return NULL;
23129
23130 attr = dw2_linkage_name_attr (die, cu);
23131 const char *attr_name = attr->as_string ();
23132 if (attr == NULL || attr_name == NULL)
23133 return NULL;
23134
23135 /* dwarf2_name had to be already called. */
23136 gdb_assert (attr->canonical_string_p ());
23137
23138 /* Strip the base name, keep any leading namespaces/classes. */
23139 base = strrchr (attr_name, ':');
23140 if (base == NULL || base == attr_name || base[-1] != ':')
23141 return "";
23142
23143 struct objfile *objfile = cu->per_objfile->objfile;
23144 return obstack_strndup (&objfile->per_bfd->storage_obstack,
23145 attr_name,
23146 &base[-1] - attr_name);
23147 }
23148
23149 /* Return the name of the namespace/class that DIE is defined within,
23150 or "" if we can't tell. The caller should not xfree the result.
23151
23152 For example, if we're within the method foo() in the following
23153 code:
23154
23155 namespace N {
23156 class C {
23157 void foo () {
23158 }
23159 };
23160 }
23161
23162 then determine_prefix on foo's die will return "N::C". */
23163
23164 static const char *
23165 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23166 {
23167 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23168 struct die_info *parent, *spec_die;
23169 struct dwarf2_cu *spec_cu;
23170 struct type *parent_type;
23171 const char *retval;
23172
23173 if (cu->language != language_cplus
23174 && cu->language != language_fortran && cu->language != language_d
23175 && cu->language != language_rust)
23176 return "";
23177
23178 retval = anonymous_struct_prefix (die, cu);
23179 if (retval)
23180 return retval;
23181
23182 /* We have to be careful in the presence of DW_AT_specification.
23183 For example, with GCC 3.4, given the code
23184
23185 namespace N {
23186 void foo() {
23187 // Definition of N::foo.
23188 }
23189 }
23190
23191 then we'll have a tree of DIEs like this:
23192
23193 1: DW_TAG_compile_unit
23194 2: DW_TAG_namespace // N
23195 3: DW_TAG_subprogram // declaration of N::foo
23196 4: DW_TAG_subprogram // definition of N::foo
23197 DW_AT_specification // refers to die #3
23198
23199 Thus, when processing die #4, we have to pretend that we're in
23200 the context of its DW_AT_specification, namely the contex of die
23201 #3. */
23202 spec_cu = cu;
23203 spec_die = die_specification (die, &spec_cu);
23204 if (spec_die == NULL)
23205 parent = die->parent;
23206 else
23207 {
23208 parent = spec_die->parent;
23209 cu = spec_cu;
23210 }
23211
23212 if (parent == NULL)
23213 return "";
23214 else if (parent->building_fullname)
23215 {
23216 const char *name;
23217 const char *parent_name;
23218
23219 /* It has been seen on RealView 2.2 built binaries,
23220 DW_TAG_template_type_param types actually _defined_ as
23221 children of the parent class:
23222
23223 enum E {};
23224 template class <class Enum> Class{};
23225 Class<enum E> class_e;
23226
23227 1: DW_TAG_class_type (Class)
23228 2: DW_TAG_enumeration_type (E)
23229 3: DW_TAG_enumerator (enum1:0)
23230 3: DW_TAG_enumerator (enum2:1)
23231 ...
23232 2: DW_TAG_template_type_param
23233 DW_AT_type DW_FORM_ref_udata (E)
23234
23235 Besides being broken debug info, it can put GDB into an
23236 infinite loop. Consider:
23237
23238 When we're building the full name for Class<E>, we'll start
23239 at Class, and go look over its template type parameters,
23240 finding E. We'll then try to build the full name of E, and
23241 reach here. We're now trying to build the full name of E,
23242 and look over the parent DIE for containing scope. In the
23243 broken case, if we followed the parent DIE of E, we'd again
23244 find Class, and once again go look at its template type
23245 arguments, etc., etc. Simply don't consider such parent die
23246 as source-level parent of this die (it can't be, the language
23247 doesn't allow it), and break the loop here. */
23248 name = dwarf2_name (die, cu);
23249 parent_name = dwarf2_name (parent, cu);
23250 complaint (_("template param type '%s' defined within parent '%s'"),
23251 name ? name : "<unknown>",
23252 parent_name ? parent_name : "<unknown>");
23253 return "";
23254 }
23255 else
23256 switch (parent->tag)
23257 {
23258 case DW_TAG_namespace:
23259 parent_type = read_type_die (parent, cu);
23260 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23261 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23262 Work around this problem here. */
23263 if (cu->language == language_cplus
23264 && strcmp (parent_type->name (), "::") == 0)
23265 return "";
23266 /* We give a name to even anonymous namespaces. */
23267 return parent_type->name ();
23268 case DW_TAG_class_type:
23269 case DW_TAG_interface_type:
23270 case DW_TAG_structure_type:
23271 case DW_TAG_union_type:
23272 case DW_TAG_module:
23273 parent_type = read_type_die (parent, cu);
23274 if (parent_type->name () != NULL)
23275 return parent_type->name ();
23276 else
23277 /* An anonymous structure is only allowed non-static data
23278 members; no typedefs, no member functions, et cetera.
23279 So it does not need a prefix. */
23280 return "";
23281 case DW_TAG_compile_unit:
23282 case DW_TAG_partial_unit:
23283 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23284 if (cu->language == language_cplus
23285 && !per_objfile->per_bfd->types.empty ()
23286 && die->child != NULL
23287 && (die->tag == DW_TAG_class_type
23288 || die->tag == DW_TAG_structure_type
23289 || die->tag == DW_TAG_union_type))
23290 {
23291 const char *name = guess_full_die_structure_name (die, cu);
23292 if (name != NULL)
23293 return name;
23294 }
23295 return "";
23296 case DW_TAG_subprogram:
23297 /* Nested subroutines in Fortran get a prefix with the name
23298 of the parent's subroutine. */
23299 if (cu->language == language_fortran)
23300 {
23301 if ((die->tag == DW_TAG_subprogram)
23302 && (dwarf2_name (parent, cu) != NULL))
23303 return dwarf2_name (parent, cu);
23304 }
23305 return determine_prefix (parent, cu);
23306 case DW_TAG_enumeration_type:
23307 parent_type = read_type_die (parent, cu);
23308 if (parent_type->is_declared_class ())
23309 {
23310 if (parent_type->name () != NULL)
23311 return parent_type->name ();
23312 return "";
23313 }
23314 /* Fall through. */
23315 default:
23316 return determine_prefix (parent, cu);
23317 }
23318 }
23319
23320 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23321 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23322 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23323 an obconcat, otherwise allocate storage for the result. The CU argument is
23324 used to determine the language and hence, the appropriate separator. */
23325
23326 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23327
23328 static char *
23329 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23330 int physname, struct dwarf2_cu *cu)
23331 {
23332 const char *lead = "";
23333 const char *sep;
23334
23335 if (suffix == NULL || suffix[0] == '\0'
23336 || prefix == NULL || prefix[0] == '\0')
23337 sep = "";
23338 else if (cu->language == language_d)
23339 {
23340 /* For D, the 'main' function could be defined in any module, but it
23341 should never be prefixed. */
23342 if (strcmp (suffix, "D main") == 0)
23343 {
23344 prefix = "";
23345 sep = "";
23346 }
23347 else
23348 sep = ".";
23349 }
23350 else if (cu->language == language_fortran && physname)
23351 {
23352 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23353 DW_AT_MIPS_linkage_name is preferred and used instead. */
23354
23355 lead = "__";
23356 sep = "_MOD_";
23357 }
23358 else
23359 sep = "::";
23360
23361 if (prefix == NULL)
23362 prefix = "";
23363 if (suffix == NULL)
23364 suffix = "";
23365
23366 if (obs == NULL)
23367 {
23368 char *retval
23369 = ((char *)
23370 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23371
23372 strcpy (retval, lead);
23373 strcat (retval, prefix);
23374 strcat (retval, sep);
23375 strcat (retval, suffix);
23376 return retval;
23377 }
23378 else
23379 {
23380 /* We have an obstack. */
23381 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23382 }
23383 }
23384
23385 /* Get name of a die, return NULL if not found. */
23386
23387 static const char *
23388 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23389 struct objfile *objfile)
23390 {
23391 if (name && cu->language == language_cplus)
23392 {
23393 gdb::unique_xmalloc_ptr<char> canon_name
23394 = cp_canonicalize_string (name);
23395
23396 if (canon_name != nullptr)
23397 name = objfile->intern (canon_name.get ());
23398 }
23399
23400 return name;
23401 }
23402
23403 /* Get name of a die, return NULL if not found.
23404 Anonymous namespaces are converted to their magic string. */
23405
23406 static const char *
23407 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23408 {
23409 struct attribute *attr;
23410 struct objfile *objfile = cu->per_objfile->objfile;
23411
23412 attr = dwarf2_attr (die, DW_AT_name, cu);
23413 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23414 if (attr_name == nullptr
23415 && die->tag != DW_TAG_namespace
23416 && die->tag != DW_TAG_class_type
23417 && die->tag != DW_TAG_interface_type
23418 && die->tag != DW_TAG_structure_type
23419 && die->tag != DW_TAG_union_type)
23420 return NULL;
23421
23422 switch (die->tag)
23423 {
23424 case DW_TAG_compile_unit:
23425 case DW_TAG_partial_unit:
23426 /* Compilation units have a DW_AT_name that is a filename, not
23427 a source language identifier. */
23428 case DW_TAG_enumeration_type:
23429 case DW_TAG_enumerator:
23430 /* These tags always have simple identifiers already; no need
23431 to canonicalize them. */
23432 return attr_name;
23433
23434 case DW_TAG_namespace:
23435 if (attr_name != nullptr)
23436 return attr_name;
23437 return CP_ANONYMOUS_NAMESPACE_STR;
23438
23439 case DW_TAG_class_type:
23440 case DW_TAG_interface_type:
23441 case DW_TAG_structure_type:
23442 case DW_TAG_union_type:
23443 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23444 structures or unions. These were of the form "._%d" in GCC 4.1,
23445 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23446 and GCC 4.4. We work around this problem by ignoring these. */
23447 if (attr_name != nullptr
23448 && (startswith (attr_name, "._")
23449 || startswith (attr_name, "<anonymous")))
23450 return NULL;
23451
23452 /* GCC might emit a nameless typedef that has a linkage name. See
23453 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23454 if (!attr || attr_name == NULL)
23455 {
23456 attr = dw2_linkage_name_attr (die, cu);
23457 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23458 if (attr == NULL || attr_name == NULL)
23459 return NULL;
23460
23461 /* Avoid demangling attr_name the second time on a second
23462 call for the same DIE. */
23463 if (!attr->canonical_string_p ())
23464 {
23465 gdb::unique_xmalloc_ptr<char> demangled
23466 (gdb_demangle (attr_name, DMGL_TYPES));
23467 if (demangled == nullptr)
23468 return nullptr;
23469
23470 attr->set_string_canonical (objfile->intern (demangled.get ()));
23471 attr_name = attr->as_string ();
23472 }
23473
23474 /* Strip any leading namespaces/classes, keep only the
23475 base name. DW_AT_name for named DIEs does not
23476 contain the prefixes. */
23477 const char *base = strrchr (attr_name, ':');
23478 if (base && base > attr_name && base[-1] == ':')
23479 return &base[1];
23480 else
23481 return attr_name;
23482 }
23483 break;
23484
23485 default:
23486 break;
23487 }
23488
23489 if (!attr->canonical_string_p ())
23490 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23491 objfile));
23492 return attr->as_string ();
23493 }
23494
23495 /* Return the die that this die in an extension of, or NULL if there
23496 is none. *EXT_CU is the CU containing DIE on input, and the CU
23497 containing the return value on output. */
23498
23499 static struct die_info *
23500 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23501 {
23502 struct attribute *attr;
23503
23504 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23505 if (attr == NULL)
23506 return NULL;
23507
23508 return follow_die_ref (die, attr, ext_cu);
23509 }
23510
23511 static void
23512 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23513 {
23514 unsigned int i;
23515
23516 print_spaces (indent, f);
23517 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23518 dwarf_tag_name (die->tag), die->abbrev,
23519 sect_offset_str (die->sect_off));
23520
23521 if (die->parent != NULL)
23522 {
23523 print_spaces (indent, f);
23524 fprintf_unfiltered (f, " parent at offset: %s\n",
23525 sect_offset_str (die->parent->sect_off));
23526 }
23527
23528 print_spaces (indent, f);
23529 fprintf_unfiltered (f, " has children: %s\n",
23530 dwarf_bool_name (die->child != NULL));
23531
23532 print_spaces (indent, f);
23533 fprintf_unfiltered (f, " attributes:\n");
23534
23535 for (i = 0; i < die->num_attrs; ++i)
23536 {
23537 print_spaces (indent, f);
23538 fprintf_unfiltered (f, " %s (%s) ",
23539 dwarf_attr_name (die->attrs[i].name),
23540 dwarf_form_name (die->attrs[i].form));
23541
23542 switch (die->attrs[i].form)
23543 {
23544 case DW_FORM_addr:
23545 case DW_FORM_addrx:
23546 case DW_FORM_GNU_addr_index:
23547 fprintf_unfiltered (f, "address: ");
23548 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23549 break;
23550 case DW_FORM_block2:
23551 case DW_FORM_block4:
23552 case DW_FORM_block:
23553 case DW_FORM_block1:
23554 fprintf_unfiltered (f, "block: size %s",
23555 pulongest (die->attrs[i].as_block ()->size));
23556 break;
23557 case DW_FORM_exprloc:
23558 fprintf_unfiltered (f, "expression: size %s",
23559 pulongest (die->attrs[i].as_block ()->size));
23560 break;
23561 case DW_FORM_data16:
23562 fprintf_unfiltered (f, "constant of 16 bytes");
23563 break;
23564 case DW_FORM_ref_addr:
23565 fprintf_unfiltered (f, "ref address: ");
23566 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23567 break;
23568 case DW_FORM_GNU_ref_alt:
23569 fprintf_unfiltered (f, "alt ref address: ");
23570 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23571 break;
23572 case DW_FORM_ref1:
23573 case DW_FORM_ref2:
23574 case DW_FORM_ref4:
23575 case DW_FORM_ref8:
23576 case DW_FORM_ref_udata:
23577 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23578 (long) (die->attrs[i].as_unsigned ()));
23579 break;
23580 case DW_FORM_data1:
23581 case DW_FORM_data2:
23582 case DW_FORM_data4:
23583 case DW_FORM_data8:
23584 case DW_FORM_udata:
23585 fprintf_unfiltered (f, "constant: %s",
23586 pulongest (die->attrs[i].as_unsigned ()));
23587 break;
23588 case DW_FORM_sec_offset:
23589 fprintf_unfiltered (f, "section offset: %s",
23590 pulongest (die->attrs[i].as_unsigned ()));
23591 break;
23592 case DW_FORM_ref_sig8:
23593 fprintf_unfiltered (f, "signature: %s",
23594 hex_string (die->attrs[i].as_signature ()));
23595 break;
23596 case DW_FORM_string:
23597 case DW_FORM_strp:
23598 case DW_FORM_line_strp:
23599 case DW_FORM_strx:
23600 case DW_FORM_GNU_str_index:
23601 case DW_FORM_GNU_strp_alt:
23602 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23603 die->attrs[i].as_string ()
23604 ? die->attrs[i].as_string () : "",
23605 die->attrs[i].canonical_string_p () ? "is" : "not");
23606 break;
23607 case DW_FORM_flag:
23608 if (die->attrs[i].as_boolean ())
23609 fprintf_unfiltered (f, "flag: TRUE");
23610 else
23611 fprintf_unfiltered (f, "flag: FALSE");
23612 break;
23613 case DW_FORM_flag_present:
23614 fprintf_unfiltered (f, "flag: TRUE");
23615 break;
23616 case DW_FORM_indirect:
23617 /* The reader will have reduced the indirect form to
23618 the "base form" so this form should not occur. */
23619 fprintf_unfiltered (f,
23620 "unexpected attribute form: DW_FORM_indirect");
23621 break;
23622 case DW_FORM_sdata:
23623 case DW_FORM_implicit_const:
23624 fprintf_unfiltered (f, "constant: %s",
23625 plongest (die->attrs[i].as_signed ()));
23626 break;
23627 default:
23628 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23629 die->attrs[i].form);
23630 break;
23631 }
23632 fprintf_unfiltered (f, "\n");
23633 }
23634 }
23635
23636 static void
23637 dump_die_for_error (struct die_info *die)
23638 {
23639 dump_die_shallow (gdb_stderr, 0, die);
23640 }
23641
23642 static void
23643 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23644 {
23645 int indent = level * 4;
23646
23647 gdb_assert (die != NULL);
23648
23649 if (level >= max_level)
23650 return;
23651
23652 dump_die_shallow (f, indent, die);
23653
23654 if (die->child != NULL)
23655 {
23656 print_spaces (indent, f);
23657 fprintf_unfiltered (f, " Children:");
23658 if (level + 1 < max_level)
23659 {
23660 fprintf_unfiltered (f, "\n");
23661 dump_die_1 (f, level + 1, max_level, die->child);
23662 }
23663 else
23664 {
23665 fprintf_unfiltered (f,
23666 " [not printed, max nesting level reached]\n");
23667 }
23668 }
23669
23670 if (die->sibling != NULL && level > 0)
23671 {
23672 dump_die_1 (f, level, max_level, die->sibling);
23673 }
23674 }
23675
23676 /* This is called from the pdie macro in gdbinit.in.
23677 It's not static so gcc will keep a copy callable from gdb. */
23678
23679 void
23680 dump_die (struct die_info *die, int max_level)
23681 {
23682 dump_die_1 (gdb_stdlog, 0, max_level, die);
23683 }
23684
23685 static void
23686 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23687 {
23688 void **slot;
23689
23690 slot = htab_find_slot_with_hash (cu->die_hash, die,
23691 to_underlying (die->sect_off),
23692 INSERT);
23693
23694 *slot = die;
23695 }
23696
23697 /* Follow reference or signature attribute ATTR of SRC_DIE.
23698 On entry *REF_CU is the CU of SRC_DIE.
23699 On exit *REF_CU is the CU of the result. */
23700
23701 static struct die_info *
23702 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23703 struct dwarf2_cu **ref_cu)
23704 {
23705 struct die_info *die;
23706
23707 if (attr->form_is_ref ())
23708 die = follow_die_ref (src_die, attr, ref_cu);
23709 else if (attr->form == DW_FORM_ref_sig8)
23710 die = follow_die_sig (src_die, attr, ref_cu);
23711 else
23712 {
23713 dump_die_for_error (src_die);
23714 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23715 objfile_name ((*ref_cu)->per_objfile->objfile));
23716 }
23717
23718 return die;
23719 }
23720
23721 /* Follow reference OFFSET.
23722 On entry *REF_CU is the CU of the source die referencing OFFSET.
23723 On exit *REF_CU is the CU of the result.
23724 Returns NULL if OFFSET is invalid. */
23725
23726 static struct die_info *
23727 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23728 struct dwarf2_cu **ref_cu)
23729 {
23730 struct die_info temp_die;
23731 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23732 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23733
23734 gdb_assert (cu->per_cu != NULL);
23735
23736 target_cu = cu;
23737
23738 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23739 "source CU contains target offset: %d",
23740 sect_offset_str (cu->per_cu->sect_off),
23741 sect_offset_str (sect_off),
23742 cu->header.offset_in_cu_p (sect_off));
23743
23744 if (cu->per_cu->is_debug_types)
23745 {
23746 /* .debug_types CUs cannot reference anything outside their CU.
23747 If they need to, they have to reference a signatured type via
23748 DW_FORM_ref_sig8. */
23749 if (!cu->header.offset_in_cu_p (sect_off))
23750 return NULL;
23751 }
23752 else if (offset_in_dwz != cu->per_cu->is_dwz
23753 || !cu->header.offset_in_cu_p (sect_off))
23754 {
23755 struct dwarf2_per_cu_data *per_cu;
23756
23757 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23758 per_objfile);
23759
23760 dwarf_read_debug_printf_v ("target CU offset: %s, "
23761 "target CU DIEs loaded: %d",
23762 sect_offset_str (per_cu->sect_off),
23763 per_objfile->get_cu (per_cu) != nullptr);
23764
23765 /* If necessary, add it to the queue and load its DIEs.
23766
23767 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23768 it doesn't mean they are currently loaded. Since we require them
23769 to be loaded, we must check for ourselves. */
23770 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23771 || per_objfile->get_cu (per_cu) == nullptr)
23772 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23773 false, cu->language);
23774
23775 target_cu = per_objfile->get_cu (per_cu);
23776 gdb_assert (target_cu != nullptr);
23777 }
23778 else if (cu->dies == NULL)
23779 {
23780 /* We're loading full DIEs during partial symbol reading. */
23781 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23782 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23783 language_minimal);
23784 }
23785
23786 *ref_cu = target_cu;
23787 temp_die.sect_off = sect_off;
23788
23789 if (target_cu != cu)
23790 target_cu->ancestor = cu;
23791
23792 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23793 &temp_die,
23794 to_underlying (sect_off));
23795 }
23796
23797 /* Follow reference attribute ATTR of SRC_DIE.
23798 On entry *REF_CU is the CU of SRC_DIE.
23799 On exit *REF_CU is the CU of the result. */
23800
23801 static struct die_info *
23802 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23803 struct dwarf2_cu **ref_cu)
23804 {
23805 sect_offset sect_off = attr->get_ref_die_offset ();
23806 struct dwarf2_cu *cu = *ref_cu;
23807 struct die_info *die;
23808
23809 die = follow_die_offset (sect_off,
23810 (attr->form == DW_FORM_GNU_ref_alt
23811 || cu->per_cu->is_dwz),
23812 ref_cu);
23813 if (!die)
23814 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23815 "at %s [in module %s]"),
23816 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23817 objfile_name (cu->per_objfile->objfile));
23818
23819 return die;
23820 }
23821
23822 /* See read.h. */
23823
23824 struct dwarf2_locexpr_baton
23825 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23826 dwarf2_per_cu_data *per_cu,
23827 dwarf2_per_objfile *per_objfile,
23828 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23829 bool resolve_abstract_p)
23830 {
23831 struct die_info *die;
23832 struct attribute *attr;
23833 struct dwarf2_locexpr_baton retval;
23834 struct objfile *objfile = per_objfile->objfile;
23835
23836 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23837 if (cu == nullptr)
23838 cu = load_cu (per_cu, per_objfile, false);
23839
23840 if (cu == nullptr)
23841 {
23842 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23843 Instead just throw an error, not much else we can do. */
23844 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23845 sect_offset_str (sect_off), objfile_name (objfile));
23846 }
23847
23848 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23849 if (!die)
23850 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23851 sect_offset_str (sect_off), objfile_name (objfile));
23852
23853 attr = dwarf2_attr (die, DW_AT_location, cu);
23854 if (!attr && resolve_abstract_p
23855 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23856 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23857 {
23858 CORE_ADDR pc = get_frame_pc ();
23859 CORE_ADDR baseaddr = objfile->text_section_offset ();
23860 struct gdbarch *gdbarch = objfile->arch ();
23861
23862 for (const auto &cand_off
23863 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23864 {
23865 struct dwarf2_cu *cand_cu = cu;
23866 struct die_info *cand
23867 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23868 if (!cand
23869 || !cand->parent
23870 || cand->parent->tag != DW_TAG_subprogram)
23871 continue;
23872
23873 CORE_ADDR pc_low, pc_high;
23874 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23875 if (pc_low == ((CORE_ADDR) -1))
23876 continue;
23877 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23878 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23879 if (!(pc_low <= pc && pc < pc_high))
23880 continue;
23881
23882 die = cand;
23883 attr = dwarf2_attr (die, DW_AT_location, cu);
23884 break;
23885 }
23886 }
23887
23888 if (!attr)
23889 {
23890 /* DWARF: "If there is no such attribute, then there is no effect.".
23891 DATA is ignored if SIZE is 0. */
23892
23893 retval.data = NULL;
23894 retval.size = 0;
23895 }
23896 else if (attr->form_is_section_offset ())
23897 {
23898 struct dwarf2_loclist_baton loclist_baton;
23899 CORE_ADDR pc = get_frame_pc ();
23900 size_t size;
23901
23902 fill_in_loclist_baton (cu, &loclist_baton, attr);
23903
23904 retval.data = dwarf2_find_location_expression (&loclist_baton,
23905 &size, pc);
23906 retval.size = size;
23907 }
23908 else
23909 {
23910 if (!attr->form_is_block ())
23911 error (_("Dwarf Error: DIE at %s referenced in module %s "
23912 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23913 sect_offset_str (sect_off), objfile_name (objfile));
23914
23915 struct dwarf_block *block = attr->as_block ();
23916 retval.data = block->data;
23917 retval.size = block->size;
23918 }
23919 retval.per_objfile = per_objfile;
23920 retval.per_cu = cu->per_cu;
23921
23922 per_objfile->age_comp_units ();
23923
23924 return retval;
23925 }
23926
23927 /* See read.h. */
23928
23929 struct dwarf2_locexpr_baton
23930 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23931 dwarf2_per_cu_data *per_cu,
23932 dwarf2_per_objfile *per_objfile,
23933 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23934 {
23935 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23936
23937 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23938 get_frame_pc);
23939 }
23940
23941 /* Write a constant of a given type as target-ordered bytes into
23942 OBSTACK. */
23943
23944 static const gdb_byte *
23945 write_constant_as_bytes (struct obstack *obstack,
23946 enum bfd_endian byte_order,
23947 struct type *type,
23948 ULONGEST value,
23949 LONGEST *len)
23950 {
23951 gdb_byte *result;
23952
23953 *len = TYPE_LENGTH (type);
23954 result = (gdb_byte *) obstack_alloc (obstack, *len);
23955 store_unsigned_integer (result, *len, byte_order, value);
23956
23957 return result;
23958 }
23959
23960 /* See read.h. */
23961
23962 const gdb_byte *
23963 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23964 dwarf2_per_cu_data *per_cu,
23965 dwarf2_per_objfile *per_objfile,
23966 obstack *obstack,
23967 LONGEST *len)
23968 {
23969 struct die_info *die;
23970 struct attribute *attr;
23971 const gdb_byte *result = NULL;
23972 struct type *type;
23973 LONGEST value;
23974 enum bfd_endian byte_order;
23975 struct objfile *objfile = per_objfile->objfile;
23976
23977 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23978 if (cu == nullptr)
23979 cu = load_cu (per_cu, per_objfile, false);
23980
23981 if (cu == nullptr)
23982 {
23983 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23984 Instead just throw an error, not much else we can do. */
23985 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23986 sect_offset_str (sect_off), objfile_name (objfile));
23987 }
23988
23989 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23990 if (!die)
23991 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23992 sect_offset_str (sect_off), objfile_name (objfile));
23993
23994 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23995 if (attr == NULL)
23996 return NULL;
23997
23998 byte_order = (bfd_big_endian (objfile->obfd)
23999 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
24000
24001 switch (attr->form)
24002 {
24003 case DW_FORM_addr:
24004 case DW_FORM_addrx:
24005 case DW_FORM_GNU_addr_index:
24006 {
24007 gdb_byte *tem;
24008
24009 *len = cu->header.addr_size;
24010 tem = (gdb_byte *) obstack_alloc (obstack, *len);
24011 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
24012 result = tem;
24013 }
24014 break;
24015 case DW_FORM_string:
24016 case DW_FORM_strp:
24017 case DW_FORM_strx:
24018 case DW_FORM_GNU_str_index:
24019 case DW_FORM_GNU_strp_alt:
24020 /* The string is already allocated on the objfile obstack, point
24021 directly to it. */
24022 {
24023 const char *attr_name = attr->as_string ();
24024 result = (const gdb_byte *) attr_name;
24025 *len = strlen (attr_name);
24026 }
24027 break;
24028 case DW_FORM_block1:
24029 case DW_FORM_block2:
24030 case DW_FORM_block4:
24031 case DW_FORM_block:
24032 case DW_FORM_exprloc:
24033 case DW_FORM_data16:
24034 {
24035 struct dwarf_block *block = attr->as_block ();
24036 result = block->data;
24037 *len = block->size;
24038 }
24039 break;
24040
24041 /* The DW_AT_const_value attributes are supposed to carry the
24042 symbol's value "represented as it would be on the target
24043 architecture." By the time we get here, it's already been
24044 converted to host endianness, so we just need to sign- or
24045 zero-extend it as appropriate. */
24046 case DW_FORM_data1:
24047 type = die_type (die, cu);
24048 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24049 if (result == NULL)
24050 result = write_constant_as_bytes (obstack, byte_order,
24051 type, value, len);
24052 break;
24053 case DW_FORM_data2:
24054 type = die_type (die, cu);
24055 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24056 if (result == NULL)
24057 result = write_constant_as_bytes (obstack, byte_order,
24058 type, value, len);
24059 break;
24060 case DW_FORM_data4:
24061 type = die_type (die, cu);
24062 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24063 if (result == NULL)
24064 result = write_constant_as_bytes (obstack, byte_order,
24065 type, value, len);
24066 break;
24067 case DW_FORM_data8:
24068 type = die_type (die, cu);
24069 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24070 if (result == NULL)
24071 result = write_constant_as_bytes (obstack, byte_order,
24072 type, value, len);
24073 break;
24074
24075 case DW_FORM_sdata:
24076 case DW_FORM_implicit_const:
24077 type = die_type (die, cu);
24078 result = write_constant_as_bytes (obstack, byte_order,
24079 type, attr->as_signed (), len);
24080 break;
24081
24082 case DW_FORM_udata:
24083 type = die_type (die, cu);
24084 result = write_constant_as_bytes (obstack, byte_order,
24085 type, attr->as_unsigned (), len);
24086 break;
24087
24088 default:
24089 complaint (_("unsupported const value attribute form: '%s'"),
24090 dwarf_form_name (attr->form));
24091 break;
24092 }
24093
24094 return result;
24095 }
24096
24097 /* See read.h. */
24098
24099 struct type *
24100 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
24101 dwarf2_per_cu_data *per_cu,
24102 dwarf2_per_objfile *per_objfile)
24103 {
24104 struct die_info *die;
24105
24106 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24107 if (cu == nullptr)
24108 cu = load_cu (per_cu, per_objfile, false);
24109
24110 if (cu == nullptr)
24111 return nullptr;
24112
24113 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24114 if (!die)
24115 return NULL;
24116
24117 return die_type (die, cu);
24118 }
24119
24120 /* See read.h. */
24121
24122 struct type *
24123 dwarf2_get_die_type (cu_offset die_offset,
24124 dwarf2_per_cu_data *per_cu,
24125 dwarf2_per_objfile *per_objfile)
24126 {
24127 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
24128 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
24129 }
24130
24131 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
24132 On entry *REF_CU is the CU of SRC_DIE.
24133 On exit *REF_CU is the CU of the result.
24134 Returns NULL if the referenced DIE isn't found. */
24135
24136 static struct die_info *
24137 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24138 struct dwarf2_cu **ref_cu)
24139 {
24140 struct die_info temp_die;
24141 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
24142 struct die_info *die;
24143 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
24144
24145
24146 /* While it might be nice to assert sig_type->type == NULL here,
24147 we can get here for DW_AT_imported_declaration where we need
24148 the DIE not the type. */
24149
24150 /* If necessary, add it to the queue and load its DIEs.
24151
24152 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24153 it doesn't mean they are currently loaded. Since we require them
24154 to be loaded, we must check for ourselves. */
24155 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
24156 language_minimal)
24157 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
24158 read_signatured_type (sig_type, per_objfile);
24159
24160 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
24161 gdb_assert (sig_cu != NULL);
24162 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24163 temp_die.sect_off = sig_type->type_offset_in_section;
24164 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24165 to_underlying (temp_die.sect_off));
24166 if (die)
24167 {
24168 /* For .gdb_index version 7 keep track of included TUs.
24169 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24170 if (per_objfile->per_bfd->index_table != NULL
24171 && per_objfile->per_bfd->index_table->version <= 7)
24172 {
24173 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24174 }
24175
24176 *ref_cu = sig_cu;
24177 if (sig_cu != cu)
24178 sig_cu->ancestor = cu;
24179
24180 return die;
24181 }
24182
24183 return NULL;
24184 }
24185
24186 /* Follow signatured type referenced by ATTR in SRC_DIE.
24187 On entry *REF_CU is the CU of SRC_DIE.
24188 On exit *REF_CU is the CU of the result.
24189 The result is the DIE of the type.
24190 If the referenced type cannot be found an error is thrown. */
24191
24192 static struct die_info *
24193 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24194 struct dwarf2_cu **ref_cu)
24195 {
24196 ULONGEST signature = attr->as_signature ();
24197 struct signatured_type *sig_type;
24198 struct die_info *die;
24199
24200 gdb_assert (attr->form == DW_FORM_ref_sig8);
24201
24202 sig_type = lookup_signatured_type (*ref_cu, signature);
24203 /* sig_type will be NULL if the signatured type is missing from
24204 the debug info. */
24205 if (sig_type == NULL)
24206 {
24207 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24208 " from DIE at %s [in module %s]"),
24209 hex_string (signature), sect_offset_str (src_die->sect_off),
24210 objfile_name ((*ref_cu)->per_objfile->objfile));
24211 }
24212
24213 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24214 if (die == NULL)
24215 {
24216 dump_die_for_error (src_die);
24217 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24218 " from DIE at %s [in module %s]"),
24219 hex_string (signature), sect_offset_str (src_die->sect_off),
24220 objfile_name ((*ref_cu)->per_objfile->objfile));
24221 }
24222
24223 return die;
24224 }
24225
24226 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24227 reading in and processing the type unit if necessary. */
24228
24229 static struct type *
24230 get_signatured_type (struct die_info *die, ULONGEST signature,
24231 struct dwarf2_cu *cu)
24232 {
24233 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24234 struct signatured_type *sig_type;
24235 struct dwarf2_cu *type_cu;
24236 struct die_info *type_die;
24237 struct type *type;
24238
24239 sig_type = lookup_signatured_type (cu, signature);
24240 /* sig_type will be NULL if the signatured type is missing from
24241 the debug info. */
24242 if (sig_type == NULL)
24243 {
24244 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24245 " from DIE at %s [in module %s]"),
24246 hex_string (signature), sect_offset_str (die->sect_off),
24247 objfile_name (per_objfile->objfile));
24248 return build_error_marker_type (cu, die);
24249 }
24250
24251 /* If we already know the type we're done. */
24252 type = per_objfile->get_type_for_signatured_type (sig_type);
24253 if (type != nullptr)
24254 return type;
24255
24256 type_cu = cu;
24257 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24258 if (type_die != NULL)
24259 {
24260 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24261 is created. This is important, for example, because for c++ classes
24262 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24263 type = read_type_die (type_die, type_cu);
24264 if (type == NULL)
24265 {
24266 complaint (_("Dwarf Error: Cannot build signatured type %s"
24267 " referenced from DIE at %s [in module %s]"),
24268 hex_string (signature), sect_offset_str (die->sect_off),
24269 objfile_name (per_objfile->objfile));
24270 type = build_error_marker_type (cu, die);
24271 }
24272 }
24273 else
24274 {
24275 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24276 " from DIE at %s [in module %s]"),
24277 hex_string (signature), sect_offset_str (die->sect_off),
24278 objfile_name (per_objfile->objfile));
24279 type = build_error_marker_type (cu, die);
24280 }
24281
24282 per_objfile->set_type_for_signatured_type (sig_type, type);
24283
24284 return type;
24285 }
24286
24287 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24288 reading in and processing the type unit if necessary. */
24289
24290 static struct type *
24291 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24292 struct dwarf2_cu *cu) /* ARI: editCase function */
24293 {
24294 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24295 if (attr->form_is_ref ())
24296 {
24297 struct dwarf2_cu *type_cu = cu;
24298 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24299
24300 return read_type_die (type_die, type_cu);
24301 }
24302 else if (attr->form == DW_FORM_ref_sig8)
24303 {
24304 return get_signatured_type (die, attr->as_signature (), cu);
24305 }
24306 else
24307 {
24308 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24309
24310 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24311 " at %s [in module %s]"),
24312 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24313 objfile_name (per_objfile->objfile));
24314 return build_error_marker_type (cu, die);
24315 }
24316 }
24317
24318 /* Load the DIEs associated with type unit PER_CU into memory. */
24319
24320 static void
24321 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24322 dwarf2_per_objfile *per_objfile)
24323 {
24324 struct signatured_type *sig_type;
24325
24326 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24327 gdb_assert (! per_cu->type_unit_group_p ());
24328
24329 /* We have the per_cu, but we need the signatured_type.
24330 Fortunately this is an easy translation. */
24331 gdb_assert (per_cu->is_debug_types);
24332 sig_type = (struct signatured_type *) per_cu;
24333
24334 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24335
24336 read_signatured_type (sig_type, per_objfile);
24337
24338 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24339 }
24340
24341 /* Read in a signatured type and build its CU and DIEs.
24342 If the type is a stub for the real type in a DWO file,
24343 read in the real type from the DWO file as well. */
24344
24345 static void
24346 read_signatured_type (signatured_type *sig_type,
24347 dwarf2_per_objfile *per_objfile)
24348 {
24349 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24350
24351 gdb_assert (per_cu->is_debug_types);
24352 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24353
24354 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24355
24356 if (!reader.dummy_p)
24357 {
24358 struct dwarf2_cu *cu = reader.cu;
24359 const gdb_byte *info_ptr = reader.info_ptr;
24360
24361 gdb_assert (cu->die_hash == NULL);
24362 cu->die_hash =
24363 htab_create_alloc_ex (cu->header.length / 12,
24364 die_hash,
24365 die_eq,
24366 NULL,
24367 &cu->comp_unit_obstack,
24368 hashtab_obstack_allocate,
24369 dummy_obstack_deallocate);
24370
24371 if (reader.comp_unit_die->has_children)
24372 reader.comp_unit_die->child
24373 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24374 reader.comp_unit_die);
24375 cu->dies = reader.comp_unit_die;
24376 /* comp_unit_die is not stored in die_hash, no need. */
24377
24378 /* We try not to read any attributes in this function, because
24379 not all CUs needed for references have been loaded yet, and
24380 symbol table processing isn't initialized. But we have to
24381 set the CU language, or we won't be able to build types
24382 correctly. Similarly, if we do not read the producer, we can
24383 not apply producer-specific interpretation. */
24384 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24385
24386 reader.keep ();
24387 }
24388
24389 sig_type->per_cu.tu_read = 1;
24390 }
24391
24392 /* Decode simple location descriptions.
24393 Given a pointer to a dwarf block that defines a location, compute
24394 the location and return the value. If COMPUTED is non-null, it is
24395 set to true to indicate that decoding was successful, and false
24396 otherwise. If COMPUTED is null, then this function may emit a
24397 complaint. */
24398
24399 static CORE_ADDR
24400 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24401 {
24402 struct objfile *objfile = cu->per_objfile->objfile;
24403 size_t i;
24404 size_t size = blk->size;
24405 const gdb_byte *data = blk->data;
24406 CORE_ADDR stack[64];
24407 int stacki;
24408 unsigned int bytes_read, unsnd;
24409 gdb_byte op;
24410
24411 if (computed != nullptr)
24412 *computed = false;
24413
24414 i = 0;
24415 stacki = 0;
24416 stack[stacki] = 0;
24417 stack[++stacki] = 0;
24418
24419 while (i < size)
24420 {
24421 op = data[i++];
24422 switch (op)
24423 {
24424 case DW_OP_lit0:
24425 case DW_OP_lit1:
24426 case DW_OP_lit2:
24427 case DW_OP_lit3:
24428 case DW_OP_lit4:
24429 case DW_OP_lit5:
24430 case DW_OP_lit6:
24431 case DW_OP_lit7:
24432 case DW_OP_lit8:
24433 case DW_OP_lit9:
24434 case DW_OP_lit10:
24435 case DW_OP_lit11:
24436 case DW_OP_lit12:
24437 case DW_OP_lit13:
24438 case DW_OP_lit14:
24439 case DW_OP_lit15:
24440 case DW_OP_lit16:
24441 case DW_OP_lit17:
24442 case DW_OP_lit18:
24443 case DW_OP_lit19:
24444 case DW_OP_lit20:
24445 case DW_OP_lit21:
24446 case DW_OP_lit22:
24447 case DW_OP_lit23:
24448 case DW_OP_lit24:
24449 case DW_OP_lit25:
24450 case DW_OP_lit26:
24451 case DW_OP_lit27:
24452 case DW_OP_lit28:
24453 case DW_OP_lit29:
24454 case DW_OP_lit30:
24455 case DW_OP_lit31:
24456 stack[++stacki] = op - DW_OP_lit0;
24457 break;
24458
24459 case DW_OP_reg0:
24460 case DW_OP_reg1:
24461 case DW_OP_reg2:
24462 case DW_OP_reg3:
24463 case DW_OP_reg4:
24464 case DW_OP_reg5:
24465 case DW_OP_reg6:
24466 case DW_OP_reg7:
24467 case DW_OP_reg8:
24468 case DW_OP_reg9:
24469 case DW_OP_reg10:
24470 case DW_OP_reg11:
24471 case DW_OP_reg12:
24472 case DW_OP_reg13:
24473 case DW_OP_reg14:
24474 case DW_OP_reg15:
24475 case DW_OP_reg16:
24476 case DW_OP_reg17:
24477 case DW_OP_reg18:
24478 case DW_OP_reg19:
24479 case DW_OP_reg20:
24480 case DW_OP_reg21:
24481 case DW_OP_reg22:
24482 case DW_OP_reg23:
24483 case DW_OP_reg24:
24484 case DW_OP_reg25:
24485 case DW_OP_reg26:
24486 case DW_OP_reg27:
24487 case DW_OP_reg28:
24488 case DW_OP_reg29:
24489 case DW_OP_reg30:
24490 case DW_OP_reg31:
24491 stack[++stacki] = op - DW_OP_reg0;
24492 if (i < size)
24493 {
24494 if (computed == nullptr)
24495 dwarf2_complex_location_expr_complaint ();
24496 else
24497 return 0;
24498 }
24499 break;
24500
24501 case DW_OP_regx:
24502 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24503 i += bytes_read;
24504 stack[++stacki] = unsnd;
24505 if (i < size)
24506 {
24507 if (computed == nullptr)
24508 dwarf2_complex_location_expr_complaint ();
24509 else
24510 return 0;
24511 }
24512 break;
24513
24514 case DW_OP_addr:
24515 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24516 &bytes_read);
24517 i += bytes_read;
24518 break;
24519
24520 case DW_OP_const1u:
24521 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24522 i += 1;
24523 break;
24524
24525 case DW_OP_const1s:
24526 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24527 i += 1;
24528 break;
24529
24530 case DW_OP_const2u:
24531 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24532 i += 2;
24533 break;
24534
24535 case DW_OP_const2s:
24536 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24537 i += 2;
24538 break;
24539
24540 case DW_OP_const4u:
24541 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24542 i += 4;
24543 break;
24544
24545 case DW_OP_const4s:
24546 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24547 i += 4;
24548 break;
24549
24550 case DW_OP_const8u:
24551 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24552 i += 8;
24553 break;
24554
24555 case DW_OP_constu:
24556 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24557 &bytes_read);
24558 i += bytes_read;
24559 break;
24560
24561 case DW_OP_consts:
24562 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24563 i += bytes_read;
24564 break;
24565
24566 case DW_OP_dup:
24567 stack[stacki + 1] = stack[stacki];
24568 stacki++;
24569 break;
24570
24571 case DW_OP_plus:
24572 stack[stacki - 1] += stack[stacki];
24573 stacki--;
24574 break;
24575
24576 case DW_OP_plus_uconst:
24577 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24578 &bytes_read);
24579 i += bytes_read;
24580 break;
24581
24582 case DW_OP_minus:
24583 stack[stacki - 1] -= stack[stacki];
24584 stacki--;
24585 break;
24586
24587 case DW_OP_deref:
24588 /* If we're not the last op, then we definitely can't encode
24589 this using GDB's address_class enum. This is valid for partial
24590 global symbols, although the variable's address will be bogus
24591 in the psymtab. */
24592 if (i < size)
24593 {
24594 if (computed == nullptr)
24595 dwarf2_complex_location_expr_complaint ();
24596 else
24597 return 0;
24598 }
24599 break;
24600
24601 case DW_OP_GNU_push_tls_address:
24602 case DW_OP_form_tls_address:
24603 /* The top of the stack has the offset from the beginning
24604 of the thread control block at which the variable is located. */
24605 /* Nothing should follow this operator, so the top of stack would
24606 be returned. */
24607 /* This is valid for partial global symbols, but the variable's
24608 address will be bogus in the psymtab. Make it always at least
24609 non-zero to not look as a variable garbage collected by linker
24610 which have DW_OP_addr 0. */
24611 if (i < size)
24612 {
24613 if (computed == nullptr)
24614 dwarf2_complex_location_expr_complaint ();
24615 else
24616 return 0;
24617 }
24618 stack[stacki]++;
24619 break;
24620
24621 case DW_OP_GNU_uninit:
24622 if (computed != nullptr)
24623 return 0;
24624 break;
24625
24626 case DW_OP_addrx:
24627 case DW_OP_GNU_addr_index:
24628 case DW_OP_GNU_const_index:
24629 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24630 &bytes_read);
24631 i += bytes_read;
24632 break;
24633
24634 default:
24635 if (computed == nullptr)
24636 {
24637 const char *name = get_DW_OP_name (op);
24638
24639 if (name)
24640 complaint (_("unsupported stack op: '%s'"),
24641 name);
24642 else
24643 complaint (_("unsupported stack op: '%02x'"),
24644 op);
24645 }
24646
24647 return (stack[stacki]);
24648 }
24649
24650 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24651 outside of the allocated space. Also enforce minimum>0. */
24652 if (stacki >= ARRAY_SIZE (stack) - 1)
24653 {
24654 if (computed == nullptr)
24655 complaint (_("location description stack overflow"));
24656 return 0;
24657 }
24658
24659 if (stacki <= 0)
24660 {
24661 if (computed == nullptr)
24662 complaint (_("location description stack underflow"));
24663 return 0;
24664 }
24665 }
24666
24667 if (computed != nullptr)
24668 *computed = true;
24669 return (stack[stacki]);
24670 }
24671
24672 /* memory allocation interface */
24673
24674 static struct dwarf_block *
24675 dwarf_alloc_block (struct dwarf2_cu *cu)
24676 {
24677 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24678 }
24679
24680 static struct die_info *
24681 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24682 {
24683 struct die_info *die;
24684 size_t size = sizeof (struct die_info);
24685
24686 if (num_attrs > 1)
24687 size += (num_attrs - 1) * sizeof (struct attribute);
24688
24689 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24690 memset (die, 0, sizeof (struct die_info));
24691 return (die);
24692 }
24693
24694 \f
24695
24696 /* Macro support. */
24697
24698 /* An overload of dwarf_decode_macros that finds the correct section
24699 and ensures it is read in before calling the other overload. */
24700
24701 static void
24702 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24703 int section_is_gnu)
24704 {
24705 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24706 struct objfile *objfile = per_objfile->objfile;
24707 const struct line_header *lh = cu->line_header;
24708 unsigned int offset_size = cu->header.offset_size;
24709 struct dwarf2_section_info *section;
24710 const char *section_name;
24711
24712 if (cu->dwo_unit != nullptr)
24713 {
24714 if (section_is_gnu)
24715 {
24716 section = &cu->dwo_unit->dwo_file->sections.macro;
24717 section_name = ".debug_macro.dwo";
24718 }
24719 else
24720 {
24721 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24722 section_name = ".debug_macinfo.dwo";
24723 }
24724 }
24725 else
24726 {
24727 if (section_is_gnu)
24728 {
24729 section = &per_objfile->per_bfd->macro;
24730 section_name = ".debug_macro";
24731 }
24732 else
24733 {
24734 section = &per_objfile->per_bfd->macinfo;
24735 section_name = ".debug_macinfo";
24736 }
24737 }
24738
24739 section->read (objfile);
24740 if (section->buffer == nullptr)
24741 {
24742 complaint (_("missing %s section"), section_name);
24743 return;
24744 }
24745
24746 buildsym_compunit *builder = cu->get_builder ();
24747
24748 struct dwarf2_section_info *str_offsets_section;
24749 struct dwarf2_section_info *str_section;
24750 ULONGEST str_offsets_base;
24751
24752 if (cu->dwo_unit != nullptr)
24753 {
24754 str_offsets_section = &cu->dwo_unit->dwo_file
24755 ->sections.str_offsets;
24756 str_section = &cu->dwo_unit->dwo_file->sections.str;
24757 str_offsets_base = cu->header.addr_size;
24758 }
24759 else
24760 {
24761 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24762 str_section = &per_objfile->per_bfd->str;
24763 str_offsets_base = *cu->str_offsets_base;
24764 }
24765
24766 dwarf_decode_macros (per_objfile, builder, section, lh,
24767 offset_size, offset, str_section, str_offsets_section,
24768 str_offsets_base, section_is_gnu);
24769 }
24770
24771 /* Return the .debug_loc section to use for CU.
24772 For DWO files use .debug_loc.dwo. */
24773
24774 static struct dwarf2_section_info *
24775 cu_debug_loc_section (struct dwarf2_cu *cu)
24776 {
24777 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24778
24779 if (cu->dwo_unit)
24780 {
24781 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24782
24783 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24784 }
24785 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24786 : &per_objfile->per_bfd->loc);
24787 }
24788
24789 /* Return the .debug_rnglists section to use for CU. */
24790 static struct dwarf2_section_info *
24791 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24792 {
24793 if (cu->header.version < 5)
24794 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24795 cu->header.version);
24796 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24797
24798 /* Make sure we read the .debug_rnglists section from the file that
24799 contains the DW_AT_ranges attribute we are reading. Normally that
24800 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24801 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24802 program. */
24803 if (cu->dwo_unit != nullptr
24804 && tag != DW_TAG_compile_unit
24805 && tag != DW_TAG_skeleton_unit)
24806 {
24807 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24808
24809 if (sections->rnglists.size > 0)
24810 return &sections->rnglists;
24811 else
24812 error (_(".debug_rnglists section is missing from .dwo file."));
24813 }
24814 return &dwarf2_per_objfile->per_bfd->rnglists;
24815 }
24816
24817 /* A helper function that fills in a dwarf2_loclist_baton. */
24818
24819 static void
24820 fill_in_loclist_baton (struct dwarf2_cu *cu,
24821 struct dwarf2_loclist_baton *baton,
24822 const struct attribute *attr)
24823 {
24824 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24825 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24826
24827 section->read (per_objfile->objfile);
24828
24829 baton->per_objfile = per_objfile;
24830 baton->per_cu = cu->per_cu;
24831 gdb_assert (baton->per_cu);
24832 /* We don't know how long the location list is, but make sure we
24833 don't run off the edge of the section. */
24834 baton->size = section->size - attr->as_unsigned ();
24835 baton->data = section->buffer + attr->as_unsigned ();
24836 if (cu->base_address.has_value ())
24837 baton->base_address = *cu->base_address;
24838 else
24839 baton->base_address = 0;
24840 baton->from_dwo = cu->dwo_unit != NULL;
24841 }
24842
24843 static void
24844 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24845 struct dwarf2_cu *cu, int is_block)
24846 {
24847 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24848 struct objfile *objfile = per_objfile->objfile;
24849 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24850
24851 if (attr->form_is_section_offset ()
24852 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24853 the section. If so, fall through to the complaint in the
24854 other branch. */
24855 && attr->as_unsigned () < section->get_size (objfile))
24856 {
24857 struct dwarf2_loclist_baton *baton;
24858
24859 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24860
24861 fill_in_loclist_baton (cu, baton, attr);
24862
24863 if (!cu->base_address.has_value ())
24864 complaint (_("Location list used without "
24865 "specifying the CU base address."));
24866
24867 SYMBOL_ACLASS_INDEX (sym) = (is_block
24868 ? dwarf2_loclist_block_index
24869 : dwarf2_loclist_index);
24870 SYMBOL_LOCATION_BATON (sym) = baton;
24871 }
24872 else
24873 {
24874 struct dwarf2_locexpr_baton *baton;
24875
24876 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24877 baton->per_objfile = per_objfile;
24878 baton->per_cu = cu->per_cu;
24879 gdb_assert (baton->per_cu);
24880
24881 if (attr->form_is_block ())
24882 {
24883 /* Note that we're just copying the block's data pointer
24884 here, not the actual data. We're still pointing into the
24885 info_buffer for SYM's objfile; right now we never release
24886 that buffer, but when we do clean up properly this may
24887 need to change. */
24888 struct dwarf_block *block = attr->as_block ();
24889 baton->size = block->size;
24890 baton->data = block->data;
24891 }
24892 else
24893 {
24894 dwarf2_invalid_attrib_class_complaint ("location description",
24895 sym->natural_name ());
24896 baton->size = 0;
24897 }
24898
24899 SYMBOL_ACLASS_INDEX (sym) = (is_block
24900 ? dwarf2_locexpr_block_index
24901 : dwarf2_locexpr_index);
24902 SYMBOL_LOCATION_BATON (sym) = baton;
24903 }
24904 }
24905
24906 /* See read.h. */
24907
24908 const comp_unit_head *
24909 dwarf2_per_cu_data::get_header () const
24910 {
24911 if (!m_header_read_in)
24912 {
24913 const gdb_byte *info_ptr
24914 = this->section->buffer + to_underlying (this->sect_off);
24915
24916 memset (&m_header, 0, sizeof (m_header));
24917
24918 read_comp_unit_head (&m_header, info_ptr, this->section,
24919 rcuh_kind::COMPILE);
24920
24921 m_header_read_in = true;
24922 }
24923
24924 return &m_header;
24925 }
24926
24927 /* See read.h. */
24928
24929 int
24930 dwarf2_per_cu_data::addr_size () const
24931 {
24932 return this->get_header ()->addr_size;
24933 }
24934
24935 /* See read.h. */
24936
24937 int
24938 dwarf2_per_cu_data::offset_size () const
24939 {
24940 return this->get_header ()->offset_size;
24941 }
24942
24943 /* See read.h. */
24944
24945 int
24946 dwarf2_per_cu_data::ref_addr_size () const
24947 {
24948 const comp_unit_head *header = this->get_header ();
24949
24950 if (header->version == 2)
24951 return header->addr_size;
24952 else
24953 return header->offset_size;
24954 }
24955
24956 /* See read.h. */
24957
24958 struct type *
24959 dwarf2_cu::addr_type () const
24960 {
24961 struct objfile *objfile = this->per_objfile->objfile;
24962 struct type *void_type = objfile_type (objfile)->builtin_void;
24963 struct type *addr_type = lookup_pointer_type (void_type);
24964 int addr_size = this->per_cu->addr_size ();
24965
24966 if (TYPE_LENGTH (addr_type) == addr_size)
24967 return addr_type;
24968
24969 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24970 return addr_type;
24971 }
24972
24973 /* A helper function for dwarf2_find_containing_comp_unit that returns
24974 the index of the result, and that searches a vector. It will
24975 return a result even if the offset in question does not actually
24976 occur in any CU. This is separate so that it can be unit
24977 tested. */
24978
24979 static int
24980 dwarf2_find_containing_comp_unit
24981 (sect_offset sect_off,
24982 unsigned int offset_in_dwz,
24983 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24984 {
24985 int low, high;
24986
24987 low = 0;
24988 high = all_comp_units.size () - 1;
24989 while (high > low)
24990 {
24991 struct dwarf2_per_cu_data *mid_cu;
24992 int mid = low + (high - low) / 2;
24993
24994 mid_cu = all_comp_units[mid];
24995 if (mid_cu->is_dwz > offset_in_dwz
24996 || (mid_cu->is_dwz == offset_in_dwz
24997 && mid_cu->sect_off + mid_cu->length > sect_off))
24998 high = mid;
24999 else
25000 low = mid + 1;
25001 }
25002 gdb_assert (low == high);
25003 return low;
25004 }
25005
25006 /* Locate the .debug_info compilation unit from CU's objfile which contains
25007 the DIE at OFFSET. Raises an error on failure. */
25008
25009 static struct dwarf2_per_cu_data *
25010 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25011 unsigned int offset_in_dwz,
25012 dwarf2_per_objfile *per_objfile)
25013 {
25014 int low = dwarf2_find_containing_comp_unit
25015 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
25016 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
25017
25018 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25019 {
25020 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25021 error (_("Dwarf Error: could not find partial DIE containing "
25022 "offset %s [in module %s]"),
25023 sect_offset_str (sect_off),
25024 bfd_get_filename (per_objfile->objfile->obfd));
25025
25026 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
25027 <= sect_off);
25028 return per_objfile->per_bfd->all_comp_units[low-1];
25029 }
25030 else
25031 {
25032 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
25033 && sect_off >= this_cu->sect_off + this_cu->length)
25034 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25035 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25036 return this_cu;
25037 }
25038 }
25039
25040 #if GDB_SELF_TEST
25041
25042 namespace selftests {
25043 namespace find_containing_comp_unit {
25044
25045 static void
25046 run_test ()
25047 {
25048 struct dwarf2_per_cu_data one {};
25049 struct dwarf2_per_cu_data two {};
25050 struct dwarf2_per_cu_data three {};
25051 struct dwarf2_per_cu_data four {};
25052
25053 one.length = 5;
25054 two.sect_off = sect_offset (one.length);
25055 two.length = 7;
25056
25057 three.length = 5;
25058 three.is_dwz = 1;
25059 four.sect_off = sect_offset (three.length);
25060 four.length = 7;
25061 four.is_dwz = 1;
25062
25063 std::vector<dwarf2_per_cu_data *> units;
25064 units.push_back (&one);
25065 units.push_back (&two);
25066 units.push_back (&three);
25067 units.push_back (&four);
25068
25069 int result;
25070
25071 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25072 SELF_CHECK (units[result] == &one);
25073 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25074 SELF_CHECK (units[result] == &one);
25075 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25076 SELF_CHECK (units[result] == &two);
25077
25078 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25079 SELF_CHECK (units[result] == &three);
25080 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25081 SELF_CHECK (units[result] == &three);
25082 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25083 SELF_CHECK (units[result] == &four);
25084 }
25085
25086 }
25087 }
25088
25089 #endif /* GDB_SELF_TEST */
25090
25091 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
25092
25093 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25094 dwarf2_per_objfile *per_objfile)
25095 : per_cu (per_cu),
25096 per_objfile (per_objfile),
25097 mark (false),
25098 has_loclist (false),
25099 checked_producer (false),
25100 producer_is_gxx_lt_4_6 (false),
25101 producer_is_gcc_lt_4_3 (false),
25102 producer_is_icc (false),
25103 producer_is_icc_lt_14 (false),
25104 producer_is_codewarrior (false),
25105 processing_has_namespace_info (false)
25106 {
25107 }
25108
25109 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25110
25111 static void
25112 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25113 enum language pretend_language)
25114 {
25115 struct attribute *attr;
25116
25117 /* Set the language we're debugging. */
25118 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25119 if (attr != nullptr)
25120 set_cu_language (attr->constant_value (0), cu);
25121 else
25122 {
25123 cu->language = pretend_language;
25124 cu->language_defn = language_def (cu->language);
25125 }
25126
25127 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25128 }
25129
25130 /* See read.h. */
25131
25132 dwarf2_cu *
25133 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
25134 {
25135 auto it = m_dwarf2_cus.find (per_cu);
25136 if (it == m_dwarf2_cus.end ())
25137 return nullptr;
25138
25139 return it->second;
25140 }
25141
25142 /* See read.h. */
25143
25144 void
25145 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25146 {
25147 gdb_assert (this->get_cu (per_cu) == nullptr);
25148
25149 m_dwarf2_cus[per_cu] = cu;
25150 }
25151
25152 /* See read.h. */
25153
25154 void
25155 dwarf2_per_objfile::age_comp_units ()
25156 {
25157 dwarf_read_debug_printf_v ("running");
25158
25159 /* This is not expected to be called in the middle of CU expansion. There is
25160 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25161 loaded in memory. Calling age_comp_units while the queue is in use could
25162 make us free the DIEs for a CU that is in the queue and therefore break
25163 that invariant. */
25164 gdb_assert (!this->per_bfd->queue.has_value ());
25165
25166 /* Start by clearing all marks. */
25167 for (auto pair : m_dwarf2_cus)
25168 pair.second->mark = false;
25169
25170 /* Traverse all CUs, mark them and their dependencies if used recently
25171 enough. */
25172 for (auto pair : m_dwarf2_cus)
25173 {
25174 dwarf2_cu *cu = pair.second;
25175
25176 cu->last_used++;
25177 if (cu->last_used <= dwarf_max_cache_age)
25178 dwarf2_mark (cu);
25179 }
25180
25181 /* Delete all CUs still not marked. */
25182 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
25183 {
25184 dwarf2_cu *cu = it->second;
25185
25186 if (!cu->mark)
25187 {
25188 dwarf_read_debug_printf_v ("deleting old CU %s",
25189 sect_offset_str (cu->per_cu->sect_off));
25190 delete cu;
25191 it = m_dwarf2_cus.erase (it);
25192 }
25193 else
25194 it++;
25195 }
25196 }
25197
25198 /* See read.h. */
25199
25200 void
25201 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25202 {
25203 auto it = m_dwarf2_cus.find (per_cu);
25204 if (it == m_dwarf2_cus.end ())
25205 return;
25206
25207 delete it->second;
25208
25209 m_dwarf2_cus.erase (it);
25210 }
25211
25212 dwarf2_per_objfile::~dwarf2_per_objfile ()
25213 {
25214 remove_all_cus ();
25215 }
25216
25217 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25218 We store these in a hash table separate from the DIEs, and preserve them
25219 when the DIEs are flushed out of cache.
25220
25221 The CU "per_cu" pointer is needed because offset alone is not enough to
25222 uniquely identify the type. A file may have multiple .debug_types sections,
25223 or the type may come from a DWO file. Furthermore, while it's more logical
25224 to use per_cu->section+offset, with Fission the section with the data is in
25225 the DWO file but we don't know that section at the point we need it.
25226 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25227 because we can enter the lookup routine, get_die_type_at_offset, from
25228 outside this file, and thus won't necessarily have PER_CU->cu.
25229 Fortunately, PER_CU is stable for the life of the objfile. */
25230
25231 struct dwarf2_per_cu_offset_and_type
25232 {
25233 const struct dwarf2_per_cu_data *per_cu;
25234 sect_offset sect_off;
25235 struct type *type;
25236 };
25237
25238 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25239
25240 static hashval_t
25241 per_cu_offset_and_type_hash (const void *item)
25242 {
25243 const struct dwarf2_per_cu_offset_and_type *ofs
25244 = (const struct dwarf2_per_cu_offset_and_type *) item;
25245
25246 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25247 }
25248
25249 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25250
25251 static int
25252 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25253 {
25254 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25255 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25256 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25257 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25258
25259 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25260 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25261 }
25262
25263 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25264 table if necessary. For convenience, return TYPE.
25265
25266 The DIEs reading must have careful ordering to:
25267 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25268 reading current DIE.
25269 * Not trying to dereference contents of still incompletely read in types
25270 while reading in other DIEs.
25271 * Enable referencing still incompletely read in types just by a pointer to
25272 the type without accessing its fields.
25273
25274 Therefore caller should follow these rules:
25275 * Try to fetch any prerequisite types we may need to build this DIE type
25276 before building the type and calling set_die_type.
25277 * After building type call set_die_type for current DIE as soon as
25278 possible before fetching more types to complete the current type.
25279 * Make the type as complete as possible before fetching more types. */
25280
25281 static struct type *
25282 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25283 bool skip_data_location)
25284 {
25285 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25286 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25287 struct objfile *objfile = per_objfile->objfile;
25288 struct attribute *attr;
25289 struct dynamic_prop prop;
25290
25291 /* For Ada types, make sure that the gnat-specific data is always
25292 initialized (if not already set). There are a few types where
25293 we should not be doing so, because the type-specific area is
25294 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25295 where the type-specific area is used to store the floatformat).
25296 But this is not a problem, because the gnat-specific information
25297 is actually not needed for these types. */
25298 if (need_gnat_info (cu)
25299 && type->code () != TYPE_CODE_FUNC
25300 && type->code () != TYPE_CODE_FLT
25301 && type->code () != TYPE_CODE_METHODPTR
25302 && type->code () != TYPE_CODE_MEMBERPTR
25303 && type->code () != TYPE_CODE_METHOD
25304 && type->code () != TYPE_CODE_FIXED_POINT
25305 && !HAVE_GNAT_AUX_INFO (type))
25306 INIT_GNAT_SPECIFIC (type);
25307
25308 /* Read DW_AT_allocated and set in type. */
25309 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25310 if (attr != NULL)
25311 {
25312 struct type *prop_type = cu->addr_sized_int_type (false);
25313 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25314 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25315 }
25316
25317 /* Read DW_AT_associated and set in type. */
25318 attr = dwarf2_attr (die, DW_AT_associated, cu);
25319 if (attr != NULL)
25320 {
25321 struct type *prop_type = cu->addr_sized_int_type (false);
25322 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25323 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25324 }
25325
25326 /* Read DW_AT_data_location and set in type. */
25327 if (!skip_data_location)
25328 {
25329 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25330 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25331 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25332 }
25333
25334 if (per_objfile->die_type_hash == NULL)
25335 per_objfile->die_type_hash
25336 = htab_up (htab_create_alloc (127,
25337 per_cu_offset_and_type_hash,
25338 per_cu_offset_and_type_eq,
25339 NULL, xcalloc, xfree));
25340
25341 ofs.per_cu = cu->per_cu;
25342 ofs.sect_off = die->sect_off;
25343 ofs.type = type;
25344 slot = (struct dwarf2_per_cu_offset_and_type **)
25345 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25346 if (*slot)
25347 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25348 sect_offset_str (die->sect_off));
25349 *slot = XOBNEW (&objfile->objfile_obstack,
25350 struct dwarf2_per_cu_offset_and_type);
25351 **slot = ofs;
25352 return type;
25353 }
25354
25355 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25356 or return NULL if the die does not have a saved type. */
25357
25358 static struct type *
25359 get_die_type_at_offset (sect_offset sect_off,
25360 dwarf2_per_cu_data *per_cu,
25361 dwarf2_per_objfile *per_objfile)
25362 {
25363 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25364
25365 if (per_objfile->die_type_hash == NULL)
25366 return NULL;
25367
25368 ofs.per_cu = per_cu;
25369 ofs.sect_off = sect_off;
25370 slot = ((struct dwarf2_per_cu_offset_and_type *)
25371 htab_find (per_objfile->die_type_hash.get (), &ofs));
25372 if (slot)
25373 return slot->type;
25374 else
25375 return NULL;
25376 }
25377
25378 /* Look up the type for DIE in CU in die_type_hash,
25379 or return NULL if DIE does not have a saved type. */
25380
25381 static struct type *
25382 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25383 {
25384 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25385 }
25386
25387 /* Add a dependence relationship from CU to REF_PER_CU. */
25388
25389 static void
25390 dwarf2_add_dependence (struct dwarf2_cu *cu,
25391 struct dwarf2_per_cu_data *ref_per_cu)
25392 {
25393 void **slot;
25394
25395 if (cu->dependencies == NULL)
25396 cu->dependencies
25397 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25398 NULL, &cu->comp_unit_obstack,
25399 hashtab_obstack_allocate,
25400 dummy_obstack_deallocate);
25401
25402 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25403 if (*slot == NULL)
25404 *slot = ref_per_cu;
25405 }
25406
25407 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25408 Set the mark field in every compilation unit in the
25409 cache that we must keep because we are keeping CU.
25410
25411 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25412
25413 static int
25414 dwarf2_mark_helper (void **slot, void *data)
25415 {
25416 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25417 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25418 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25419
25420 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25421 reading of the chain. As such dependencies remain valid it is not much
25422 useful to track and undo them during QUIT cleanups. */
25423 if (cu == nullptr)
25424 return 1;
25425
25426 if (cu->mark)
25427 return 1;
25428
25429 cu->mark = true;
25430
25431 if (cu->dependencies != nullptr)
25432 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25433
25434 return 1;
25435 }
25436
25437 /* Set the mark field in CU and in every other compilation unit in the
25438 cache that we must keep because we are keeping CU. */
25439
25440 static void
25441 dwarf2_mark (struct dwarf2_cu *cu)
25442 {
25443 if (cu->mark)
25444 return;
25445
25446 cu->mark = true;
25447
25448 if (cu->dependencies != nullptr)
25449 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25450 }
25451
25452 /* Trivial hash function for partial_die_info: the hash value of a DIE
25453 is its offset in .debug_info for this objfile. */
25454
25455 static hashval_t
25456 partial_die_hash (const void *item)
25457 {
25458 const struct partial_die_info *part_die
25459 = (const struct partial_die_info *) item;
25460
25461 return to_underlying (part_die->sect_off);
25462 }
25463
25464 /* Trivial comparison function for partial_die_info structures: two DIEs
25465 are equal if they have the same offset. */
25466
25467 static int
25468 partial_die_eq (const void *item_lhs, const void *item_rhs)
25469 {
25470 const struct partial_die_info *part_die_lhs
25471 = (const struct partial_die_info *) item_lhs;
25472 const struct partial_die_info *part_die_rhs
25473 = (const struct partial_die_info *) item_rhs;
25474
25475 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25476 }
25477
25478 struct cmd_list_element *set_dwarf_cmdlist;
25479 struct cmd_list_element *show_dwarf_cmdlist;
25480
25481 static void
25482 show_check_physname (struct ui_file *file, int from_tty,
25483 struct cmd_list_element *c, const char *value)
25484 {
25485 fprintf_filtered (file,
25486 _("Whether to check \"physname\" is %s.\n"),
25487 value);
25488 }
25489
25490 void _initialize_dwarf2_read ();
25491 void
25492 _initialize_dwarf2_read ()
25493 {
25494 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25495 Set DWARF specific variables.\n\
25496 Configure DWARF variables such as the cache size."),
25497 &set_dwarf_cmdlist, "maintenance set dwarf ",
25498 0/*allow-unknown*/, &maintenance_set_cmdlist);
25499
25500 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25501 Show DWARF specific variables.\n\
25502 Show DWARF variables such as the cache size."),
25503 &show_dwarf_cmdlist, "maintenance show dwarf ",
25504 0/*allow-unknown*/, &maintenance_show_cmdlist);
25505
25506 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25507 &dwarf_max_cache_age, _("\
25508 Set the upper bound on the age of cached DWARF compilation units."), _("\
25509 Show the upper bound on the age of cached DWARF compilation units."), _("\
25510 A higher limit means that cached compilation units will be stored\n\
25511 in memory longer, and more total memory will be used. Zero disables\n\
25512 caching, which can slow down startup."),
25513 NULL,
25514 show_dwarf_max_cache_age,
25515 &set_dwarf_cmdlist,
25516 &show_dwarf_cmdlist);
25517
25518 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25519 Set debugging of the DWARF reader."), _("\
25520 Show debugging of the DWARF reader."), _("\
25521 When enabled (non-zero), debugging messages are printed during DWARF\n\
25522 reading and symtab expansion. A value of 1 (one) provides basic\n\
25523 information. A value greater than 1 provides more verbose information."),
25524 NULL,
25525 NULL,
25526 &setdebuglist, &showdebuglist);
25527
25528 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25529 Set debugging of the DWARF DIE reader."), _("\
25530 Show debugging of the DWARF DIE reader."), _("\
25531 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25532 The value is the maximum depth to print."),
25533 NULL,
25534 NULL,
25535 &setdebuglist, &showdebuglist);
25536
25537 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25538 Set debugging of the dwarf line reader."), _("\
25539 Show debugging of the dwarf line reader."), _("\
25540 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25541 A value of 1 (one) provides basic information.\n\
25542 A value greater than 1 provides more verbose information."),
25543 NULL,
25544 NULL,
25545 &setdebuglist, &showdebuglist);
25546
25547 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25548 Set cross-checking of \"physname\" code against demangler."), _("\
25549 Show cross-checking of \"physname\" code against demangler."), _("\
25550 When enabled, GDB's internal \"physname\" code is checked against\n\
25551 the demangler."),
25552 NULL, show_check_physname,
25553 &setdebuglist, &showdebuglist);
25554
25555 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25556 no_class, &use_deprecated_index_sections, _("\
25557 Set whether to use deprecated gdb_index sections."), _("\
25558 Show whether to use deprecated gdb_index sections."), _("\
25559 When enabled, deprecated .gdb_index sections are used anyway.\n\
25560 Normally they are ignored either because of a missing feature or\n\
25561 performance issue.\n\
25562 Warning: This option must be enabled before gdb reads the file."),
25563 NULL,
25564 NULL,
25565 &setlist, &showlist);
25566
25567 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25568 &dwarf2_locexpr_funcs);
25569 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25570 &dwarf2_loclist_funcs);
25571
25572 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25573 &dwarf2_block_frame_base_locexpr_funcs);
25574 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25575 &dwarf2_block_frame_base_loclist_funcs);
25576
25577 #if GDB_SELF_TEST
25578 selftests::register_test ("dw2_expand_symtabs_matching",
25579 selftests::dw2_expand_symtabs_matching::run_test);
25580 selftests::register_test ("dwarf2_find_containing_comp_unit",
25581 selftests::find_containing_comp_unit::run_test);
25582 #endif
25583 }
This page took 0.532497 seconds and 5 git commands to generate.