Allow multiple partial symbol readers per objfile
[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 BFD
1955 doesn't require relocations. */
1956 if (!gdb_bfd_requires_relocations (objfile->obfd))
1957 {
1958 /* See if one has been created for this BFD yet. */
1959 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1960
1961 if (per_bfd == nullptr)
1962 {
1963 /* No, create it now. */
1964 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1965 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1966 }
1967 }
1968 else
1969 {
1970 /* No sharing possible, create one specifically for this objfile. */
1971 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1972 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1973 }
1974
1975 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1976 }
1977
1978 return (!per_objfile->per_bfd->info.is_virtual
1979 && per_objfile->per_bfd->info.s.section != NULL
1980 && !per_objfile->per_bfd->abbrev.is_virtual
1981 && per_objfile->per_bfd->abbrev.s.section != NULL);
1982 }
1983
1984 /* See declaration. */
1985
1986 void
1987 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1988 const dwarf2_debug_sections &names)
1989 {
1990 flagword aflag = bfd_section_flags (sectp);
1991
1992 if ((aflag & SEC_HAS_CONTENTS) == 0)
1993 {
1994 }
1995 else if (elf_section_data (sectp)->this_hdr.sh_size
1996 > bfd_get_file_size (abfd))
1997 {
1998 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1999 warning (_("Discarding section %s which has a section size (%s"
2000 ") larger than the file size [in module %s]"),
2001 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2002 bfd_get_filename (abfd));
2003 }
2004 else if (names.info.matches (sectp->name))
2005 {
2006 this->info.s.section = sectp;
2007 this->info.size = bfd_section_size (sectp);
2008 }
2009 else if (names.abbrev.matches (sectp->name))
2010 {
2011 this->abbrev.s.section = sectp;
2012 this->abbrev.size = bfd_section_size (sectp);
2013 }
2014 else if (names.line.matches (sectp->name))
2015 {
2016 this->line.s.section = sectp;
2017 this->line.size = bfd_section_size (sectp);
2018 }
2019 else if (names.loc.matches (sectp->name))
2020 {
2021 this->loc.s.section = sectp;
2022 this->loc.size = bfd_section_size (sectp);
2023 }
2024 else if (names.loclists.matches (sectp->name))
2025 {
2026 this->loclists.s.section = sectp;
2027 this->loclists.size = bfd_section_size (sectp);
2028 }
2029 else if (names.macinfo.matches (sectp->name))
2030 {
2031 this->macinfo.s.section = sectp;
2032 this->macinfo.size = bfd_section_size (sectp);
2033 }
2034 else if (names.macro.matches (sectp->name))
2035 {
2036 this->macro.s.section = sectp;
2037 this->macro.size = bfd_section_size (sectp);
2038 }
2039 else if (names.str.matches (sectp->name))
2040 {
2041 this->str.s.section = sectp;
2042 this->str.size = bfd_section_size (sectp);
2043 }
2044 else if (names.str_offsets.matches (sectp->name))
2045 {
2046 this->str_offsets.s.section = sectp;
2047 this->str_offsets.size = bfd_section_size (sectp);
2048 }
2049 else if (names.line_str.matches (sectp->name))
2050 {
2051 this->line_str.s.section = sectp;
2052 this->line_str.size = bfd_section_size (sectp);
2053 }
2054 else if (names.addr.matches (sectp->name))
2055 {
2056 this->addr.s.section = sectp;
2057 this->addr.size = bfd_section_size (sectp);
2058 }
2059 else if (names.frame.matches (sectp->name))
2060 {
2061 this->frame.s.section = sectp;
2062 this->frame.size = bfd_section_size (sectp);
2063 }
2064 else if (names.eh_frame.matches (sectp->name))
2065 {
2066 this->eh_frame.s.section = sectp;
2067 this->eh_frame.size = bfd_section_size (sectp);
2068 }
2069 else if (names.ranges.matches (sectp->name))
2070 {
2071 this->ranges.s.section = sectp;
2072 this->ranges.size = bfd_section_size (sectp);
2073 }
2074 else if (names.rnglists.matches (sectp->name))
2075 {
2076 this->rnglists.s.section = sectp;
2077 this->rnglists.size = bfd_section_size (sectp);
2078 }
2079 else if (names.types.matches (sectp->name))
2080 {
2081 struct dwarf2_section_info type_section;
2082
2083 memset (&type_section, 0, sizeof (type_section));
2084 type_section.s.section = sectp;
2085 type_section.size = bfd_section_size (sectp);
2086
2087 this->types.push_back (type_section);
2088 }
2089 else if (names.gdb_index.matches (sectp->name))
2090 {
2091 this->gdb_index.s.section = sectp;
2092 this->gdb_index.size = bfd_section_size (sectp);
2093 }
2094 else if (names.debug_names.matches (sectp->name))
2095 {
2096 this->debug_names.s.section = sectp;
2097 this->debug_names.size = bfd_section_size (sectp);
2098 }
2099 else if (names.debug_aranges.matches (sectp->name))
2100 {
2101 this->debug_aranges.s.section = sectp;
2102 this->debug_aranges.size = bfd_section_size (sectp);
2103 }
2104
2105 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2106 && bfd_section_vma (sectp) == 0)
2107 this->has_section_at_zero = true;
2108 }
2109
2110 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2111 SECTION_NAME. */
2112
2113 void
2114 dwarf2_get_section_info (struct objfile *objfile,
2115 enum dwarf2_section_enum sect,
2116 asection **sectp, const gdb_byte **bufp,
2117 bfd_size_type *sizep)
2118 {
2119 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2120 struct dwarf2_section_info *info;
2121
2122 /* We may see an objfile without any DWARF, in which case we just
2123 return nothing. */
2124 if (per_objfile == NULL)
2125 {
2126 *sectp = NULL;
2127 *bufp = NULL;
2128 *sizep = 0;
2129 return;
2130 }
2131 switch (sect)
2132 {
2133 case DWARF2_DEBUG_FRAME:
2134 info = &per_objfile->per_bfd->frame;
2135 break;
2136 case DWARF2_EH_FRAME:
2137 info = &per_objfile->per_bfd->eh_frame;
2138 break;
2139 default:
2140 gdb_assert_not_reached ("unexpected section");
2141 }
2142
2143 info->read (objfile);
2144
2145 *sectp = info->get_bfd_section ();
2146 *bufp = info->buffer;
2147 *sizep = info->size;
2148 }
2149
2150 \f
2151 /* DWARF quick_symbol_functions support. */
2152
2153 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2154 unique line tables, so we maintain a separate table of all .debug_line
2155 derived entries to support the sharing.
2156 All the quick functions need is the list of file names. We discard the
2157 line_header when we're done and don't need to record it here. */
2158 struct quick_file_names
2159 {
2160 /* The data used to construct the hash key. */
2161 struct stmt_list_hash hash;
2162
2163 /* The number of entries in file_names, real_names. */
2164 unsigned int num_file_names;
2165
2166 /* The file names from the line table, after being run through
2167 file_full_name. */
2168 const char **file_names;
2169
2170 /* The file names from the line table after being run through
2171 gdb_realpath. These are computed lazily. */
2172 const char **real_names;
2173 };
2174
2175 /* When using the index (and thus not using psymtabs), each CU has an
2176 object of this type. This is used to hold information needed by
2177 the various "quick" methods. */
2178 struct dwarf2_per_cu_quick_data
2179 {
2180 /* The file table. This can be NULL if there was no file table
2181 or it's currently not read in.
2182 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2183 struct quick_file_names *file_names;
2184
2185 /* A temporary mark bit used when iterating over all CUs in
2186 expand_symtabs_matching. */
2187 unsigned int mark : 1;
2188
2189 /* True if we've tried to read the file table and found there isn't one.
2190 There will be no point in trying to read it again next time. */
2191 unsigned int no_file_data : 1;
2192 };
2193
2194 struct dwarf2_base_index_functions : public quick_symbol_functions
2195 {
2196 bool has_symbols (struct objfile *objfile) override;
2197
2198 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2199
2200 void forget_cached_source_info (struct objfile *objfile) override;
2201
2202 bool map_symtabs_matching_filename
2203 (struct objfile *objfile, const char *name, const char *real_path,
2204 gdb::function_view<bool (symtab *)> callback) override;
2205
2206 enum language lookup_global_symbol_language (struct objfile *objfile,
2207 const char *name,
2208 domain_enum domain,
2209 bool *symbol_found_p) override
2210 {
2211 *symbol_found_p = false;
2212 return language_unknown;
2213 }
2214
2215 void print_stats (struct objfile *objfile, bool print_bcache) override;
2216
2217 void expand_all_symtabs (struct objfile *objfile) override;
2218
2219 void expand_symtabs_with_fullname (struct objfile *objfile,
2220 const char *fullname) override;
2221
2222 struct compunit_symtab *find_pc_sect_compunit_symtab
2223 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2224 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2225
2226 struct compunit_symtab *find_compunit_symtab_by_address
2227 (struct objfile *objfile, CORE_ADDR address) override
2228 {
2229 return nullptr;
2230 }
2231
2232 void map_symbol_filenames (struct objfile *objfile,
2233 symbol_filename_ftype *fun, void *data,
2234 int need_fullname) override;
2235 };
2236
2237 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2238 {
2239 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2240 block_enum block_index,
2241 const char *name,
2242 domain_enum domain) override;
2243
2244 void dump (struct objfile *objfile) override;
2245
2246 void expand_symtabs_for_function (struct objfile *objfile,
2247 const char *func_name) override;
2248
2249 void map_matching_symbols
2250 (struct objfile *,
2251 const lookup_name_info &lookup_name,
2252 domain_enum domain,
2253 int global,
2254 gdb::function_view<symbol_found_callback_ftype> callback,
2255 symbol_compare_ftype *ordered_compare) override;
2256
2257 void expand_symtabs_matching
2258 (struct objfile *objfile,
2259 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2260 const lookup_name_info *lookup_name,
2261 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2262 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2263 enum search_domain kind) override;
2264 };
2265
2266 struct dwarf2_debug_names_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 quick_symbol_functions_up
2296 make_dwarf_gdb_index ()
2297 {
2298 return quick_symbol_functions_up (new dwarf2_gdb_index);
2299 }
2300
2301 quick_symbol_functions_up
2302 make_dwarf_debug_names ()
2303 {
2304 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2305 }
2306
2307 /* Utility hash function for a stmt_list_hash. */
2308
2309 static hashval_t
2310 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2311 {
2312 hashval_t v = 0;
2313
2314 if (stmt_list_hash->dwo_unit != NULL)
2315 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2316 v += to_underlying (stmt_list_hash->line_sect_off);
2317 return v;
2318 }
2319
2320 /* Utility equality function for a stmt_list_hash. */
2321
2322 static int
2323 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2324 const struct stmt_list_hash *rhs)
2325 {
2326 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2327 return 0;
2328 if (lhs->dwo_unit != NULL
2329 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2330 return 0;
2331
2332 return lhs->line_sect_off == rhs->line_sect_off;
2333 }
2334
2335 /* Hash function for a quick_file_names. */
2336
2337 static hashval_t
2338 hash_file_name_entry (const void *e)
2339 {
2340 const struct quick_file_names *file_data
2341 = (const struct quick_file_names *) e;
2342
2343 return hash_stmt_list_entry (&file_data->hash);
2344 }
2345
2346 /* Equality function for a quick_file_names. */
2347
2348 static int
2349 eq_file_name_entry (const void *a, const void *b)
2350 {
2351 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2352 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2353
2354 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2355 }
2356
2357 /* Delete function for a quick_file_names. */
2358
2359 static void
2360 delete_file_name_entry (void *e)
2361 {
2362 struct quick_file_names *file_data = (struct quick_file_names *) e;
2363 int i;
2364
2365 for (i = 0; i < file_data->num_file_names; ++i)
2366 {
2367 xfree ((void*) file_data->file_names[i]);
2368 if (file_data->real_names)
2369 xfree ((void*) file_data->real_names[i]);
2370 }
2371
2372 /* The space for the struct itself lives on the obstack, so we don't
2373 free it here. */
2374 }
2375
2376 /* Create a quick_file_names hash table. */
2377
2378 static htab_up
2379 create_quick_file_names_table (unsigned int nr_initial_entries)
2380 {
2381 return htab_up (htab_create_alloc (nr_initial_entries,
2382 hash_file_name_entry, eq_file_name_entry,
2383 delete_file_name_entry, xcalloc, xfree));
2384 }
2385
2386 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2387 function is unrelated to symtabs, symtab would have to be created afterwards.
2388 You should call age_cached_comp_units after processing the CU. */
2389
2390 static dwarf2_cu *
2391 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2392 bool skip_partial)
2393 {
2394 if (per_cu->is_debug_types)
2395 load_full_type_unit (per_cu, per_objfile);
2396 else
2397 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2398 skip_partial, language_minimal);
2399
2400 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2401 if (cu == nullptr)
2402 return nullptr; /* Dummy CU. */
2403
2404 dwarf2_find_base_address (cu->dies, cu);
2405
2406 return cu;
2407 }
2408
2409 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2410
2411 static void
2412 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2413 dwarf2_per_objfile *per_objfile, bool skip_partial)
2414 {
2415 /* Skip type_unit_groups, reading the type units they contain
2416 is handled elsewhere. */
2417 if (per_cu->type_unit_group_p ())
2418 return;
2419
2420 {
2421 /* The destructor of dwarf2_queue_guard frees any entries left on
2422 the queue. After this point we're guaranteed to leave this function
2423 with the dwarf queue empty. */
2424 dwarf2_queue_guard q_guard (per_objfile);
2425
2426 if (!per_objfile->symtab_set_p (per_cu))
2427 {
2428 queue_comp_unit (per_cu, per_objfile, language_minimal);
2429 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2430
2431 /* If we just loaded a CU from a DWO, and we're working with an index
2432 that may badly handle TUs, load all the TUs in that DWO as well.
2433 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2434 if (!per_cu->is_debug_types
2435 && cu != NULL
2436 && cu->dwo_unit != NULL
2437 && per_objfile->per_bfd->index_table != NULL
2438 && per_objfile->per_bfd->index_table->version <= 7
2439 /* DWP files aren't supported yet. */
2440 && get_dwp_file (per_objfile) == NULL)
2441 queue_and_load_all_dwo_tus (cu);
2442 }
2443
2444 process_queue (per_objfile);
2445 }
2446
2447 /* Age the cache, releasing compilation units that have not
2448 been used recently. */
2449 per_objfile->age_comp_units ();
2450 }
2451
2452 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2453 the per-objfile for which this symtab is instantiated.
2454
2455 Returns the resulting symbol table. */
2456
2457 static struct compunit_symtab *
2458 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2459 dwarf2_per_objfile *per_objfile,
2460 bool skip_partial)
2461 {
2462 gdb_assert (per_objfile->per_bfd->using_index);
2463
2464 if (!per_objfile->symtab_set_p (per_cu))
2465 {
2466 free_cached_comp_units freer (per_objfile);
2467 scoped_restore decrementer = increment_reading_symtab ();
2468 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2469 process_cu_includes (per_objfile);
2470 }
2471
2472 return per_objfile->get_symtab (per_cu);
2473 }
2474
2475 /* See declaration. */
2476
2477 dwarf2_per_cu_data *
2478 dwarf2_per_bfd::get_cutu (int index)
2479 {
2480 if (index >= this->all_comp_units.size ())
2481 {
2482 index -= this->all_comp_units.size ();
2483 gdb_assert (index < this->all_type_units.size ());
2484 return &this->all_type_units[index]->per_cu;
2485 }
2486
2487 return this->all_comp_units[index];
2488 }
2489
2490 /* See declaration. */
2491
2492 dwarf2_per_cu_data *
2493 dwarf2_per_bfd::get_cu (int index)
2494 {
2495 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2496
2497 return this->all_comp_units[index];
2498 }
2499
2500 /* See declaration. */
2501
2502 signatured_type *
2503 dwarf2_per_bfd::get_tu (int index)
2504 {
2505 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2506
2507 return this->all_type_units[index];
2508 }
2509
2510 /* See read.h. */
2511
2512 dwarf2_per_cu_data *
2513 dwarf2_per_bfd::allocate_per_cu ()
2514 {
2515 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2516 result->per_bfd = this;
2517 result->index = m_num_psymtabs++;
2518 return result;
2519 }
2520
2521 /* See read.h. */
2522
2523 signatured_type *
2524 dwarf2_per_bfd::allocate_signatured_type ()
2525 {
2526 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2527 result->per_cu.per_bfd = this;
2528 result->per_cu.index = m_num_psymtabs++;
2529 return result;
2530 }
2531
2532 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2533 obstack, and constructed with the specified field values. */
2534
2535 static dwarf2_per_cu_data *
2536 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2537 struct dwarf2_section_info *section,
2538 int is_dwz,
2539 sect_offset sect_off, ULONGEST length)
2540 {
2541 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2542 the_cu->sect_off = sect_off;
2543 the_cu->length = length;
2544 the_cu->section = section;
2545 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2546 struct dwarf2_per_cu_quick_data);
2547 the_cu->is_dwz = is_dwz;
2548 return the_cu;
2549 }
2550
2551 /* A helper for create_cus_from_index that handles a given list of
2552 CUs. */
2553
2554 static void
2555 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2556 const gdb_byte *cu_list, offset_type n_elements,
2557 struct dwarf2_section_info *section,
2558 int is_dwz)
2559 {
2560 for (offset_type i = 0; i < n_elements; i += 2)
2561 {
2562 gdb_static_assert (sizeof (ULONGEST) >= 8);
2563
2564 sect_offset sect_off
2565 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2566 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2567 cu_list += 2 * 8;
2568
2569 dwarf2_per_cu_data *per_cu
2570 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2571 length);
2572 per_bfd->all_comp_units.push_back (per_cu);
2573 }
2574 }
2575
2576 /* Read the CU list from the mapped index, and use it to create all
2577 the CU objects for PER_BFD. */
2578
2579 static void
2580 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2581 const gdb_byte *cu_list, offset_type cu_list_elements,
2582 const gdb_byte *dwz_list, offset_type dwz_elements)
2583 {
2584 gdb_assert (per_bfd->all_comp_units.empty ());
2585 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2586
2587 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2588 &per_bfd->info, 0);
2589
2590 if (dwz_elements == 0)
2591 return;
2592
2593 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2594 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2595 &dwz->info, 1);
2596 }
2597
2598 /* Create the signatured type hash table from the index. */
2599
2600 static void
2601 create_signatured_type_table_from_index
2602 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2603 const gdb_byte *bytes, offset_type elements)
2604 {
2605 gdb_assert (per_bfd->all_type_units.empty ());
2606 per_bfd->all_type_units.reserve (elements / 3);
2607
2608 htab_up sig_types_hash = allocate_signatured_type_table ();
2609
2610 for (offset_type i = 0; i < elements; i += 3)
2611 {
2612 struct signatured_type *sig_type;
2613 ULONGEST signature;
2614 void **slot;
2615 cu_offset type_offset_in_tu;
2616
2617 gdb_static_assert (sizeof (ULONGEST) >= 8);
2618 sect_offset sect_off
2619 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2620 type_offset_in_tu
2621 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2622 BFD_ENDIAN_LITTLE);
2623 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2624 bytes += 3 * 8;
2625
2626 sig_type = per_bfd->allocate_signatured_type ();
2627 sig_type->signature = signature;
2628 sig_type->type_offset_in_tu = type_offset_in_tu;
2629 sig_type->per_cu.is_debug_types = 1;
2630 sig_type->per_cu.section = section;
2631 sig_type->per_cu.sect_off = sect_off;
2632 sig_type->per_cu.v.quick
2633 = OBSTACK_ZALLOC (&per_bfd->obstack,
2634 struct dwarf2_per_cu_quick_data);
2635
2636 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2637 *slot = sig_type;
2638
2639 per_bfd->all_type_units.push_back (sig_type);
2640 }
2641
2642 per_bfd->signatured_types = std::move (sig_types_hash);
2643 }
2644
2645 /* Create the signatured type hash table from .debug_names. */
2646
2647 static void
2648 create_signatured_type_table_from_debug_names
2649 (dwarf2_per_objfile *per_objfile,
2650 const mapped_debug_names &map,
2651 struct dwarf2_section_info *section,
2652 struct dwarf2_section_info *abbrev_section)
2653 {
2654 struct objfile *objfile = per_objfile->objfile;
2655
2656 section->read (objfile);
2657 abbrev_section->read (objfile);
2658
2659 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2660 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2661
2662 htab_up sig_types_hash = allocate_signatured_type_table ();
2663
2664 for (uint32_t i = 0; i < map.tu_count; ++i)
2665 {
2666 struct signatured_type *sig_type;
2667 void **slot;
2668
2669 sect_offset sect_off
2670 = (sect_offset) (extract_unsigned_integer
2671 (map.tu_table_reordered + i * map.offset_size,
2672 map.offset_size,
2673 map.dwarf5_byte_order));
2674
2675 comp_unit_head cu_header;
2676 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2677 abbrev_section,
2678 section->buffer + to_underlying (sect_off),
2679 rcuh_kind::TYPE);
2680
2681 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2682 sig_type->signature = cu_header.signature;
2683 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2684 sig_type->per_cu.is_debug_types = 1;
2685 sig_type->per_cu.section = section;
2686 sig_type->per_cu.sect_off = sect_off;
2687 sig_type->per_cu.v.quick
2688 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2689 struct dwarf2_per_cu_quick_data);
2690
2691 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2692 *slot = sig_type;
2693
2694 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2695 }
2696
2697 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2698 }
2699
2700 /* Read the address map data from the mapped index, and use it to
2701 populate the psymtabs_addrmap. */
2702
2703 static void
2704 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2705 struct mapped_index *index)
2706 {
2707 struct objfile *objfile = per_objfile->objfile;
2708 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2709 struct gdbarch *gdbarch = objfile->arch ();
2710 const gdb_byte *iter, *end;
2711 struct addrmap *mutable_map;
2712 CORE_ADDR baseaddr;
2713
2714 auto_obstack temp_obstack;
2715
2716 mutable_map = addrmap_create_mutable (&temp_obstack);
2717
2718 iter = index->address_table.data ();
2719 end = iter + index->address_table.size ();
2720
2721 baseaddr = objfile->text_section_offset ();
2722
2723 while (iter < end)
2724 {
2725 ULONGEST hi, lo, cu_index;
2726 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2727 iter += 8;
2728 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2729 iter += 8;
2730 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2731 iter += 4;
2732
2733 if (lo > hi)
2734 {
2735 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2736 hex_string (lo), hex_string (hi));
2737 continue;
2738 }
2739
2740 if (cu_index >= per_bfd->all_comp_units.size ())
2741 {
2742 complaint (_(".gdb_index address table has invalid CU number %u"),
2743 (unsigned) cu_index);
2744 continue;
2745 }
2746
2747 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2748 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2749 addrmap_set_empty (mutable_map, lo, hi - 1,
2750 per_bfd->get_cu (cu_index));
2751 }
2752
2753 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2754 &per_bfd->obstack);
2755 }
2756
2757 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2758 populate the psymtabs_addrmap. */
2759
2760 static void
2761 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2762 struct dwarf2_section_info *section)
2763 {
2764 struct objfile *objfile = per_objfile->objfile;
2765 bfd *abfd = objfile->obfd;
2766 struct gdbarch *gdbarch = objfile->arch ();
2767 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2768 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2769
2770 auto_obstack temp_obstack;
2771 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2772
2773 std::unordered_map<sect_offset,
2774 dwarf2_per_cu_data *,
2775 gdb::hash_enum<sect_offset>>
2776 debug_info_offset_to_per_cu;
2777 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2778 {
2779 const auto insertpair
2780 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2781 if (!insertpair.second)
2782 {
2783 warning (_("Section .debug_aranges in %s has duplicate "
2784 "debug_info_offset %s, ignoring .debug_aranges."),
2785 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2786 return;
2787 }
2788 }
2789
2790 section->read (objfile);
2791
2792 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2793
2794 const gdb_byte *addr = section->buffer;
2795
2796 while (addr < section->buffer + section->size)
2797 {
2798 const gdb_byte *const entry_addr = addr;
2799 unsigned int bytes_read;
2800
2801 const LONGEST entry_length = read_initial_length (abfd, addr,
2802 &bytes_read);
2803 addr += bytes_read;
2804
2805 const gdb_byte *const entry_end = addr + entry_length;
2806 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2807 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2808 if (addr + entry_length > section->buffer + section->size)
2809 {
2810 warning (_("Section .debug_aranges in %s entry at offset %s "
2811 "length %s exceeds section length %s, "
2812 "ignoring .debug_aranges."),
2813 objfile_name (objfile),
2814 plongest (entry_addr - section->buffer),
2815 plongest (bytes_read + entry_length),
2816 pulongest (section->size));
2817 return;
2818 }
2819
2820 /* The version number. */
2821 const uint16_t version = read_2_bytes (abfd, addr);
2822 addr += 2;
2823 if (version != 2)
2824 {
2825 warning (_("Section .debug_aranges in %s entry at offset %s "
2826 "has unsupported version %d, ignoring .debug_aranges."),
2827 objfile_name (objfile),
2828 plongest (entry_addr - section->buffer), version);
2829 return;
2830 }
2831
2832 const uint64_t debug_info_offset
2833 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2834 addr += offset_size;
2835 const auto per_cu_it
2836 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2837 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2838 {
2839 warning (_("Section .debug_aranges in %s entry at offset %s "
2840 "debug_info_offset %s does not exists, "
2841 "ignoring .debug_aranges."),
2842 objfile_name (objfile),
2843 plongest (entry_addr - section->buffer),
2844 pulongest (debug_info_offset));
2845 return;
2846 }
2847 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2848
2849 const uint8_t address_size = *addr++;
2850 if (address_size < 1 || address_size > 8)
2851 {
2852 warning (_("Section .debug_aranges in %s entry at offset %s "
2853 "address_size %u is invalid, ignoring .debug_aranges."),
2854 objfile_name (objfile),
2855 plongest (entry_addr - section->buffer), address_size);
2856 return;
2857 }
2858
2859 const uint8_t segment_selector_size = *addr++;
2860 if (segment_selector_size != 0)
2861 {
2862 warning (_("Section .debug_aranges in %s entry at offset %s "
2863 "segment_selector_size %u is not supported, "
2864 "ignoring .debug_aranges."),
2865 objfile_name (objfile),
2866 plongest (entry_addr - section->buffer),
2867 segment_selector_size);
2868 return;
2869 }
2870
2871 /* Must pad to an alignment boundary that is twice the address
2872 size. It is undocumented by the DWARF standard but GCC does
2873 use it. */
2874 for (size_t padding = ((-(addr - section->buffer))
2875 & (2 * address_size - 1));
2876 padding > 0; padding--)
2877 if (*addr++ != 0)
2878 {
2879 warning (_("Section .debug_aranges in %s entry at offset %s "
2880 "padding is not zero, ignoring .debug_aranges."),
2881 objfile_name (objfile),
2882 plongest (entry_addr - section->buffer));
2883 return;
2884 }
2885
2886 for (;;)
2887 {
2888 if (addr + 2 * address_size > entry_end)
2889 {
2890 warning (_("Section .debug_aranges in %s entry at offset %s "
2891 "address list is not properly terminated, "
2892 "ignoring .debug_aranges."),
2893 objfile_name (objfile),
2894 plongest (entry_addr - section->buffer));
2895 return;
2896 }
2897 ULONGEST start = extract_unsigned_integer (addr, address_size,
2898 dwarf5_byte_order);
2899 addr += address_size;
2900 ULONGEST length = extract_unsigned_integer (addr, address_size,
2901 dwarf5_byte_order);
2902 addr += address_size;
2903 if (start == 0 && length == 0)
2904 break;
2905 if (start == 0 && !per_bfd->has_section_at_zero)
2906 {
2907 /* Symbol was eliminated due to a COMDAT group. */
2908 continue;
2909 }
2910 ULONGEST end = start + length;
2911 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2912 - baseaddr);
2913 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2914 - baseaddr);
2915 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2916 }
2917 }
2918
2919 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2920 &per_bfd->obstack);
2921 }
2922
2923 /* Find a slot in the mapped index INDEX for the object named NAME.
2924 If NAME is found, set *VEC_OUT to point to the CU vector in the
2925 constant pool and return true. If NAME cannot be found, return
2926 false. */
2927
2928 static bool
2929 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2930 offset_type **vec_out)
2931 {
2932 offset_type hash;
2933 offset_type slot, step;
2934 int (*cmp) (const char *, const char *);
2935
2936 gdb::unique_xmalloc_ptr<char> without_params;
2937 if (current_language->la_language == language_cplus
2938 || current_language->la_language == language_fortran
2939 || current_language->la_language == language_d)
2940 {
2941 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2942 not contain any. */
2943
2944 if (strchr (name, '(') != NULL)
2945 {
2946 without_params = cp_remove_params (name);
2947
2948 if (without_params != NULL)
2949 name = without_params.get ();
2950 }
2951 }
2952
2953 /* Index version 4 did not support case insensitive searches. But the
2954 indices for case insensitive languages are built in lowercase, therefore
2955 simulate our NAME being searched is also lowercased. */
2956 hash = mapped_index_string_hash ((index->version == 4
2957 && case_sensitivity == case_sensitive_off
2958 ? 5 : index->version),
2959 name);
2960
2961 slot = hash & (index->symbol_table.size () - 1);
2962 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2963 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2964
2965 for (;;)
2966 {
2967 const char *str;
2968
2969 const auto &bucket = index->symbol_table[slot];
2970 if (bucket.name == 0 && bucket.vec == 0)
2971 return false;
2972
2973 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2974 if (!cmp (name, str))
2975 {
2976 *vec_out = (offset_type *) (index->constant_pool
2977 + MAYBE_SWAP (bucket.vec));
2978 return true;
2979 }
2980
2981 slot = (slot + step) & (index->symbol_table.size () - 1);
2982 }
2983 }
2984
2985 /* A helper function that reads the .gdb_index from BUFFER and fills
2986 in MAP. FILENAME is the name of the file containing the data;
2987 it is used for error reporting. DEPRECATED_OK is true if it is
2988 ok to use deprecated sections.
2989
2990 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2991 out parameters that are filled in with information about the CU and
2992 TU lists in the section.
2993
2994 Returns true if all went well, false otherwise. */
2995
2996 static bool
2997 read_gdb_index_from_buffer (const char *filename,
2998 bool deprecated_ok,
2999 gdb::array_view<const gdb_byte> buffer,
3000 struct mapped_index *map,
3001 const gdb_byte **cu_list,
3002 offset_type *cu_list_elements,
3003 const gdb_byte **types_list,
3004 offset_type *types_list_elements)
3005 {
3006 const gdb_byte *addr = &buffer[0];
3007
3008 /* Version check. */
3009 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3010 /* Versions earlier than 3 emitted every copy of a psymbol. This
3011 causes the index to behave very poorly for certain requests. Version 3
3012 contained incomplete addrmap. So, it seems better to just ignore such
3013 indices. */
3014 if (version < 4)
3015 {
3016 static int warning_printed = 0;
3017 if (!warning_printed)
3018 {
3019 warning (_("Skipping obsolete .gdb_index section in %s."),
3020 filename);
3021 warning_printed = 1;
3022 }
3023 return 0;
3024 }
3025 /* Index version 4 uses a different hash function than index version
3026 5 and later.
3027
3028 Versions earlier than 6 did not emit psymbols for inlined
3029 functions. Using these files will cause GDB not to be able to
3030 set breakpoints on inlined functions by name, so we ignore these
3031 indices unless the user has done
3032 "set use-deprecated-index-sections on". */
3033 if (version < 6 && !deprecated_ok)
3034 {
3035 static int warning_printed = 0;
3036 if (!warning_printed)
3037 {
3038 warning (_("\
3039 Skipping deprecated .gdb_index section in %s.\n\
3040 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3041 to use the section anyway."),
3042 filename);
3043 warning_printed = 1;
3044 }
3045 return 0;
3046 }
3047 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3048 of the TU (for symbols coming from TUs),
3049 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3050 Plus gold-generated indices can have duplicate entries for global symbols,
3051 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3052 These are just performance bugs, and we can't distinguish gdb-generated
3053 indices from gold-generated ones, so issue no warning here. */
3054
3055 /* Indexes with higher version than the one supported by GDB may be no
3056 longer backward compatible. */
3057 if (version > 8)
3058 return 0;
3059
3060 map->version = version;
3061
3062 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3063
3064 int i = 0;
3065 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3066 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3067 / 8);
3068 ++i;
3069
3070 *types_list = addr + MAYBE_SWAP (metadata[i]);
3071 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3072 - MAYBE_SWAP (metadata[i]))
3073 / 8);
3074 ++i;
3075
3076 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3077 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3078 map->address_table
3079 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3080 ++i;
3081
3082 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3083 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3084 map->symbol_table
3085 = gdb::array_view<mapped_index::symbol_table_slot>
3086 ((mapped_index::symbol_table_slot *) symbol_table,
3087 (mapped_index::symbol_table_slot *) symbol_table_end);
3088
3089 ++i;
3090 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3091
3092 return 1;
3093 }
3094
3095 /* Callback types for dwarf2_read_gdb_index. */
3096
3097 typedef gdb::function_view
3098 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3099 get_gdb_index_contents_ftype;
3100 typedef gdb::function_view
3101 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3102 get_gdb_index_contents_dwz_ftype;
3103
3104 /* Read .gdb_index. If everything went ok, initialize the "quick"
3105 elements of all the CUs and return 1. Otherwise, return 0. */
3106
3107 static int
3108 dwarf2_read_gdb_index
3109 (dwarf2_per_objfile *per_objfile,
3110 get_gdb_index_contents_ftype get_gdb_index_contents,
3111 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3112 {
3113 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3114 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3115 struct dwz_file *dwz;
3116 struct objfile *objfile = per_objfile->objfile;
3117 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3118
3119 gdb::array_view<const gdb_byte> main_index_contents
3120 = get_gdb_index_contents (objfile, per_bfd);
3121
3122 if (main_index_contents.empty ())
3123 return 0;
3124
3125 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3126 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3127 use_deprecated_index_sections,
3128 main_index_contents, map.get (), &cu_list,
3129 &cu_list_elements, &types_list,
3130 &types_list_elements))
3131 return 0;
3132
3133 /* Don't use the index if it's empty. */
3134 if (map->symbol_table.empty ())
3135 return 0;
3136
3137 /* If there is a .dwz file, read it so we can get its CU list as
3138 well. */
3139 dwz = dwarf2_get_dwz_file (per_bfd);
3140 if (dwz != NULL)
3141 {
3142 struct mapped_index dwz_map;
3143 const gdb_byte *dwz_types_ignore;
3144 offset_type dwz_types_elements_ignore;
3145
3146 gdb::array_view<const gdb_byte> dwz_index_content
3147 = get_gdb_index_contents_dwz (objfile, dwz);
3148
3149 if (dwz_index_content.empty ())
3150 return 0;
3151
3152 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3153 1, dwz_index_content, &dwz_map,
3154 &dwz_list, &dwz_list_elements,
3155 &dwz_types_ignore,
3156 &dwz_types_elements_ignore))
3157 {
3158 warning (_("could not read '.gdb_index' section from %s; skipping"),
3159 bfd_get_filename (dwz->dwz_bfd.get ()));
3160 return 0;
3161 }
3162 }
3163
3164 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3165 dwz_list_elements);
3166
3167 if (types_list_elements)
3168 {
3169 /* We can only handle a single .debug_types when we have an
3170 index. */
3171 if (per_bfd->types.size () != 1)
3172 return 0;
3173
3174 dwarf2_section_info *section = &per_bfd->types[0];
3175
3176 create_signatured_type_table_from_index (per_bfd, section, types_list,
3177 types_list_elements);
3178 }
3179
3180 create_addrmap_from_index (per_objfile, map.get ());
3181
3182 per_bfd->index_table = std::move (map);
3183 per_bfd->using_index = 1;
3184 per_bfd->quick_file_names_table =
3185 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3186
3187 return 1;
3188 }
3189
3190 /* die_reader_func for dw2_get_file_names. */
3191
3192 static void
3193 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3194 struct die_info *comp_unit_die)
3195 {
3196 struct dwarf2_cu *cu = reader->cu;
3197 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3198 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3199 struct dwarf2_per_cu_data *lh_cu;
3200 struct attribute *attr;
3201 void **slot;
3202 struct quick_file_names *qfn;
3203
3204 gdb_assert (! this_cu->is_debug_types);
3205
3206 /* Our callers never want to match partial units -- instead they
3207 will match the enclosing full CU. */
3208 if (comp_unit_die->tag == DW_TAG_partial_unit)
3209 {
3210 this_cu->v.quick->no_file_data = 1;
3211 return;
3212 }
3213
3214 lh_cu = this_cu;
3215 slot = NULL;
3216
3217 line_header_up lh;
3218 sect_offset line_offset {};
3219
3220 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3221 if (attr != nullptr && attr->form_is_unsigned ())
3222 {
3223 struct quick_file_names find_entry;
3224
3225 line_offset = (sect_offset) attr->as_unsigned ();
3226
3227 /* We may have already read in this line header (TU line header sharing).
3228 If we have we're done. */
3229 find_entry.hash.dwo_unit = cu->dwo_unit;
3230 find_entry.hash.line_sect_off = line_offset;
3231 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3232 &find_entry, INSERT);
3233 if (*slot != NULL)
3234 {
3235 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3236 return;
3237 }
3238
3239 lh = dwarf_decode_line_header (line_offset, cu);
3240 }
3241 if (lh == NULL)
3242 {
3243 lh_cu->v.quick->no_file_data = 1;
3244 return;
3245 }
3246
3247 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3248 qfn->hash.dwo_unit = cu->dwo_unit;
3249 qfn->hash.line_sect_off = line_offset;
3250 gdb_assert (slot != NULL);
3251 *slot = qfn;
3252
3253 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3254
3255 int offset = 0;
3256 if (strcmp (fnd.name, "<unknown>") != 0)
3257 ++offset;
3258
3259 qfn->num_file_names = offset + lh->file_names_size ();
3260 qfn->file_names =
3261 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3262 qfn->num_file_names);
3263 if (offset != 0)
3264 qfn->file_names[0] = xstrdup (fnd.name);
3265 for (int i = 0; i < lh->file_names_size (); ++i)
3266 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3267 fnd.comp_dir).release ();
3268 qfn->real_names = NULL;
3269
3270 lh_cu->v.quick->file_names = qfn;
3271 }
3272
3273 /* A helper for the "quick" functions which attempts to read the line
3274 table for THIS_CU. */
3275
3276 static struct quick_file_names *
3277 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3278 dwarf2_per_objfile *per_objfile)
3279 {
3280 /* This should never be called for TUs. */
3281 gdb_assert (! this_cu->is_debug_types);
3282 /* Nor type unit groups. */
3283 gdb_assert (! this_cu->type_unit_group_p ());
3284
3285 if (this_cu->v.quick->file_names != NULL)
3286 return this_cu->v.quick->file_names;
3287 /* If we know there is no line data, no point in looking again. */
3288 if (this_cu->v.quick->no_file_data)
3289 return NULL;
3290
3291 cutu_reader reader (this_cu, per_objfile);
3292 if (!reader.dummy_p)
3293 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3294
3295 if (this_cu->v.quick->no_file_data)
3296 return NULL;
3297 return this_cu->v.quick->file_names;
3298 }
3299
3300 /* A helper for the "quick" functions which computes and caches the
3301 real path for a given file name from the line table. */
3302
3303 static const char *
3304 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3305 struct quick_file_names *qfn, int index)
3306 {
3307 if (qfn->real_names == NULL)
3308 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3309 qfn->num_file_names, const char *);
3310
3311 if (qfn->real_names[index] == NULL)
3312 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3313
3314 return qfn->real_names[index];
3315 }
3316
3317 struct symtab *
3318 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3319 {
3320 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3321 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3322 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3323
3324 if (cust == NULL)
3325 return NULL;
3326
3327 return compunit_primary_filetab (cust);
3328 }
3329
3330 /* Traversal function for dw2_forget_cached_source_info. */
3331
3332 static int
3333 dw2_free_cached_file_names (void **slot, void *info)
3334 {
3335 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3336
3337 if (file_data->real_names)
3338 {
3339 int i;
3340
3341 for (i = 0; i < file_data->num_file_names; ++i)
3342 {
3343 xfree ((void*) file_data->real_names[i]);
3344 file_data->real_names[i] = NULL;
3345 }
3346 }
3347
3348 return 1;
3349 }
3350
3351 void
3352 dwarf2_base_index_functions::forget_cached_source_info
3353 (struct objfile *objfile)
3354 {
3355 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3356
3357 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3358 dw2_free_cached_file_names, NULL);
3359 }
3360
3361 /* Helper function for dw2_map_symtabs_matching_filename that expands
3362 the symtabs and calls the iterator. */
3363
3364 static int
3365 dw2_map_expand_apply (struct objfile *objfile,
3366 struct dwarf2_per_cu_data *per_cu,
3367 const char *name, const char *real_path,
3368 gdb::function_view<bool (symtab *)> callback)
3369 {
3370 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3371
3372 /* Don't visit already-expanded CUs. */
3373 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3374 if (per_objfile->symtab_set_p (per_cu))
3375 return 0;
3376
3377 /* This may expand more than one symtab, and we want to iterate over
3378 all of them. */
3379 dw2_instantiate_symtab (per_cu, per_objfile, false);
3380
3381 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3382 last_made, callback);
3383 }
3384
3385 /* Implementation of the map_symtabs_matching_filename method. */
3386
3387 bool
3388 dwarf2_base_index_functions::map_symtabs_matching_filename
3389 (struct objfile *objfile, const char *name, const char *real_path,
3390 gdb::function_view<bool (symtab *)> callback)
3391 {
3392 const char *name_basename = lbasename (name);
3393 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3394
3395 /* The rule is CUs specify all the files, including those used by
3396 any TU, so there's no need to scan TUs here. */
3397
3398 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3399 {
3400 /* We only need to look at symtabs not already expanded. */
3401 if (per_objfile->symtab_set_p (per_cu))
3402 continue;
3403
3404 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3405 if (file_data == NULL)
3406 continue;
3407
3408 for (int j = 0; j < file_data->num_file_names; ++j)
3409 {
3410 const char *this_name = file_data->file_names[j];
3411 const char *this_real_name;
3412
3413 if (compare_filenames_for_search (this_name, name))
3414 {
3415 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3416 callback))
3417 return true;
3418 continue;
3419 }
3420
3421 /* Before we invoke realpath, which can get expensive when many
3422 files are involved, do a quick comparison of the basenames. */
3423 if (! basenames_may_differ
3424 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3425 continue;
3426
3427 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3428 if (compare_filenames_for_search (this_real_name, name))
3429 {
3430 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3431 callback))
3432 return true;
3433 continue;
3434 }
3435
3436 if (real_path != NULL)
3437 {
3438 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3439 gdb_assert (IS_ABSOLUTE_PATH (name));
3440 if (this_real_name != NULL
3441 && FILENAME_CMP (real_path, this_real_name) == 0)
3442 {
3443 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3444 callback))
3445 return true;
3446 continue;
3447 }
3448 }
3449 }
3450 }
3451
3452 return false;
3453 }
3454
3455 /* Struct used to manage iterating over all CUs looking for a symbol. */
3456
3457 struct dw2_symtab_iterator
3458 {
3459 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3460 dwarf2_per_objfile *per_objfile;
3461 /* If set, only look for symbols that match that block. Valid values are
3462 GLOBAL_BLOCK and STATIC_BLOCK. */
3463 gdb::optional<block_enum> block_index;
3464 /* The kind of symbol we're looking for. */
3465 domain_enum domain;
3466 /* The list of CUs from the index entry of the symbol,
3467 or NULL if not found. */
3468 offset_type *vec;
3469 /* The next element in VEC to look at. */
3470 int next;
3471 /* The number of elements in VEC, or zero if there is no match. */
3472 int length;
3473 /* Have we seen a global version of the symbol?
3474 If so we can ignore all further global instances.
3475 This is to work around gold/15646, inefficient gold-generated
3476 indices. */
3477 int global_seen;
3478 };
3479
3480 /* Initialize the index symtab iterator ITER, common part. */
3481
3482 static void
3483 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3484 dwarf2_per_objfile *per_objfile,
3485 gdb::optional<block_enum> block_index,
3486 domain_enum domain)
3487 {
3488 iter->per_objfile = per_objfile;
3489 iter->block_index = block_index;
3490 iter->domain = domain;
3491 iter->next = 0;
3492 iter->global_seen = 0;
3493 iter->vec = NULL;
3494 iter->length = 0;
3495 }
3496
3497 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3498
3499 static void
3500 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3501 dwarf2_per_objfile *per_objfile,
3502 gdb::optional<block_enum> block_index,
3503 domain_enum domain,
3504 const char *name)
3505 {
3506 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3507
3508 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3509 /* index is NULL if OBJF_READNOW. */
3510 if (index == NULL)
3511 return;
3512
3513 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3514 iter->length = MAYBE_SWAP (*iter->vec);
3515 }
3516
3517 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3518
3519 static void
3520 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3521 dwarf2_per_objfile *per_objfile,
3522 gdb::optional<block_enum> block_index,
3523 domain_enum domain, offset_type namei)
3524 {
3525 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3526
3527 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3528 /* index is NULL if OBJF_READNOW. */
3529 if (index == NULL)
3530 return;
3531
3532 gdb_assert (!index->symbol_name_slot_invalid (namei));
3533 const auto &bucket = index->symbol_table[namei];
3534
3535 iter->vec = (offset_type *) (index->constant_pool
3536 + MAYBE_SWAP (bucket.vec));
3537 iter->length = MAYBE_SWAP (*iter->vec);
3538 }
3539
3540 /* Return the next matching CU or NULL if there are no more. */
3541
3542 static struct dwarf2_per_cu_data *
3543 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3544 {
3545 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3546
3547 for ( ; iter->next < iter->length; ++iter->next)
3548 {
3549 offset_type cu_index_and_attrs =
3550 MAYBE_SWAP (iter->vec[iter->next + 1]);
3551 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3552 gdb_index_symbol_kind symbol_kind =
3553 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3554 /* Only check the symbol attributes if they're present.
3555 Indices prior to version 7 don't record them,
3556 and indices >= 7 may elide them for certain symbols
3557 (gold does this). */
3558 int attrs_valid =
3559 (per_objfile->per_bfd->index_table->version >= 7
3560 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3561
3562 /* Don't crash on bad data. */
3563 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3564 + per_objfile->per_bfd->all_type_units.size ()))
3565 {
3566 complaint (_(".gdb_index entry has bad CU index"
3567 " [in module %s]"), objfile_name (per_objfile->objfile));
3568 continue;
3569 }
3570
3571 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3572
3573 /* Skip if already read in. */
3574 if (per_objfile->symtab_set_p (per_cu))
3575 continue;
3576
3577 /* Check static vs global. */
3578 if (attrs_valid)
3579 {
3580 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3581
3582 if (iter->block_index.has_value ())
3583 {
3584 bool want_static = *iter->block_index == STATIC_BLOCK;
3585
3586 if (is_static != want_static)
3587 continue;
3588 }
3589
3590 /* Work around gold/15646. */
3591 if (!is_static
3592 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3593 {
3594 if (iter->global_seen)
3595 continue;
3596
3597 iter->global_seen = 1;
3598 }
3599 }
3600
3601 /* Only check the symbol's kind if it has one. */
3602 if (attrs_valid)
3603 {
3604 switch (iter->domain)
3605 {
3606 case VAR_DOMAIN:
3607 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3608 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3609 /* Some types are also in VAR_DOMAIN. */
3610 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3611 continue;
3612 break;
3613 case STRUCT_DOMAIN:
3614 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3615 continue;
3616 break;
3617 case LABEL_DOMAIN:
3618 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3619 continue;
3620 break;
3621 case MODULE_DOMAIN:
3622 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3623 continue;
3624 break;
3625 default:
3626 break;
3627 }
3628 }
3629
3630 ++iter->next;
3631 return per_cu;
3632 }
3633
3634 return NULL;
3635 }
3636
3637 struct compunit_symtab *
3638 dwarf2_gdb_index::lookup_symbol (struct objfile *objfile,
3639 block_enum block_index,
3640 const char *name, domain_enum domain)
3641 {
3642 struct compunit_symtab *stab_best = NULL;
3643 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3644
3645 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3646
3647 struct dw2_symtab_iterator iter;
3648 struct dwarf2_per_cu_data *per_cu;
3649
3650 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3651
3652 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3653 {
3654 struct symbol *sym, *with_opaque = NULL;
3655 struct compunit_symtab *stab
3656 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3657 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3658 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3659
3660 sym = block_find_symbol (block, name, domain,
3661 block_find_non_opaque_type_preferred,
3662 &with_opaque);
3663
3664 /* Some caution must be observed with overloaded functions
3665 and methods, since the index will not contain any overload
3666 information (but NAME might contain it). */
3667
3668 if (sym != NULL
3669 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3670 return stab;
3671 if (with_opaque != NULL
3672 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3673 stab_best = stab;
3674
3675 /* Keep looking through other CUs. */
3676 }
3677
3678 return stab_best;
3679 }
3680
3681 void
3682 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3683 bool print_bcache)
3684 {
3685 if (print_bcache)
3686 return;
3687
3688 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3689 int total = (per_objfile->per_bfd->all_comp_units.size ()
3690 + per_objfile->per_bfd->all_type_units.size ());
3691 int count = 0;
3692
3693 for (int i = 0; i < total; ++i)
3694 {
3695 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3696
3697 if (!per_objfile->symtab_set_p (per_cu))
3698 ++count;
3699 }
3700 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3701 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3702 }
3703
3704 /* This dumps minimal information about the index.
3705 It is called via "mt print objfiles".
3706 One use is to verify .gdb_index has been loaded by the
3707 gdb.dwarf2/gdb-index.exp testcase. */
3708
3709 void
3710 dwarf2_gdb_index::dump (struct objfile *objfile)
3711 {
3712 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3713
3714 gdb_assert (per_objfile->per_bfd->using_index);
3715 printf_filtered (".gdb_index:");
3716 if (per_objfile->per_bfd->index_table != NULL)
3717 {
3718 printf_filtered (" version %d\n",
3719 per_objfile->per_bfd->index_table->version);
3720 }
3721 else
3722 printf_filtered (" faked for \"readnow\"\n");
3723 printf_filtered ("\n");
3724 }
3725
3726 void
3727 dwarf2_gdb_index::expand_symtabs_for_function (struct objfile *objfile,
3728 const char *func_name)
3729 {
3730 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3731
3732 struct dw2_symtab_iterator iter;
3733 struct dwarf2_per_cu_data *per_cu;
3734
3735 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3736
3737 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3738 dw2_instantiate_symtab (per_cu, per_objfile, false);
3739
3740 }
3741
3742 void
3743 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3744 {
3745 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3746 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3747 + per_objfile->per_bfd->all_type_units.size ());
3748
3749 for (int i = 0; i < total_units; ++i)
3750 {
3751 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3752
3753 /* We don't want to directly expand a partial CU, because if we
3754 read it with the wrong language, then assertion failures can
3755 be triggered later on. See PR symtab/23010. So, tell
3756 dw2_instantiate_symtab to skip partial CUs -- any important
3757 partial CU will be read via DW_TAG_imported_unit anyway. */
3758 dw2_instantiate_symtab (per_cu, per_objfile, true);
3759 }
3760 }
3761
3762 void
3763 dwarf2_base_index_functions::expand_symtabs_with_fullname
3764 (struct objfile *objfile, const char *fullname)
3765 {
3766 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3767
3768 /* We don't need to consider type units here.
3769 This is only called for examining code, e.g. expand_line_sal.
3770 There can be an order of magnitude (or more) more type units
3771 than comp units, and we avoid them if we can. */
3772
3773 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3774 {
3775 /* We only need to look at symtabs not already expanded. */
3776 if (per_objfile->symtab_set_p (per_cu))
3777 continue;
3778
3779 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3780 if (file_data == NULL)
3781 continue;
3782
3783 for (int j = 0; j < file_data->num_file_names; ++j)
3784 {
3785 const char *this_fullname = file_data->file_names[j];
3786
3787 if (filename_cmp (this_fullname, fullname) == 0)
3788 {
3789 dw2_instantiate_symtab (per_cu, per_objfile, false);
3790 break;
3791 }
3792 }
3793 }
3794 }
3795
3796 static void
3797 dw2_expand_symtabs_matching_symbol
3798 (mapped_index_base &index,
3799 const lookup_name_info &lookup_name_in,
3800 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3801 enum search_domain kind,
3802 gdb::function_view<bool (offset_type)> match_callback,
3803 dwarf2_per_objfile *per_objfile);
3804
3805 static void
3806 dw2_expand_symtabs_matching_one
3807 (dwarf2_per_cu_data *per_cu,
3808 dwarf2_per_objfile *per_objfile,
3809 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3810 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3811
3812 static void
3813 dw2_map_matching_symbols
3814 (struct objfile *objfile,
3815 const lookup_name_info &name, domain_enum domain,
3816 int global,
3817 gdb::function_view<symbol_found_callback_ftype> callback,
3818 symbol_compare_ftype *ordered_compare)
3819 {
3820 /* Used for Ada. */
3821 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3822
3823 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3824
3825 if (per_objfile->per_bfd->index_table != nullptr)
3826 {
3827 mapped_index &index = *per_objfile->per_bfd->index_table;
3828
3829 const char *match_name = name.ada ().lookup_name ().c_str ();
3830 auto matcher = [&] (const char *symname)
3831 {
3832 if (ordered_compare == nullptr)
3833 return true;
3834 return ordered_compare (symname, match_name) == 0;
3835 };
3836
3837 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3838 [&] (offset_type namei)
3839 {
3840 struct dw2_symtab_iterator iter;
3841 struct dwarf2_per_cu_data *per_cu;
3842
3843 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3844 namei);
3845 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3846 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3847 nullptr);
3848 return true;
3849 }, per_objfile);
3850 }
3851 else
3852 {
3853 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3854 proceed assuming all symtabs have been read in. */
3855 }
3856
3857 for (compunit_symtab *cust : objfile->compunits ())
3858 {
3859 const struct block *block;
3860
3861 if (cust == NULL)
3862 continue;
3863 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3864 if (!iterate_over_symbols_terminated (block, name,
3865 domain, callback))
3866 return;
3867 }
3868 }
3869
3870 void
3871 dwarf2_gdb_index::map_matching_symbols
3872 (struct objfile *objfile,
3873 const lookup_name_info &name, domain_enum domain,
3874 int global,
3875 gdb::function_view<symbol_found_callback_ftype> callback,
3876 symbol_compare_ftype *ordered_compare)
3877 {
3878 dw2_map_matching_symbols (objfile, name, domain, global, callback,
3879 ordered_compare);
3880 }
3881
3882 /* Starting from a search name, return the string that finds the upper
3883 bound of all strings that start with SEARCH_NAME in a sorted name
3884 list. Returns the empty string to indicate that the upper bound is
3885 the end of the list. */
3886
3887 static std::string
3888 make_sort_after_prefix_name (const char *search_name)
3889 {
3890 /* When looking to complete "func", we find the upper bound of all
3891 symbols that start with "func" by looking for where we'd insert
3892 the closest string that would follow "func" in lexicographical
3893 order. Usually, that's "func"-with-last-character-incremented,
3894 i.e. "fund". Mind non-ASCII characters, though. Usually those
3895 will be UTF-8 multi-byte sequences, but we can't be certain.
3896 Especially mind the 0xff character, which is a valid character in
3897 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3898 rule out compilers allowing it in identifiers. Note that
3899 conveniently, strcmp/strcasecmp are specified to compare
3900 characters interpreted as unsigned char. So what we do is treat
3901 the whole string as a base 256 number composed of a sequence of
3902 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3903 to 0, and carries 1 to the following more-significant position.
3904 If the very first character in SEARCH_NAME ends up incremented
3905 and carries/overflows, then the upper bound is the end of the
3906 list. The string after the empty string is also the empty
3907 string.
3908
3909 Some examples of this operation:
3910
3911 SEARCH_NAME => "+1" RESULT
3912
3913 "abc" => "abd"
3914 "ab\xff" => "ac"
3915 "\xff" "a" "\xff" => "\xff" "b"
3916 "\xff" => ""
3917 "\xff\xff" => ""
3918 "" => ""
3919
3920 Then, with these symbols for example:
3921
3922 func
3923 func1
3924 fund
3925
3926 completing "func" looks for symbols between "func" and
3927 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3928 which finds "func" and "func1", but not "fund".
3929
3930 And with:
3931
3932 funcÿ (Latin1 'ÿ' [0xff])
3933 funcÿ1
3934 fund
3935
3936 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3937 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3938
3939 And with:
3940
3941 ÿÿ (Latin1 'ÿ' [0xff])
3942 ÿÿ1
3943
3944 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3945 the end of the list.
3946 */
3947 std::string after = search_name;
3948 while (!after.empty () && (unsigned char) after.back () == 0xff)
3949 after.pop_back ();
3950 if (!after.empty ())
3951 after.back () = (unsigned char) after.back () + 1;
3952 return after;
3953 }
3954
3955 /* See declaration. */
3956
3957 std::pair<std::vector<name_component>::const_iterator,
3958 std::vector<name_component>::const_iterator>
3959 mapped_index_base::find_name_components_bounds
3960 (const lookup_name_info &lookup_name_without_params, language lang,
3961 dwarf2_per_objfile *per_objfile) const
3962 {
3963 auto *name_cmp
3964 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3965
3966 const char *lang_name
3967 = lookup_name_without_params.language_lookup_name (lang);
3968
3969 /* Comparison function object for lower_bound that matches against a
3970 given symbol name. */
3971 auto lookup_compare_lower = [&] (const name_component &elem,
3972 const char *name)
3973 {
3974 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3975 const char *elem_name = elem_qualified + elem.name_offset;
3976 return name_cmp (elem_name, name) < 0;
3977 };
3978
3979 /* Comparison function object for upper_bound that matches against a
3980 given symbol name. */
3981 auto lookup_compare_upper = [&] (const char *name,
3982 const name_component &elem)
3983 {
3984 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3985 const char *elem_name = elem_qualified + elem.name_offset;
3986 return name_cmp (name, elem_name) < 0;
3987 };
3988
3989 auto begin = this->name_components.begin ();
3990 auto end = this->name_components.end ();
3991
3992 /* Find the lower bound. */
3993 auto lower = [&] ()
3994 {
3995 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3996 return begin;
3997 else
3998 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3999 } ();
4000
4001 /* Find the upper bound. */
4002 auto upper = [&] ()
4003 {
4004 if (lookup_name_without_params.completion_mode ())
4005 {
4006 /* In completion mode, we want UPPER to point past all
4007 symbols names that have the same prefix. I.e., with
4008 these symbols, and completing "func":
4009
4010 function << lower bound
4011 function1
4012 other_function << upper bound
4013
4014 We find the upper bound by looking for the insertion
4015 point of "func"-with-last-character-incremented,
4016 i.e. "fund". */
4017 std::string after = make_sort_after_prefix_name (lang_name);
4018 if (after.empty ())
4019 return end;
4020 return std::lower_bound (lower, end, after.c_str (),
4021 lookup_compare_lower);
4022 }
4023 else
4024 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4025 } ();
4026
4027 return {lower, upper};
4028 }
4029
4030 /* See declaration. */
4031
4032 void
4033 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4034 {
4035 if (!this->name_components.empty ())
4036 return;
4037
4038 this->name_components_casing = case_sensitivity;
4039 auto *name_cmp
4040 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4041
4042 /* The code below only knows how to break apart components of C++
4043 symbol names (and other languages that use '::' as
4044 namespace/module separator) and Ada symbol names. */
4045 auto count = this->symbol_name_count ();
4046 for (offset_type idx = 0; idx < count; idx++)
4047 {
4048 if (this->symbol_name_slot_invalid (idx))
4049 continue;
4050
4051 const char *name = this->symbol_name_at (idx, per_objfile);
4052
4053 /* Add each name component to the name component table. */
4054 unsigned int previous_len = 0;
4055
4056 if (strstr (name, "::") != nullptr)
4057 {
4058 for (unsigned int current_len = cp_find_first_component (name);
4059 name[current_len] != '\0';
4060 current_len += cp_find_first_component (name + current_len))
4061 {
4062 gdb_assert (name[current_len] == ':');
4063 this->name_components.push_back ({previous_len, idx});
4064 /* Skip the '::'. */
4065 current_len += 2;
4066 previous_len = current_len;
4067 }
4068 }
4069 else
4070 {
4071 /* Handle the Ada encoded (aka mangled) form here. */
4072 for (const char *iter = strstr (name, "__");
4073 iter != nullptr;
4074 iter = strstr (iter, "__"))
4075 {
4076 this->name_components.push_back ({previous_len, idx});
4077 iter += 2;
4078 previous_len = iter - name;
4079 }
4080 }
4081
4082 this->name_components.push_back ({previous_len, idx});
4083 }
4084
4085 /* Sort name_components elements by name. */
4086 auto name_comp_compare = [&] (const name_component &left,
4087 const name_component &right)
4088 {
4089 const char *left_qualified
4090 = this->symbol_name_at (left.idx, per_objfile);
4091 const char *right_qualified
4092 = this->symbol_name_at (right.idx, per_objfile);
4093
4094 const char *left_name = left_qualified + left.name_offset;
4095 const char *right_name = right_qualified + right.name_offset;
4096
4097 return name_cmp (left_name, right_name) < 0;
4098 };
4099
4100 std::sort (this->name_components.begin (),
4101 this->name_components.end (),
4102 name_comp_compare);
4103 }
4104
4105 /* Helper for dw2_expand_symtabs_matching that works with a
4106 mapped_index_base instead of the containing objfile. This is split
4107 to a separate function in order to be able to unit test the
4108 name_components matching using a mock mapped_index_base. For each
4109 symbol name that matches, calls MATCH_CALLBACK, passing it the
4110 symbol's index in the mapped_index_base symbol table. */
4111
4112 static void
4113 dw2_expand_symtabs_matching_symbol
4114 (mapped_index_base &index,
4115 const lookup_name_info &lookup_name_in,
4116 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4117 enum search_domain kind,
4118 gdb::function_view<bool (offset_type)> match_callback,
4119 dwarf2_per_objfile *per_objfile)
4120 {
4121 lookup_name_info lookup_name_without_params
4122 = lookup_name_in.make_ignore_params ();
4123
4124 /* Build the symbol name component sorted vector, if we haven't
4125 yet. */
4126 index.build_name_components (per_objfile);
4127
4128 /* The same symbol may appear more than once in the range though.
4129 E.g., if we're looking for symbols that complete "w", and we have
4130 a symbol named "w1::w2", we'll find the two name components for
4131 that same symbol in the range. To be sure we only call the
4132 callback once per symbol, we first collect the symbol name
4133 indexes that matched in a temporary vector and ignore
4134 duplicates. */
4135 std::vector<offset_type> matches;
4136
4137 struct name_and_matcher
4138 {
4139 symbol_name_matcher_ftype *matcher;
4140 const char *name;
4141
4142 bool operator== (const name_and_matcher &other) const
4143 {
4144 return matcher == other.matcher && strcmp (name, other.name) == 0;
4145 }
4146 };
4147
4148 /* A vector holding all the different symbol name matchers, for all
4149 languages. */
4150 std::vector<name_and_matcher> matchers;
4151
4152 for (int i = 0; i < nr_languages; i++)
4153 {
4154 enum language lang_e = (enum language) i;
4155
4156 const language_defn *lang = language_def (lang_e);
4157 symbol_name_matcher_ftype *name_matcher
4158 = lang->get_symbol_name_matcher (lookup_name_without_params);
4159
4160 name_and_matcher key {
4161 name_matcher,
4162 lookup_name_without_params.language_lookup_name (lang_e)
4163 };
4164
4165 /* Don't insert the same comparison routine more than once.
4166 Note that we do this linear walk. This is not a problem in
4167 practice because the number of supported languages is
4168 low. */
4169 if (std::find (matchers.begin (), matchers.end (), key)
4170 != matchers.end ())
4171 continue;
4172 matchers.push_back (std::move (key));
4173
4174 auto bounds
4175 = index.find_name_components_bounds (lookup_name_without_params,
4176 lang_e, per_objfile);
4177
4178 /* Now for each symbol name in range, check to see if we have a name
4179 match, and if so, call the MATCH_CALLBACK callback. */
4180
4181 for (; bounds.first != bounds.second; ++bounds.first)
4182 {
4183 const char *qualified
4184 = index.symbol_name_at (bounds.first->idx, per_objfile);
4185
4186 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4187 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4188 continue;
4189
4190 matches.push_back (bounds.first->idx);
4191 }
4192 }
4193
4194 std::sort (matches.begin (), matches.end ());
4195
4196 /* Finally call the callback, once per match. */
4197 ULONGEST prev = -1;
4198 for (offset_type idx : matches)
4199 {
4200 if (prev != idx)
4201 {
4202 if (!match_callback (idx))
4203 break;
4204 prev = idx;
4205 }
4206 }
4207
4208 /* Above we use a type wider than idx's for 'prev', since 0 and
4209 (offset_type)-1 are both possible values. */
4210 static_assert (sizeof (prev) > sizeof (offset_type), "");
4211 }
4212
4213 #if GDB_SELF_TEST
4214
4215 namespace selftests { namespace dw2_expand_symtabs_matching {
4216
4217 /* A mock .gdb_index/.debug_names-like name index table, enough to
4218 exercise dw2_expand_symtabs_matching_symbol, which works with the
4219 mapped_index_base interface. Builds an index from the symbol list
4220 passed as parameter to the constructor. */
4221 class mock_mapped_index : public mapped_index_base
4222 {
4223 public:
4224 mock_mapped_index (gdb::array_view<const char *> symbols)
4225 : m_symbol_table (symbols)
4226 {}
4227
4228 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4229
4230 /* Return the number of names in the symbol table. */
4231 size_t symbol_name_count () const override
4232 {
4233 return m_symbol_table.size ();
4234 }
4235
4236 /* Get the name of the symbol at IDX in the symbol table. */
4237 const char *symbol_name_at
4238 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4239 {
4240 return m_symbol_table[idx];
4241 }
4242
4243 private:
4244 gdb::array_view<const char *> m_symbol_table;
4245 };
4246
4247 /* Convenience function that converts a NULL pointer to a "<null>"
4248 string, to pass to print routines. */
4249
4250 static const char *
4251 string_or_null (const char *str)
4252 {
4253 return str != NULL ? str : "<null>";
4254 }
4255
4256 /* Check if a lookup_name_info built from
4257 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4258 index. EXPECTED_LIST is the list of expected matches, in expected
4259 matching order. If no match expected, then an empty list is
4260 specified. Returns true on success. On failure prints a warning
4261 indicating the file:line that failed, and returns false. */
4262
4263 static bool
4264 check_match (const char *file, int line,
4265 mock_mapped_index &mock_index,
4266 const char *name, symbol_name_match_type match_type,
4267 bool completion_mode,
4268 std::initializer_list<const char *> expected_list,
4269 dwarf2_per_objfile *per_objfile)
4270 {
4271 lookup_name_info lookup_name (name, match_type, completion_mode);
4272
4273 bool matched = true;
4274
4275 auto mismatch = [&] (const char *expected_str,
4276 const char *got)
4277 {
4278 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4279 "expected=\"%s\", got=\"%s\"\n"),
4280 file, line,
4281 (match_type == symbol_name_match_type::FULL
4282 ? "FULL" : "WILD"),
4283 name, string_or_null (expected_str), string_or_null (got));
4284 matched = false;
4285 };
4286
4287 auto expected_it = expected_list.begin ();
4288 auto expected_end = expected_list.end ();
4289
4290 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4291 NULL, ALL_DOMAIN,
4292 [&] (offset_type idx)
4293 {
4294 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4295 const char *expected_str
4296 = expected_it == expected_end ? NULL : *expected_it++;
4297
4298 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4299 mismatch (expected_str, matched_name);
4300 return true;
4301 }, per_objfile);
4302
4303 const char *expected_str
4304 = expected_it == expected_end ? NULL : *expected_it++;
4305 if (expected_str != NULL)
4306 mismatch (expected_str, NULL);
4307
4308 return matched;
4309 }
4310
4311 /* The symbols added to the mock mapped_index for testing (in
4312 canonical form). */
4313 static const char *test_symbols[] = {
4314 "function",
4315 "std::bar",
4316 "std::zfunction",
4317 "std::zfunction2",
4318 "w1::w2",
4319 "ns::foo<char*>",
4320 "ns::foo<int>",
4321 "ns::foo<long>",
4322 "ns2::tmpl<int>::foo2",
4323 "(anonymous namespace)::A::B::C",
4324
4325 /* These are used to check that the increment-last-char in the
4326 matching algorithm for completion doesn't match "t1_fund" when
4327 completing "t1_func". */
4328 "t1_func",
4329 "t1_func1",
4330 "t1_fund",
4331 "t1_fund1",
4332
4333 /* A UTF-8 name with multi-byte sequences to make sure that
4334 cp-name-parser understands this as a single identifier ("função"
4335 is "function" in PT). */
4336 u8"u8função",
4337
4338 /* \377 (0xff) is Latin1 'ÿ'. */
4339 "yfunc\377",
4340
4341 /* \377 (0xff) is Latin1 'ÿ'. */
4342 "\377",
4343 "\377\377123",
4344
4345 /* A name with all sorts of complications. Starts with "z" to make
4346 it easier for the completion tests below. */
4347 #define Z_SYM_NAME \
4348 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4349 "::tuple<(anonymous namespace)::ui*, " \
4350 "std::default_delete<(anonymous namespace)::ui>, void>"
4351
4352 Z_SYM_NAME
4353 };
4354
4355 /* Returns true if the mapped_index_base::find_name_component_bounds
4356 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4357 in completion mode. */
4358
4359 static bool
4360 check_find_bounds_finds (mapped_index_base &index,
4361 const char *search_name,
4362 gdb::array_view<const char *> expected_syms,
4363 dwarf2_per_objfile *per_objfile)
4364 {
4365 lookup_name_info lookup_name (search_name,
4366 symbol_name_match_type::FULL, true);
4367
4368 auto bounds = index.find_name_components_bounds (lookup_name,
4369 language_cplus,
4370 per_objfile);
4371
4372 size_t distance = std::distance (bounds.first, bounds.second);
4373 if (distance != expected_syms.size ())
4374 return false;
4375
4376 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4377 {
4378 auto nc_elem = bounds.first + exp_elem;
4379 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4380 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4381 return false;
4382 }
4383
4384 return true;
4385 }
4386
4387 /* Test the lower-level mapped_index::find_name_component_bounds
4388 method. */
4389
4390 static void
4391 test_mapped_index_find_name_component_bounds ()
4392 {
4393 mock_mapped_index mock_index (test_symbols);
4394
4395 mock_index.build_name_components (NULL /* per_objfile */);
4396
4397 /* Test the lower-level mapped_index::find_name_component_bounds
4398 method in completion mode. */
4399 {
4400 static const char *expected_syms[] = {
4401 "t1_func",
4402 "t1_func1",
4403 };
4404
4405 SELF_CHECK (check_find_bounds_finds
4406 (mock_index, "t1_func", expected_syms,
4407 NULL /* per_objfile */));
4408 }
4409
4410 /* Check that the increment-last-char in the name matching algorithm
4411 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4412 {
4413 static const char *expected_syms1[] = {
4414 "\377",
4415 "\377\377123",
4416 };
4417 SELF_CHECK (check_find_bounds_finds
4418 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4419
4420 static const char *expected_syms2[] = {
4421 "\377\377123",
4422 };
4423 SELF_CHECK (check_find_bounds_finds
4424 (mock_index, "\377\377", expected_syms2,
4425 NULL /* per_objfile */));
4426 }
4427 }
4428
4429 /* Test dw2_expand_symtabs_matching_symbol. */
4430
4431 static void
4432 test_dw2_expand_symtabs_matching_symbol ()
4433 {
4434 mock_mapped_index mock_index (test_symbols);
4435
4436 /* We let all tests run until the end even if some fails, for debug
4437 convenience. */
4438 bool any_mismatch = false;
4439
4440 /* Create the expected symbols list (an initializer_list). Needed
4441 because lists have commas, and we need to pass them to CHECK,
4442 which is a macro. */
4443 #define EXPECT(...) { __VA_ARGS__ }
4444
4445 /* Wrapper for check_match that passes down the current
4446 __FILE__/__LINE__. */
4447 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4448 any_mismatch |= !check_match (__FILE__, __LINE__, \
4449 mock_index, \
4450 NAME, MATCH_TYPE, COMPLETION_MODE, \
4451 EXPECTED_LIST, NULL)
4452
4453 /* Identity checks. */
4454 for (const char *sym : test_symbols)
4455 {
4456 /* Should be able to match all existing symbols. */
4457 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4458 EXPECT (sym));
4459
4460 /* Should be able to match all existing symbols with
4461 parameters. */
4462 std::string with_params = std::string (sym) + "(int)";
4463 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4464 EXPECT (sym));
4465
4466 /* Should be able to match all existing symbols with
4467 parameters and qualifiers. */
4468 with_params = std::string (sym) + " ( int ) const";
4469 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4470 EXPECT (sym));
4471
4472 /* This should really find sym, but cp-name-parser.y doesn't
4473 know about lvalue/rvalue qualifiers yet. */
4474 with_params = std::string (sym) + " ( int ) &&";
4475 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4476 {});
4477 }
4478
4479 /* Check that the name matching algorithm for completion doesn't get
4480 confused with Latin1 'ÿ' / 0xff. */
4481 {
4482 static const char str[] = "\377";
4483 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4484 EXPECT ("\377", "\377\377123"));
4485 }
4486
4487 /* Check that the increment-last-char in the matching algorithm for
4488 completion doesn't match "t1_fund" when completing "t1_func". */
4489 {
4490 static const char str[] = "t1_func";
4491 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4492 EXPECT ("t1_func", "t1_func1"));
4493 }
4494
4495 /* Check that completion mode works at each prefix of the expected
4496 symbol name. */
4497 {
4498 static const char str[] = "function(int)";
4499 size_t len = strlen (str);
4500 std::string lookup;
4501
4502 for (size_t i = 1; i < len; i++)
4503 {
4504 lookup.assign (str, i);
4505 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4506 EXPECT ("function"));
4507 }
4508 }
4509
4510 /* While "w" is a prefix of both components, the match function
4511 should still only be called once. */
4512 {
4513 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4514 EXPECT ("w1::w2"));
4515 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4516 EXPECT ("w1::w2"));
4517 }
4518
4519 /* Same, with a "complicated" symbol. */
4520 {
4521 static const char str[] = Z_SYM_NAME;
4522 size_t len = strlen (str);
4523 std::string lookup;
4524
4525 for (size_t i = 1; i < len; i++)
4526 {
4527 lookup.assign (str, i);
4528 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4529 EXPECT (Z_SYM_NAME));
4530 }
4531 }
4532
4533 /* In FULL mode, an incomplete symbol doesn't match. */
4534 {
4535 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4536 {});
4537 }
4538
4539 /* A complete symbol with parameters matches any overload, since the
4540 index has no overload info. */
4541 {
4542 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4543 EXPECT ("std::zfunction", "std::zfunction2"));
4544 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4545 EXPECT ("std::zfunction", "std::zfunction2"));
4546 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4547 EXPECT ("std::zfunction", "std::zfunction2"));
4548 }
4549
4550 /* Check that whitespace is ignored appropriately. A symbol with a
4551 template argument list. */
4552 {
4553 static const char expected[] = "ns::foo<int>";
4554 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4555 EXPECT (expected));
4556 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4557 EXPECT (expected));
4558 }
4559
4560 /* Check that whitespace is ignored appropriately. A symbol with a
4561 template argument list that includes a pointer. */
4562 {
4563 static const char expected[] = "ns::foo<char*>";
4564 /* Try both completion and non-completion modes. */
4565 static const bool completion_mode[2] = {false, true};
4566 for (size_t i = 0; i < 2; i++)
4567 {
4568 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4569 completion_mode[i], EXPECT (expected));
4570 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4571 completion_mode[i], EXPECT (expected));
4572
4573 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4574 completion_mode[i], EXPECT (expected));
4575 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4576 completion_mode[i], EXPECT (expected));
4577 }
4578 }
4579
4580 {
4581 /* Check method qualifiers are ignored. */
4582 static const char expected[] = "ns::foo<char*>";
4583 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4584 symbol_name_match_type::FULL, true, EXPECT (expected));
4585 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4586 symbol_name_match_type::FULL, true, EXPECT (expected));
4587 CHECK_MATCH ("foo < char * > ( int ) const",
4588 symbol_name_match_type::WILD, true, EXPECT (expected));
4589 CHECK_MATCH ("foo < char * > ( int ) &&",
4590 symbol_name_match_type::WILD, true, EXPECT (expected));
4591 }
4592
4593 /* Test lookup names that don't match anything. */
4594 {
4595 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4596 {});
4597
4598 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4599 {});
4600 }
4601
4602 /* Some wild matching tests, exercising "(anonymous namespace)",
4603 which should not be confused with a parameter list. */
4604 {
4605 static const char *syms[] = {
4606 "A::B::C",
4607 "B::C",
4608 "C",
4609 "A :: B :: C ( int )",
4610 "B :: C ( int )",
4611 "C ( int )",
4612 };
4613
4614 for (const char *s : syms)
4615 {
4616 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4617 EXPECT ("(anonymous namespace)::A::B::C"));
4618 }
4619 }
4620
4621 {
4622 static const char expected[] = "ns2::tmpl<int>::foo2";
4623 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4624 EXPECT (expected));
4625 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4626 EXPECT (expected));
4627 }
4628
4629 SELF_CHECK (!any_mismatch);
4630
4631 #undef EXPECT
4632 #undef CHECK_MATCH
4633 }
4634
4635 static void
4636 run_test ()
4637 {
4638 test_mapped_index_find_name_component_bounds ();
4639 test_dw2_expand_symtabs_matching_symbol ();
4640 }
4641
4642 }} // namespace selftests::dw2_expand_symtabs_matching
4643
4644 #endif /* GDB_SELF_TEST */
4645
4646 /* If FILE_MATCHER is NULL or if PER_CU has
4647 dwarf2_per_cu_quick_data::MARK set (see
4648 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4649 EXPANSION_NOTIFY on it. */
4650
4651 static void
4652 dw2_expand_symtabs_matching_one
4653 (dwarf2_per_cu_data *per_cu,
4654 dwarf2_per_objfile *per_objfile,
4655 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4656 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4657 {
4658 if (file_matcher == NULL || per_cu->v.quick->mark)
4659 {
4660 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4661
4662 compunit_symtab *symtab
4663 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4664 gdb_assert (symtab != nullptr);
4665
4666 if (expansion_notify != NULL && symtab_was_null)
4667 expansion_notify (symtab);
4668 }
4669 }
4670
4671 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4672 matched, to expand corresponding CUs that were marked. IDX is the
4673 index of the symbol name that matched. */
4674
4675 static void
4676 dw2_expand_marked_cus
4677 (dwarf2_per_objfile *per_objfile, offset_type idx,
4678 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4679 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4680 search_domain kind)
4681 {
4682 offset_type *vec, vec_len, vec_idx;
4683 bool global_seen = false;
4684 mapped_index &index = *per_objfile->per_bfd->index_table;
4685
4686 vec = (offset_type *) (index.constant_pool
4687 + MAYBE_SWAP (index.symbol_table[idx].vec));
4688 vec_len = MAYBE_SWAP (vec[0]);
4689 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4690 {
4691 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4692 /* This value is only valid for index versions >= 7. */
4693 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4694 gdb_index_symbol_kind symbol_kind =
4695 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4696 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4697 /* Only check the symbol attributes if they're present.
4698 Indices prior to version 7 don't record them,
4699 and indices >= 7 may elide them for certain symbols
4700 (gold does this). */
4701 int attrs_valid =
4702 (index.version >= 7
4703 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4704
4705 /* Work around gold/15646. */
4706 if (attrs_valid
4707 && !is_static
4708 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4709 {
4710 if (global_seen)
4711 continue;
4712
4713 global_seen = true;
4714 }
4715
4716 /* Only check the symbol's kind if it has one. */
4717 if (attrs_valid)
4718 {
4719 switch (kind)
4720 {
4721 case VARIABLES_DOMAIN:
4722 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4723 continue;
4724 break;
4725 case FUNCTIONS_DOMAIN:
4726 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4727 continue;
4728 break;
4729 case TYPES_DOMAIN:
4730 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4731 continue;
4732 break;
4733 case MODULES_DOMAIN:
4734 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4735 continue;
4736 break;
4737 default:
4738 break;
4739 }
4740 }
4741
4742 /* Don't crash on bad data. */
4743 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4744 + per_objfile->per_bfd->all_type_units.size ()))
4745 {
4746 complaint (_(".gdb_index entry has bad CU index"
4747 " [in module %s]"), objfile_name (per_objfile->objfile));
4748 continue;
4749 }
4750
4751 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4752 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4753 expansion_notify);
4754 }
4755 }
4756
4757 /* If FILE_MATCHER is non-NULL, set all the
4758 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4759 that match FILE_MATCHER. */
4760
4761 static void
4762 dw_expand_symtabs_matching_file_matcher
4763 (dwarf2_per_objfile *per_objfile,
4764 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4765 {
4766 if (file_matcher == NULL)
4767 return;
4768
4769 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4770 htab_eq_pointer,
4771 NULL, xcalloc, xfree));
4772 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4773 htab_eq_pointer,
4774 NULL, xcalloc, xfree));
4775
4776 /* The rule is CUs specify all the files, including those used by
4777 any TU, so there's no need to scan TUs here. */
4778
4779 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4780 {
4781 QUIT;
4782
4783 per_cu->v.quick->mark = 0;
4784
4785 /* We only need to look at symtabs not already expanded. */
4786 if (per_objfile->symtab_set_p (per_cu))
4787 continue;
4788
4789 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4790 if (file_data == NULL)
4791 continue;
4792
4793 if (htab_find (visited_not_found.get (), file_data) != NULL)
4794 continue;
4795 else if (htab_find (visited_found.get (), file_data) != NULL)
4796 {
4797 per_cu->v.quick->mark = 1;
4798 continue;
4799 }
4800
4801 for (int j = 0; j < file_data->num_file_names; ++j)
4802 {
4803 const char *this_real_name;
4804
4805 if (file_matcher (file_data->file_names[j], false))
4806 {
4807 per_cu->v.quick->mark = 1;
4808 break;
4809 }
4810
4811 /* Before we invoke realpath, which can get expensive when many
4812 files are involved, do a quick comparison of the basenames. */
4813 if (!basenames_may_differ
4814 && !file_matcher (lbasename (file_data->file_names[j]),
4815 true))
4816 continue;
4817
4818 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4819 if (file_matcher (this_real_name, false))
4820 {
4821 per_cu->v.quick->mark = 1;
4822 break;
4823 }
4824 }
4825
4826 void **slot = htab_find_slot (per_cu->v.quick->mark
4827 ? visited_found.get ()
4828 : visited_not_found.get (),
4829 file_data, INSERT);
4830 *slot = file_data;
4831 }
4832 }
4833
4834 static void
4835 dw2_expand_symtabs_matching
4836 (struct objfile *objfile,
4837 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4838 const lookup_name_info *lookup_name,
4839 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4840 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4841 enum search_domain kind)
4842 {
4843 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4844
4845 /* index_table is NULL if OBJF_READNOW. */
4846 if (!per_objfile->per_bfd->index_table)
4847 return;
4848
4849 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4850
4851 if (symbol_matcher == NULL && lookup_name == NULL)
4852 {
4853 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4854 {
4855 QUIT;
4856
4857 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4858 file_matcher, expansion_notify);
4859 }
4860 return;
4861 }
4862
4863 mapped_index &index = *per_objfile->per_bfd->index_table;
4864
4865 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4866 symbol_matcher,
4867 kind, [&] (offset_type idx)
4868 {
4869 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4870 kind);
4871 return true;
4872 }, per_objfile);
4873 }
4874
4875 void
4876 dwarf2_gdb_index::expand_symtabs_matching
4877 (struct objfile *objfile,
4878 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4879 const lookup_name_info *lookup_name,
4880 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4881 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4882 enum search_domain kind)
4883 {
4884 dw2_expand_symtabs_matching (objfile, file_matcher, lookup_name,
4885 symbol_matcher, expansion_notify, kind);
4886 }
4887
4888 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4889 symtab. */
4890
4891 static struct compunit_symtab *
4892 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4893 CORE_ADDR pc)
4894 {
4895 int i;
4896
4897 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4898 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4899 return cust;
4900
4901 if (cust->includes == NULL)
4902 return NULL;
4903
4904 for (i = 0; cust->includes[i]; ++i)
4905 {
4906 struct compunit_symtab *s = cust->includes[i];
4907
4908 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4909 if (s != NULL)
4910 return s;
4911 }
4912
4913 return NULL;
4914 }
4915
4916 struct compunit_symtab *
4917 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4918 (struct objfile *objfile,
4919 struct bound_minimal_symbol msymbol,
4920 CORE_ADDR pc,
4921 struct obj_section *section,
4922 int warn_if_readin)
4923 {
4924 struct dwarf2_per_cu_data *data;
4925 struct compunit_symtab *result;
4926
4927 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4928 if (per_objfile->per_bfd->index_addrmap == nullptr)
4929 return NULL;
4930
4931 CORE_ADDR baseaddr = objfile->text_section_offset ();
4932 data = ((struct dwarf2_per_cu_data *)
4933 addrmap_find (per_objfile->per_bfd->index_addrmap,
4934 pc - baseaddr));
4935 if (!data)
4936 return NULL;
4937
4938 if (warn_if_readin && per_objfile->symtab_set_p (data))
4939 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4940 paddress (objfile->arch (), pc));
4941
4942 result = recursively_find_pc_sect_compunit_symtab
4943 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4944
4945 gdb_assert (result != NULL);
4946 return result;
4947 }
4948
4949 void
4950 dwarf2_base_index_functions::map_symbol_filenames (struct objfile *objfile,
4951 symbol_filename_ftype *fun,
4952 void *data,
4953 int need_fullname)
4954 {
4955 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4956
4957 if (!per_objfile->per_bfd->filenames_cache)
4958 {
4959 per_objfile->per_bfd->filenames_cache.emplace ();
4960
4961 htab_up visited (htab_create_alloc (10,
4962 htab_hash_pointer, htab_eq_pointer,
4963 NULL, xcalloc, xfree));
4964
4965 /* The rule is CUs specify all the files, including those used
4966 by any TU, so there's no need to scan TUs here. We can
4967 ignore file names coming from already-expanded CUs. */
4968
4969 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4970 {
4971 if (per_objfile->symtab_set_p (per_cu))
4972 {
4973 void **slot = htab_find_slot (visited.get (),
4974 per_cu->v.quick->file_names,
4975 INSERT);
4976
4977 *slot = per_cu->v.quick->file_names;
4978 }
4979 }
4980
4981 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4982 {
4983 /* We only need to look at symtabs not already expanded. */
4984 if (per_objfile->symtab_set_p (per_cu))
4985 continue;
4986
4987 quick_file_names *file_data
4988 = dw2_get_file_names (per_cu, per_objfile);
4989 if (file_data == NULL)
4990 continue;
4991
4992 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4993 if (*slot)
4994 {
4995 /* Already visited. */
4996 continue;
4997 }
4998 *slot = file_data;
4999
5000 for (int j = 0; j < file_data->num_file_names; ++j)
5001 {
5002 const char *filename = file_data->file_names[j];
5003 per_objfile->per_bfd->filenames_cache->seen (filename);
5004 }
5005 }
5006 }
5007
5008 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
5009 {
5010 gdb::unique_xmalloc_ptr<char> this_real_name;
5011
5012 if (need_fullname)
5013 this_real_name = gdb_realpath (filename);
5014 (*fun) (filename, this_real_name.get (), data);
5015 });
5016 }
5017
5018 bool
5019 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
5020 {
5021 return true;
5022 }
5023
5024 /* DWARF-5 debug_names reader. */
5025
5026 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5027 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5028
5029 /* A helper function that reads the .debug_names section in SECTION
5030 and fills in MAP. FILENAME is the name of the file containing the
5031 section; it is used for error reporting.
5032
5033 Returns true if all went well, false otherwise. */
5034
5035 static bool
5036 read_debug_names_from_section (struct objfile *objfile,
5037 const char *filename,
5038 struct dwarf2_section_info *section,
5039 mapped_debug_names &map)
5040 {
5041 if (section->empty ())
5042 return false;
5043
5044 /* Older elfutils strip versions could keep the section in the main
5045 executable while splitting it for the separate debug info file. */
5046 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5047 return false;
5048
5049 section->read (objfile);
5050
5051 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5052
5053 const gdb_byte *addr = section->buffer;
5054
5055 bfd *const abfd = section->get_bfd_owner ();
5056
5057 unsigned int bytes_read;
5058 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5059 addr += bytes_read;
5060
5061 map.dwarf5_is_dwarf64 = bytes_read != 4;
5062 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5063 if (bytes_read + length != section->size)
5064 {
5065 /* There may be multiple per-CU indices. */
5066 warning (_("Section .debug_names in %s length %s does not match "
5067 "section length %s, ignoring .debug_names."),
5068 filename, plongest (bytes_read + length),
5069 pulongest (section->size));
5070 return false;
5071 }
5072
5073 /* The version number. */
5074 uint16_t version = read_2_bytes (abfd, addr);
5075 addr += 2;
5076 if (version != 5)
5077 {
5078 warning (_("Section .debug_names in %s has unsupported version %d, "
5079 "ignoring .debug_names."),
5080 filename, version);
5081 return false;
5082 }
5083
5084 /* Padding. */
5085 uint16_t padding = read_2_bytes (abfd, addr);
5086 addr += 2;
5087 if (padding != 0)
5088 {
5089 warning (_("Section .debug_names in %s has unsupported padding %d, "
5090 "ignoring .debug_names."),
5091 filename, padding);
5092 return false;
5093 }
5094
5095 /* comp_unit_count - The number of CUs in the CU list. */
5096 map.cu_count = read_4_bytes (abfd, addr);
5097 addr += 4;
5098
5099 /* local_type_unit_count - The number of TUs in the local TU
5100 list. */
5101 map.tu_count = read_4_bytes (abfd, addr);
5102 addr += 4;
5103
5104 /* foreign_type_unit_count - The number of TUs in the foreign TU
5105 list. */
5106 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5107 addr += 4;
5108 if (foreign_tu_count != 0)
5109 {
5110 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5111 "ignoring .debug_names."),
5112 filename, static_cast<unsigned long> (foreign_tu_count));
5113 return false;
5114 }
5115
5116 /* bucket_count - The number of hash buckets in the hash lookup
5117 table. */
5118 map.bucket_count = read_4_bytes (abfd, addr);
5119 addr += 4;
5120
5121 /* name_count - The number of unique names in the index. */
5122 map.name_count = read_4_bytes (abfd, addr);
5123 addr += 4;
5124
5125 /* abbrev_table_size - The size in bytes of the abbreviations
5126 table. */
5127 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5128 addr += 4;
5129
5130 /* augmentation_string_size - The size in bytes of the augmentation
5131 string. This value is rounded up to a multiple of 4. */
5132 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5133 addr += 4;
5134 map.augmentation_is_gdb = ((augmentation_string_size
5135 == sizeof (dwarf5_augmentation))
5136 && memcmp (addr, dwarf5_augmentation,
5137 sizeof (dwarf5_augmentation)) == 0);
5138 augmentation_string_size += (-augmentation_string_size) & 3;
5139 addr += augmentation_string_size;
5140
5141 /* List of CUs */
5142 map.cu_table_reordered = addr;
5143 addr += map.cu_count * map.offset_size;
5144
5145 /* List of Local TUs */
5146 map.tu_table_reordered = addr;
5147 addr += map.tu_count * map.offset_size;
5148
5149 /* Hash Lookup Table */
5150 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5151 addr += map.bucket_count * 4;
5152 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5153 addr += map.name_count * 4;
5154
5155 /* Name Table */
5156 map.name_table_string_offs_reordered = addr;
5157 addr += map.name_count * map.offset_size;
5158 map.name_table_entry_offs_reordered = addr;
5159 addr += map.name_count * map.offset_size;
5160
5161 const gdb_byte *abbrev_table_start = addr;
5162 for (;;)
5163 {
5164 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5165 addr += bytes_read;
5166 if (index_num == 0)
5167 break;
5168
5169 const auto insertpair
5170 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5171 if (!insertpair.second)
5172 {
5173 warning (_("Section .debug_names in %s has duplicate index %s, "
5174 "ignoring .debug_names."),
5175 filename, pulongest (index_num));
5176 return false;
5177 }
5178 mapped_debug_names::index_val &indexval = insertpair.first->second;
5179 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5180 addr += bytes_read;
5181
5182 for (;;)
5183 {
5184 mapped_debug_names::index_val::attr attr;
5185 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5186 addr += bytes_read;
5187 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5188 addr += bytes_read;
5189 if (attr.form == DW_FORM_implicit_const)
5190 {
5191 attr.implicit_const = read_signed_leb128 (abfd, addr,
5192 &bytes_read);
5193 addr += bytes_read;
5194 }
5195 if (attr.dw_idx == 0 && attr.form == 0)
5196 break;
5197 indexval.attr_vec.push_back (std::move (attr));
5198 }
5199 }
5200 if (addr != abbrev_table_start + abbrev_table_size)
5201 {
5202 warning (_("Section .debug_names in %s has abbreviation_table "
5203 "of size %s vs. written as %u, ignoring .debug_names."),
5204 filename, plongest (addr - abbrev_table_start),
5205 abbrev_table_size);
5206 return false;
5207 }
5208 map.entry_pool = addr;
5209
5210 return true;
5211 }
5212
5213 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5214 list. */
5215
5216 static void
5217 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5218 const mapped_debug_names &map,
5219 dwarf2_section_info &section,
5220 bool is_dwz)
5221 {
5222 if (!map.augmentation_is_gdb)
5223 {
5224 for (uint32_t i = 0; i < map.cu_count; ++i)
5225 {
5226 sect_offset sect_off
5227 = (sect_offset) (extract_unsigned_integer
5228 (map.cu_table_reordered + i * map.offset_size,
5229 map.offset_size,
5230 map.dwarf5_byte_order));
5231 /* We don't know the length of the CU, because the CU list in a
5232 .debug_names index can be incomplete, so we can't use the start
5233 of the next CU as end of this CU. We create the CUs here with
5234 length 0, and in cutu_reader::cutu_reader we'll fill in the
5235 actual length. */
5236 dwarf2_per_cu_data *per_cu
5237 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5238 sect_off, 0);
5239 per_bfd->all_comp_units.push_back (per_cu);
5240 }
5241 return;
5242 }
5243
5244 sect_offset sect_off_prev;
5245 for (uint32_t i = 0; i <= map.cu_count; ++i)
5246 {
5247 sect_offset sect_off_next;
5248 if (i < map.cu_count)
5249 {
5250 sect_off_next
5251 = (sect_offset) (extract_unsigned_integer
5252 (map.cu_table_reordered + i * map.offset_size,
5253 map.offset_size,
5254 map.dwarf5_byte_order));
5255 }
5256 else
5257 sect_off_next = (sect_offset) section.size;
5258 if (i >= 1)
5259 {
5260 const ULONGEST length = sect_off_next - sect_off_prev;
5261 dwarf2_per_cu_data *per_cu
5262 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5263 sect_off_prev, length);
5264 per_bfd->all_comp_units.push_back (per_cu);
5265 }
5266 sect_off_prev = sect_off_next;
5267 }
5268 }
5269
5270 /* Read the CU list from the mapped index, and use it to create all
5271 the CU objects for this dwarf2_per_objfile. */
5272
5273 static void
5274 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5275 const mapped_debug_names &map,
5276 const mapped_debug_names &dwz_map)
5277 {
5278 gdb_assert (per_bfd->all_comp_units.empty ());
5279 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5280
5281 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5282 false /* is_dwz */);
5283
5284 if (dwz_map.cu_count == 0)
5285 return;
5286
5287 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5288 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5289 true /* is_dwz */);
5290 }
5291
5292 /* Read .debug_names. If everything went ok, initialize the "quick"
5293 elements of all the CUs and return true. Otherwise, return false. */
5294
5295 static bool
5296 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5297 {
5298 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5299 mapped_debug_names dwz_map;
5300 struct objfile *objfile = per_objfile->objfile;
5301 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5302
5303 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5304 &per_objfile->per_bfd->debug_names, *map))
5305 return false;
5306
5307 /* Don't use the index if it's empty. */
5308 if (map->name_count == 0)
5309 return false;
5310
5311 /* If there is a .dwz file, read it so we can get its CU list as
5312 well. */
5313 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5314 if (dwz != NULL)
5315 {
5316 if (!read_debug_names_from_section (objfile,
5317 bfd_get_filename (dwz->dwz_bfd.get ()),
5318 &dwz->debug_names, dwz_map))
5319 {
5320 warning (_("could not read '.debug_names' section from %s; skipping"),
5321 bfd_get_filename (dwz->dwz_bfd.get ()));
5322 return false;
5323 }
5324 }
5325
5326 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5327
5328 if (map->tu_count != 0)
5329 {
5330 /* We can only handle a single .debug_types when we have an
5331 index. */
5332 if (per_bfd->types.size () != 1)
5333 return false;
5334
5335 dwarf2_section_info *section = &per_bfd->types[0];
5336
5337 create_signatured_type_table_from_debug_names
5338 (per_objfile, *map, section, &per_bfd->abbrev);
5339 }
5340
5341 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5342
5343 per_bfd->debug_names_table = std::move (map);
5344 per_bfd->using_index = 1;
5345 per_bfd->quick_file_names_table =
5346 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5347
5348 return true;
5349 }
5350
5351 /* Type used to manage iterating over all CUs looking for a symbol for
5352 .debug_names. */
5353
5354 class dw2_debug_names_iterator
5355 {
5356 public:
5357 dw2_debug_names_iterator (const mapped_debug_names &map,
5358 gdb::optional<block_enum> block_index,
5359 domain_enum domain,
5360 const char *name, dwarf2_per_objfile *per_objfile)
5361 : m_map (map), m_block_index (block_index), m_domain (domain),
5362 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5363 m_per_objfile (per_objfile)
5364 {}
5365
5366 dw2_debug_names_iterator (const mapped_debug_names &map,
5367 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5368 : m_map (map),
5369 m_search (search),
5370 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5371 m_per_objfile (per_objfile)
5372 {}
5373
5374 dw2_debug_names_iterator (const mapped_debug_names &map,
5375 block_enum block_index, domain_enum domain,
5376 uint32_t namei, dwarf2_per_objfile *per_objfile)
5377 : m_map (map), m_block_index (block_index), m_domain (domain),
5378 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5379 m_per_objfile (per_objfile)
5380 {}
5381
5382 /* Return the next matching CU or NULL if there are no more. */
5383 dwarf2_per_cu_data *next ();
5384
5385 private:
5386 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5387 const char *name,
5388 dwarf2_per_objfile *per_objfile);
5389 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5390 uint32_t namei,
5391 dwarf2_per_objfile *per_objfile);
5392
5393 /* The internalized form of .debug_names. */
5394 const mapped_debug_names &m_map;
5395
5396 /* If set, only look for symbols that match that block. Valid values are
5397 GLOBAL_BLOCK and STATIC_BLOCK. */
5398 const gdb::optional<block_enum> m_block_index;
5399
5400 /* The kind of symbol we're looking for. */
5401 const domain_enum m_domain = UNDEF_DOMAIN;
5402 const search_domain m_search = ALL_DOMAIN;
5403
5404 /* The list of CUs from the index entry of the symbol, or NULL if
5405 not found. */
5406 const gdb_byte *m_addr;
5407
5408 dwarf2_per_objfile *m_per_objfile;
5409 };
5410
5411 const char *
5412 mapped_debug_names::namei_to_name
5413 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5414 {
5415 const ULONGEST namei_string_offs
5416 = extract_unsigned_integer ((name_table_string_offs_reordered
5417 + namei * offset_size),
5418 offset_size,
5419 dwarf5_byte_order);
5420 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5421 }
5422
5423 /* Find a slot in .debug_names for the object named NAME. If NAME is
5424 found, return pointer to its pool data. If NAME cannot be found,
5425 return NULL. */
5426
5427 const gdb_byte *
5428 dw2_debug_names_iterator::find_vec_in_debug_names
5429 (const mapped_debug_names &map, const char *name,
5430 dwarf2_per_objfile *per_objfile)
5431 {
5432 int (*cmp) (const char *, const char *);
5433
5434 gdb::unique_xmalloc_ptr<char> without_params;
5435 if (current_language->la_language == language_cplus
5436 || current_language->la_language == language_fortran
5437 || current_language->la_language == language_d)
5438 {
5439 /* NAME is already canonical. Drop any qualifiers as
5440 .debug_names does not contain any. */
5441
5442 if (strchr (name, '(') != NULL)
5443 {
5444 without_params = cp_remove_params (name);
5445 if (without_params != NULL)
5446 name = without_params.get ();
5447 }
5448 }
5449
5450 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5451
5452 const uint32_t full_hash = dwarf5_djb_hash (name);
5453 uint32_t namei
5454 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5455 (map.bucket_table_reordered
5456 + (full_hash % map.bucket_count)), 4,
5457 map.dwarf5_byte_order);
5458 if (namei == 0)
5459 return NULL;
5460 --namei;
5461 if (namei >= map.name_count)
5462 {
5463 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5464 "[in module %s]"),
5465 namei, map.name_count,
5466 objfile_name (per_objfile->objfile));
5467 return NULL;
5468 }
5469
5470 for (;;)
5471 {
5472 const uint32_t namei_full_hash
5473 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5474 (map.hash_table_reordered + namei), 4,
5475 map.dwarf5_byte_order);
5476 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5477 return NULL;
5478
5479 if (full_hash == namei_full_hash)
5480 {
5481 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5482
5483 #if 0 /* An expensive sanity check. */
5484 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5485 {
5486 complaint (_("Wrong .debug_names hash for string at index %u "
5487 "[in module %s]"),
5488 namei, objfile_name (dwarf2_per_objfile->objfile));
5489 return NULL;
5490 }
5491 #endif
5492
5493 if (cmp (namei_string, name) == 0)
5494 {
5495 const ULONGEST namei_entry_offs
5496 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5497 + namei * map.offset_size),
5498 map.offset_size, map.dwarf5_byte_order);
5499 return map.entry_pool + namei_entry_offs;
5500 }
5501 }
5502
5503 ++namei;
5504 if (namei >= map.name_count)
5505 return NULL;
5506 }
5507 }
5508
5509 const gdb_byte *
5510 dw2_debug_names_iterator::find_vec_in_debug_names
5511 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5512 {
5513 if (namei >= map.name_count)
5514 {
5515 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5516 "[in module %s]"),
5517 namei, map.name_count,
5518 objfile_name (per_objfile->objfile));
5519 return NULL;
5520 }
5521
5522 const ULONGEST namei_entry_offs
5523 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5524 + namei * map.offset_size),
5525 map.offset_size, map.dwarf5_byte_order);
5526 return map.entry_pool + namei_entry_offs;
5527 }
5528
5529 /* See dw2_debug_names_iterator. */
5530
5531 dwarf2_per_cu_data *
5532 dw2_debug_names_iterator::next ()
5533 {
5534 if (m_addr == NULL)
5535 return NULL;
5536
5537 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5538 struct objfile *objfile = m_per_objfile->objfile;
5539 bfd *const abfd = objfile->obfd;
5540
5541 again:
5542
5543 unsigned int bytes_read;
5544 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5545 m_addr += bytes_read;
5546 if (abbrev == 0)
5547 return NULL;
5548
5549 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5550 if (indexval_it == m_map.abbrev_map.cend ())
5551 {
5552 complaint (_("Wrong .debug_names undefined abbrev code %s "
5553 "[in module %s]"),
5554 pulongest (abbrev), objfile_name (objfile));
5555 return NULL;
5556 }
5557 const mapped_debug_names::index_val &indexval = indexval_it->second;
5558 enum class symbol_linkage {
5559 unknown,
5560 static_,
5561 extern_,
5562 } symbol_linkage_ = symbol_linkage::unknown;
5563 dwarf2_per_cu_data *per_cu = NULL;
5564 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5565 {
5566 ULONGEST ull;
5567 switch (attr.form)
5568 {
5569 case DW_FORM_implicit_const:
5570 ull = attr.implicit_const;
5571 break;
5572 case DW_FORM_flag_present:
5573 ull = 1;
5574 break;
5575 case DW_FORM_udata:
5576 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5577 m_addr += bytes_read;
5578 break;
5579 case DW_FORM_ref4:
5580 ull = read_4_bytes (abfd, m_addr);
5581 m_addr += 4;
5582 break;
5583 case DW_FORM_ref8:
5584 ull = read_8_bytes (abfd, m_addr);
5585 m_addr += 8;
5586 break;
5587 case DW_FORM_ref_sig8:
5588 ull = read_8_bytes (abfd, m_addr);
5589 m_addr += 8;
5590 break;
5591 default:
5592 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5593 dwarf_form_name (attr.form),
5594 objfile_name (objfile));
5595 return NULL;
5596 }
5597 switch (attr.dw_idx)
5598 {
5599 case DW_IDX_compile_unit:
5600 /* Don't crash on bad data. */
5601 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5602 {
5603 complaint (_(".debug_names entry has bad CU index %s"
5604 " [in module %s]"),
5605 pulongest (ull),
5606 objfile_name (objfile));
5607 continue;
5608 }
5609 per_cu = per_bfd->get_cutu (ull);
5610 break;
5611 case DW_IDX_type_unit:
5612 /* Don't crash on bad data. */
5613 if (ull >= per_bfd->all_type_units.size ())
5614 {
5615 complaint (_(".debug_names entry has bad TU index %s"
5616 " [in module %s]"),
5617 pulongest (ull),
5618 objfile_name (objfile));
5619 continue;
5620 }
5621 per_cu = &per_bfd->get_tu (ull)->per_cu;
5622 break;
5623 case DW_IDX_die_offset:
5624 /* In a per-CU index (as opposed to a per-module index), index
5625 entries without CU attribute implicitly refer to the single CU. */
5626 if (per_cu == NULL)
5627 per_cu = per_bfd->get_cu (0);
5628 break;
5629 case DW_IDX_GNU_internal:
5630 if (!m_map.augmentation_is_gdb)
5631 break;
5632 symbol_linkage_ = symbol_linkage::static_;
5633 break;
5634 case DW_IDX_GNU_external:
5635 if (!m_map.augmentation_is_gdb)
5636 break;
5637 symbol_linkage_ = symbol_linkage::extern_;
5638 break;
5639 }
5640 }
5641
5642 /* Skip if already read in. */
5643 if (m_per_objfile->symtab_set_p (per_cu))
5644 goto again;
5645
5646 /* Check static vs global. */
5647 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5648 {
5649 const bool want_static = *m_block_index == STATIC_BLOCK;
5650 const bool symbol_is_static =
5651 symbol_linkage_ == symbol_linkage::static_;
5652 if (want_static != symbol_is_static)
5653 goto again;
5654 }
5655
5656 /* Match dw2_symtab_iter_next, symbol_kind
5657 and debug_names::psymbol_tag. */
5658 switch (m_domain)
5659 {
5660 case VAR_DOMAIN:
5661 switch (indexval.dwarf_tag)
5662 {
5663 case DW_TAG_variable:
5664 case DW_TAG_subprogram:
5665 /* Some types are also in VAR_DOMAIN. */
5666 case DW_TAG_typedef:
5667 case DW_TAG_structure_type:
5668 break;
5669 default:
5670 goto again;
5671 }
5672 break;
5673 case STRUCT_DOMAIN:
5674 switch (indexval.dwarf_tag)
5675 {
5676 case DW_TAG_typedef:
5677 case DW_TAG_structure_type:
5678 break;
5679 default:
5680 goto again;
5681 }
5682 break;
5683 case LABEL_DOMAIN:
5684 switch (indexval.dwarf_tag)
5685 {
5686 case 0:
5687 case DW_TAG_variable:
5688 break;
5689 default:
5690 goto again;
5691 }
5692 break;
5693 case MODULE_DOMAIN:
5694 switch (indexval.dwarf_tag)
5695 {
5696 case DW_TAG_module:
5697 break;
5698 default:
5699 goto again;
5700 }
5701 break;
5702 default:
5703 break;
5704 }
5705
5706 /* Match dw2_expand_symtabs_matching, symbol_kind and
5707 debug_names::psymbol_tag. */
5708 switch (m_search)
5709 {
5710 case VARIABLES_DOMAIN:
5711 switch (indexval.dwarf_tag)
5712 {
5713 case DW_TAG_variable:
5714 break;
5715 default:
5716 goto again;
5717 }
5718 break;
5719 case FUNCTIONS_DOMAIN:
5720 switch (indexval.dwarf_tag)
5721 {
5722 case DW_TAG_subprogram:
5723 break;
5724 default:
5725 goto again;
5726 }
5727 break;
5728 case TYPES_DOMAIN:
5729 switch (indexval.dwarf_tag)
5730 {
5731 case DW_TAG_typedef:
5732 case DW_TAG_structure_type:
5733 break;
5734 default:
5735 goto again;
5736 }
5737 break;
5738 case MODULES_DOMAIN:
5739 switch (indexval.dwarf_tag)
5740 {
5741 case DW_TAG_module:
5742 break;
5743 default:
5744 goto again;
5745 }
5746 default:
5747 break;
5748 }
5749
5750 return per_cu;
5751 }
5752
5753 struct compunit_symtab *
5754 dwarf2_debug_names_index::lookup_symbol
5755 (struct objfile *objfile, block_enum block_index,
5756 const char *name, domain_enum domain)
5757 {
5758 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5759
5760 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5761 if (!mapp)
5762 {
5763 /* index is NULL if OBJF_READNOW. */
5764 return NULL;
5765 }
5766 const auto &map = *mapp;
5767
5768 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5769
5770 struct compunit_symtab *stab_best = NULL;
5771 struct dwarf2_per_cu_data *per_cu;
5772 while ((per_cu = iter.next ()) != NULL)
5773 {
5774 struct symbol *sym, *with_opaque = NULL;
5775 compunit_symtab *stab
5776 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5777 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5778 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5779
5780 sym = block_find_symbol (block, name, domain,
5781 block_find_non_opaque_type_preferred,
5782 &with_opaque);
5783
5784 /* Some caution must be observed with overloaded functions and
5785 methods, since the index will not contain any overload
5786 information (but NAME might contain it). */
5787
5788 if (sym != NULL
5789 && strcmp_iw (sym->search_name (), name) == 0)
5790 return stab;
5791 if (with_opaque != NULL
5792 && strcmp_iw (with_opaque->search_name (), name) == 0)
5793 stab_best = stab;
5794
5795 /* Keep looking through other CUs. */
5796 }
5797
5798 return stab_best;
5799 }
5800
5801 /* This dumps minimal information about .debug_names. It is called
5802 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5803 uses this to verify that .debug_names has been loaded. */
5804
5805 void
5806 dwarf2_debug_names_index::dump (struct objfile *objfile)
5807 {
5808 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5809
5810 gdb_assert (per_objfile->per_bfd->using_index);
5811 printf_filtered (".debug_names:");
5812 if (per_objfile->per_bfd->debug_names_table)
5813 printf_filtered (" exists\n");
5814 else
5815 printf_filtered (" faked for \"readnow\"\n");
5816 printf_filtered ("\n");
5817 }
5818
5819 void
5820 dwarf2_debug_names_index::expand_symtabs_for_function
5821 (struct objfile *objfile, const char *func_name)
5822 {
5823 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5824
5825 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5826 if (per_objfile->per_bfd->debug_names_table)
5827 {
5828 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5829
5830 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5831 per_objfile);
5832
5833 struct dwarf2_per_cu_data *per_cu;
5834 while ((per_cu = iter.next ()) != NULL)
5835 dw2_instantiate_symtab (per_cu, per_objfile, false);
5836 }
5837 }
5838
5839 void
5840 dwarf2_debug_names_index::map_matching_symbols
5841 (struct objfile *objfile,
5842 const lookup_name_info &name, domain_enum domain,
5843 int global,
5844 gdb::function_view<symbol_found_callback_ftype> callback,
5845 symbol_compare_ftype *ordered_compare)
5846 {
5847 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5848
5849 /* debug_names_table is NULL if OBJF_READNOW. */
5850 if (!per_objfile->per_bfd->debug_names_table)
5851 return;
5852
5853 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5854 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5855
5856 const char *match_name = name.ada ().lookup_name ().c_str ();
5857 auto matcher = [&] (const char *symname)
5858 {
5859 if (ordered_compare == nullptr)
5860 return true;
5861 return ordered_compare (symname, match_name) == 0;
5862 };
5863
5864 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5865 [&] (offset_type namei)
5866 {
5867 /* The name was matched, now expand corresponding CUs that were
5868 marked. */
5869 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5870 per_objfile);
5871
5872 struct dwarf2_per_cu_data *per_cu;
5873 while ((per_cu = iter.next ()) != NULL)
5874 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5875 nullptr);
5876 return true;
5877 }, per_objfile);
5878
5879 /* It's a shame we couldn't do this inside the
5880 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5881 that have already been expanded. Instead, this loop matches what
5882 the psymtab code does. */
5883 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5884 {
5885 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5886 if (symtab != nullptr)
5887 {
5888 const struct block *block
5889 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5890 if (!iterate_over_symbols_terminated (block, name,
5891 domain, callback))
5892 break;
5893 }
5894 }
5895 }
5896
5897 void
5898 dwarf2_debug_names_index::expand_symtabs_matching
5899 (struct objfile *objfile,
5900 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5901 const lookup_name_info *lookup_name,
5902 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5903 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5904 enum search_domain kind)
5905 {
5906 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5907
5908 /* debug_names_table is NULL if OBJF_READNOW. */
5909 if (!per_objfile->per_bfd->debug_names_table)
5910 return;
5911
5912 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5913
5914 if (symbol_matcher == NULL && lookup_name == NULL)
5915 {
5916 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5917 {
5918 QUIT;
5919
5920 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5921 expansion_notify);
5922 }
5923 return;
5924 }
5925
5926 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5927
5928 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5929 symbol_matcher,
5930 kind, [&] (offset_type namei)
5931 {
5932 /* The name was matched, now expand corresponding CUs that were
5933 marked. */
5934 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5935
5936 struct dwarf2_per_cu_data *per_cu;
5937 while ((per_cu = iter.next ()) != NULL)
5938 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5939 expansion_notify);
5940 return true;
5941 }, per_objfile);
5942 }
5943
5944 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5945 to either a dwarf2_per_bfd or dwz_file object. */
5946
5947 template <typename T>
5948 static gdb::array_view<const gdb_byte>
5949 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5950 {
5951 dwarf2_section_info *section = &section_owner->gdb_index;
5952
5953 if (section->empty ())
5954 return {};
5955
5956 /* Older elfutils strip versions could keep the section in the main
5957 executable while splitting it for the separate debug info file. */
5958 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5959 return {};
5960
5961 section->read (obj);
5962
5963 /* dwarf2_section_info::size is a bfd_size_type, while
5964 gdb::array_view works with size_t. On 32-bit hosts, with
5965 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5966 is 32-bit. So we need an explicit narrowing conversion here.
5967 This is fine, because it's impossible to allocate or mmap an
5968 array/buffer larger than what size_t can represent. */
5969 return gdb::make_array_view (section->buffer, section->size);
5970 }
5971
5972 /* Lookup the index cache for the contents of the index associated to
5973 DWARF2_OBJ. */
5974
5975 static gdb::array_view<const gdb_byte>
5976 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5977 {
5978 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5979 if (build_id == nullptr)
5980 return {};
5981
5982 return global_index_cache.lookup_gdb_index (build_id,
5983 &dwarf2_per_bfd->index_cache_res);
5984 }
5985
5986 /* Same as the above, but for DWZ. */
5987
5988 static gdb::array_view<const gdb_byte>
5989 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5990 {
5991 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5992 if (build_id == nullptr)
5993 return {};
5994
5995 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5996 }
5997
5998 /* See symfile.h. */
5999
6000 bool
6001 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6002 {
6003 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6004 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6005
6006 dwarf_read_debug_printf ("called");
6007
6008 /* If we're about to read full symbols, don't bother with the
6009 indices. In this case we also don't care if some other debug
6010 format is making psymtabs, because they are all about to be
6011 expanded anyway. */
6012 if ((objfile->flags & OBJF_READNOW))
6013 {
6014 dwarf_read_debug_printf ("readnow requested");
6015
6016 /* When using READNOW, the using_index flag (set below) indicates that
6017 PER_BFD was already initialized, when we loaded some other objfile. */
6018 if (per_bfd->using_index)
6019 {
6020 dwarf_read_debug_printf ("using_index already set");
6021 *index_kind = dw_index_kind::GDB_INDEX;
6022 per_objfile->resize_symtabs ();
6023 return true;
6024 }
6025
6026 per_bfd->using_index = 1;
6027 create_all_comp_units (per_objfile);
6028 create_all_type_units (per_objfile);
6029 per_bfd->quick_file_names_table
6030 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6031 per_objfile->resize_symtabs ();
6032
6033 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6034 + per_bfd->all_type_units.size ()); ++i)
6035 {
6036 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6037
6038 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6039 struct dwarf2_per_cu_quick_data);
6040 }
6041
6042 /* Return 1 so that gdb sees the "quick" functions. However,
6043 these functions will be no-ops because we will have expanded
6044 all symtabs. */
6045 *index_kind = dw_index_kind::GDB_INDEX;
6046 return true;
6047 }
6048
6049 /* Was a debug names index already read when we processed an objfile sharing
6050 PER_BFD? */
6051 if (per_bfd->debug_names_table != nullptr)
6052 {
6053 dwarf_read_debug_printf ("re-using shared debug names table");
6054 *index_kind = dw_index_kind::DEBUG_NAMES;
6055 per_objfile->resize_symtabs ();
6056 return true;
6057 }
6058
6059 /* Was a GDB index already read when we processed an objfile sharing
6060 PER_BFD? */
6061 if (per_bfd->index_table != nullptr)
6062 {
6063 dwarf_read_debug_printf ("re-using shared index table");
6064 *index_kind = dw_index_kind::GDB_INDEX;
6065 per_objfile->resize_symtabs ();
6066 return true;
6067 }
6068
6069 /* There might already be partial symtabs built for this BFD. This happens
6070 when loading the same binary twice with the index-cache enabled. If so,
6071 don't try to read an index. The objfile / per_objfile initialization will
6072 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6073 code path. */
6074 if (per_bfd->partial_symtabs != nullptr)
6075 {
6076 dwarf_read_debug_printf ("re-using shared partial symtabs");
6077 return false;
6078 }
6079
6080 if (dwarf2_read_debug_names (per_objfile))
6081 {
6082 dwarf_read_debug_printf ("found debug names");
6083 *index_kind = dw_index_kind::DEBUG_NAMES;
6084 per_objfile->resize_symtabs ();
6085 return true;
6086 }
6087
6088 if (dwarf2_read_gdb_index (per_objfile,
6089 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6090 get_gdb_index_contents_from_section<dwz_file>))
6091 {
6092 dwarf_read_debug_printf ("found gdb index from file");
6093 *index_kind = dw_index_kind::GDB_INDEX;
6094 per_objfile->resize_symtabs ();
6095 return true;
6096 }
6097
6098 /* ... otherwise, try to find the index in the index cache. */
6099 if (dwarf2_read_gdb_index (per_objfile,
6100 get_gdb_index_contents_from_cache,
6101 get_gdb_index_contents_from_cache_dwz))
6102 {
6103 dwarf_read_debug_printf ("found gdb index from cache");
6104 global_index_cache.hit ();
6105 *index_kind = dw_index_kind::GDB_INDEX;
6106 per_objfile->resize_symtabs ();
6107 return true;
6108 }
6109
6110 global_index_cache.miss ();
6111 return false;
6112 }
6113
6114 \f
6115
6116 /* Build a partial symbol table. */
6117
6118 void
6119 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
6120 {
6121 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6122 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6123
6124 if (per_bfd->partial_symtabs != nullptr)
6125 {
6126 /* Partial symbols were already read, so now we can simply
6127 attach them. */
6128 if (psf == nullptr)
6129 {
6130 psf = new psymbol_functions (per_bfd->partial_symtabs);
6131 objfile->qf.emplace_front (psf);
6132 }
6133 else
6134 psf->set_partial_symtabs (per_bfd->partial_symtabs);
6135 per_objfile->resize_symtabs ();
6136 return;
6137 }
6138
6139 if (psf == nullptr)
6140 {
6141 psf = new psymbol_functions;
6142 objfile->qf.emplace_front (psf);
6143 }
6144 const std::shared_ptr<psymtab_storage> &partial_symtabs
6145 = psf->get_partial_symtabs ();
6146
6147 /* Set the local reference to partial symtabs, so that we don't try
6148 to read them again if reading another objfile with the same BFD.
6149 If we can't in fact share, this won't make a difference anyway as
6150 the dwarf2_per_bfd object won't be shared. */
6151 per_bfd->partial_symtabs = partial_symtabs;
6152
6153 try
6154 {
6155 /* This isn't really ideal: all the data we allocate on the
6156 objfile's obstack is still uselessly kept around. However,
6157 freeing it seems unsafe. */
6158 psymtab_discarder psymtabs (partial_symtabs.get ());
6159 dwarf2_build_psymtabs_hard (per_objfile);
6160 psymtabs.keep ();
6161
6162 per_objfile->resize_symtabs ();
6163
6164 /* (maybe) store an index in the cache. */
6165 global_index_cache.store (per_objfile);
6166 }
6167 catch (const gdb_exception_error &except)
6168 {
6169 exception_print (gdb_stderr, except);
6170 }
6171 }
6172
6173 /* Find the base address of the compilation unit for range lists and
6174 location lists. It will normally be specified by DW_AT_low_pc.
6175 In DWARF-3 draft 4, the base address could be overridden by
6176 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6177 compilation units with discontinuous ranges. */
6178
6179 static void
6180 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6181 {
6182 struct attribute *attr;
6183
6184 cu->base_address.reset ();
6185
6186 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6187 if (attr != nullptr)
6188 cu->base_address = attr->as_address ();
6189 else
6190 {
6191 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6192 if (attr != nullptr)
6193 cu->base_address = attr->as_address ();
6194 }
6195 }
6196
6197 /* Helper function that returns the proper abbrev section for
6198 THIS_CU. */
6199
6200 static struct dwarf2_section_info *
6201 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6202 {
6203 struct dwarf2_section_info *abbrev;
6204 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6205
6206 if (this_cu->is_dwz)
6207 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
6208 else
6209 abbrev = &per_bfd->abbrev;
6210
6211 return abbrev;
6212 }
6213
6214 /* Fetch the abbreviation table offset from a comp or type unit header. */
6215
6216 static sect_offset
6217 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6218 struct dwarf2_section_info *section,
6219 sect_offset sect_off)
6220 {
6221 bfd *abfd = section->get_bfd_owner ();
6222 const gdb_byte *info_ptr;
6223 unsigned int initial_length_size, offset_size;
6224 uint16_t version;
6225
6226 section->read (per_objfile->objfile);
6227 info_ptr = section->buffer + to_underlying (sect_off);
6228 read_initial_length (abfd, info_ptr, &initial_length_size);
6229 offset_size = initial_length_size == 4 ? 4 : 8;
6230 info_ptr += initial_length_size;
6231
6232 version = read_2_bytes (abfd, info_ptr);
6233 info_ptr += 2;
6234 if (version >= 5)
6235 {
6236 /* Skip unit type and address size. */
6237 info_ptr += 2;
6238 }
6239
6240 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6241 }
6242
6243 /* A partial symtab that is used only for include files. */
6244 struct dwarf2_include_psymtab : public partial_symtab
6245 {
6246 dwarf2_include_psymtab (const char *filename,
6247 psymtab_storage *partial_symtabs,
6248 struct objfile *objfile)
6249 : partial_symtab (filename, partial_symtabs, objfile)
6250 {
6251 }
6252
6253 void read_symtab (struct objfile *objfile) override
6254 {
6255 /* It's an include file, no symbols to read for it.
6256 Everything is in the includer symtab. */
6257
6258 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6259 expansion of the includer psymtab. We use the dependencies[0] field to
6260 model the includer. But if we go the regular route of calling
6261 expand_psymtab here, and having expand_psymtab call expand_dependencies
6262 to expand the includer, we'll only use expand_psymtab on the includer
6263 (making it a non-toplevel psymtab), while if we expand the includer via
6264 another path, we'll use read_symtab (making it a toplevel psymtab).
6265 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6266 psymtab, and trigger read_symtab on the includer here directly. */
6267 includer ()->read_symtab (objfile);
6268 }
6269
6270 void expand_psymtab (struct objfile *objfile) override
6271 {
6272 /* This is not called by read_symtab, and should not be called by any
6273 expand_dependencies. */
6274 gdb_assert (false);
6275 }
6276
6277 bool readin_p (struct objfile *objfile) const override
6278 {
6279 return includer ()->readin_p (objfile);
6280 }
6281
6282 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6283 {
6284 return nullptr;
6285 }
6286
6287 private:
6288 partial_symtab *includer () const
6289 {
6290 /* An include psymtab has exactly one dependency: the psymtab that
6291 includes it. */
6292 gdb_assert (this->number_of_dependencies == 1);
6293 return this->dependencies[0];
6294 }
6295 };
6296
6297 /* Allocate a new partial symtab for file named NAME and mark this new
6298 partial symtab as being an include of PST. */
6299
6300 static void
6301 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
6302 const char *name,
6303 dwarf2_psymtab *pst,
6304 psymtab_storage *partial_symtabs,
6305 struct objfile *objfile)
6306 {
6307 dwarf2_include_psymtab *subpst
6308 = new dwarf2_include_psymtab (name, partial_symtabs, objfile);
6309
6310 if (!IS_ABSOLUTE_PATH (subpst->filename))
6311 subpst->dirname = pst->dirname;
6312
6313 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
6314 subpst->dependencies[0] = pst;
6315 subpst->number_of_dependencies = 1;
6316 }
6317
6318 /* Read the Line Number Program data and extract the list of files
6319 included by the source file represented by PST. Build an include
6320 partial symtab for each of these included files. */
6321
6322 static void
6323 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6324 struct die_info *die,
6325 dwarf2_psymtab *pst)
6326 {
6327 line_header_up lh;
6328 struct attribute *attr;
6329
6330 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6331 if (attr != nullptr && attr->form_is_unsigned ())
6332 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6333 if (lh == NULL)
6334 return; /* No linetable, so no includes. */
6335
6336 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6337 that we pass in the raw text_low here; that is ok because we're
6338 only decoding the line table to make include partial symtabs, and
6339 so the addresses aren't really used. */
6340 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6341 pst->raw_text_low (), 1);
6342 }
6343
6344 static hashval_t
6345 hash_signatured_type (const void *item)
6346 {
6347 const struct signatured_type *sig_type
6348 = (const struct signatured_type *) item;
6349
6350 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6351 return sig_type->signature;
6352 }
6353
6354 static int
6355 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6356 {
6357 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6358 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6359
6360 return lhs->signature == rhs->signature;
6361 }
6362
6363 /* Allocate a hash table for signatured types. */
6364
6365 static htab_up
6366 allocate_signatured_type_table ()
6367 {
6368 return htab_up (htab_create_alloc (41,
6369 hash_signatured_type,
6370 eq_signatured_type,
6371 NULL, xcalloc, xfree));
6372 }
6373
6374 /* A helper function to add a signatured type CU to a table. */
6375
6376 static int
6377 add_signatured_type_cu_to_table (void **slot, void *datum)
6378 {
6379 struct signatured_type *sigt = (struct signatured_type *) *slot;
6380 std::vector<signatured_type *> *all_type_units
6381 = (std::vector<signatured_type *> *) datum;
6382
6383 all_type_units->push_back (sigt);
6384
6385 return 1;
6386 }
6387
6388 /* A helper for create_debug_types_hash_table. Read types from SECTION
6389 and fill them into TYPES_HTAB. It will process only type units,
6390 therefore DW_UT_type. */
6391
6392 static void
6393 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6394 struct dwo_file *dwo_file,
6395 dwarf2_section_info *section, htab_up &types_htab,
6396 rcuh_kind section_kind)
6397 {
6398 struct objfile *objfile = per_objfile->objfile;
6399 struct dwarf2_section_info *abbrev_section;
6400 bfd *abfd;
6401 const gdb_byte *info_ptr, *end_ptr;
6402
6403 abbrev_section = (dwo_file != NULL
6404 ? &dwo_file->sections.abbrev
6405 : &per_objfile->per_bfd->abbrev);
6406
6407 dwarf_read_debug_printf ("Reading %s for %s",
6408 section->get_name (),
6409 abbrev_section->get_file_name ());
6410
6411 section->read (objfile);
6412 info_ptr = section->buffer;
6413
6414 if (info_ptr == NULL)
6415 return;
6416
6417 /* We can't set abfd until now because the section may be empty or
6418 not present, in which case the bfd is unknown. */
6419 abfd = section->get_bfd_owner ();
6420
6421 /* We don't use cutu_reader here because we don't need to read
6422 any dies: the signature is in the header. */
6423
6424 end_ptr = info_ptr + section->size;
6425 while (info_ptr < end_ptr)
6426 {
6427 struct signatured_type *sig_type;
6428 struct dwo_unit *dwo_tu;
6429 void **slot;
6430 const gdb_byte *ptr = info_ptr;
6431 struct comp_unit_head header;
6432 unsigned int length;
6433
6434 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6435
6436 /* Initialize it due to a false compiler warning. */
6437 header.signature = -1;
6438 header.type_cu_offset_in_tu = (cu_offset) -1;
6439
6440 /* We need to read the type's signature in order to build the hash
6441 table, but we don't need anything else just yet. */
6442
6443 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6444 abbrev_section, ptr, section_kind);
6445
6446 length = header.get_length ();
6447
6448 /* Skip dummy type units. */
6449 if (ptr >= info_ptr + length
6450 || peek_abbrev_code (abfd, ptr) == 0
6451 || (header.unit_type != DW_UT_type
6452 && header.unit_type != DW_UT_split_type))
6453 {
6454 info_ptr += length;
6455 continue;
6456 }
6457
6458 if (types_htab == NULL)
6459 {
6460 if (dwo_file)
6461 types_htab = allocate_dwo_unit_table ();
6462 else
6463 types_htab = allocate_signatured_type_table ();
6464 }
6465
6466 if (dwo_file)
6467 {
6468 sig_type = NULL;
6469 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6470 dwo_tu->dwo_file = dwo_file;
6471 dwo_tu->signature = header.signature;
6472 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6473 dwo_tu->section = section;
6474 dwo_tu->sect_off = sect_off;
6475 dwo_tu->length = length;
6476 }
6477 else
6478 {
6479 /* N.B.: type_offset is not usable if this type uses a DWO file.
6480 The real type_offset is in the DWO file. */
6481 dwo_tu = NULL;
6482 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6483 sig_type->signature = header.signature;
6484 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6485 sig_type->per_cu.is_debug_types = 1;
6486 sig_type->per_cu.section = section;
6487 sig_type->per_cu.sect_off = sect_off;
6488 sig_type->per_cu.length = length;
6489 }
6490
6491 slot = htab_find_slot (types_htab.get (),
6492 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6493 INSERT);
6494 gdb_assert (slot != NULL);
6495 if (*slot != NULL)
6496 {
6497 sect_offset dup_sect_off;
6498
6499 if (dwo_file)
6500 {
6501 const struct dwo_unit *dup_tu
6502 = (const struct dwo_unit *) *slot;
6503
6504 dup_sect_off = dup_tu->sect_off;
6505 }
6506 else
6507 {
6508 const struct signatured_type *dup_tu
6509 = (const struct signatured_type *) *slot;
6510
6511 dup_sect_off = dup_tu->per_cu.sect_off;
6512 }
6513
6514 complaint (_("debug type entry at offset %s is duplicate to"
6515 " the entry at offset %s, signature %s"),
6516 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6517 hex_string (header.signature));
6518 }
6519 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6520
6521 dwarf_read_debug_printf_v (" offset %s, signature %s",
6522 sect_offset_str (sect_off),
6523 hex_string (header.signature));
6524
6525 info_ptr += length;
6526 }
6527 }
6528
6529 /* Create the hash table of all entries in the .debug_types
6530 (or .debug_types.dwo) section(s).
6531 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6532 otherwise it is NULL.
6533
6534 The result is a pointer to the hash table or NULL if there are no types.
6535
6536 Note: This function processes DWO files only, not DWP files. */
6537
6538 static void
6539 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6540 struct dwo_file *dwo_file,
6541 gdb::array_view<dwarf2_section_info> type_sections,
6542 htab_up &types_htab)
6543 {
6544 for (dwarf2_section_info &section : type_sections)
6545 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6546 rcuh_kind::TYPE);
6547 }
6548
6549 /* Create the hash table of all entries in the .debug_types section,
6550 and initialize all_type_units.
6551 The result is zero if there is an error (e.g. missing .debug_types section),
6552 otherwise non-zero. */
6553
6554 static int
6555 create_all_type_units (dwarf2_per_objfile *per_objfile)
6556 {
6557 htab_up types_htab;
6558
6559 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6560 types_htab, rcuh_kind::COMPILE);
6561 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6562 types_htab);
6563 if (types_htab == NULL)
6564 {
6565 per_objfile->per_bfd->signatured_types = NULL;
6566 return 0;
6567 }
6568
6569 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6570
6571 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6572 per_objfile->per_bfd->all_type_units.reserve
6573 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6574
6575 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6576 add_signatured_type_cu_to_table,
6577 &per_objfile->per_bfd->all_type_units);
6578
6579 return 1;
6580 }
6581
6582 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6583 If SLOT is non-NULL, it is the entry to use in the hash table.
6584 Otherwise we find one. */
6585
6586 static struct signatured_type *
6587 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6588 {
6589 if (per_objfile->per_bfd->all_type_units.size ()
6590 == per_objfile->per_bfd->all_type_units.capacity ())
6591 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6592
6593 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6594
6595 per_objfile->resize_symtabs ();
6596
6597 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6598 sig_type->signature = sig;
6599 sig_type->per_cu.is_debug_types = 1;
6600 if (per_objfile->per_bfd->using_index)
6601 {
6602 sig_type->per_cu.v.quick =
6603 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6604 struct dwarf2_per_cu_quick_data);
6605 }
6606
6607 if (slot == NULL)
6608 {
6609 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6610 sig_type, INSERT);
6611 }
6612 gdb_assert (*slot == NULL);
6613 *slot = sig_type;
6614 /* The rest of sig_type must be filled in by the caller. */
6615 return sig_type;
6616 }
6617
6618 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6619 Fill in SIG_ENTRY with DWO_ENTRY. */
6620
6621 static void
6622 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6623 struct signatured_type *sig_entry,
6624 struct dwo_unit *dwo_entry)
6625 {
6626 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6627
6628 /* Make sure we're not clobbering something we don't expect to. */
6629 gdb_assert (! sig_entry->per_cu.queued);
6630 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6631 if (per_bfd->using_index)
6632 {
6633 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6634 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6635 }
6636 else
6637 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6638 gdb_assert (sig_entry->signature == dwo_entry->signature);
6639 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6640 gdb_assert (sig_entry->type_unit_group == NULL);
6641 gdb_assert (sig_entry->dwo_unit == NULL);
6642
6643 sig_entry->per_cu.section = dwo_entry->section;
6644 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6645 sig_entry->per_cu.length = dwo_entry->length;
6646 sig_entry->per_cu.reading_dwo_directly = 1;
6647 sig_entry->per_cu.per_bfd = per_bfd;
6648 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6649 sig_entry->dwo_unit = dwo_entry;
6650 }
6651
6652 /* Subroutine of lookup_signatured_type.
6653 If we haven't read the TU yet, create the signatured_type data structure
6654 for a TU to be read in directly from a DWO file, bypassing the stub.
6655 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6656 using .gdb_index, then when reading a CU we want to stay in the DWO file
6657 containing that CU. Otherwise we could end up reading several other DWO
6658 files (due to comdat folding) to process the transitive closure of all the
6659 mentioned TUs, and that can be slow. The current DWO file will have every
6660 type signature that it needs.
6661 We only do this for .gdb_index because in the psymtab case we already have
6662 to read all the DWOs to build the type unit groups. */
6663
6664 static struct signatured_type *
6665 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6666 {
6667 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6668 struct dwo_file *dwo_file;
6669 struct dwo_unit find_dwo_entry, *dwo_entry;
6670 struct signatured_type find_sig_entry, *sig_entry;
6671 void **slot;
6672
6673 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6674
6675 /* If TU skeletons have been removed then we may not have read in any
6676 TUs yet. */
6677 if (per_objfile->per_bfd->signatured_types == NULL)
6678 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6679
6680 /* We only ever need to read in one copy of a signatured type.
6681 Use the global signatured_types array to do our own comdat-folding
6682 of types. If this is the first time we're reading this TU, and
6683 the TU has an entry in .gdb_index, replace the recorded data from
6684 .gdb_index with this TU. */
6685
6686 find_sig_entry.signature = sig;
6687 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6688 &find_sig_entry, INSERT);
6689 sig_entry = (struct signatured_type *) *slot;
6690
6691 /* We can get here with the TU already read, *or* in the process of being
6692 read. Don't reassign the global entry to point to this DWO if that's
6693 the case. Also note that if the TU is already being read, it may not
6694 have come from a DWO, the program may be a mix of Fission-compiled
6695 code and non-Fission-compiled code. */
6696
6697 /* Have we already tried to read this TU?
6698 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6699 needn't exist in the global table yet). */
6700 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6701 return sig_entry;
6702
6703 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6704 dwo_unit of the TU itself. */
6705 dwo_file = cu->dwo_unit->dwo_file;
6706
6707 /* Ok, this is the first time we're reading this TU. */
6708 if (dwo_file->tus == NULL)
6709 return NULL;
6710 find_dwo_entry.signature = sig;
6711 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6712 &find_dwo_entry);
6713 if (dwo_entry == NULL)
6714 return NULL;
6715
6716 /* If the global table doesn't have an entry for this TU, add one. */
6717 if (sig_entry == NULL)
6718 sig_entry = add_type_unit (per_objfile, sig, slot);
6719
6720 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6721 sig_entry->per_cu.tu_read = 1;
6722 return sig_entry;
6723 }
6724
6725 /* Subroutine of lookup_signatured_type.
6726 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6727 then try the DWP file. If the TU stub (skeleton) has been removed then
6728 it won't be in .gdb_index. */
6729
6730 static struct signatured_type *
6731 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6732 {
6733 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6734 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6735 struct dwo_unit *dwo_entry;
6736 struct signatured_type find_sig_entry, *sig_entry;
6737 void **slot;
6738
6739 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6740 gdb_assert (dwp_file != NULL);
6741
6742 /* If TU skeletons have been removed then we may not have read in any
6743 TUs yet. */
6744 if (per_objfile->per_bfd->signatured_types == NULL)
6745 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6746
6747 find_sig_entry.signature = sig;
6748 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6749 &find_sig_entry, INSERT);
6750 sig_entry = (struct signatured_type *) *slot;
6751
6752 /* Have we already tried to read this TU?
6753 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6754 needn't exist in the global table yet). */
6755 if (sig_entry != NULL)
6756 return sig_entry;
6757
6758 if (dwp_file->tus == NULL)
6759 return NULL;
6760 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6761 1 /* is_debug_types */);
6762 if (dwo_entry == NULL)
6763 return NULL;
6764
6765 sig_entry = add_type_unit (per_objfile, sig, slot);
6766 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6767
6768 return sig_entry;
6769 }
6770
6771 /* Lookup a signature based type for DW_FORM_ref_sig8.
6772 Returns NULL if signature SIG is not present in the table.
6773 It is up to the caller to complain about this. */
6774
6775 static struct signatured_type *
6776 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6777 {
6778 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6779
6780 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6781 {
6782 /* We're in a DWO/DWP file, and we're using .gdb_index.
6783 These cases require special processing. */
6784 if (get_dwp_file (per_objfile) == NULL)
6785 return lookup_dwo_signatured_type (cu, sig);
6786 else
6787 return lookup_dwp_signatured_type (cu, sig);
6788 }
6789 else
6790 {
6791 struct signatured_type find_entry, *entry;
6792
6793 if (per_objfile->per_bfd->signatured_types == NULL)
6794 return NULL;
6795 find_entry.signature = sig;
6796 entry = ((struct signatured_type *)
6797 htab_find (per_objfile->per_bfd->signatured_types.get (),
6798 &find_entry));
6799 return entry;
6800 }
6801 }
6802
6803 /* Low level DIE reading support. */
6804
6805 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6806
6807 static void
6808 init_cu_die_reader (struct die_reader_specs *reader,
6809 struct dwarf2_cu *cu,
6810 struct dwarf2_section_info *section,
6811 struct dwo_file *dwo_file,
6812 struct abbrev_table *abbrev_table)
6813 {
6814 gdb_assert (section->readin && section->buffer != NULL);
6815 reader->abfd = section->get_bfd_owner ();
6816 reader->cu = cu;
6817 reader->dwo_file = dwo_file;
6818 reader->die_section = section;
6819 reader->buffer = section->buffer;
6820 reader->buffer_end = section->buffer + section->size;
6821 reader->abbrev_table = abbrev_table;
6822 }
6823
6824 /* Subroutine of cutu_reader to simplify it.
6825 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6826 There's just a lot of work to do, and cutu_reader is big enough
6827 already.
6828
6829 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6830 from it to the DIE in the DWO. If NULL we are skipping the stub.
6831 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6832 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6833 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6834 STUB_COMP_DIR may be non-NULL.
6835 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6836 are filled in with the info of the DIE from the DWO file.
6837 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6838 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6839 kept around for at least as long as *RESULT_READER.
6840
6841 The result is non-zero if a valid (non-dummy) DIE was found. */
6842
6843 static int
6844 read_cutu_die_from_dwo (dwarf2_cu *cu,
6845 struct dwo_unit *dwo_unit,
6846 struct die_info *stub_comp_unit_die,
6847 const char *stub_comp_dir,
6848 struct die_reader_specs *result_reader,
6849 const gdb_byte **result_info_ptr,
6850 struct die_info **result_comp_unit_die,
6851 abbrev_table_up *result_dwo_abbrev_table)
6852 {
6853 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6854 dwarf2_per_cu_data *per_cu = cu->per_cu;
6855 struct objfile *objfile = per_objfile->objfile;
6856 bfd *abfd;
6857 const gdb_byte *begin_info_ptr, *info_ptr;
6858 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6859 int i,num_extra_attrs;
6860 struct dwarf2_section_info *dwo_abbrev_section;
6861 struct die_info *comp_unit_die;
6862
6863 /* At most one of these may be provided. */
6864 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6865
6866 /* These attributes aren't processed until later:
6867 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6868 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6869 referenced later. However, these attributes are found in the stub
6870 which we won't have later. In order to not impose this complication
6871 on the rest of the code, we read them here and copy them to the
6872 DWO CU/TU die. */
6873
6874 stmt_list = NULL;
6875 low_pc = NULL;
6876 high_pc = NULL;
6877 ranges = NULL;
6878 comp_dir = NULL;
6879
6880 if (stub_comp_unit_die != NULL)
6881 {
6882 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6883 DWO file. */
6884 if (!per_cu->is_debug_types)
6885 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6886 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6887 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6888 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6889 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6890
6891 cu->addr_base = stub_comp_unit_die->addr_base ();
6892
6893 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6894 We need the value before we can process DW_AT_ranges values from the
6895 DWO. */
6896 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6897
6898 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6899 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6900 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6901 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6902 section. */
6903 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6904 }
6905 else if (stub_comp_dir != NULL)
6906 {
6907 /* Reconstruct the comp_dir attribute to simplify the code below. */
6908 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6909 comp_dir->name = DW_AT_comp_dir;
6910 comp_dir->form = DW_FORM_string;
6911 comp_dir->set_string_noncanonical (stub_comp_dir);
6912 }
6913
6914 /* Set up for reading the DWO CU/TU. */
6915 cu->dwo_unit = dwo_unit;
6916 dwarf2_section_info *section = dwo_unit->section;
6917 section->read (objfile);
6918 abfd = section->get_bfd_owner ();
6919 begin_info_ptr = info_ptr = (section->buffer
6920 + to_underlying (dwo_unit->sect_off));
6921 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6922
6923 if (per_cu->is_debug_types)
6924 {
6925 signatured_type *sig_type = (struct signatured_type *) per_cu;
6926
6927 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6928 section, dwo_abbrev_section,
6929 info_ptr, rcuh_kind::TYPE);
6930 /* This is not an assert because it can be caused by bad debug info. */
6931 if (sig_type->signature != cu->header.signature)
6932 {
6933 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6934 " TU at offset %s [in module %s]"),
6935 hex_string (sig_type->signature),
6936 hex_string (cu->header.signature),
6937 sect_offset_str (dwo_unit->sect_off),
6938 bfd_get_filename (abfd));
6939 }
6940 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6941 /* For DWOs coming from DWP files, we don't know the CU length
6942 nor the type's offset in the TU until now. */
6943 dwo_unit->length = cu->header.get_length ();
6944 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6945
6946 /* Establish the type offset that can be used to lookup the type.
6947 For DWO files, we don't know it until now. */
6948 sig_type->type_offset_in_section
6949 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6950 }
6951 else
6952 {
6953 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6954 section, dwo_abbrev_section,
6955 info_ptr, rcuh_kind::COMPILE);
6956 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6957 /* For DWOs coming from DWP files, we don't know the CU length
6958 until now. */
6959 dwo_unit->length = cu->header.get_length ();
6960 }
6961
6962 dwo_abbrev_section->read (objfile);
6963 *result_dwo_abbrev_table
6964 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6965 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6966 result_dwo_abbrev_table->get ());
6967
6968 /* Read in the die, but leave space to copy over the attributes
6969 from the stub. This has the benefit of simplifying the rest of
6970 the code - all the work to maintain the illusion of a single
6971 DW_TAG_{compile,type}_unit DIE is done here. */
6972 num_extra_attrs = ((stmt_list != NULL)
6973 + (low_pc != NULL)
6974 + (high_pc != NULL)
6975 + (ranges != NULL)
6976 + (comp_dir != NULL));
6977 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6978 num_extra_attrs);
6979
6980 /* Copy over the attributes from the stub to the DIE we just read in. */
6981 comp_unit_die = *result_comp_unit_die;
6982 i = comp_unit_die->num_attrs;
6983 if (stmt_list != NULL)
6984 comp_unit_die->attrs[i++] = *stmt_list;
6985 if (low_pc != NULL)
6986 comp_unit_die->attrs[i++] = *low_pc;
6987 if (high_pc != NULL)
6988 comp_unit_die->attrs[i++] = *high_pc;
6989 if (ranges != NULL)
6990 comp_unit_die->attrs[i++] = *ranges;
6991 if (comp_dir != NULL)
6992 comp_unit_die->attrs[i++] = *comp_dir;
6993 comp_unit_die->num_attrs += num_extra_attrs;
6994
6995 if (dwarf_die_debug)
6996 {
6997 fprintf_unfiltered (gdb_stdlog,
6998 "Read die from %s@0x%x of %s:\n",
6999 section->get_name (),
7000 (unsigned) (begin_info_ptr - section->buffer),
7001 bfd_get_filename (abfd));
7002 dump_die (comp_unit_die, dwarf_die_debug);
7003 }
7004
7005 /* Skip dummy compilation units. */
7006 if (info_ptr >= begin_info_ptr + dwo_unit->length
7007 || peek_abbrev_code (abfd, info_ptr) == 0)
7008 return 0;
7009
7010 *result_info_ptr = info_ptr;
7011 return 1;
7012 }
7013
7014 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7015 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7016 signature is part of the header. */
7017 static gdb::optional<ULONGEST>
7018 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7019 {
7020 if (cu->header.version >= 5)
7021 return cu->header.signature;
7022 struct attribute *attr;
7023 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7024 if (attr == nullptr || !attr->form_is_unsigned ())
7025 return gdb::optional<ULONGEST> ();
7026 return attr->as_unsigned ();
7027 }
7028
7029 /* Subroutine of cutu_reader to simplify it.
7030 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7031 Returns NULL if the specified DWO unit cannot be found. */
7032
7033 static struct dwo_unit *
7034 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7035 {
7036 dwarf2_per_cu_data *per_cu = cu->per_cu;
7037 struct dwo_unit *dwo_unit;
7038 const char *comp_dir;
7039
7040 gdb_assert (cu != NULL);
7041
7042 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7043 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7044 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7045
7046 if (per_cu->is_debug_types)
7047 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7048 else
7049 {
7050 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7051
7052 if (!signature.has_value ())
7053 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7054 " [in module %s]"),
7055 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7056
7057 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7058 }
7059
7060 return dwo_unit;
7061 }
7062
7063 /* Subroutine of cutu_reader to simplify it.
7064 See it for a description of the parameters.
7065 Read a TU directly from a DWO file, bypassing the stub. */
7066
7067 void
7068 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7069 dwarf2_per_objfile *per_objfile,
7070 dwarf2_cu *existing_cu)
7071 {
7072 struct signatured_type *sig_type;
7073
7074 /* Verify we can do the following downcast, and that we have the
7075 data we need. */
7076 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7077 sig_type = (struct signatured_type *) this_cu;
7078 gdb_assert (sig_type->dwo_unit != NULL);
7079
7080 dwarf2_cu *cu;
7081
7082 if (existing_cu != nullptr)
7083 {
7084 cu = existing_cu;
7085 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7086 /* There's no need to do the rereading_dwo_cu handling that
7087 cutu_reader does since we don't read the stub. */
7088 }
7089 else
7090 {
7091 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7092 in per_objfile yet. */
7093 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7094 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7095 cu = m_new_cu.get ();
7096 }
7097
7098 /* A future optimization, if needed, would be to use an existing
7099 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7100 could share abbrev tables. */
7101
7102 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7103 NULL /* stub_comp_unit_die */,
7104 sig_type->dwo_unit->dwo_file->comp_dir,
7105 this, &info_ptr,
7106 &comp_unit_die,
7107 &m_dwo_abbrev_table) == 0)
7108 {
7109 /* Dummy die. */
7110 dummy_p = true;
7111 }
7112 }
7113
7114 /* Initialize a CU (or TU) and read its DIEs.
7115 If the CU defers to a DWO file, read the DWO file as well.
7116
7117 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7118 Otherwise the table specified in the comp unit header is read in and used.
7119 This is an optimization for when we already have the abbrev table.
7120
7121 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7122 allocated. */
7123
7124 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7125 dwarf2_per_objfile *per_objfile,
7126 struct abbrev_table *abbrev_table,
7127 dwarf2_cu *existing_cu,
7128 bool skip_partial)
7129 : die_reader_specs {},
7130 m_this_cu (this_cu)
7131 {
7132 struct objfile *objfile = per_objfile->objfile;
7133 struct dwarf2_section_info *section = this_cu->section;
7134 bfd *abfd = section->get_bfd_owner ();
7135 const gdb_byte *begin_info_ptr;
7136 struct signatured_type *sig_type = NULL;
7137 struct dwarf2_section_info *abbrev_section;
7138 /* Non-zero if CU currently points to a DWO file and we need to
7139 reread it. When this happens we need to reread the skeleton die
7140 before we can reread the DWO file (this only applies to CUs, not TUs). */
7141 int rereading_dwo_cu = 0;
7142
7143 if (dwarf_die_debug)
7144 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7145 this_cu->is_debug_types ? "type" : "comp",
7146 sect_offset_str (this_cu->sect_off));
7147
7148 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7149 file (instead of going through the stub), short-circuit all of this. */
7150 if (this_cu->reading_dwo_directly)
7151 {
7152 /* Narrow down the scope of possibilities to have to understand. */
7153 gdb_assert (this_cu->is_debug_types);
7154 gdb_assert (abbrev_table == NULL);
7155 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7156 return;
7157 }
7158
7159 /* This is cheap if the section is already read in. */
7160 section->read (objfile);
7161
7162 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7163
7164 abbrev_section = get_abbrev_section_for_cu (this_cu);
7165
7166 dwarf2_cu *cu;
7167
7168 if (existing_cu != nullptr)
7169 {
7170 cu = existing_cu;
7171 /* If this CU is from a DWO file we need to start over, we need to
7172 refetch the attributes from the skeleton CU.
7173 This could be optimized by retrieving those attributes from when we
7174 were here the first time: the previous comp_unit_die was stored in
7175 comp_unit_obstack. But there's no data yet that we need this
7176 optimization. */
7177 if (cu->dwo_unit != NULL)
7178 rereading_dwo_cu = 1;
7179 }
7180 else
7181 {
7182 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7183 in per_objfile yet. */
7184 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7185 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7186 cu = m_new_cu.get ();
7187 }
7188
7189 /* Get the header. */
7190 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7191 {
7192 /* We already have the header, there's no need to read it in again. */
7193 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7194 }
7195 else
7196 {
7197 if (this_cu->is_debug_types)
7198 {
7199 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7200 section, abbrev_section,
7201 info_ptr, rcuh_kind::TYPE);
7202
7203 /* Since per_cu is the first member of struct signatured_type,
7204 we can go from a pointer to one to a pointer to the other. */
7205 sig_type = (struct signatured_type *) this_cu;
7206 gdb_assert (sig_type->signature == cu->header.signature);
7207 gdb_assert (sig_type->type_offset_in_tu
7208 == cu->header.type_cu_offset_in_tu);
7209 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7210
7211 /* LENGTH has not been set yet for type units if we're
7212 using .gdb_index. */
7213 this_cu->length = cu->header.get_length ();
7214
7215 /* Establish the type offset that can be used to lookup the type. */
7216 sig_type->type_offset_in_section =
7217 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7218
7219 this_cu->dwarf_version = cu->header.version;
7220 }
7221 else
7222 {
7223 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7224 section, abbrev_section,
7225 info_ptr,
7226 rcuh_kind::COMPILE);
7227
7228 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7229 if (this_cu->length == 0)
7230 this_cu->length = cu->header.get_length ();
7231 else
7232 gdb_assert (this_cu->length == cu->header.get_length ());
7233 this_cu->dwarf_version = cu->header.version;
7234 }
7235 }
7236
7237 /* Skip dummy compilation units. */
7238 if (info_ptr >= begin_info_ptr + this_cu->length
7239 || peek_abbrev_code (abfd, info_ptr) == 0)
7240 {
7241 dummy_p = true;
7242 return;
7243 }
7244
7245 /* If we don't have them yet, read the abbrevs for this compilation unit.
7246 And if we need to read them now, make sure they're freed when we're
7247 done. */
7248 if (abbrev_table != NULL)
7249 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7250 else
7251 {
7252 abbrev_section->read (objfile);
7253 m_abbrev_table_holder
7254 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7255 abbrev_table = m_abbrev_table_holder.get ();
7256 }
7257
7258 /* Read the top level CU/TU die. */
7259 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7260 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7261
7262 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7263 {
7264 dummy_p = true;
7265 return;
7266 }
7267
7268 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7269 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7270 table from the DWO file and pass the ownership over to us. It will be
7271 referenced from READER, so we must make sure to free it after we're done
7272 with READER.
7273
7274 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7275 DWO CU, that this test will fail (the attribute will not be present). */
7276 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7277 if (dwo_name != nullptr)
7278 {
7279 struct dwo_unit *dwo_unit;
7280 struct die_info *dwo_comp_unit_die;
7281
7282 if (comp_unit_die->has_children)
7283 {
7284 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7285 " has children (offset %s) [in module %s]"),
7286 sect_offset_str (this_cu->sect_off),
7287 bfd_get_filename (abfd));
7288 }
7289 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7290 if (dwo_unit != NULL)
7291 {
7292 if (read_cutu_die_from_dwo (cu, dwo_unit,
7293 comp_unit_die, NULL,
7294 this, &info_ptr,
7295 &dwo_comp_unit_die,
7296 &m_dwo_abbrev_table) == 0)
7297 {
7298 /* Dummy die. */
7299 dummy_p = true;
7300 return;
7301 }
7302 comp_unit_die = dwo_comp_unit_die;
7303 }
7304 else
7305 {
7306 /* Yikes, we couldn't find the rest of the DIE, we only have
7307 the stub. A complaint has already been logged. There's
7308 not much more we can do except pass on the stub DIE to
7309 die_reader_func. We don't want to throw an error on bad
7310 debug info. */
7311 }
7312 }
7313 }
7314
7315 void
7316 cutu_reader::keep ()
7317 {
7318 /* Done, clean up. */
7319 gdb_assert (!dummy_p);
7320 if (m_new_cu != NULL)
7321 {
7322 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7323 now. */
7324 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7325 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7326 }
7327 }
7328
7329 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7330 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7331 assumed to have already done the lookup to find the DWO file).
7332
7333 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7334 THIS_CU->is_debug_types, but nothing else.
7335
7336 We fill in THIS_CU->length.
7337
7338 THIS_CU->cu is always freed when done.
7339 This is done in order to not leave THIS_CU->cu in a state where we have
7340 to care whether it refers to the "main" CU or the DWO CU.
7341
7342 When parent_cu is passed, it is used to provide a default value for
7343 str_offsets_base and addr_base from the parent. */
7344
7345 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7346 dwarf2_per_objfile *per_objfile,
7347 struct dwarf2_cu *parent_cu,
7348 struct dwo_file *dwo_file)
7349 : die_reader_specs {},
7350 m_this_cu (this_cu)
7351 {
7352 struct objfile *objfile = per_objfile->objfile;
7353 struct dwarf2_section_info *section = this_cu->section;
7354 bfd *abfd = section->get_bfd_owner ();
7355 struct dwarf2_section_info *abbrev_section;
7356 const gdb_byte *begin_info_ptr, *info_ptr;
7357
7358 if (dwarf_die_debug)
7359 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7360 this_cu->is_debug_types ? "type" : "comp",
7361 sect_offset_str (this_cu->sect_off));
7362
7363 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7364
7365 abbrev_section = (dwo_file != NULL
7366 ? &dwo_file->sections.abbrev
7367 : get_abbrev_section_for_cu (this_cu));
7368
7369 /* This is cheap if the section is already read in. */
7370 section->read (objfile);
7371
7372 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7373
7374 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7375 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7376 section, abbrev_section, info_ptr,
7377 (this_cu->is_debug_types
7378 ? rcuh_kind::TYPE
7379 : rcuh_kind::COMPILE));
7380
7381 if (parent_cu != nullptr)
7382 {
7383 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7384 m_new_cu->addr_base = parent_cu->addr_base;
7385 }
7386 this_cu->length = m_new_cu->header.get_length ();
7387
7388 /* Skip dummy compilation units. */
7389 if (info_ptr >= begin_info_ptr + this_cu->length
7390 || peek_abbrev_code (abfd, info_ptr) == 0)
7391 {
7392 dummy_p = true;
7393 return;
7394 }
7395
7396 abbrev_section->read (objfile);
7397 m_abbrev_table_holder
7398 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7399
7400 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7401 m_abbrev_table_holder.get ());
7402 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7403 }
7404
7405 \f
7406 /* Type Unit Groups.
7407
7408 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7409 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7410 so that all types coming from the same compilation (.o file) are grouped
7411 together. A future step could be to put the types in the same symtab as
7412 the CU the types ultimately came from. */
7413
7414 static hashval_t
7415 hash_type_unit_group (const void *item)
7416 {
7417 const struct type_unit_group *tu_group
7418 = (const struct type_unit_group *) item;
7419
7420 return hash_stmt_list_entry (&tu_group->hash);
7421 }
7422
7423 static int
7424 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7425 {
7426 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7427 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7428
7429 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7430 }
7431
7432 /* Allocate a hash table for type unit groups. */
7433
7434 static htab_up
7435 allocate_type_unit_groups_table ()
7436 {
7437 return htab_up (htab_create_alloc (3,
7438 hash_type_unit_group,
7439 eq_type_unit_group,
7440 NULL, xcalloc, xfree));
7441 }
7442
7443 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7444 partial symtabs. We combine several TUs per psymtab to not let the size
7445 of any one psymtab grow too big. */
7446 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7447 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7448
7449 /* Helper routine for get_type_unit_group.
7450 Create the type_unit_group object used to hold one or more TUs. */
7451
7452 static struct type_unit_group *
7453 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7454 {
7455 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7456 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7457 struct dwarf2_per_cu_data *per_cu;
7458 struct type_unit_group *tu_group;
7459
7460 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7461 per_cu = &tu_group->per_cu;
7462 per_cu->per_bfd = per_bfd;
7463
7464 if (per_bfd->using_index)
7465 {
7466 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7467 struct dwarf2_per_cu_quick_data);
7468 }
7469 else
7470 {
7471 unsigned int line_offset = to_underlying (line_offset_struct);
7472 dwarf2_psymtab *pst;
7473 std::string name;
7474
7475 /* Give the symtab a useful name for debug purposes. */
7476 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7477 name = string_printf ("<type_units_%d>",
7478 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7479 else
7480 name = string_printf ("<type_units_at_0x%x>", line_offset);
7481
7482 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7483 pst->anonymous = true;
7484 }
7485
7486 tu_group->hash.dwo_unit = cu->dwo_unit;
7487 tu_group->hash.line_sect_off = line_offset_struct;
7488
7489 return tu_group;
7490 }
7491
7492 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7493 STMT_LIST is a DW_AT_stmt_list attribute. */
7494
7495 static struct type_unit_group *
7496 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7497 {
7498 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7499 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7500 struct type_unit_group *tu_group;
7501 void **slot;
7502 unsigned int line_offset;
7503 struct type_unit_group type_unit_group_for_lookup;
7504
7505 if (per_objfile->per_bfd->type_unit_groups == NULL)
7506 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7507
7508 /* Do we need to create a new group, or can we use an existing one? */
7509
7510 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7511 {
7512 line_offset = stmt_list->as_unsigned ();
7513 ++tu_stats->nr_symtab_sharers;
7514 }
7515 else
7516 {
7517 /* Ugh, no stmt_list. Rare, but we have to handle it.
7518 We can do various things here like create one group per TU or
7519 spread them over multiple groups to split up the expansion work.
7520 To avoid worst case scenarios (too many groups or too large groups)
7521 we, umm, group them in bunches. */
7522 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7523 | (tu_stats->nr_stmt_less_type_units
7524 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7525 ++tu_stats->nr_stmt_less_type_units;
7526 }
7527
7528 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7529 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7530 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7531 &type_unit_group_for_lookup, INSERT);
7532 if (*slot != NULL)
7533 {
7534 tu_group = (struct type_unit_group *) *slot;
7535 gdb_assert (tu_group != NULL);
7536 }
7537 else
7538 {
7539 sect_offset line_offset_struct = (sect_offset) line_offset;
7540 tu_group = create_type_unit_group (cu, line_offset_struct);
7541 *slot = tu_group;
7542 ++tu_stats->nr_symtabs;
7543 }
7544
7545 return tu_group;
7546 }
7547 \f
7548 /* Partial symbol tables. */
7549
7550 /* Create a psymtab named NAME and assign it to PER_CU.
7551
7552 The caller must fill in the following details:
7553 dirname, textlow, texthigh. */
7554
7555 static dwarf2_psymtab *
7556 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7557 dwarf2_per_objfile *per_objfile,
7558 const char *name)
7559 {
7560 struct objfile *objfile = per_objfile->objfile;
7561 dwarf2_psymtab *pst;
7562
7563 pst = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7564 objfile, per_cu);
7565
7566 pst->psymtabs_addrmap_supported = true;
7567
7568 /* This is the glue that links PST into GDB's symbol API. */
7569 per_cu->v.psymtab = pst;
7570
7571 return pst;
7572 }
7573
7574 /* DIE reader function for process_psymtab_comp_unit. */
7575
7576 static void
7577 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7578 const gdb_byte *info_ptr,
7579 struct die_info *comp_unit_die,
7580 enum language pretend_language)
7581 {
7582 struct dwarf2_cu *cu = reader->cu;
7583 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7584 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7585 struct objfile *objfile = per_objfile->objfile;
7586 struct gdbarch *gdbarch = objfile->arch ();
7587 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7588 CORE_ADDR baseaddr;
7589 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7590 dwarf2_psymtab *pst;
7591 enum pc_bounds_kind cu_bounds_kind;
7592 const char *filename;
7593
7594 gdb_assert (! per_cu->is_debug_types);
7595
7596 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7597
7598 /* Allocate a new partial symbol table structure. */
7599 gdb::unique_xmalloc_ptr<char> debug_filename;
7600 static const char artificial[] = "<artificial>";
7601 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7602 if (filename == NULL)
7603 filename = "";
7604 else if (strcmp (filename, artificial) == 0)
7605 {
7606 debug_filename.reset (concat (artificial, "@",
7607 sect_offset_str (per_cu->sect_off),
7608 (char *) NULL));
7609 filename = debug_filename.get ();
7610 }
7611
7612 pst = create_partial_symtab (per_cu, per_objfile, filename);
7613
7614 /* This must be done before calling dwarf2_build_include_psymtabs. */
7615 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7616
7617 baseaddr = objfile->text_section_offset ();
7618
7619 dwarf2_find_base_address (comp_unit_die, cu);
7620
7621 /* Possibly set the default values of LOWPC and HIGHPC from
7622 `DW_AT_ranges'. */
7623 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7624 &best_highpc, cu, pst);
7625 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7626 {
7627 CORE_ADDR low
7628 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7629 - baseaddr);
7630 CORE_ADDR high
7631 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7632 - baseaddr - 1);
7633 /* Store the contiguous range if it is not empty; it can be
7634 empty for CUs with no code. */
7635 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7636 low, high, pst);
7637 }
7638
7639 /* Check if comp unit has_children.
7640 If so, read the rest of the partial symbols from this comp unit.
7641 If not, there's no more debug_info for this comp unit. */
7642 if (comp_unit_die->has_children)
7643 {
7644 struct partial_die_info *first_die;
7645 CORE_ADDR lowpc, highpc;
7646
7647 lowpc = ((CORE_ADDR) -1);
7648 highpc = ((CORE_ADDR) 0);
7649
7650 first_die = load_partial_dies (reader, info_ptr, 1);
7651
7652 scan_partial_symbols (first_die, &lowpc, &highpc,
7653 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7654
7655 /* If we didn't find a lowpc, set it to highpc to avoid
7656 complaints from `maint check'. */
7657 if (lowpc == ((CORE_ADDR) -1))
7658 lowpc = highpc;
7659
7660 /* If the compilation unit didn't have an explicit address range,
7661 then use the information extracted from its child dies. */
7662 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7663 {
7664 best_lowpc = lowpc;
7665 best_highpc = highpc;
7666 }
7667 }
7668 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7669 best_lowpc + baseaddr)
7670 - baseaddr);
7671 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7672 best_highpc + baseaddr)
7673 - baseaddr);
7674
7675 pst->end ();
7676
7677 if (!cu->per_cu->imported_symtabs_empty ())
7678 {
7679 int i;
7680 int len = cu->per_cu->imported_symtabs_size ();
7681
7682 /* Fill in 'dependencies' here; we fill in 'users' in a
7683 post-pass. */
7684 pst->number_of_dependencies = len;
7685 pst->dependencies
7686 = per_bfd->partial_symtabs->allocate_dependencies (len);
7687 for (i = 0; i < len; ++i)
7688 {
7689 pst->dependencies[i]
7690 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7691 }
7692
7693 cu->per_cu->imported_symtabs_free ();
7694 }
7695
7696 /* Get the list of files included in the current compilation unit,
7697 and build a psymtab for each of them. */
7698 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7699
7700 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7701 ", %d global, %d static syms",
7702 per_cu->is_debug_types ? "type" : "comp",
7703 sect_offset_str (per_cu->sect_off),
7704 paddress (gdbarch, pst->text_low (objfile)),
7705 paddress (gdbarch, pst->text_high (objfile)),
7706 (int) pst->global_psymbols.size (),
7707 (int) pst->static_psymbols.size ());
7708 }
7709
7710 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7711 Process compilation unit THIS_CU for a psymtab. */
7712
7713 static void
7714 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7715 dwarf2_per_objfile *per_objfile,
7716 bool want_partial_unit,
7717 enum language pretend_language)
7718 {
7719 /* If this compilation unit was already read in, free the
7720 cached copy in order to read it in again. This is
7721 necessary because we skipped some symbols when we first
7722 read in the compilation unit (see load_partial_dies).
7723 This problem could be avoided, but the benefit is unclear. */
7724 per_objfile->remove_cu (this_cu);
7725
7726 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7727
7728 switch (reader.comp_unit_die->tag)
7729 {
7730 case DW_TAG_compile_unit:
7731 this_cu->unit_type = DW_UT_compile;
7732 break;
7733 case DW_TAG_partial_unit:
7734 this_cu->unit_type = DW_UT_partial;
7735 break;
7736 case DW_TAG_type_unit:
7737 this_cu->unit_type = DW_UT_type;
7738 break;
7739 default:
7740 abort ();
7741 }
7742
7743 if (reader.dummy_p)
7744 {
7745 /* Nothing. */
7746 }
7747 else if (this_cu->is_debug_types)
7748 build_type_psymtabs_reader (&reader, reader.info_ptr,
7749 reader.comp_unit_die);
7750 else if (want_partial_unit
7751 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7752 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7753 reader.comp_unit_die,
7754 pretend_language);
7755
7756 this_cu->lang = reader.cu->language;
7757
7758 /* Age out any secondary CUs. */
7759 per_objfile->age_comp_units ();
7760 }
7761
7762 /* Reader function for build_type_psymtabs. */
7763
7764 static void
7765 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7766 const gdb_byte *info_ptr,
7767 struct die_info *type_unit_die)
7768 {
7769 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7770 struct dwarf2_cu *cu = reader->cu;
7771 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7772 struct signatured_type *sig_type;
7773 struct type_unit_group *tu_group;
7774 struct attribute *attr;
7775 struct partial_die_info *first_die;
7776 CORE_ADDR lowpc, highpc;
7777 dwarf2_psymtab *pst;
7778
7779 gdb_assert (per_cu->is_debug_types);
7780 sig_type = (struct signatured_type *) per_cu;
7781
7782 if (! type_unit_die->has_children)
7783 return;
7784
7785 attr = type_unit_die->attr (DW_AT_stmt_list);
7786 tu_group = get_type_unit_group (cu, attr);
7787
7788 if (tu_group->tus == nullptr)
7789 tu_group->tus = new std::vector<signatured_type *>;
7790 tu_group->tus->push_back (sig_type);
7791
7792 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7793 pst = create_partial_symtab (per_cu, per_objfile, "");
7794 pst->anonymous = true;
7795
7796 first_die = load_partial_dies (reader, info_ptr, 1);
7797
7798 lowpc = (CORE_ADDR) -1;
7799 highpc = (CORE_ADDR) 0;
7800 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7801
7802 pst->end ();
7803 }
7804
7805 /* Struct used to sort TUs by their abbreviation table offset. */
7806
7807 struct tu_abbrev_offset
7808 {
7809 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7810 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7811 {}
7812
7813 signatured_type *sig_type;
7814 sect_offset abbrev_offset;
7815 };
7816
7817 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7818
7819 static bool
7820 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7821 const struct tu_abbrev_offset &b)
7822 {
7823 return a.abbrev_offset < b.abbrev_offset;
7824 }
7825
7826 /* Efficiently read all the type units.
7827 This does the bulk of the work for build_type_psymtabs.
7828
7829 The efficiency is because we sort TUs by the abbrev table they use and
7830 only read each abbrev table once. In one program there are 200K TUs
7831 sharing 8K abbrev tables.
7832
7833 The main purpose of this function is to support building the
7834 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7835 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7836 can collapse the search space by grouping them by stmt_list.
7837 The savings can be significant, in the same program from above the 200K TUs
7838 share 8K stmt_list tables.
7839
7840 FUNC is expected to call get_type_unit_group, which will create the
7841 struct type_unit_group if necessary and add it to
7842 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7843
7844 static void
7845 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7846 {
7847 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7848 abbrev_table_up abbrev_table;
7849 sect_offset abbrev_offset;
7850
7851 /* It's up to the caller to not call us multiple times. */
7852 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7853
7854 if (per_objfile->per_bfd->all_type_units.empty ())
7855 return;
7856
7857 /* TUs typically share abbrev tables, and there can be way more TUs than
7858 abbrev tables. Sort by abbrev table to reduce the number of times we
7859 read each abbrev table in.
7860 Alternatives are to punt or to maintain a cache of abbrev tables.
7861 This is simpler and efficient enough for now.
7862
7863 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7864 symtab to use). Typically TUs with the same abbrev offset have the same
7865 stmt_list value too so in practice this should work well.
7866
7867 The basic algorithm here is:
7868
7869 sort TUs by abbrev table
7870 for each TU with same abbrev table:
7871 read abbrev table if first user
7872 read TU top level DIE
7873 [IWBN if DWO skeletons had DW_AT_stmt_list]
7874 call FUNC */
7875
7876 dwarf_read_debug_printf ("Building type unit groups ...");
7877
7878 /* Sort in a separate table to maintain the order of all_type_units
7879 for .gdb_index: TU indices directly index all_type_units. */
7880 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7881 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7882
7883 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7884 sorted_by_abbrev.emplace_back
7885 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7886 sig_type->per_cu.sect_off));
7887
7888 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7889 sort_tu_by_abbrev_offset);
7890
7891 abbrev_offset = (sect_offset) ~(unsigned) 0;
7892
7893 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7894 {
7895 /* Switch to the next abbrev table if necessary. */
7896 if (abbrev_table == NULL
7897 || tu.abbrev_offset != abbrev_offset)
7898 {
7899 abbrev_offset = tu.abbrev_offset;
7900 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7901 abbrev_table =
7902 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7903 ++tu_stats->nr_uniq_abbrev_tables;
7904 }
7905
7906 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7907 abbrev_table.get (), nullptr, false);
7908 if (!reader.dummy_p)
7909 build_type_psymtabs_reader (&reader, reader.info_ptr,
7910 reader.comp_unit_die);
7911 }
7912 }
7913
7914 /* Print collected type unit statistics. */
7915
7916 static void
7917 print_tu_stats (dwarf2_per_objfile *per_objfile)
7918 {
7919 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7920
7921 dwarf_read_debug_printf ("Type unit statistics:");
7922 dwarf_read_debug_printf (" %zu TUs",
7923 per_objfile->per_bfd->all_type_units.size ());
7924 dwarf_read_debug_printf (" %d uniq abbrev tables",
7925 tu_stats->nr_uniq_abbrev_tables);
7926 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7927 tu_stats->nr_symtabs);
7928 dwarf_read_debug_printf (" %d symtab sharers",
7929 tu_stats->nr_symtab_sharers);
7930 dwarf_read_debug_printf (" %d type units without a stmt_list",
7931 tu_stats->nr_stmt_less_type_units);
7932 dwarf_read_debug_printf (" %d all_type_units reallocs",
7933 tu_stats->nr_all_type_units_reallocs);
7934 }
7935
7936 /* Traversal function for build_type_psymtabs. */
7937
7938 static int
7939 build_type_psymtab_dependencies (void **slot, void *info)
7940 {
7941 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7942 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7943 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7944 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7945 dwarf2_psymtab *pst = per_cu->v.psymtab;
7946 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7947 int i;
7948
7949 gdb_assert (len > 0);
7950 gdb_assert (per_cu->type_unit_group_p ());
7951
7952 pst->number_of_dependencies = len;
7953 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7954 for (i = 0; i < len; ++i)
7955 {
7956 struct signatured_type *iter = tu_group->tus->at (i);
7957 gdb_assert (iter->per_cu.is_debug_types);
7958 pst->dependencies[i] = iter->per_cu.v.psymtab;
7959 iter->type_unit_group = tu_group;
7960 }
7961
7962 delete tu_group->tus;
7963 tu_group->tus = nullptr;
7964
7965 return 1;
7966 }
7967
7968 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7969 Build partial symbol tables for the .debug_types comp-units. */
7970
7971 static void
7972 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7973 {
7974 if (! create_all_type_units (per_objfile))
7975 return;
7976
7977 build_type_psymtabs_1 (per_objfile);
7978 }
7979
7980 /* Traversal function for process_skeletonless_type_unit.
7981 Read a TU in a DWO file and build partial symbols for it. */
7982
7983 static int
7984 process_skeletonless_type_unit (void **slot, void *info)
7985 {
7986 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7987 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7988 struct signatured_type find_entry, *entry;
7989
7990 /* If this TU doesn't exist in the global table, add it and read it in. */
7991
7992 if (per_objfile->per_bfd->signatured_types == NULL)
7993 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7994
7995 find_entry.signature = dwo_unit->signature;
7996 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7997 &find_entry, INSERT);
7998 /* If we've already seen this type there's nothing to do. What's happening
7999 is we're doing our own version of comdat-folding here. */
8000 if (*slot != NULL)
8001 return 1;
8002
8003 /* This does the job that create_all_type_units would have done for
8004 this TU. */
8005 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8006 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
8007 *slot = entry;
8008
8009 /* This does the job that build_type_psymtabs_1 would have done. */
8010 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
8011 if (!reader.dummy_p)
8012 build_type_psymtabs_reader (&reader, reader.info_ptr,
8013 reader.comp_unit_die);
8014
8015 return 1;
8016 }
8017
8018 /* Traversal function for process_skeletonless_type_units. */
8019
8020 static int
8021 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8022 {
8023 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8024
8025 if (dwo_file->tus != NULL)
8026 htab_traverse_noresize (dwo_file->tus.get (),
8027 process_skeletonless_type_unit, info);
8028
8029 return 1;
8030 }
8031
8032 /* Scan all TUs of DWO files, verifying we've processed them.
8033 This is needed in case a TU was emitted without its skeleton.
8034 Note: This can't be done until we know what all the DWO files are. */
8035
8036 static void
8037 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8038 {
8039 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8040 if (get_dwp_file (per_objfile) == NULL
8041 && per_objfile->per_bfd->dwo_files != NULL)
8042 {
8043 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8044 process_dwo_file_for_skeletonless_type_units,
8045 per_objfile);
8046 }
8047 }
8048
8049 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8050
8051 static void
8052 set_partial_user (dwarf2_per_objfile *per_objfile)
8053 {
8054 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8055 {
8056 dwarf2_psymtab *pst = per_cu->v.psymtab;
8057
8058 if (pst == NULL)
8059 continue;
8060
8061 for (int j = 0; j < pst->number_of_dependencies; ++j)
8062 {
8063 /* Set the 'user' field only if it is not already set. */
8064 if (pst->dependencies[j]->user == NULL)
8065 pst->dependencies[j]->user = pst;
8066 }
8067 }
8068 }
8069
8070 /* Build the partial symbol table by doing a quick pass through the
8071 .debug_info and .debug_abbrev sections. */
8072
8073 static void
8074 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8075 {
8076 struct objfile *objfile = per_objfile->objfile;
8077 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
8078
8079 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8080 objfile_name (objfile));
8081
8082 scoped_restore restore_reading_psyms
8083 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8084 true);
8085
8086 per_bfd->info.read (objfile);
8087
8088 /* Any cached compilation units will be linked by the per-objfile
8089 read_in_chain. Make sure to free them when we're done. */
8090 free_cached_comp_units freer (per_objfile);
8091
8092 build_type_psymtabs (per_objfile);
8093
8094 create_all_comp_units (per_objfile);
8095
8096 /* Create a temporary address map on a temporary obstack. We later
8097 copy this to the final obstack. */
8098 auto_obstack temp_obstack;
8099
8100 scoped_restore save_psymtabs_addrmap
8101 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
8102 addrmap_create_mutable (&temp_obstack));
8103
8104 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
8105 {
8106 if (per_cu->v.psymtab != NULL)
8107 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8108 continue;
8109 process_psymtab_comp_unit (per_cu, per_objfile, false,
8110 language_minimal);
8111 }
8112
8113 /* This has to wait until we read the CUs, we need the list of DWOs. */
8114 process_skeletonless_type_units (per_objfile);
8115
8116 /* Now that all TUs have been processed we can fill in the dependencies. */
8117 if (per_bfd->type_unit_groups != NULL)
8118 {
8119 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
8120 build_type_psymtab_dependencies, per_objfile);
8121 }
8122
8123 if (dwarf_read_debug > 0)
8124 print_tu_stats (per_objfile);
8125
8126 set_partial_user (per_objfile);
8127
8128 per_bfd->partial_symtabs->psymtabs_addrmap
8129 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
8130 per_bfd->partial_symtabs->obstack ());
8131 /* At this point we want to keep the address map. */
8132 save_psymtabs_addrmap.release ();
8133
8134 dwarf_read_debug_printf ("Done building psymtabs of %s",
8135 objfile_name (objfile));
8136 }
8137
8138 /* Load the partial DIEs for a secondary CU into memory.
8139 This is also used when rereading a primary CU with load_all_dies. */
8140
8141 static void
8142 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8143 dwarf2_per_objfile *per_objfile,
8144 dwarf2_cu *existing_cu)
8145 {
8146 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8147
8148 if (!reader.dummy_p)
8149 {
8150 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8151 language_minimal);
8152
8153 /* Check if comp unit has_children.
8154 If so, read the rest of the partial symbols from this comp unit.
8155 If not, there's no more debug_info for this comp unit. */
8156 if (reader.comp_unit_die->has_children)
8157 load_partial_dies (&reader, reader.info_ptr, 0);
8158
8159 reader.keep ();
8160 }
8161 }
8162
8163 static void
8164 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8165 struct dwarf2_section_info *section,
8166 struct dwarf2_section_info *abbrev_section,
8167 unsigned int is_dwz)
8168 {
8169 const gdb_byte *info_ptr;
8170 struct objfile *objfile = per_objfile->objfile;
8171
8172 dwarf_read_debug_printf ("Reading %s for %s",
8173 section->get_name (),
8174 section->get_file_name ());
8175
8176 section->read (objfile);
8177
8178 info_ptr = section->buffer;
8179
8180 while (info_ptr < section->buffer + section->size)
8181 {
8182 struct dwarf2_per_cu_data *this_cu;
8183
8184 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8185
8186 comp_unit_head cu_header;
8187 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8188 abbrev_section, info_ptr,
8189 rcuh_kind::COMPILE);
8190
8191 /* Save the compilation unit for later lookup. */
8192 if (cu_header.unit_type != DW_UT_type)
8193 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8194 else
8195 {
8196 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8197 sig_type->signature = cu_header.signature;
8198 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8199 this_cu = &sig_type->per_cu;
8200 }
8201 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8202 this_cu->sect_off = sect_off;
8203 this_cu->length = cu_header.length + cu_header.initial_length_size;
8204 this_cu->is_dwz = is_dwz;
8205 this_cu->section = section;
8206
8207 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8208
8209 info_ptr = info_ptr + this_cu->length;
8210 }
8211 }
8212
8213 /* Create a list of all compilation units in OBJFILE.
8214 This is only done for -readnow and building partial symtabs. */
8215
8216 static void
8217 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8218 {
8219 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8220 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8221 &per_objfile->per_bfd->abbrev, 0);
8222
8223 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8224 if (dwz != NULL)
8225 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8226 }
8227
8228 /* Process all loaded DIEs for compilation unit CU, starting at
8229 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8230 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8231 DW_AT_ranges). See the comments of add_partial_subprogram on how
8232 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8233
8234 static void
8235 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8236 CORE_ADDR *highpc, int set_addrmap,
8237 struct dwarf2_cu *cu)
8238 {
8239 struct partial_die_info *pdi;
8240
8241 /* Now, march along the PDI's, descending into ones which have
8242 interesting children but skipping the children of the other ones,
8243 until we reach the end of the compilation unit. */
8244
8245 pdi = first_die;
8246
8247 while (pdi != NULL)
8248 {
8249 pdi->fixup (cu);
8250
8251 /* Anonymous namespaces or modules have no name but have interesting
8252 children, so we need to look at them. Ditto for anonymous
8253 enums. */
8254
8255 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8256 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8257 || pdi->tag == DW_TAG_imported_unit
8258 || pdi->tag == DW_TAG_inlined_subroutine)
8259 {
8260 switch (pdi->tag)
8261 {
8262 case DW_TAG_subprogram:
8263 case DW_TAG_inlined_subroutine:
8264 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8265 if (cu->language == language_cplus)
8266 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8267 set_addrmap, cu);
8268 break;
8269 case DW_TAG_constant:
8270 case DW_TAG_variable:
8271 case DW_TAG_typedef:
8272 case DW_TAG_union_type:
8273 if (!pdi->is_declaration
8274 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8275 {
8276 add_partial_symbol (pdi, cu);
8277 }
8278 break;
8279 case DW_TAG_class_type:
8280 case DW_TAG_interface_type:
8281 case DW_TAG_structure_type:
8282 if (!pdi->is_declaration)
8283 {
8284 add_partial_symbol (pdi, cu);
8285 }
8286 if ((cu->language == language_rust
8287 || cu->language == language_cplus) && pdi->has_children)
8288 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8289 set_addrmap, cu);
8290 break;
8291 case DW_TAG_enumeration_type:
8292 if (!pdi->is_declaration)
8293 add_partial_enumeration (pdi, cu);
8294 break;
8295 case DW_TAG_base_type:
8296 case DW_TAG_subrange_type:
8297 /* File scope base type definitions are added to the partial
8298 symbol table. */
8299 add_partial_symbol (pdi, cu);
8300 break;
8301 case DW_TAG_namespace:
8302 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8303 break;
8304 case DW_TAG_module:
8305 if (!pdi->is_declaration)
8306 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8307 break;
8308 case DW_TAG_imported_unit:
8309 {
8310 struct dwarf2_per_cu_data *per_cu;
8311
8312 /* For now we don't handle imported units in type units. */
8313 if (cu->per_cu->is_debug_types)
8314 {
8315 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8316 " supported in type units [in module %s]"),
8317 objfile_name (cu->per_objfile->objfile));
8318 }
8319
8320 per_cu = dwarf2_find_containing_comp_unit
8321 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8322
8323 /* Go read the partial unit, if needed. */
8324 if (per_cu->v.psymtab == NULL)
8325 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8326 cu->language);
8327
8328 cu->per_cu->imported_symtabs_push (per_cu);
8329 }
8330 break;
8331 case DW_TAG_imported_declaration:
8332 add_partial_symbol (pdi, cu);
8333 break;
8334 default:
8335 break;
8336 }
8337 }
8338
8339 /* If the die has a sibling, skip to the sibling. */
8340
8341 pdi = pdi->die_sibling;
8342 }
8343 }
8344
8345 /* Functions used to compute the fully scoped name of a partial DIE.
8346
8347 Normally, this is simple. For C++, the parent DIE's fully scoped
8348 name is concatenated with "::" and the partial DIE's name.
8349 Enumerators are an exception; they use the scope of their parent
8350 enumeration type, i.e. the name of the enumeration type is not
8351 prepended to the enumerator.
8352
8353 There are two complexities. One is DW_AT_specification; in this
8354 case "parent" means the parent of the target of the specification,
8355 instead of the direct parent of the DIE. The other is compilers
8356 which do not emit DW_TAG_namespace; in this case we try to guess
8357 the fully qualified name of structure types from their members'
8358 linkage names. This must be done using the DIE's children rather
8359 than the children of any DW_AT_specification target. We only need
8360 to do this for structures at the top level, i.e. if the target of
8361 any DW_AT_specification (if any; otherwise the DIE itself) does not
8362 have a parent. */
8363
8364 /* Compute the scope prefix associated with PDI's parent, in
8365 compilation unit CU. The result will be allocated on CU's
8366 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8367 field. NULL is returned if no prefix is necessary. */
8368 static const char *
8369 partial_die_parent_scope (struct partial_die_info *pdi,
8370 struct dwarf2_cu *cu)
8371 {
8372 const char *grandparent_scope;
8373 struct partial_die_info *parent, *real_pdi;
8374
8375 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8376 then this means the parent of the specification DIE. */
8377
8378 real_pdi = pdi;
8379 while (real_pdi->has_specification)
8380 {
8381 auto res = find_partial_die (real_pdi->spec_offset,
8382 real_pdi->spec_is_dwz, cu);
8383 real_pdi = res.pdi;
8384 cu = res.cu;
8385 }
8386
8387 parent = real_pdi->die_parent;
8388 if (parent == NULL)
8389 return NULL;
8390
8391 if (parent->scope_set)
8392 return parent->scope;
8393
8394 parent->fixup (cu);
8395
8396 grandparent_scope = partial_die_parent_scope (parent, cu);
8397
8398 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8399 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8400 Work around this problem here. */
8401 if (cu->language == language_cplus
8402 && parent->tag == DW_TAG_namespace
8403 && strcmp (parent->name (cu), "::") == 0
8404 && grandparent_scope == NULL)
8405 {
8406 parent->scope = NULL;
8407 parent->scope_set = 1;
8408 return NULL;
8409 }
8410
8411 /* Nested subroutines in Fortran get a prefix. */
8412 if (pdi->tag == DW_TAG_enumerator)
8413 /* Enumerators should not get the name of the enumeration as a prefix. */
8414 parent->scope = grandparent_scope;
8415 else if (parent->tag == DW_TAG_namespace
8416 || parent->tag == DW_TAG_module
8417 || parent->tag == DW_TAG_structure_type
8418 || parent->tag == DW_TAG_class_type
8419 || parent->tag == DW_TAG_interface_type
8420 || parent->tag == DW_TAG_union_type
8421 || parent->tag == DW_TAG_enumeration_type
8422 || (cu->language == language_fortran
8423 && parent->tag == DW_TAG_subprogram
8424 && pdi->tag == DW_TAG_subprogram))
8425 {
8426 if (grandparent_scope == NULL)
8427 parent->scope = parent->name (cu);
8428 else
8429 parent->scope = typename_concat (&cu->comp_unit_obstack,
8430 grandparent_scope,
8431 parent->name (cu), 0, cu);
8432 }
8433 else
8434 {
8435 /* FIXME drow/2004-04-01: What should we be doing with
8436 function-local names? For partial symbols, we should probably be
8437 ignoring them. */
8438 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8439 dwarf_tag_name (parent->tag),
8440 sect_offset_str (pdi->sect_off));
8441 parent->scope = grandparent_scope;
8442 }
8443
8444 parent->scope_set = 1;
8445 return parent->scope;
8446 }
8447
8448 /* Return the fully scoped name associated with PDI, from compilation unit
8449 CU. The result will be allocated with malloc. */
8450
8451 static gdb::unique_xmalloc_ptr<char>
8452 partial_die_full_name (struct partial_die_info *pdi,
8453 struct dwarf2_cu *cu)
8454 {
8455 const char *parent_scope;
8456
8457 /* If this is a template instantiation, we can not work out the
8458 template arguments from partial DIEs. So, unfortunately, we have
8459 to go through the full DIEs. At least any work we do building
8460 types here will be reused if full symbols are loaded later. */
8461 if (pdi->has_template_arguments)
8462 {
8463 pdi->fixup (cu);
8464
8465 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8466 {
8467 struct die_info *die;
8468 struct attribute attr;
8469 struct dwarf2_cu *ref_cu = cu;
8470
8471 /* DW_FORM_ref_addr is using section offset. */
8472 attr.name = (enum dwarf_attribute) 0;
8473 attr.form = DW_FORM_ref_addr;
8474 attr.u.unsnd = to_underlying (pdi->sect_off);
8475 die = follow_die_ref (NULL, &attr, &ref_cu);
8476
8477 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8478 }
8479 }
8480
8481 parent_scope = partial_die_parent_scope (pdi, cu);
8482 if (parent_scope == NULL)
8483 return NULL;
8484 else
8485 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8486 pdi->name (cu),
8487 0, cu));
8488 }
8489
8490 static void
8491 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8492 {
8493 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8494 struct objfile *objfile = per_objfile->objfile;
8495 struct gdbarch *gdbarch = objfile->arch ();
8496 CORE_ADDR addr = 0;
8497 const char *actual_name = NULL;
8498 CORE_ADDR baseaddr;
8499
8500 baseaddr = objfile->text_section_offset ();
8501
8502 gdb::unique_xmalloc_ptr<char> built_actual_name
8503 = partial_die_full_name (pdi, cu);
8504 if (built_actual_name != NULL)
8505 actual_name = built_actual_name.get ();
8506
8507 if (actual_name == NULL)
8508 actual_name = pdi->name (cu);
8509
8510 partial_symbol psymbol;
8511 memset (&psymbol, 0, sizeof (psymbol));
8512 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8513 psymbol.ginfo.set_section_index (-1);
8514
8515 /* The code below indicates that the psymbol should be installed by
8516 setting this. */
8517 gdb::optional<psymbol_placement> where;
8518
8519 switch (pdi->tag)
8520 {
8521 case DW_TAG_inlined_subroutine:
8522 case DW_TAG_subprogram:
8523 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8524 - baseaddr);
8525 if (pdi->is_external
8526 || cu->language == language_ada
8527 || (cu->language == language_fortran
8528 && pdi->die_parent != NULL
8529 && pdi->die_parent->tag == DW_TAG_subprogram))
8530 {
8531 /* Normally, only "external" DIEs are part of the global scope.
8532 But in Ada and Fortran, we want to be able to access nested
8533 procedures globally. So all Ada and Fortran subprograms are
8534 stored in the global scope. */
8535 where = psymbol_placement::GLOBAL;
8536 }
8537 else
8538 where = psymbol_placement::STATIC;
8539
8540 psymbol.domain = VAR_DOMAIN;
8541 psymbol.aclass = LOC_BLOCK;
8542 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8543 psymbol.ginfo.value.address = addr;
8544
8545 if (pdi->main_subprogram && actual_name != NULL)
8546 set_objfile_main_name (objfile, actual_name, cu->language);
8547 break;
8548 case DW_TAG_constant:
8549 psymbol.domain = VAR_DOMAIN;
8550 psymbol.aclass = LOC_STATIC;
8551 where = (pdi->is_external
8552 ? psymbol_placement::GLOBAL
8553 : psymbol_placement::STATIC);
8554 break;
8555 case DW_TAG_variable:
8556 if (pdi->d.locdesc)
8557 addr = decode_locdesc (pdi->d.locdesc, cu);
8558
8559 if (pdi->d.locdesc
8560 && addr == 0
8561 && !per_objfile->per_bfd->has_section_at_zero)
8562 {
8563 /* A global or static variable may also have been stripped
8564 out by the linker if unused, in which case its address
8565 will be nullified; do not add such variables into partial
8566 symbol table then. */
8567 }
8568 else if (pdi->is_external)
8569 {
8570 /* Global Variable.
8571 Don't enter into the minimal symbol tables as there is
8572 a minimal symbol table entry from the ELF symbols already.
8573 Enter into partial symbol table if it has a location
8574 descriptor or a type.
8575 If the location descriptor is missing, new_symbol will create
8576 a LOC_UNRESOLVED symbol, the address of the variable will then
8577 be determined from the minimal symbol table whenever the variable
8578 is referenced.
8579 The address for the partial symbol table entry is not
8580 used by GDB, but it comes in handy for debugging partial symbol
8581 table building. */
8582
8583 if (pdi->d.locdesc || pdi->has_type)
8584 {
8585 psymbol.domain = VAR_DOMAIN;
8586 psymbol.aclass = LOC_STATIC;
8587 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8588 psymbol.ginfo.value.address = addr;
8589 where = psymbol_placement::GLOBAL;
8590 }
8591 }
8592 else
8593 {
8594 int has_loc = pdi->d.locdesc != NULL;
8595
8596 /* Static Variable. Skip symbols whose value we cannot know (those
8597 without location descriptors or constant values). */
8598 if (!has_loc && !pdi->has_const_value)
8599 return;
8600
8601 psymbol.domain = VAR_DOMAIN;
8602 psymbol.aclass = LOC_STATIC;
8603 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8604 if (has_loc)
8605 psymbol.ginfo.value.address = addr;
8606 where = psymbol_placement::STATIC;
8607 }
8608 break;
8609 case DW_TAG_array_type:
8610 case DW_TAG_typedef:
8611 case DW_TAG_base_type:
8612 case DW_TAG_subrange_type:
8613 psymbol.domain = VAR_DOMAIN;
8614 psymbol.aclass = LOC_TYPEDEF;
8615 where = psymbol_placement::STATIC;
8616 break;
8617 case DW_TAG_imported_declaration:
8618 case DW_TAG_namespace:
8619 psymbol.domain = VAR_DOMAIN;
8620 psymbol.aclass = LOC_TYPEDEF;
8621 where = psymbol_placement::GLOBAL;
8622 break;
8623 case DW_TAG_module:
8624 /* With Fortran 77 there might be a "BLOCK DATA" module
8625 available without any name. If so, we skip the module as it
8626 doesn't bring any value. */
8627 if (actual_name != nullptr)
8628 {
8629 psymbol.domain = MODULE_DOMAIN;
8630 psymbol.aclass = LOC_TYPEDEF;
8631 where = psymbol_placement::GLOBAL;
8632 }
8633 break;
8634 case DW_TAG_class_type:
8635 case DW_TAG_interface_type:
8636 case DW_TAG_structure_type:
8637 case DW_TAG_union_type:
8638 case DW_TAG_enumeration_type:
8639 /* Skip external references. The DWARF standard says in the section
8640 about "Structure, Union, and Class Type Entries": "An incomplete
8641 structure, union or class type is represented by a structure,
8642 union or class entry that does not have a byte size attribute
8643 and that has a DW_AT_declaration attribute." */
8644 if (!pdi->has_byte_size && pdi->is_declaration)
8645 return;
8646
8647 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8648 static vs. global. */
8649 psymbol.domain = STRUCT_DOMAIN;
8650 psymbol.aclass = LOC_TYPEDEF;
8651 where = (cu->language == language_cplus
8652 ? psymbol_placement::GLOBAL
8653 : psymbol_placement::STATIC);
8654 break;
8655 case DW_TAG_enumerator:
8656 psymbol.domain = VAR_DOMAIN;
8657 psymbol.aclass = LOC_CONST;
8658 where = (cu->language == language_cplus
8659 ? psymbol_placement::GLOBAL
8660 : psymbol_placement::STATIC);
8661 break;
8662 default:
8663 break;
8664 }
8665
8666 if (where.has_value ())
8667 {
8668 if (built_actual_name != nullptr)
8669 actual_name = objfile->intern (actual_name);
8670 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8671 psymbol.ginfo.set_linkage_name (actual_name);
8672 else
8673 {
8674 psymbol.ginfo.set_demangled_name (actual_name,
8675 &objfile->objfile_obstack);
8676 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8677 }
8678 cu->per_cu->v.psymtab->add_psymbol
8679 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8680 objfile);
8681 }
8682 }
8683
8684 /* Read a partial die corresponding to a namespace; also, add a symbol
8685 corresponding to that namespace to the symbol table. NAMESPACE is
8686 the name of the enclosing namespace. */
8687
8688 static void
8689 add_partial_namespace (struct partial_die_info *pdi,
8690 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8691 int set_addrmap, struct dwarf2_cu *cu)
8692 {
8693 /* Add a symbol for the namespace. */
8694
8695 add_partial_symbol (pdi, cu);
8696
8697 /* Now scan partial symbols in that namespace. */
8698
8699 if (pdi->has_children)
8700 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8701 }
8702
8703 /* Read a partial die corresponding to a Fortran module. */
8704
8705 static void
8706 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8707 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8708 {
8709 /* Add a symbol for the namespace. */
8710
8711 add_partial_symbol (pdi, cu);
8712
8713 /* Now scan partial symbols in that module. */
8714
8715 if (pdi->has_children)
8716 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8717 }
8718
8719 /* Read a partial die corresponding to a subprogram or an inlined
8720 subprogram and create a partial symbol for that subprogram.
8721 When the CU language allows it, this routine also defines a partial
8722 symbol for each nested subprogram that this subprogram contains.
8723 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8724 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8725
8726 PDI may also be a lexical block, in which case we simply search
8727 recursively for subprograms defined inside that lexical block.
8728 Again, this is only performed when the CU language allows this
8729 type of definitions. */
8730
8731 static void
8732 add_partial_subprogram (struct partial_die_info *pdi,
8733 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8734 int set_addrmap, struct dwarf2_cu *cu)
8735 {
8736 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8737 {
8738 if (pdi->has_pc_info)
8739 {
8740 if (pdi->lowpc < *lowpc)
8741 *lowpc = pdi->lowpc;
8742 if (pdi->highpc > *highpc)
8743 *highpc = pdi->highpc;
8744 if (set_addrmap)
8745 {
8746 struct objfile *objfile = cu->per_objfile->objfile;
8747 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8748 struct gdbarch *gdbarch = objfile->arch ();
8749 CORE_ADDR baseaddr;
8750 CORE_ADDR this_highpc;
8751 CORE_ADDR this_lowpc;
8752
8753 baseaddr = objfile->text_section_offset ();
8754 this_lowpc
8755 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8756 pdi->lowpc + baseaddr)
8757 - baseaddr);
8758 this_highpc
8759 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8760 pdi->highpc + baseaddr)
8761 - baseaddr);
8762 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8763 this_lowpc, this_highpc - 1,
8764 cu->per_cu->v.psymtab);
8765 }
8766 }
8767
8768 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8769 {
8770 if (!pdi->is_declaration)
8771 /* Ignore subprogram DIEs that do not have a name, they are
8772 illegal. Do not emit a complaint at this point, we will
8773 do so when we convert this psymtab into a symtab. */
8774 if (pdi->name (cu))
8775 add_partial_symbol (pdi, cu);
8776 }
8777 }
8778
8779 if (! pdi->has_children)
8780 return;
8781
8782 if (cu->language == language_ada || cu->language == language_fortran)
8783 {
8784 pdi = pdi->die_child;
8785 while (pdi != NULL)
8786 {
8787 pdi->fixup (cu);
8788 if (pdi->tag == DW_TAG_subprogram
8789 || pdi->tag == DW_TAG_inlined_subroutine
8790 || pdi->tag == DW_TAG_lexical_block)
8791 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8792 pdi = pdi->die_sibling;
8793 }
8794 }
8795 }
8796
8797 /* Read a partial die corresponding to an enumeration type. */
8798
8799 static void
8800 add_partial_enumeration (struct partial_die_info *enum_pdi,
8801 struct dwarf2_cu *cu)
8802 {
8803 struct partial_die_info *pdi;
8804
8805 if (enum_pdi->name (cu) != NULL)
8806 add_partial_symbol (enum_pdi, cu);
8807
8808 pdi = enum_pdi->die_child;
8809 while (pdi)
8810 {
8811 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8812 complaint (_("malformed enumerator DIE ignored"));
8813 else
8814 add_partial_symbol (pdi, cu);
8815 pdi = pdi->die_sibling;
8816 }
8817 }
8818
8819 /* Return the initial uleb128 in the die at INFO_PTR. */
8820
8821 static unsigned int
8822 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8823 {
8824 unsigned int bytes_read;
8825
8826 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8827 }
8828
8829 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8830 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8831
8832 Return the corresponding abbrev, or NULL if the number is zero (indicating
8833 an empty DIE). In either case *BYTES_READ will be set to the length of
8834 the initial number. */
8835
8836 static const struct abbrev_info *
8837 peek_die_abbrev (const die_reader_specs &reader,
8838 const gdb_byte *info_ptr, unsigned int *bytes_read)
8839 {
8840 dwarf2_cu *cu = reader.cu;
8841 bfd *abfd = reader.abfd;
8842 unsigned int abbrev_number
8843 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8844
8845 if (abbrev_number == 0)
8846 return NULL;
8847
8848 const abbrev_info *abbrev
8849 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8850 if (!abbrev)
8851 {
8852 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8853 " at offset %s [in module %s]"),
8854 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8855 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8856 }
8857
8858 return abbrev;
8859 }
8860
8861 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8862 Returns a pointer to the end of a series of DIEs, terminated by an empty
8863 DIE. Any children of the skipped DIEs will also be skipped. */
8864
8865 static const gdb_byte *
8866 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8867 {
8868 while (1)
8869 {
8870 unsigned int bytes_read;
8871 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8872 &bytes_read);
8873
8874 if (abbrev == NULL)
8875 return info_ptr + bytes_read;
8876 else
8877 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8878 }
8879 }
8880
8881 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8882 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8883 abbrev corresponding to that skipped uleb128 should be passed in
8884 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8885 children. */
8886
8887 static const gdb_byte *
8888 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8889 const struct abbrev_info *abbrev)
8890 {
8891 unsigned int bytes_read;
8892 struct attribute attr;
8893 bfd *abfd = reader->abfd;
8894 struct dwarf2_cu *cu = reader->cu;
8895 const gdb_byte *buffer = reader->buffer;
8896 const gdb_byte *buffer_end = reader->buffer_end;
8897 unsigned int form, i;
8898
8899 for (i = 0; i < abbrev->num_attrs; i++)
8900 {
8901 /* The only abbrev we care about is DW_AT_sibling. */
8902 if (abbrev->attrs[i].name == DW_AT_sibling)
8903 {
8904 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8905 if (attr.form == DW_FORM_ref_addr)
8906 complaint (_("ignoring absolute DW_AT_sibling"));
8907 else
8908 {
8909 sect_offset off = attr.get_ref_die_offset ();
8910 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8911
8912 if (sibling_ptr < info_ptr)
8913 complaint (_("DW_AT_sibling points backwards"));
8914 else if (sibling_ptr > reader->buffer_end)
8915 reader->die_section->overflow_complaint ();
8916 else
8917 return sibling_ptr;
8918 }
8919 }
8920
8921 /* If it isn't DW_AT_sibling, skip this attribute. */
8922 form = abbrev->attrs[i].form;
8923 skip_attribute:
8924 switch (form)
8925 {
8926 case DW_FORM_ref_addr:
8927 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8928 and later it is offset sized. */
8929 if (cu->header.version == 2)
8930 info_ptr += cu->header.addr_size;
8931 else
8932 info_ptr += cu->header.offset_size;
8933 break;
8934 case DW_FORM_GNU_ref_alt:
8935 info_ptr += cu->header.offset_size;
8936 break;
8937 case DW_FORM_addr:
8938 info_ptr += cu->header.addr_size;
8939 break;
8940 case DW_FORM_data1:
8941 case DW_FORM_ref1:
8942 case DW_FORM_flag:
8943 case DW_FORM_strx1:
8944 info_ptr += 1;
8945 break;
8946 case DW_FORM_flag_present:
8947 case DW_FORM_implicit_const:
8948 break;
8949 case DW_FORM_data2:
8950 case DW_FORM_ref2:
8951 case DW_FORM_strx2:
8952 info_ptr += 2;
8953 break;
8954 case DW_FORM_strx3:
8955 info_ptr += 3;
8956 break;
8957 case DW_FORM_data4:
8958 case DW_FORM_ref4:
8959 case DW_FORM_strx4:
8960 info_ptr += 4;
8961 break;
8962 case DW_FORM_data8:
8963 case DW_FORM_ref8:
8964 case DW_FORM_ref_sig8:
8965 info_ptr += 8;
8966 break;
8967 case DW_FORM_data16:
8968 info_ptr += 16;
8969 break;
8970 case DW_FORM_string:
8971 read_direct_string (abfd, info_ptr, &bytes_read);
8972 info_ptr += bytes_read;
8973 break;
8974 case DW_FORM_sec_offset:
8975 case DW_FORM_strp:
8976 case DW_FORM_GNU_strp_alt:
8977 info_ptr += cu->header.offset_size;
8978 break;
8979 case DW_FORM_exprloc:
8980 case DW_FORM_block:
8981 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8982 info_ptr += bytes_read;
8983 break;
8984 case DW_FORM_block1:
8985 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8986 break;
8987 case DW_FORM_block2:
8988 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8989 break;
8990 case DW_FORM_block4:
8991 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8992 break;
8993 case DW_FORM_addrx:
8994 case DW_FORM_strx:
8995 case DW_FORM_sdata:
8996 case DW_FORM_udata:
8997 case DW_FORM_ref_udata:
8998 case DW_FORM_GNU_addr_index:
8999 case DW_FORM_GNU_str_index:
9000 case DW_FORM_rnglistx:
9001 case DW_FORM_loclistx:
9002 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9003 break;
9004 case DW_FORM_indirect:
9005 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9006 info_ptr += bytes_read;
9007 /* We need to continue parsing from here, so just go back to
9008 the top. */
9009 goto skip_attribute;
9010
9011 default:
9012 error (_("Dwarf Error: Cannot handle %s "
9013 "in DWARF reader [in module %s]"),
9014 dwarf_form_name (form),
9015 bfd_get_filename (abfd));
9016 }
9017 }
9018
9019 if (abbrev->has_children)
9020 return skip_children (reader, info_ptr);
9021 else
9022 return info_ptr;
9023 }
9024
9025 /* Locate ORIG_PDI's sibling.
9026 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9027
9028 static const gdb_byte *
9029 locate_pdi_sibling (const struct die_reader_specs *reader,
9030 struct partial_die_info *orig_pdi,
9031 const gdb_byte *info_ptr)
9032 {
9033 /* Do we know the sibling already? */
9034
9035 if (orig_pdi->sibling)
9036 return orig_pdi->sibling;
9037
9038 /* Are there any children to deal with? */
9039
9040 if (!orig_pdi->has_children)
9041 return info_ptr;
9042
9043 /* Skip the children the long way. */
9044
9045 return skip_children (reader, info_ptr);
9046 }
9047
9048 /* Expand this partial symbol table into a full symbol table. SELF is
9049 not NULL. */
9050
9051 void
9052 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9053 {
9054 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9055
9056 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9057
9058 /* If this psymtab is constructed from a debug-only objfile, the
9059 has_section_at_zero flag will not necessarily be correct. We
9060 can get the correct value for this flag by looking at the data
9061 associated with the (presumably stripped) associated objfile. */
9062 if (objfile->separate_debug_objfile_backlink)
9063 {
9064 dwarf2_per_objfile *per_objfile_backlink
9065 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9066
9067 per_objfile->per_bfd->has_section_at_zero
9068 = per_objfile_backlink->per_bfd->has_section_at_zero;
9069 }
9070
9071 expand_psymtab (objfile);
9072
9073 process_cu_includes (per_objfile);
9074 }
9075 \f
9076 /* Reading in full CUs. */
9077
9078 /* Add PER_CU to the queue. */
9079
9080 static void
9081 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9082 dwarf2_per_objfile *per_objfile,
9083 enum language pretend_language)
9084 {
9085 per_cu->queued = 1;
9086
9087 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9088 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
9089 }
9090
9091 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9092
9093 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9094 dependency.
9095
9096 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9097 DIEs, false otherwise.
9098
9099 Explanation: there is an invariant that if a CU is queued for expansion
9100 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9101 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9102 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9103 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9104 invariant is respected.
9105
9106 The caller is therefore not required to load the CU's DIEs (we return false)
9107 if:
9108
9109 - the CU is already expanded, and therefore does not get enqueued
9110 - the CU gets enqueued for expansion, but its DIEs are already loaded
9111
9112 Note that the caller should not use this function's return value as an
9113 indicator of whether the CU's DIEs are loaded right now, it should check
9114 that by calling `dwarf2_per_objfile::get_cu` instead. */
9115
9116 static int
9117 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9118 dwarf2_per_cu_data *per_cu,
9119 dwarf2_per_objfile *per_objfile,
9120 enum language pretend_language)
9121 {
9122 /* We may arrive here during partial symbol reading, if we need full
9123 DIEs to process an unusual case (e.g. template arguments). Do
9124 not queue PER_CU, just tell our caller to load its DIEs. */
9125 if (per_cu->per_bfd->reading_partial_symbols)
9126 {
9127 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9128
9129 if (cu == NULL || cu->dies == NULL)
9130 return 1;
9131 return 0;
9132 }
9133
9134 /* Mark the dependence relation so that we don't flush PER_CU
9135 too early. */
9136 if (dependent_cu != NULL)
9137 dwarf2_add_dependence (dependent_cu, per_cu);
9138
9139 /* If it's already on the queue, we have nothing to do. */
9140 if (per_cu->queued)
9141 {
9142 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9143 loaded. */
9144 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9145
9146 /* If the CU is queued for expansion, it should not already be
9147 expanded. */
9148 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9149
9150 /* The DIEs are already loaded, the caller doesn't need to do it. */
9151 return 0;
9152 }
9153
9154 bool queued = false;
9155 if (!per_objfile->symtab_set_p (per_cu))
9156 {
9157 /* Add it to the queue. */
9158 queue_comp_unit (per_cu, per_objfile, pretend_language);
9159 queued = true;
9160 }
9161
9162 /* If the compilation unit is already loaded, just mark it as
9163 used. */
9164 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9165 if (cu != nullptr)
9166 cu->last_used = 0;
9167
9168 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9169 and the DIEs are not already loaded. */
9170 return queued && cu == nullptr;
9171 }
9172
9173 /* Process the queue. */
9174
9175 static void
9176 process_queue (dwarf2_per_objfile *per_objfile)
9177 {
9178 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9179 objfile_name (per_objfile->objfile));
9180
9181 /* The queue starts out with one item, but following a DIE reference
9182 may load a new CU, adding it to the end of the queue. */
9183 while (!per_objfile->per_bfd->queue->empty ())
9184 {
9185 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
9186 dwarf2_per_cu_data *per_cu = item.per_cu;
9187
9188 if (!per_objfile->symtab_set_p (per_cu))
9189 {
9190 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9191
9192 /* Skip dummy CUs. */
9193 if (cu != nullptr)
9194 {
9195 unsigned int debug_print_threshold;
9196 char buf[100];
9197
9198 if (per_cu->is_debug_types)
9199 {
9200 struct signatured_type *sig_type =
9201 (struct signatured_type *) per_cu;
9202
9203 sprintf (buf, "TU %s at offset %s",
9204 hex_string (sig_type->signature),
9205 sect_offset_str (per_cu->sect_off));
9206 /* There can be 100s of TUs.
9207 Only print them in verbose mode. */
9208 debug_print_threshold = 2;
9209 }
9210 else
9211 {
9212 sprintf (buf, "CU at offset %s",
9213 sect_offset_str (per_cu->sect_off));
9214 debug_print_threshold = 1;
9215 }
9216
9217 if (dwarf_read_debug >= debug_print_threshold)
9218 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9219
9220 if (per_cu->is_debug_types)
9221 process_full_type_unit (cu, item.pretend_language);
9222 else
9223 process_full_comp_unit (cu, item.pretend_language);
9224
9225 if (dwarf_read_debug >= debug_print_threshold)
9226 dwarf_read_debug_printf ("Done expanding %s", buf);
9227 }
9228 }
9229
9230 per_cu->queued = 0;
9231 per_objfile->per_bfd->queue->pop ();
9232 }
9233
9234 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9235 objfile_name (per_objfile->objfile));
9236 }
9237
9238 /* Read in full symbols for PST, and anything it depends on. */
9239
9240 void
9241 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9242 {
9243 gdb_assert (!readin_p (objfile));
9244
9245 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9246 free_cached_comp_units freer (per_objfile);
9247 expand_dependencies (objfile);
9248
9249 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9250 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9251 }
9252
9253 /* See psympriv.h. */
9254
9255 bool
9256 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9257 {
9258 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9259 return per_objfile->symtab_set_p (per_cu_data);
9260 }
9261
9262 /* See psympriv.h. */
9263
9264 compunit_symtab *
9265 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9266 {
9267 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9268 return per_objfile->get_symtab (per_cu_data);
9269 }
9270
9271 /* Trivial hash function for die_info: the hash value of a DIE
9272 is its offset in .debug_info for this objfile. */
9273
9274 static hashval_t
9275 die_hash (const void *item)
9276 {
9277 const struct die_info *die = (const struct die_info *) item;
9278
9279 return to_underlying (die->sect_off);
9280 }
9281
9282 /* Trivial comparison function for die_info structures: two DIEs
9283 are equal if they have the same offset. */
9284
9285 static int
9286 die_eq (const void *item_lhs, const void *item_rhs)
9287 {
9288 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9289 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9290
9291 return die_lhs->sect_off == die_rhs->sect_off;
9292 }
9293
9294 /* Load the DIEs associated with PER_CU into memory.
9295
9296 In some cases, the caller, while reading partial symbols, will need to load
9297 the full symbols for the CU for some reason. It will already have a
9298 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9299 rather than creating a new one. */
9300
9301 static void
9302 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9303 dwarf2_per_objfile *per_objfile,
9304 dwarf2_cu *existing_cu,
9305 bool skip_partial,
9306 enum language pretend_language)
9307 {
9308 gdb_assert (! this_cu->is_debug_types);
9309
9310 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9311 if (reader.dummy_p)
9312 return;
9313
9314 struct dwarf2_cu *cu = reader.cu;
9315 const gdb_byte *info_ptr = reader.info_ptr;
9316
9317 gdb_assert (cu->die_hash == NULL);
9318 cu->die_hash =
9319 htab_create_alloc_ex (cu->header.length / 12,
9320 die_hash,
9321 die_eq,
9322 NULL,
9323 &cu->comp_unit_obstack,
9324 hashtab_obstack_allocate,
9325 dummy_obstack_deallocate);
9326
9327 if (reader.comp_unit_die->has_children)
9328 reader.comp_unit_die->child
9329 = read_die_and_siblings (&reader, reader.info_ptr,
9330 &info_ptr, reader.comp_unit_die);
9331 cu->dies = reader.comp_unit_die;
9332 /* comp_unit_die is not stored in die_hash, no need. */
9333
9334 /* We try not to read any attributes in this function, because not
9335 all CUs needed for references have been loaded yet, and symbol
9336 table processing isn't initialized. But we have to set the CU language,
9337 or we won't be able to build types correctly.
9338 Similarly, if we do not read the producer, we can not apply
9339 producer-specific interpretation. */
9340 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9341
9342 reader.keep ();
9343 }
9344
9345 /* Add a DIE to the delayed physname list. */
9346
9347 static void
9348 add_to_method_list (struct type *type, int fnfield_index, int index,
9349 const char *name, struct die_info *die,
9350 struct dwarf2_cu *cu)
9351 {
9352 struct delayed_method_info mi;
9353 mi.type = type;
9354 mi.fnfield_index = fnfield_index;
9355 mi.index = index;
9356 mi.name = name;
9357 mi.die = die;
9358 cu->method_list.push_back (mi);
9359 }
9360
9361 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9362 "const" / "volatile". If so, decrements LEN by the length of the
9363 modifier and return true. Otherwise return false. */
9364
9365 template<size_t N>
9366 static bool
9367 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9368 {
9369 size_t mod_len = sizeof (mod) - 1;
9370 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9371 {
9372 len -= mod_len;
9373 return true;
9374 }
9375 return false;
9376 }
9377
9378 /* Compute the physnames of any methods on the CU's method list.
9379
9380 The computation of method physnames is delayed in order to avoid the
9381 (bad) condition that one of the method's formal parameters is of an as yet
9382 incomplete type. */
9383
9384 static void
9385 compute_delayed_physnames (struct dwarf2_cu *cu)
9386 {
9387 /* Only C++ delays computing physnames. */
9388 if (cu->method_list.empty ())
9389 return;
9390 gdb_assert (cu->language == language_cplus);
9391
9392 for (const delayed_method_info &mi : cu->method_list)
9393 {
9394 const char *physname;
9395 struct fn_fieldlist *fn_flp
9396 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9397 physname = dwarf2_physname (mi.name, mi.die, cu);
9398 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9399 = physname ? physname : "";
9400
9401 /* Since there's no tag to indicate whether a method is a
9402 const/volatile overload, extract that information out of the
9403 demangled name. */
9404 if (physname != NULL)
9405 {
9406 size_t len = strlen (physname);
9407
9408 while (1)
9409 {
9410 if (physname[len] == ')') /* shortcut */
9411 break;
9412 else if (check_modifier (physname, len, " const"))
9413 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9414 else if (check_modifier (physname, len, " volatile"))
9415 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9416 else
9417 break;
9418 }
9419 }
9420 }
9421
9422 /* The list is no longer needed. */
9423 cu->method_list.clear ();
9424 }
9425
9426 /* Go objects should be embedded in a DW_TAG_module DIE,
9427 and it's not clear if/how imported objects will appear.
9428 To keep Go support simple until that's worked out,
9429 go back through what we've read and create something usable.
9430 We could do this while processing each DIE, and feels kinda cleaner,
9431 but that way is more invasive.
9432 This is to, for example, allow the user to type "p var" or "b main"
9433 without having to specify the package name, and allow lookups
9434 of module.object to work in contexts that use the expression
9435 parser. */
9436
9437 static void
9438 fixup_go_packaging (struct dwarf2_cu *cu)
9439 {
9440 gdb::unique_xmalloc_ptr<char> package_name;
9441 struct pending *list;
9442 int i;
9443
9444 for (list = *cu->get_builder ()->get_global_symbols ();
9445 list != NULL;
9446 list = list->next)
9447 {
9448 for (i = 0; i < list->nsyms; ++i)
9449 {
9450 struct symbol *sym = list->symbol[i];
9451
9452 if (sym->language () == language_go
9453 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9454 {
9455 gdb::unique_xmalloc_ptr<char> this_package_name
9456 (go_symbol_package_name (sym));
9457
9458 if (this_package_name == NULL)
9459 continue;
9460 if (package_name == NULL)
9461 package_name = std::move (this_package_name);
9462 else
9463 {
9464 struct objfile *objfile = cu->per_objfile->objfile;
9465 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9466 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9467 (symbol_symtab (sym) != NULL
9468 ? symtab_to_filename_for_display
9469 (symbol_symtab (sym))
9470 : objfile_name (objfile)),
9471 this_package_name.get (), package_name.get ());
9472 }
9473 }
9474 }
9475 }
9476
9477 if (package_name != NULL)
9478 {
9479 struct objfile *objfile = cu->per_objfile->objfile;
9480 const char *saved_package_name = objfile->intern (package_name.get ());
9481 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9482 saved_package_name);
9483 struct symbol *sym;
9484
9485 sym = new (&objfile->objfile_obstack) symbol;
9486 sym->set_language (language_go, &objfile->objfile_obstack);
9487 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9488 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9489 e.g., "main" finds the "main" module and not C's main(). */
9490 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9491 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9492 SYMBOL_TYPE (sym) = type;
9493
9494 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9495 }
9496 }
9497
9498 /* Allocate a fully-qualified name consisting of the two parts on the
9499 obstack. */
9500
9501 static const char *
9502 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9503 {
9504 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9505 }
9506
9507 /* A helper that allocates a variant part to attach to a Rust enum
9508 type. OBSTACK is where the results should be allocated. TYPE is
9509 the type we're processing. DISCRIMINANT_INDEX is the index of the
9510 discriminant. It must be the index of one of the fields of TYPE,
9511 or -1 to mean there is no discriminant (univariant enum).
9512 DEFAULT_INDEX is the index of the default field; or -1 if there is
9513 no default. RANGES is indexed by "effective" field number (the
9514 field index, but omitting the discriminant and default fields) and
9515 must hold the discriminant values used by the variants. Note that
9516 RANGES must have a lifetime at least as long as OBSTACK -- either
9517 already allocated on it, or static. */
9518
9519 static void
9520 alloc_rust_variant (struct obstack *obstack, struct type *type,
9521 int discriminant_index, int default_index,
9522 gdb::array_view<discriminant_range> ranges)
9523 {
9524 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9525 gdb_assert (discriminant_index == -1
9526 || (discriminant_index >= 0
9527 && discriminant_index < type->num_fields ()));
9528 gdb_assert (default_index == -1
9529 || (default_index >= 0 && default_index < type->num_fields ()));
9530
9531 /* We have one variant for each non-discriminant field. */
9532 int n_variants = type->num_fields ();
9533 if (discriminant_index != -1)
9534 --n_variants;
9535
9536 variant *variants = new (obstack) variant[n_variants];
9537 int var_idx = 0;
9538 int range_idx = 0;
9539 for (int i = 0; i < type->num_fields (); ++i)
9540 {
9541 if (i == discriminant_index)
9542 continue;
9543
9544 variants[var_idx].first_field = i;
9545 variants[var_idx].last_field = i + 1;
9546
9547 /* The default field does not need a range, but other fields do.
9548 We skipped the discriminant above. */
9549 if (i != default_index)
9550 {
9551 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9552 ++range_idx;
9553 }
9554
9555 ++var_idx;
9556 }
9557
9558 gdb_assert (range_idx == ranges.size ());
9559 gdb_assert (var_idx == n_variants);
9560
9561 variant_part *part = new (obstack) variant_part;
9562 part->discriminant_index = discriminant_index;
9563 /* If there is no discriminant, then whether it is signed is of no
9564 consequence. */
9565 part->is_unsigned
9566 = (discriminant_index == -1
9567 ? false
9568 : type->field (discriminant_index).type ()->is_unsigned ());
9569 part->variants = gdb::array_view<variant> (variants, n_variants);
9570
9571 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9572 gdb::array_view<variant_part> *prop_value
9573 = new (storage) gdb::array_view<variant_part> (part, 1);
9574
9575 struct dynamic_prop prop;
9576 prop.set_variant_parts (prop_value);
9577
9578 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9579 }
9580
9581 /* Some versions of rustc emitted enums in an unusual way.
9582
9583 Ordinary enums were emitted as unions. The first element of each
9584 structure in the union was named "RUST$ENUM$DISR". This element
9585 held the discriminant.
9586
9587 These versions of Rust also implemented the "non-zero"
9588 optimization. When the enum had two values, and one is empty and
9589 the other holds a pointer that cannot be zero, the pointer is used
9590 as the discriminant, with a zero value meaning the empty variant.
9591 Here, the union's first member is of the form
9592 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9593 where the fieldnos are the indices of the fields that should be
9594 traversed in order to find the field (which may be several fields deep)
9595 and the variantname is the name of the variant of the case when the
9596 field is zero.
9597
9598 This function recognizes whether TYPE is of one of these forms,
9599 and, if so, smashes it to be a variant type. */
9600
9601 static void
9602 quirk_rust_enum (struct type *type, struct objfile *objfile)
9603 {
9604 gdb_assert (type->code () == TYPE_CODE_UNION);
9605
9606 /* We don't need to deal with empty enums. */
9607 if (type->num_fields () == 0)
9608 return;
9609
9610 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9611 if (type->num_fields () == 1
9612 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9613 {
9614 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9615
9616 /* Decode the field name to find the offset of the
9617 discriminant. */
9618 ULONGEST bit_offset = 0;
9619 struct type *field_type = type->field (0).type ();
9620 while (name[0] >= '0' && name[0] <= '9')
9621 {
9622 char *tail;
9623 unsigned long index = strtoul (name, &tail, 10);
9624 name = tail;
9625 if (*name != '$'
9626 || index >= field_type->num_fields ()
9627 || (TYPE_FIELD_LOC_KIND (field_type, index)
9628 != FIELD_LOC_KIND_BITPOS))
9629 {
9630 complaint (_("Could not parse Rust enum encoding string \"%s\""
9631 "[in module %s]"),
9632 TYPE_FIELD_NAME (type, 0),
9633 objfile_name (objfile));
9634 return;
9635 }
9636 ++name;
9637
9638 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9639 field_type = field_type->field (index).type ();
9640 }
9641
9642 /* Smash this type to be a structure type. We have to do this
9643 because the type has already been recorded. */
9644 type->set_code (TYPE_CODE_STRUCT);
9645 type->set_num_fields (3);
9646 /* Save the field we care about. */
9647 struct field saved_field = type->field (0);
9648 type->set_fields
9649 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9650
9651 /* Put the discriminant at index 0. */
9652 type->field (0).set_type (field_type);
9653 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9654 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9655 SET_FIELD_BITPOS (type->field (0), bit_offset);
9656
9657 /* The order of fields doesn't really matter, so put the real
9658 field at index 1 and the data-less field at index 2. */
9659 type->field (1) = saved_field;
9660 TYPE_FIELD_NAME (type, 1)
9661 = rust_last_path_segment (type->field (1).type ()->name ());
9662 type->field (1).type ()->set_name
9663 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9664 TYPE_FIELD_NAME (type, 1)));
9665
9666 const char *dataless_name
9667 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9668 name);
9669 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9670 dataless_name);
9671 type->field (2).set_type (dataless_type);
9672 /* NAME points into the original discriminant name, which
9673 already has the correct lifetime. */
9674 TYPE_FIELD_NAME (type, 2) = name;
9675 SET_FIELD_BITPOS (type->field (2), 0);
9676
9677 /* Indicate that this is a variant type. */
9678 static discriminant_range ranges[1] = { { 0, 0 } };
9679 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9680 }
9681 /* A union with a single anonymous field is probably an old-style
9682 univariant enum. */
9683 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9684 {
9685 /* Smash this type to be a structure type. We have to do this
9686 because the type has already been recorded. */
9687 type->set_code (TYPE_CODE_STRUCT);
9688
9689 struct type *field_type = type->field (0).type ();
9690 const char *variant_name
9691 = rust_last_path_segment (field_type->name ());
9692 TYPE_FIELD_NAME (type, 0) = variant_name;
9693 field_type->set_name
9694 (rust_fully_qualify (&objfile->objfile_obstack,
9695 type->name (), variant_name));
9696
9697 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9698 }
9699 else
9700 {
9701 struct type *disr_type = nullptr;
9702 for (int i = 0; i < type->num_fields (); ++i)
9703 {
9704 disr_type = type->field (i).type ();
9705
9706 if (disr_type->code () != TYPE_CODE_STRUCT)
9707 {
9708 /* All fields of a true enum will be structs. */
9709 return;
9710 }
9711 else if (disr_type->num_fields () == 0)
9712 {
9713 /* Could be data-less variant, so keep going. */
9714 disr_type = nullptr;
9715 }
9716 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9717 "RUST$ENUM$DISR") != 0)
9718 {
9719 /* Not a Rust enum. */
9720 return;
9721 }
9722 else
9723 {
9724 /* Found one. */
9725 break;
9726 }
9727 }
9728
9729 /* If we got here without a discriminant, then it's probably
9730 just a union. */
9731 if (disr_type == nullptr)
9732 return;
9733
9734 /* Smash this type to be a structure type. We have to do this
9735 because the type has already been recorded. */
9736 type->set_code (TYPE_CODE_STRUCT);
9737
9738 /* Make space for the discriminant field. */
9739 struct field *disr_field = &disr_type->field (0);
9740 field *new_fields
9741 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9742 * sizeof (struct field)));
9743 memcpy (new_fields + 1, type->fields (),
9744 type->num_fields () * sizeof (struct field));
9745 type->set_fields (new_fields);
9746 type->set_num_fields (type->num_fields () + 1);
9747
9748 /* Install the discriminant at index 0 in the union. */
9749 type->field (0) = *disr_field;
9750 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9751 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9752
9753 /* We need a way to find the correct discriminant given a
9754 variant name. For convenience we build a map here. */
9755 struct type *enum_type = disr_field->type ();
9756 std::unordered_map<std::string, ULONGEST> discriminant_map;
9757 for (int i = 0; i < enum_type->num_fields (); ++i)
9758 {
9759 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9760 {
9761 const char *name
9762 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9763 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9764 }
9765 }
9766
9767 int n_fields = type->num_fields ();
9768 /* We don't need a range entry for the discriminant, but we do
9769 need one for every other field, as there is no default
9770 variant. */
9771 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9772 discriminant_range,
9773 n_fields - 1);
9774 /* Skip the discriminant here. */
9775 for (int i = 1; i < n_fields; ++i)
9776 {
9777 /* Find the final word in the name of this variant's type.
9778 That name can be used to look up the correct
9779 discriminant. */
9780 const char *variant_name
9781 = rust_last_path_segment (type->field (i).type ()->name ());
9782
9783 auto iter = discriminant_map.find (variant_name);
9784 if (iter != discriminant_map.end ())
9785 {
9786 ranges[i - 1].low = iter->second;
9787 ranges[i - 1].high = iter->second;
9788 }
9789
9790 /* In Rust, each element should have the size of the
9791 enclosing enum. */
9792 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9793
9794 /* Remove the discriminant field, if it exists. */
9795 struct type *sub_type = type->field (i).type ();
9796 if (sub_type->num_fields () > 0)
9797 {
9798 sub_type->set_num_fields (sub_type->num_fields () - 1);
9799 sub_type->set_fields (sub_type->fields () + 1);
9800 }
9801 TYPE_FIELD_NAME (type, i) = variant_name;
9802 sub_type->set_name
9803 (rust_fully_qualify (&objfile->objfile_obstack,
9804 type->name (), variant_name));
9805 }
9806
9807 /* Indicate that this is a variant type. */
9808 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9809 gdb::array_view<discriminant_range> (ranges,
9810 n_fields - 1));
9811 }
9812 }
9813
9814 /* Rewrite some Rust unions to be structures with variants parts. */
9815
9816 static void
9817 rust_union_quirks (struct dwarf2_cu *cu)
9818 {
9819 gdb_assert (cu->language == language_rust);
9820 for (type *type_ : cu->rust_unions)
9821 quirk_rust_enum (type_, cu->per_objfile->objfile);
9822 /* We don't need this any more. */
9823 cu->rust_unions.clear ();
9824 }
9825
9826 /* See read.h. */
9827
9828 type_unit_group_unshareable *
9829 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9830 {
9831 auto iter = this->m_type_units.find (tu_group);
9832 if (iter != this->m_type_units.end ())
9833 return iter->second.get ();
9834
9835 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9836 type_unit_group_unshareable *result = uniq.get ();
9837 this->m_type_units[tu_group] = std::move (uniq);
9838 return result;
9839 }
9840
9841 struct type *
9842 dwarf2_per_objfile::get_type_for_signatured_type
9843 (signatured_type *sig_type) const
9844 {
9845 auto iter = this->m_type_map.find (sig_type);
9846 if (iter == this->m_type_map.end ())
9847 return nullptr;
9848
9849 return iter->second;
9850 }
9851
9852 void dwarf2_per_objfile::set_type_for_signatured_type
9853 (signatured_type *sig_type, struct type *type)
9854 {
9855 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9856
9857 this->m_type_map[sig_type] = type;
9858 }
9859
9860 /* A helper function for computing the list of all symbol tables
9861 included by PER_CU. */
9862
9863 static void
9864 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9865 htab_t all_children, htab_t all_type_symtabs,
9866 dwarf2_per_cu_data *per_cu,
9867 dwarf2_per_objfile *per_objfile,
9868 struct compunit_symtab *immediate_parent)
9869 {
9870 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9871 if (*slot != NULL)
9872 {
9873 /* This inclusion and its children have been processed. */
9874 return;
9875 }
9876
9877 *slot = per_cu;
9878
9879 /* Only add a CU if it has a symbol table. */
9880 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9881 if (cust != NULL)
9882 {
9883 /* If this is a type unit only add its symbol table if we haven't
9884 seen it yet (type unit per_cu's can share symtabs). */
9885 if (per_cu->is_debug_types)
9886 {
9887 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9888 if (*slot == NULL)
9889 {
9890 *slot = cust;
9891 result->push_back (cust);
9892 if (cust->user == NULL)
9893 cust->user = immediate_parent;
9894 }
9895 }
9896 else
9897 {
9898 result->push_back (cust);
9899 if (cust->user == NULL)
9900 cust->user = immediate_parent;
9901 }
9902 }
9903
9904 if (!per_cu->imported_symtabs_empty ())
9905 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9906 {
9907 recursively_compute_inclusions (result, all_children,
9908 all_type_symtabs, ptr, per_objfile,
9909 cust);
9910 }
9911 }
9912
9913 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9914 PER_CU. */
9915
9916 static void
9917 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9918 dwarf2_per_objfile *per_objfile)
9919 {
9920 gdb_assert (! per_cu->is_debug_types);
9921
9922 if (!per_cu->imported_symtabs_empty ())
9923 {
9924 int len;
9925 std::vector<compunit_symtab *> result_symtabs;
9926 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9927
9928 /* If we don't have a symtab, we can just skip this case. */
9929 if (cust == NULL)
9930 return;
9931
9932 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9933 htab_eq_pointer,
9934 NULL, xcalloc, xfree));
9935 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9936 htab_eq_pointer,
9937 NULL, xcalloc, xfree));
9938
9939 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9940 {
9941 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9942 all_type_symtabs.get (), ptr,
9943 per_objfile, cust);
9944 }
9945
9946 /* Now we have a transitive closure of all the included symtabs. */
9947 len = result_symtabs.size ();
9948 cust->includes
9949 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9950 struct compunit_symtab *, len + 1);
9951 memcpy (cust->includes, result_symtabs.data (),
9952 len * sizeof (compunit_symtab *));
9953 cust->includes[len] = NULL;
9954 }
9955 }
9956
9957 /* Compute the 'includes' field for the symtabs of all the CUs we just
9958 read. */
9959
9960 static void
9961 process_cu_includes (dwarf2_per_objfile *per_objfile)
9962 {
9963 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9964 {
9965 if (! iter->is_debug_types)
9966 compute_compunit_symtab_includes (iter, per_objfile);
9967 }
9968
9969 per_objfile->per_bfd->just_read_cus.clear ();
9970 }
9971
9972 /* Generate full symbol information for CU, whose DIEs have
9973 already been loaded into memory. */
9974
9975 static void
9976 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9977 {
9978 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9979 struct objfile *objfile = per_objfile->objfile;
9980 struct gdbarch *gdbarch = objfile->arch ();
9981 CORE_ADDR lowpc, highpc;
9982 struct compunit_symtab *cust;
9983 CORE_ADDR baseaddr;
9984 struct block *static_block;
9985 CORE_ADDR addr;
9986
9987 baseaddr = objfile->text_section_offset ();
9988
9989 /* Clear the list here in case something was left over. */
9990 cu->method_list.clear ();
9991
9992 cu->language = pretend_language;
9993 cu->language_defn = language_def (cu->language);
9994
9995 dwarf2_find_base_address (cu->dies, cu);
9996
9997 /* Do line number decoding in read_file_scope () */
9998 process_die (cu->dies, cu);
9999
10000 /* For now fudge the Go package. */
10001 if (cu->language == language_go)
10002 fixup_go_packaging (cu);
10003
10004 /* Now that we have processed all the DIEs in the CU, all the types
10005 should be complete, and it should now be safe to compute all of the
10006 physnames. */
10007 compute_delayed_physnames (cu);
10008
10009 if (cu->language == language_rust)
10010 rust_union_quirks (cu);
10011
10012 /* Some compilers don't define a DW_AT_high_pc attribute for the
10013 compilation unit. If the DW_AT_high_pc is missing, synthesize
10014 it, by scanning the DIE's below the compilation unit. */
10015 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10016
10017 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10018 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10019
10020 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10021 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10022 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10023 addrmap to help ensure it has an accurate map of pc values belonging to
10024 this comp unit. */
10025 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10026
10027 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10028 SECT_OFF_TEXT (objfile),
10029 0);
10030
10031 if (cust != NULL)
10032 {
10033 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10034
10035 /* Set symtab language to language from DW_AT_language. If the
10036 compilation is from a C file generated by language preprocessors, do
10037 not set the language if it was already deduced by start_subfile. */
10038 if (!(cu->language == language_c
10039 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10040 COMPUNIT_FILETABS (cust)->language = cu->language;
10041
10042 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10043 produce DW_AT_location with location lists but it can be possibly
10044 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10045 there were bugs in prologue debug info, fixed later in GCC-4.5
10046 by "unwind info for epilogues" patch (which is not directly related).
10047
10048 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10049 needed, it would be wrong due to missing DW_AT_producer there.
10050
10051 Still one can confuse GDB by using non-standard GCC compilation
10052 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10053 */
10054 if (cu->has_loclist && gcc_4_minor >= 5)
10055 cust->locations_valid = 1;
10056
10057 if (gcc_4_minor >= 5)
10058 cust->epilogue_unwind_valid = 1;
10059
10060 cust->call_site_htab = cu->call_site_htab;
10061 }
10062
10063 per_objfile->set_symtab (cu->per_cu, cust);
10064
10065 /* Push it for inclusion processing later. */
10066 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10067
10068 /* Not needed any more. */
10069 cu->reset_builder ();
10070 }
10071
10072 /* Generate full symbol information for type unit CU, whose DIEs have
10073 already been loaded into memory. */
10074
10075 static void
10076 process_full_type_unit (dwarf2_cu *cu,
10077 enum language pretend_language)
10078 {
10079 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10080 struct objfile *objfile = per_objfile->objfile;
10081 struct compunit_symtab *cust;
10082 struct signatured_type *sig_type;
10083
10084 gdb_assert (cu->per_cu->is_debug_types);
10085 sig_type = (struct signatured_type *) cu->per_cu;
10086
10087 /* Clear the list here in case something was left over. */
10088 cu->method_list.clear ();
10089
10090 cu->language = pretend_language;
10091 cu->language_defn = language_def (cu->language);
10092
10093 /* The symbol tables are set up in read_type_unit_scope. */
10094 process_die (cu->dies, cu);
10095
10096 /* For now fudge the Go package. */
10097 if (cu->language == language_go)
10098 fixup_go_packaging (cu);
10099
10100 /* Now that we have processed all the DIEs in the CU, all the types
10101 should be complete, and it should now be safe to compute all of the
10102 physnames. */
10103 compute_delayed_physnames (cu);
10104
10105 if (cu->language == language_rust)
10106 rust_union_quirks (cu);
10107
10108 /* TUs share symbol tables.
10109 If this is the first TU to use this symtab, complete the construction
10110 of it with end_expandable_symtab. Otherwise, complete the addition of
10111 this TU's symbols to the existing symtab. */
10112 type_unit_group_unshareable *tug_unshare =
10113 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10114 if (tug_unshare->compunit_symtab == NULL)
10115 {
10116 buildsym_compunit *builder = cu->get_builder ();
10117 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10118 tug_unshare->compunit_symtab = cust;
10119
10120 if (cust != NULL)
10121 {
10122 /* Set symtab language to language from DW_AT_language. If the
10123 compilation is from a C file generated by language preprocessors,
10124 do not set the language if it was already deduced by
10125 start_subfile. */
10126 if (!(cu->language == language_c
10127 && COMPUNIT_FILETABS (cust)->language != language_c))
10128 COMPUNIT_FILETABS (cust)->language = cu->language;
10129 }
10130 }
10131 else
10132 {
10133 cu->get_builder ()->augment_type_symtab ();
10134 cust = tug_unshare->compunit_symtab;
10135 }
10136
10137 per_objfile->set_symtab (cu->per_cu, cust);
10138
10139 /* Not needed any more. */
10140 cu->reset_builder ();
10141 }
10142
10143 /* Process an imported unit DIE. */
10144
10145 static void
10146 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10147 {
10148 struct attribute *attr;
10149
10150 /* For now we don't handle imported units in type units. */
10151 if (cu->per_cu->is_debug_types)
10152 {
10153 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10154 " supported in type units [in module %s]"),
10155 objfile_name (cu->per_objfile->objfile));
10156 }
10157
10158 attr = dwarf2_attr (die, DW_AT_import, cu);
10159 if (attr != NULL)
10160 {
10161 sect_offset sect_off = attr->get_ref_die_offset ();
10162 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10163 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10164 dwarf2_per_cu_data *per_cu
10165 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10166
10167 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10168 into another compilation unit, at root level. Regard this as a hint,
10169 and ignore it. */
10170 if (die->parent && die->parent->parent == NULL
10171 && per_cu->unit_type == DW_UT_compile
10172 && per_cu->lang == language_cplus)
10173 return;
10174
10175 /* If necessary, add it to the queue and load its DIEs. */
10176 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10177 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10178 false, cu->language);
10179
10180 cu->per_cu->imported_symtabs_push (per_cu);
10181 }
10182 }
10183
10184 /* RAII object that represents a process_die scope: i.e.,
10185 starts/finishes processing a DIE. */
10186 class process_die_scope
10187 {
10188 public:
10189 process_die_scope (die_info *die, dwarf2_cu *cu)
10190 : m_die (die), m_cu (cu)
10191 {
10192 /* We should only be processing DIEs not already in process. */
10193 gdb_assert (!m_die->in_process);
10194 m_die->in_process = true;
10195 }
10196
10197 ~process_die_scope ()
10198 {
10199 m_die->in_process = false;
10200
10201 /* If we're done processing the DIE for the CU that owns the line
10202 header, we don't need the line header anymore. */
10203 if (m_cu->line_header_die_owner == m_die)
10204 {
10205 delete m_cu->line_header;
10206 m_cu->line_header = NULL;
10207 m_cu->line_header_die_owner = NULL;
10208 }
10209 }
10210
10211 private:
10212 die_info *m_die;
10213 dwarf2_cu *m_cu;
10214 };
10215
10216 /* Process a die and its children. */
10217
10218 static void
10219 process_die (struct die_info *die, struct dwarf2_cu *cu)
10220 {
10221 process_die_scope scope (die, cu);
10222
10223 switch (die->tag)
10224 {
10225 case DW_TAG_padding:
10226 break;
10227 case DW_TAG_compile_unit:
10228 case DW_TAG_partial_unit:
10229 read_file_scope (die, cu);
10230 break;
10231 case DW_TAG_type_unit:
10232 read_type_unit_scope (die, cu);
10233 break;
10234 case DW_TAG_subprogram:
10235 /* Nested subprograms in Fortran get a prefix. */
10236 if (cu->language == language_fortran
10237 && die->parent != NULL
10238 && die->parent->tag == DW_TAG_subprogram)
10239 cu->processing_has_namespace_info = true;
10240 /* Fall through. */
10241 case DW_TAG_inlined_subroutine:
10242 read_func_scope (die, cu);
10243 break;
10244 case DW_TAG_lexical_block:
10245 case DW_TAG_try_block:
10246 case DW_TAG_catch_block:
10247 read_lexical_block_scope (die, cu);
10248 break;
10249 case DW_TAG_call_site:
10250 case DW_TAG_GNU_call_site:
10251 read_call_site_scope (die, cu);
10252 break;
10253 case DW_TAG_class_type:
10254 case DW_TAG_interface_type:
10255 case DW_TAG_structure_type:
10256 case DW_TAG_union_type:
10257 process_structure_scope (die, cu);
10258 break;
10259 case DW_TAG_enumeration_type:
10260 process_enumeration_scope (die, cu);
10261 break;
10262
10263 /* These dies have a type, but processing them does not create
10264 a symbol or recurse to process the children. Therefore we can
10265 read them on-demand through read_type_die. */
10266 case DW_TAG_subroutine_type:
10267 case DW_TAG_set_type:
10268 case DW_TAG_pointer_type:
10269 case DW_TAG_ptr_to_member_type:
10270 case DW_TAG_reference_type:
10271 case DW_TAG_rvalue_reference_type:
10272 case DW_TAG_string_type:
10273 break;
10274
10275 case DW_TAG_array_type:
10276 /* We only need to handle this case for Ada -- in other
10277 languages, it's normal for the compiler to emit a typedef
10278 instead. */
10279 if (cu->language != language_ada)
10280 break;
10281 /* FALLTHROUGH */
10282 case DW_TAG_base_type:
10283 case DW_TAG_subrange_type:
10284 case DW_TAG_typedef:
10285 /* Add a typedef symbol for the type definition, if it has a
10286 DW_AT_name. */
10287 new_symbol (die, read_type_die (die, cu), cu);
10288 break;
10289 case DW_TAG_common_block:
10290 read_common_block (die, cu);
10291 break;
10292 case DW_TAG_common_inclusion:
10293 break;
10294 case DW_TAG_namespace:
10295 cu->processing_has_namespace_info = true;
10296 read_namespace (die, cu);
10297 break;
10298 case DW_TAG_module:
10299 cu->processing_has_namespace_info = true;
10300 read_module (die, cu);
10301 break;
10302 case DW_TAG_imported_declaration:
10303 cu->processing_has_namespace_info = true;
10304 if (read_namespace_alias (die, cu))
10305 break;
10306 /* The declaration is not a global namespace alias. */
10307 /* Fall through. */
10308 case DW_TAG_imported_module:
10309 cu->processing_has_namespace_info = true;
10310 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10311 || cu->language != language_fortran))
10312 complaint (_("Tag '%s' has unexpected children"),
10313 dwarf_tag_name (die->tag));
10314 read_import_statement (die, cu);
10315 break;
10316
10317 case DW_TAG_imported_unit:
10318 process_imported_unit_die (die, cu);
10319 break;
10320
10321 case DW_TAG_variable:
10322 read_variable (die, cu);
10323 break;
10324
10325 default:
10326 new_symbol (die, NULL, cu);
10327 break;
10328 }
10329 }
10330 \f
10331 /* DWARF name computation. */
10332
10333 /* A helper function for dwarf2_compute_name which determines whether DIE
10334 needs to have the name of the scope prepended to the name listed in the
10335 die. */
10336
10337 static int
10338 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10339 {
10340 struct attribute *attr;
10341
10342 switch (die->tag)
10343 {
10344 case DW_TAG_namespace:
10345 case DW_TAG_typedef:
10346 case DW_TAG_class_type:
10347 case DW_TAG_interface_type:
10348 case DW_TAG_structure_type:
10349 case DW_TAG_union_type:
10350 case DW_TAG_enumeration_type:
10351 case DW_TAG_enumerator:
10352 case DW_TAG_subprogram:
10353 case DW_TAG_inlined_subroutine:
10354 case DW_TAG_member:
10355 case DW_TAG_imported_declaration:
10356 return 1;
10357
10358 case DW_TAG_variable:
10359 case DW_TAG_constant:
10360 /* We only need to prefix "globally" visible variables. These include
10361 any variable marked with DW_AT_external or any variable that
10362 lives in a namespace. [Variables in anonymous namespaces
10363 require prefixing, but they are not DW_AT_external.] */
10364
10365 if (dwarf2_attr (die, DW_AT_specification, cu))
10366 {
10367 struct dwarf2_cu *spec_cu = cu;
10368
10369 return die_needs_namespace (die_specification (die, &spec_cu),
10370 spec_cu);
10371 }
10372
10373 attr = dwarf2_attr (die, DW_AT_external, cu);
10374 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10375 && die->parent->tag != DW_TAG_module)
10376 return 0;
10377 /* A variable in a lexical block of some kind does not need a
10378 namespace, even though in C++ such variables may be external
10379 and have a mangled name. */
10380 if (die->parent->tag == DW_TAG_lexical_block
10381 || die->parent->tag == DW_TAG_try_block
10382 || die->parent->tag == DW_TAG_catch_block
10383 || die->parent->tag == DW_TAG_subprogram)
10384 return 0;
10385 return 1;
10386
10387 default:
10388 return 0;
10389 }
10390 }
10391
10392 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10393 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10394 defined for the given DIE. */
10395
10396 static struct attribute *
10397 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10398 {
10399 struct attribute *attr;
10400
10401 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10402 if (attr == NULL)
10403 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10404
10405 return attr;
10406 }
10407
10408 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10409 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10410 defined for the given DIE. */
10411
10412 static const char *
10413 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10414 {
10415 const char *linkage_name;
10416
10417 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10418 if (linkage_name == NULL)
10419 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10420
10421 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10422 See https://github.com/rust-lang/rust/issues/32925. */
10423 if (cu->language == language_rust && linkage_name != NULL
10424 && strchr (linkage_name, '{') != NULL)
10425 linkage_name = NULL;
10426
10427 return linkage_name;
10428 }
10429
10430 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10431 compute the physname for the object, which include a method's:
10432 - formal parameters (C++),
10433 - receiver type (Go),
10434
10435 The term "physname" is a bit confusing.
10436 For C++, for example, it is the demangled name.
10437 For Go, for example, it's the mangled name.
10438
10439 For Ada, return the DIE's linkage name rather than the fully qualified
10440 name. PHYSNAME is ignored..
10441
10442 The result is allocated on the objfile->per_bfd's obstack and
10443 canonicalized. */
10444
10445 static const char *
10446 dwarf2_compute_name (const char *name,
10447 struct die_info *die, struct dwarf2_cu *cu,
10448 int physname)
10449 {
10450 struct objfile *objfile = cu->per_objfile->objfile;
10451
10452 if (name == NULL)
10453 name = dwarf2_name (die, cu);
10454
10455 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10456 but otherwise compute it by typename_concat inside GDB.
10457 FIXME: Actually this is not really true, or at least not always true.
10458 It's all very confusing. compute_and_set_names doesn't try to demangle
10459 Fortran names because there is no mangling standard. So new_symbol
10460 will set the demangled name to the result of dwarf2_full_name, and it is
10461 the demangled name that GDB uses if it exists. */
10462 if (cu->language == language_ada
10463 || (cu->language == language_fortran && physname))
10464 {
10465 /* For Ada unit, we prefer the linkage name over the name, as
10466 the former contains the exported name, which the user expects
10467 to be able to reference. Ideally, we want the user to be able
10468 to reference this entity using either natural or linkage name,
10469 but we haven't started looking at this enhancement yet. */
10470 const char *linkage_name = dw2_linkage_name (die, cu);
10471
10472 if (linkage_name != NULL)
10473 return linkage_name;
10474 }
10475
10476 /* These are the only languages we know how to qualify names in. */
10477 if (name != NULL
10478 && (cu->language == language_cplus
10479 || cu->language == language_fortran || cu->language == language_d
10480 || cu->language == language_rust))
10481 {
10482 if (die_needs_namespace (die, cu))
10483 {
10484 const char *prefix;
10485 const char *canonical_name = NULL;
10486
10487 string_file buf;
10488
10489 prefix = determine_prefix (die, cu);
10490 if (*prefix != '\0')
10491 {
10492 gdb::unique_xmalloc_ptr<char> prefixed_name
10493 (typename_concat (NULL, prefix, name, physname, cu));
10494
10495 buf.puts (prefixed_name.get ());
10496 }
10497 else
10498 buf.puts (name);
10499
10500 /* Template parameters may be specified in the DIE's DW_AT_name, or
10501 as children with DW_TAG_template_type_param or
10502 DW_TAG_value_type_param. If the latter, add them to the name
10503 here. If the name already has template parameters, then
10504 skip this step; some versions of GCC emit both, and
10505 it is more efficient to use the pre-computed name.
10506
10507 Something to keep in mind about this process: it is very
10508 unlikely, or in some cases downright impossible, to produce
10509 something that will match the mangled name of a function.
10510 If the definition of the function has the same debug info,
10511 we should be able to match up with it anyway. But fallbacks
10512 using the minimal symbol, for instance to find a method
10513 implemented in a stripped copy of libstdc++, will not work.
10514 If we do not have debug info for the definition, we will have to
10515 match them up some other way.
10516
10517 When we do name matching there is a related problem with function
10518 templates; two instantiated function templates are allowed to
10519 differ only by their return types, which we do not add here. */
10520
10521 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10522 {
10523 struct attribute *attr;
10524 struct die_info *child;
10525 int first = 1;
10526 const language_defn *cplus_lang = language_def (cu->language);
10527
10528 die->building_fullname = 1;
10529
10530 for (child = die->child; child != NULL; child = child->sibling)
10531 {
10532 struct type *type;
10533 LONGEST value;
10534 const gdb_byte *bytes;
10535 struct dwarf2_locexpr_baton *baton;
10536 struct value *v;
10537
10538 if (child->tag != DW_TAG_template_type_param
10539 && child->tag != DW_TAG_template_value_param)
10540 continue;
10541
10542 if (first)
10543 {
10544 buf.puts ("<");
10545 first = 0;
10546 }
10547 else
10548 buf.puts (", ");
10549
10550 attr = dwarf2_attr (child, DW_AT_type, cu);
10551 if (attr == NULL)
10552 {
10553 complaint (_("template parameter missing DW_AT_type"));
10554 buf.puts ("UNKNOWN_TYPE");
10555 continue;
10556 }
10557 type = die_type (child, cu);
10558
10559 if (child->tag == DW_TAG_template_type_param)
10560 {
10561 cplus_lang->print_type (type, "", &buf, -1, 0,
10562 &type_print_raw_options);
10563 continue;
10564 }
10565
10566 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10567 if (attr == NULL)
10568 {
10569 complaint (_("template parameter missing "
10570 "DW_AT_const_value"));
10571 buf.puts ("UNKNOWN_VALUE");
10572 continue;
10573 }
10574
10575 dwarf2_const_value_attr (attr, type, name,
10576 &cu->comp_unit_obstack, cu,
10577 &value, &bytes, &baton);
10578
10579 if (type->has_no_signedness ())
10580 /* GDB prints characters as NUMBER 'CHAR'. If that's
10581 changed, this can use value_print instead. */
10582 cplus_lang->printchar (value, type, &buf);
10583 else
10584 {
10585 struct value_print_options opts;
10586
10587 if (baton != NULL)
10588 v = dwarf2_evaluate_loc_desc (type, NULL,
10589 baton->data,
10590 baton->size,
10591 baton->per_cu,
10592 baton->per_objfile);
10593 else if (bytes != NULL)
10594 {
10595 v = allocate_value (type);
10596 memcpy (value_contents_writeable (v), bytes,
10597 TYPE_LENGTH (type));
10598 }
10599 else
10600 v = value_from_longest (type, value);
10601
10602 /* Specify decimal so that we do not depend on
10603 the radix. */
10604 get_formatted_print_options (&opts, 'd');
10605 opts.raw = 1;
10606 value_print (v, &buf, &opts);
10607 release_value (v);
10608 }
10609 }
10610
10611 die->building_fullname = 0;
10612
10613 if (!first)
10614 {
10615 /* Close the argument list, with a space if necessary
10616 (nested templates). */
10617 if (!buf.empty () && buf.string ().back () == '>')
10618 buf.puts (" >");
10619 else
10620 buf.puts (">");
10621 }
10622 }
10623
10624 /* For C++ methods, append formal parameter type
10625 information, if PHYSNAME. */
10626
10627 if (physname && die->tag == DW_TAG_subprogram
10628 && cu->language == language_cplus)
10629 {
10630 struct type *type = read_type_die (die, cu);
10631
10632 c_type_print_args (type, &buf, 1, cu->language,
10633 &type_print_raw_options);
10634
10635 if (cu->language == language_cplus)
10636 {
10637 /* Assume that an artificial first parameter is
10638 "this", but do not crash if it is not. RealView
10639 marks unnamed (and thus unused) parameters as
10640 artificial; there is no way to differentiate
10641 the two cases. */
10642 if (type->num_fields () > 0
10643 && TYPE_FIELD_ARTIFICIAL (type, 0)
10644 && type->field (0).type ()->code () == TYPE_CODE_PTR
10645 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10646 buf.puts (" const");
10647 }
10648 }
10649
10650 const std::string &intermediate_name = buf.string ();
10651
10652 if (cu->language == language_cplus)
10653 canonical_name
10654 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10655 objfile);
10656
10657 /* If we only computed INTERMEDIATE_NAME, or if
10658 INTERMEDIATE_NAME is already canonical, then we need to
10659 intern it. */
10660 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10661 name = objfile->intern (intermediate_name);
10662 else
10663 name = canonical_name;
10664 }
10665 }
10666
10667 return name;
10668 }
10669
10670 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10671 If scope qualifiers are appropriate they will be added. The result
10672 will be allocated on the storage_obstack, or NULL if the DIE does
10673 not have a name. NAME may either be from a previous call to
10674 dwarf2_name or NULL.
10675
10676 The output string will be canonicalized (if C++). */
10677
10678 static const char *
10679 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10680 {
10681 return dwarf2_compute_name (name, die, cu, 0);
10682 }
10683
10684 /* Construct a physname for the given DIE in CU. NAME may either be
10685 from a previous call to dwarf2_name or NULL. The result will be
10686 allocated on the objfile_objstack or NULL if the DIE does not have a
10687 name.
10688
10689 The output string will be canonicalized (if C++). */
10690
10691 static const char *
10692 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10693 {
10694 struct objfile *objfile = cu->per_objfile->objfile;
10695 const char *retval, *mangled = NULL, *canon = NULL;
10696 int need_copy = 1;
10697
10698 /* In this case dwarf2_compute_name is just a shortcut not building anything
10699 on its own. */
10700 if (!die_needs_namespace (die, cu))
10701 return dwarf2_compute_name (name, die, cu, 1);
10702
10703 if (cu->language != language_rust)
10704 mangled = dw2_linkage_name (die, cu);
10705
10706 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10707 has computed. */
10708 gdb::unique_xmalloc_ptr<char> demangled;
10709 if (mangled != NULL)
10710 {
10711
10712 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10713 {
10714 /* Do nothing (do not demangle the symbol name). */
10715 }
10716 else
10717 {
10718 /* Use DMGL_RET_DROP for C++ template functions to suppress
10719 their return type. It is easier for GDB users to search
10720 for such functions as `name(params)' than `long name(params)'.
10721 In such case the minimal symbol names do not match the full
10722 symbol names but for template functions there is never a need
10723 to look up their definition from their declaration so
10724 the only disadvantage remains the minimal symbol variant
10725 `long name(params)' does not have the proper inferior type. */
10726 demangled.reset (gdb_demangle (mangled,
10727 (DMGL_PARAMS | DMGL_ANSI
10728 | DMGL_RET_DROP)));
10729 }
10730 if (demangled)
10731 canon = demangled.get ();
10732 else
10733 {
10734 canon = mangled;
10735 need_copy = 0;
10736 }
10737 }
10738
10739 if (canon == NULL || check_physname)
10740 {
10741 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10742
10743 if (canon != NULL && strcmp (physname, canon) != 0)
10744 {
10745 /* It may not mean a bug in GDB. The compiler could also
10746 compute DW_AT_linkage_name incorrectly. But in such case
10747 GDB would need to be bug-to-bug compatible. */
10748
10749 complaint (_("Computed physname <%s> does not match demangled <%s> "
10750 "(from linkage <%s>) - DIE at %s [in module %s]"),
10751 physname, canon, mangled, sect_offset_str (die->sect_off),
10752 objfile_name (objfile));
10753
10754 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10755 is available here - over computed PHYSNAME. It is safer
10756 against both buggy GDB and buggy compilers. */
10757
10758 retval = canon;
10759 }
10760 else
10761 {
10762 retval = physname;
10763 need_copy = 0;
10764 }
10765 }
10766 else
10767 retval = canon;
10768
10769 if (need_copy)
10770 retval = objfile->intern (retval);
10771
10772 return retval;
10773 }
10774
10775 /* Inspect DIE in CU for a namespace alias. If one exists, record
10776 a new symbol for it.
10777
10778 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10779
10780 static int
10781 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10782 {
10783 struct attribute *attr;
10784
10785 /* If the die does not have a name, this is not a namespace
10786 alias. */
10787 attr = dwarf2_attr (die, DW_AT_name, cu);
10788 if (attr != NULL)
10789 {
10790 int num;
10791 struct die_info *d = die;
10792 struct dwarf2_cu *imported_cu = cu;
10793
10794 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10795 keep inspecting DIEs until we hit the underlying import. */
10796 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10797 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10798 {
10799 attr = dwarf2_attr (d, DW_AT_import, cu);
10800 if (attr == NULL)
10801 break;
10802
10803 d = follow_die_ref (d, attr, &imported_cu);
10804 if (d->tag != DW_TAG_imported_declaration)
10805 break;
10806 }
10807
10808 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10809 {
10810 complaint (_("DIE at %s has too many recursively imported "
10811 "declarations"), sect_offset_str (d->sect_off));
10812 return 0;
10813 }
10814
10815 if (attr != NULL)
10816 {
10817 struct type *type;
10818 sect_offset sect_off = attr->get_ref_die_offset ();
10819
10820 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10821 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10822 {
10823 /* This declaration is a global namespace alias. Add
10824 a symbol for it whose type is the aliased namespace. */
10825 new_symbol (die, type, cu);
10826 return 1;
10827 }
10828 }
10829 }
10830
10831 return 0;
10832 }
10833
10834 /* Return the using directives repository (global or local?) to use in the
10835 current context for CU.
10836
10837 For Ada, imported declarations can materialize renamings, which *may* be
10838 global. However it is impossible (for now?) in DWARF to distinguish
10839 "external" imported declarations and "static" ones. As all imported
10840 declarations seem to be static in all other languages, make them all CU-wide
10841 global only in Ada. */
10842
10843 static struct using_direct **
10844 using_directives (struct dwarf2_cu *cu)
10845 {
10846 if (cu->language == language_ada
10847 && cu->get_builder ()->outermost_context_p ())
10848 return cu->get_builder ()->get_global_using_directives ();
10849 else
10850 return cu->get_builder ()->get_local_using_directives ();
10851 }
10852
10853 /* Read the import statement specified by the given die and record it. */
10854
10855 static void
10856 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10857 {
10858 struct objfile *objfile = cu->per_objfile->objfile;
10859 struct attribute *import_attr;
10860 struct die_info *imported_die, *child_die;
10861 struct dwarf2_cu *imported_cu;
10862 const char *imported_name;
10863 const char *imported_name_prefix;
10864 const char *canonical_name;
10865 const char *import_alias;
10866 const char *imported_declaration = NULL;
10867 const char *import_prefix;
10868 std::vector<const char *> excludes;
10869
10870 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10871 if (import_attr == NULL)
10872 {
10873 complaint (_("Tag '%s' has no DW_AT_import"),
10874 dwarf_tag_name (die->tag));
10875 return;
10876 }
10877
10878 imported_cu = cu;
10879 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10880 imported_name = dwarf2_name (imported_die, imported_cu);
10881 if (imported_name == NULL)
10882 {
10883 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10884
10885 The import in the following code:
10886 namespace A
10887 {
10888 typedef int B;
10889 }
10890
10891 int main ()
10892 {
10893 using A::B;
10894 B b;
10895 return b;
10896 }
10897
10898 ...
10899 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10900 <52> DW_AT_decl_file : 1
10901 <53> DW_AT_decl_line : 6
10902 <54> DW_AT_import : <0x75>
10903 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10904 <59> DW_AT_name : B
10905 <5b> DW_AT_decl_file : 1
10906 <5c> DW_AT_decl_line : 2
10907 <5d> DW_AT_type : <0x6e>
10908 ...
10909 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10910 <76> DW_AT_byte_size : 4
10911 <77> DW_AT_encoding : 5 (signed)
10912
10913 imports the wrong die ( 0x75 instead of 0x58 ).
10914 This case will be ignored until the gcc bug is fixed. */
10915 return;
10916 }
10917
10918 /* Figure out the local name after import. */
10919 import_alias = dwarf2_name (die, cu);
10920
10921 /* Figure out where the statement is being imported to. */
10922 import_prefix = determine_prefix (die, cu);
10923
10924 /* Figure out what the scope of the imported die is and prepend it
10925 to the name of the imported die. */
10926 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10927
10928 if (imported_die->tag != DW_TAG_namespace
10929 && imported_die->tag != DW_TAG_module)
10930 {
10931 imported_declaration = imported_name;
10932 canonical_name = imported_name_prefix;
10933 }
10934 else if (strlen (imported_name_prefix) > 0)
10935 canonical_name = obconcat (&objfile->objfile_obstack,
10936 imported_name_prefix,
10937 (cu->language == language_d ? "." : "::"),
10938 imported_name, (char *) NULL);
10939 else
10940 canonical_name = imported_name;
10941
10942 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10943 for (child_die = die->child; child_die && child_die->tag;
10944 child_die = child_die->sibling)
10945 {
10946 /* DWARF-4: A Fortran use statement with a “rename list” may be
10947 represented by an imported module entry with an import attribute
10948 referring to the module and owned entries corresponding to those
10949 entities that are renamed as part of being imported. */
10950
10951 if (child_die->tag != DW_TAG_imported_declaration)
10952 {
10953 complaint (_("child DW_TAG_imported_declaration expected "
10954 "- DIE at %s [in module %s]"),
10955 sect_offset_str (child_die->sect_off),
10956 objfile_name (objfile));
10957 continue;
10958 }
10959
10960 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10961 if (import_attr == NULL)
10962 {
10963 complaint (_("Tag '%s' has no DW_AT_import"),
10964 dwarf_tag_name (child_die->tag));
10965 continue;
10966 }
10967
10968 imported_cu = cu;
10969 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10970 &imported_cu);
10971 imported_name = dwarf2_name (imported_die, imported_cu);
10972 if (imported_name == NULL)
10973 {
10974 complaint (_("child DW_TAG_imported_declaration has unknown "
10975 "imported name - DIE at %s [in module %s]"),
10976 sect_offset_str (child_die->sect_off),
10977 objfile_name (objfile));
10978 continue;
10979 }
10980
10981 excludes.push_back (imported_name);
10982
10983 process_die (child_die, cu);
10984 }
10985
10986 add_using_directive (using_directives (cu),
10987 import_prefix,
10988 canonical_name,
10989 import_alias,
10990 imported_declaration,
10991 excludes,
10992 0,
10993 &objfile->objfile_obstack);
10994 }
10995
10996 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10997 types, but gives them a size of zero. Starting with version 14,
10998 ICC is compatible with GCC. */
10999
11000 static bool
11001 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11002 {
11003 if (!cu->checked_producer)
11004 check_producer (cu);
11005
11006 return cu->producer_is_icc_lt_14;
11007 }
11008
11009 /* ICC generates a DW_AT_type for C void functions. This was observed on
11010 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11011 which says that void functions should not have a DW_AT_type. */
11012
11013 static bool
11014 producer_is_icc (struct dwarf2_cu *cu)
11015 {
11016 if (!cu->checked_producer)
11017 check_producer (cu);
11018
11019 return cu->producer_is_icc;
11020 }
11021
11022 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11023 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11024 this, it was first present in GCC release 4.3.0. */
11025
11026 static bool
11027 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11028 {
11029 if (!cu->checked_producer)
11030 check_producer (cu);
11031
11032 return cu->producer_is_gcc_lt_4_3;
11033 }
11034
11035 static file_and_directory
11036 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11037 {
11038 file_and_directory res;
11039
11040 /* Find the filename. Do not use dwarf2_name here, since the filename
11041 is not a source language identifier. */
11042 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11043 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11044
11045 if (res.comp_dir == NULL
11046 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11047 && IS_ABSOLUTE_PATH (res.name))
11048 {
11049 res.comp_dir_storage = ldirname (res.name);
11050 if (!res.comp_dir_storage.empty ())
11051 res.comp_dir = res.comp_dir_storage.c_str ();
11052 }
11053 if (res.comp_dir != NULL)
11054 {
11055 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11056 directory, get rid of it. */
11057 const char *cp = strchr (res.comp_dir, ':');
11058
11059 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11060 res.comp_dir = cp + 1;
11061 }
11062
11063 if (res.name == NULL)
11064 res.name = "<unknown>";
11065
11066 return res;
11067 }
11068
11069 /* Handle DW_AT_stmt_list for a compilation unit.
11070 DIE is the DW_TAG_compile_unit die for CU.
11071 COMP_DIR is the compilation directory. LOWPC is passed to
11072 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11073
11074 static void
11075 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11076 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11077 {
11078 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11079 struct attribute *attr;
11080 struct line_header line_header_local;
11081 hashval_t line_header_local_hash;
11082 void **slot;
11083 int decode_mapping;
11084
11085 gdb_assert (! cu->per_cu->is_debug_types);
11086
11087 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11088 if (attr == NULL || !attr->form_is_unsigned ())
11089 return;
11090
11091 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11092
11093 /* The line header hash table is only created if needed (it exists to
11094 prevent redundant reading of the line table for partial_units).
11095 If we're given a partial_unit, we'll need it. If we're given a
11096 compile_unit, then use the line header hash table if it's already
11097 created, but don't create one just yet. */
11098
11099 if (per_objfile->line_header_hash == NULL
11100 && die->tag == DW_TAG_partial_unit)
11101 {
11102 per_objfile->line_header_hash
11103 .reset (htab_create_alloc (127, line_header_hash_voidp,
11104 line_header_eq_voidp,
11105 free_line_header_voidp,
11106 xcalloc, xfree));
11107 }
11108
11109 line_header_local.sect_off = line_offset;
11110 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11111 line_header_local_hash = line_header_hash (&line_header_local);
11112 if (per_objfile->line_header_hash != NULL)
11113 {
11114 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11115 &line_header_local,
11116 line_header_local_hash, NO_INSERT);
11117
11118 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11119 is not present in *SLOT (since if there is something in *SLOT then
11120 it will be for a partial_unit). */
11121 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11122 {
11123 gdb_assert (*slot != NULL);
11124 cu->line_header = (struct line_header *) *slot;
11125 return;
11126 }
11127 }
11128
11129 /* dwarf_decode_line_header does not yet provide sufficient information.
11130 We always have to call also dwarf_decode_lines for it. */
11131 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11132 if (lh == NULL)
11133 return;
11134
11135 cu->line_header = lh.release ();
11136 cu->line_header_die_owner = die;
11137
11138 if (per_objfile->line_header_hash == NULL)
11139 slot = NULL;
11140 else
11141 {
11142 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11143 &line_header_local,
11144 line_header_local_hash, INSERT);
11145 gdb_assert (slot != NULL);
11146 }
11147 if (slot != NULL && *slot == NULL)
11148 {
11149 /* This newly decoded line number information unit will be owned
11150 by line_header_hash hash table. */
11151 *slot = cu->line_header;
11152 cu->line_header_die_owner = NULL;
11153 }
11154 else
11155 {
11156 /* We cannot free any current entry in (*slot) as that struct line_header
11157 may be already used by multiple CUs. Create only temporary decoded
11158 line_header for this CU - it may happen at most once for each line
11159 number information unit. And if we're not using line_header_hash
11160 then this is what we want as well. */
11161 gdb_assert (die->tag != DW_TAG_partial_unit);
11162 }
11163 decode_mapping = (die->tag != DW_TAG_partial_unit);
11164 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11165 decode_mapping);
11166
11167 }
11168
11169 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11170
11171 static void
11172 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11173 {
11174 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11175 struct objfile *objfile = per_objfile->objfile;
11176 struct gdbarch *gdbarch = objfile->arch ();
11177 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11178 CORE_ADDR highpc = ((CORE_ADDR) 0);
11179 struct attribute *attr;
11180 struct die_info *child_die;
11181 CORE_ADDR baseaddr;
11182
11183 prepare_one_comp_unit (cu, die, cu->language);
11184 baseaddr = objfile->text_section_offset ();
11185
11186 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11187
11188 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11189 from finish_block. */
11190 if (lowpc == ((CORE_ADDR) -1))
11191 lowpc = highpc;
11192 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11193
11194 file_and_directory fnd = find_file_and_directory (die, cu);
11195
11196 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11197 standardised yet. As a workaround for the language detection we fall
11198 back to the DW_AT_producer string. */
11199 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11200 cu->language = language_opencl;
11201
11202 /* Similar hack for Go. */
11203 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11204 set_cu_language (DW_LANG_Go, cu);
11205
11206 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11207
11208 /* Decode line number information if present. We do this before
11209 processing child DIEs, so that the line header table is available
11210 for DW_AT_decl_file. */
11211 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11212
11213 /* Process all dies in compilation unit. */
11214 if (die->child != NULL)
11215 {
11216 child_die = die->child;
11217 while (child_die && child_die->tag)
11218 {
11219 process_die (child_die, cu);
11220 child_die = child_die->sibling;
11221 }
11222 }
11223
11224 /* Decode macro information, if present. Dwarf 2 macro information
11225 refers to information in the line number info statement program
11226 header, so we can only read it if we've read the header
11227 successfully. */
11228 attr = dwarf2_attr (die, DW_AT_macros, cu);
11229 if (attr == NULL)
11230 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11231 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11232 {
11233 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11234 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11235
11236 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11237 }
11238 else
11239 {
11240 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11241 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11242 {
11243 unsigned int macro_offset = attr->as_unsigned ();
11244
11245 dwarf_decode_macros (cu, macro_offset, 0);
11246 }
11247 }
11248 }
11249
11250 void
11251 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11252 {
11253 struct type_unit_group *tu_group;
11254 int first_time;
11255 struct attribute *attr;
11256 unsigned int i;
11257 struct signatured_type *sig_type;
11258
11259 gdb_assert (per_cu->is_debug_types);
11260 sig_type = (struct signatured_type *) per_cu;
11261
11262 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11263
11264 /* If we're using .gdb_index (includes -readnow) then
11265 per_cu->type_unit_group may not have been set up yet. */
11266 if (sig_type->type_unit_group == NULL)
11267 sig_type->type_unit_group = get_type_unit_group (this, attr);
11268 tu_group = sig_type->type_unit_group;
11269
11270 /* If we've already processed this stmt_list there's no real need to
11271 do it again, we could fake it and just recreate the part we need
11272 (file name,index -> symtab mapping). If data shows this optimization
11273 is useful we can do it then. */
11274 type_unit_group_unshareable *tug_unshare
11275 = per_objfile->get_type_unit_group_unshareable (tu_group);
11276 first_time = tug_unshare->compunit_symtab == NULL;
11277
11278 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11279 debug info. */
11280 line_header_up lh;
11281 if (attr != NULL && attr->form_is_unsigned ())
11282 {
11283 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11284 lh = dwarf_decode_line_header (line_offset, this);
11285 }
11286 if (lh == NULL)
11287 {
11288 if (first_time)
11289 start_symtab ("", NULL, 0);
11290 else
11291 {
11292 gdb_assert (tug_unshare->symtabs == NULL);
11293 gdb_assert (m_builder == nullptr);
11294 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11295 m_builder.reset (new struct buildsym_compunit
11296 (COMPUNIT_OBJFILE (cust), "",
11297 COMPUNIT_DIRNAME (cust),
11298 compunit_language (cust),
11299 0, cust));
11300 list_in_scope = get_builder ()->get_file_symbols ();
11301 }
11302 return;
11303 }
11304
11305 line_header = lh.release ();
11306 line_header_die_owner = die;
11307
11308 if (first_time)
11309 {
11310 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11311
11312 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11313 still initializing it, and our caller (a few levels up)
11314 process_full_type_unit still needs to know if this is the first
11315 time. */
11316
11317 tug_unshare->symtabs
11318 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11319 struct symtab *, line_header->file_names_size ());
11320
11321 auto &file_names = line_header->file_names ();
11322 for (i = 0; i < file_names.size (); ++i)
11323 {
11324 file_entry &fe = file_names[i];
11325 dwarf2_start_subfile (this, fe.name,
11326 fe.include_dir (line_header));
11327 buildsym_compunit *b = get_builder ();
11328 if (b->get_current_subfile ()->symtab == NULL)
11329 {
11330 /* NOTE: start_subfile will recognize when it's been
11331 passed a file it has already seen. So we can't
11332 assume there's a simple mapping from
11333 cu->line_header->file_names to subfiles, plus
11334 cu->line_header->file_names may contain dups. */
11335 b->get_current_subfile ()->symtab
11336 = allocate_symtab (cust, b->get_current_subfile ()->name);
11337 }
11338
11339 fe.symtab = b->get_current_subfile ()->symtab;
11340 tug_unshare->symtabs[i] = fe.symtab;
11341 }
11342 }
11343 else
11344 {
11345 gdb_assert (m_builder == nullptr);
11346 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11347 m_builder.reset (new struct buildsym_compunit
11348 (COMPUNIT_OBJFILE (cust), "",
11349 COMPUNIT_DIRNAME (cust),
11350 compunit_language (cust),
11351 0, cust));
11352 list_in_scope = get_builder ()->get_file_symbols ();
11353
11354 auto &file_names = line_header->file_names ();
11355 for (i = 0; i < file_names.size (); ++i)
11356 {
11357 file_entry &fe = file_names[i];
11358 fe.symtab = tug_unshare->symtabs[i];
11359 }
11360 }
11361
11362 /* The main symtab is allocated last. Type units don't have DW_AT_name
11363 so they don't have a "real" (so to speak) symtab anyway.
11364 There is later code that will assign the main symtab to all symbols
11365 that don't have one. We need to handle the case of a symbol with a
11366 missing symtab (DW_AT_decl_file) anyway. */
11367 }
11368
11369 /* Process DW_TAG_type_unit.
11370 For TUs we want to skip the first top level sibling if it's not the
11371 actual type being defined by this TU. In this case the first top
11372 level sibling is there to provide context only. */
11373
11374 static void
11375 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11376 {
11377 struct die_info *child_die;
11378
11379 prepare_one_comp_unit (cu, die, language_minimal);
11380
11381 /* Initialize (or reinitialize) the machinery for building symtabs.
11382 We do this before processing child DIEs, so that the line header table
11383 is available for DW_AT_decl_file. */
11384 cu->setup_type_unit_groups (die);
11385
11386 if (die->child != NULL)
11387 {
11388 child_die = die->child;
11389 while (child_die && child_die->tag)
11390 {
11391 process_die (child_die, cu);
11392 child_die = child_die->sibling;
11393 }
11394 }
11395 }
11396 \f
11397 /* DWO/DWP files.
11398
11399 http://gcc.gnu.org/wiki/DebugFission
11400 http://gcc.gnu.org/wiki/DebugFissionDWP
11401
11402 To simplify handling of both DWO files ("object" files with the DWARF info)
11403 and DWP files (a file with the DWOs packaged up into one file), we treat
11404 DWP files as having a collection of virtual DWO files. */
11405
11406 static hashval_t
11407 hash_dwo_file (const void *item)
11408 {
11409 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11410 hashval_t hash;
11411
11412 hash = htab_hash_string (dwo_file->dwo_name);
11413 if (dwo_file->comp_dir != NULL)
11414 hash += htab_hash_string (dwo_file->comp_dir);
11415 return hash;
11416 }
11417
11418 static int
11419 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11420 {
11421 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11422 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11423
11424 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11425 return 0;
11426 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11427 return lhs->comp_dir == rhs->comp_dir;
11428 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11429 }
11430
11431 /* Allocate a hash table for DWO files. */
11432
11433 static htab_up
11434 allocate_dwo_file_hash_table ()
11435 {
11436 auto delete_dwo_file = [] (void *item)
11437 {
11438 struct dwo_file *dwo_file = (struct dwo_file *) item;
11439
11440 delete dwo_file;
11441 };
11442
11443 return htab_up (htab_create_alloc (41,
11444 hash_dwo_file,
11445 eq_dwo_file,
11446 delete_dwo_file,
11447 xcalloc, xfree));
11448 }
11449
11450 /* Lookup DWO file DWO_NAME. */
11451
11452 static void **
11453 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11454 const char *dwo_name,
11455 const char *comp_dir)
11456 {
11457 struct dwo_file find_entry;
11458 void **slot;
11459
11460 if (per_objfile->per_bfd->dwo_files == NULL)
11461 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11462
11463 find_entry.dwo_name = dwo_name;
11464 find_entry.comp_dir = comp_dir;
11465 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11466 INSERT);
11467
11468 return slot;
11469 }
11470
11471 static hashval_t
11472 hash_dwo_unit (const void *item)
11473 {
11474 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11475
11476 /* This drops the top 32 bits of the id, but is ok for a hash. */
11477 return dwo_unit->signature;
11478 }
11479
11480 static int
11481 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11482 {
11483 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11484 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11485
11486 /* The signature is assumed to be unique within the DWO file.
11487 So while object file CU dwo_id's always have the value zero,
11488 that's OK, assuming each object file DWO file has only one CU,
11489 and that's the rule for now. */
11490 return lhs->signature == rhs->signature;
11491 }
11492
11493 /* Allocate a hash table for DWO CUs,TUs.
11494 There is one of these tables for each of CUs,TUs for each DWO file. */
11495
11496 static htab_up
11497 allocate_dwo_unit_table ()
11498 {
11499 /* Start out with a pretty small number.
11500 Generally DWO files contain only one CU and maybe some TUs. */
11501 return htab_up (htab_create_alloc (3,
11502 hash_dwo_unit,
11503 eq_dwo_unit,
11504 NULL, xcalloc, xfree));
11505 }
11506
11507 /* die_reader_func for create_dwo_cu. */
11508
11509 static void
11510 create_dwo_cu_reader (const struct die_reader_specs *reader,
11511 const gdb_byte *info_ptr,
11512 struct die_info *comp_unit_die,
11513 struct dwo_file *dwo_file,
11514 struct dwo_unit *dwo_unit)
11515 {
11516 struct dwarf2_cu *cu = reader->cu;
11517 sect_offset sect_off = cu->per_cu->sect_off;
11518 struct dwarf2_section_info *section = cu->per_cu->section;
11519
11520 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11521 if (!signature.has_value ())
11522 {
11523 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11524 " its dwo_id [in module %s]"),
11525 sect_offset_str (sect_off), dwo_file->dwo_name);
11526 return;
11527 }
11528
11529 dwo_unit->dwo_file = dwo_file;
11530 dwo_unit->signature = *signature;
11531 dwo_unit->section = section;
11532 dwo_unit->sect_off = sect_off;
11533 dwo_unit->length = cu->per_cu->length;
11534
11535 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11536 sect_offset_str (sect_off),
11537 hex_string (dwo_unit->signature));
11538 }
11539
11540 /* Create the dwo_units for the CUs in a DWO_FILE.
11541 Note: This function processes DWO files only, not DWP files. */
11542
11543 static void
11544 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11545 dwarf2_cu *cu, struct dwo_file &dwo_file,
11546 dwarf2_section_info &section, htab_up &cus_htab)
11547 {
11548 struct objfile *objfile = per_objfile->objfile;
11549 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11550 const gdb_byte *info_ptr, *end_ptr;
11551
11552 section.read (objfile);
11553 info_ptr = section.buffer;
11554
11555 if (info_ptr == NULL)
11556 return;
11557
11558 dwarf_read_debug_printf ("Reading %s for %s:",
11559 section.get_name (),
11560 section.get_file_name ());
11561
11562 end_ptr = info_ptr + section.size;
11563 while (info_ptr < end_ptr)
11564 {
11565 struct dwarf2_per_cu_data per_cu;
11566 struct dwo_unit read_unit {};
11567 struct dwo_unit *dwo_unit;
11568 void **slot;
11569 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11570
11571 memset (&per_cu, 0, sizeof (per_cu));
11572 per_cu.per_bfd = per_bfd;
11573 per_cu.is_debug_types = 0;
11574 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11575 per_cu.section = &section;
11576
11577 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11578 if (!reader.dummy_p)
11579 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11580 &dwo_file, &read_unit);
11581 info_ptr += per_cu.length;
11582
11583 // If the unit could not be parsed, skip it.
11584 if (read_unit.dwo_file == NULL)
11585 continue;
11586
11587 if (cus_htab == NULL)
11588 cus_htab = allocate_dwo_unit_table ();
11589
11590 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11591 struct dwo_unit);
11592 *dwo_unit = read_unit;
11593 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11594 gdb_assert (slot != NULL);
11595 if (*slot != NULL)
11596 {
11597 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11598 sect_offset dup_sect_off = dup_cu->sect_off;
11599
11600 complaint (_("debug cu entry at offset %s is duplicate to"
11601 " the entry at offset %s, signature %s"),
11602 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11603 hex_string (dwo_unit->signature));
11604 }
11605 *slot = (void *)dwo_unit;
11606 }
11607 }
11608
11609 /* DWP file .debug_{cu,tu}_index section format:
11610 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11611 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11612
11613 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11614 officially standard DWP format was published with DWARF v5 and is called
11615 Version 5. There are no versions 3 or 4.
11616
11617 DWP Version 1:
11618
11619 Both index sections have the same format, and serve to map a 64-bit
11620 signature to a set of section numbers. Each section begins with a header,
11621 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11622 indexes, and a pool of 32-bit section numbers. The index sections will be
11623 aligned at 8-byte boundaries in the file.
11624
11625 The index section header consists of:
11626
11627 V, 32 bit version number
11628 -, 32 bits unused
11629 N, 32 bit number of compilation units or type units in the index
11630 M, 32 bit number of slots in the hash table
11631
11632 Numbers are recorded using the byte order of the application binary.
11633
11634 The hash table begins at offset 16 in the section, and consists of an array
11635 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11636 order of the application binary). Unused slots in the hash table are 0.
11637 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11638
11639 The parallel table begins immediately after the hash table
11640 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11641 array of 32-bit indexes (using the byte order of the application binary),
11642 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11643 table contains a 32-bit index into the pool of section numbers. For unused
11644 hash table slots, the corresponding entry in the parallel table will be 0.
11645
11646 The pool of section numbers begins immediately following the hash table
11647 (at offset 16 + 12 * M from the beginning of the section). The pool of
11648 section numbers consists of an array of 32-bit words (using the byte order
11649 of the application binary). Each item in the array is indexed starting
11650 from 0. The hash table entry provides the index of the first section
11651 number in the set. Additional section numbers in the set follow, and the
11652 set is terminated by a 0 entry (section number 0 is not used in ELF).
11653
11654 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11655 section must be the first entry in the set, and the .debug_abbrev.dwo must
11656 be the second entry. Other members of the set may follow in any order.
11657
11658 ---
11659
11660 DWP Versions 2 and 5:
11661
11662 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11663 and the entries in the index tables are now offsets into these sections.
11664 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11665 section.
11666
11667 Index Section Contents:
11668 Header
11669 Hash Table of Signatures dwp_hash_table.hash_table
11670 Parallel Table of Indices dwp_hash_table.unit_table
11671 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11672 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11673
11674 The index section header consists of:
11675
11676 V, 32 bit version number
11677 L, 32 bit number of columns in the table of section offsets
11678 N, 32 bit number of compilation units or type units in the index
11679 M, 32 bit number of slots in the hash table
11680
11681 Numbers are recorded using the byte order of the application binary.
11682
11683 The hash table has the same format as version 1.
11684 The parallel table of indices has the same format as version 1,
11685 except that the entries are origin-1 indices into the table of sections
11686 offsets and the table of section sizes.
11687
11688 The table of offsets begins immediately following the parallel table
11689 (at offset 16 + 12 * M from the beginning of the section). The table is
11690 a two-dimensional array of 32-bit words (using the byte order of the
11691 application binary), with L columns and N+1 rows, in row-major order.
11692 Each row in the array is indexed starting from 0. The first row provides
11693 a key to the remaining rows: each column in this row provides an identifier
11694 for a debug section, and the offsets in the same column of subsequent rows
11695 refer to that section. The section identifiers for Version 2 are:
11696
11697 DW_SECT_INFO 1 .debug_info.dwo
11698 DW_SECT_TYPES 2 .debug_types.dwo
11699 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11700 DW_SECT_LINE 4 .debug_line.dwo
11701 DW_SECT_LOC 5 .debug_loc.dwo
11702 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11703 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11704 DW_SECT_MACRO 8 .debug_macro.dwo
11705
11706 The section identifiers for Version 5 are:
11707
11708 DW_SECT_INFO_V5 1 .debug_info.dwo
11709 DW_SECT_RESERVED_V5 2 --
11710 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11711 DW_SECT_LINE_V5 4 .debug_line.dwo
11712 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11713 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11714 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11715 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11716
11717 The offsets provided by the CU and TU index sections are the base offsets
11718 for the contributions made by each CU or TU to the corresponding section
11719 in the package file. Each CU and TU header contains an abbrev_offset
11720 field, used to find the abbreviations table for that CU or TU within the
11721 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11722 be interpreted as relative to the base offset given in the index section.
11723 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11724 should be interpreted as relative to the base offset for .debug_line.dwo,
11725 and offsets into other debug sections obtained from DWARF attributes should
11726 also be interpreted as relative to the corresponding base offset.
11727
11728 The table of sizes begins immediately following the table of offsets.
11729 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11730 with L columns and N rows, in row-major order. Each row in the array is
11731 indexed starting from 1 (row 0 is shared by the two tables).
11732
11733 ---
11734
11735 Hash table lookup is handled the same in version 1 and 2:
11736
11737 We assume that N and M will not exceed 2^32 - 1.
11738 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11739
11740 Given a 64-bit compilation unit signature or a type signature S, an entry
11741 in the hash table is located as follows:
11742
11743 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11744 the low-order k bits all set to 1.
11745
11746 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11747
11748 3) If the hash table entry at index H matches the signature, use that
11749 entry. If the hash table entry at index H is unused (all zeroes),
11750 terminate the search: the signature is not present in the table.
11751
11752 4) Let H = (H + H') modulo M. Repeat at Step 3.
11753
11754 Because M > N and H' and M are relatively prime, the search is guaranteed
11755 to stop at an unused slot or find the match. */
11756
11757 /* Create a hash table to map DWO IDs to their CU/TU entry in
11758 .debug_{info,types}.dwo in DWP_FILE.
11759 Returns NULL if there isn't one.
11760 Note: This function processes DWP files only, not DWO files. */
11761
11762 static struct dwp_hash_table *
11763 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11764 struct dwp_file *dwp_file, int is_debug_types)
11765 {
11766 struct objfile *objfile = per_objfile->objfile;
11767 bfd *dbfd = dwp_file->dbfd.get ();
11768 const gdb_byte *index_ptr, *index_end;
11769 struct dwarf2_section_info *index;
11770 uint32_t version, nr_columns, nr_units, nr_slots;
11771 struct dwp_hash_table *htab;
11772
11773 if (is_debug_types)
11774 index = &dwp_file->sections.tu_index;
11775 else
11776 index = &dwp_file->sections.cu_index;
11777
11778 if (index->empty ())
11779 return NULL;
11780 index->read (objfile);
11781
11782 index_ptr = index->buffer;
11783 index_end = index_ptr + index->size;
11784
11785 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11786 For now it's safe to just read 4 bytes (particularly as it's difficult to
11787 tell if you're dealing with Version 5 before you've read the version). */
11788 version = read_4_bytes (dbfd, index_ptr);
11789 index_ptr += 4;
11790 if (version == 2 || version == 5)
11791 nr_columns = read_4_bytes (dbfd, index_ptr);
11792 else
11793 nr_columns = 0;
11794 index_ptr += 4;
11795 nr_units = read_4_bytes (dbfd, index_ptr);
11796 index_ptr += 4;
11797 nr_slots = read_4_bytes (dbfd, index_ptr);
11798 index_ptr += 4;
11799
11800 if (version != 1 && version != 2 && version != 5)
11801 {
11802 error (_("Dwarf Error: unsupported DWP file version (%s)"
11803 " [in module %s]"),
11804 pulongest (version), dwp_file->name);
11805 }
11806 if (nr_slots != (nr_slots & -nr_slots))
11807 {
11808 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11809 " is not power of 2 [in module %s]"),
11810 pulongest (nr_slots), dwp_file->name);
11811 }
11812
11813 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11814 htab->version = version;
11815 htab->nr_columns = nr_columns;
11816 htab->nr_units = nr_units;
11817 htab->nr_slots = nr_slots;
11818 htab->hash_table = index_ptr;
11819 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11820
11821 /* Exit early if the table is empty. */
11822 if (nr_slots == 0 || nr_units == 0
11823 || (version == 2 && nr_columns == 0)
11824 || (version == 5 && nr_columns == 0))
11825 {
11826 /* All must be zero. */
11827 if (nr_slots != 0 || nr_units != 0
11828 || (version == 2 && nr_columns != 0)
11829 || (version == 5 && nr_columns != 0))
11830 {
11831 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11832 " all zero [in modules %s]"),
11833 dwp_file->name);
11834 }
11835 return htab;
11836 }
11837
11838 if (version == 1)
11839 {
11840 htab->section_pool.v1.indices =
11841 htab->unit_table + sizeof (uint32_t) * nr_slots;
11842 /* It's harder to decide whether the section is too small in v1.
11843 V1 is deprecated anyway so we punt. */
11844 }
11845 else if (version == 2)
11846 {
11847 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11848 int *ids = htab->section_pool.v2.section_ids;
11849 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11850 /* Reverse map for error checking. */
11851 int ids_seen[DW_SECT_MAX + 1];
11852 int i;
11853
11854 if (nr_columns < 2)
11855 {
11856 error (_("Dwarf Error: bad DWP hash table, too few columns"
11857 " in section table [in module %s]"),
11858 dwp_file->name);
11859 }
11860 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11861 {
11862 error (_("Dwarf Error: bad DWP hash table, too many columns"
11863 " in section table [in module %s]"),
11864 dwp_file->name);
11865 }
11866 memset (ids, 255, sizeof_ids);
11867 memset (ids_seen, 255, sizeof (ids_seen));
11868 for (i = 0; i < nr_columns; ++i)
11869 {
11870 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11871
11872 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11873 {
11874 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11875 " in section table [in module %s]"),
11876 id, dwp_file->name);
11877 }
11878 if (ids_seen[id] != -1)
11879 {
11880 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11881 " id %d in section table [in module %s]"),
11882 id, dwp_file->name);
11883 }
11884 ids_seen[id] = i;
11885 ids[i] = id;
11886 }
11887 /* Must have exactly one info or types section. */
11888 if (((ids_seen[DW_SECT_INFO] != -1)
11889 + (ids_seen[DW_SECT_TYPES] != -1))
11890 != 1)
11891 {
11892 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11893 " DWO info/types section [in module %s]"),
11894 dwp_file->name);
11895 }
11896 /* Must have an abbrev section. */
11897 if (ids_seen[DW_SECT_ABBREV] == -1)
11898 {
11899 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11900 " section [in module %s]"),
11901 dwp_file->name);
11902 }
11903 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11904 htab->section_pool.v2.sizes =
11905 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11906 * nr_units * nr_columns);
11907 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11908 * nr_units * nr_columns))
11909 > index_end)
11910 {
11911 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11912 " [in module %s]"),
11913 dwp_file->name);
11914 }
11915 }
11916 else /* version == 5 */
11917 {
11918 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11919 int *ids = htab->section_pool.v5.section_ids;
11920 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11921 /* Reverse map for error checking. */
11922 int ids_seen[DW_SECT_MAX_V5 + 1];
11923
11924 if (nr_columns < 2)
11925 {
11926 error (_("Dwarf Error: bad DWP hash table, too few columns"
11927 " in section table [in module %s]"),
11928 dwp_file->name);
11929 }
11930 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11931 {
11932 error (_("Dwarf Error: bad DWP hash table, too many columns"
11933 " in section table [in module %s]"),
11934 dwp_file->name);
11935 }
11936 memset (ids, 255, sizeof_ids);
11937 memset (ids_seen, 255, sizeof (ids_seen));
11938 for (int i = 0; i < nr_columns; ++i)
11939 {
11940 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11941
11942 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11943 {
11944 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11945 " in section table [in module %s]"),
11946 id, dwp_file->name);
11947 }
11948 if (ids_seen[id] != -1)
11949 {
11950 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11951 " id %d in section table [in module %s]"),
11952 id, dwp_file->name);
11953 }
11954 ids_seen[id] = i;
11955 ids[i] = id;
11956 }
11957 /* Must have seen an info section. */
11958 if (ids_seen[DW_SECT_INFO_V5] == -1)
11959 {
11960 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11961 " DWO info/types section [in module %s]"),
11962 dwp_file->name);
11963 }
11964 /* Must have an abbrev section. */
11965 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11966 {
11967 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11968 " section [in module %s]"),
11969 dwp_file->name);
11970 }
11971 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11972 htab->section_pool.v5.sizes
11973 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11974 * nr_units * nr_columns);
11975 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11976 * nr_units * nr_columns))
11977 > index_end)
11978 {
11979 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11980 " [in module %s]"),
11981 dwp_file->name);
11982 }
11983 }
11984
11985 return htab;
11986 }
11987
11988 /* Update SECTIONS with the data from SECTP.
11989
11990 This function is like the other "locate" section routines, but in
11991 this context the sections to read comes from the DWP V1 hash table,
11992 not the full ELF section table.
11993
11994 The result is non-zero for success, or zero if an error was found. */
11995
11996 static int
11997 locate_v1_virtual_dwo_sections (asection *sectp,
11998 struct virtual_v1_dwo_sections *sections)
11999 {
12000 const struct dwop_section_names *names = &dwop_section_names;
12001
12002 if (names->abbrev_dwo.matches (sectp->name))
12003 {
12004 /* There can be only one. */
12005 if (sections->abbrev.s.section != NULL)
12006 return 0;
12007 sections->abbrev.s.section = sectp;
12008 sections->abbrev.size = bfd_section_size (sectp);
12009 }
12010 else if (names->info_dwo.matches (sectp->name)
12011 || names->types_dwo.matches (sectp->name))
12012 {
12013 /* There can be only one. */
12014 if (sections->info_or_types.s.section != NULL)
12015 return 0;
12016 sections->info_or_types.s.section = sectp;
12017 sections->info_or_types.size = bfd_section_size (sectp);
12018 }
12019 else if (names->line_dwo.matches (sectp->name))
12020 {
12021 /* There can be only one. */
12022 if (sections->line.s.section != NULL)
12023 return 0;
12024 sections->line.s.section = sectp;
12025 sections->line.size = bfd_section_size (sectp);
12026 }
12027 else if (names->loc_dwo.matches (sectp->name))
12028 {
12029 /* There can be only one. */
12030 if (sections->loc.s.section != NULL)
12031 return 0;
12032 sections->loc.s.section = sectp;
12033 sections->loc.size = bfd_section_size (sectp);
12034 }
12035 else if (names->macinfo_dwo.matches (sectp->name))
12036 {
12037 /* There can be only one. */
12038 if (sections->macinfo.s.section != NULL)
12039 return 0;
12040 sections->macinfo.s.section = sectp;
12041 sections->macinfo.size = bfd_section_size (sectp);
12042 }
12043 else if (names->macro_dwo.matches (sectp->name))
12044 {
12045 /* There can be only one. */
12046 if (sections->macro.s.section != NULL)
12047 return 0;
12048 sections->macro.s.section = sectp;
12049 sections->macro.size = bfd_section_size (sectp);
12050 }
12051 else if (names->str_offsets_dwo.matches (sectp->name))
12052 {
12053 /* There can be only one. */
12054 if (sections->str_offsets.s.section != NULL)
12055 return 0;
12056 sections->str_offsets.s.section = sectp;
12057 sections->str_offsets.size = bfd_section_size (sectp);
12058 }
12059 else
12060 {
12061 /* No other kind of section is valid. */
12062 return 0;
12063 }
12064
12065 return 1;
12066 }
12067
12068 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12069 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12070 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12071 This is for DWP version 1 files. */
12072
12073 static struct dwo_unit *
12074 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12075 struct dwp_file *dwp_file,
12076 uint32_t unit_index,
12077 const char *comp_dir,
12078 ULONGEST signature, int is_debug_types)
12079 {
12080 const struct dwp_hash_table *dwp_htab =
12081 is_debug_types ? dwp_file->tus : dwp_file->cus;
12082 bfd *dbfd = dwp_file->dbfd.get ();
12083 const char *kind = is_debug_types ? "TU" : "CU";
12084 struct dwo_file *dwo_file;
12085 struct dwo_unit *dwo_unit;
12086 struct virtual_v1_dwo_sections sections;
12087 void **dwo_file_slot;
12088 int i;
12089
12090 gdb_assert (dwp_file->version == 1);
12091
12092 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12093 kind, pulongest (unit_index), hex_string (signature),
12094 dwp_file->name);
12095
12096 /* Fetch the sections of this DWO unit.
12097 Put a limit on the number of sections we look for so that bad data
12098 doesn't cause us to loop forever. */
12099
12100 #define MAX_NR_V1_DWO_SECTIONS \
12101 (1 /* .debug_info or .debug_types */ \
12102 + 1 /* .debug_abbrev */ \
12103 + 1 /* .debug_line */ \
12104 + 1 /* .debug_loc */ \
12105 + 1 /* .debug_str_offsets */ \
12106 + 1 /* .debug_macro or .debug_macinfo */ \
12107 + 1 /* trailing zero */)
12108
12109 memset (&sections, 0, sizeof (sections));
12110
12111 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12112 {
12113 asection *sectp;
12114 uint32_t section_nr =
12115 read_4_bytes (dbfd,
12116 dwp_htab->section_pool.v1.indices
12117 + (unit_index + i) * sizeof (uint32_t));
12118
12119 if (section_nr == 0)
12120 break;
12121 if (section_nr >= dwp_file->num_sections)
12122 {
12123 error (_("Dwarf Error: bad DWP hash table, section number too large"
12124 " [in module %s]"),
12125 dwp_file->name);
12126 }
12127
12128 sectp = dwp_file->elf_sections[section_nr];
12129 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12130 {
12131 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12132 " [in module %s]"),
12133 dwp_file->name);
12134 }
12135 }
12136
12137 if (i < 2
12138 || sections.info_or_types.empty ()
12139 || sections.abbrev.empty ())
12140 {
12141 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12142 " [in module %s]"),
12143 dwp_file->name);
12144 }
12145 if (i == MAX_NR_V1_DWO_SECTIONS)
12146 {
12147 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12148 " [in module %s]"),
12149 dwp_file->name);
12150 }
12151
12152 /* It's easier for the rest of the code if we fake a struct dwo_file and
12153 have dwo_unit "live" in that. At least for now.
12154
12155 The DWP file can be made up of a random collection of CUs and TUs.
12156 However, for each CU + set of TUs that came from the same original DWO
12157 file, we can combine them back into a virtual DWO file to save space
12158 (fewer struct dwo_file objects to allocate). Remember that for really
12159 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12160
12161 std::string virtual_dwo_name =
12162 string_printf ("virtual-dwo/%d-%d-%d-%d",
12163 sections.abbrev.get_id (),
12164 sections.line.get_id (),
12165 sections.loc.get_id (),
12166 sections.str_offsets.get_id ());
12167 /* Can we use an existing virtual DWO file? */
12168 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12169 comp_dir);
12170 /* Create one if necessary. */
12171 if (*dwo_file_slot == NULL)
12172 {
12173 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12174 virtual_dwo_name.c_str ());
12175
12176 dwo_file = new struct dwo_file;
12177 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12178 dwo_file->comp_dir = comp_dir;
12179 dwo_file->sections.abbrev = sections.abbrev;
12180 dwo_file->sections.line = sections.line;
12181 dwo_file->sections.loc = sections.loc;
12182 dwo_file->sections.macinfo = sections.macinfo;
12183 dwo_file->sections.macro = sections.macro;
12184 dwo_file->sections.str_offsets = sections.str_offsets;
12185 /* The "str" section is global to the entire DWP file. */
12186 dwo_file->sections.str = dwp_file->sections.str;
12187 /* The info or types section is assigned below to dwo_unit,
12188 there's no need to record it in dwo_file.
12189 Also, we can't simply record type sections in dwo_file because
12190 we record a pointer into the vector in dwo_unit. As we collect more
12191 types we'll grow the vector and eventually have to reallocate space
12192 for it, invalidating all copies of pointers into the previous
12193 contents. */
12194 *dwo_file_slot = dwo_file;
12195 }
12196 else
12197 {
12198 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12199 virtual_dwo_name.c_str ());
12200
12201 dwo_file = (struct dwo_file *) *dwo_file_slot;
12202 }
12203
12204 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12205 dwo_unit->dwo_file = dwo_file;
12206 dwo_unit->signature = signature;
12207 dwo_unit->section =
12208 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12209 *dwo_unit->section = sections.info_or_types;
12210 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12211
12212 return dwo_unit;
12213 }
12214
12215 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12216 simplify them. Given a pointer to the containing section SECTION, and
12217 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12218 virtual section of just that piece. */
12219
12220 static struct dwarf2_section_info
12221 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12222 struct dwarf2_section_info *section,
12223 bfd_size_type offset, bfd_size_type size)
12224 {
12225 struct dwarf2_section_info result;
12226 asection *sectp;
12227
12228 gdb_assert (section != NULL);
12229 gdb_assert (!section->is_virtual);
12230
12231 memset (&result, 0, sizeof (result));
12232 result.s.containing_section = section;
12233 result.is_virtual = true;
12234
12235 if (size == 0)
12236 return result;
12237
12238 sectp = section->get_bfd_section ();
12239
12240 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12241 bounds of the real section. This is a pretty-rare event, so just
12242 flag an error (easier) instead of a warning and trying to cope. */
12243 if (sectp == NULL
12244 || offset + size > bfd_section_size (sectp))
12245 {
12246 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12247 " in section %s [in module %s]"),
12248 sectp ? bfd_section_name (sectp) : "<unknown>",
12249 objfile_name (per_objfile->objfile));
12250 }
12251
12252 result.virtual_offset = offset;
12253 result.size = size;
12254 return result;
12255 }
12256
12257 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12258 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12259 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12260 This is for DWP version 2 files. */
12261
12262 static struct dwo_unit *
12263 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12264 struct dwp_file *dwp_file,
12265 uint32_t unit_index,
12266 const char *comp_dir,
12267 ULONGEST signature, int is_debug_types)
12268 {
12269 const struct dwp_hash_table *dwp_htab =
12270 is_debug_types ? dwp_file->tus : dwp_file->cus;
12271 bfd *dbfd = dwp_file->dbfd.get ();
12272 const char *kind = is_debug_types ? "TU" : "CU";
12273 struct dwo_file *dwo_file;
12274 struct dwo_unit *dwo_unit;
12275 struct virtual_v2_or_v5_dwo_sections sections;
12276 void **dwo_file_slot;
12277 int i;
12278
12279 gdb_assert (dwp_file->version == 2);
12280
12281 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12282 kind, pulongest (unit_index), hex_string (signature),
12283 dwp_file->name);
12284
12285 /* Fetch the section offsets of this DWO unit. */
12286
12287 memset (&sections, 0, sizeof (sections));
12288
12289 for (i = 0; i < dwp_htab->nr_columns; ++i)
12290 {
12291 uint32_t offset = read_4_bytes (dbfd,
12292 dwp_htab->section_pool.v2.offsets
12293 + (((unit_index - 1) * dwp_htab->nr_columns
12294 + i)
12295 * sizeof (uint32_t)));
12296 uint32_t size = read_4_bytes (dbfd,
12297 dwp_htab->section_pool.v2.sizes
12298 + (((unit_index - 1) * dwp_htab->nr_columns
12299 + i)
12300 * sizeof (uint32_t)));
12301
12302 switch (dwp_htab->section_pool.v2.section_ids[i])
12303 {
12304 case DW_SECT_INFO:
12305 case DW_SECT_TYPES:
12306 sections.info_or_types_offset = offset;
12307 sections.info_or_types_size = size;
12308 break;
12309 case DW_SECT_ABBREV:
12310 sections.abbrev_offset = offset;
12311 sections.abbrev_size = size;
12312 break;
12313 case DW_SECT_LINE:
12314 sections.line_offset = offset;
12315 sections.line_size = size;
12316 break;
12317 case DW_SECT_LOC:
12318 sections.loc_offset = offset;
12319 sections.loc_size = size;
12320 break;
12321 case DW_SECT_STR_OFFSETS:
12322 sections.str_offsets_offset = offset;
12323 sections.str_offsets_size = size;
12324 break;
12325 case DW_SECT_MACINFO:
12326 sections.macinfo_offset = offset;
12327 sections.macinfo_size = size;
12328 break;
12329 case DW_SECT_MACRO:
12330 sections.macro_offset = offset;
12331 sections.macro_size = size;
12332 break;
12333 }
12334 }
12335
12336 /* It's easier for the rest of the code if we fake a struct dwo_file and
12337 have dwo_unit "live" in that. At least for now.
12338
12339 The DWP file can be made up of a random collection of CUs and TUs.
12340 However, for each CU + set of TUs that came from the same original DWO
12341 file, we can combine them back into a virtual DWO file to save space
12342 (fewer struct dwo_file objects to allocate). Remember that for really
12343 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12344
12345 std::string virtual_dwo_name =
12346 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12347 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12348 (long) (sections.line_size ? sections.line_offset : 0),
12349 (long) (sections.loc_size ? sections.loc_offset : 0),
12350 (long) (sections.str_offsets_size
12351 ? sections.str_offsets_offset : 0));
12352 /* Can we use an existing virtual DWO file? */
12353 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12354 comp_dir);
12355 /* Create one if necessary. */
12356 if (*dwo_file_slot == NULL)
12357 {
12358 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12359 virtual_dwo_name.c_str ());
12360
12361 dwo_file = new struct dwo_file;
12362 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12363 dwo_file->comp_dir = comp_dir;
12364 dwo_file->sections.abbrev =
12365 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12366 sections.abbrev_offset,
12367 sections.abbrev_size);
12368 dwo_file->sections.line =
12369 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12370 sections.line_offset,
12371 sections.line_size);
12372 dwo_file->sections.loc =
12373 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12374 sections.loc_offset, sections.loc_size);
12375 dwo_file->sections.macinfo =
12376 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12377 sections.macinfo_offset,
12378 sections.macinfo_size);
12379 dwo_file->sections.macro =
12380 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12381 sections.macro_offset,
12382 sections.macro_size);
12383 dwo_file->sections.str_offsets =
12384 create_dwp_v2_or_v5_section (per_objfile,
12385 &dwp_file->sections.str_offsets,
12386 sections.str_offsets_offset,
12387 sections.str_offsets_size);
12388 /* The "str" section is global to the entire DWP file. */
12389 dwo_file->sections.str = dwp_file->sections.str;
12390 /* The info or types section is assigned below to dwo_unit,
12391 there's no need to record it in dwo_file.
12392 Also, we can't simply record type sections in dwo_file because
12393 we record a pointer into the vector in dwo_unit. As we collect more
12394 types we'll grow the vector and eventually have to reallocate space
12395 for it, invalidating all copies of pointers into the previous
12396 contents. */
12397 *dwo_file_slot = dwo_file;
12398 }
12399 else
12400 {
12401 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12402 virtual_dwo_name.c_str ());
12403
12404 dwo_file = (struct dwo_file *) *dwo_file_slot;
12405 }
12406
12407 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12408 dwo_unit->dwo_file = dwo_file;
12409 dwo_unit->signature = signature;
12410 dwo_unit->section =
12411 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12412 *dwo_unit->section = create_dwp_v2_or_v5_section
12413 (per_objfile,
12414 is_debug_types
12415 ? &dwp_file->sections.types
12416 : &dwp_file->sections.info,
12417 sections.info_or_types_offset,
12418 sections.info_or_types_size);
12419 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12420
12421 return dwo_unit;
12422 }
12423
12424 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12425 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12426 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12427 This is for DWP version 5 files. */
12428
12429 static struct dwo_unit *
12430 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12431 struct dwp_file *dwp_file,
12432 uint32_t unit_index,
12433 const char *comp_dir,
12434 ULONGEST signature, int is_debug_types)
12435 {
12436 const struct dwp_hash_table *dwp_htab
12437 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12438 bfd *dbfd = dwp_file->dbfd.get ();
12439 const char *kind = is_debug_types ? "TU" : "CU";
12440 struct dwo_file *dwo_file;
12441 struct dwo_unit *dwo_unit;
12442 struct virtual_v2_or_v5_dwo_sections sections {};
12443 void **dwo_file_slot;
12444
12445 gdb_assert (dwp_file->version == 5);
12446
12447 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12448 kind, pulongest (unit_index), hex_string (signature),
12449 dwp_file->name);
12450
12451 /* Fetch the section offsets of this DWO unit. */
12452
12453 /* memset (&sections, 0, sizeof (sections)); */
12454
12455 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12456 {
12457 uint32_t offset = read_4_bytes (dbfd,
12458 dwp_htab->section_pool.v5.offsets
12459 + (((unit_index - 1)
12460 * dwp_htab->nr_columns
12461 + i)
12462 * sizeof (uint32_t)));
12463 uint32_t size = read_4_bytes (dbfd,
12464 dwp_htab->section_pool.v5.sizes
12465 + (((unit_index - 1) * dwp_htab->nr_columns
12466 + i)
12467 * sizeof (uint32_t)));
12468
12469 switch (dwp_htab->section_pool.v5.section_ids[i])
12470 {
12471 case DW_SECT_ABBREV_V5:
12472 sections.abbrev_offset = offset;
12473 sections.abbrev_size = size;
12474 break;
12475 case DW_SECT_INFO_V5:
12476 sections.info_or_types_offset = offset;
12477 sections.info_or_types_size = size;
12478 break;
12479 case DW_SECT_LINE_V5:
12480 sections.line_offset = offset;
12481 sections.line_size = size;
12482 break;
12483 case DW_SECT_LOCLISTS_V5:
12484 sections.loclists_offset = offset;
12485 sections.loclists_size = size;
12486 break;
12487 case DW_SECT_MACRO_V5:
12488 sections.macro_offset = offset;
12489 sections.macro_size = size;
12490 break;
12491 case DW_SECT_RNGLISTS_V5:
12492 sections.rnglists_offset = offset;
12493 sections.rnglists_size = size;
12494 break;
12495 case DW_SECT_STR_OFFSETS_V5:
12496 sections.str_offsets_offset = offset;
12497 sections.str_offsets_size = size;
12498 break;
12499 case DW_SECT_RESERVED_V5:
12500 default:
12501 break;
12502 }
12503 }
12504
12505 /* It's easier for the rest of the code if we fake a struct dwo_file and
12506 have dwo_unit "live" in that. At least for now.
12507
12508 The DWP file can be made up of a random collection of CUs and TUs.
12509 However, for each CU + set of TUs that came from the same original DWO
12510 file, we can combine them back into a virtual DWO file to save space
12511 (fewer struct dwo_file objects to allocate). Remember that for really
12512 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12513
12514 std::string virtual_dwo_name =
12515 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12516 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12517 (long) (sections.line_size ? sections.line_offset : 0),
12518 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12519 (long) (sections.str_offsets_size
12520 ? sections.str_offsets_offset : 0),
12521 (long) (sections.macro_size ? sections.macro_offset : 0),
12522 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12523 /* Can we use an existing virtual DWO file? */
12524 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12525 virtual_dwo_name.c_str (),
12526 comp_dir);
12527 /* Create one if necessary. */
12528 if (*dwo_file_slot == NULL)
12529 {
12530 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12531 virtual_dwo_name.c_str ());
12532
12533 dwo_file = new struct dwo_file;
12534 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12535 dwo_file->comp_dir = comp_dir;
12536 dwo_file->sections.abbrev =
12537 create_dwp_v2_or_v5_section (per_objfile,
12538 &dwp_file->sections.abbrev,
12539 sections.abbrev_offset,
12540 sections.abbrev_size);
12541 dwo_file->sections.line =
12542 create_dwp_v2_or_v5_section (per_objfile,
12543 &dwp_file->sections.line,
12544 sections.line_offset, sections.line_size);
12545 dwo_file->sections.macro =
12546 create_dwp_v2_or_v5_section (per_objfile,
12547 &dwp_file->sections.macro,
12548 sections.macro_offset,
12549 sections.macro_size);
12550 dwo_file->sections.loclists =
12551 create_dwp_v2_or_v5_section (per_objfile,
12552 &dwp_file->sections.loclists,
12553 sections.loclists_offset,
12554 sections.loclists_size);
12555 dwo_file->sections.rnglists =
12556 create_dwp_v2_or_v5_section (per_objfile,
12557 &dwp_file->sections.rnglists,
12558 sections.rnglists_offset,
12559 sections.rnglists_size);
12560 dwo_file->sections.str_offsets =
12561 create_dwp_v2_or_v5_section (per_objfile,
12562 &dwp_file->sections.str_offsets,
12563 sections.str_offsets_offset,
12564 sections.str_offsets_size);
12565 /* The "str" section is global to the entire DWP file. */
12566 dwo_file->sections.str = dwp_file->sections.str;
12567 /* The info or types section is assigned below to dwo_unit,
12568 there's no need to record it in dwo_file.
12569 Also, we can't simply record type sections in dwo_file because
12570 we record a pointer into the vector in dwo_unit. As we collect more
12571 types we'll grow the vector and eventually have to reallocate space
12572 for it, invalidating all copies of pointers into the previous
12573 contents. */
12574 *dwo_file_slot = dwo_file;
12575 }
12576 else
12577 {
12578 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12579 virtual_dwo_name.c_str ());
12580
12581 dwo_file = (struct dwo_file *) *dwo_file_slot;
12582 }
12583
12584 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12585 dwo_unit->dwo_file = dwo_file;
12586 dwo_unit->signature = signature;
12587 dwo_unit->section
12588 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12589 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12590 &dwp_file->sections.info,
12591 sections.info_or_types_offset,
12592 sections.info_or_types_size);
12593 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12594
12595 return dwo_unit;
12596 }
12597
12598 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12599 Returns NULL if the signature isn't found. */
12600
12601 static struct dwo_unit *
12602 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12603 struct dwp_file *dwp_file, const char *comp_dir,
12604 ULONGEST signature, int is_debug_types)
12605 {
12606 const struct dwp_hash_table *dwp_htab =
12607 is_debug_types ? dwp_file->tus : dwp_file->cus;
12608 bfd *dbfd = dwp_file->dbfd.get ();
12609 uint32_t mask = dwp_htab->nr_slots - 1;
12610 uint32_t hash = signature & mask;
12611 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12612 unsigned int i;
12613 void **slot;
12614 struct dwo_unit find_dwo_cu;
12615
12616 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12617 find_dwo_cu.signature = signature;
12618 slot = htab_find_slot (is_debug_types
12619 ? dwp_file->loaded_tus.get ()
12620 : dwp_file->loaded_cus.get (),
12621 &find_dwo_cu, INSERT);
12622
12623 if (*slot != NULL)
12624 return (struct dwo_unit *) *slot;
12625
12626 /* Use a for loop so that we don't loop forever on bad debug info. */
12627 for (i = 0; i < dwp_htab->nr_slots; ++i)
12628 {
12629 ULONGEST signature_in_table;
12630
12631 signature_in_table =
12632 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12633 if (signature_in_table == signature)
12634 {
12635 uint32_t unit_index =
12636 read_4_bytes (dbfd,
12637 dwp_htab->unit_table + hash * sizeof (uint32_t));
12638
12639 if (dwp_file->version == 1)
12640 {
12641 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12642 unit_index, comp_dir,
12643 signature, is_debug_types);
12644 }
12645 else if (dwp_file->version == 2)
12646 {
12647 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12648 unit_index, comp_dir,
12649 signature, is_debug_types);
12650 }
12651 else /* version == 5 */
12652 {
12653 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12654 unit_index, comp_dir,
12655 signature, is_debug_types);
12656 }
12657 return (struct dwo_unit *) *slot;
12658 }
12659 if (signature_in_table == 0)
12660 return NULL;
12661 hash = (hash + hash2) & mask;
12662 }
12663
12664 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12665 " [in module %s]"),
12666 dwp_file->name);
12667 }
12668
12669 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12670 Open the file specified by FILE_NAME and hand it off to BFD for
12671 preliminary analysis. Return a newly initialized bfd *, which
12672 includes a canonicalized copy of FILE_NAME.
12673 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12674 SEARCH_CWD is true if the current directory is to be searched.
12675 It will be searched before debug-file-directory.
12676 If successful, the file is added to the bfd include table of the
12677 objfile's bfd (see gdb_bfd_record_inclusion).
12678 If unable to find/open the file, return NULL.
12679 NOTE: This function is derived from symfile_bfd_open. */
12680
12681 static gdb_bfd_ref_ptr
12682 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12683 const char *file_name, int is_dwp, int search_cwd)
12684 {
12685 int desc;
12686 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12687 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12688 to debug_file_directory. */
12689 const char *search_path;
12690 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12691
12692 gdb::unique_xmalloc_ptr<char> search_path_holder;
12693 if (search_cwd)
12694 {
12695 if (*debug_file_directory != '\0')
12696 {
12697 search_path_holder.reset (concat (".", dirname_separator_string,
12698 debug_file_directory,
12699 (char *) NULL));
12700 search_path = search_path_holder.get ();
12701 }
12702 else
12703 search_path = ".";
12704 }
12705 else
12706 search_path = debug_file_directory;
12707
12708 openp_flags flags = OPF_RETURN_REALPATH;
12709 if (is_dwp)
12710 flags |= OPF_SEARCH_IN_PATH;
12711
12712 gdb::unique_xmalloc_ptr<char> absolute_name;
12713 desc = openp (search_path, flags, file_name,
12714 O_RDONLY | O_BINARY, &absolute_name);
12715 if (desc < 0)
12716 return NULL;
12717
12718 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12719 gnutarget, desc));
12720 if (sym_bfd == NULL)
12721 return NULL;
12722 bfd_set_cacheable (sym_bfd.get (), 1);
12723
12724 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12725 return NULL;
12726
12727 /* Success. Record the bfd as having been included by the objfile's bfd.
12728 This is important because things like demangled_names_hash lives in the
12729 objfile's per_bfd space and may have references to things like symbol
12730 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12731 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12732
12733 return sym_bfd;
12734 }
12735
12736 /* Try to open DWO file FILE_NAME.
12737 COMP_DIR is the DW_AT_comp_dir attribute.
12738 The result is the bfd handle of the file.
12739 If there is a problem finding or opening the file, return NULL.
12740 Upon success, the canonicalized path of the file is stored in the bfd,
12741 same as symfile_bfd_open. */
12742
12743 static gdb_bfd_ref_ptr
12744 open_dwo_file (dwarf2_per_objfile *per_objfile,
12745 const char *file_name, const char *comp_dir)
12746 {
12747 if (IS_ABSOLUTE_PATH (file_name))
12748 return try_open_dwop_file (per_objfile, file_name,
12749 0 /*is_dwp*/, 0 /*search_cwd*/);
12750
12751 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12752
12753 if (comp_dir != NULL)
12754 {
12755 gdb::unique_xmalloc_ptr<char> path_to_try
12756 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12757
12758 /* NOTE: If comp_dir is a relative path, this will also try the
12759 search path, which seems useful. */
12760 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12761 0 /*is_dwp*/,
12762 1 /*search_cwd*/));
12763 if (abfd != NULL)
12764 return abfd;
12765 }
12766
12767 /* That didn't work, try debug-file-directory, which, despite its name,
12768 is a list of paths. */
12769
12770 if (*debug_file_directory == '\0')
12771 return NULL;
12772
12773 return try_open_dwop_file (per_objfile, file_name,
12774 0 /*is_dwp*/, 1 /*search_cwd*/);
12775 }
12776
12777 /* This function is mapped across the sections and remembers the offset and
12778 size of each of the DWO debugging sections we are interested in. */
12779
12780 static void
12781 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12782 dwo_sections *dwo_sections)
12783 {
12784 const struct dwop_section_names *names = &dwop_section_names;
12785
12786 if (names->abbrev_dwo.matches (sectp->name))
12787 {
12788 dwo_sections->abbrev.s.section = sectp;
12789 dwo_sections->abbrev.size = bfd_section_size (sectp);
12790 }
12791 else if (names->info_dwo.matches (sectp->name))
12792 {
12793 dwo_sections->info.s.section = sectp;
12794 dwo_sections->info.size = bfd_section_size (sectp);
12795 }
12796 else if (names->line_dwo.matches (sectp->name))
12797 {
12798 dwo_sections->line.s.section = sectp;
12799 dwo_sections->line.size = bfd_section_size (sectp);
12800 }
12801 else if (names->loc_dwo.matches (sectp->name))
12802 {
12803 dwo_sections->loc.s.section = sectp;
12804 dwo_sections->loc.size = bfd_section_size (sectp);
12805 }
12806 else if (names->loclists_dwo.matches (sectp->name))
12807 {
12808 dwo_sections->loclists.s.section = sectp;
12809 dwo_sections->loclists.size = bfd_section_size (sectp);
12810 }
12811 else if (names->macinfo_dwo.matches (sectp->name))
12812 {
12813 dwo_sections->macinfo.s.section = sectp;
12814 dwo_sections->macinfo.size = bfd_section_size (sectp);
12815 }
12816 else if (names->macro_dwo.matches (sectp->name))
12817 {
12818 dwo_sections->macro.s.section = sectp;
12819 dwo_sections->macro.size = bfd_section_size (sectp);
12820 }
12821 else if (names->rnglists_dwo.matches (sectp->name))
12822 {
12823 dwo_sections->rnglists.s.section = sectp;
12824 dwo_sections->rnglists.size = bfd_section_size (sectp);
12825 }
12826 else if (names->str_dwo.matches (sectp->name))
12827 {
12828 dwo_sections->str.s.section = sectp;
12829 dwo_sections->str.size = bfd_section_size (sectp);
12830 }
12831 else if (names->str_offsets_dwo.matches (sectp->name))
12832 {
12833 dwo_sections->str_offsets.s.section = sectp;
12834 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12835 }
12836 else if (names->types_dwo.matches (sectp->name))
12837 {
12838 struct dwarf2_section_info type_section;
12839
12840 memset (&type_section, 0, sizeof (type_section));
12841 type_section.s.section = sectp;
12842 type_section.size = bfd_section_size (sectp);
12843 dwo_sections->types.push_back (type_section);
12844 }
12845 }
12846
12847 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12848 by PER_CU. This is for the non-DWP case.
12849 The result is NULL if DWO_NAME can't be found. */
12850
12851 static struct dwo_file *
12852 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12853 const char *comp_dir)
12854 {
12855 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12856
12857 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12858 if (dbfd == NULL)
12859 {
12860 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12861
12862 return NULL;
12863 }
12864
12865 dwo_file_up dwo_file (new struct dwo_file);
12866 dwo_file->dwo_name = dwo_name;
12867 dwo_file->comp_dir = comp_dir;
12868 dwo_file->dbfd = std::move (dbfd);
12869
12870 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12871 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12872 &dwo_file->sections);
12873
12874 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12875 dwo_file->cus);
12876
12877 if (cu->per_cu->dwarf_version < 5)
12878 {
12879 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12880 dwo_file->sections.types, dwo_file->tus);
12881 }
12882 else
12883 {
12884 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12885 &dwo_file->sections.info, dwo_file->tus,
12886 rcuh_kind::COMPILE);
12887 }
12888
12889 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12890
12891 return dwo_file.release ();
12892 }
12893
12894 /* This function is mapped across the sections and remembers the offset and
12895 size of each of the DWP debugging sections common to version 1 and 2 that
12896 we are interested in. */
12897
12898 static void
12899 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12900 dwp_file *dwp_file)
12901 {
12902 const struct dwop_section_names *names = &dwop_section_names;
12903 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12904
12905 /* Record the ELF section number for later lookup: this is what the
12906 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12907 gdb_assert (elf_section_nr < dwp_file->num_sections);
12908 dwp_file->elf_sections[elf_section_nr] = sectp;
12909
12910 /* Look for specific sections that we need. */
12911 if (names->str_dwo.matches (sectp->name))
12912 {
12913 dwp_file->sections.str.s.section = sectp;
12914 dwp_file->sections.str.size = bfd_section_size (sectp);
12915 }
12916 else if (names->cu_index.matches (sectp->name))
12917 {
12918 dwp_file->sections.cu_index.s.section = sectp;
12919 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12920 }
12921 else if (names->tu_index.matches (sectp->name))
12922 {
12923 dwp_file->sections.tu_index.s.section = sectp;
12924 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12925 }
12926 }
12927
12928 /* This function is mapped across the sections and remembers the offset and
12929 size of each of the DWP version 2 debugging sections that we are interested
12930 in. This is split into a separate function because we don't know if we
12931 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12932
12933 static void
12934 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12935 {
12936 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12937 const struct dwop_section_names *names = &dwop_section_names;
12938 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12939
12940 /* Record the ELF section number for later lookup: this is what the
12941 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12942 gdb_assert (elf_section_nr < dwp_file->num_sections);
12943 dwp_file->elf_sections[elf_section_nr] = sectp;
12944
12945 /* Look for specific sections that we need. */
12946 if (names->abbrev_dwo.matches (sectp->name))
12947 {
12948 dwp_file->sections.abbrev.s.section = sectp;
12949 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12950 }
12951 else if (names->info_dwo.matches (sectp->name))
12952 {
12953 dwp_file->sections.info.s.section = sectp;
12954 dwp_file->sections.info.size = bfd_section_size (sectp);
12955 }
12956 else if (names->line_dwo.matches (sectp->name))
12957 {
12958 dwp_file->sections.line.s.section = sectp;
12959 dwp_file->sections.line.size = bfd_section_size (sectp);
12960 }
12961 else if (names->loc_dwo.matches (sectp->name))
12962 {
12963 dwp_file->sections.loc.s.section = sectp;
12964 dwp_file->sections.loc.size = bfd_section_size (sectp);
12965 }
12966 else if (names->macinfo_dwo.matches (sectp->name))
12967 {
12968 dwp_file->sections.macinfo.s.section = sectp;
12969 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12970 }
12971 else if (names->macro_dwo.matches (sectp->name))
12972 {
12973 dwp_file->sections.macro.s.section = sectp;
12974 dwp_file->sections.macro.size = bfd_section_size (sectp);
12975 }
12976 else if (names->str_offsets_dwo.matches (sectp->name))
12977 {
12978 dwp_file->sections.str_offsets.s.section = sectp;
12979 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12980 }
12981 else if (names->types_dwo.matches (sectp->name))
12982 {
12983 dwp_file->sections.types.s.section = sectp;
12984 dwp_file->sections.types.size = bfd_section_size (sectp);
12985 }
12986 }
12987
12988 /* This function is mapped across the sections and remembers the offset and
12989 size of each of the DWP version 5 debugging sections that we are interested
12990 in. This is split into a separate function because we don't know if we
12991 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12992
12993 static void
12994 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12995 {
12996 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12997 const struct dwop_section_names *names = &dwop_section_names;
12998 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12999
13000 /* Record the ELF section number for later lookup: this is what the
13001 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13002 gdb_assert (elf_section_nr < dwp_file->num_sections);
13003 dwp_file->elf_sections[elf_section_nr] = sectp;
13004
13005 /* Look for specific sections that we need. */
13006 if (names->abbrev_dwo.matches (sectp->name))
13007 {
13008 dwp_file->sections.abbrev.s.section = sectp;
13009 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13010 }
13011 else if (names->info_dwo.matches (sectp->name))
13012 {
13013 dwp_file->sections.info.s.section = sectp;
13014 dwp_file->sections.info.size = bfd_section_size (sectp);
13015 }
13016 else if (names->line_dwo.matches (sectp->name))
13017 {
13018 dwp_file->sections.line.s.section = sectp;
13019 dwp_file->sections.line.size = bfd_section_size (sectp);
13020 }
13021 else if (names->loclists_dwo.matches (sectp->name))
13022 {
13023 dwp_file->sections.loclists.s.section = sectp;
13024 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13025 }
13026 else if (names->macro_dwo.matches (sectp->name))
13027 {
13028 dwp_file->sections.macro.s.section = sectp;
13029 dwp_file->sections.macro.size = bfd_section_size (sectp);
13030 }
13031 else if (names->rnglists_dwo.matches (sectp->name))
13032 {
13033 dwp_file->sections.rnglists.s.section = sectp;
13034 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13035 }
13036 else if (names->str_offsets_dwo.matches (sectp->name))
13037 {
13038 dwp_file->sections.str_offsets.s.section = sectp;
13039 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13040 }
13041 }
13042
13043 /* Hash function for dwp_file loaded CUs/TUs. */
13044
13045 static hashval_t
13046 hash_dwp_loaded_cutus (const void *item)
13047 {
13048 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13049
13050 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13051 return dwo_unit->signature;
13052 }
13053
13054 /* Equality function for dwp_file loaded CUs/TUs. */
13055
13056 static int
13057 eq_dwp_loaded_cutus (const void *a, const void *b)
13058 {
13059 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13060 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13061
13062 return dua->signature == dub->signature;
13063 }
13064
13065 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13066
13067 static htab_up
13068 allocate_dwp_loaded_cutus_table ()
13069 {
13070 return htab_up (htab_create_alloc (3,
13071 hash_dwp_loaded_cutus,
13072 eq_dwp_loaded_cutus,
13073 NULL, xcalloc, xfree));
13074 }
13075
13076 /* Try to open DWP file FILE_NAME.
13077 The result is the bfd handle of the file.
13078 If there is a problem finding or opening the file, return NULL.
13079 Upon success, the canonicalized path of the file is stored in the bfd,
13080 same as symfile_bfd_open. */
13081
13082 static gdb_bfd_ref_ptr
13083 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13084 {
13085 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13086 1 /*is_dwp*/,
13087 1 /*search_cwd*/));
13088 if (abfd != NULL)
13089 return abfd;
13090
13091 /* Work around upstream bug 15652.
13092 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13093 [Whether that's a "bug" is debatable, but it is getting in our way.]
13094 We have no real idea where the dwp file is, because gdb's realpath-ing
13095 of the executable's path may have discarded the needed info.
13096 [IWBN if the dwp file name was recorded in the executable, akin to
13097 .gnu_debuglink, but that doesn't exist yet.]
13098 Strip the directory from FILE_NAME and search again. */
13099 if (*debug_file_directory != '\0')
13100 {
13101 /* Don't implicitly search the current directory here.
13102 If the user wants to search "." to handle this case,
13103 it must be added to debug-file-directory. */
13104 return try_open_dwop_file (per_objfile, lbasename (file_name),
13105 1 /*is_dwp*/,
13106 0 /*search_cwd*/);
13107 }
13108
13109 return NULL;
13110 }
13111
13112 /* Initialize the use of the DWP file for the current objfile.
13113 By convention the name of the DWP file is ${objfile}.dwp.
13114 The result is NULL if it can't be found. */
13115
13116 static std::unique_ptr<struct dwp_file>
13117 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13118 {
13119 struct objfile *objfile = per_objfile->objfile;
13120
13121 /* Try to find first .dwp for the binary file before any symbolic links
13122 resolving. */
13123
13124 /* If the objfile is a debug file, find the name of the real binary
13125 file and get the name of dwp file from there. */
13126 std::string dwp_name;
13127 if (objfile->separate_debug_objfile_backlink != NULL)
13128 {
13129 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13130 const char *backlink_basename = lbasename (backlink->original_name);
13131
13132 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13133 }
13134 else
13135 dwp_name = objfile->original_name;
13136
13137 dwp_name += ".dwp";
13138
13139 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13140 if (dbfd == NULL
13141 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13142 {
13143 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13144 dwp_name = objfile_name (objfile);
13145 dwp_name += ".dwp";
13146 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13147 }
13148
13149 if (dbfd == NULL)
13150 {
13151 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13152
13153 return std::unique_ptr<dwp_file> ();
13154 }
13155
13156 const char *name = bfd_get_filename (dbfd.get ());
13157 std::unique_ptr<struct dwp_file> dwp_file
13158 (new struct dwp_file (name, std::move (dbfd)));
13159
13160 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13161 dwp_file->elf_sections =
13162 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13163 dwp_file->num_sections, asection *);
13164
13165 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13166 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13167 dwp_file.get ());
13168
13169 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13170
13171 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13172
13173 /* The DWP file version is stored in the hash table. Oh well. */
13174 if (dwp_file->cus && dwp_file->tus
13175 && dwp_file->cus->version != dwp_file->tus->version)
13176 {
13177 /* Technically speaking, we should try to limp along, but this is
13178 pretty bizarre. We use pulongest here because that's the established
13179 portability solution (e.g, we cannot use %u for uint32_t). */
13180 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13181 " TU version %s [in DWP file %s]"),
13182 pulongest (dwp_file->cus->version),
13183 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13184 }
13185
13186 if (dwp_file->cus)
13187 dwp_file->version = dwp_file->cus->version;
13188 else if (dwp_file->tus)
13189 dwp_file->version = dwp_file->tus->version;
13190 else
13191 dwp_file->version = 2;
13192
13193 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13194 {
13195 if (dwp_file->version == 2)
13196 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13197 dwp_file.get ());
13198 else
13199 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13200 dwp_file.get ());
13201 }
13202
13203 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13204 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13205
13206 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13207 dwarf_read_debug_printf (" %s CUs, %s TUs",
13208 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13209 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13210
13211 return dwp_file;
13212 }
13213
13214 /* Wrapper around open_and_init_dwp_file, only open it once. */
13215
13216 static struct dwp_file *
13217 get_dwp_file (dwarf2_per_objfile *per_objfile)
13218 {
13219 if (!per_objfile->per_bfd->dwp_checked)
13220 {
13221 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13222 per_objfile->per_bfd->dwp_checked = 1;
13223 }
13224 return per_objfile->per_bfd->dwp_file.get ();
13225 }
13226
13227 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13228 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13229 or in the DWP file for the objfile, referenced by THIS_UNIT.
13230 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13231 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13232
13233 This is called, for example, when wanting to read a variable with a
13234 complex location. Therefore we don't want to do file i/o for every call.
13235 Therefore we don't want to look for a DWO file on every call.
13236 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13237 then we check if we've already seen DWO_NAME, and only THEN do we check
13238 for a DWO file.
13239
13240 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13241 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13242
13243 static struct dwo_unit *
13244 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13245 ULONGEST signature, int is_debug_types)
13246 {
13247 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13248 struct objfile *objfile = per_objfile->objfile;
13249 const char *kind = is_debug_types ? "TU" : "CU";
13250 void **dwo_file_slot;
13251 struct dwo_file *dwo_file;
13252 struct dwp_file *dwp_file;
13253
13254 /* First see if there's a DWP file.
13255 If we have a DWP file but didn't find the DWO inside it, don't
13256 look for the original DWO file. It makes gdb behave differently
13257 depending on whether one is debugging in the build tree. */
13258
13259 dwp_file = get_dwp_file (per_objfile);
13260 if (dwp_file != NULL)
13261 {
13262 const struct dwp_hash_table *dwp_htab =
13263 is_debug_types ? dwp_file->tus : dwp_file->cus;
13264
13265 if (dwp_htab != NULL)
13266 {
13267 struct dwo_unit *dwo_cutu =
13268 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13269 is_debug_types);
13270
13271 if (dwo_cutu != NULL)
13272 {
13273 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13274 kind, hex_string (signature),
13275 host_address_to_string (dwo_cutu));
13276
13277 return dwo_cutu;
13278 }
13279 }
13280 }
13281 else
13282 {
13283 /* No DWP file, look for the DWO file. */
13284
13285 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13286 if (*dwo_file_slot == NULL)
13287 {
13288 /* Read in the file and build a table of the CUs/TUs it contains. */
13289 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13290 }
13291 /* NOTE: This will be NULL if unable to open the file. */
13292 dwo_file = (struct dwo_file *) *dwo_file_slot;
13293
13294 if (dwo_file != NULL)
13295 {
13296 struct dwo_unit *dwo_cutu = NULL;
13297
13298 if (is_debug_types && dwo_file->tus)
13299 {
13300 struct dwo_unit find_dwo_cutu;
13301
13302 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13303 find_dwo_cutu.signature = signature;
13304 dwo_cutu
13305 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13306 &find_dwo_cutu);
13307 }
13308 else if (!is_debug_types && dwo_file->cus)
13309 {
13310 struct dwo_unit find_dwo_cutu;
13311
13312 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13313 find_dwo_cutu.signature = signature;
13314 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13315 &find_dwo_cutu);
13316 }
13317
13318 if (dwo_cutu != NULL)
13319 {
13320 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13321 kind, dwo_name, hex_string (signature),
13322 host_address_to_string (dwo_cutu));
13323
13324 return dwo_cutu;
13325 }
13326 }
13327 }
13328
13329 /* We didn't find it. This could mean a dwo_id mismatch, or
13330 someone deleted the DWO/DWP file, or the search path isn't set up
13331 correctly to find the file. */
13332
13333 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13334 kind, dwo_name, hex_string (signature));
13335
13336 /* This is a warning and not a complaint because it can be caused by
13337 pilot error (e.g., user accidentally deleting the DWO). */
13338 {
13339 /* Print the name of the DWP file if we looked there, helps the user
13340 better diagnose the problem. */
13341 std::string dwp_text;
13342
13343 if (dwp_file != NULL)
13344 dwp_text = string_printf (" [in DWP file %s]",
13345 lbasename (dwp_file->name));
13346
13347 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13348 " [in module %s]"),
13349 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13350 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13351 }
13352 return NULL;
13353 }
13354
13355 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13356 See lookup_dwo_cutu_unit for details. */
13357
13358 static struct dwo_unit *
13359 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13360 ULONGEST signature)
13361 {
13362 gdb_assert (!cu->per_cu->is_debug_types);
13363
13364 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13365 }
13366
13367 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13368 See lookup_dwo_cutu_unit for details. */
13369
13370 static struct dwo_unit *
13371 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13372 {
13373 gdb_assert (cu->per_cu->is_debug_types);
13374
13375 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13376
13377 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13378 }
13379
13380 /* Traversal function for queue_and_load_all_dwo_tus. */
13381
13382 static int
13383 queue_and_load_dwo_tu (void **slot, void *info)
13384 {
13385 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13386 dwarf2_cu *cu = (dwarf2_cu *) info;
13387 ULONGEST signature = dwo_unit->signature;
13388 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13389
13390 if (sig_type != NULL)
13391 {
13392 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13393
13394 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13395 a real dependency of PER_CU on SIG_TYPE. That is detected later
13396 while processing PER_CU. */
13397 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13398 load_full_type_unit (sig_cu, cu->per_objfile);
13399 cu->per_cu->imported_symtabs_push (sig_cu);
13400 }
13401
13402 return 1;
13403 }
13404
13405 /* Queue all TUs contained in the DWO of CU to be read in.
13406 The DWO may have the only definition of the type, though it may not be
13407 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13408 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13409
13410 static void
13411 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13412 {
13413 struct dwo_unit *dwo_unit;
13414 struct dwo_file *dwo_file;
13415
13416 gdb_assert (cu != nullptr);
13417 gdb_assert (!cu->per_cu->is_debug_types);
13418 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13419
13420 dwo_unit = cu->dwo_unit;
13421 gdb_assert (dwo_unit != NULL);
13422
13423 dwo_file = dwo_unit->dwo_file;
13424 if (dwo_file->tus != NULL)
13425 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13426 }
13427
13428 /* Read in various DIEs. */
13429
13430 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13431 Inherit only the children of the DW_AT_abstract_origin DIE not being
13432 already referenced by DW_AT_abstract_origin from the children of the
13433 current DIE. */
13434
13435 static void
13436 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13437 {
13438 struct die_info *child_die;
13439 sect_offset *offsetp;
13440 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13441 struct die_info *origin_die;
13442 /* Iterator of the ORIGIN_DIE children. */
13443 struct die_info *origin_child_die;
13444 struct attribute *attr;
13445 struct dwarf2_cu *origin_cu;
13446 struct pending **origin_previous_list_in_scope;
13447
13448 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13449 if (!attr)
13450 return;
13451
13452 /* Note that following die references may follow to a die in a
13453 different cu. */
13454
13455 origin_cu = cu;
13456 origin_die = follow_die_ref (die, attr, &origin_cu);
13457
13458 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13459 symbols in. */
13460 origin_previous_list_in_scope = origin_cu->list_in_scope;
13461 origin_cu->list_in_scope = cu->list_in_scope;
13462
13463 if (die->tag != origin_die->tag
13464 && !(die->tag == DW_TAG_inlined_subroutine
13465 && origin_die->tag == DW_TAG_subprogram))
13466 complaint (_("DIE %s and its abstract origin %s have different tags"),
13467 sect_offset_str (die->sect_off),
13468 sect_offset_str (origin_die->sect_off));
13469
13470 std::vector<sect_offset> offsets;
13471
13472 for (child_die = die->child;
13473 child_die && child_die->tag;
13474 child_die = child_die->sibling)
13475 {
13476 struct die_info *child_origin_die;
13477 struct dwarf2_cu *child_origin_cu;
13478
13479 /* We are trying to process concrete instance entries:
13480 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13481 it's not relevant to our analysis here. i.e. detecting DIEs that are
13482 present in the abstract instance but not referenced in the concrete
13483 one. */
13484 if (child_die->tag == DW_TAG_call_site
13485 || child_die->tag == DW_TAG_GNU_call_site)
13486 continue;
13487
13488 /* For each CHILD_DIE, find the corresponding child of
13489 ORIGIN_DIE. If there is more than one layer of
13490 DW_AT_abstract_origin, follow them all; there shouldn't be,
13491 but GCC versions at least through 4.4 generate this (GCC PR
13492 40573). */
13493 child_origin_die = child_die;
13494 child_origin_cu = cu;
13495 while (1)
13496 {
13497 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13498 child_origin_cu);
13499 if (attr == NULL)
13500 break;
13501 child_origin_die = follow_die_ref (child_origin_die, attr,
13502 &child_origin_cu);
13503 }
13504
13505 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13506 counterpart may exist. */
13507 if (child_origin_die != child_die)
13508 {
13509 if (child_die->tag != child_origin_die->tag
13510 && !(child_die->tag == DW_TAG_inlined_subroutine
13511 && child_origin_die->tag == DW_TAG_subprogram))
13512 complaint (_("Child DIE %s and its abstract origin %s have "
13513 "different tags"),
13514 sect_offset_str (child_die->sect_off),
13515 sect_offset_str (child_origin_die->sect_off));
13516 if (child_origin_die->parent != origin_die)
13517 complaint (_("Child DIE %s and its abstract origin %s have "
13518 "different parents"),
13519 sect_offset_str (child_die->sect_off),
13520 sect_offset_str (child_origin_die->sect_off));
13521 else
13522 offsets.push_back (child_origin_die->sect_off);
13523 }
13524 }
13525 std::sort (offsets.begin (), offsets.end ());
13526 sect_offset *offsets_end = offsets.data () + offsets.size ();
13527 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13528 if (offsetp[-1] == *offsetp)
13529 complaint (_("Multiple children of DIE %s refer "
13530 "to DIE %s as their abstract origin"),
13531 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13532
13533 offsetp = offsets.data ();
13534 origin_child_die = origin_die->child;
13535 while (origin_child_die && origin_child_die->tag)
13536 {
13537 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13538 while (offsetp < offsets_end
13539 && *offsetp < origin_child_die->sect_off)
13540 offsetp++;
13541 if (offsetp >= offsets_end
13542 || *offsetp > origin_child_die->sect_off)
13543 {
13544 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13545 Check whether we're already processing ORIGIN_CHILD_DIE.
13546 This can happen with mutually referenced abstract_origins.
13547 PR 16581. */
13548 if (!origin_child_die->in_process)
13549 process_die (origin_child_die, origin_cu);
13550 }
13551 origin_child_die = origin_child_die->sibling;
13552 }
13553 origin_cu->list_in_scope = origin_previous_list_in_scope;
13554
13555 if (cu != origin_cu)
13556 compute_delayed_physnames (origin_cu);
13557 }
13558
13559 static void
13560 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13561 {
13562 struct objfile *objfile = cu->per_objfile->objfile;
13563 struct gdbarch *gdbarch = objfile->arch ();
13564 struct context_stack *newobj;
13565 CORE_ADDR lowpc;
13566 CORE_ADDR highpc;
13567 struct die_info *child_die;
13568 struct attribute *attr, *call_line, *call_file;
13569 const char *name;
13570 CORE_ADDR baseaddr;
13571 struct block *block;
13572 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13573 std::vector<struct symbol *> template_args;
13574 struct template_symbol *templ_func = NULL;
13575
13576 if (inlined_func)
13577 {
13578 /* If we do not have call site information, we can't show the
13579 caller of this inlined function. That's too confusing, so
13580 only use the scope for local variables. */
13581 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13582 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13583 if (call_line == NULL || call_file == NULL)
13584 {
13585 read_lexical_block_scope (die, cu);
13586 return;
13587 }
13588 }
13589
13590 baseaddr = objfile->text_section_offset ();
13591
13592 name = dwarf2_name (die, cu);
13593
13594 /* Ignore functions with missing or empty names. These are actually
13595 illegal according to the DWARF standard. */
13596 if (name == NULL)
13597 {
13598 complaint (_("missing name for subprogram DIE at %s"),
13599 sect_offset_str (die->sect_off));
13600 return;
13601 }
13602
13603 /* Ignore functions with missing or invalid low and high pc attributes. */
13604 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13605 <= PC_BOUNDS_INVALID)
13606 {
13607 attr = dwarf2_attr (die, DW_AT_external, cu);
13608 if (attr == nullptr || !attr->as_boolean ())
13609 complaint (_("cannot get low and high bounds "
13610 "for subprogram DIE at %s"),
13611 sect_offset_str (die->sect_off));
13612 return;
13613 }
13614
13615 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13616 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13617
13618 /* If we have any template arguments, then we must allocate a
13619 different sort of symbol. */
13620 for (child_die = die->child; child_die; child_die = child_die->sibling)
13621 {
13622 if (child_die->tag == DW_TAG_template_type_param
13623 || child_die->tag == DW_TAG_template_value_param)
13624 {
13625 templ_func = new (&objfile->objfile_obstack) template_symbol;
13626 templ_func->subclass = SYMBOL_TEMPLATE;
13627 break;
13628 }
13629 }
13630
13631 newobj = cu->get_builder ()->push_context (0, lowpc);
13632 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13633 (struct symbol *) templ_func);
13634
13635 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13636 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13637 cu->language);
13638
13639 /* If there is a location expression for DW_AT_frame_base, record
13640 it. */
13641 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13642 if (attr != nullptr)
13643 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13644
13645 /* If there is a location for the static link, record it. */
13646 newobj->static_link = NULL;
13647 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13648 if (attr != nullptr)
13649 {
13650 newobj->static_link
13651 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13652 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13653 cu->addr_type ());
13654 }
13655
13656 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13657
13658 if (die->child != NULL)
13659 {
13660 child_die = die->child;
13661 while (child_die && child_die->tag)
13662 {
13663 if (child_die->tag == DW_TAG_template_type_param
13664 || child_die->tag == DW_TAG_template_value_param)
13665 {
13666 struct symbol *arg = new_symbol (child_die, NULL, cu);
13667
13668 if (arg != NULL)
13669 template_args.push_back (arg);
13670 }
13671 else
13672 process_die (child_die, cu);
13673 child_die = child_die->sibling;
13674 }
13675 }
13676
13677 inherit_abstract_dies (die, cu);
13678
13679 /* If we have a DW_AT_specification, we might need to import using
13680 directives from the context of the specification DIE. See the
13681 comment in determine_prefix. */
13682 if (cu->language == language_cplus
13683 && dwarf2_attr (die, DW_AT_specification, cu))
13684 {
13685 struct dwarf2_cu *spec_cu = cu;
13686 struct die_info *spec_die = die_specification (die, &spec_cu);
13687
13688 while (spec_die)
13689 {
13690 child_die = spec_die->child;
13691 while (child_die && child_die->tag)
13692 {
13693 if (child_die->tag == DW_TAG_imported_module)
13694 process_die (child_die, spec_cu);
13695 child_die = child_die->sibling;
13696 }
13697
13698 /* In some cases, GCC generates specification DIEs that
13699 themselves contain DW_AT_specification attributes. */
13700 spec_die = die_specification (spec_die, &spec_cu);
13701 }
13702 }
13703
13704 struct context_stack cstk = cu->get_builder ()->pop_context ();
13705 /* Make a block for the local symbols within. */
13706 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13707 cstk.static_link, lowpc, highpc);
13708
13709 /* For C++, set the block's scope. */
13710 if ((cu->language == language_cplus
13711 || cu->language == language_fortran
13712 || cu->language == language_d
13713 || cu->language == language_rust)
13714 && cu->processing_has_namespace_info)
13715 block_set_scope (block, determine_prefix (die, cu),
13716 &objfile->objfile_obstack);
13717
13718 /* If we have address ranges, record them. */
13719 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13720
13721 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13722
13723 /* Attach template arguments to function. */
13724 if (!template_args.empty ())
13725 {
13726 gdb_assert (templ_func != NULL);
13727
13728 templ_func->n_template_arguments = template_args.size ();
13729 templ_func->template_arguments
13730 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13731 templ_func->n_template_arguments);
13732 memcpy (templ_func->template_arguments,
13733 template_args.data (),
13734 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13735
13736 /* Make sure that the symtab is set on the new symbols. Even
13737 though they don't appear in this symtab directly, other parts
13738 of gdb assume that symbols do, and this is reasonably
13739 true. */
13740 for (symbol *sym : template_args)
13741 symbol_set_symtab (sym, symbol_symtab (templ_func));
13742 }
13743
13744 /* In C++, we can have functions nested inside functions (e.g., when
13745 a function declares a class that has methods). This means that
13746 when we finish processing a function scope, we may need to go
13747 back to building a containing block's symbol lists. */
13748 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13749 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13750
13751 /* If we've finished processing a top-level function, subsequent
13752 symbols go in the file symbol list. */
13753 if (cu->get_builder ()->outermost_context_p ())
13754 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13755 }
13756
13757 /* Process all the DIES contained within a lexical block scope. Start
13758 a new scope, process the dies, and then close the scope. */
13759
13760 static void
13761 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13762 {
13763 struct objfile *objfile = cu->per_objfile->objfile;
13764 struct gdbarch *gdbarch = objfile->arch ();
13765 CORE_ADDR lowpc, highpc;
13766 struct die_info *child_die;
13767 CORE_ADDR baseaddr;
13768
13769 baseaddr = objfile->text_section_offset ();
13770
13771 /* Ignore blocks with missing or invalid low and high pc attributes. */
13772 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13773 as multiple lexical blocks? Handling children in a sane way would
13774 be nasty. Might be easier to properly extend generic blocks to
13775 describe ranges. */
13776 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13777 {
13778 case PC_BOUNDS_NOT_PRESENT:
13779 /* DW_TAG_lexical_block has no attributes, process its children as if
13780 there was no wrapping by that DW_TAG_lexical_block.
13781 GCC does no longer produces such DWARF since GCC r224161. */
13782 for (child_die = die->child;
13783 child_die != NULL && child_die->tag;
13784 child_die = child_die->sibling)
13785 {
13786 /* We might already be processing this DIE. This can happen
13787 in an unusual circumstance -- where a subroutine A
13788 appears lexically in another subroutine B, but A actually
13789 inlines B. The recursion is broken here, rather than in
13790 inherit_abstract_dies, because it seems better to simply
13791 drop concrete children here. */
13792 if (!child_die->in_process)
13793 process_die (child_die, cu);
13794 }
13795 return;
13796 case PC_BOUNDS_INVALID:
13797 return;
13798 }
13799 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13800 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13801
13802 cu->get_builder ()->push_context (0, lowpc);
13803 if (die->child != NULL)
13804 {
13805 child_die = die->child;
13806 while (child_die && child_die->tag)
13807 {
13808 process_die (child_die, cu);
13809 child_die = child_die->sibling;
13810 }
13811 }
13812 inherit_abstract_dies (die, cu);
13813 struct context_stack cstk = cu->get_builder ()->pop_context ();
13814
13815 if (*cu->get_builder ()->get_local_symbols () != NULL
13816 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13817 {
13818 struct block *block
13819 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13820 cstk.start_addr, highpc);
13821
13822 /* Note that recording ranges after traversing children, as we
13823 do here, means that recording a parent's ranges entails
13824 walking across all its children's ranges as they appear in
13825 the address map, which is quadratic behavior.
13826
13827 It would be nicer to record the parent's ranges before
13828 traversing its children, simply overriding whatever you find
13829 there. But since we don't even decide whether to create a
13830 block until after we've traversed its children, that's hard
13831 to do. */
13832 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13833 }
13834 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13835 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13836 }
13837
13838 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13839
13840 static void
13841 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13842 {
13843 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13844 struct objfile *objfile = per_objfile->objfile;
13845 struct gdbarch *gdbarch = objfile->arch ();
13846 CORE_ADDR pc, baseaddr;
13847 struct attribute *attr;
13848 struct call_site *call_site, call_site_local;
13849 void **slot;
13850 int nparams;
13851 struct die_info *child_die;
13852
13853 baseaddr = objfile->text_section_offset ();
13854
13855 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13856 if (attr == NULL)
13857 {
13858 /* This was a pre-DWARF-5 GNU extension alias
13859 for DW_AT_call_return_pc. */
13860 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13861 }
13862 if (!attr)
13863 {
13864 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13865 "DIE %s [in module %s]"),
13866 sect_offset_str (die->sect_off), objfile_name (objfile));
13867 return;
13868 }
13869 pc = attr->as_address () + baseaddr;
13870 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13871
13872 if (cu->call_site_htab == NULL)
13873 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13874 NULL, &objfile->objfile_obstack,
13875 hashtab_obstack_allocate, NULL);
13876 call_site_local.pc = pc;
13877 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13878 if (*slot != NULL)
13879 {
13880 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13881 "DIE %s [in module %s]"),
13882 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13883 objfile_name (objfile));
13884 return;
13885 }
13886
13887 /* Count parameters at the caller. */
13888
13889 nparams = 0;
13890 for (child_die = die->child; child_die && child_die->tag;
13891 child_die = child_die->sibling)
13892 {
13893 if (child_die->tag != DW_TAG_call_site_parameter
13894 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13895 {
13896 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13897 "DW_TAG_call_site child DIE %s [in module %s]"),
13898 child_die->tag, sect_offset_str (child_die->sect_off),
13899 objfile_name (objfile));
13900 continue;
13901 }
13902
13903 nparams++;
13904 }
13905
13906 call_site
13907 = ((struct call_site *)
13908 obstack_alloc (&objfile->objfile_obstack,
13909 sizeof (*call_site)
13910 + (sizeof (*call_site->parameter) * (nparams - 1))));
13911 *slot = call_site;
13912 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13913 call_site->pc = pc;
13914
13915 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13916 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13917 {
13918 struct die_info *func_die;
13919
13920 /* Skip also over DW_TAG_inlined_subroutine. */
13921 for (func_die = die->parent;
13922 func_die && func_die->tag != DW_TAG_subprogram
13923 && func_die->tag != DW_TAG_subroutine_type;
13924 func_die = func_die->parent);
13925
13926 /* DW_AT_call_all_calls is a superset
13927 of DW_AT_call_all_tail_calls. */
13928 if (func_die
13929 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13930 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13931 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13932 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13933 {
13934 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13935 not complete. But keep CALL_SITE for look ups via call_site_htab,
13936 both the initial caller containing the real return address PC and
13937 the final callee containing the current PC of a chain of tail
13938 calls do not need to have the tail call list complete. But any
13939 function candidate for a virtual tail call frame searched via
13940 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13941 determined unambiguously. */
13942 }
13943 else
13944 {
13945 struct type *func_type = NULL;
13946
13947 if (func_die)
13948 func_type = get_die_type (func_die, cu);
13949 if (func_type != NULL)
13950 {
13951 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13952
13953 /* Enlist this call site to the function. */
13954 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13955 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13956 }
13957 else
13958 complaint (_("Cannot find function owning DW_TAG_call_site "
13959 "DIE %s [in module %s]"),
13960 sect_offset_str (die->sect_off), objfile_name (objfile));
13961 }
13962 }
13963
13964 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13965 if (attr == NULL)
13966 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13967 if (attr == NULL)
13968 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13969 if (attr == NULL)
13970 {
13971 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13972 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13973 }
13974 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13975 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13976 /* Keep NULL DWARF_BLOCK. */;
13977 else if (attr->form_is_block ())
13978 {
13979 struct dwarf2_locexpr_baton *dlbaton;
13980 struct dwarf_block *block = attr->as_block ();
13981
13982 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13983 dlbaton->data = block->data;
13984 dlbaton->size = block->size;
13985 dlbaton->per_objfile = per_objfile;
13986 dlbaton->per_cu = cu->per_cu;
13987
13988 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13989 }
13990 else if (attr->form_is_ref ())
13991 {
13992 struct dwarf2_cu *target_cu = cu;
13993 struct die_info *target_die;
13994
13995 target_die = follow_die_ref (die, attr, &target_cu);
13996 gdb_assert (target_cu->per_objfile->objfile == objfile);
13997 if (die_is_declaration (target_die, target_cu))
13998 {
13999 const char *target_physname;
14000
14001 /* Prefer the mangled name; otherwise compute the demangled one. */
14002 target_physname = dw2_linkage_name (target_die, target_cu);
14003 if (target_physname == NULL)
14004 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14005 if (target_physname == NULL)
14006 complaint (_("DW_AT_call_target target DIE has invalid "
14007 "physname, for referencing DIE %s [in module %s]"),
14008 sect_offset_str (die->sect_off), objfile_name (objfile));
14009 else
14010 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14011 }
14012 else
14013 {
14014 CORE_ADDR lowpc;
14015
14016 /* DW_AT_entry_pc should be preferred. */
14017 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14018 <= PC_BOUNDS_INVALID)
14019 complaint (_("DW_AT_call_target target DIE has invalid "
14020 "low pc, for referencing DIE %s [in module %s]"),
14021 sect_offset_str (die->sect_off), objfile_name (objfile));
14022 else
14023 {
14024 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14025 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14026 }
14027 }
14028 }
14029 else
14030 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14031 "block nor reference, for DIE %s [in module %s]"),
14032 sect_offset_str (die->sect_off), objfile_name (objfile));
14033
14034 call_site->per_cu = cu->per_cu;
14035 call_site->per_objfile = per_objfile;
14036
14037 for (child_die = die->child;
14038 child_die && child_die->tag;
14039 child_die = child_die->sibling)
14040 {
14041 struct call_site_parameter *parameter;
14042 struct attribute *loc, *origin;
14043
14044 if (child_die->tag != DW_TAG_call_site_parameter
14045 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14046 {
14047 /* Already printed the complaint above. */
14048 continue;
14049 }
14050
14051 gdb_assert (call_site->parameter_count < nparams);
14052 parameter = &call_site->parameter[call_site->parameter_count];
14053
14054 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14055 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14056 register is contained in DW_AT_call_value. */
14057
14058 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14059 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14060 if (origin == NULL)
14061 {
14062 /* This was a pre-DWARF-5 GNU extension alias
14063 for DW_AT_call_parameter. */
14064 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14065 }
14066 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14067 {
14068 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14069
14070 sect_offset sect_off = origin->get_ref_die_offset ();
14071 if (!cu->header.offset_in_cu_p (sect_off))
14072 {
14073 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14074 binding can be done only inside one CU. Such referenced DIE
14075 therefore cannot be even moved to DW_TAG_partial_unit. */
14076 complaint (_("DW_AT_call_parameter offset is not in CU for "
14077 "DW_TAG_call_site child DIE %s [in module %s]"),
14078 sect_offset_str (child_die->sect_off),
14079 objfile_name (objfile));
14080 continue;
14081 }
14082 parameter->u.param_cu_off
14083 = (cu_offset) (sect_off - cu->header.sect_off);
14084 }
14085 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14086 {
14087 complaint (_("No DW_FORM_block* DW_AT_location for "
14088 "DW_TAG_call_site child DIE %s [in module %s]"),
14089 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14090 continue;
14091 }
14092 else
14093 {
14094 struct dwarf_block *block = loc->as_block ();
14095
14096 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14097 (block->data, &block->data[block->size]);
14098 if (parameter->u.dwarf_reg != -1)
14099 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14100 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14101 &block->data[block->size],
14102 &parameter->u.fb_offset))
14103 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14104 else
14105 {
14106 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14107 "for DW_FORM_block* DW_AT_location is supported for "
14108 "DW_TAG_call_site child DIE %s "
14109 "[in module %s]"),
14110 sect_offset_str (child_die->sect_off),
14111 objfile_name (objfile));
14112 continue;
14113 }
14114 }
14115
14116 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14117 if (attr == NULL)
14118 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14119 if (attr == NULL || !attr->form_is_block ())
14120 {
14121 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14122 "DW_TAG_call_site child DIE %s [in module %s]"),
14123 sect_offset_str (child_die->sect_off),
14124 objfile_name (objfile));
14125 continue;
14126 }
14127
14128 struct dwarf_block *block = attr->as_block ();
14129 parameter->value = block->data;
14130 parameter->value_size = block->size;
14131
14132 /* Parameters are not pre-cleared by memset above. */
14133 parameter->data_value = NULL;
14134 parameter->data_value_size = 0;
14135 call_site->parameter_count++;
14136
14137 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14138 if (attr == NULL)
14139 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14140 if (attr != nullptr)
14141 {
14142 if (!attr->form_is_block ())
14143 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14144 "DW_TAG_call_site child DIE %s [in module %s]"),
14145 sect_offset_str (child_die->sect_off),
14146 objfile_name (objfile));
14147 else
14148 {
14149 block = attr->as_block ();
14150 parameter->data_value = block->data;
14151 parameter->data_value_size = block->size;
14152 }
14153 }
14154 }
14155 }
14156
14157 /* Helper function for read_variable. If DIE represents a virtual
14158 table, then return the type of the concrete object that is
14159 associated with the virtual table. Otherwise, return NULL. */
14160
14161 static struct type *
14162 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14163 {
14164 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14165 if (attr == NULL)
14166 return NULL;
14167
14168 /* Find the type DIE. */
14169 struct die_info *type_die = NULL;
14170 struct dwarf2_cu *type_cu = cu;
14171
14172 if (attr->form_is_ref ())
14173 type_die = follow_die_ref (die, attr, &type_cu);
14174 if (type_die == NULL)
14175 return NULL;
14176
14177 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14178 return NULL;
14179 return die_containing_type (type_die, type_cu);
14180 }
14181
14182 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14183
14184 static void
14185 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14186 {
14187 struct rust_vtable_symbol *storage = NULL;
14188
14189 if (cu->language == language_rust)
14190 {
14191 struct type *containing_type = rust_containing_type (die, cu);
14192
14193 if (containing_type != NULL)
14194 {
14195 struct objfile *objfile = cu->per_objfile->objfile;
14196
14197 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14198 storage->concrete_type = containing_type;
14199 storage->subclass = SYMBOL_RUST_VTABLE;
14200 }
14201 }
14202
14203 struct symbol *res = new_symbol (die, NULL, cu, storage);
14204 struct attribute *abstract_origin
14205 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14206 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14207 if (res == NULL && loc && abstract_origin)
14208 {
14209 /* We have a variable without a name, but with a location and an abstract
14210 origin. This may be a concrete instance of an abstract variable
14211 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14212 later. */
14213 struct dwarf2_cu *origin_cu = cu;
14214 struct die_info *origin_die
14215 = follow_die_ref (die, abstract_origin, &origin_cu);
14216 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14217 per_objfile->per_bfd->abstract_to_concrete
14218 [origin_die->sect_off].push_back (die->sect_off);
14219 }
14220 }
14221
14222 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14223 reading .debug_rnglists.
14224 Callback's type should be:
14225 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14226 Return true if the attributes are present and valid, otherwise,
14227 return false. */
14228
14229 template <typename Callback>
14230 static bool
14231 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14232 dwarf_tag tag, Callback &&callback)
14233 {
14234 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14235 struct objfile *objfile = per_objfile->objfile;
14236 bfd *obfd = objfile->obfd;
14237 /* Base address selection entry. */
14238 gdb::optional<CORE_ADDR> base;
14239 const gdb_byte *buffer;
14240 CORE_ADDR baseaddr;
14241 bool overflow = false;
14242 ULONGEST addr_index;
14243 struct dwarf2_section_info *rnglists_section;
14244
14245 base = cu->base_address;
14246 rnglists_section = cu_debug_rnglists_section (cu, tag);
14247 rnglists_section->read (objfile);
14248
14249 if (offset >= rnglists_section->size)
14250 {
14251 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14252 offset);
14253 return false;
14254 }
14255 buffer = rnglists_section->buffer + offset;
14256
14257 baseaddr = objfile->text_section_offset ();
14258
14259 while (1)
14260 {
14261 /* Initialize it due to a false compiler warning. */
14262 CORE_ADDR range_beginning = 0, range_end = 0;
14263 const gdb_byte *buf_end = (rnglists_section->buffer
14264 + rnglists_section->size);
14265 unsigned int bytes_read;
14266
14267 if (buffer == buf_end)
14268 {
14269 overflow = true;
14270 break;
14271 }
14272 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14273 switch (rlet)
14274 {
14275 case DW_RLE_end_of_list:
14276 break;
14277 case DW_RLE_base_address:
14278 if (buffer + cu->header.addr_size > buf_end)
14279 {
14280 overflow = true;
14281 break;
14282 }
14283 base = cu->header.read_address (obfd, buffer, &bytes_read);
14284 buffer += bytes_read;
14285 break;
14286 case DW_RLE_base_addressx:
14287 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14288 buffer += bytes_read;
14289 base = read_addr_index (cu, addr_index);
14290 break;
14291 case DW_RLE_start_length:
14292 if (buffer + cu->header.addr_size > buf_end)
14293 {
14294 overflow = true;
14295 break;
14296 }
14297 range_beginning = cu->header.read_address (obfd, buffer,
14298 &bytes_read);
14299 buffer += bytes_read;
14300 range_end = (range_beginning
14301 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14302 buffer += bytes_read;
14303 if (buffer > buf_end)
14304 {
14305 overflow = true;
14306 break;
14307 }
14308 break;
14309 case DW_RLE_startx_length:
14310 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14311 buffer += bytes_read;
14312 range_beginning = read_addr_index (cu, addr_index);
14313 if (buffer > buf_end)
14314 {
14315 overflow = true;
14316 break;
14317 }
14318 range_end = (range_beginning
14319 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14320 buffer += bytes_read;
14321 break;
14322 case DW_RLE_offset_pair:
14323 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14324 buffer += bytes_read;
14325 if (buffer > buf_end)
14326 {
14327 overflow = true;
14328 break;
14329 }
14330 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14331 buffer += bytes_read;
14332 if (buffer > buf_end)
14333 {
14334 overflow = true;
14335 break;
14336 }
14337 break;
14338 case DW_RLE_start_end:
14339 if (buffer + 2 * cu->header.addr_size > buf_end)
14340 {
14341 overflow = true;
14342 break;
14343 }
14344 range_beginning = cu->header.read_address (obfd, buffer,
14345 &bytes_read);
14346 buffer += bytes_read;
14347 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14348 buffer += bytes_read;
14349 break;
14350 case DW_RLE_startx_endx:
14351 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14352 buffer += bytes_read;
14353 range_beginning = read_addr_index (cu, addr_index);
14354 if (buffer > buf_end)
14355 {
14356 overflow = true;
14357 break;
14358 }
14359 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14360 buffer += bytes_read;
14361 range_end = read_addr_index (cu, addr_index);
14362 break;
14363 default:
14364 complaint (_("Invalid .debug_rnglists data (no base address)"));
14365 return false;
14366 }
14367 if (rlet == DW_RLE_end_of_list || overflow)
14368 break;
14369 if (rlet == DW_RLE_base_address)
14370 continue;
14371
14372 if (range_beginning > range_end)
14373 {
14374 /* Inverted range entries are invalid. */
14375 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14376 return false;
14377 }
14378
14379 /* Empty range entries have no effect. */
14380 if (range_beginning == range_end)
14381 continue;
14382
14383 /* Only DW_RLE_offset_pair needs the base address added. */
14384 if (rlet == DW_RLE_offset_pair)
14385 {
14386 if (!base.has_value ())
14387 {
14388 /* We have no valid base address for the DW_RLE_offset_pair. */
14389 complaint (_("Invalid .debug_rnglists data (no base address for "
14390 "DW_RLE_offset_pair)"));
14391 return false;
14392 }
14393
14394 range_beginning += *base;
14395 range_end += *base;
14396 }
14397
14398 /* A not-uncommon case of bad debug info.
14399 Don't pollute the addrmap with bad data. */
14400 if (range_beginning + baseaddr == 0
14401 && !per_objfile->per_bfd->has_section_at_zero)
14402 {
14403 complaint (_(".debug_rnglists entry has start address of zero"
14404 " [in module %s]"), objfile_name (objfile));
14405 continue;
14406 }
14407
14408 callback (range_beginning, range_end);
14409 }
14410
14411 if (overflow)
14412 {
14413 complaint (_("Offset %d is not terminated "
14414 "for DW_AT_ranges attribute"),
14415 offset);
14416 return false;
14417 }
14418
14419 return true;
14420 }
14421
14422 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14423 Callback's type should be:
14424 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14425 Return 1 if the attributes are present and valid, otherwise, return 0. */
14426
14427 template <typename Callback>
14428 static int
14429 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14430 Callback &&callback)
14431 {
14432 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14433 struct objfile *objfile = per_objfile->objfile;
14434 struct comp_unit_head *cu_header = &cu->header;
14435 bfd *obfd = objfile->obfd;
14436 unsigned int addr_size = cu_header->addr_size;
14437 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14438 /* Base address selection entry. */
14439 gdb::optional<CORE_ADDR> base;
14440 unsigned int dummy;
14441 const gdb_byte *buffer;
14442 CORE_ADDR baseaddr;
14443
14444 if (cu_header->version >= 5)
14445 return dwarf2_rnglists_process (offset, cu, tag, callback);
14446
14447 base = cu->base_address;
14448
14449 per_objfile->per_bfd->ranges.read (objfile);
14450 if (offset >= per_objfile->per_bfd->ranges.size)
14451 {
14452 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14453 offset);
14454 return 0;
14455 }
14456 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14457
14458 baseaddr = objfile->text_section_offset ();
14459
14460 while (1)
14461 {
14462 CORE_ADDR range_beginning, range_end;
14463
14464 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14465 buffer += addr_size;
14466 range_end = cu->header.read_address (obfd, buffer, &dummy);
14467 buffer += addr_size;
14468 offset += 2 * addr_size;
14469
14470 /* An end of list marker is a pair of zero addresses. */
14471 if (range_beginning == 0 && range_end == 0)
14472 /* Found the end of list entry. */
14473 break;
14474
14475 /* Each base address selection entry is a pair of 2 values.
14476 The first is the largest possible address, the second is
14477 the base address. Check for a base address here. */
14478 if ((range_beginning & mask) == mask)
14479 {
14480 /* If we found the largest possible address, then we already
14481 have the base address in range_end. */
14482 base = range_end;
14483 continue;
14484 }
14485
14486 if (!base.has_value ())
14487 {
14488 /* We have no valid base address for the ranges
14489 data. */
14490 complaint (_("Invalid .debug_ranges data (no base address)"));
14491 return 0;
14492 }
14493
14494 if (range_beginning > range_end)
14495 {
14496 /* Inverted range entries are invalid. */
14497 complaint (_("Invalid .debug_ranges data (inverted range)"));
14498 return 0;
14499 }
14500
14501 /* Empty range entries have no effect. */
14502 if (range_beginning == range_end)
14503 continue;
14504
14505 range_beginning += *base;
14506 range_end += *base;
14507
14508 /* A not-uncommon case of bad debug info.
14509 Don't pollute the addrmap with bad data. */
14510 if (range_beginning + baseaddr == 0
14511 && !per_objfile->per_bfd->has_section_at_zero)
14512 {
14513 complaint (_(".debug_ranges entry has start address of zero"
14514 " [in module %s]"), objfile_name (objfile));
14515 continue;
14516 }
14517
14518 callback (range_beginning, range_end);
14519 }
14520
14521 return 1;
14522 }
14523
14524 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14525 Return 1 if the attributes are present and valid, otherwise, return 0.
14526 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14527
14528 static int
14529 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14530 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14531 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14532 {
14533 struct objfile *objfile = cu->per_objfile->objfile;
14534 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14535 struct gdbarch *gdbarch = objfile->arch ();
14536 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14537 int low_set = 0;
14538 CORE_ADDR low = 0;
14539 CORE_ADDR high = 0;
14540 int retval;
14541
14542 retval = dwarf2_ranges_process (offset, cu, tag,
14543 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14544 {
14545 if (ranges_pst != NULL)
14546 {
14547 CORE_ADDR lowpc;
14548 CORE_ADDR highpc;
14549
14550 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14551 range_beginning + baseaddr)
14552 - baseaddr);
14553 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14554 range_end + baseaddr)
14555 - baseaddr);
14556 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14557 lowpc, highpc - 1, ranges_pst);
14558 }
14559
14560 /* FIXME: This is recording everything as a low-high
14561 segment of consecutive addresses. We should have a
14562 data structure for discontiguous block ranges
14563 instead. */
14564 if (! low_set)
14565 {
14566 low = range_beginning;
14567 high = range_end;
14568 low_set = 1;
14569 }
14570 else
14571 {
14572 if (range_beginning < low)
14573 low = range_beginning;
14574 if (range_end > high)
14575 high = range_end;
14576 }
14577 });
14578 if (!retval)
14579 return 0;
14580
14581 if (! low_set)
14582 /* If the first entry is an end-of-list marker, the range
14583 describes an empty scope, i.e. no instructions. */
14584 return 0;
14585
14586 if (low_return)
14587 *low_return = low;
14588 if (high_return)
14589 *high_return = high;
14590 return 1;
14591 }
14592
14593 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14594 definition for the return value. *LOWPC and *HIGHPC are set iff
14595 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14596
14597 static enum pc_bounds_kind
14598 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14599 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14600 dwarf2_psymtab *pst)
14601 {
14602 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14603 struct attribute *attr;
14604 struct attribute *attr_high;
14605 CORE_ADDR low = 0;
14606 CORE_ADDR high = 0;
14607 enum pc_bounds_kind ret;
14608
14609 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14610 if (attr_high)
14611 {
14612 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14613 if (attr != nullptr)
14614 {
14615 low = attr->as_address ();
14616 high = attr_high->as_address ();
14617 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14618 high += low;
14619 }
14620 else
14621 /* Found high w/o low attribute. */
14622 return PC_BOUNDS_INVALID;
14623
14624 /* Found consecutive range of addresses. */
14625 ret = PC_BOUNDS_HIGH_LOW;
14626 }
14627 else
14628 {
14629 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14630 if (attr != nullptr && attr->form_is_unsigned ())
14631 {
14632 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14633 on DWARF version). */
14634 ULONGEST ranges_offset = attr->as_unsigned ();
14635
14636 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14637 this value. */
14638 if (die->tag != DW_TAG_compile_unit)
14639 ranges_offset += cu->gnu_ranges_base;
14640
14641 /* Value of the DW_AT_ranges attribute is the offset in the
14642 .debug_ranges section. */
14643 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14644 die->tag))
14645 return PC_BOUNDS_INVALID;
14646 /* Found discontinuous range of addresses. */
14647 ret = PC_BOUNDS_RANGES;
14648 }
14649 else
14650 return PC_BOUNDS_NOT_PRESENT;
14651 }
14652
14653 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14654 if (high <= low)
14655 return PC_BOUNDS_INVALID;
14656
14657 /* When using the GNU linker, .gnu.linkonce. sections are used to
14658 eliminate duplicate copies of functions and vtables and such.
14659 The linker will arbitrarily choose one and discard the others.
14660 The AT_*_pc values for such functions refer to local labels in
14661 these sections. If the section from that file was discarded, the
14662 labels are not in the output, so the relocs get a value of 0.
14663 If this is a discarded function, mark the pc bounds as invalid,
14664 so that GDB will ignore it. */
14665 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14666 return PC_BOUNDS_INVALID;
14667
14668 *lowpc = low;
14669 if (highpc)
14670 *highpc = high;
14671 return ret;
14672 }
14673
14674 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14675 its low and high PC addresses. Do nothing if these addresses could not
14676 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14677 and HIGHPC to the high address if greater than HIGHPC. */
14678
14679 static void
14680 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14681 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14682 struct dwarf2_cu *cu)
14683 {
14684 CORE_ADDR low, high;
14685 struct die_info *child = die->child;
14686
14687 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14688 {
14689 *lowpc = std::min (*lowpc, low);
14690 *highpc = std::max (*highpc, high);
14691 }
14692
14693 /* If the language does not allow nested subprograms (either inside
14694 subprograms or lexical blocks), we're done. */
14695 if (cu->language != language_ada)
14696 return;
14697
14698 /* Check all the children of the given DIE. If it contains nested
14699 subprograms, then check their pc bounds. Likewise, we need to
14700 check lexical blocks as well, as they may also contain subprogram
14701 definitions. */
14702 while (child && child->tag)
14703 {
14704 if (child->tag == DW_TAG_subprogram
14705 || child->tag == DW_TAG_lexical_block)
14706 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14707 child = child->sibling;
14708 }
14709 }
14710
14711 /* Get the low and high pc's represented by the scope DIE, and store
14712 them in *LOWPC and *HIGHPC. If the correct values can't be
14713 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14714
14715 static void
14716 get_scope_pc_bounds (struct die_info *die,
14717 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14718 struct dwarf2_cu *cu)
14719 {
14720 CORE_ADDR best_low = (CORE_ADDR) -1;
14721 CORE_ADDR best_high = (CORE_ADDR) 0;
14722 CORE_ADDR current_low, current_high;
14723
14724 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14725 >= PC_BOUNDS_RANGES)
14726 {
14727 best_low = current_low;
14728 best_high = current_high;
14729 }
14730 else
14731 {
14732 struct die_info *child = die->child;
14733
14734 while (child && child->tag)
14735 {
14736 switch (child->tag) {
14737 case DW_TAG_subprogram:
14738 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14739 break;
14740 case DW_TAG_namespace:
14741 case DW_TAG_module:
14742 /* FIXME: carlton/2004-01-16: Should we do this for
14743 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14744 that current GCC's always emit the DIEs corresponding
14745 to definitions of methods of classes as children of a
14746 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14747 the DIEs giving the declarations, which could be
14748 anywhere). But I don't see any reason why the
14749 standards says that they have to be there. */
14750 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14751
14752 if (current_low != ((CORE_ADDR) -1))
14753 {
14754 best_low = std::min (best_low, current_low);
14755 best_high = std::max (best_high, current_high);
14756 }
14757 break;
14758 default:
14759 /* Ignore. */
14760 break;
14761 }
14762
14763 child = child->sibling;
14764 }
14765 }
14766
14767 *lowpc = best_low;
14768 *highpc = best_high;
14769 }
14770
14771 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14772 in DIE. */
14773
14774 static void
14775 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14776 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14777 {
14778 struct objfile *objfile = cu->per_objfile->objfile;
14779 struct gdbarch *gdbarch = objfile->arch ();
14780 struct attribute *attr;
14781 struct attribute *attr_high;
14782
14783 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14784 if (attr_high)
14785 {
14786 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14787 if (attr != nullptr)
14788 {
14789 CORE_ADDR low = attr->as_address ();
14790 CORE_ADDR high = attr_high->as_address ();
14791
14792 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14793 high += low;
14794
14795 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14796 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14797 cu->get_builder ()->record_block_range (block, low, high - 1);
14798 }
14799 }
14800
14801 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14802 if (attr != nullptr && attr->form_is_unsigned ())
14803 {
14804 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14805 on DWARF version). */
14806 ULONGEST ranges_offset = attr->as_unsigned ();
14807
14808 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14809 this value. */
14810 if (die->tag != DW_TAG_compile_unit)
14811 ranges_offset += cu->gnu_ranges_base;
14812
14813 std::vector<blockrange> blockvec;
14814 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14815 [&] (CORE_ADDR start, CORE_ADDR end)
14816 {
14817 start += baseaddr;
14818 end += baseaddr;
14819 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14820 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14821 cu->get_builder ()->record_block_range (block, start, end - 1);
14822 blockvec.emplace_back (start, end);
14823 });
14824
14825 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14826 }
14827 }
14828
14829 /* Check whether the producer field indicates either of GCC < 4.6, or the
14830 Intel C/C++ compiler, and cache the result in CU. */
14831
14832 static void
14833 check_producer (struct dwarf2_cu *cu)
14834 {
14835 int major, minor;
14836
14837 if (cu->producer == NULL)
14838 {
14839 /* For unknown compilers expect their behavior is DWARF version
14840 compliant.
14841
14842 GCC started to support .debug_types sections by -gdwarf-4 since
14843 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14844 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14845 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14846 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14847 }
14848 else if (producer_is_gcc (cu->producer, &major, &minor))
14849 {
14850 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14851 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14852 }
14853 else if (producer_is_icc (cu->producer, &major, &minor))
14854 {
14855 cu->producer_is_icc = true;
14856 cu->producer_is_icc_lt_14 = major < 14;
14857 }
14858 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14859 cu->producer_is_codewarrior = true;
14860 else
14861 {
14862 /* For other non-GCC compilers, expect their behavior is DWARF version
14863 compliant. */
14864 }
14865
14866 cu->checked_producer = true;
14867 }
14868
14869 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14870 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14871 during 4.6.0 experimental. */
14872
14873 static bool
14874 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14875 {
14876 if (!cu->checked_producer)
14877 check_producer (cu);
14878
14879 return cu->producer_is_gxx_lt_4_6;
14880 }
14881
14882
14883 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14884 with incorrect is_stmt attributes. */
14885
14886 static bool
14887 producer_is_codewarrior (struct dwarf2_cu *cu)
14888 {
14889 if (!cu->checked_producer)
14890 check_producer (cu);
14891
14892 return cu->producer_is_codewarrior;
14893 }
14894
14895 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14896 If that attribute is not available, return the appropriate
14897 default. */
14898
14899 static enum dwarf_access_attribute
14900 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14901 {
14902 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14903 if (attr != nullptr)
14904 {
14905 LONGEST value = attr->constant_value (-1);
14906 if (value == DW_ACCESS_public
14907 || value == DW_ACCESS_protected
14908 || value == DW_ACCESS_private)
14909 return (dwarf_access_attribute) value;
14910 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14911 plongest (value));
14912 }
14913
14914 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14915 {
14916 /* The default DWARF 2 accessibility for members is public, the default
14917 accessibility for inheritance is private. */
14918
14919 if (die->tag != DW_TAG_inheritance)
14920 return DW_ACCESS_public;
14921 else
14922 return DW_ACCESS_private;
14923 }
14924 else
14925 {
14926 /* DWARF 3+ defines the default accessibility a different way. The same
14927 rules apply now for DW_TAG_inheritance as for the members and it only
14928 depends on the container kind. */
14929
14930 if (die->parent->tag == DW_TAG_class_type)
14931 return DW_ACCESS_private;
14932 else
14933 return DW_ACCESS_public;
14934 }
14935 }
14936
14937 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14938 offset. If the attribute was not found return 0, otherwise return
14939 1. If it was found but could not properly be handled, set *OFFSET
14940 to 0. */
14941
14942 static int
14943 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14944 LONGEST *offset)
14945 {
14946 struct attribute *attr;
14947
14948 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14949 if (attr != NULL)
14950 {
14951 *offset = 0;
14952
14953 /* Note that we do not check for a section offset first here.
14954 This is because DW_AT_data_member_location is new in DWARF 4,
14955 so if we see it, we can assume that a constant form is really
14956 a constant and not a section offset. */
14957 if (attr->form_is_constant ())
14958 *offset = attr->constant_value (0);
14959 else if (attr->form_is_section_offset ())
14960 dwarf2_complex_location_expr_complaint ();
14961 else if (attr->form_is_block ())
14962 *offset = decode_locdesc (attr->as_block (), cu);
14963 else
14964 dwarf2_complex_location_expr_complaint ();
14965
14966 return 1;
14967 }
14968
14969 return 0;
14970 }
14971
14972 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14973
14974 static void
14975 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14976 struct field *field)
14977 {
14978 struct attribute *attr;
14979
14980 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14981 if (attr != NULL)
14982 {
14983 if (attr->form_is_constant ())
14984 {
14985 LONGEST offset = attr->constant_value (0);
14986 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14987 }
14988 else if (attr->form_is_section_offset ())
14989 dwarf2_complex_location_expr_complaint ();
14990 else if (attr->form_is_block ())
14991 {
14992 bool handled;
14993 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14994 if (handled)
14995 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14996 else
14997 {
14998 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14999 struct objfile *objfile = per_objfile->objfile;
15000 struct dwarf2_locexpr_baton *dlbaton
15001 = XOBNEW (&objfile->objfile_obstack,
15002 struct dwarf2_locexpr_baton);
15003 dlbaton->data = attr->as_block ()->data;
15004 dlbaton->size = attr->as_block ()->size;
15005 /* When using this baton, we want to compute the address
15006 of the field, not the value. This is why
15007 is_reference is set to false here. */
15008 dlbaton->is_reference = false;
15009 dlbaton->per_objfile = per_objfile;
15010 dlbaton->per_cu = cu->per_cu;
15011
15012 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15013 }
15014 }
15015 else
15016 dwarf2_complex_location_expr_complaint ();
15017 }
15018 }
15019
15020 /* Add an aggregate field to the field list. */
15021
15022 static void
15023 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15024 struct dwarf2_cu *cu)
15025 {
15026 struct objfile *objfile = cu->per_objfile->objfile;
15027 struct gdbarch *gdbarch = objfile->arch ();
15028 struct nextfield *new_field;
15029 struct attribute *attr;
15030 struct field *fp;
15031 const char *fieldname = "";
15032
15033 if (die->tag == DW_TAG_inheritance)
15034 {
15035 fip->baseclasses.emplace_back ();
15036 new_field = &fip->baseclasses.back ();
15037 }
15038 else
15039 {
15040 fip->fields.emplace_back ();
15041 new_field = &fip->fields.back ();
15042 }
15043
15044 new_field->offset = die->sect_off;
15045
15046 new_field->accessibility = dwarf2_access_attribute (die, cu);
15047 if (new_field->accessibility != DW_ACCESS_public)
15048 fip->non_public_fields = true;
15049
15050 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15051 if (attr != nullptr)
15052 new_field->virtuality = attr->as_virtuality ();
15053 else
15054 new_field->virtuality = DW_VIRTUALITY_none;
15055
15056 fp = &new_field->field;
15057
15058 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15059 {
15060 /* Data member other than a C++ static data member. */
15061
15062 /* Get type of field. */
15063 fp->set_type (die_type (die, cu));
15064
15065 SET_FIELD_BITPOS (*fp, 0);
15066
15067 /* Get bit size of field (zero if none). */
15068 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15069 if (attr != nullptr)
15070 {
15071 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15072 }
15073 else
15074 {
15075 FIELD_BITSIZE (*fp) = 0;
15076 }
15077
15078 /* Get bit offset of field. */
15079 handle_data_member_location (die, cu, fp);
15080 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15081 if (attr != nullptr && attr->form_is_constant ())
15082 {
15083 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15084 {
15085 /* For big endian bits, the DW_AT_bit_offset gives the
15086 additional bit offset from the MSB of the containing
15087 anonymous object to the MSB of the field. We don't
15088 have to do anything special since we don't need to
15089 know the size of the anonymous object. */
15090 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15091 + attr->constant_value (0)));
15092 }
15093 else
15094 {
15095 /* For little endian bits, compute the bit offset to the
15096 MSB of the anonymous object, subtract off the number of
15097 bits from the MSB of the field to the MSB of the
15098 object, and then subtract off the number of bits of
15099 the field itself. The result is the bit offset of
15100 the LSB of the field. */
15101 int anonymous_size;
15102 int bit_offset = attr->constant_value (0);
15103
15104 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15105 if (attr != nullptr && attr->form_is_constant ())
15106 {
15107 /* The size of the anonymous object containing
15108 the bit field is explicit, so use the
15109 indicated size (in bytes). */
15110 anonymous_size = attr->constant_value (0);
15111 }
15112 else
15113 {
15114 /* The size of the anonymous object containing
15115 the bit field must be inferred from the type
15116 attribute of the data member containing the
15117 bit field. */
15118 anonymous_size = TYPE_LENGTH (fp->type ());
15119 }
15120 SET_FIELD_BITPOS (*fp,
15121 (FIELD_BITPOS (*fp)
15122 + anonymous_size * bits_per_byte
15123 - bit_offset - FIELD_BITSIZE (*fp)));
15124 }
15125 }
15126 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15127 if (attr != NULL)
15128 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15129 + attr->constant_value (0)));
15130
15131 /* Get name of field. */
15132 fieldname = dwarf2_name (die, cu);
15133 if (fieldname == NULL)
15134 fieldname = "";
15135
15136 /* The name is already allocated along with this objfile, so we don't
15137 need to duplicate it for the type. */
15138 fp->name = fieldname;
15139
15140 /* Change accessibility for artificial fields (e.g. virtual table
15141 pointer or virtual base class pointer) to private. */
15142 if (dwarf2_attr (die, DW_AT_artificial, cu))
15143 {
15144 FIELD_ARTIFICIAL (*fp) = 1;
15145 new_field->accessibility = DW_ACCESS_private;
15146 fip->non_public_fields = true;
15147 }
15148 }
15149 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15150 {
15151 /* C++ static member. */
15152
15153 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15154 is a declaration, but all versions of G++ as of this writing
15155 (so through at least 3.2.1) incorrectly generate
15156 DW_TAG_variable tags. */
15157
15158 const char *physname;
15159
15160 /* Get name of field. */
15161 fieldname = dwarf2_name (die, cu);
15162 if (fieldname == NULL)
15163 return;
15164
15165 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15166 if (attr
15167 /* Only create a symbol if this is an external value.
15168 new_symbol checks this and puts the value in the global symbol
15169 table, which we want. If it is not external, new_symbol
15170 will try to put the value in cu->list_in_scope which is wrong. */
15171 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15172 {
15173 /* A static const member, not much different than an enum as far as
15174 we're concerned, except that we can support more types. */
15175 new_symbol (die, NULL, cu);
15176 }
15177
15178 /* Get physical name. */
15179 physname = dwarf2_physname (fieldname, die, cu);
15180
15181 /* The name is already allocated along with this objfile, so we don't
15182 need to duplicate it for the type. */
15183 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15184 fp->set_type (die_type (die, cu));
15185 FIELD_NAME (*fp) = fieldname;
15186 }
15187 else if (die->tag == DW_TAG_inheritance)
15188 {
15189 /* C++ base class field. */
15190 handle_data_member_location (die, cu, fp);
15191 FIELD_BITSIZE (*fp) = 0;
15192 fp->set_type (die_type (die, cu));
15193 FIELD_NAME (*fp) = fp->type ()->name ();
15194 }
15195 else
15196 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15197 }
15198
15199 /* Can the type given by DIE define another type? */
15200
15201 static bool
15202 type_can_define_types (const struct die_info *die)
15203 {
15204 switch (die->tag)
15205 {
15206 case DW_TAG_typedef:
15207 case DW_TAG_class_type:
15208 case DW_TAG_structure_type:
15209 case DW_TAG_union_type:
15210 case DW_TAG_enumeration_type:
15211 return true;
15212
15213 default:
15214 return false;
15215 }
15216 }
15217
15218 /* Add a type definition defined in the scope of the FIP's class. */
15219
15220 static void
15221 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15222 struct dwarf2_cu *cu)
15223 {
15224 struct decl_field fp;
15225 memset (&fp, 0, sizeof (fp));
15226
15227 gdb_assert (type_can_define_types (die));
15228
15229 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15230 fp.name = dwarf2_name (die, cu);
15231 fp.type = read_type_die (die, cu);
15232
15233 /* Save accessibility. */
15234 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15235 switch (accessibility)
15236 {
15237 case DW_ACCESS_public:
15238 /* The assumed value if neither private nor protected. */
15239 break;
15240 case DW_ACCESS_private:
15241 fp.is_private = 1;
15242 break;
15243 case DW_ACCESS_protected:
15244 fp.is_protected = 1;
15245 break;
15246 }
15247
15248 if (die->tag == DW_TAG_typedef)
15249 fip->typedef_field_list.push_back (fp);
15250 else
15251 fip->nested_types_list.push_back (fp);
15252 }
15253
15254 /* A convenience typedef that's used when finding the discriminant
15255 field for a variant part. */
15256 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15257 offset_map_type;
15258
15259 /* Compute the discriminant range for a given variant. OBSTACK is
15260 where the results will be stored. VARIANT is the variant to
15261 process. IS_UNSIGNED indicates whether the discriminant is signed
15262 or unsigned. */
15263
15264 static const gdb::array_view<discriminant_range>
15265 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15266 bool is_unsigned)
15267 {
15268 std::vector<discriminant_range> ranges;
15269
15270 if (variant.default_branch)
15271 return {};
15272
15273 if (variant.discr_list_data == nullptr)
15274 {
15275 discriminant_range r
15276 = {variant.discriminant_value, variant.discriminant_value};
15277 ranges.push_back (r);
15278 }
15279 else
15280 {
15281 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15282 variant.discr_list_data->size);
15283 while (!data.empty ())
15284 {
15285 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15286 {
15287 complaint (_("invalid discriminant marker: %d"), data[0]);
15288 break;
15289 }
15290 bool is_range = data[0] == DW_DSC_range;
15291 data = data.slice (1);
15292
15293 ULONGEST low, high;
15294 unsigned int bytes_read;
15295
15296 if (data.empty ())
15297 {
15298 complaint (_("DW_AT_discr_list missing low value"));
15299 break;
15300 }
15301 if (is_unsigned)
15302 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15303 else
15304 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15305 &bytes_read);
15306 data = data.slice (bytes_read);
15307
15308 if (is_range)
15309 {
15310 if (data.empty ())
15311 {
15312 complaint (_("DW_AT_discr_list missing high value"));
15313 break;
15314 }
15315 if (is_unsigned)
15316 high = read_unsigned_leb128 (nullptr, data.data (),
15317 &bytes_read);
15318 else
15319 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15320 &bytes_read);
15321 data = data.slice (bytes_read);
15322 }
15323 else
15324 high = low;
15325
15326 ranges.push_back ({ low, high });
15327 }
15328 }
15329
15330 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15331 ranges.size ());
15332 std::copy (ranges.begin (), ranges.end (), result);
15333 return gdb::array_view<discriminant_range> (result, ranges.size ());
15334 }
15335
15336 static const gdb::array_view<variant_part> create_variant_parts
15337 (struct obstack *obstack,
15338 const offset_map_type &offset_map,
15339 struct field_info *fi,
15340 const std::vector<variant_part_builder> &variant_parts);
15341
15342 /* Fill in a "struct variant" for a given variant field. RESULT is
15343 the variant to fill in. OBSTACK is where any needed allocations
15344 will be done. OFFSET_MAP holds the mapping from section offsets to
15345 fields for the type. FI describes the fields of the type we're
15346 processing. FIELD is the variant field we're converting. */
15347
15348 static void
15349 create_one_variant (variant &result, struct obstack *obstack,
15350 const offset_map_type &offset_map,
15351 struct field_info *fi, const variant_field &field)
15352 {
15353 result.discriminants = convert_variant_range (obstack, field, false);
15354 result.first_field = field.first_field + fi->baseclasses.size ();
15355 result.last_field = field.last_field + fi->baseclasses.size ();
15356 result.parts = create_variant_parts (obstack, offset_map, fi,
15357 field.variant_parts);
15358 }
15359
15360 /* Fill in a "struct variant_part" for a given variant part. RESULT
15361 is the variant part to fill in. OBSTACK is where any needed
15362 allocations will be done. OFFSET_MAP holds the mapping from
15363 section offsets to fields for the type. FI describes the fields of
15364 the type we're processing. BUILDER is the variant part to be
15365 converted. */
15366
15367 static void
15368 create_one_variant_part (variant_part &result,
15369 struct obstack *obstack,
15370 const offset_map_type &offset_map,
15371 struct field_info *fi,
15372 const variant_part_builder &builder)
15373 {
15374 auto iter = offset_map.find (builder.discriminant_offset);
15375 if (iter == offset_map.end ())
15376 {
15377 result.discriminant_index = -1;
15378 /* Doesn't matter. */
15379 result.is_unsigned = false;
15380 }
15381 else
15382 {
15383 result.discriminant_index = iter->second;
15384 result.is_unsigned
15385 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15386 }
15387
15388 size_t n = builder.variants.size ();
15389 variant *output = new (obstack) variant[n];
15390 for (size_t i = 0; i < n; ++i)
15391 create_one_variant (output[i], obstack, offset_map, fi,
15392 builder.variants[i]);
15393
15394 result.variants = gdb::array_view<variant> (output, n);
15395 }
15396
15397 /* Create a vector of variant parts that can be attached to a type.
15398 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15399 holds the mapping from section offsets to fields for the type. FI
15400 describes the fields of the type we're processing. VARIANT_PARTS
15401 is the vector to convert. */
15402
15403 static const gdb::array_view<variant_part>
15404 create_variant_parts (struct obstack *obstack,
15405 const offset_map_type &offset_map,
15406 struct field_info *fi,
15407 const std::vector<variant_part_builder> &variant_parts)
15408 {
15409 if (variant_parts.empty ())
15410 return {};
15411
15412 size_t n = variant_parts.size ();
15413 variant_part *result = new (obstack) variant_part[n];
15414 for (size_t i = 0; i < n; ++i)
15415 create_one_variant_part (result[i], obstack, offset_map, fi,
15416 variant_parts[i]);
15417
15418 return gdb::array_view<variant_part> (result, n);
15419 }
15420
15421 /* Compute the variant part vector for FIP, attaching it to TYPE when
15422 done. */
15423
15424 static void
15425 add_variant_property (struct field_info *fip, struct type *type,
15426 struct dwarf2_cu *cu)
15427 {
15428 /* Map section offsets of fields to their field index. Note the
15429 field index here does not take the number of baseclasses into
15430 account. */
15431 offset_map_type offset_map;
15432 for (int i = 0; i < fip->fields.size (); ++i)
15433 offset_map[fip->fields[i].offset] = i;
15434
15435 struct objfile *objfile = cu->per_objfile->objfile;
15436 gdb::array_view<variant_part> parts
15437 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15438 fip->variant_parts);
15439
15440 struct dynamic_prop prop;
15441 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15442 obstack_copy (&objfile->objfile_obstack, &parts,
15443 sizeof (parts)));
15444
15445 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15446 }
15447
15448 /* Create the vector of fields, and attach it to the type. */
15449
15450 static void
15451 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15452 struct dwarf2_cu *cu)
15453 {
15454 int nfields = fip->nfields ();
15455
15456 /* Record the field count, allocate space for the array of fields,
15457 and create blank accessibility bitfields if necessary. */
15458 type->set_num_fields (nfields);
15459 type->set_fields
15460 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15461
15462 if (fip->non_public_fields && cu->language != language_ada)
15463 {
15464 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15465
15466 TYPE_FIELD_PRIVATE_BITS (type) =
15467 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15468 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15469
15470 TYPE_FIELD_PROTECTED_BITS (type) =
15471 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15472 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15473
15474 TYPE_FIELD_IGNORE_BITS (type) =
15475 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15476 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15477 }
15478
15479 /* If the type has baseclasses, allocate and clear a bit vector for
15480 TYPE_FIELD_VIRTUAL_BITS. */
15481 if (!fip->baseclasses.empty () && cu->language != language_ada)
15482 {
15483 int num_bytes = B_BYTES (fip->baseclasses.size ());
15484 unsigned char *pointer;
15485
15486 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15487 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15488 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15489 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15490 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15491 }
15492
15493 if (!fip->variant_parts.empty ())
15494 add_variant_property (fip, type, cu);
15495
15496 /* Copy the saved-up fields into the field vector. */
15497 for (int i = 0; i < nfields; ++i)
15498 {
15499 struct nextfield &field
15500 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15501 : fip->fields[i - fip->baseclasses.size ()]);
15502
15503 type->field (i) = field.field;
15504 switch (field.accessibility)
15505 {
15506 case DW_ACCESS_private:
15507 if (cu->language != language_ada)
15508 SET_TYPE_FIELD_PRIVATE (type, i);
15509 break;
15510
15511 case DW_ACCESS_protected:
15512 if (cu->language != language_ada)
15513 SET_TYPE_FIELD_PROTECTED (type, i);
15514 break;
15515
15516 case DW_ACCESS_public:
15517 break;
15518
15519 default:
15520 /* Unknown accessibility. Complain and treat it as public. */
15521 {
15522 complaint (_("unsupported accessibility %d"),
15523 field.accessibility);
15524 }
15525 break;
15526 }
15527 if (i < fip->baseclasses.size ())
15528 {
15529 switch (field.virtuality)
15530 {
15531 case DW_VIRTUALITY_virtual:
15532 case DW_VIRTUALITY_pure_virtual:
15533 if (cu->language == language_ada)
15534 error (_("unexpected virtuality in component of Ada type"));
15535 SET_TYPE_FIELD_VIRTUAL (type, i);
15536 break;
15537 }
15538 }
15539 }
15540 }
15541
15542 /* Return true if this member function is a constructor, false
15543 otherwise. */
15544
15545 static int
15546 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15547 {
15548 const char *fieldname;
15549 const char *type_name;
15550 int len;
15551
15552 if (die->parent == NULL)
15553 return 0;
15554
15555 if (die->parent->tag != DW_TAG_structure_type
15556 && die->parent->tag != DW_TAG_union_type
15557 && die->parent->tag != DW_TAG_class_type)
15558 return 0;
15559
15560 fieldname = dwarf2_name (die, cu);
15561 type_name = dwarf2_name (die->parent, cu);
15562 if (fieldname == NULL || type_name == NULL)
15563 return 0;
15564
15565 len = strlen (fieldname);
15566 return (strncmp (fieldname, type_name, len) == 0
15567 && (type_name[len] == '\0' || type_name[len] == '<'));
15568 }
15569
15570 /* Add a member function to the proper fieldlist. */
15571
15572 static void
15573 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15574 struct type *type, struct dwarf2_cu *cu)
15575 {
15576 struct objfile *objfile = cu->per_objfile->objfile;
15577 struct attribute *attr;
15578 int i;
15579 struct fnfieldlist *flp = nullptr;
15580 struct fn_field *fnp;
15581 const char *fieldname;
15582 struct type *this_type;
15583
15584 if (cu->language == language_ada)
15585 error (_("unexpected member function in Ada type"));
15586
15587 /* Get name of member function. */
15588 fieldname = dwarf2_name (die, cu);
15589 if (fieldname == NULL)
15590 return;
15591
15592 /* Look up member function name in fieldlist. */
15593 for (i = 0; i < fip->fnfieldlists.size (); i++)
15594 {
15595 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15596 {
15597 flp = &fip->fnfieldlists[i];
15598 break;
15599 }
15600 }
15601
15602 /* Create a new fnfieldlist if necessary. */
15603 if (flp == nullptr)
15604 {
15605 fip->fnfieldlists.emplace_back ();
15606 flp = &fip->fnfieldlists.back ();
15607 flp->name = fieldname;
15608 i = fip->fnfieldlists.size () - 1;
15609 }
15610
15611 /* Create a new member function field and add it to the vector of
15612 fnfieldlists. */
15613 flp->fnfields.emplace_back ();
15614 fnp = &flp->fnfields.back ();
15615
15616 /* Delay processing of the physname until later. */
15617 if (cu->language == language_cplus)
15618 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15619 die, cu);
15620 else
15621 {
15622 const char *physname = dwarf2_physname (fieldname, die, cu);
15623 fnp->physname = physname ? physname : "";
15624 }
15625
15626 fnp->type = alloc_type (objfile);
15627 this_type = read_type_die (die, cu);
15628 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15629 {
15630 int nparams = this_type->num_fields ();
15631
15632 /* TYPE is the domain of this method, and THIS_TYPE is the type
15633 of the method itself (TYPE_CODE_METHOD). */
15634 smash_to_method_type (fnp->type, type,
15635 TYPE_TARGET_TYPE (this_type),
15636 this_type->fields (),
15637 this_type->num_fields (),
15638 this_type->has_varargs ());
15639
15640 /* Handle static member functions.
15641 Dwarf2 has no clean way to discern C++ static and non-static
15642 member functions. G++ helps GDB by marking the first
15643 parameter for non-static member functions (which is the this
15644 pointer) as artificial. We obtain this information from
15645 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15646 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15647 fnp->voffset = VOFFSET_STATIC;
15648 }
15649 else
15650 complaint (_("member function type missing for '%s'"),
15651 dwarf2_full_name (fieldname, die, cu));
15652
15653 /* Get fcontext from DW_AT_containing_type if present. */
15654 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15655 fnp->fcontext = die_containing_type (die, cu);
15656
15657 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15658 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15659
15660 /* Get accessibility. */
15661 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15662 switch (accessibility)
15663 {
15664 case DW_ACCESS_private:
15665 fnp->is_private = 1;
15666 break;
15667 case DW_ACCESS_protected:
15668 fnp->is_protected = 1;
15669 break;
15670 }
15671
15672 /* Check for artificial methods. */
15673 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15674 if (attr && attr->as_boolean ())
15675 fnp->is_artificial = 1;
15676
15677 /* Check for defaulted methods. */
15678 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15679 if (attr != nullptr)
15680 fnp->defaulted = attr->defaulted ();
15681
15682 /* Check for deleted methods. */
15683 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15684 if (attr != nullptr && attr->as_boolean ())
15685 fnp->is_deleted = 1;
15686
15687 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15688
15689 /* Get index in virtual function table if it is a virtual member
15690 function. For older versions of GCC, this is an offset in the
15691 appropriate virtual table, as specified by DW_AT_containing_type.
15692 For everyone else, it is an expression to be evaluated relative
15693 to the object address. */
15694
15695 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15696 if (attr != nullptr)
15697 {
15698 if (attr->form_is_block () && attr->as_block ()->size > 0)
15699 {
15700 struct dwarf_block *block = attr->as_block ();
15701
15702 if (block->data[0] == DW_OP_constu)
15703 {
15704 /* Old-style GCC. */
15705 fnp->voffset = decode_locdesc (block, cu) + 2;
15706 }
15707 else if (block->data[0] == DW_OP_deref
15708 || (block->size > 1
15709 && block->data[0] == DW_OP_deref_size
15710 && block->data[1] == cu->header.addr_size))
15711 {
15712 fnp->voffset = decode_locdesc (block, cu);
15713 if ((fnp->voffset % cu->header.addr_size) != 0)
15714 dwarf2_complex_location_expr_complaint ();
15715 else
15716 fnp->voffset /= cu->header.addr_size;
15717 fnp->voffset += 2;
15718 }
15719 else
15720 dwarf2_complex_location_expr_complaint ();
15721
15722 if (!fnp->fcontext)
15723 {
15724 /* If there is no `this' field and no DW_AT_containing_type,
15725 we cannot actually find a base class context for the
15726 vtable! */
15727 if (this_type->num_fields () == 0
15728 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15729 {
15730 complaint (_("cannot determine context for virtual member "
15731 "function \"%s\" (offset %s)"),
15732 fieldname, sect_offset_str (die->sect_off));
15733 }
15734 else
15735 {
15736 fnp->fcontext
15737 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15738 }
15739 }
15740 }
15741 else if (attr->form_is_section_offset ())
15742 {
15743 dwarf2_complex_location_expr_complaint ();
15744 }
15745 else
15746 {
15747 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15748 fieldname);
15749 }
15750 }
15751 else
15752 {
15753 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15754 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15755 {
15756 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15757 complaint (_("Member function \"%s\" (offset %s) is virtual "
15758 "but the vtable offset is not specified"),
15759 fieldname, sect_offset_str (die->sect_off));
15760 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15761 TYPE_CPLUS_DYNAMIC (type) = 1;
15762 }
15763 }
15764 }
15765
15766 /* Create the vector of member function fields, and attach it to the type. */
15767
15768 static void
15769 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15770 struct dwarf2_cu *cu)
15771 {
15772 if (cu->language == language_ada)
15773 error (_("unexpected member functions in Ada type"));
15774
15775 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15776 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15777 TYPE_ALLOC (type,
15778 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15779
15780 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15781 {
15782 struct fnfieldlist &nf = fip->fnfieldlists[i];
15783 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15784
15785 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15786 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15787 fn_flp->fn_fields = (struct fn_field *)
15788 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15789
15790 for (int k = 0; k < nf.fnfields.size (); ++k)
15791 fn_flp->fn_fields[k] = nf.fnfields[k];
15792 }
15793
15794 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15795 }
15796
15797 /* Returns non-zero if NAME is the name of a vtable member in CU's
15798 language, zero otherwise. */
15799 static int
15800 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15801 {
15802 static const char vptr[] = "_vptr";
15803
15804 /* Look for the C++ form of the vtable. */
15805 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15806 return 1;
15807
15808 return 0;
15809 }
15810
15811 /* GCC outputs unnamed structures that are really pointers to member
15812 functions, with the ABI-specified layout. If TYPE describes
15813 such a structure, smash it into a member function type.
15814
15815 GCC shouldn't do this; it should just output pointer to member DIEs.
15816 This is GCC PR debug/28767. */
15817
15818 static void
15819 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15820 {
15821 struct type *pfn_type, *self_type, *new_type;
15822
15823 /* Check for a structure with no name and two children. */
15824 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15825 return;
15826
15827 /* Check for __pfn and __delta members. */
15828 if (TYPE_FIELD_NAME (type, 0) == NULL
15829 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15830 || TYPE_FIELD_NAME (type, 1) == NULL
15831 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15832 return;
15833
15834 /* Find the type of the method. */
15835 pfn_type = type->field (0).type ();
15836 if (pfn_type == NULL
15837 || pfn_type->code () != TYPE_CODE_PTR
15838 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15839 return;
15840
15841 /* Look for the "this" argument. */
15842 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15843 if (pfn_type->num_fields () == 0
15844 /* || pfn_type->field (0).type () == NULL */
15845 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15846 return;
15847
15848 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15849 new_type = alloc_type (objfile);
15850 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15851 pfn_type->fields (), pfn_type->num_fields (),
15852 pfn_type->has_varargs ());
15853 smash_to_methodptr_type (type, new_type);
15854 }
15855
15856 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15857 requires rewriting, then copy it and return the updated copy.
15858 Otherwise return nullptr. */
15859
15860 static struct type *
15861 rewrite_array_type (struct type *type)
15862 {
15863 if (type->code () != TYPE_CODE_ARRAY)
15864 return nullptr;
15865
15866 struct type *index_type = type->index_type ();
15867 range_bounds *current_bounds = index_type->bounds ();
15868
15869 /* Handle multi-dimensional arrays. */
15870 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15871 if (new_target == nullptr)
15872 {
15873 /* Maybe we don't need to rewrite this array. */
15874 if (current_bounds->low.kind () == PROP_CONST
15875 && current_bounds->high.kind () == PROP_CONST)
15876 return nullptr;
15877 }
15878
15879 /* Either the target type was rewritten, or the bounds have to be
15880 updated. Either way we want to copy the type and update
15881 everything. */
15882 struct type *copy = copy_type (type);
15883 int nfields = copy->num_fields ();
15884 field *new_fields
15885 = ((struct field *) TYPE_ZALLOC (copy,
15886 nfields * sizeof (struct field)));
15887 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15888 copy->set_fields (new_fields);
15889 if (new_target != nullptr)
15890 TYPE_TARGET_TYPE (copy) = new_target;
15891
15892 struct type *index_copy = copy_type (index_type);
15893 range_bounds *bounds
15894 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15895 sizeof (range_bounds));
15896 *bounds = *current_bounds;
15897 bounds->low.set_const_val (1);
15898 bounds->high.set_const_val (0);
15899 index_copy->set_bounds (bounds);
15900 copy->set_index_type (index_copy);
15901
15902 return copy;
15903 }
15904
15905 /* While some versions of GCC will generate complicated DWARF for an
15906 array (see quirk_ada_thick_pointer), more recent versions were
15907 modified to emit an explicit thick pointer structure. However, in
15908 this case, the array still has DWARF expressions for its ranges,
15909 and these must be ignored. */
15910
15911 static void
15912 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15913 struct type *type)
15914 {
15915 gdb_assert (cu->language == language_ada);
15916
15917 /* Check for a structure with two children. */
15918 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15919 return;
15920
15921 /* Check for P_ARRAY and P_BOUNDS members. */
15922 if (TYPE_FIELD_NAME (type, 0) == NULL
15923 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15924 || TYPE_FIELD_NAME (type, 1) == NULL
15925 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15926 return;
15927
15928 /* Make sure we're looking at a pointer to an array. */
15929 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15930 return;
15931
15932 /* The Ada code already knows how to handle these types, so all that
15933 we need to do is turn the bounds into static bounds. However, we
15934 don't want to rewrite existing array or index types in-place,
15935 because those may be referenced in other contexts where this
15936 rewriting is undesirable. */
15937 struct type *new_ary_type
15938 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15939 if (new_ary_type != nullptr)
15940 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15941 }
15942
15943 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15944 appropriate error checking and issuing complaints if there is a
15945 problem. */
15946
15947 static ULONGEST
15948 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15949 {
15950 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15951
15952 if (attr == nullptr)
15953 return 0;
15954
15955 if (!attr->form_is_constant ())
15956 {
15957 complaint (_("DW_AT_alignment must have constant form"
15958 " - DIE at %s [in module %s]"),
15959 sect_offset_str (die->sect_off),
15960 objfile_name (cu->per_objfile->objfile));
15961 return 0;
15962 }
15963
15964 LONGEST val = attr->constant_value (0);
15965 if (val < 0)
15966 {
15967 complaint (_("DW_AT_alignment value must not be negative"
15968 " - DIE at %s [in module %s]"),
15969 sect_offset_str (die->sect_off),
15970 objfile_name (cu->per_objfile->objfile));
15971 return 0;
15972 }
15973 ULONGEST align = val;
15974
15975 if (align == 0)
15976 {
15977 complaint (_("DW_AT_alignment value must not be zero"
15978 " - DIE at %s [in module %s]"),
15979 sect_offset_str (die->sect_off),
15980 objfile_name (cu->per_objfile->objfile));
15981 return 0;
15982 }
15983 if ((align & (align - 1)) != 0)
15984 {
15985 complaint (_("DW_AT_alignment value must be a power of 2"
15986 " - DIE at %s [in module %s]"),
15987 sect_offset_str (die->sect_off),
15988 objfile_name (cu->per_objfile->objfile));
15989 return 0;
15990 }
15991
15992 return align;
15993 }
15994
15995 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15996 the alignment for TYPE. */
15997
15998 static void
15999 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16000 struct type *type)
16001 {
16002 if (!set_type_align (type, get_alignment (cu, die)))
16003 complaint (_("DW_AT_alignment value too large"
16004 " - DIE at %s [in module %s]"),
16005 sect_offset_str (die->sect_off),
16006 objfile_name (cu->per_objfile->objfile));
16007 }
16008
16009 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16010 constant for a type, according to DWARF5 spec, Table 5.5. */
16011
16012 static bool
16013 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16014 {
16015 switch (value)
16016 {
16017 case DW_CC_normal:
16018 case DW_CC_pass_by_reference:
16019 case DW_CC_pass_by_value:
16020 return true;
16021
16022 default:
16023 complaint (_("unrecognized DW_AT_calling_convention value "
16024 "(%s) for a type"), pulongest (value));
16025 return false;
16026 }
16027 }
16028
16029 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16030 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16031 also according to GNU-specific values (see include/dwarf2.h). */
16032
16033 static bool
16034 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16035 {
16036 switch (value)
16037 {
16038 case DW_CC_normal:
16039 case DW_CC_program:
16040 case DW_CC_nocall:
16041 return true;
16042
16043 case DW_CC_GNU_renesas_sh:
16044 case DW_CC_GNU_borland_fastcall_i386:
16045 case DW_CC_GDB_IBM_OpenCL:
16046 return true;
16047
16048 default:
16049 complaint (_("unrecognized DW_AT_calling_convention value "
16050 "(%s) for a subroutine"), pulongest (value));
16051 return false;
16052 }
16053 }
16054
16055 /* Called when we find the DIE that starts a structure or union scope
16056 (definition) to create a type for the structure or union. Fill in
16057 the type's name and general properties; the members will not be
16058 processed until process_structure_scope. A symbol table entry for
16059 the type will also not be done until process_structure_scope (assuming
16060 the type has a name).
16061
16062 NOTE: we need to call these functions regardless of whether or not the
16063 DIE has a DW_AT_name attribute, since it might be an anonymous
16064 structure or union. This gets the type entered into our set of
16065 user defined types. */
16066
16067 static struct type *
16068 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16069 {
16070 struct objfile *objfile = cu->per_objfile->objfile;
16071 struct type *type;
16072 struct attribute *attr;
16073 const char *name;
16074
16075 /* If the definition of this type lives in .debug_types, read that type.
16076 Don't follow DW_AT_specification though, that will take us back up
16077 the chain and we want to go down. */
16078 attr = die->attr (DW_AT_signature);
16079 if (attr != nullptr)
16080 {
16081 type = get_DW_AT_signature_type (die, attr, cu);
16082
16083 /* The type's CU may not be the same as CU.
16084 Ensure TYPE is recorded with CU in die_type_hash. */
16085 return set_die_type (die, type, cu);
16086 }
16087
16088 type = alloc_type (objfile);
16089 INIT_CPLUS_SPECIFIC (type);
16090
16091 name = dwarf2_name (die, cu);
16092 if (name != NULL)
16093 {
16094 if (cu->language == language_cplus
16095 || cu->language == language_d
16096 || cu->language == language_rust)
16097 {
16098 const char *full_name = dwarf2_full_name (name, die, cu);
16099
16100 /* dwarf2_full_name might have already finished building the DIE's
16101 type. If so, there is no need to continue. */
16102 if (get_die_type (die, cu) != NULL)
16103 return get_die_type (die, cu);
16104
16105 type->set_name (full_name);
16106 }
16107 else
16108 {
16109 /* The name is already allocated along with this objfile, so
16110 we don't need to duplicate it for the type. */
16111 type->set_name (name);
16112 }
16113 }
16114
16115 if (die->tag == DW_TAG_structure_type)
16116 {
16117 type->set_code (TYPE_CODE_STRUCT);
16118 }
16119 else if (die->tag == DW_TAG_union_type)
16120 {
16121 type->set_code (TYPE_CODE_UNION);
16122 }
16123 else
16124 {
16125 type->set_code (TYPE_CODE_STRUCT);
16126 }
16127
16128 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16129 TYPE_DECLARED_CLASS (type) = 1;
16130
16131 /* Store the calling convention in the type if it's available in
16132 the die. Otherwise the calling convention remains set to
16133 the default value DW_CC_normal. */
16134 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16135 if (attr != nullptr
16136 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16137 {
16138 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16139 TYPE_CPLUS_CALLING_CONVENTION (type)
16140 = (enum dwarf_calling_convention) (attr->constant_value (0));
16141 }
16142
16143 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16144 if (attr != nullptr)
16145 {
16146 if (attr->form_is_constant ())
16147 TYPE_LENGTH (type) = attr->constant_value (0);
16148 else
16149 {
16150 struct dynamic_prop prop;
16151 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16152 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16153 TYPE_LENGTH (type) = 0;
16154 }
16155 }
16156 else
16157 {
16158 TYPE_LENGTH (type) = 0;
16159 }
16160
16161 maybe_set_alignment (cu, die, type);
16162
16163 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16164 {
16165 /* ICC<14 does not output the required DW_AT_declaration on
16166 incomplete types, but gives them a size of zero. */
16167 type->set_is_stub (true);
16168 }
16169 else
16170 type->set_stub_is_supported (true);
16171
16172 if (die_is_declaration (die, cu))
16173 type->set_is_stub (true);
16174 else if (attr == NULL && die->child == NULL
16175 && producer_is_realview (cu->producer))
16176 /* RealView does not output the required DW_AT_declaration
16177 on incomplete types. */
16178 type->set_is_stub (true);
16179
16180 /* We need to add the type field to the die immediately so we don't
16181 infinitely recurse when dealing with pointers to the structure
16182 type within the structure itself. */
16183 set_die_type (die, type, cu);
16184
16185 /* set_die_type should be already done. */
16186 set_descriptive_type (type, die, cu);
16187
16188 return type;
16189 }
16190
16191 static void handle_struct_member_die
16192 (struct die_info *child_die,
16193 struct type *type,
16194 struct field_info *fi,
16195 std::vector<struct symbol *> *template_args,
16196 struct dwarf2_cu *cu);
16197
16198 /* A helper for handle_struct_member_die that handles
16199 DW_TAG_variant_part. */
16200
16201 static void
16202 handle_variant_part (struct die_info *die, struct type *type,
16203 struct field_info *fi,
16204 std::vector<struct symbol *> *template_args,
16205 struct dwarf2_cu *cu)
16206 {
16207 variant_part_builder *new_part;
16208 if (fi->current_variant_part == nullptr)
16209 {
16210 fi->variant_parts.emplace_back ();
16211 new_part = &fi->variant_parts.back ();
16212 }
16213 else if (!fi->current_variant_part->processing_variant)
16214 {
16215 complaint (_("nested DW_TAG_variant_part seen "
16216 "- DIE at %s [in module %s]"),
16217 sect_offset_str (die->sect_off),
16218 objfile_name (cu->per_objfile->objfile));
16219 return;
16220 }
16221 else
16222 {
16223 variant_field &current = fi->current_variant_part->variants.back ();
16224 current.variant_parts.emplace_back ();
16225 new_part = &current.variant_parts.back ();
16226 }
16227
16228 /* When we recurse, we want callees to add to this new variant
16229 part. */
16230 scoped_restore save_current_variant_part
16231 = make_scoped_restore (&fi->current_variant_part, new_part);
16232
16233 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16234 if (discr == NULL)
16235 {
16236 /* It's a univariant form, an extension we support. */
16237 }
16238 else if (discr->form_is_ref ())
16239 {
16240 struct dwarf2_cu *target_cu = cu;
16241 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16242
16243 new_part->discriminant_offset = target_die->sect_off;
16244 }
16245 else
16246 {
16247 complaint (_("DW_AT_discr does not have DIE reference form"
16248 " - DIE at %s [in module %s]"),
16249 sect_offset_str (die->sect_off),
16250 objfile_name (cu->per_objfile->objfile));
16251 }
16252
16253 for (die_info *child_die = die->child;
16254 child_die != NULL;
16255 child_die = child_die->sibling)
16256 handle_struct_member_die (child_die, type, fi, template_args, cu);
16257 }
16258
16259 /* A helper for handle_struct_member_die that handles
16260 DW_TAG_variant. */
16261
16262 static void
16263 handle_variant (struct die_info *die, struct type *type,
16264 struct field_info *fi,
16265 std::vector<struct symbol *> *template_args,
16266 struct dwarf2_cu *cu)
16267 {
16268 if (fi->current_variant_part == nullptr)
16269 {
16270 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16271 "- DIE at %s [in module %s]"),
16272 sect_offset_str (die->sect_off),
16273 objfile_name (cu->per_objfile->objfile));
16274 return;
16275 }
16276 if (fi->current_variant_part->processing_variant)
16277 {
16278 complaint (_("nested DW_TAG_variant seen "
16279 "- DIE at %s [in module %s]"),
16280 sect_offset_str (die->sect_off),
16281 objfile_name (cu->per_objfile->objfile));
16282 return;
16283 }
16284
16285 scoped_restore save_processing_variant
16286 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16287 true);
16288
16289 fi->current_variant_part->variants.emplace_back ();
16290 variant_field &variant = fi->current_variant_part->variants.back ();
16291 variant.first_field = fi->fields.size ();
16292
16293 /* In a variant we want to get the discriminant and also add a
16294 field for our sole member child. */
16295 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16296 if (discr == nullptr || !discr->form_is_constant ())
16297 {
16298 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16299 if (discr == nullptr || discr->as_block ()->size == 0)
16300 variant.default_branch = true;
16301 else
16302 variant.discr_list_data = discr->as_block ();
16303 }
16304 else
16305 variant.discriminant_value = discr->constant_value (0);
16306
16307 for (die_info *variant_child = die->child;
16308 variant_child != NULL;
16309 variant_child = variant_child->sibling)
16310 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16311
16312 variant.last_field = fi->fields.size ();
16313 }
16314
16315 /* A helper for process_structure_scope that handles a single member
16316 DIE. */
16317
16318 static void
16319 handle_struct_member_die (struct die_info *child_die, struct type *type,
16320 struct field_info *fi,
16321 std::vector<struct symbol *> *template_args,
16322 struct dwarf2_cu *cu)
16323 {
16324 if (child_die->tag == DW_TAG_member
16325 || child_die->tag == DW_TAG_variable)
16326 {
16327 /* NOTE: carlton/2002-11-05: A C++ static data member
16328 should be a DW_TAG_member that is a declaration, but
16329 all versions of G++ as of this writing (so through at
16330 least 3.2.1) incorrectly generate DW_TAG_variable
16331 tags for them instead. */
16332 dwarf2_add_field (fi, child_die, cu);
16333 }
16334 else if (child_die->tag == DW_TAG_subprogram)
16335 {
16336 /* Rust doesn't have member functions in the C++ sense.
16337 However, it does emit ordinary functions as children
16338 of a struct DIE. */
16339 if (cu->language == language_rust)
16340 read_func_scope (child_die, cu);
16341 else
16342 {
16343 /* C++ member function. */
16344 dwarf2_add_member_fn (fi, child_die, type, cu);
16345 }
16346 }
16347 else if (child_die->tag == DW_TAG_inheritance)
16348 {
16349 /* C++ base class field. */
16350 dwarf2_add_field (fi, child_die, cu);
16351 }
16352 else if (type_can_define_types (child_die))
16353 dwarf2_add_type_defn (fi, child_die, cu);
16354 else if (child_die->tag == DW_TAG_template_type_param
16355 || child_die->tag == DW_TAG_template_value_param)
16356 {
16357 struct symbol *arg = new_symbol (child_die, NULL, cu);
16358
16359 if (arg != NULL)
16360 template_args->push_back (arg);
16361 }
16362 else if (child_die->tag == DW_TAG_variant_part)
16363 handle_variant_part (child_die, type, fi, template_args, cu);
16364 else if (child_die->tag == DW_TAG_variant)
16365 handle_variant (child_die, type, fi, template_args, cu);
16366 }
16367
16368 /* Finish creating a structure or union type, including filling in
16369 its members and creating a symbol for it. */
16370
16371 static void
16372 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16373 {
16374 struct objfile *objfile = cu->per_objfile->objfile;
16375 struct die_info *child_die;
16376 struct type *type;
16377
16378 type = get_die_type (die, cu);
16379 if (type == NULL)
16380 type = read_structure_type (die, cu);
16381
16382 bool has_template_parameters = false;
16383 if (die->child != NULL && ! die_is_declaration (die, cu))
16384 {
16385 struct field_info fi;
16386 std::vector<struct symbol *> template_args;
16387
16388 child_die = die->child;
16389
16390 while (child_die && child_die->tag)
16391 {
16392 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16393 child_die = child_die->sibling;
16394 }
16395
16396 /* Attach template arguments to type. */
16397 if (!template_args.empty ())
16398 {
16399 has_template_parameters = true;
16400 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16401 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16402 TYPE_TEMPLATE_ARGUMENTS (type)
16403 = XOBNEWVEC (&objfile->objfile_obstack,
16404 struct symbol *,
16405 TYPE_N_TEMPLATE_ARGUMENTS (type));
16406 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16407 template_args.data (),
16408 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16409 * sizeof (struct symbol *)));
16410 }
16411
16412 /* Attach fields and member functions to the type. */
16413 if (fi.nfields () > 0)
16414 dwarf2_attach_fields_to_type (&fi, type, cu);
16415 if (!fi.fnfieldlists.empty ())
16416 {
16417 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16418
16419 /* Get the type which refers to the base class (possibly this
16420 class itself) which contains the vtable pointer for the current
16421 class from the DW_AT_containing_type attribute. This use of
16422 DW_AT_containing_type is a GNU extension. */
16423
16424 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16425 {
16426 struct type *t = die_containing_type (die, cu);
16427
16428 set_type_vptr_basetype (type, t);
16429 if (type == t)
16430 {
16431 int i;
16432
16433 /* Our own class provides vtbl ptr. */
16434 for (i = t->num_fields () - 1;
16435 i >= TYPE_N_BASECLASSES (t);
16436 --i)
16437 {
16438 const char *fieldname = TYPE_FIELD_NAME (t, i);
16439
16440 if (is_vtable_name (fieldname, cu))
16441 {
16442 set_type_vptr_fieldno (type, i);
16443 break;
16444 }
16445 }
16446
16447 /* Complain if virtual function table field not found. */
16448 if (i < TYPE_N_BASECLASSES (t))
16449 complaint (_("virtual function table pointer "
16450 "not found when defining class '%s'"),
16451 type->name () ? type->name () : "");
16452 }
16453 else
16454 {
16455 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16456 }
16457 }
16458 else if (cu->producer
16459 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16460 {
16461 /* The IBM XLC compiler does not provide direct indication
16462 of the containing type, but the vtable pointer is
16463 always named __vfp. */
16464
16465 int i;
16466
16467 for (i = type->num_fields () - 1;
16468 i >= TYPE_N_BASECLASSES (type);
16469 --i)
16470 {
16471 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16472 {
16473 set_type_vptr_fieldno (type, i);
16474 set_type_vptr_basetype (type, type);
16475 break;
16476 }
16477 }
16478 }
16479 }
16480
16481 /* Copy fi.typedef_field_list linked list elements content into the
16482 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16483 if (!fi.typedef_field_list.empty ())
16484 {
16485 int count = fi.typedef_field_list.size ();
16486
16487 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16488 TYPE_TYPEDEF_FIELD_ARRAY (type)
16489 = ((struct decl_field *)
16490 TYPE_ALLOC (type,
16491 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16492 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16493
16494 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16495 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16496 }
16497
16498 /* Copy fi.nested_types_list linked list elements content into the
16499 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16500 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16501 {
16502 int count = fi.nested_types_list.size ();
16503
16504 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16505 TYPE_NESTED_TYPES_ARRAY (type)
16506 = ((struct decl_field *)
16507 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16508 TYPE_NESTED_TYPES_COUNT (type) = count;
16509
16510 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16511 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16512 }
16513 }
16514
16515 quirk_gcc_member_function_pointer (type, objfile);
16516 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16517 cu->rust_unions.push_back (type);
16518 else if (cu->language == language_ada)
16519 quirk_ada_thick_pointer_struct (die, cu, type);
16520
16521 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16522 snapshots) has been known to create a die giving a declaration
16523 for a class that has, as a child, a die giving a definition for a
16524 nested class. So we have to process our children even if the
16525 current die is a declaration. Normally, of course, a declaration
16526 won't have any children at all. */
16527
16528 child_die = die->child;
16529
16530 while (child_die != NULL && child_die->tag)
16531 {
16532 if (child_die->tag == DW_TAG_member
16533 || child_die->tag == DW_TAG_variable
16534 || child_die->tag == DW_TAG_inheritance
16535 || child_die->tag == DW_TAG_template_value_param
16536 || child_die->tag == DW_TAG_template_type_param)
16537 {
16538 /* Do nothing. */
16539 }
16540 else
16541 process_die (child_die, cu);
16542
16543 child_die = child_die->sibling;
16544 }
16545
16546 /* Do not consider external references. According to the DWARF standard,
16547 these DIEs are identified by the fact that they have no byte_size
16548 attribute, and a declaration attribute. */
16549 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16550 || !die_is_declaration (die, cu)
16551 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16552 {
16553 struct symbol *sym = new_symbol (die, type, cu);
16554
16555 if (has_template_parameters)
16556 {
16557 struct symtab *symtab;
16558 if (sym != nullptr)
16559 symtab = symbol_symtab (sym);
16560 else if (cu->line_header != nullptr)
16561 {
16562 /* Any related symtab will do. */
16563 symtab
16564 = cu->line_header->file_names ()[0].symtab;
16565 }
16566 else
16567 {
16568 symtab = nullptr;
16569 complaint (_("could not find suitable "
16570 "symtab for template parameter"
16571 " - DIE at %s [in module %s]"),
16572 sect_offset_str (die->sect_off),
16573 objfile_name (objfile));
16574 }
16575
16576 if (symtab != nullptr)
16577 {
16578 /* Make sure that the symtab is set on the new symbols.
16579 Even though they don't appear in this symtab directly,
16580 other parts of gdb assume that symbols do, and this is
16581 reasonably true. */
16582 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16583 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16584 }
16585 }
16586 }
16587 }
16588
16589 /* Assuming DIE is an enumeration type, and TYPE is its associated
16590 type, update TYPE using some information only available in DIE's
16591 children. In particular, the fields are computed. */
16592
16593 static void
16594 update_enumeration_type_from_children (struct die_info *die,
16595 struct type *type,
16596 struct dwarf2_cu *cu)
16597 {
16598 struct die_info *child_die;
16599 int unsigned_enum = 1;
16600 int flag_enum = 1;
16601
16602 auto_obstack obstack;
16603 std::vector<struct field> fields;
16604
16605 for (child_die = die->child;
16606 child_die != NULL && child_die->tag;
16607 child_die = child_die->sibling)
16608 {
16609 struct attribute *attr;
16610 LONGEST value;
16611 const gdb_byte *bytes;
16612 struct dwarf2_locexpr_baton *baton;
16613 const char *name;
16614
16615 if (child_die->tag != DW_TAG_enumerator)
16616 continue;
16617
16618 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16619 if (attr == NULL)
16620 continue;
16621
16622 name = dwarf2_name (child_die, cu);
16623 if (name == NULL)
16624 name = "<anonymous enumerator>";
16625
16626 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16627 &value, &bytes, &baton);
16628 if (value < 0)
16629 {
16630 unsigned_enum = 0;
16631 flag_enum = 0;
16632 }
16633 else
16634 {
16635 if (count_one_bits_ll (value) >= 2)
16636 flag_enum = 0;
16637 }
16638
16639 fields.emplace_back ();
16640 struct field &field = fields.back ();
16641 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16642 SET_FIELD_ENUMVAL (field, value);
16643 }
16644
16645 if (!fields.empty ())
16646 {
16647 type->set_num_fields (fields.size ());
16648 type->set_fields
16649 ((struct field *)
16650 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16651 memcpy (type->fields (), fields.data (),
16652 sizeof (struct field) * fields.size ());
16653 }
16654
16655 if (unsigned_enum)
16656 type->set_is_unsigned (true);
16657
16658 if (flag_enum)
16659 TYPE_FLAG_ENUM (type) = 1;
16660 }
16661
16662 /* Given a DW_AT_enumeration_type die, set its type. We do not
16663 complete the type's fields yet, or create any symbols. */
16664
16665 static struct type *
16666 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16667 {
16668 struct objfile *objfile = cu->per_objfile->objfile;
16669 struct type *type;
16670 struct attribute *attr;
16671 const char *name;
16672
16673 /* If the definition of this type lives in .debug_types, read that type.
16674 Don't follow DW_AT_specification though, that will take us back up
16675 the chain and we want to go down. */
16676 attr = die->attr (DW_AT_signature);
16677 if (attr != nullptr)
16678 {
16679 type = get_DW_AT_signature_type (die, attr, cu);
16680
16681 /* The type's CU may not be the same as CU.
16682 Ensure TYPE is recorded with CU in die_type_hash. */
16683 return set_die_type (die, type, cu);
16684 }
16685
16686 type = alloc_type (objfile);
16687
16688 type->set_code (TYPE_CODE_ENUM);
16689 name = dwarf2_full_name (NULL, die, cu);
16690 if (name != NULL)
16691 type->set_name (name);
16692
16693 attr = dwarf2_attr (die, DW_AT_type, cu);
16694 if (attr != NULL)
16695 {
16696 struct type *underlying_type = die_type (die, cu);
16697
16698 TYPE_TARGET_TYPE (type) = underlying_type;
16699 }
16700
16701 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16702 if (attr != nullptr)
16703 {
16704 TYPE_LENGTH (type) = attr->constant_value (0);
16705 }
16706 else
16707 {
16708 TYPE_LENGTH (type) = 0;
16709 }
16710
16711 maybe_set_alignment (cu, die, type);
16712
16713 /* The enumeration DIE can be incomplete. In Ada, any type can be
16714 declared as private in the package spec, and then defined only
16715 inside the package body. Such types are known as Taft Amendment
16716 Types. When another package uses such a type, an incomplete DIE
16717 may be generated by the compiler. */
16718 if (die_is_declaration (die, cu))
16719 type->set_is_stub (true);
16720
16721 /* If this type has an underlying type that is not a stub, then we
16722 may use its attributes. We always use the "unsigned" attribute
16723 in this situation, because ordinarily we guess whether the type
16724 is unsigned -- but the guess can be wrong and the underlying type
16725 can tell us the reality. However, we defer to a local size
16726 attribute if one exists, because this lets the compiler override
16727 the underlying type if needed. */
16728 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16729 {
16730 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16731 underlying_type = check_typedef (underlying_type);
16732
16733 type->set_is_unsigned (underlying_type->is_unsigned ());
16734
16735 if (TYPE_LENGTH (type) == 0)
16736 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16737
16738 if (TYPE_RAW_ALIGN (type) == 0
16739 && TYPE_RAW_ALIGN (underlying_type) != 0)
16740 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16741 }
16742
16743 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16744
16745 set_die_type (die, type, cu);
16746
16747 /* Finish the creation of this type by using the enum's children.
16748 Note that, as usual, this must come after set_die_type to avoid
16749 infinite recursion when trying to compute the names of the
16750 enumerators. */
16751 update_enumeration_type_from_children (die, type, cu);
16752
16753 return type;
16754 }
16755
16756 /* Given a pointer to a die which begins an enumeration, process all
16757 the dies that define the members of the enumeration, and create the
16758 symbol for the enumeration type.
16759
16760 NOTE: We reverse the order of the element list. */
16761
16762 static void
16763 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16764 {
16765 struct type *this_type;
16766
16767 this_type = get_die_type (die, cu);
16768 if (this_type == NULL)
16769 this_type = read_enumeration_type (die, cu);
16770
16771 if (die->child != NULL)
16772 {
16773 struct die_info *child_die;
16774 const char *name;
16775
16776 child_die = die->child;
16777 while (child_die && child_die->tag)
16778 {
16779 if (child_die->tag != DW_TAG_enumerator)
16780 {
16781 process_die (child_die, cu);
16782 }
16783 else
16784 {
16785 name = dwarf2_name (child_die, cu);
16786 if (name)
16787 new_symbol (child_die, this_type, cu);
16788 }
16789
16790 child_die = child_die->sibling;
16791 }
16792 }
16793
16794 /* If we are reading an enum from a .debug_types unit, and the enum
16795 is a declaration, and the enum is not the signatured type in the
16796 unit, then we do not want to add a symbol for it. Adding a
16797 symbol would in some cases obscure the true definition of the
16798 enum, giving users an incomplete type when the definition is
16799 actually available. Note that we do not want to do this for all
16800 enums which are just declarations, because C++0x allows forward
16801 enum declarations. */
16802 if (cu->per_cu->is_debug_types
16803 && die_is_declaration (die, cu))
16804 {
16805 struct signatured_type *sig_type;
16806
16807 sig_type = (struct signatured_type *) cu->per_cu;
16808 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16809 if (sig_type->type_offset_in_section != die->sect_off)
16810 return;
16811 }
16812
16813 new_symbol (die, this_type, cu);
16814 }
16815
16816 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16817 expression for an index type and finds the corresponding field
16818 offset in the hidden "P_BOUNDS" structure. Returns true on success
16819 and updates *FIELD, false if it fails to recognize an
16820 expression. */
16821
16822 static bool
16823 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16824 int *bounds_offset, struct field *field,
16825 struct dwarf2_cu *cu)
16826 {
16827 struct attribute *attr = dwarf2_attr (die, name, cu);
16828 if (attr == nullptr || !attr->form_is_block ())
16829 return false;
16830
16831 const struct dwarf_block *block = attr->as_block ();
16832 const gdb_byte *start = block->data;
16833 const gdb_byte *end = block->data + block->size;
16834
16835 /* The expression to recognize generally looks like:
16836
16837 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16838 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16839
16840 However, the second "plus_uconst" may be missing:
16841
16842 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16843 DW_OP_deref_size: 4)
16844
16845 This happens when the field is at the start of the structure.
16846
16847 Also, the final deref may not be sized:
16848
16849 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16850 DW_OP_deref)
16851
16852 This happens when the size of the index type happens to be the
16853 same as the architecture's word size. This can occur with or
16854 without the second plus_uconst. */
16855
16856 if (end - start < 2)
16857 return false;
16858 if (*start++ != DW_OP_push_object_address)
16859 return false;
16860 if (*start++ != DW_OP_plus_uconst)
16861 return false;
16862
16863 uint64_t this_bound_off;
16864 start = gdb_read_uleb128 (start, end, &this_bound_off);
16865 if (start == nullptr || (int) this_bound_off != this_bound_off)
16866 return false;
16867 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16868 is consistent among all bounds. */
16869 if (*bounds_offset == -1)
16870 *bounds_offset = this_bound_off;
16871 else if (*bounds_offset != this_bound_off)
16872 return false;
16873
16874 if (start == end || *start++ != DW_OP_deref)
16875 return false;
16876
16877 int offset = 0;
16878 if (start ==end)
16879 return false;
16880 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16881 {
16882 /* This means an offset of 0. */
16883 }
16884 else if (*start++ != DW_OP_plus_uconst)
16885 return false;
16886 else
16887 {
16888 /* The size is the parameter to DW_OP_plus_uconst. */
16889 uint64_t val;
16890 start = gdb_read_uleb128 (start, end, &val);
16891 if (start == nullptr)
16892 return false;
16893 if ((int) val != val)
16894 return false;
16895 offset = val;
16896 }
16897
16898 if (start == end)
16899 return false;
16900
16901 uint64_t size;
16902 if (*start == DW_OP_deref_size)
16903 {
16904 start = gdb_read_uleb128 (start + 1, end, &size);
16905 if (start == nullptr)
16906 return false;
16907 }
16908 else if (*start == DW_OP_deref)
16909 {
16910 size = cu->header.addr_size;
16911 ++start;
16912 }
16913 else
16914 return false;
16915
16916 SET_FIELD_BITPOS (*field, 8 * offset);
16917 if (size != TYPE_LENGTH (field->type ()))
16918 FIELD_BITSIZE (*field) = 8 * size;
16919
16920 return true;
16921 }
16922
16923 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16924 some kinds of Ada arrays:
16925
16926 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16927 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16928 <11e0> DW_AT_data_location: 2 byte block: 97 6
16929 (DW_OP_push_object_address; DW_OP_deref)
16930 <11e3> DW_AT_type : <0x1173>
16931 <11e7> DW_AT_sibling : <0x1201>
16932 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16933 <11ec> DW_AT_type : <0x1206>
16934 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16935 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16936 DW_OP_deref_size: 4)
16937 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16938 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16939 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16940
16941 This actually represents a "thick pointer", which is a structure
16942 with two elements: one that is a pointer to the array data, and one
16943 that is a pointer to another structure; this second structure holds
16944 the array bounds.
16945
16946 This returns a new type on success, or nullptr if this didn't
16947 recognize the type. */
16948
16949 static struct type *
16950 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16951 struct type *type)
16952 {
16953 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16954 /* So far we've only seen this with block form. */
16955 if (attr == nullptr || !attr->form_is_block ())
16956 return nullptr;
16957
16958 /* Note that this will fail if the structure layout is changed by
16959 the compiler. However, we have no good way to recognize some
16960 other layout, because we don't know what expression the compiler
16961 might choose to emit should this happen. */
16962 struct dwarf_block *blk = attr->as_block ();
16963 if (blk->size != 2
16964 || blk->data[0] != DW_OP_push_object_address
16965 || blk->data[1] != DW_OP_deref)
16966 return nullptr;
16967
16968 int bounds_offset = -1;
16969 int max_align = -1;
16970 std::vector<struct field> range_fields;
16971 for (struct die_info *child_die = die->child;
16972 child_die;
16973 child_die = child_die->sibling)
16974 {
16975 if (child_die->tag == DW_TAG_subrange_type)
16976 {
16977 struct type *underlying = read_subrange_index_type (child_die, cu);
16978
16979 int this_align = type_align (underlying);
16980 if (this_align > max_align)
16981 max_align = this_align;
16982
16983 range_fields.emplace_back ();
16984 range_fields.emplace_back ();
16985
16986 struct field &lower = range_fields[range_fields.size () - 2];
16987 struct field &upper = range_fields[range_fields.size () - 1];
16988
16989 lower.set_type (underlying);
16990 FIELD_ARTIFICIAL (lower) = 1;
16991
16992 upper.set_type (underlying);
16993 FIELD_ARTIFICIAL (upper) = 1;
16994
16995 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16996 &bounds_offset, &lower, cu)
16997 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16998 &bounds_offset, &upper, cu))
16999 return nullptr;
17000 }
17001 }
17002
17003 /* This shouldn't really happen, but double-check that we found
17004 where the bounds are stored. */
17005 if (bounds_offset == -1)
17006 return nullptr;
17007
17008 struct objfile *objfile = cu->per_objfile->objfile;
17009 for (int i = 0; i < range_fields.size (); i += 2)
17010 {
17011 char name[20];
17012
17013 /* Set the name of each field in the bounds. */
17014 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17015 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17016 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17017 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17018 }
17019
17020 struct type *bounds = alloc_type (objfile);
17021 bounds->set_code (TYPE_CODE_STRUCT);
17022
17023 bounds->set_num_fields (range_fields.size ());
17024 bounds->set_fields
17025 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17026 * sizeof (struct field))));
17027 memcpy (bounds->fields (), range_fields.data (),
17028 bounds->num_fields () * sizeof (struct field));
17029
17030 int last_fieldno = range_fields.size () - 1;
17031 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17032 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17033 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17034
17035 /* Rewrite the existing array type in place. Specifically, we
17036 remove any dynamic properties we might have read, and we replace
17037 the index types. */
17038 struct type *iter = type;
17039 for (int i = 0; i < range_fields.size (); i += 2)
17040 {
17041 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17042 iter->main_type->dyn_prop_list = nullptr;
17043 iter->set_index_type
17044 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17045 iter = TYPE_TARGET_TYPE (iter);
17046 }
17047
17048 struct type *result = alloc_type (objfile);
17049 result->set_code (TYPE_CODE_STRUCT);
17050
17051 result->set_num_fields (2);
17052 result->set_fields
17053 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17054 * sizeof (struct field))));
17055
17056 /* The names are chosen to coincide with what the compiler does with
17057 -fgnat-encodings=all, which the Ada code in gdb already
17058 understands. */
17059 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17060 result->field (0).set_type (lookup_pointer_type (type));
17061
17062 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17063 result->field (1).set_type (lookup_pointer_type (bounds));
17064 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17065
17066 result->set_name (type->name ());
17067 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17068 + TYPE_LENGTH (result->field (1).type ()));
17069
17070 return result;
17071 }
17072
17073 /* Extract all information from a DW_TAG_array_type DIE and put it in
17074 the DIE's type field. For now, this only handles one dimensional
17075 arrays. */
17076
17077 static struct type *
17078 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17079 {
17080 struct objfile *objfile = cu->per_objfile->objfile;
17081 struct die_info *child_die;
17082 struct type *type;
17083 struct type *element_type, *range_type, *index_type;
17084 struct attribute *attr;
17085 const char *name;
17086 struct dynamic_prop *byte_stride_prop = NULL;
17087 unsigned int bit_stride = 0;
17088
17089 element_type = die_type (die, cu);
17090
17091 /* The die_type call above may have already set the type for this DIE. */
17092 type = get_die_type (die, cu);
17093 if (type)
17094 return type;
17095
17096 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17097 if (attr != NULL)
17098 {
17099 int stride_ok;
17100 struct type *prop_type = cu->addr_sized_int_type (false);
17101
17102 byte_stride_prop
17103 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17104 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17105 prop_type);
17106 if (!stride_ok)
17107 {
17108 complaint (_("unable to read array DW_AT_byte_stride "
17109 " - DIE at %s [in module %s]"),
17110 sect_offset_str (die->sect_off),
17111 objfile_name (cu->per_objfile->objfile));
17112 /* Ignore this attribute. We will likely not be able to print
17113 arrays of this type correctly, but there is little we can do
17114 to help if we cannot read the attribute's value. */
17115 byte_stride_prop = NULL;
17116 }
17117 }
17118
17119 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17120 if (attr != NULL)
17121 bit_stride = attr->constant_value (0);
17122
17123 /* Irix 6.2 native cc creates array types without children for
17124 arrays with unspecified length. */
17125 if (die->child == NULL)
17126 {
17127 index_type = objfile_type (objfile)->builtin_int;
17128 range_type = create_static_range_type (NULL, index_type, 0, -1);
17129 type = create_array_type_with_stride (NULL, element_type, range_type,
17130 byte_stride_prop, bit_stride);
17131 return set_die_type (die, type, cu);
17132 }
17133
17134 std::vector<struct type *> range_types;
17135 child_die = die->child;
17136 while (child_die && child_die->tag)
17137 {
17138 if (child_die->tag == DW_TAG_subrange_type)
17139 {
17140 struct type *child_type = read_type_die (child_die, cu);
17141
17142 if (child_type != NULL)
17143 {
17144 /* The range type was succesfully read. Save it for the
17145 array type creation. */
17146 range_types.push_back (child_type);
17147 }
17148 }
17149 child_die = child_die->sibling;
17150 }
17151
17152 if (range_types.empty ())
17153 {
17154 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17155 sect_offset_str (die->sect_off),
17156 objfile_name (cu->per_objfile->objfile));
17157 return NULL;
17158 }
17159
17160 /* Dwarf2 dimensions are output from left to right, create the
17161 necessary array types in backwards order. */
17162
17163 type = element_type;
17164
17165 if (read_array_order (die, cu) == DW_ORD_col_major)
17166 {
17167 int i = 0;
17168
17169 while (i < range_types.size ())
17170 {
17171 type = create_array_type_with_stride (NULL, type, range_types[i++],
17172 byte_stride_prop, bit_stride);
17173 bit_stride = 0;
17174 byte_stride_prop = nullptr;
17175 }
17176 }
17177 else
17178 {
17179 size_t ndim = range_types.size ();
17180 while (ndim-- > 0)
17181 {
17182 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17183 byte_stride_prop, bit_stride);
17184 bit_stride = 0;
17185 byte_stride_prop = nullptr;
17186 }
17187 }
17188
17189 gdb_assert (type != element_type);
17190
17191 /* Understand Dwarf2 support for vector types (like they occur on
17192 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17193 array type. This is not part of the Dwarf2/3 standard yet, but a
17194 custom vendor extension. The main difference between a regular
17195 array and the vector variant is that vectors are passed by value
17196 to functions. */
17197 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17198 if (attr != nullptr)
17199 make_vector_type (type);
17200
17201 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17202 implementation may choose to implement triple vectors using this
17203 attribute. */
17204 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17205 if (attr != nullptr && attr->form_is_unsigned ())
17206 {
17207 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17208 TYPE_LENGTH (type) = attr->as_unsigned ();
17209 else
17210 complaint (_("DW_AT_byte_size for array type smaller "
17211 "than the total size of elements"));
17212 }
17213
17214 name = dwarf2_name (die, cu);
17215 if (name)
17216 type->set_name (name);
17217
17218 maybe_set_alignment (cu, die, type);
17219
17220 struct type *replacement_type = nullptr;
17221 if (cu->language == language_ada)
17222 {
17223 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17224 if (replacement_type != nullptr)
17225 type = replacement_type;
17226 }
17227
17228 /* Install the type in the die. */
17229 set_die_type (die, type, cu, replacement_type != nullptr);
17230
17231 /* set_die_type should be already done. */
17232 set_descriptive_type (type, die, cu);
17233
17234 return type;
17235 }
17236
17237 static enum dwarf_array_dim_ordering
17238 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17239 {
17240 struct attribute *attr;
17241
17242 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17243
17244 if (attr != nullptr)
17245 {
17246 LONGEST val = attr->constant_value (-1);
17247 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17248 return (enum dwarf_array_dim_ordering) val;
17249 }
17250
17251 /* GNU F77 is a special case, as at 08/2004 array type info is the
17252 opposite order to the dwarf2 specification, but data is still
17253 laid out as per normal fortran.
17254
17255 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17256 version checking. */
17257
17258 if (cu->language == language_fortran
17259 && cu->producer && strstr (cu->producer, "GNU F77"))
17260 {
17261 return DW_ORD_row_major;
17262 }
17263
17264 switch (cu->language_defn->array_ordering ())
17265 {
17266 case array_column_major:
17267 return DW_ORD_col_major;
17268 case array_row_major:
17269 default:
17270 return DW_ORD_row_major;
17271 };
17272 }
17273
17274 /* Extract all information from a DW_TAG_set_type DIE and put it in
17275 the DIE's type field. */
17276
17277 static struct type *
17278 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17279 {
17280 struct type *domain_type, *set_type;
17281 struct attribute *attr;
17282
17283 domain_type = die_type (die, cu);
17284
17285 /* The die_type call above may have already set the type for this DIE. */
17286 set_type = get_die_type (die, cu);
17287 if (set_type)
17288 return set_type;
17289
17290 set_type = create_set_type (NULL, domain_type);
17291
17292 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17293 if (attr != nullptr && attr->form_is_unsigned ())
17294 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17295
17296 maybe_set_alignment (cu, die, set_type);
17297
17298 return set_die_type (die, set_type, cu);
17299 }
17300
17301 /* A helper for read_common_block that creates a locexpr baton.
17302 SYM is the symbol which we are marking as computed.
17303 COMMON_DIE is the DIE for the common block.
17304 COMMON_LOC is the location expression attribute for the common
17305 block itself.
17306 MEMBER_LOC is the location expression attribute for the particular
17307 member of the common block that we are processing.
17308 CU is the CU from which the above come. */
17309
17310 static void
17311 mark_common_block_symbol_computed (struct symbol *sym,
17312 struct die_info *common_die,
17313 struct attribute *common_loc,
17314 struct attribute *member_loc,
17315 struct dwarf2_cu *cu)
17316 {
17317 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17318 struct objfile *objfile = per_objfile->objfile;
17319 struct dwarf2_locexpr_baton *baton;
17320 gdb_byte *ptr;
17321 unsigned int cu_off;
17322 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17323 LONGEST offset = 0;
17324
17325 gdb_assert (common_loc && member_loc);
17326 gdb_assert (common_loc->form_is_block ());
17327 gdb_assert (member_loc->form_is_block ()
17328 || member_loc->form_is_constant ());
17329
17330 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17331 baton->per_objfile = per_objfile;
17332 baton->per_cu = cu->per_cu;
17333 gdb_assert (baton->per_cu);
17334
17335 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17336
17337 if (member_loc->form_is_constant ())
17338 {
17339 offset = member_loc->constant_value (0);
17340 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17341 }
17342 else
17343 baton->size += member_loc->as_block ()->size;
17344
17345 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17346 baton->data = ptr;
17347
17348 *ptr++ = DW_OP_call4;
17349 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17350 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17351 ptr += 4;
17352
17353 if (member_loc->form_is_constant ())
17354 {
17355 *ptr++ = DW_OP_addr;
17356 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17357 ptr += cu->header.addr_size;
17358 }
17359 else
17360 {
17361 /* We have to copy the data here, because DW_OP_call4 will only
17362 use a DW_AT_location attribute. */
17363 struct dwarf_block *block = member_loc->as_block ();
17364 memcpy (ptr, block->data, block->size);
17365 ptr += block->size;
17366 }
17367
17368 *ptr++ = DW_OP_plus;
17369 gdb_assert (ptr - baton->data == baton->size);
17370
17371 SYMBOL_LOCATION_BATON (sym) = baton;
17372 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17373 }
17374
17375 /* Create appropriate locally-scoped variables for all the
17376 DW_TAG_common_block entries. Also create a struct common_block
17377 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17378 is used to separate the common blocks name namespace from regular
17379 variable names. */
17380
17381 static void
17382 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17383 {
17384 struct attribute *attr;
17385
17386 attr = dwarf2_attr (die, DW_AT_location, cu);
17387 if (attr != nullptr)
17388 {
17389 /* Support the .debug_loc offsets. */
17390 if (attr->form_is_block ())
17391 {
17392 /* Ok. */
17393 }
17394 else if (attr->form_is_section_offset ())
17395 {
17396 dwarf2_complex_location_expr_complaint ();
17397 attr = NULL;
17398 }
17399 else
17400 {
17401 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17402 "common block member");
17403 attr = NULL;
17404 }
17405 }
17406
17407 if (die->child != NULL)
17408 {
17409 struct objfile *objfile = cu->per_objfile->objfile;
17410 struct die_info *child_die;
17411 size_t n_entries = 0, size;
17412 struct common_block *common_block;
17413 struct symbol *sym;
17414
17415 for (child_die = die->child;
17416 child_die && child_die->tag;
17417 child_die = child_die->sibling)
17418 ++n_entries;
17419
17420 size = (sizeof (struct common_block)
17421 + (n_entries - 1) * sizeof (struct symbol *));
17422 common_block
17423 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17424 size);
17425 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17426 common_block->n_entries = 0;
17427
17428 for (child_die = die->child;
17429 child_die && child_die->tag;
17430 child_die = child_die->sibling)
17431 {
17432 /* Create the symbol in the DW_TAG_common_block block in the current
17433 symbol scope. */
17434 sym = new_symbol (child_die, NULL, cu);
17435 if (sym != NULL)
17436 {
17437 struct attribute *member_loc;
17438
17439 common_block->contents[common_block->n_entries++] = sym;
17440
17441 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17442 cu);
17443 if (member_loc)
17444 {
17445 /* GDB has handled this for a long time, but it is
17446 not specified by DWARF. It seems to have been
17447 emitted by gfortran at least as recently as:
17448 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17449 complaint (_("Variable in common block has "
17450 "DW_AT_data_member_location "
17451 "- DIE at %s [in module %s]"),
17452 sect_offset_str (child_die->sect_off),
17453 objfile_name (objfile));
17454
17455 if (member_loc->form_is_section_offset ())
17456 dwarf2_complex_location_expr_complaint ();
17457 else if (member_loc->form_is_constant ()
17458 || member_loc->form_is_block ())
17459 {
17460 if (attr != nullptr)
17461 mark_common_block_symbol_computed (sym, die, attr,
17462 member_loc, cu);
17463 }
17464 else
17465 dwarf2_complex_location_expr_complaint ();
17466 }
17467 }
17468 }
17469
17470 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17471 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17472 }
17473 }
17474
17475 /* Create a type for a C++ namespace. */
17476
17477 static struct type *
17478 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17479 {
17480 struct objfile *objfile = cu->per_objfile->objfile;
17481 const char *previous_prefix, *name;
17482 int is_anonymous;
17483 struct type *type;
17484
17485 /* For extensions, reuse the type of the original namespace. */
17486 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17487 {
17488 struct die_info *ext_die;
17489 struct dwarf2_cu *ext_cu = cu;
17490
17491 ext_die = dwarf2_extension (die, &ext_cu);
17492 type = read_type_die (ext_die, ext_cu);
17493
17494 /* EXT_CU may not be the same as CU.
17495 Ensure TYPE is recorded with CU in die_type_hash. */
17496 return set_die_type (die, type, cu);
17497 }
17498
17499 name = namespace_name (die, &is_anonymous, cu);
17500
17501 /* Now build the name of the current namespace. */
17502
17503 previous_prefix = determine_prefix (die, cu);
17504 if (previous_prefix[0] != '\0')
17505 name = typename_concat (&objfile->objfile_obstack,
17506 previous_prefix, name, 0, cu);
17507
17508 /* Create the type. */
17509 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17510
17511 return set_die_type (die, type, cu);
17512 }
17513
17514 /* Read a namespace scope. */
17515
17516 static void
17517 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17518 {
17519 struct objfile *objfile = cu->per_objfile->objfile;
17520 int is_anonymous;
17521
17522 /* Add a symbol associated to this if we haven't seen the namespace
17523 before. Also, add a using directive if it's an anonymous
17524 namespace. */
17525
17526 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17527 {
17528 struct type *type;
17529
17530 type = read_type_die (die, cu);
17531 new_symbol (die, type, cu);
17532
17533 namespace_name (die, &is_anonymous, cu);
17534 if (is_anonymous)
17535 {
17536 const char *previous_prefix = determine_prefix (die, cu);
17537
17538 std::vector<const char *> excludes;
17539 add_using_directive (using_directives (cu),
17540 previous_prefix, type->name (), NULL,
17541 NULL, excludes, 0, &objfile->objfile_obstack);
17542 }
17543 }
17544
17545 if (die->child != NULL)
17546 {
17547 struct die_info *child_die = die->child;
17548
17549 while (child_die && child_die->tag)
17550 {
17551 process_die (child_die, cu);
17552 child_die = child_die->sibling;
17553 }
17554 }
17555 }
17556
17557 /* Read a Fortran module as type. This DIE can be only a declaration used for
17558 imported module. Still we need that type as local Fortran "use ... only"
17559 declaration imports depend on the created type in determine_prefix. */
17560
17561 static struct type *
17562 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17563 {
17564 struct objfile *objfile = cu->per_objfile->objfile;
17565 const char *module_name;
17566 struct type *type;
17567
17568 module_name = dwarf2_name (die, cu);
17569 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17570
17571 return set_die_type (die, type, cu);
17572 }
17573
17574 /* Read a Fortran module. */
17575
17576 static void
17577 read_module (struct die_info *die, struct dwarf2_cu *cu)
17578 {
17579 struct die_info *child_die = die->child;
17580 struct type *type;
17581
17582 type = read_type_die (die, cu);
17583 new_symbol (die, type, cu);
17584
17585 while (child_die && child_die->tag)
17586 {
17587 process_die (child_die, cu);
17588 child_die = child_die->sibling;
17589 }
17590 }
17591
17592 /* Return the name of the namespace represented by DIE. Set
17593 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17594 namespace. */
17595
17596 static const char *
17597 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17598 {
17599 struct die_info *current_die;
17600 const char *name = NULL;
17601
17602 /* Loop through the extensions until we find a name. */
17603
17604 for (current_die = die;
17605 current_die != NULL;
17606 current_die = dwarf2_extension (die, &cu))
17607 {
17608 /* We don't use dwarf2_name here so that we can detect the absence
17609 of a name -> anonymous namespace. */
17610 name = dwarf2_string_attr (die, DW_AT_name, cu);
17611
17612 if (name != NULL)
17613 break;
17614 }
17615
17616 /* Is it an anonymous namespace? */
17617
17618 *is_anonymous = (name == NULL);
17619 if (*is_anonymous)
17620 name = CP_ANONYMOUS_NAMESPACE_STR;
17621
17622 return name;
17623 }
17624
17625 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17626 the user defined type vector. */
17627
17628 static struct type *
17629 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17630 {
17631 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17632 struct comp_unit_head *cu_header = &cu->header;
17633 struct type *type;
17634 struct attribute *attr_byte_size;
17635 struct attribute *attr_address_class;
17636 int byte_size, addr_class;
17637 struct type *target_type;
17638
17639 target_type = die_type (die, cu);
17640
17641 /* The die_type call above may have already set the type for this DIE. */
17642 type = get_die_type (die, cu);
17643 if (type)
17644 return type;
17645
17646 type = lookup_pointer_type (target_type);
17647
17648 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17649 if (attr_byte_size)
17650 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17651 else
17652 byte_size = cu_header->addr_size;
17653
17654 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17655 if (attr_address_class)
17656 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17657 else
17658 addr_class = DW_ADDR_none;
17659
17660 ULONGEST alignment = get_alignment (cu, die);
17661
17662 /* If the pointer size, alignment, or address class is different
17663 than the default, create a type variant marked as such and set
17664 the length accordingly. */
17665 if (TYPE_LENGTH (type) != byte_size
17666 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17667 && alignment != TYPE_RAW_ALIGN (type))
17668 || addr_class != DW_ADDR_none)
17669 {
17670 if (gdbarch_address_class_type_flags_p (gdbarch))
17671 {
17672 type_instance_flags type_flags
17673 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17674 addr_class);
17675 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17676 == 0);
17677 type = make_type_with_address_space (type, type_flags);
17678 }
17679 else if (TYPE_LENGTH (type) != byte_size)
17680 {
17681 complaint (_("invalid pointer size %d"), byte_size);
17682 }
17683 else if (TYPE_RAW_ALIGN (type) != alignment)
17684 {
17685 complaint (_("Invalid DW_AT_alignment"
17686 " - DIE at %s [in module %s]"),
17687 sect_offset_str (die->sect_off),
17688 objfile_name (cu->per_objfile->objfile));
17689 }
17690 else
17691 {
17692 /* Should we also complain about unhandled address classes? */
17693 }
17694 }
17695
17696 TYPE_LENGTH (type) = byte_size;
17697 set_type_align (type, alignment);
17698 return set_die_type (die, type, cu);
17699 }
17700
17701 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17702 the user defined type vector. */
17703
17704 static struct type *
17705 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17706 {
17707 struct type *type;
17708 struct type *to_type;
17709 struct type *domain;
17710
17711 to_type = die_type (die, cu);
17712 domain = die_containing_type (die, cu);
17713
17714 /* The calls above may have already set the type for this DIE. */
17715 type = get_die_type (die, cu);
17716 if (type)
17717 return type;
17718
17719 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17720 type = lookup_methodptr_type (to_type);
17721 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17722 {
17723 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17724
17725 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17726 to_type->fields (), to_type->num_fields (),
17727 to_type->has_varargs ());
17728 type = lookup_methodptr_type (new_type);
17729 }
17730 else
17731 type = lookup_memberptr_type (to_type, domain);
17732
17733 return set_die_type (die, type, cu);
17734 }
17735
17736 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17737 the user defined type vector. */
17738
17739 static struct type *
17740 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17741 enum type_code refcode)
17742 {
17743 struct comp_unit_head *cu_header = &cu->header;
17744 struct type *type, *target_type;
17745 struct attribute *attr;
17746
17747 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17748
17749 target_type = die_type (die, cu);
17750
17751 /* The die_type call above may have already set the type for this DIE. */
17752 type = get_die_type (die, cu);
17753 if (type)
17754 return type;
17755
17756 type = lookup_reference_type (target_type, refcode);
17757 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17758 if (attr != nullptr)
17759 {
17760 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17761 }
17762 else
17763 {
17764 TYPE_LENGTH (type) = cu_header->addr_size;
17765 }
17766 maybe_set_alignment (cu, die, type);
17767 return set_die_type (die, type, cu);
17768 }
17769
17770 /* Add the given cv-qualifiers to the element type of the array. GCC
17771 outputs DWARF type qualifiers that apply to an array, not the
17772 element type. But GDB relies on the array element type to carry
17773 the cv-qualifiers. This mimics section 6.7.3 of the C99
17774 specification. */
17775
17776 static struct type *
17777 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17778 struct type *base_type, int cnst, int voltl)
17779 {
17780 struct type *el_type, *inner_array;
17781
17782 base_type = copy_type (base_type);
17783 inner_array = base_type;
17784
17785 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17786 {
17787 TYPE_TARGET_TYPE (inner_array) =
17788 copy_type (TYPE_TARGET_TYPE (inner_array));
17789 inner_array = TYPE_TARGET_TYPE (inner_array);
17790 }
17791
17792 el_type = TYPE_TARGET_TYPE (inner_array);
17793 cnst |= TYPE_CONST (el_type);
17794 voltl |= TYPE_VOLATILE (el_type);
17795 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17796
17797 return set_die_type (die, base_type, cu);
17798 }
17799
17800 static struct type *
17801 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17802 {
17803 struct type *base_type, *cv_type;
17804
17805 base_type = die_type (die, cu);
17806
17807 /* The die_type call above may have already set the type for this DIE. */
17808 cv_type = get_die_type (die, cu);
17809 if (cv_type)
17810 return cv_type;
17811
17812 /* In case the const qualifier is applied to an array type, the element type
17813 is so qualified, not the array type (section 6.7.3 of C99). */
17814 if (base_type->code () == TYPE_CODE_ARRAY)
17815 return add_array_cv_type (die, cu, base_type, 1, 0);
17816
17817 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17818 return set_die_type (die, cv_type, cu);
17819 }
17820
17821 static struct type *
17822 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17823 {
17824 struct type *base_type, *cv_type;
17825
17826 base_type = die_type (die, cu);
17827
17828 /* The die_type call above may have already set the type for this DIE. */
17829 cv_type = get_die_type (die, cu);
17830 if (cv_type)
17831 return cv_type;
17832
17833 /* In case the volatile qualifier is applied to an array type, the
17834 element type is so qualified, not the array type (section 6.7.3
17835 of C99). */
17836 if (base_type->code () == TYPE_CODE_ARRAY)
17837 return add_array_cv_type (die, cu, base_type, 0, 1);
17838
17839 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17840 return set_die_type (die, cv_type, cu);
17841 }
17842
17843 /* Handle DW_TAG_restrict_type. */
17844
17845 static struct type *
17846 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17847 {
17848 struct type *base_type, *cv_type;
17849
17850 base_type = die_type (die, cu);
17851
17852 /* The die_type call above may have already set the type for this DIE. */
17853 cv_type = get_die_type (die, cu);
17854 if (cv_type)
17855 return cv_type;
17856
17857 cv_type = make_restrict_type (base_type);
17858 return set_die_type (die, cv_type, cu);
17859 }
17860
17861 /* Handle DW_TAG_atomic_type. */
17862
17863 static struct type *
17864 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17865 {
17866 struct type *base_type, *cv_type;
17867
17868 base_type = die_type (die, cu);
17869
17870 /* The die_type call above may have already set the type for this DIE. */
17871 cv_type = get_die_type (die, cu);
17872 if (cv_type)
17873 return cv_type;
17874
17875 cv_type = make_atomic_type (base_type);
17876 return set_die_type (die, cv_type, cu);
17877 }
17878
17879 /* Extract all information from a DW_TAG_string_type DIE and add to
17880 the user defined type vector. It isn't really a user defined type,
17881 but it behaves like one, with other DIE's using an AT_user_def_type
17882 attribute to reference it. */
17883
17884 static struct type *
17885 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17886 {
17887 struct objfile *objfile = cu->per_objfile->objfile;
17888 struct gdbarch *gdbarch = objfile->arch ();
17889 struct type *type, *range_type, *index_type, *char_type;
17890 struct attribute *attr;
17891 struct dynamic_prop prop;
17892 bool length_is_constant = true;
17893 LONGEST length;
17894
17895 /* There are a couple of places where bit sizes might be made use of
17896 when parsing a DW_TAG_string_type, however, no producer that we know
17897 of make use of these. Handling bit sizes that are a multiple of the
17898 byte size is easy enough, but what about other bit sizes? Lets deal
17899 with that problem when we have to. Warn about these attributes being
17900 unsupported, then parse the type and ignore them like we always
17901 have. */
17902 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17903 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17904 {
17905 static bool warning_printed = false;
17906 if (!warning_printed)
17907 {
17908 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17909 "currently supported on DW_TAG_string_type."));
17910 warning_printed = true;
17911 }
17912 }
17913
17914 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17915 if (attr != nullptr && !attr->form_is_constant ())
17916 {
17917 /* The string length describes the location at which the length of
17918 the string can be found. The size of the length field can be
17919 specified with one of the attributes below. */
17920 struct type *prop_type;
17921 struct attribute *len
17922 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17923 if (len == nullptr)
17924 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17925 if (len != nullptr && len->form_is_constant ())
17926 {
17927 /* Pass 0 as the default as we know this attribute is constant
17928 and the default value will not be returned. */
17929 LONGEST sz = len->constant_value (0);
17930 prop_type = cu->per_objfile->int_type (sz, true);
17931 }
17932 else
17933 {
17934 /* If the size is not specified then we assume it is the size of
17935 an address on this target. */
17936 prop_type = cu->addr_sized_int_type (true);
17937 }
17938
17939 /* Convert the attribute into a dynamic property. */
17940 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17941 length = 1;
17942 else
17943 length_is_constant = false;
17944 }
17945 else if (attr != nullptr)
17946 {
17947 /* This DW_AT_string_length just contains the length with no
17948 indirection. There's no need to create a dynamic property in this
17949 case. Pass 0 for the default value as we know it will not be
17950 returned in this case. */
17951 length = attr->constant_value (0);
17952 }
17953 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17954 {
17955 /* We don't currently support non-constant byte sizes for strings. */
17956 length = attr->constant_value (1);
17957 }
17958 else
17959 {
17960 /* Use 1 as a fallback length if we have nothing else. */
17961 length = 1;
17962 }
17963
17964 index_type = objfile_type (objfile)->builtin_int;
17965 if (length_is_constant)
17966 range_type = create_static_range_type (NULL, index_type, 1, length);
17967 else
17968 {
17969 struct dynamic_prop low_bound;
17970
17971 low_bound.set_const_val (1);
17972 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17973 }
17974 char_type = language_string_char_type (cu->language_defn, gdbarch);
17975 type = create_string_type (NULL, char_type, range_type);
17976
17977 return set_die_type (die, type, cu);
17978 }
17979
17980 /* Assuming that DIE corresponds to a function, returns nonzero
17981 if the function is prototyped. */
17982
17983 static int
17984 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17985 {
17986 struct attribute *attr;
17987
17988 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17989 if (attr && attr->as_boolean ())
17990 return 1;
17991
17992 /* The DWARF standard implies that the DW_AT_prototyped attribute
17993 is only meaningful for C, but the concept also extends to other
17994 languages that allow unprototyped functions (Eg: Objective C).
17995 For all other languages, assume that functions are always
17996 prototyped. */
17997 if (cu->language != language_c
17998 && cu->language != language_objc
17999 && cu->language != language_opencl)
18000 return 1;
18001
18002 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18003 prototyped and unprototyped functions; default to prototyped,
18004 since that is more common in modern code (and RealView warns
18005 about unprototyped functions). */
18006 if (producer_is_realview (cu->producer))
18007 return 1;
18008
18009 return 0;
18010 }
18011
18012 /* Handle DIES due to C code like:
18013
18014 struct foo
18015 {
18016 int (*funcp)(int a, long l);
18017 int b;
18018 };
18019
18020 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18021
18022 static struct type *
18023 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18024 {
18025 struct objfile *objfile = cu->per_objfile->objfile;
18026 struct type *type; /* Type that this function returns. */
18027 struct type *ftype; /* Function that returns above type. */
18028 struct attribute *attr;
18029
18030 type = die_type (die, cu);
18031
18032 /* The die_type call above may have already set the type for this DIE. */
18033 ftype = get_die_type (die, cu);
18034 if (ftype)
18035 return ftype;
18036
18037 ftype = lookup_function_type (type);
18038
18039 if (prototyped_function_p (die, cu))
18040 ftype->set_is_prototyped (true);
18041
18042 /* Store the calling convention in the type if it's available in
18043 the subroutine die. Otherwise set the calling convention to
18044 the default value DW_CC_normal. */
18045 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18046 if (attr != nullptr
18047 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18048 TYPE_CALLING_CONVENTION (ftype)
18049 = (enum dwarf_calling_convention) attr->constant_value (0);
18050 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18051 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18052 else
18053 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18054
18055 /* Record whether the function returns normally to its caller or not
18056 if the DWARF producer set that information. */
18057 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18058 if (attr && attr->as_boolean ())
18059 TYPE_NO_RETURN (ftype) = 1;
18060
18061 /* We need to add the subroutine type to the die immediately so
18062 we don't infinitely recurse when dealing with parameters
18063 declared as the same subroutine type. */
18064 set_die_type (die, ftype, cu);
18065
18066 if (die->child != NULL)
18067 {
18068 struct type *void_type = objfile_type (objfile)->builtin_void;
18069 struct die_info *child_die;
18070 int nparams, iparams;
18071
18072 /* Count the number of parameters.
18073 FIXME: GDB currently ignores vararg functions, but knows about
18074 vararg member functions. */
18075 nparams = 0;
18076 child_die = die->child;
18077 while (child_die && child_die->tag)
18078 {
18079 if (child_die->tag == DW_TAG_formal_parameter)
18080 nparams++;
18081 else if (child_die->tag == DW_TAG_unspecified_parameters)
18082 ftype->set_has_varargs (true);
18083
18084 child_die = child_die->sibling;
18085 }
18086
18087 /* Allocate storage for parameters and fill them in. */
18088 ftype->set_num_fields (nparams);
18089 ftype->set_fields
18090 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18091
18092 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18093 even if we error out during the parameters reading below. */
18094 for (iparams = 0; iparams < nparams; iparams++)
18095 ftype->field (iparams).set_type (void_type);
18096
18097 iparams = 0;
18098 child_die = die->child;
18099 while (child_die && child_die->tag)
18100 {
18101 if (child_die->tag == DW_TAG_formal_parameter)
18102 {
18103 struct type *arg_type;
18104
18105 /* DWARF version 2 has no clean way to discern C++
18106 static and non-static member functions. G++ helps
18107 GDB by marking the first parameter for non-static
18108 member functions (which is the this pointer) as
18109 artificial. We pass this information to
18110 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18111
18112 DWARF version 3 added DW_AT_object_pointer, which GCC
18113 4.5 does not yet generate. */
18114 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18115 if (attr != nullptr)
18116 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18117 else
18118 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18119 arg_type = die_type (child_die, cu);
18120
18121 /* RealView does not mark THIS as const, which the testsuite
18122 expects. GCC marks THIS as const in method definitions,
18123 but not in the class specifications (GCC PR 43053). */
18124 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18125 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18126 {
18127 int is_this = 0;
18128 struct dwarf2_cu *arg_cu = cu;
18129 const char *name = dwarf2_name (child_die, cu);
18130
18131 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18132 if (attr != nullptr)
18133 {
18134 /* If the compiler emits this, use it. */
18135 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18136 is_this = 1;
18137 }
18138 else if (name && strcmp (name, "this") == 0)
18139 /* Function definitions will have the argument names. */
18140 is_this = 1;
18141 else if (name == NULL && iparams == 0)
18142 /* Declarations may not have the names, so like
18143 elsewhere in GDB, assume an artificial first
18144 argument is "this". */
18145 is_this = 1;
18146
18147 if (is_this)
18148 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18149 arg_type, 0);
18150 }
18151
18152 ftype->field (iparams).set_type (arg_type);
18153 iparams++;
18154 }
18155 child_die = child_die->sibling;
18156 }
18157 }
18158
18159 return ftype;
18160 }
18161
18162 static struct type *
18163 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18164 {
18165 struct objfile *objfile = cu->per_objfile->objfile;
18166 const char *name = NULL;
18167 struct type *this_type, *target_type;
18168
18169 name = dwarf2_full_name (NULL, die, cu);
18170 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18171 this_type->set_target_is_stub (true);
18172 set_die_type (die, this_type, cu);
18173 target_type = die_type (die, cu);
18174 if (target_type != this_type)
18175 TYPE_TARGET_TYPE (this_type) = target_type;
18176 else
18177 {
18178 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18179 spec and cause infinite loops in GDB. */
18180 complaint (_("Self-referential DW_TAG_typedef "
18181 "- DIE at %s [in module %s]"),
18182 sect_offset_str (die->sect_off), objfile_name (objfile));
18183 TYPE_TARGET_TYPE (this_type) = NULL;
18184 }
18185 if (name == NULL)
18186 {
18187 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18188 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18189 Handle these by just returning the target type, rather than
18190 constructing an anonymous typedef type and trying to handle this
18191 elsewhere. */
18192 set_die_type (die, target_type, cu);
18193 return target_type;
18194 }
18195 return this_type;
18196 }
18197
18198 /* Helper for get_dwarf2_rational_constant that computes the value of
18199 a given gmp_mpz given an attribute. */
18200
18201 static void
18202 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18203 {
18204 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18205 location expression that pushes an implicit value. */
18206 if (attr->form == DW_FORM_exprloc)
18207 {
18208 dwarf_block *blk = attr->as_block ();
18209 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18210 {
18211 uint64_t len;
18212 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18213 blk->data + blk->size,
18214 &len);
18215 if (ptr - blk->data + len <= blk->size)
18216 {
18217 mpz_import (value->val, len,
18218 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18219 1, 0, 0, ptr);
18220 return;
18221 }
18222 }
18223
18224 /* On failure set it to 1. */
18225 *value = gdb_mpz (1);
18226 }
18227 else if (attr->form_is_block ())
18228 {
18229 dwarf_block *blk = attr->as_block ();
18230 mpz_import (value->val, blk->size,
18231 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18232 1, 0, 0, blk->data);
18233 }
18234 else
18235 *value = gdb_mpz (attr->constant_value (1));
18236 }
18237
18238 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18239 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18240
18241 If the numerator and/or numerator attribute is missing,
18242 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18243 untouched. */
18244
18245 static void
18246 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18247 gdb_mpz *numerator, gdb_mpz *denominator)
18248 {
18249 struct attribute *num_attr, *denom_attr;
18250
18251 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18252 if (num_attr == nullptr)
18253 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18254 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18255
18256 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18257 if (denom_attr == nullptr)
18258 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18259 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18260
18261 if (num_attr == nullptr || denom_attr == nullptr)
18262 return;
18263
18264 get_mpz (cu, numerator, num_attr);
18265 get_mpz (cu, denominator, denom_attr);
18266 }
18267
18268 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18269 rational constant, rather than a signed one.
18270
18271 If the rational constant has a negative value, a complaint
18272 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18273
18274 static void
18275 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18276 struct dwarf2_cu *cu,
18277 gdb_mpz *numerator,
18278 gdb_mpz *denominator)
18279 {
18280 gdb_mpz num (1);
18281 gdb_mpz denom (1);
18282
18283 get_dwarf2_rational_constant (die, cu, &num, &denom);
18284 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18285 {
18286 mpz_neg (num.val, num.val);
18287 mpz_neg (denom.val, denom.val);
18288 }
18289 else if (mpz_sgn (num.val) == -1)
18290 {
18291 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18292 " in DIE at %s"),
18293 sect_offset_str (die->sect_off));
18294 return;
18295 }
18296 else if (mpz_sgn (denom.val) == -1)
18297 {
18298 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18299 " in DIE at %s"),
18300 sect_offset_str (die->sect_off));
18301 return;
18302 }
18303
18304 *numerator = std::move (num);
18305 *denominator = std::move (denom);
18306 }
18307
18308 /* Assuming that ENCODING is a string whose contents starting at the
18309 K'th character is "_nn" where "nn" is a decimal number, scan that
18310 number and set RESULT to the value. K is updated to point to the
18311 character immediately following the number.
18312
18313 If the string does not conform to the format described above, false
18314 is returned, and K may or may not be changed. */
18315
18316 static bool
18317 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18318 {
18319 /* The next character should be an underscore ('_') followed
18320 by a digit. */
18321 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18322 return false;
18323
18324 /* Skip the underscore. */
18325 k++;
18326 int start = k;
18327
18328 /* Determine the number of digits for our number. */
18329 while (isdigit (encoding[k]))
18330 k++;
18331 if (k == start)
18332 return false;
18333
18334 std::string copy (&encoding[start], k - start);
18335 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18336 return false;
18337
18338 return true;
18339 }
18340
18341 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18342 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18343 DENOM, update OFFSET, and return true on success. Return false on
18344 failure. */
18345
18346 static bool
18347 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18348 gdb_mpz *num, gdb_mpz *denom)
18349 {
18350 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18351 return false;
18352 return ada_get_gnat_encoded_number (encoding, offset, denom);
18353 }
18354
18355 /* Assuming DIE corresponds to a fixed point type, finish the creation
18356 of the corresponding TYPE by setting its type-specific data. CU is
18357 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18358 encodings. It is nullptr if the GNAT encoding should be
18359 ignored. */
18360
18361 static void
18362 finish_fixed_point_type (struct type *type, const char *suffix,
18363 struct die_info *die, struct dwarf2_cu *cu)
18364 {
18365 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18366 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18367
18368 /* If GNAT encodings are preferred, don't examine the
18369 attributes. */
18370 struct attribute *attr = nullptr;
18371 if (suffix == nullptr)
18372 {
18373 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18374 if (attr == nullptr)
18375 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18376 if (attr == nullptr)
18377 attr = dwarf2_attr (die, DW_AT_small, cu);
18378 }
18379
18380 /* Numerator and denominator of our fixed-point type's scaling factor.
18381 The default is a scaling factor of 1, which we use as a fallback
18382 when we are not able to decode it (problem with the debugging info,
18383 unsupported forms, bug in GDB, etc...). Using that as the default
18384 allows us to at least print the unscaled value, which might still
18385 be useful to a user. */
18386 gdb_mpz scale_num (1);
18387 gdb_mpz scale_denom (1);
18388
18389 if (attr == nullptr)
18390 {
18391 int offset = 0;
18392 if (suffix != nullptr
18393 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18394 &scale_denom)
18395 /* The number might be encoded as _nn_dd_nn_dd, where the
18396 second ratio is the 'small value. In this situation, we
18397 want the second value. */
18398 && (suffix[offset] != '_'
18399 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18400 &scale_denom)))
18401 {
18402 /* Found it. */
18403 }
18404 else
18405 {
18406 /* Scaling factor not found. Assume a scaling factor of 1,
18407 and hope for the best. At least the user will be able to
18408 see the encoded value. */
18409 scale_num = 1;
18410 scale_denom = 1;
18411 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18412 sect_offset_str (die->sect_off));
18413 }
18414 }
18415 else if (attr->name == DW_AT_binary_scale)
18416 {
18417 LONGEST scale_exp = attr->constant_value (0);
18418 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18419
18420 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18421 }
18422 else if (attr->name == DW_AT_decimal_scale)
18423 {
18424 LONGEST scale_exp = attr->constant_value (0);
18425 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18426
18427 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18428 }
18429 else if (attr->name == DW_AT_small)
18430 {
18431 struct die_info *scale_die;
18432 struct dwarf2_cu *scale_cu = cu;
18433
18434 scale_die = follow_die_ref (die, attr, &scale_cu);
18435 if (scale_die->tag == DW_TAG_constant)
18436 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18437 &scale_num, &scale_denom);
18438 else
18439 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18440 " (DIE at %s)"),
18441 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18442 }
18443 else
18444 {
18445 complaint (_("unsupported scale attribute %s for fixed-point type"
18446 " (DIE at %s)"),
18447 dwarf_attr_name (attr->name),
18448 sect_offset_str (die->sect_off));
18449 }
18450
18451 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18452 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18453 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18454 mpq_canonicalize (scaling_factor.val);
18455 }
18456
18457 /* The gnat-encoding suffix for fixed point. */
18458
18459 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18460
18461 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18462 "XF" suffix of the name. The text after this is what encodes the
18463 'small and 'delta information. Otherwise, return nullptr. */
18464
18465 static const char *
18466 gnat_encoded_fixed_point_type_info (const char *name)
18467 {
18468 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18469 }
18470
18471 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18472 (which may be different from NAME) to the architecture back-end to allow
18473 it to guess the correct format if necessary. */
18474
18475 static struct type *
18476 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18477 const char *name_hint, enum bfd_endian byte_order)
18478 {
18479 struct gdbarch *gdbarch = objfile->arch ();
18480 const struct floatformat **format;
18481 struct type *type;
18482
18483 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18484 if (format)
18485 type = init_float_type (objfile, bits, name, format, byte_order);
18486 else
18487 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18488
18489 return type;
18490 }
18491
18492 /* Allocate an integer type of size BITS and name NAME. */
18493
18494 static struct type *
18495 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18496 int bits, int unsigned_p, const char *name)
18497 {
18498 struct type *type;
18499
18500 /* Versions of Intel's C Compiler generate an integer type called "void"
18501 instead of using DW_TAG_unspecified_type. This has been seen on
18502 at least versions 14, 17, and 18. */
18503 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18504 && strcmp (name, "void") == 0)
18505 type = objfile_type (objfile)->builtin_void;
18506 else
18507 type = init_integer_type (objfile, bits, unsigned_p, name);
18508
18509 return type;
18510 }
18511
18512 /* Return true if DIE has a DW_AT_small attribute whose value is
18513 a constant rational, where both the numerator and denominator
18514 are equal to zero.
18515
18516 CU is the DIE's Compilation Unit. */
18517
18518 static bool
18519 has_zero_over_zero_small_attribute (struct die_info *die,
18520 struct dwarf2_cu *cu)
18521 {
18522 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18523 if (attr == nullptr)
18524 return false;
18525
18526 struct dwarf2_cu *scale_cu = cu;
18527 struct die_info *scale_die
18528 = follow_die_ref (die, attr, &scale_cu);
18529
18530 if (scale_die->tag != DW_TAG_constant)
18531 return false;
18532
18533 gdb_mpz num (1), denom (1);
18534 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18535 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18536 }
18537
18538 /* Initialise and return a floating point type of size BITS suitable for
18539 use as a component of a complex number. The NAME_HINT is passed through
18540 when initialising the floating point type and is the name of the complex
18541 type.
18542
18543 As DWARF doesn't currently provide an explicit name for the components
18544 of a complex number, but it can be helpful to have these components
18545 named, we try to select a suitable name based on the size of the
18546 component. */
18547 static struct type *
18548 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18549 struct objfile *objfile,
18550 int bits, const char *name_hint,
18551 enum bfd_endian byte_order)
18552 {
18553 gdbarch *gdbarch = objfile->arch ();
18554 struct type *tt = nullptr;
18555
18556 /* Try to find a suitable floating point builtin type of size BITS.
18557 We're going to use the name of this type as the name for the complex
18558 target type that we are about to create. */
18559 switch (cu->language)
18560 {
18561 case language_fortran:
18562 switch (bits)
18563 {
18564 case 32:
18565 tt = builtin_f_type (gdbarch)->builtin_real;
18566 break;
18567 case 64:
18568 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18569 break;
18570 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18571 case 128:
18572 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18573 break;
18574 }
18575 break;
18576 default:
18577 switch (bits)
18578 {
18579 case 32:
18580 tt = builtin_type (gdbarch)->builtin_float;
18581 break;
18582 case 64:
18583 tt = builtin_type (gdbarch)->builtin_double;
18584 break;
18585 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18586 case 128:
18587 tt = builtin_type (gdbarch)->builtin_long_double;
18588 break;
18589 }
18590 break;
18591 }
18592
18593 /* If the type we found doesn't match the size we were looking for, then
18594 pretend we didn't find a type at all, the complex target type we
18595 create will then be nameless. */
18596 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18597 tt = nullptr;
18598
18599 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18600 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18601 }
18602
18603 /* Find a representation of a given base type and install
18604 it in the TYPE field of the die. */
18605
18606 static struct type *
18607 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18608 {
18609 struct objfile *objfile = cu->per_objfile->objfile;
18610 struct type *type;
18611 struct attribute *attr;
18612 int encoding = 0, bits = 0;
18613 const char *name;
18614 gdbarch *arch;
18615
18616 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18617 if (attr != nullptr && attr->form_is_constant ())
18618 encoding = attr->constant_value (0);
18619 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18620 if (attr != nullptr)
18621 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18622 name = dwarf2_name (die, cu);
18623 if (!name)
18624 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18625
18626 arch = objfile->arch ();
18627 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18628
18629 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18630 if (attr != nullptr && attr->form_is_constant ())
18631 {
18632 int endianity = attr->constant_value (0);
18633
18634 switch (endianity)
18635 {
18636 case DW_END_big:
18637 byte_order = BFD_ENDIAN_BIG;
18638 break;
18639 case DW_END_little:
18640 byte_order = BFD_ENDIAN_LITTLE;
18641 break;
18642 default:
18643 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18644 break;
18645 }
18646 }
18647
18648 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18649 && cu->language == language_ada
18650 && has_zero_over_zero_small_attribute (die, cu))
18651 {
18652 /* brobecker/2018-02-24: This is a fixed point type for which
18653 the scaling factor is represented as fraction whose value
18654 does not make sense (zero divided by zero), so we should
18655 normally never see these. However, there is a small category
18656 of fixed point types for which GNAT is unable to provide
18657 the scaling factor via the standard DWARF mechanisms, and
18658 for which the info is provided via the GNAT encodings instead.
18659 This is likely what this DIE is about. */
18660 encoding = (encoding == DW_ATE_signed_fixed
18661 ? DW_ATE_signed
18662 : DW_ATE_unsigned);
18663 }
18664
18665 /* With GNAT encodings, fixed-point information will be encoded in
18666 the type name. Note that this can also occur with the above
18667 zero-over-zero case, which is why this is a separate "if" rather
18668 than an "else if". */
18669 const char *gnat_encoding_suffix = nullptr;
18670 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18671 && cu->language == language_ada
18672 && name != nullptr)
18673 {
18674 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18675 if (gnat_encoding_suffix != nullptr)
18676 {
18677 gdb_assert (startswith (gnat_encoding_suffix,
18678 GNAT_FIXED_POINT_SUFFIX));
18679 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18680 name, gnat_encoding_suffix - name);
18681 /* Use -1 here so that SUFFIX points at the "_" after the
18682 "XF". */
18683 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18684
18685 encoding = (encoding == DW_ATE_signed
18686 ? DW_ATE_signed_fixed
18687 : DW_ATE_unsigned_fixed);
18688 }
18689 }
18690
18691 switch (encoding)
18692 {
18693 case DW_ATE_address:
18694 /* Turn DW_ATE_address into a void * pointer. */
18695 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18696 type = init_pointer_type (objfile, bits, name, type);
18697 break;
18698 case DW_ATE_boolean:
18699 type = init_boolean_type (objfile, bits, 1, name);
18700 break;
18701 case DW_ATE_complex_float:
18702 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18703 byte_order);
18704 if (type->code () == TYPE_CODE_ERROR)
18705 {
18706 if (name == nullptr)
18707 {
18708 struct obstack *obstack
18709 = &cu->per_objfile->objfile->objfile_obstack;
18710 name = obconcat (obstack, "_Complex ", type->name (),
18711 nullptr);
18712 }
18713 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18714 }
18715 else
18716 type = init_complex_type (name, type);
18717 break;
18718 case DW_ATE_decimal_float:
18719 type = init_decfloat_type (objfile, bits, name);
18720 break;
18721 case DW_ATE_float:
18722 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18723 break;
18724 case DW_ATE_signed:
18725 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18726 break;
18727 case DW_ATE_unsigned:
18728 if (cu->language == language_fortran
18729 && name
18730 && startswith (name, "character("))
18731 type = init_character_type (objfile, bits, 1, name);
18732 else
18733 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18734 break;
18735 case DW_ATE_signed_char:
18736 if (cu->language == language_ada || cu->language == language_m2
18737 || cu->language == language_pascal
18738 || cu->language == language_fortran)
18739 type = init_character_type (objfile, bits, 0, name);
18740 else
18741 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18742 break;
18743 case DW_ATE_unsigned_char:
18744 if (cu->language == language_ada || cu->language == language_m2
18745 || cu->language == language_pascal
18746 || cu->language == language_fortran
18747 || cu->language == language_rust)
18748 type = init_character_type (objfile, bits, 1, name);
18749 else
18750 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18751 break;
18752 case DW_ATE_UTF:
18753 {
18754 if (bits == 16)
18755 type = builtin_type (arch)->builtin_char16;
18756 else if (bits == 32)
18757 type = builtin_type (arch)->builtin_char32;
18758 else
18759 {
18760 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18761 bits);
18762 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18763 }
18764 return set_die_type (die, type, cu);
18765 }
18766 break;
18767 case DW_ATE_signed_fixed:
18768 type = init_fixed_point_type (objfile, bits, 0, name);
18769 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18770 break;
18771 case DW_ATE_unsigned_fixed:
18772 type = init_fixed_point_type (objfile, bits, 1, name);
18773 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18774 break;
18775
18776 default:
18777 complaint (_("unsupported DW_AT_encoding: '%s'"),
18778 dwarf_type_encoding_name (encoding));
18779 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18780 break;
18781 }
18782
18783 if (name && strcmp (name, "char") == 0)
18784 type->set_has_no_signedness (true);
18785
18786 maybe_set_alignment (cu, die, type);
18787
18788 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18789
18790 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18791 {
18792 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18793 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18794 {
18795 unsigned real_bit_size = attr->as_unsigned ();
18796 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18797 /* Only use the attributes if they make sense together. */
18798 if (attr == nullptr
18799 || (attr->as_unsigned () + real_bit_size
18800 <= 8 * TYPE_LENGTH (type)))
18801 {
18802 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18803 = real_bit_size;
18804 if (attr != nullptr)
18805 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18806 = attr->as_unsigned ();
18807 }
18808 }
18809 }
18810
18811 return set_die_type (die, type, cu);
18812 }
18813
18814 /* Parse dwarf attribute if it's a block, reference or constant and put the
18815 resulting value of the attribute into struct bound_prop.
18816 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18817
18818 static int
18819 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18820 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18821 struct type *default_type)
18822 {
18823 struct dwarf2_property_baton *baton;
18824 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18825 struct objfile *objfile = per_objfile->objfile;
18826 struct obstack *obstack = &objfile->objfile_obstack;
18827
18828 gdb_assert (default_type != NULL);
18829
18830 if (attr == NULL || prop == NULL)
18831 return 0;
18832
18833 if (attr->form_is_block ())
18834 {
18835 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18836 baton->property_type = default_type;
18837 baton->locexpr.per_cu = cu->per_cu;
18838 baton->locexpr.per_objfile = per_objfile;
18839
18840 struct dwarf_block *block = attr->as_block ();
18841 baton->locexpr.size = block->size;
18842 baton->locexpr.data = block->data;
18843 switch (attr->name)
18844 {
18845 case DW_AT_string_length:
18846 baton->locexpr.is_reference = true;
18847 break;
18848 default:
18849 baton->locexpr.is_reference = false;
18850 break;
18851 }
18852
18853 prop->set_locexpr (baton);
18854 gdb_assert (prop->baton () != NULL);
18855 }
18856 else if (attr->form_is_ref ())
18857 {
18858 struct dwarf2_cu *target_cu = cu;
18859 struct die_info *target_die;
18860 struct attribute *target_attr;
18861
18862 target_die = follow_die_ref (die, attr, &target_cu);
18863 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18864 if (target_attr == NULL)
18865 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18866 target_cu);
18867 if (target_attr == NULL)
18868 return 0;
18869
18870 switch (target_attr->name)
18871 {
18872 case DW_AT_location:
18873 if (target_attr->form_is_section_offset ())
18874 {
18875 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18876 baton->property_type = die_type (target_die, target_cu);
18877 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18878 prop->set_loclist (baton);
18879 gdb_assert (prop->baton () != NULL);
18880 }
18881 else if (target_attr->form_is_block ())
18882 {
18883 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18884 baton->property_type = die_type (target_die, target_cu);
18885 baton->locexpr.per_cu = cu->per_cu;
18886 baton->locexpr.per_objfile = per_objfile;
18887 struct dwarf_block *block = target_attr->as_block ();
18888 baton->locexpr.size = block->size;
18889 baton->locexpr.data = block->data;
18890 baton->locexpr.is_reference = true;
18891 prop->set_locexpr (baton);
18892 gdb_assert (prop->baton () != NULL);
18893 }
18894 else
18895 {
18896 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18897 "dynamic property");
18898 return 0;
18899 }
18900 break;
18901 case DW_AT_data_member_location:
18902 {
18903 LONGEST offset;
18904
18905 if (!handle_data_member_location (target_die, target_cu,
18906 &offset))
18907 return 0;
18908
18909 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18910 baton->property_type = read_type_die (target_die->parent,
18911 target_cu);
18912 baton->offset_info.offset = offset;
18913 baton->offset_info.type = die_type (target_die, target_cu);
18914 prop->set_addr_offset (baton);
18915 break;
18916 }
18917 }
18918 }
18919 else if (attr->form_is_constant ())
18920 prop->set_const_val (attr->constant_value (0));
18921 else
18922 {
18923 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18924 dwarf2_name (die, cu));
18925 return 0;
18926 }
18927
18928 return 1;
18929 }
18930
18931 /* See read.h. */
18932
18933 struct type *
18934 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18935 {
18936 struct type *int_type;
18937
18938 /* Helper macro to examine the various builtin types. */
18939 #define TRY_TYPE(F) \
18940 int_type = (unsigned_p \
18941 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18942 : objfile_type (objfile)->builtin_ ## F); \
18943 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18944 return int_type
18945
18946 TRY_TYPE (char);
18947 TRY_TYPE (short);
18948 TRY_TYPE (int);
18949 TRY_TYPE (long);
18950 TRY_TYPE (long_long);
18951
18952 #undef TRY_TYPE
18953
18954 gdb_assert_not_reached ("unable to find suitable integer type");
18955 }
18956
18957 /* See read.h. */
18958
18959 struct type *
18960 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18961 {
18962 int addr_size = this->per_cu->addr_size ();
18963 return this->per_objfile->int_type (addr_size, unsigned_p);
18964 }
18965
18966 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18967 present (which is valid) then compute the default type based on the
18968 compilation units address size. */
18969
18970 static struct type *
18971 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18972 {
18973 struct type *index_type = die_type (die, cu);
18974
18975 /* Dwarf-2 specifications explicitly allows to create subrange types
18976 without specifying a base type.
18977 In that case, the base type must be set to the type of
18978 the lower bound, upper bound or count, in that order, if any of these
18979 three attributes references an object that has a type.
18980 If no base type is found, the Dwarf-2 specifications say that
18981 a signed integer type of size equal to the size of an address should
18982 be used.
18983 For the following C code: `extern char gdb_int [];'
18984 GCC produces an empty range DIE.
18985 FIXME: muller/2010-05-28: Possible references to object for low bound,
18986 high bound or count are not yet handled by this code. */
18987 if (index_type->code () == TYPE_CODE_VOID)
18988 index_type = cu->addr_sized_int_type (false);
18989
18990 return index_type;
18991 }
18992
18993 /* Read the given DW_AT_subrange DIE. */
18994
18995 static struct type *
18996 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18997 {
18998 struct type *base_type, *orig_base_type;
18999 struct type *range_type;
19000 struct attribute *attr;
19001 struct dynamic_prop low, high;
19002 int low_default_is_valid;
19003 int high_bound_is_count = 0;
19004 const char *name;
19005 ULONGEST negative_mask;
19006
19007 orig_base_type = read_subrange_index_type (die, cu);
19008
19009 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
19010 whereas the real type might be. So, we use ORIG_BASE_TYPE when
19011 creating the range type, but we use the result of check_typedef
19012 when examining properties of the type. */
19013 base_type = check_typedef (orig_base_type);
19014
19015 /* The die_type call above may have already set the type for this DIE. */
19016 range_type = get_die_type (die, cu);
19017 if (range_type)
19018 return range_type;
19019
19020 high.set_const_val (0);
19021
19022 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19023 omitting DW_AT_lower_bound. */
19024 switch (cu->language)
19025 {
19026 case language_c:
19027 case language_cplus:
19028 low.set_const_val (0);
19029 low_default_is_valid = 1;
19030 break;
19031 case language_fortran:
19032 low.set_const_val (1);
19033 low_default_is_valid = 1;
19034 break;
19035 case language_d:
19036 case language_objc:
19037 case language_rust:
19038 low.set_const_val (0);
19039 low_default_is_valid = (cu->header.version >= 4);
19040 break;
19041 case language_ada:
19042 case language_m2:
19043 case language_pascal:
19044 low.set_const_val (1);
19045 low_default_is_valid = (cu->header.version >= 4);
19046 break;
19047 default:
19048 low.set_const_val (0);
19049 low_default_is_valid = 0;
19050 break;
19051 }
19052
19053 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
19054 if (attr != nullptr)
19055 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
19056 else if (!low_default_is_valid)
19057 complaint (_("Missing DW_AT_lower_bound "
19058 "- DIE at %s [in module %s]"),
19059 sect_offset_str (die->sect_off),
19060 objfile_name (cu->per_objfile->objfile));
19061
19062 struct attribute *attr_ub, *attr_count;
19063 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
19064 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19065 {
19066 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
19067 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19068 {
19069 /* If bounds are constant do the final calculation here. */
19070 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19071 high.set_const_val (low.const_val () + high.const_val () - 1);
19072 else
19073 high_bound_is_count = 1;
19074 }
19075 else
19076 {
19077 if (attr_ub != NULL)
19078 complaint (_("Unresolved DW_AT_upper_bound "
19079 "- DIE at %s [in module %s]"),
19080 sect_offset_str (die->sect_off),
19081 objfile_name (cu->per_objfile->objfile));
19082 if (attr_count != NULL)
19083 complaint (_("Unresolved DW_AT_count "
19084 "- DIE at %s [in module %s]"),
19085 sect_offset_str (die->sect_off),
19086 objfile_name (cu->per_objfile->objfile));
19087 }
19088 }
19089
19090 LONGEST bias = 0;
19091 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
19092 if (bias_attr != nullptr && bias_attr->form_is_constant ())
19093 bias = bias_attr->constant_value (0);
19094
19095 /* Normally, the DWARF producers are expected to use a signed
19096 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19097 But this is unfortunately not always the case, as witnessed
19098 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19099 is used instead. To work around that ambiguity, we treat
19100 the bounds as signed, and thus sign-extend their values, when
19101 the base type is signed. */
19102 negative_mask =
19103 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19104 if (low.kind () == PROP_CONST
19105 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19106 low.set_const_val (low.const_val () | negative_mask);
19107 if (high.kind () == PROP_CONST
19108 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19109 high.set_const_val (high.const_val () | negative_mask);
19110
19111 /* Check for bit and byte strides. */
19112 struct dynamic_prop byte_stride_prop;
19113 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19114 if (attr_byte_stride != nullptr)
19115 {
19116 struct type *prop_type = cu->addr_sized_int_type (false);
19117 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19118 prop_type);
19119 }
19120
19121 struct dynamic_prop bit_stride_prop;
19122 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19123 if (attr_bit_stride != nullptr)
19124 {
19125 /* It only makes sense to have either a bit or byte stride. */
19126 if (attr_byte_stride != nullptr)
19127 {
19128 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19129 "- DIE at %s [in module %s]"),
19130 sect_offset_str (die->sect_off),
19131 objfile_name (cu->per_objfile->objfile));
19132 attr_bit_stride = nullptr;
19133 }
19134 else
19135 {
19136 struct type *prop_type = cu->addr_sized_int_type (false);
19137 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19138 prop_type);
19139 }
19140 }
19141
19142 if (attr_byte_stride != nullptr
19143 || attr_bit_stride != nullptr)
19144 {
19145 bool byte_stride_p = (attr_byte_stride != nullptr);
19146 struct dynamic_prop *stride
19147 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19148
19149 range_type
19150 = create_range_type_with_stride (NULL, orig_base_type, &low,
19151 &high, bias, stride, byte_stride_p);
19152 }
19153 else
19154 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19155
19156 if (high_bound_is_count)
19157 range_type->bounds ()->flag_upper_bound_is_count = 1;
19158
19159 /* Ada expects an empty array on no boundary attributes. */
19160 if (attr == NULL && cu->language != language_ada)
19161 range_type->bounds ()->high.set_undefined ();
19162
19163 name = dwarf2_name (die, cu);
19164 if (name)
19165 range_type->set_name (name);
19166
19167 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19168 if (attr != nullptr)
19169 TYPE_LENGTH (range_type) = attr->constant_value (0);
19170
19171 maybe_set_alignment (cu, die, range_type);
19172
19173 set_die_type (die, range_type, cu);
19174
19175 /* set_die_type should be already done. */
19176 set_descriptive_type (range_type, die, cu);
19177
19178 return range_type;
19179 }
19180
19181 static struct type *
19182 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19183 {
19184 struct type *type;
19185
19186 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19187 type->set_name (dwarf2_name (die, cu));
19188
19189 /* In Ada, an unspecified type is typically used when the description
19190 of the type is deferred to a different unit. When encountering
19191 such a type, we treat it as a stub, and try to resolve it later on,
19192 when needed. */
19193 if (cu->language == language_ada)
19194 type->set_is_stub (true);
19195
19196 return set_die_type (die, type, cu);
19197 }
19198
19199 /* Read a single die and all its descendents. Set the die's sibling
19200 field to NULL; set other fields in the die correctly, and set all
19201 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19202 location of the info_ptr after reading all of those dies. PARENT
19203 is the parent of the die in question. */
19204
19205 static struct die_info *
19206 read_die_and_children (const struct die_reader_specs *reader,
19207 const gdb_byte *info_ptr,
19208 const gdb_byte **new_info_ptr,
19209 struct die_info *parent)
19210 {
19211 struct die_info *die;
19212 const gdb_byte *cur_ptr;
19213
19214 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19215 if (die == NULL)
19216 {
19217 *new_info_ptr = cur_ptr;
19218 return NULL;
19219 }
19220 store_in_ref_table (die, reader->cu);
19221
19222 if (die->has_children)
19223 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19224 else
19225 {
19226 die->child = NULL;
19227 *new_info_ptr = cur_ptr;
19228 }
19229
19230 die->sibling = NULL;
19231 die->parent = parent;
19232 return die;
19233 }
19234
19235 /* Read a die, all of its descendents, and all of its siblings; set
19236 all of the fields of all of the dies correctly. Arguments are as
19237 in read_die_and_children. */
19238
19239 static struct die_info *
19240 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19241 const gdb_byte *info_ptr,
19242 const gdb_byte **new_info_ptr,
19243 struct die_info *parent)
19244 {
19245 struct die_info *first_die, *last_sibling;
19246 const gdb_byte *cur_ptr;
19247
19248 cur_ptr = info_ptr;
19249 first_die = last_sibling = NULL;
19250
19251 while (1)
19252 {
19253 struct die_info *die
19254 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19255
19256 if (die == NULL)
19257 {
19258 *new_info_ptr = cur_ptr;
19259 return first_die;
19260 }
19261
19262 if (!first_die)
19263 first_die = die;
19264 else
19265 last_sibling->sibling = die;
19266
19267 last_sibling = die;
19268 }
19269 }
19270
19271 /* Read a die, all of its descendents, and all of its siblings; set
19272 all of the fields of all of the dies correctly. Arguments are as
19273 in read_die_and_children.
19274 This the main entry point for reading a DIE and all its children. */
19275
19276 static struct die_info *
19277 read_die_and_siblings (const struct die_reader_specs *reader,
19278 const gdb_byte *info_ptr,
19279 const gdb_byte **new_info_ptr,
19280 struct die_info *parent)
19281 {
19282 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19283 new_info_ptr, parent);
19284
19285 if (dwarf_die_debug)
19286 {
19287 fprintf_unfiltered (gdb_stdlog,
19288 "Read die from %s@0x%x of %s:\n",
19289 reader->die_section->get_name (),
19290 (unsigned) (info_ptr - reader->die_section->buffer),
19291 bfd_get_filename (reader->abfd));
19292 dump_die (die, dwarf_die_debug);
19293 }
19294
19295 return die;
19296 }
19297
19298 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19299 attributes.
19300 The caller is responsible for filling in the extra attributes
19301 and updating (*DIEP)->num_attrs.
19302 Set DIEP to point to a newly allocated die with its information,
19303 except for its child, sibling, and parent fields. */
19304
19305 static const gdb_byte *
19306 read_full_die_1 (const struct die_reader_specs *reader,
19307 struct die_info **diep, const gdb_byte *info_ptr,
19308 int num_extra_attrs)
19309 {
19310 unsigned int abbrev_number, bytes_read, i;
19311 const struct abbrev_info *abbrev;
19312 struct die_info *die;
19313 struct dwarf2_cu *cu = reader->cu;
19314 bfd *abfd = reader->abfd;
19315
19316 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19317 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19318 info_ptr += bytes_read;
19319 if (!abbrev_number)
19320 {
19321 *diep = NULL;
19322 return info_ptr;
19323 }
19324
19325 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19326 if (!abbrev)
19327 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19328 abbrev_number,
19329 bfd_get_filename (abfd));
19330
19331 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19332 die->sect_off = sect_off;
19333 die->tag = abbrev->tag;
19334 die->abbrev = abbrev_number;
19335 die->has_children = abbrev->has_children;
19336
19337 /* Make the result usable.
19338 The caller needs to update num_attrs after adding the extra
19339 attributes. */
19340 die->num_attrs = abbrev->num_attrs;
19341
19342 bool any_need_reprocess = false;
19343 for (i = 0; i < abbrev->num_attrs; ++i)
19344 {
19345 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19346 info_ptr);
19347 if (die->attrs[i].requires_reprocessing_p ())
19348 any_need_reprocess = true;
19349 }
19350
19351 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19352 if (attr != nullptr && attr->form_is_unsigned ())
19353 cu->str_offsets_base = attr->as_unsigned ();
19354
19355 attr = die->attr (DW_AT_loclists_base);
19356 if (attr != nullptr)
19357 cu->loclist_base = attr->as_unsigned ();
19358
19359 auto maybe_addr_base = die->addr_base ();
19360 if (maybe_addr_base.has_value ())
19361 cu->addr_base = *maybe_addr_base;
19362
19363 attr = die->attr (DW_AT_rnglists_base);
19364 if (attr != nullptr)
19365 cu->rnglists_base = attr->as_unsigned ();
19366
19367 if (any_need_reprocess)
19368 {
19369 for (i = 0; i < abbrev->num_attrs; ++i)
19370 {
19371 if (die->attrs[i].requires_reprocessing_p ())
19372 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19373 }
19374 }
19375 *diep = die;
19376 return info_ptr;
19377 }
19378
19379 /* Read a die and all its attributes.
19380 Set DIEP to point to a newly allocated die with its information,
19381 except for its child, sibling, and parent fields. */
19382
19383 static const gdb_byte *
19384 read_full_die (const struct die_reader_specs *reader,
19385 struct die_info **diep, const gdb_byte *info_ptr)
19386 {
19387 const gdb_byte *result;
19388
19389 result = read_full_die_1 (reader, diep, info_ptr, 0);
19390
19391 if (dwarf_die_debug)
19392 {
19393 fprintf_unfiltered (gdb_stdlog,
19394 "Read die from %s@0x%x of %s:\n",
19395 reader->die_section->get_name (),
19396 (unsigned) (info_ptr - reader->die_section->buffer),
19397 bfd_get_filename (reader->abfd));
19398 dump_die (*diep, dwarf_die_debug);
19399 }
19400
19401 return result;
19402 }
19403 \f
19404
19405 /* Returns nonzero if TAG represents a type that we might generate a partial
19406 symbol for. */
19407
19408 static int
19409 is_type_tag_for_partial (int tag, enum language lang)
19410 {
19411 switch (tag)
19412 {
19413 #if 0
19414 /* Some types that would be reasonable to generate partial symbols for,
19415 that we don't at present. Note that normally this does not
19416 matter, mainly because C compilers don't give names to these
19417 types, but instead emit DW_TAG_typedef. */
19418 case DW_TAG_file_type:
19419 case DW_TAG_ptr_to_member_type:
19420 case DW_TAG_set_type:
19421 case DW_TAG_string_type:
19422 case DW_TAG_subroutine_type:
19423 #endif
19424
19425 /* GNAT may emit an array with a name, but no typedef, so we
19426 need to make a symbol in this case. */
19427 case DW_TAG_array_type:
19428 return lang == language_ada;
19429
19430 case DW_TAG_base_type:
19431 case DW_TAG_class_type:
19432 case DW_TAG_interface_type:
19433 case DW_TAG_enumeration_type:
19434 case DW_TAG_structure_type:
19435 case DW_TAG_subrange_type:
19436 case DW_TAG_typedef:
19437 case DW_TAG_union_type:
19438 return 1;
19439 default:
19440 return 0;
19441 }
19442 }
19443
19444 /* Load all DIEs that are interesting for partial symbols into memory. */
19445
19446 static struct partial_die_info *
19447 load_partial_dies (const struct die_reader_specs *reader,
19448 const gdb_byte *info_ptr, int building_psymtab)
19449 {
19450 struct dwarf2_cu *cu = reader->cu;
19451 struct objfile *objfile = cu->per_objfile->objfile;
19452 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19453 unsigned int bytes_read;
19454 unsigned int load_all = 0;
19455 int nesting_level = 1;
19456
19457 parent_die = NULL;
19458 last_die = NULL;
19459
19460 gdb_assert (cu->per_cu != NULL);
19461 if (cu->per_cu->load_all_dies)
19462 load_all = 1;
19463
19464 cu->partial_dies
19465 = htab_create_alloc_ex (cu->header.length / 12,
19466 partial_die_hash,
19467 partial_die_eq,
19468 NULL,
19469 &cu->comp_unit_obstack,
19470 hashtab_obstack_allocate,
19471 dummy_obstack_deallocate);
19472
19473 while (1)
19474 {
19475 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19476 &bytes_read);
19477
19478 /* A NULL abbrev means the end of a series of children. */
19479 if (abbrev == NULL)
19480 {
19481 if (--nesting_level == 0)
19482 return first_die;
19483
19484 info_ptr += bytes_read;
19485 last_die = parent_die;
19486 parent_die = parent_die->die_parent;
19487 continue;
19488 }
19489
19490 /* Check for template arguments. We never save these; if
19491 they're seen, we just mark the parent, and go on our way. */
19492 if (parent_die != NULL
19493 && cu->language == language_cplus
19494 && (abbrev->tag == DW_TAG_template_type_param
19495 || abbrev->tag == DW_TAG_template_value_param))
19496 {
19497 parent_die->has_template_arguments = 1;
19498
19499 if (!load_all)
19500 {
19501 /* We don't need a partial DIE for the template argument. */
19502 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19503 continue;
19504 }
19505 }
19506
19507 /* We only recurse into c++ subprograms looking for template arguments.
19508 Skip their other children. */
19509 if (!load_all
19510 && cu->language == language_cplus
19511 && parent_die != NULL
19512 && parent_die->tag == DW_TAG_subprogram
19513 && abbrev->tag != DW_TAG_inlined_subroutine)
19514 {
19515 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19516 continue;
19517 }
19518
19519 /* Check whether this DIE is interesting enough to save. Normally
19520 we would not be interested in members here, but there may be
19521 later variables referencing them via DW_AT_specification (for
19522 static members). */
19523 if (!load_all
19524 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19525 && abbrev->tag != DW_TAG_constant
19526 && abbrev->tag != DW_TAG_enumerator
19527 && abbrev->tag != DW_TAG_subprogram
19528 && abbrev->tag != DW_TAG_inlined_subroutine
19529 && abbrev->tag != DW_TAG_lexical_block
19530 && abbrev->tag != DW_TAG_variable
19531 && abbrev->tag != DW_TAG_namespace
19532 && abbrev->tag != DW_TAG_module
19533 && abbrev->tag != DW_TAG_member
19534 && abbrev->tag != DW_TAG_imported_unit
19535 && abbrev->tag != DW_TAG_imported_declaration)
19536 {
19537 /* Otherwise we skip to the next sibling, if any. */
19538 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19539 continue;
19540 }
19541
19542 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19543 abbrev);
19544
19545 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19546
19547 /* This two-pass algorithm for processing partial symbols has a
19548 high cost in cache pressure. Thus, handle some simple cases
19549 here which cover the majority of C partial symbols. DIEs
19550 which neither have specification tags in them, nor could have
19551 specification tags elsewhere pointing at them, can simply be
19552 processed and discarded.
19553
19554 This segment is also optional; scan_partial_symbols and
19555 add_partial_symbol will handle these DIEs if we chain
19556 them in normally. When compilers which do not emit large
19557 quantities of duplicate debug information are more common,
19558 this code can probably be removed. */
19559
19560 /* Any complete simple types at the top level (pretty much all
19561 of them, for a language without namespaces), can be processed
19562 directly. */
19563 if (parent_die == NULL
19564 && pdi.has_specification == 0
19565 && pdi.is_declaration == 0
19566 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19567 || pdi.tag == DW_TAG_base_type
19568 || pdi.tag == DW_TAG_array_type
19569 || pdi.tag == DW_TAG_subrange_type))
19570 {
19571 if (building_psymtab && pdi.raw_name != NULL)
19572 add_partial_symbol (&pdi, cu);
19573
19574 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19575 continue;
19576 }
19577
19578 /* The exception for DW_TAG_typedef with has_children above is
19579 a workaround of GCC PR debug/47510. In the case of this complaint
19580 type_name_or_error will error on such types later.
19581
19582 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19583 it could not find the child DIEs referenced later, this is checked
19584 above. In correct DWARF DW_TAG_typedef should have no children. */
19585
19586 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19587 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19588 "- DIE at %s [in module %s]"),
19589 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19590
19591 /* If we're at the second level, and we're an enumerator, and
19592 our parent has no specification (meaning possibly lives in a
19593 namespace elsewhere), then we can add the partial symbol now
19594 instead of queueing it. */
19595 if (pdi.tag == DW_TAG_enumerator
19596 && parent_die != NULL
19597 && parent_die->die_parent == NULL
19598 && parent_die->tag == DW_TAG_enumeration_type
19599 && parent_die->has_specification == 0)
19600 {
19601 if (pdi.raw_name == NULL)
19602 complaint (_("malformed enumerator DIE ignored"));
19603 else if (building_psymtab)
19604 add_partial_symbol (&pdi, cu);
19605
19606 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19607 continue;
19608 }
19609
19610 struct partial_die_info *part_die
19611 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19612
19613 /* We'll save this DIE so link it in. */
19614 part_die->die_parent = parent_die;
19615 part_die->die_sibling = NULL;
19616 part_die->die_child = NULL;
19617
19618 if (last_die && last_die == parent_die)
19619 last_die->die_child = part_die;
19620 else if (last_die)
19621 last_die->die_sibling = part_die;
19622
19623 last_die = part_die;
19624
19625 if (first_die == NULL)
19626 first_die = part_die;
19627
19628 /* Maybe add the DIE to the hash table. Not all DIEs that we
19629 find interesting need to be in the hash table, because we
19630 also have the parent/sibling/child chains; only those that we
19631 might refer to by offset later during partial symbol reading.
19632
19633 For now this means things that might have be the target of a
19634 DW_AT_specification, DW_AT_abstract_origin, or
19635 DW_AT_extension. DW_AT_extension will refer only to
19636 namespaces; DW_AT_abstract_origin refers to functions (and
19637 many things under the function DIE, but we do not recurse
19638 into function DIEs during partial symbol reading) and
19639 possibly variables as well; DW_AT_specification refers to
19640 declarations. Declarations ought to have the DW_AT_declaration
19641 flag. It happens that GCC forgets to put it in sometimes, but
19642 only for functions, not for types.
19643
19644 Adding more things than necessary to the hash table is harmless
19645 except for the performance cost. Adding too few will result in
19646 wasted time in find_partial_die, when we reread the compilation
19647 unit with load_all_dies set. */
19648
19649 if (load_all
19650 || abbrev->tag == DW_TAG_constant
19651 || abbrev->tag == DW_TAG_subprogram
19652 || abbrev->tag == DW_TAG_variable
19653 || abbrev->tag == DW_TAG_namespace
19654 || part_die->is_declaration)
19655 {
19656 void **slot;
19657
19658 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19659 to_underlying (part_die->sect_off),
19660 INSERT);
19661 *slot = part_die;
19662 }
19663
19664 /* For some DIEs we want to follow their children (if any). For C
19665 we have no reason to follow the children of structures; for other
19666 languages we have to, so that we can get at method physnames
19667 to infer fully qualified class names, for DW_AT_specification,
19668 and for C++ template arguments. For C++, we also look one level
19669 inside functions to find template arguments (if the name of the
19670 function does not already contain the template arguments).
19671
19672 For Ada and Fortran, we need to scan the children of subprograms
19673 and lexical blocks as well because these languages allow the
19674 definition of nested entities that could be interesting for the
19675 debugger, such as nested subprograms for instance. */
19676 if (last_die->has_children
19677 && (load_all
19678 || last_die->tag == DW_TAG_namespace
19679 || last_die->tag == DW_TAG_module
19680 || last_die->tag == DW_TAG_enumeration_type
19681 || (cu->language == language_cplus
19682 && last_die->tag == DW_TAG_subprogram
19683 && (last_die->raw_name == NULL
19684 || strchr (last_die->raw_name, '<') == NULL))
19685 || (cu->language != language_c
19686 && (last_die->tag == DW_TAG_class_type
19687 || last_die->tag == DW_TAG_interface_type
19688 || last_die->tag == DW_TAG_structure_type
19689 || last_die->tag == DW_TAG_union_type))
19690 || ((cu->language == language_ada
19691 || cu->language == language_fortran)
19692 && (last_die->tag == DW_TAG_subprogram
19693 || last_die->tag == DW_TAG_lexical_block))))
19694 {
19695 nesting_level++;
19696 parent_die = last_die;
19697 continue;
19698 }
19699
19700 /* Otherwise we skip to the next sibling, if any. */
19701 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19702
19703 /* Back to the top, do it again. */
19704 }
19705 }
19706
19707 partial_die_info::partial_die_info (sect_offset sect_off_,
19708 const struct abbrev_info *abbrev)
19709 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19710 {
19711 }
19712
19713 /* See class definition. */
19714
19715 const char *
19716 partial_die_info::name (dwarf2_cu *cu)
19717 {
19718 if (!canonical_name && raw_name != nullptr)
19719 {
19720 struct objfile *objfile = cu->per_objfile->objfile;
19721 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19722 canonical_name = 1;
19723 }
19724
19725 return raw_name;
19726 }
19727
19728 /* Read a minimal amount of information into the minimal die structure.
19729 INFO_PTR should point just after the initial uleb128 of a DIE. */
19730
19731 const gdb_byte *
19732 partial_die_info::read (const struct die_reader_specs *reader,
19733 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19734 {
19735 struct dwarf2_cu *cu = reader->cu;
19736 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19737 unsigned int i;
19738 int has_low_pc_attr = 0;
19739 int has_high_pc_attr = 0;
19740 int high_pc_relative = 0;
19741
19742 for (i = 0; i < abbrev.num_attrs; ++i)
19743 {
19744 attribute attr;
19745 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19746 /* String and address offsets that need to do the reprocessing have
19747 already been read at this point, so there is no need to wait until
19748 the loop terminates to do the reprocessing. */
19749 if (attr.requires_reprocessing_p ())
19750 read_attribute_reprocess (reader, &attr, tag);
19751 /* Store the data if it is of an attribute we want to keep in a
19752 partial symbol table. */
19753 switch (attr.name)
19754 {
19755 case DW_AT_name:
19756 switch (tag)
19757 {
19758 case DW_TAG_compile_unit:
19759 case DW_TAG_partial_unit:
19760 case DW_TAG_type_unit:
19761 /* Compilation units have a DW_AT_name that is a filename, not
19762 a source language identifier. */
19763 case DW_TAG_enumeration_type:
19764 case DW_TAG_enumerator:
19765 /* These tags always have simple identifiers already; no need
19766 to canonicalize them. */
19767 canonical_name = 1;
19768 raw_name = attr.as_string ();
19769 break;
19770 default:
19771 canonical_name = 0;
19772 raw_name = attr.as_string ();
19773 break;
19774 }
19775 break;
19776 case DW_AT_linkage_name:
19777 case DW_AT_MIPS_linkage_name:
19778 /* Note that both forms of linkage name might appear. We
19779 assume they will be the same, and we only store the last
19780 one we see. */
19781 linkage_name = attr.as_string ();
19782 break;
19783 case DW_AT_low_pc:
19784 has_low_pc_attr = 1;
19785 lowpc = attr.as_address ();
19786 break;
19787 case DW_AT_high_pc:
19788 has_high_pc_attr = 1;
19789 highpc = attr.as_address ();
19790 if (cu->header.version >= 4 && attr.form_is_constant ())
19791 high_pc_relative = 1;
19792 break;
19793 case DW_AT_location:
19794 /* Support the .debug_loc offsets. */
19795 if (attr.form_is_block ())
19796 {
19797 d.locdesc = attr.as_block ();
19798 }
19799 else if (attr.form_is_section_offset ())
19800 {
19801 dwarf2_complex_location_expr_complaint ();
19802 }
19803 else
19804 {
19805 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19806 "partial symbol information");
19807 }
19808 break;
19809 case DW_AT_external:
19810 is_external = attr.as_boolean ();
19811 break;
19812 case DW_AT_declaration:
19813 is_declaration = attr.as_boolean ();
19814 break;
19815 case DW_AT_type:
19816 has_type = 1;
19817 break;
19818 case DW_AT_abstract_origin:
19819 case DW_AT_specification:
19820 case DW_AT_extension:
19821 has_specification = 1;
19822 spec_offset = attr.get_ref_die_offset ();
19823 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19824 || cu->per_cu->is_dwz);
19825 break;
19826 case DW_AT_sibling:
19827 /* Ignore absolute siblings, they might point outside of
19828 the current compile unit. */
19829 if (attr.form == DW_FORM_ref_addr)
19830 complaint (_("ignoring absolute DW_AT_sibling"));
19831 else
19832 {
19833 const gdb_byte *buffer = reader->buffer;
19834 sect_offset off = attr.get_ref_die_offset ();
19835 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19836
19837 if (sibling_ptr < info_ptr)
19838 complaint (_("DW_AT_sibling points backwards"));
19839 else if (sibling_ptr > reader->buffer_end)
19840 reader->die_section->overflow_complaint ();
19841 else
19842 sibling = sibling_ptr;
19843 }
19844 break;
19845 case DW_AT_byte_size:
19846 has_byte_size = 1;
19847 break;
19848 case DW_AT_const_value:
19849 has_const_value = 1;
19850 break;
19851 case DW_AT_calling_convention:
19852 /* DWARF doesn't provide a way to identify a program's source-level
19853 entry point. DW_AT_calling_convention attributes are only meant
19854 to describe functions' calling conventions.
19855
19856 However, because it's a necessary piece of information in
19857 Fortran, and before DWARF 4 DW_CC_program was the only
19858 piece of debugging information whose definition refers to
19859 a 'main program' at all, several compilers marked Fortran
19860 main programs with DW_CC_program --- even when those
19861 functions use the standard calling conventions.
19862
19863 Although DWARF now specifies a way to provide this
19864 information, we support this practice for backward
19865 compatibility. */
19866 if (attr.constant_value (0) == DW_CC_program
19867 && cu->language == language_fortran)
19868 main_subprogram = 1;
19869 break;
19870 case DW_AT_inline:
19871 {
19872 LONGEST value = attr.constant_value (-1);
19873 if (value == DW_INL_inlined
19874 || value == DW_INL_declared_inlined)
19875 may_be_inlined = 1;
19876 }
19877 break;
19878
19879 case DW_AT_import:
19880 if (tag == DW_TAG_imported_unit)
19881 {
19882 d.sect_off = attr.get_ref_die_offset ();
19883 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19884 || cu->per_cu->is_dwz);
19885 }
19886 break;
19887
19888 case DW_AT_main_subprogram:
19889 main_subprogram = attr.as_boolean ();
19890 break;
19891
19892 case DW_AT_ranges:
19893 {
19894 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19895 on DWARF version). */
19896 ULONGEST ranges_offset = attr.as_unsigned ();
19897
19898 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19899 this value. */
19900 if (tag != DW_TAG_compile_unit)
19901 ranges_offset += cu->gnu_ranges_base;
19902
19903 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19904 nullptr, tag))
19905 has_pc_info = 1;
19906 }
19907 break;
19908
19909 default:
19910 break;
19911 }
19912 }
19913
19914 /* For Ada, if both the name and the linkage name appear, we prefer
19915 the latter. This lets "catch exception" work better, regardless
19916 of the order in which the name and linkage name were emitted.
19917 Really, though, this is just a workaround for the fact that gdb
19918 doesn't store both the name and the linkage name. */
19919 if (cu->language == language_ada && linkage_name != nullptr)
19920 raw_name = linkage_name;
19921
19922 if (high_pc_relative)
19923 highpc += lowpc;
19924
19925 if (has_low_pc_attr && has_high_pc_attr)
19926 {
19927 /* When using the GNU linker, .gnu.linkonce. sections are used to
19928 eliminate duplicate copies of functions and vtables and such.
19929 The linker will arbitrarily choose one and discard the others.
19930 The AT_*_pc values for such functions refer to local labels in
19931 these sections. If the section from that file was discarded, the
19932 labels are not in the output, so the relocs get a value of 0.
19933 If this is a discarded function, mark the pc bounds as invalid,
19934 so that GDB will ignore it. */
19935 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19936 {
19937 struct objfile *objfile = per_objfile->objfile;
19938 struct gdbarch *gdbarch = objfile->arch ();
19939
19940 complaint (_("DW_AT_low_pc %s is zero "
19941 "for DIE at %s [in module %s]"),
19942 paddress (gdbarch, lowpc),
19943 sect_offset_str (sect_off),
19944 objfile_name (objfile));
19945 }
19946 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19947 else if (lowpc >= highpc)
19948 {
19949 struct objfile *objfile = per_objfile->objfile;
19950 struct gdbarch *gdbarch = objfile->arch ();
19951
19952 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19953 "for DIE at %s [in module %s]"),
19954 paddress (gdbarch, lowpc),
19955 paddress (gdbarch, highpc),
19956 sect_offset_str (sect_off),
19957 objfile_name (objfile));
19958 }
19959 else
19960 has_pc_info = 1;
19961 }
19962
19963 return info_ptr;
19964 }
19965
19966 /* Find a cached partial DIE at OFFSET in CU. */
19967
19968 struct partial_die_info *
19969 dwarf2_cu::find_partial_die (sect_offset sect_off)
19970 {
19971 struct partial_die_info *lookup_die = NULL;
19972 struct partial_die_info part_die (sect_off);
19973
19974 lookup_die = ((struct partial_die_info *)
19975 htab_find_with_hash (partial_dies, &part_die,
19976 to_underlying (sect_off)));
19977
19978 return lookup_die;
19979 }
19980
19981 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19982 except in the case of .debug_types DIEs which do not reference
19983 outside their CU (they do however referencing other types via
19984 DW_FORM_ref_sig8). */
19985
19986 static const struct cu_partial_die_info
19987 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19988 {
19989 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19990 struct objfile *objfile = per_objfile->objfile;
19991 struct partial_die_info *pd = NULL;
19992
19993 if (offset_in_dwz == cu->per_cu->is_dwz
19994 && cu->header.offset_in_cu_p (sect_off))
19995 {
19996 pd = cu->find_partial_die (sect_off);
19997 if (pd != NULL)
19998 return { cu, pd };
19999 /* We missed recording what we needed.
20000 Load all dies and try again. */
20001 }
20002 else
20003 {
20004 /* TUs don't reference other CUs/TUs (except via type signatures). */
20005 if (cu->per_cu->is_debug_types)
20006 {
20007 error (_("Dwarf Error: Type Unit at offset %s contains"
20008 " external reference to offset %s [in module %s].\n"),
20009 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
20010 bfd_get_filename (objfile->obfd));
20011 }
20012 dwarf2_per_cu_data *per_cu
20013 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20014 per_objfile);
20015
20016 cu = per_objfile->get_cu (per_cu);
20017 if (cu == NULL || cu->partial_dies == NULL)
20018 load_partial_comp_unit (per_cu, per_objfile, nullptr);
20019
20020 cu = per_objfile->get_cu (per_cu);
20021
20022 cu->last_used = 0;
20023 pd = cu->find_partial_die (sect_off);
20024 }
20025
20026 /* If we didn't find it, and not all dies have been loaded,
20027 load them all and try again. */
20028
20029 if (pd == NULL && cu->per_cu->load_all_dies == 0)
20030 {
20031 cu->per_cu->load_all_dies = 1;
20032
20033 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20034 THIS_CU->cu may already be in use. So we can't just free it and
20035 replace its DIEs with the ones we read in. Instead, we leave those
20036 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20037 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20038 set. */
20039 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
20040
20041 pd = cu->find_partial_die (sect_off);
20042 }
20043
20044 if (pd == NULL)
20045 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
20046 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
20047 return { cu, pd };
20048 }
20049
20050 /* See if we can figure out if the class lives in a namespace. We do
20051 this by looking for a member function; its demangled name will
20052 contain namespace info, if there is any. */
20053
20054 static void
20055 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20056 struct dwarf2_cu *cu)
20057 {
20058 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20059 what template types look like, because the demangler
20060 frequently doesn't give the same name as the debug info. We
20061 could fix this by only using the demangled name to get the
20062 prefix (but see comment in read_structure_type). */
20063
20064 struct partial_die_info *real_pdi;
20065 struct partial_die_info *child_pdi;
20066
20067 /* If this DIE (this DIE's specification, if any) has a parent, then
20068 we should not do this. We'll prepend the parent's fully qualified
20069 name when we create the partial symbol. */
20070
20071 real_pdi = struct_pdi;
20072 while (real_pdi->has_specification)
20073 {
20074 auto res = find_partial_die (real_pdi->spec_offset,
20075 real_pdi->spec_is_dwz, cu);
20076 real_pdi = res.pdi;
20077 cu = res.cu;
20078 }
20079
20080 if (real_pdi->die_parent != NULL)
20081 return;
20082
20083 for (child_pdi = struct_pdi->die_child;
20084 child_pdi != NULL;
20085 child_pdi = child_pdi->die_sibling)
20086 {
20087 if (child_pdi->tag == DW_TAG_subprogram
20088 && child_pdi->linkage_name != NULL)
20089 {
20090 gdb::unique_xmalloc_ptr<char> actual_class_name
20091 (cu->language_defn->class_name_from_physname
20092 (child_pdi->linkage_name));
20093 if (actual_class_name != NULL)
20094 {
20095 struct objfile *objfile = cu->per_objfile->objfile;
20096 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20097 struct_pdi->canonical_name = 1;
20098 }
20099 break;
20100 }
20101 }
20102 }
20103
20104 /* Return true if a DIE with TAG may have the DW_AT_const_value
20105 attribute. */
20106
20107 static bool
20108 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20109 {
20110 switch (tag)
20111 {
20112 case DW_TAG_constant:
20113 case DW_TAG_enumerator:
20114 case DW_TAG_formal_parameter:
20115 case DW_TAG_template_value_param:
20116 case DW_TAG_variable:
20117 return true;
20118 }
20119
20120 return false;
20121 }
20122
20123 void
20124 partial_die_info::fixup (struct dwarf2_cu *cu)
20125 {
20126 /* Once we've fixed up a die, there's no point in doing so again.
20127 This also avoids a memory leak if we were to call
20128 guess_partial_die_structure_name multiple times. */
20129 if (fixup_called)
20130 return;
20131
20132 /* If we found a reference attribute and the DIE has no name, try
20133 to find a name in the referred to DIE. */
20134
20135 if (raw_name == NULL && has_specification)
20136 {
20137 struct partial_die_info *spec_die;
20138
20139 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20140 spec_die = res.pdi;
20141 cu = res.cu;
20142
20143 spec_die->fixup (cu);
20144
20145 if (spec_die->raw_name)
20146 {
20147 raw_name = spec_die->raw_name;
20148 canonical_name = spec_die->canonical_name;
20149
20150 /* Copy DW_AT_external attribute if it is set. */
20151 if (spec_die->is_external)
20152 is_external = spec_die->is_external;
20153 }
20154 }
20155
20156 if (!has_const_value && has_specification
20157 && can_have_DW_AT_const_value_p (tag))
20158 {
20159 struct partial_die_info *spec_die;
20160
20161 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20162 spec_die = res.pdi;
20163 cu = res.cu;
20164
20165 spec_die->fixup (cu);
20166
20167 if (spec_die->has_const_value)
20168 {
20169 /* Copy DW_AT_const_value attribute if it is set. */
20170 has_const_value = spec_die->has_const_value;
20171 }
20172 }
20173
20174 /* Set default names for some unnamed DIEs. */
20175
20176 if (raw_name == NULL && tag == DW_TAG_namespace)
20177 {
20178 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20179 canonical_name = 1;
20180 }
20181
20182 /* If there is no parent die to provide a namespace, and there are
20183 children, see if we can determine the namespace from their linkage
20184 name. */
20185 if (cu->language == language_cplus
20186 && !cu->per_objfile->per_bfd->types.empty ()
20187 && die_parent == NULL
20188 && has_children
20189 && (tag == DW_TAG_class_type
20190 || tag == DW_TAG_structure_type
20191 || tag == DW_TAG_union_type))
20192 guess_partial_die_structure_name (this, cu);
20193
20194 /* GCC might emit a nameless struct or union that has a linkage
20195 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20196 if (raw_name == NULL
20197 && (tag == DW_TAG_class_type
20198 || tag == DW_TAG_interface_type
20199 || tag == DW_TAG_structure_type
20200 || tag == DW_TAG_union_type)
20201 && linkage_name != NULL)
20202 {
20203 gdb::unique_xmalloc_ptr<char> demangled
20204 (gdb_demangle (linkage_name, DMGL_TYPES));
20205 if (demangled != nullptr)
20206 {
20207 const char *base;
20208
20209 /* Strip any leading namespaces/classes, keep only the base name.
20210 DW_AT_name for named DIEs does not contain the prefixes. */
20211 base = strrchr (demangled.get (), ':');
20212 if (base && base > demangled.get () && base[-1] == ':')
20213 base++;
20214 else
20215 base = demangled.get ();
20216
20217 struct objfile *objfile = cu->per_objfile->objfile;
20218 raw_name = objfile->intern (base);
20219 canonical_name = 1;
20220 }
20221 }
20222
20223 fixup_called = 1;
20224 }
20225
20226 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20227 contents from the given SECTION in the HEADER.
20228
20229 HEADER_OFFSET is the offset of the header in the section. */
20230 static void
20231 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20232 struct dwarf2_section_info *section,
20233 sect_offset header_offset)
20234 {
20235 unsigned int bytes_read;
20236 bfd *abfd = section->get_bfd_owner ();
20237 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20238
20239 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20240 info_ptr += bytes_read;
20241
20242 header->version = read_2_bytes (abfd, info_ptr);
20243 info_ptr += 2;
20244
20245 header->addr_size = read_1_byte (abfd, info_ptr);
20246 info_ptr += 1;
20247
20248 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20249 info_ptr += 1;
20250
20251 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20252 }
20253
20254 /* Return the DW_AT_loclists_base value for the CU. */
20255 static ULONGEST
20256 lookup_loclist_base (struct dwarf2_cu *cu)
20257 {
20258 /* For the .dwo unit, the loclist_base points to the first offset following
20259 the header. The header consists of the following entities-
20260 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20261 bit format)
20262 2. version (2 bytes)
20263 3. address size (1 byte)
20264 4. segment selector size (1 byte)
20265 5. offset entry count (4 bytes)
20266 These sizes are derived as per the DWARFv5 standard. */
20267 if (cu->dwo_unit != nullptr)
20268 {
20269 if (cu->header.initial_length_size == 4)
20270 return LOCLIST_HEADER_SIZE32;
20271 return LOCLIST_HEADER_SIZE64;
20272 }
20273 return cu->loclist_base;
20274 }
20275
20276 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20277 array of offsets in the .debug_loclists section. */
20278
20279 static sect_offset
20280 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20281 {
20282 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20283 struct objfile *objfile = per_objfile->objfile;
20284 bfd *abfd = objfile->obfd;
20285 ULONGEST loclist_header_size =
20286 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20287 : LOCLIST_HEADER_SIZE64);
20288 ULONGEST loclist_base = lookup_loclist_base (cu);
20289
20290 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20291 ULONGEST start_offset =
20292 loclist_base + loclist_index * cu->header.offset_size;
20293
20294 /* Get loclists section. */
20295 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20296
20297 /* Read the loclists section content. */
20298 section->read (objfile);
20299 if (section->buffer == NULL)
20300 error (_("DW_FORM_loclistx used without .debug_loclists "
20301 "section [in module %s]"), objfile_name (objfile));
20302
20303 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20304 so if loclist_base is smaller than the header size, we have a problem. */
20305 if (loclist_base < loclist_header_size)
20306 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20307 objfile_name (objfile));
20308
20309 /* Read the header of the loclists contribution. */
20310 struct loclists_rnglists_header header;
20311 read_loclists_rnglists_header (&header, section,
20312 (sect_offset) (loclist_base - loclist_header_size));
20313
20314 /* Verify the loclist index is valid. */
20315 if (loclist_index >= header.offset_entry_count)
20316 error (_("DW_FORM_loclistx pointing outside of "
20317 ".debug_loclists offset array [in module %s]"),
20318 objfile_name (objfile));
20319
20320 /* Validate that reading won't go beyond the end of the section. */
20321 if (start_offset + cu->header.offset_size > section->size)
20322 error (_("Reading DW_FORM_loclistx index beyond end of"
20323 ".debug_loclists section [in module %s]"),
20324 objfile_name (objfile));
20325
20326 const gdb_byte *info_ptr = section->buffer + start_offset;
20327
20328 if (cu->header.offset_size == 4)
20329 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20330 else
20331 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20332 }
20333
20334 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20335 array of offsets in the .debug_rnglists section. */
20336
20337 static sect_offset
20338 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20339 dwarf_tag tag)
20340 {
20341 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20342 struct objfile *objfile = dwarf2_per_objfile->objfile;
20343 bfd *abfd = objfile->obfd;
20344 ULONGEST rnglist_header_size =
20345 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20346 : RNGLIST_HEADER_SIZE64);
20347
20348 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20349 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20350 doesn't apply. */
20351 ULONGEST rnglist_base =
20352 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20353
20354 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20355 ULONGEST start_offset =
20356 rnglist_base + rnglist_index * cu->header.offset_size;
20357
20358 /* Get rnglists section. */
20359 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20360
20361 /* Read the rnglists section content. */
20362 section->read (objfile);
20363 if (section->buffer == nullptr)
20364 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20365 "[in module %s]"),
20366 objfile_name (objfile));
20367
20368 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20369 so if rnglist_base is smaller than the header size, we have a problem. */
20370 if (rnglist_base < rnglist_header_size)
20371 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20372 objfile_name (objfile));
20373
20374 /* Read the header of the rnglists contribution. */
20375 struct loclists_rnglists_header header;
20376 read_loclists_rnglists_header (&header, section,
20377 (sect_offset) (rnglist_base - rnglist_header_size));
20378
20379 /* Verify the rnglist index is valid. */
20380 if (rnglist_index >= header.offset_entry_count)
20381 error (_("DW_FORM_rnglistx index pointing outside of "
20382 ".debug_rnglists offset array [in module %s]"),
20383 objfile_name (objfile));
20384
20385 /* Validate that reading won't go beyond the end of the section. */
20386 if (start_offset + cu->header.offset_size > section->size)
20387 error (_("Reading DW_FORM_rnglistx index beyond end of"
20388 ".debug_rnglists section [in module %s]"),
20389 objfile_name (objfile));
20390
20391 const gdb_byte *info_ptr = section->buffer + start_offset;
20392
20393 if (cu->header.offset_size == 4)
20394 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20395 else
20396 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20397 }
20398
20399 /* Process the attributes that had to be skipped in the first round. These
20400 attributes are the ones that need str_offsets_base or addr_base attributes.
20401 They could not have been processed in the first round, because at the time
20402 the values of str_offsets_base or addr_base may not have been known. */
20403 static void
20404 read_attribute_reprocess (const struct die_reader_specs *reader,
20405 struct attribute *attr, dwarf_tag tag)
20406 {
20407 struct dwarf2_cu *cu = reader->cu;
20408 switch (attr->form)
20409 {
20410 case DW_FORM_addrx:
20411 case DW_FORM_GNU_addr_index:
20412 attr->set_address (read_addr_index (cu,
20413 attr->as_unsigned_reprocess ()));
20414 break;
20415 case DW_FORM_loclistx:
20416 {
20417 sect_offset loclists_sect_off
20418 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20419
20420 attr->set_unsigned (to_underlying (loclists_sect_off));
20421 }
20422 break;
20423 case DW_FORM_rnglistx:
20424 {
20425 sect_offset rnglists_sect_off
20426 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20427
20428 attr->set_unsigned (to_underlying (rnglists_sect_off));
20429 }
20430 break;
20431 case DW_FORM_strx:
20432 case DW_FORM_strx1:
20433 case DW_FORM_strx2:
20434 case DW_FORM_strx3:
20435 case DW_FORM_strx4:
20436 case DW_FORM_GNU_str_index:
20437 {
20438 unsigned int str_index = attr->as_unsigned_reprocess ();
20439 gdb_assert (!attr->canonical_string_p ());
20440 if (reader->dwo_file != NULL)
20441 attr->set_string_noncanonical (read_dwo_str_index (reader,
20442 str_index));
20443 else
20444 attr->set_string_noncanonical (read_stub_str_index (cu,
20445 str_index));
20446 break;
20447 }
20448 default:
20449 gdb_assert_not_reached (_("Unexpected DWARF form."));
20450 }
20451 }
20452
20453 /* Read an attribute value described by an attribute form. */
20454
20455 static const gdb_byte *
20456 read_attribute_value (const struct die_reader_specs *reader,
20457 struct attribute *attr, unsigned form,
20458 LONGEST implicit_const, const gdb_byte *info_ptr)
20459 {
20460 struct dwarf2_cu *cu = reader->cu;
20461 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20462 struct objfile *objfile = per_objfile->objfile;
20463 bfd *abfd = reader->abfd;
20464 struct comp_unit_head *cu_header = &cu->header;
20465 unsigned int bytes_read;
20466 struct dwarf_block *blk;
20467
20468 attr->form = (enum dwarf_form) form;
20469 switch (form)
20470 {
20471 case DW_FORM_ref_addr:
20472 if (cu_header->version == 2)
20473 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20474 &bytes_read));
20475 else
20476 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20477 &bytes_read));
20478 info_ptr += bytes_read;
20479 break;
20480 case DW_FORM_GNU_ref_alt:
20481 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20482 &bytes_read));
20483 info_ptr += bytes_read;
20484 break;
20485 case DW_FORM_addr:
20486 {
20487 struct gdbarch *gdbarch = objfile->arch ();
20488 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20489 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20490 attr->set_address (addr);
20491 info_ptr += bytes_read;
20492 }
20493 break;
20494 case DW_FORM_block2:
20495 blk = dwarf_alloc_block (cu);
20496 blk->size = read_2_bytes (abfd, info_ptr);
20497 info_ptr += 2;
20498 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20499 info_ptr += blk->size;
20500 attr->set_block (blk);
20501 break;
20502 case DW_FORM_block4:
20503 blk = dwarf_alloc_block (cu);
20504 blk->size = read_4_bytes (abfd, info_ptr);
20505 info_ptr += 4;
20506 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20507 info_ptr += blk->size;
20508 attr->set_block (blk);
20509 break;
20510 case DW_FORM_data2:
20511 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20512 info_ptr += 2;
20513 break;
20514 case DW_FORM_data4:
20515 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20516 info_ptr += 4;
20517 break;
20518 case DW_FORM_data8:
20519 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20520 info_ptr += 8;
20521 break;
20522 case DW_FORM_data16:
20523 blk = dwarf_alloc_block (cu);
20524 blk->size = 16;
20525 blk->data = read_n_bytes (abfd, info_ptr, 16);
20526 info_ptr += 16;
20527 attr->set_block (blk);
20528 break;
20529 case DW_FORM_sec_offset:
20530 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20531 &bytes_read));
20532 info_ptr += bytes_read;
20533 break;
20534 case DW_FORM_loclistx:
20535 {
20536 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20537 &bytes_read));
20538 info_ptr += bytes_read;
20539 }
20540 break;
20541 case DW_FORM_string:
20542 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20543 &bytes_read));
20544 info_ptr += bytes_read;
20545 break;
20546 case DW_FORM_strp:
20547 if (!cu->per_cu->is_dwz)
20548 {
20549 attr->set_string_noncanonical
20550 (read_indirect_string (per_objfile,
20551 abfd, info_ptr, cu_header,
20552 &bytes_read));
20553 info_ptr += bytes_read;
20554 break;
20555 }
20556 /* FALLTHROUGH */
20557 case DW_FORM_line_strp:
20558 if (!cu->per_cu->is_dwz)
20559 {
20560 attr->set_string_noncanonical
20561 (per_objfile->read_line_string (info_ptr, cu_header,
20562 &bytes_read));
20563 info_ptr += bytes_read;
20564 break;
20565 }
20566 /* FALLTHROUGH */
20567 case DW_FORM_GNU_strp_alt:
20568 {
20569 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20570 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20571 &bytes_read);
20572
20573 attr->set_string_noncanonical
20574 (dwz->read_string (objfile, str_offset));
20575 info_ptr += bytes_read;
20576 }
20577 break;
20578 case DW_FORM_exprloc:
20579 case DW_FORM_block:
20580 blk = dwarf_alloc_block (cu);
20581 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20582 info_ptr += bytes_read;
20583 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20584 info_ptr += blk->size;
20585 attr->set_block (blk);
20586 break;
20587 case DW_FORM_block1:
20588 blk = dwarf_alloc_block (cu);
20589 blk->size = read_1_byte (abfd, info_ptr);
20590 info_ptr += 1;
20591 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20592 info_ptr += blk->size;
20593 attr->set_block (blk);
20594 break;
20595 case DW_FORM_data1:
20596 case DW_FORM_flag:
20597 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20598 info_ptr += 1;
20599 break;
20600 case DW_FORM_flag_present:
20601 attr->set_unsigned (1);
20602 break;
20603 case DW_FORM_sdata:
20604 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20605 info_ptr += bytes_read;
20606 break;
20607 case DW_FORM_rnglistx:
20608 {
20609 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20610 &bytes_read));
20611 info_ptr += bytes_read;
20612 }
20613 break;
20614 case DW_FORM_udata:
20615 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20616 info_ptr += bytes_read;
20617 break;
20618 case DW_FORM_ref1:
20619 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20620 + read_1_byte (abfd, info_ptr)));
20621 info_ptr += 1;
20622 break;
20623 case DW_FORM_ref2:
20624 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20625 + read_2_bytes (abfd, info_ptr)));
20626 info_ptr += 2;
20627 break;
20628 case DW_FORM_ref4:
20629 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20630 + read_4_bytes (abfd, info_ptr)));
20631 info_ptr += 4;
20632 break;
20633 case DW_FORM_ref8:
20634 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20635 + read_8_bytes (abfd, info_ptr)));
20636 info_ptr += 8;
20637 break;
20638 case DW_FORM_ref_sig8:
20639 attr->set_signature (read_8_bytes (abfd, info_ptr));
20640 info_ptr += 8;
20641 break;
20642 case DW_FORM_ref_udata:
20643 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20644 + read_unsigned_leb128 (abfd, info_ptr,
20645 &bytes_read)));
20646 info_ptr += bytes_read;
20647 break;
20648 case DW_FORM_indirect:
20649 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20650 info_ptr += bytes_read;
20651 if (form == DW_FORM_implicit_const)
20652 {
20653 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20654 info_ptr += bytes_read;
20655 }
20656 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20657 info_ptr);
20658 break;
20659 case DW_FORM_implicit_const:
20660 attr->set_signed (implicit_const);
20661 break;
20662 case DW_FORM_addrx:
20663 case DW_FORM_GNU_addr_index:
20664 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20665 &bytes_read));
20666 info_ptr += bytes_read;
20667 break;
20668 case DW_FORM_strx:
20669 case DW_FORM_strx1:
20670 case DW_FORM_strx2:
20671 case DW_FORM_strx3:
20672 case DW_FORM_strx4:
20673 case DW_FORM_GNU_str_index:
20674 {
20675 ULONGEST str_index;
20676 if (form == DW_FORM_strx1)
20677 {
20678 str_index = read_1_byte (abfd, info_ptr);
20679 info_ptr += 1;
20680 }
20681 else if (form == DW_FORM_strx2)
20682 {
20683 str_index = read_2_bytes (abfd, info_ptr);
20684 info_ptr += 2;
20685 }
20686 else if (form == DW_FORM_strx3)
20687 {
20688 str_index = read_3_bytes (abfd, info_ptr);
20689 info_ptr += 3;
20690 }
20691 else if (form == DW_FORM_strx4)
20692 {
20693 str_index = read_4_bytes (abfd, info_ptr);
20694 info_ptr += 4;
20695 }
20696 else
20697 {
20698 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20699 info_ptr += bytes_read;
20700 }
20701 attr->set_unsigned_reprocess (str_index);
20702 }
20703 break;
20704 default:
20705 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20706 dwarf_form_name (form),
20707 bfd_get_filename (abfd));
20708 }
20709
20710 /* Super hack. */
20711 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20712 attr->form = DW_FORM_GNU_ref_alt;
20713
20714 /* We have seen instances where the compiler tried to emit a byte
20715 size attribute of -1 which ended up being encoded as an unsigned
20716 0xffffffff. Although 0xffffffff is technically a valid size value,
20717 an object of this size seems pretty unlikely so we can relatively
20718 safely treat these cases as if the size attribute was invalid and
20719 treat them as zero by default. */
20720 if (attr->name == DW_AT_byte_size
20721 && form == DW_FORM_data4
20722 && attr->as_unsigned () >= 0xffffffff)
20723 {
20724 complaint
20725 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20726 hex_string (attr->as_unsigned ()));
20727 attr->set_unsigned (0);
20728 }
20729
20730 return info_ptr;
20731 }
20732
20733 /* Read an attribute described by an abbreviated attribute. */
20734
20735 static const gdb_byte *
20736 read_attribute (const struct die_reader_specs *reader,
20737 struct attribute *attr, const struct attr_abbrev *abbrev,
20738 const gdb_byte *info_ptr)
20739 {
20740 attr->name = abbrev->name;
20741 attr->string_is_canonical = 0;
20742 attr->requires_reprocessing = 0;
20743 return read_attribute_value (reader, attr, abbrev->form,
20744 abbrev->implicit_const, info_ptr);
20745 }
20746
20747 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20748
20749 static const char *
20750 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20751 LONGEST str_offset)
20752 {
20753 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20754 str_offset, "DW_FORM_strp");
20755 }
20756
20757 /* Return pointer to string at .debug_str offset as read from BUF.
20758 BUF is assumed to be in a compilation unit described by CU_HEADER.
20759 Return *BYTES_READ_PTR count of bytes read from BUF. */
20760
20761 static const char *
20762 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20763 const gdb_byte *buf,
20764 const struct comp_unit_head *cu_header,
20765 unsigned int *bytes_read_ptr)
20766 {
20767 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20768
20769 return read_indirect_string_at_offset (per_objfile, str_offset);
20770 }
20771
20772 /* See read.h. */
20773
20774 const char *
20775 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20776 const struct comp_unit_head *cu_header,
20777 unsigned int *bytes_read_ptr)
20778 {
20779 bfd *abfd = objfile->obfd;
20780 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20781
20782 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20783 }
20784
20785 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20786 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20787 ADDR_SIZE is the size of addresses from the CU header. */
20788
20789 static CORE_ADDR
20790 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20791 gdb::optional<ULONGEST> addr_base, int addr_size)
20792 {
20793 struct objfile *objfile = per_objfile->objfile;
20794 bfd *abfd = objfile->obfd;
20795 const gdb_byte *info_ptr;
20796 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20797
20798 per_objfile->per_bfd->addr.read (objfile);
20799 if (per_objfile->per_bfd->addr.buffer == NULL)
20800 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20801 objfile_name (objfile));
20802 if (addr_base_or_zero + addr_index * addr_size
20803 >= per_objfile->per_bfd->addr.size)
20804 error (_("DW_FORM_addr_index pointing outside of "
20805 ".debug_addr section [in module %s]"),
20806 objfile_name (objfile));
20807 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20808 + addr_index * addr_size);
20809 if (addr_size == 4)
20810 return bfd_get_32 (abfd, info_ptr);
20811 else
20812 return bfd_get_64 (abfd, info_ptr);
20813 }
20814
20815 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20816
20817 static CORE_ADDR
20818 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20819 {
20820 return read_addr_index_1 (cu->per_objfile, addr_index,
20821 cu->addr_base, cu->header.addr_size);
20822 }
20823
20824 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20825
20826 static CORE_ADDR
20827 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20828 unsigned int *bytes_read)
20829 {
20830 bfd *abfd = cu->per_objfile->objfile->obfd;
20831 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20832
20833 return read_addr_index (cu, addr_index);
20834 }
20835
20836 /* See read.h. */
20837
20838 CORE_ADDR
20839 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20840 dwarf2_per_objfile *per_objfile,
20841 unsigned int addr_index)
20842 {
20843 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20844 gdb::optional<ULONGEST> addr_base;
20845 int addr_size;
20846
20847 /* We need addr_base and addr_size.
20848 If we don't have PER_CU->cu, we have to get it.
20849 Nasty, but the alternative is storing the needed info in PER_CU,
20850 which at this point doesn't seem justified: it's not clear how frequently
20851 it would get used and it would increase the size of every PER_CU.
20852 Entry points like dwarf2_per_cu_addr_size do a similar thing
20853 so we're not in uncharted territory here.
20854 Alas we need to be a bit more complicated as addr_base is contained
20855 in the DIE.
20856
20857 We don't need to read the entire CU(/TU).
20858 We just need the header and top level die.
20859
20860 IWBN to use the aging mechanism to let us lazily later discard the CU.
20861 For now we skip this optimization. */
20862
20863 if (cu != NULL)
20864 {
20865 addr_base = cu->addr_base;
20866 addr_size = cu->header.addr_size;
20867 }
20868 else
20869 {
20870 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20871 addr_base = reader.cu->addr_base;
20872 addr_size = reader.cu->header.addr_size;
20873 }
20874
20875 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20876 }
20877
20878 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20879 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20880 DWO file. */
20881
20882 static const char *
20883 read_str_index (struct dwarf2_cu *cu,
20884 struct dwarf2_section_info *str_section,
20885 struct dwarf2_section_info *str_offsets_section,
20886 ULONGEST str_offsets_base, ULONGEST str_index)
20887 {
20888 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20889 struct objfile *objfile = per_objfile->objfile;
20890 const char *objf_name = objfile_name (objfile);
20891 bfd *abfd = objfile->obfd;
20892 const gdb_byte *info_ptr;
20893 ULONGEST str_offset;
20894 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20895
20896 str_section->read (objfile);
20897 str_offsets_section->read (objfile);
20898 if (str_section->buffer == NULL)
20899 error (_("%s used without %s section"
20900 " in CU at offset %s [in module %s]"),
20901 form_name, str_section->get_name (),
20902 sect_offset_str (cu->header.sect_off), objf_name);
20903 if (str_offsets_section->buffer == NULL)
20904 error (_("%s used without %s section"
20905 " in CU at offset %s [in module %s]"),
20906 form_name, str_section->get_name (),
20907 sect_offset_str (cu->header.sect_off), objf_name);
20908 info_ptr = (str_offsets_section->buffer
20909 + str_offsets_base
20910 + str_index * cu->header.offset_size);
20911 if (cu->header.offset_size == 4)
20912 str_offset = bfd_get_32 (abfd, info_ptr);
20913 else
20914 str_offset = bfd_get_64 (abfd, info_ptr);
20915 if (str_offset >= str_section->size)
20916 error (_("Offset from %s pointing outside of"
20917 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20918 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20919 return (const char *) (str_section->buffer + str_offset);
20920 }
20921
20922 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20923
20924 static const char *
20925 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20926 {
20927 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20928 ? reader->cu->header.addr_size : 0;
20929 return read_str_index (reader->cu,
20930 &reader->dwo_file->sections.str,
20931 &reader->dwo_file->sections.str_offsets,
20932 str_offsets_base, str_index);
20933 }
20934
20935 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20936
20937 static const char *
20938 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20939 {
20940 struct objfile *objfile = cu->per_objfile->objfile;
20941 const char *objf_name = objfile_name (objfile);
20942 static const char form_name[] = "DW_FORM_GNU_str_index";
20943 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20944
20945 if (!cu->str_offsets_base.has_value ())
20946 error (_("%s used in Fission stub without %s"
20947 " in CU at offset 0x%lx [in module %s]"),
20948 form_name, str_offsets_attr_name,
20949 (long) cu->header.offset_size, objf_name);
20950
20951 return read_str_index (cu,
20952 &cu->per_objfile->per_bfd->str,
20953 &cu->per_objfile->per_bfd->str_offsets,
20954 *cu->str_offsets_base, str_index);
20955 }
20956
20957 /* Return the length of an LEB128 number in BUF. */
20958
20959 static int
20960 leb128_size (const gdb_byte *buf)
20961 {
20962 const gdb_byte *begin = buf;
20963 gdb_byte byte;
20964
20965 while (1)
20966 {
20967 byte = *buf++;
20968 if ((byte & 128) == 0)
20969 return buf - begin;
20970 }
20971 }
20972
20973 static void
20974 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20975 {
20976 switch (lang)
20977 {
20978 case DW_LANG_C89:
20979 case DW_LANG_C99:
20980 case DW_LANG_C11:
20981 case DW_LANG_C:
20982 case DW_LANG_UPC:
20983 cu->language = language_c;
20984 break;
20985 case DW_LANG_Java:
20986 case DW_LANG_C_plus_plus:
20987 case DW_LANG_C_plus_plus_11:
20988 case DW_LANG_C_plus_plus_14:
20989 cu->language = language_cplus;
20990 break;
20991 case DW_LANG_D:
20992 cu->language = language_d;
20993 break;
20994 case DW_LANG_Fortran77:
20995 case DW_LANG_Fortran90:
20996 case DW_LANG_Fortran95:
20997 case DW_LANG_Fortran03:
20998 case DW_LANG_Fortran08:
20999 cu->language = language_fortran;
21000 break;
21001 case DW_LANG_Go:
21002 cu->language = language_go;
21003 break;
21004 case DW_LANG_Mips_Assembler:
21005 cu->language = language_asm;
21006 break;
21007 case DW_LANG_Ada83:
21008 case DW_LANG_Ada95:
21009 cu->language = language_ada;
21010 break;
21011 case DW_LANG_Modula2:
21012 cu->language = language_m2;
21013 break;
21014 case DW_LANG_Pascal83:
21015 cu->language = language_pascal;
21016 break;
21017 case DW_LANG_ObjC:
21018 cu->language = language_objc;
21019 break;
21020 case DW_LANG_Rust:
21021 case DW_LANG_Rust_old:
21022 cu->language = language_rust;
21023 break;
21024 case DW_LANG_Cobol74:
21025 case DW_LANG_Cobol85:
21026 default:
21027 cu->language = language_minimal;
21028 break;
21029 }
21030 cu->language_defn = language_def (cu->language);
21031 }
21032
21033 /* Return the named attribute or NULL if not there. */
21034
21035 static struct attribute *
21036 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21037 {
21038 for (;;)
21039 {
21040 unsigned int i;
21041 struct attribute *spec = NULL;
21042
21043 for (i = 0; i < die->num_attrs; ++i)
21044 {
21045 if (die->attrs[i].name == name)
21046 return &die->attrs[i];
21047 if (die->attrs[i].name == DW_AT_specification
21048 || die->attrs[i].name == DW_AT_abstract_origin)
21049 spec = &die->attrs[i];
21050 }
21051
21052 if (!spec)
21053 break;
21054
21055 die = follow_die_ref (die, spec, &cu);
21056 }
21057
21058 return NULL;
21059 }
21060
21061 /* Return the string associated with a string-typed attribute, or NULL if it
21062 is either not found or is of an incorrect type. */
21063
21064 static const char *
21065 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21066 {
21067 struct attribute *attr;
21068 const char *str = NULL;
21069
21070 attr = dwarf2_attr (die, name, cu);
21071
21072 if (attr != NULL)
21073 {
21074 str = attr->as_string ();
21075 if (str == nullptr)
21076 complaint (_("string type expected for attribute %s for "
21077 "DIE at %s in module %s"),
21078 dwarf_attr_name (name), sect_offset_str (die->sect_off),
21079 objfile_name (cu->per_objfile->objfile));
21080 }
21081
21082 return str;
21083 }
21084
21085 /* Return the dwo name or NULL if not present. If present, it is in either
21086 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
21087 static const char *
21088 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21089 {
21090 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21091 if (dwo_name == nullptr)
21092 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21093 return dwo_name;
21094 }
21095
21096 /* Return non-zero iff the attribute NAME is defined for the given DIE,
21097 and holds a non-zero value. This function should only be used for
21098 DW_FORM_flag or DW_FORM_flag_present attributes. */
21099
21100 static int
21101 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21102 {
21103 struct attribute *attr = dwarf2_attr (die, name, cu);
21104
21105 return attr != nullptr && attr->as_boolean ();
21106 }
21107
21108 static int
21109 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
21110 {
21111 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21112 which value is non-zero. However, we have to be careful with
21113 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21114 (via dwarf2_flag_true_p) follows this attribute. So we may
21115 end up accidently finding a declaration attribute that belongs
21116 to a different DIE referenced by the specification attribute,
21117 even though the given DIE does not have a declaration attribute. */
21118 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21119 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
21120 }
21121
21122 /* Return the die giving the specification for DIE, if there is
21123 one. *SPEC_CU is the CU containing DIE on input, and the CU
21124 containing the return value on output. If there is no
21125 specification, but there is an abstract origin, that is
21126 returned. */
21127
21128 static struct die_info *
21129 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
21130 {
21131 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21132 *spec_cu);
21133
21134 if (spec_attr == NULL)
21135 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21136
21137 if (spec_attr == NULL)
21138 return NULL;
21139 else
21140 return follow_die_ref (die, spec_attr, spec_cu);
21141 }
21142
21143 /* Stub for free_line_header to match void * callback types. */
21144
21145 static void
21146 free_line_header_voidp (void *arg)
21147 {
21148 struct line_header *lh = (struct line_header *) arg;
21149
21150 delete lh;
21151 }
21152
21153 /* A convenience function to find the proper .debug_line section for a CU. */
21154
21155 static struct dwarf2_section_info *
21156 get_debug_line_section (struct dwarf2_cu *cu)
21157 {
21158 struct dwarf2_section_info *section;
21159 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21160
21161 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21162 DWO file. */
21163 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21164 section = &cu->dwo_unit->dwo_file->sections.line;
21165 else if (cu->per_cu->is_dwz)
21166 {
21167 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
21168
21169 section = &dwz->line;
21170 }
21171 else
21172 section = &per_objfile->per_bfd->line;
21173
21174 return section;
21175 }
21176
21177 /* Read the statement program header starting at OFFSET in
21178 .debug_line, or .debug_line.dwo. Return a pointer
21179 to a struct line_header, allocated using xmalloc.
21180 Returns NULL if there is a problem reading the header, e.g., if it
21181 has a version we don't understand.
21182
21183 NOTE: the strings in the include directory and file name tables of
21184 the returned object point into the dwarf line section buffer,
21185 and must not be freed. */
21186
21187 static line_header_up
21188 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21189 {
21190 struct dwarf2_section_info *section;
21191 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21192
21193 section = get_debug_line_section (cu);
21194 section->read (per_objfile->objfile);
21195 if (section->buffer == NULL)
21196 {
21197 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21198 complaint (_("missing .debug_line.dwo section"));
21199 else
21200 complaint (_("missing .debug_line section"));
21201 return 0;
21202 }
21203
21204 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21205 per_objfile, section, &cu->header);
21206 }
21207
21208 /* Subroutine of dwarf_decode_lines to simplify it.
21209 Return the file name of the psymtab for the given file_entry.
21210 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21211 If space for the result is malloc'd, *NAME_HOLDER will be set.
21212 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21213
21214 static const char *
21215 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21216 const dwarf2_psymtab *pst,
21217 const char *comp_dir,
21218 gdb::unique_xmalloc_ptr<char> *name_holder)
21219 {
21220 const char *include_name = fe.name;
21221 const char *include_name_to_compare = include_name;
21222 const char *pst_filename;
21223 int file_is_pst;
21224
21225 const char *dir_name = fe.include_dir (lh);
21226
21227 gdb::unique_xmalloc_ptr<char> hold_compare;
21228 if (!IS_ABSOLUTE_PATH (include_name)
21229 && (dir_name != NULL || comp_dir != NULL))
21230 {
21231 /* Avoid creating a duplicate psymtab for PST.
21232 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21233 Before we do the comparison, however, we need to account
21234 for DIR_NAME and COMP_DIR.
21235 First prepend dir_name (if non-NULL). If we still don't
21236 have an absolute path prepend comp_dir (if non-NULL).
21237 However, the directory we record in the include-file's
21238 psymtab does not contain COMP_DIR (to match the
21239 corresponding symtab(s)).
21240
21241 Example:
21242
21243 bash$ cd /tmp
21244 bash$ gcc -g ./hello.c
21245 include_name = "hello.c"
21246 dir_name = "."
21247 DW_AT_comp_dir = comp_dir = "/tmp"
21248 DW_AT_name = "./hello.c"
21249
21250 */
21251
21252 if (dir_name != NULL)
21253 {
21254 name_holder->reset (concat (dir_name, SLASH_STRING,
21255 include_name, (char *) NULL));
21256 include_name = name_holder->get ();
21257 include_name_to_compare = include_name;
21258 }
21259 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21260 {
21261 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21262 include_name, (char *) NULL));
21263 include_name_to_compare = hold_compare.get ();
21264 }
21265 }
21266
21267 pst_filename = pst->filename;
21268 gdb::unique_xmalloc_ptr<char> copied_name;
21269 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21270 {
21271 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21272 pst_filename, (char *) NULL));
21273 pst_filename = copied_name.get ();
21274 }
21275
21276 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21277
21278 if (file_is_pst)
21279 return NULL;
21280 return include_name;
21281 }
21282
21283 /* State machine to track the state of the line number program. */
21284
21285 class lnp_state_machine
21286 {
21287 public:
21288 /* Initialize a machine state for the start of a line number
21289 program. */
21290 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21291 bool record_lines_p);
21292
21293 file_entry *current_file ()
21294 {
21295 /* lh->file_names is 0-based, but the file name numbers in the
21296 statement program are 1-based. */
21297 return m_line_header->file_name_at (m_file);
21298 }
21299
21300 /* Record the line in the state machine. END_SEQUENCE is true if
21301 we're processing the end of a sequence. */
21302 void record_line (bool end_sequence);
21303
21304 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21305 nop-out rest of the lines in this sequence. */
21306 void check_line_address (struct dwarf2_cu *cu,
21307 const gdb_byte *line_ptr,
21308 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21309
21310 void handle_set_discriminator (unsigned int discriminator)
21311 {
21312 m_discriminator = discriminator;
21313 m_line_has_non_zero_discriminator |= discriminator != 0;
21314 }
21315
21316 /* Handle DW_LNE_set_address. */
21317 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21318 {
21319 m_op_index = 0;
21320 address += baseaddr;
21321 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21322 }
21323
21324 /* Handle DW_LNS_advance_pc. */
21325 void handle_advance_pc (CORE_ADDR adjust);
21326
21327 /* Handle a special opcode. */
21328 void handle_special_opcode (unsigned char op_code);
21329
21330 /* Handle DW_LNS_advance_line. */
21331 void handle_advance_line (int line_delta)
21332 {
21333 advance_line (line_delta);
21334 }
21335
21336 /* Handle DW_LNS_set_file. */
21337 void handle_set_file (file_name_index file);
21338
21339 /* Handle DW_LNS_negate_stmt. */
21340 void handle_negate_stmt ()
21341 {
21342 m_is_stmt = !m_is_stmt;
21343 }
21344
21345 /* Handle DW_LNS_const_add_pc. */
21346 void handle_const_add_pc ();
21347
21348 /* Handle DW_LNS_fixed_advance_pc. */
21349 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21350 {
21351 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21352 m_op_index = 0;
21353 }
21354
21355 /* Handle DW_LNS_copy. */
21356 void handle_copy ()
21357 {
21358 record_line (false);
21359 m_discriminator = 0;
21360 }
21361
21362 /* Handle DW_LNE_end_sequence. */
21363 void handle_end_sequence ()
21364 {
21365 m_currently_recording_lines = true;
21366 }
21367
21368 private:
21369 /* Advance the line by LINE_DELTA. */
21370 void advance_line (int line_delta)
21371 {
21372 m_line += line_delta;
21373
21374 if (line_delta != 0)
21375 m_line_has_non_zero_discriminator = m_discriminator != 0;
21376 }
21377
21378 struct dwarf2_cu *m_cu;
21379
21380 gdbarch *m_gdbarch;
21381
21382 /* True if we're recording lines.
21383 Otherwise we're building partial symtabs and are just interested in
21384 finding include files mentioned by the line number program. */
21385 bool m_record_lines_p;
21386
21387 /* The line number header. */
21388 line_header *m_line_header;
21389
21390 /* These are part of the standard DWARF line number state machine,
21391 and initialized according to the DWARF spec. */
21392
21393 unsigned char m_op_index = 0;
21394 /* The line table index of the current file. */
21395 file_name_index m_file = 1;
21396 unsigned int m_line = 1;
21397
21398 /* These are initialized in the constructor. */
21399
21400 CORE_ADDR m_address;
21401 bool m_is_stmt;
21402 unsigned int m_discriminator;
21403
21404 /* Additional bits of state we need to track. */
21405
21406 /* The last file that we called dwarf2_start_subfile for.
21407 This is only used for TLLs. */
21408 unsigned int m_last_file = 0;
21409 /* The last file a line number was recorded for. */
21410 struct subfile *m_last_subfile = NULL;
21411
21412 /* The address of the last line entry. */
21413 CORE_ADDR m_last_address;
21414
21415 /* Set to true when a previous line at the same address (using
21416 m_last_address) had m_is_stmt true. This is reset to false when a
21417 line entry at a new address (m_address different to m_last_address) is
21418 processed. */
21419 bool m_stmt_at_address = false;
21420
21421 /* When true, record the lines we decode. */
21422 bool m_currently_recording_lines = false;
21423
21424 /* The last line number that was recorded, used to coalesce
21425 consecutive entries for the same line. This can happen, for
21426 example, when discriminators are present. PR 17276. */
21427 unsigned int m_last_line = 0;
21428 bool m_line_has_non_zero_discriminator = false;
21429 };
21430
21431 void
21432 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21433 {
21434 CORE_ADDR addr_adj = (((m_op_index + adjust)
21435 / m_line_header->maximum_ops_per_instruction)
21436 * m_line_header->minimum_instruction_length);
21437 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21438 m_op_index = ((m_op_index + adjust)
21439 % m_line_header->maximum_ops_per_instruction);
21440 }
21441
21442 void
21443 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21444 {
21445 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21446 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21447 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21448 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21449 / m_line_header->maximum_ops_per_instruction)
21450 * m_line_header->minimum_instruction_length);
21451 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21452 m_op_index = ((m_op_index + adj_opcode_d)
21453 % m_line_header->maximum_ops_per_instruction);
21454
21455 int line_delta = m_line_header->line_base + adj_opcode_r;
21456 advance_line (line_delta);
21457 record_line (false);
21458 m_discriminator = 0;
21459 }
21460
21461 void
21462 lnp_state_machine::handle_set_file (file_name_index file)
21463 {
21464 m_file = file;
21465
21466 const file_entry *fe = current_file ();
21467 if (fe == NULL)
21468 dwarf2_debug_line_missing_file_complaint ();
21469 else if (m_record_lines_p)
21470 {
21471 const char *dir = fe->include_dir (m_line_header);
21472
21473 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21474 m_line_has_non_zero_discriminator = m_discriminator != 0;
21475 dwarf2_start_subfile (m_cu, fe->name, dir);
21476 }
21477 }
21478
21479 void
21480 lnp_state_machine::handle_const_add_pc ()
21481 {
21482 CORE_ADDR adjust
21483 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21484
21485 CORE_ADDR addr_adj
21486 = (((m_op_index + adjust)
21487 / m_line_header->maximum_ops_per_instruction)
21488 * m_line_header->minimum_instruction_length);
21489
21490 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21491 m_op_index = ((m_op_index + adjust)
21492 % m_line_header->maximum_ops_per_instruction);
21493 }
21494
21495 /* Return non-zero if we should add LINE to the line number table.
21496 LINE is the line to add, LAST_LINE is the last line that was added,
21497 LAST_SUBFILE is the subfile for LAST_LINE.
21498 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21499 had a non-zero discriminator.
21500
21501 We have to be careful in the presence of discriminators.
21502 E.g., for this line:
21503
21504 for (i = 0; i < 100000; i++);
21505
21506 clang can emit four line number entries for that one line,
21507 each with a different discriminator.
21508 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21509
21510 However, we want gdb to coalesce all four entries into one.
21511 Otherwise the user could stepi into the middle of the line and
21512 gdb would get confused about whether the pc really was in the
21513 middle of the line.
21514
21515 Things are further complicated by the fact that two consecutive
21516 line number entries for the same line is a heuristic used by gcc
21517 to denote the end of the prologue. So we can't just discard duplicate
21518 entries, we have to be selective about it. The heuristic we use is
21519 that we only collapse consecutive entries for the same line if at least
21520 one of those entries has a non-zero discriminator. PR 17276.
21521
21522 Note: Addresses in the line number state machine can never go backwards
21523 within one sequence, thus this coalescing is ok. */
21524
21525 static int
21526 dwarf_record_line_p (struct dwarf2_cu *cu,
21527 unsigned int line, unsigned int last_line,
21528 int line_has_non_zero_discriminator,
21529 struct subfile *last_subfile)
21530 {
21531 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21532 return 1;
21533 if (line != last_line)
21534 return 1;
21535 /* Same line for the same file that we've seen already.
21536 As a last check, for pr 17276, only record the line if the line
21537 has never had a non-zero discriminator. */
21538 if (!line_has_non_zero_discriminator)
21539 return 1;
21540 return 0;
21541 }
21542
21543 /* Use the CU's builder to record line number LINE beginning at
21544 address ADDRESS in the line table of subfile SUBFILE. */
21545
21546 static void
21547 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21548 unsigned int line, CORE_ADDR address, bool is_stmt,
21549 struct dwarf2_cu *cu)
21550 {
21551 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21552
21553 if (dwarf_line_debug)
21554 {
21555 fprintf_unfiltered (gdb_stdlog,
21556 "Recording line %u, file %s, address %s\n",
21557 line, lbasename (subfile->name),
21558 paddress (gdbarch, address));
21559 }
21560
21561 if (cu != nullptr)
21562 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21563 }
21564
21565 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21566 Mark the end of a set of line number records.
21567 The arguments are the same as for dwarf_record_line_1.
21568 If SUBFILE is NULL the request is ignored. */
21569
21570 static void
21571 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21572 CORE_ADDR address, struct dwarf2_cu *cu)
21573 {
21574 if (subfile == NULL)
21575 return;
21576
21577 if (dwarf_line_debug)
21578 {
21579 fprintf_unfiltered (gdb_stdlog,
21580 "Finishing current line, file %s, address %s\n",
21581 lbasename (subfile->name),
21582 paddress (gdbarch, address));
21583 }
21584
21585 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21586 }
21587
21588 void
21589 lnp_state_machine::record_line (bool end_sequence)
21590 {
21591 if (dwarf_line_debug)
21592 {
21593 fprintf_unfiltered (gdb_stdlog,
21594 "Processing actual line %u: file %u,"
21595 " address %s, is_stmt %u, discrim %u%s\n",
21596 m_line, m_file,
21597 paddress (m_gdbarch, m_address),
21598 m_is_stmt, m_discriminator,
21599 (end_sequence ? "\t(end sequence)" : ""));
21600 }
21601
21602 file_entry *fe = current_file ();
21603
21604 if (fe == NULL)
21605 dwarf2_debug_line_missing_file_complaint ();
21606 /* For now we ignore lines not starting on an instruction boundary.
21607 But not when processing end_sequence for compatibility with the
21608 previous version of the code. */
21609 else if (m_op_index == 0 || end_sequence)
21610 {
21611 fe->included_p = 1;
21612 if (m_record_lines_p)
21613 {
21614 /* When we switch files we insert an end maker in the first file,
21615 switch to the second file and add a new line entry. The
21616 problem is that the end marker inserted in the first file will
21617 discard any previous line entries at the same address. If the
21618 line entries in the first file are marked as is-stmt, while
21619 the new line in the second file is non-stmt, then this means
21620 the end marker will discard is-stmt lines so we can have a
21621 non-stmt line. This means that there are less addresses at
21622 which the user can insert a breakpoint.
21623
21624 To improve this we track the last address in m_last_address,
21625 and whether we have seen an is-stmt at this address. Then
21626 when switching files, if we have seen a stmt at the current
21627 address, and we are switching to create a non-stmt line, then
21628 discard the new line. */
21629 bool file_changed
21630 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21631 bool ignore_this_line
21632 = ((file_changed && !end_sequence && m_last_address == m_address
21633 && !m_is_stmt && m_stmt_at_address)
21634 || (!end_sequence && m_line == 0));
21635
21636 if ((file_changed && !ignore_this_line) || end_sequence)
21637 {
21638 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21639 m_currently_recording_lines ? m_cu : nullptr);
21640 }
21641
21642 if (!end_sequence && !ignore_this_line)
21643 {
21644 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21645
21646 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21647 m_line_has_non_zero_discriminator,
21648 m_last_subfile))
21649 {
21650 buildsym_compunit *builder = m_cu->get_builder ();
21651 dwarf_record_line_1 (m_gdbarch,
21652 builder->get_current_subfile (),
21653 m_line, m_address, is_stmt,
21654 m_currently_recording_lines ? m_cu : nullptr);
21655 }
21656 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21657 m_last_line = m_line;
21658 }
21659 }
21660 }
21661
21662 /* Track whether we have seen any m_is_stmt true at m_address in case we
21663 have multiple line table entries all at m_address. */
21664 if (m_last_address != m_address)
21665 {
21666 m_stmt_at_address = false;
21667 m_last_address = m_address;
21668 }
21669 m_stmt_at_address |= m_is_stmt;
21670 }
21671
21672 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21673 line_header *lh, bool record_lines_p)
21674 {
21675 m_cu = cu;
21676 m_gdbarch = arch;
21677 m_record_lines_p = record_lines_p;
21678 m_line_header = lh;
21679
21680 m_currently_recording_lines = true;
21681
21682 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21683 was a line entry for it so that the backend has a chance to adjust it
21684 and also record it in case it needs it. This is currently used by MIPS
21685 code, cf. `mips_adjust_dwarf2_line'. */
21686 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21687 m_is_stmt = lh->default_is_stmt;
21688 m_discriminator = 0;
21689
21690 m_last_address = m_address;
21691 m_stmt_at_address = false;
21692 }
21693
21694 void
21695 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21696 const gdb_byte *line_ptr,
21697 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21698 {
21699 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21700 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21701 located at 0x0. In this case, additionally check that if
21702 ADDRESS < UNRELOCATED_LOWPC. */
21703
21704 if ((address == 0 && address < unrelocated_lowpc)
21705 || address == (CORE_ADDR) -1)
21706 {
21707 /* This line table is for a function which has been
21708 GCd by the linker. Ignore it. PR gdb/12528 */
21709
21710 struct objfile *objfile = cu->per_objfile->objfile;
21711 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21712
21713 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21714 line_offset, objfile_name (objfile));
21715 m_currently_recording_lines = false;
21716 /* Note: m_currently_recording_lines is left as false until we see
21717 DW_LNE_end_sequence. */
21718 }
21719 }
21720
21721 /* Subroutine of dwarf_decode_lines to simplify it.
21722 Process the line number information in LH.
21723 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21724 program in order to set included_p for every referenced header. */
21725
21726 static void
21727 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21728 const int decode_for_pst_p, CORE_ADDR lowpc)
21729 {
21730 const gdb_byte *line_ptr, *extended_end;
21731 const gdb_byte *line_end;
21732 unsigned int bytes_read, extended_len;
21733 unsigned char op_code, extended_op;
21734 CORE_ADDR baseaddr;
21735 struct objfile *objfile = cu->per_objfile->objfile;
21736 bfd *abfd = objfile->obfd;
21737 struct gdbarch *gdbarch = objfile->arch ();
21738 /* True if we're recording line info (as opposed to building partial
21739 symtabs and just interested in finding include files mentioned by
21740 the line number program). */
21741 bool record_lines_p = !decode_for_pst_p;
21742
21743 baseaddr = objfile->text_section_offset ();
21744
21745 line_ptr = lh->statement_program_start;
21746 line_end = lh->statement_program_end;
21747
21748 /* Read the statement sequences until there's nothing left. */
21749 while (line_ptr < line_end)
21750 {
21751 /* The DWARF line number program state machine. Reset the state
21752 machine at the start of each sequence. */
21753 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21754 bool end_sequence = false;
21755
21756 if (record_lines_p)
21757 {
21758 /* Start a subfile for the current file of the state
21759 machine. */
21760 const file_entry *fe = state_machine.current_file ();
21761
21762 if (fe != NULL)
21763 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21764 }
21765
21766 /* Decode the table. */
21767 while (line_ptr < line_end && !end_sequence)
21768 {
21769 op_code = read_1_byte (abfd, line_ptr);
21770 line_ptr += 1;
21771
21772 if (op_code >= lh->opcode_base)
21773 {
21774 /* Special opcode. */
21775 state_machine.handle_special_opcode (op_code);
21776 }
21777 else switch (op_code)
21778 {
21779 case DW_LNS_extended_op:
21780 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21781 &bytes_read);
21782 line_ptr += bytes_read;
21783 extended_end = line_ptr + extended_len;
21784 extended_op = read_1_byte (abfd, line_ptr);
21785 line_ptr += 1;
21786 if (DW_LNE_lo_user <= extended_op
21787 && extended_op <= DW_LNE_hi_user)
21788 {
21789 /* Vendor extension, ignore. */
21790 line_ptr = extended_end;
21791 break;
21792 }
21793 switch (extended_op)
21794 {
21795 case DW_LNE_end_sequence:
21796 state_machine.handle_end_sequence ();
21797 end_sequence = true;
21798 break;
21799 case DW_LNE_set_address:
21800 {
21801 CORE_ADDR address
21802 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21803 line_ptr += bytes_read;
21804
21805 state_machine.check_line_address (cu, line_ptr,
21806 lowpc - baseaddr, address);
21807 state_machine.handle_set_address (baseaddr, address);
21808 }
21809 break;
21810 case DW_LNE_define_file:
21811 {
21812 const char *cur_file;
21813 unsigned int mod_time, length;
21814 dir_index dindex;
21815
21816 cur_file = read_direct_string (abfd, line_ptr,
21817 &bytes_read);
21818 line_ptr += bytes_read;
21819 dindex = (dir_index)
21820 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21821 line_ptr += bytes_read;
21822 mod_time =
21823 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21824 line_ptr += bytes_read;
21825 length =
21826 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21827 line_ptr += bytes_read;
21828 lh->add_file_name (cur_file, dindex, mod_time, length);
21829 }
21830 break;
21831 case DW_LNE_set_discriminator:
21832 {
21833 /* The discriminator is not interesting to the
21834 debugger; just ignore it. We still need to
21835 check its value though:
21836 if there are consecutive entries for the same
21837 (non-prologue) line we want to coalesce them.
21838 PR 17276. */
21839 unsigned int discr
21840 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21841 line_ptr += bytes_read;
21842
21843 state_machine.handle_set_discriminator (discr);
21844 }
21845 break;
21846 default:
21847 complaint (_("mangled .debug_line section"));
21848 return;
21849 }
21850 /* Make sure that we parsed the extended op correctly. If e.g.
21851 we expected a different address size than the producer used,
21852 we may have read the wrong number of bytes. */
21853 if (line_ptr != extended_end)
21854 {
21855 complaint (_("mangled .debug_line section"));
21856 return;
21857 }
21858 break;
21859 case DW_LNS_copy:
21860 state_machine.handle_copy ();
21861 break;
21862 case DW_LNS_advance_pc:
21863 {
21864 CORE_ADDR adjust
21865 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21866 line_ptr += bytes_read;
21867
21868 state_machine.handle_advance_pc (adjust);
21869 }
21870 break;
21871 case DW_LNS_advance_line:
21872 {
21873 int line_delta
21874 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21875 line_ptr += bytes_read;
21876
21877 state_machine.handle_advance_line (line_delta);
21878 }
21879 break;
21880 case DW_LNS_set_file:
21881 {
21882 file_name_index file
21883 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21884 &bytes_read);
21885 line_ptr += bytes_read;
21886
21887 state_machine.handle_set_file (file);
21888 }
21889 break;
21890 case DW_LNS_set_column:
21891 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21892 line_ptr += bytes_read;
21893 break;
21894 case DW_LNS_negate_stmt:
21895 state_machine.handle_negate_stmt ();
21896 break;
21897 case DW_LNS_set_basic_block:
21898 break;
21899 /* Add to the address register of the state machine the
21900 address increment value corresponding to special opcode
21901 255. I.e., this value is scaled by the minimum
21902 instruction length since special opcode 255 would have
21903 scaled the increment. */
21904 case DW_LNS_const_add_pc:
21905 state_machine.handle_const_add_pc ();
21906 break;
21907 case DW_LNS_fixed_advance_pc:
21908 {
21909 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21910 line_ptr += 2;
21911
21912 state_machine.handle_fixed_advance_pc (addr_adj);
21913 }
21914 break;
21915 default:
21916 {
21917 /* Unknown standard opcode, ignore it. */
21918 int i;
21919
21920 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21921 {
21922 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21923 line_ptr += bytes_read;
21924 }
21925 }
21926 }
21927 }
21928
21929 if (!end_sequence)
21930 dwarf2_debug_line_missing_end_sequence_complaint ();
21931
21932 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21933 in which case we still finish recording the last line). */
21934 state_machine.record_line (true);
21935 }
21936 }
21937
21938 /* Decode the Line Number Program (LNP) for the given line_header
21939 structure and CU. The actual information extracted and the type
21940 of structures created from the LNP depends on the value of PST.
21941
21942 1. If PST is NULL, then this procedure uses the data from the program
21943 to create all necessary symbol tables, and their linetables.
21944
21945 2. If PST is not NULL, this procedure reads the program to determine
21946 the list of files included by the unit represented by PST, and
21947 builds all the associated partial symbol tables.
21948
21949 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21950 It is used for relative paths in the line table.
21951 NOTE: When processing partial symtabs (pst != NULL),
21952 comp_dir == pst->dirname.
21953
21954 NOTE: It is important that psymtabs have the same file name (via strcmp)
21955 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21956 symtab we don't use it in the name of the psymtabs we create.
21957 E.g. expand_line_sal requires this when finding psymtabs to expand.
21958 A good testcase for this is mb-inline.exp.
21959
21960 LOWPC is the lowest address in CU (or 0 if not known).
21961
21962 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21963 for its PC<->lines mapping information. Otherwise only the filename
21964 table is read in. */
21965
21966 static void
21967 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21968 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21969 CORE_ADDR lowpc, int decode_mapping)
21970 {
21971 struct objfile *objfile = cu->per_objfile->objfile;
21972 const int decode_for_pst_p = (pst != NULL);
21973
21974 if (decode_mapping)
21975 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21976
21977 if (decode_for_pst_p)
21978 {
21979 /* Now that we're done scanning the Line Header Program, we can
21980 create the psymtab of each included file. */
21981 for (auto &file_entry : lh->file_names ())
21982 if (file_entry.included_p == 1)
21983 {
21984 gdb::unique_xmalloc_ptr<char> name_holder;
21985 const char *include_name =
21986 psymtab_include_file_name (lh, file_entry, pst,
21987 comp_dir, &name_holder);
21988 if (include_name != NULL)
21989 dwarf2_create_include_psymtab
21990 (cu->per_objfile->per_bfd, include_name, pst,
21991 cu->per_objfile->per_bfd->partial_symtabs.get (),
21992 objfile);
21993 }
21994 }
21995 else
21996 {
21997 /* Make sure a symtab is created for every file, even files
21998 which contain only variables (i.e. no code with associated
21999 line numbers). */
22000 buildsym_compunit *builder = cu->get_builder ();
22001 struct compunit_symtab *cust = builder->get_compunit_symtab ();
22002
22003 for (auto &fe : lh->file_names ())
22004 {
22005 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
22006 if (builder->get_current_subfile ()->symtab == NULL)
22007 {
22008 builder->get_current_subfile ()->symtab
22009 = allocate_symtab (cust,
22010 builder->get_current_subfile ()->name);
22011 }
22012 fe.symtab = builder->get_current_subfile ()->symtab;
22013 }
22014 }
22015 }
22016
22017 /* Start a subfile for DWARF. FILENAME is the name of the file and
22018 DIRNAME the name of the source directory which contains FILENAME
22019 or NULL if not known.
22020 This routine tries to keep line numbers from identical absolute and
22021 relative file names in a common subfile.
22022
22023 Using the `list' example from the GDB testsuite, which resides in
22024 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22025 of /srcdir/list0.c yields the following debugging information for list0.c:
22026
22027 DW_AT_name: /srcdir/list0.c
22028 DW_AT_comp_dir: /compdir
22029 files.files[0].name: list0.h
22030 files.files[0].dir: /srcdir
22031 files.files[1].name: list0.c
22032 files.files[1].dir: /srcdir
22033
22034 The line number information for list0.c has to end up in a single
22035 subfile, so that `break /srcdir/list0.c:1' works as expected.
22036 start_subfile will ensure that this happens provided that we pass the
22037 concatenation of files.files[1].dir and files.files[1].name as the
22038 subfile's name. */
22039
22040 static void
22041 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22042 const char *dirname)
22043 {
22044 gdb::unique_xmalloc_ptr<char> copy;
22045
22046 /* In order not to lose the line information directory,
22047 we concatenate it to the filename when it makes sense.
22048 Note that the Dwarf3 standard says (speaking of filenames in line
22049 information): ``The directory index is ignored for file names
22050 that represent full path names''. Thus ignoring dirname in the
22051 `else' branch below isn't an issue. */
22052
22053 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
22054 {
22055 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22056 filename = copy.get ();
22057 }
22058
22059 cu->get_builder ()->start_subfile (filename);
22060 }
22061
22062 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22063 buildsym_compunit constructor. */
22064
22065 struct compunit_symtab *
22066 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22067 CORE_ADDR low_pc)
22068 {
22069 gdb_assert (m_builder == nullptr);
22070
22071 m_builder.reset (new struct buildsym_compunit
22072 (this->per_objfile->objfile,
22073 name, comp_dir, language, low_pc));
22074
22075 list_in_scope = get_builder ()->get_file_symbols ();
22076
22077 get_builder ()->record_debugformat ("DWARF 2");
22078 get_builder ()->record_producer (producer);
22079
22080 processing_has_namespace_info = false;
22081
22082 return get_builder ()->get_compunit_symtab ();
22083 }
22084
22085 static void
22086 var_decode_location (struct attribute *attr, struct symbol *sym,
22087 struct dwarf2_cu *cu)
22088 {
22089 struct objfile *objfile = cu->per_objfile->objfile;
22090 struct comp_unit_head *cu_header = &cu->header;
22091
22092 /* NOTE drow/2003-01-30: There used to be a comment and some special
22093 code here to turn a symbol with DW_AT_external and a
22094 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22095 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22096 with some versions of binutils) where shared libraries could have
22097 relocations against symbols in their debug information - the
22098 minimal symbol would have the right address, but the debug info
22099 would not. It's no longer necessary, because we will explicitly
22100 apply relocations when we read in the debug information now. */
22101
22102 /* A DW_AT_location attribute with no contents indicates that a
22103 variable has been optimized away. */
22104 if (attr->form_is_block () && attr->as_block ()->size == 0)
22105 {
22106 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22107 return;
22108 }
22109
22110 /* Handle one degenerate form of location expression specially, to
22111 preserve GDB's previous behavior when section offsets are
22112 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22113 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
22114
22115 if (attr->form_is_block ())
22116 {
22117 struct dwarf_block *block = attr->as_block ();
22118
22119 if ((block->data[0] == DW_OP_addr
22120 && block->size == 1 + cu_header->addr_size)
22121 || ((block->data[0] == DW_OP_GNU_addr_index
22122 || block->data[0] == DW_OP_addrx)
22123 && (block->size
22124 == 1 + leb128_size (&block->data[1]))))
22125 {
22126 unsigned int dummy;
22127
22128 if (block->data[0] == DW_OP_addr)
22129 SET_SYMBOL_VALUE_ADDRESS
22130 (sym, cu->header.read_address (objfile->obfd,
22131 block->data + 1,
22132 &dummy));
22133 else
22134 SET_SYMBOL_VALUE_ADDRESS
22135 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22136 &dummy));
22137 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22138 fixup_symbol_section (sym, objfile);
22139 SET_SYMBOL_VALUE_ADDRESS
22140 (sym,
22141 SYMBOL_VALUE_ADDRESS (sym)
22142 + objfile->section_offsets[sym->section_index ()]);
22143 return;
22144 }
22145 }
22146
22147 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22148 expression evaluator, and use LOC_COMPUTED only when necessary
22149 (i.e. when the value of a register or memory location is
22150 referenced, or a thread-local block, etc.). Then again, it might
22151 not be worthwhile. I'm assuming that it isn't unless performance
22152 or memory numbers show me otherwise. */
22153
22154 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22155
22156 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22157 cu->has_loclist = true;
22158 }
22159
22160 /* Given a pointer to a DWARF information entry, figure out if we need
22161 to make a symbol table entry for it, and if so, create a new entry
22162 and return a pointer to it.
22163 If TYPE is NULL, determine symbol type from the die, otherwise
22164 used the passed type.
22165 If SPACE is not NULL, use it to hold the new symbol. If it is
22166 NULL, allocate a new symbol on the objfile's obstack. */
22167
22168 static struct symbol *
22169 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22170 struct symbol *space)
22171 {
22172 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22173 struct objfile *objfile = per_objfile->objfile;
22174 struct gdbarch *gdbarch = objfile->arch ();
22175 struct symbol *sym = NULL;
22176 const char *name;
22177 struct attribute *attr = NULL;
22178 struct attribute *attr2 = NULL;
22179 CORE_ADDR baseaddr;
22180 struct pending **list_to_add = NULL;
22181
22182 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22183
22184 baseaddr = objfile->text_section_offset ();
22185
22186 name = dwarf2_name (die, cu);
22187 if (name)
22188 {
22189 int suppress_add = 0;
22190
22191 if (space)
22192 sym = space;
22193 else
22194 sym = new (&objfile->objfile_obstack) symbol;
22195 OBJSTAT (objfile, n_syms++);
22196
22197 /* Cache this symbol's name and the name's demangled form (if any). */
22198 sym->set_language (cu->language, &objfile->objfile_obstack);
22199 /* Fortran does not have mangling standard and the mangling does differ
22200 between gfortran, iFort etc. */
22201 const char *physname
22202 = (cu->language == language_fortran
22203 ? dwarf2_full_name (name, die, cu)
22204 : dwarf2_physname (name, die, cu));
22205 const char *linkagename = dw2_linkage_name (die, cu);
22206
22207 if (linkagename == nullptr || cu->language == language_ada)
22208 sym->set_linkage_name (physname);
22209 else
22210 {
22211 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22212 sym->set_linkage_name (linkagename);
22213 }
22214
22215 /* Default assumptions.
22216 Use the passed type or decode it from the die. */
22217 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22218 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22219 if (type != NULL)
22220 SYMBOL_TYPE (sym) = type;
22221 else
22222 SYMBOL_TYPE (sym) = die_type (die, cu);
22223 attr = dwarf2_attr (die,
22224 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22225 cu);
22226 if (attr != nullptr)
22227 SYMBOL_LINE (sym) = attr->constant_value (0);
22228
22229 attr = dwarf2_attr (die,
22230 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22231 cu);
22232 if (attr != nullptr && attr->is_nonnegative ())
22233 {
22234 file_name_index file_index
22235 = (file_name_index) attr->as_nonnegative ();
22236 struct file_entry *fe;
22237
22238 if (cu->line_header != NULL)
22239 fe = cu->line_header->file_name_at (file_index);
22240 else
22241 fe = NULL;
22242
22243 if (fe == NULL)
22244 complaint (_("file index out of range"));
22245 else
22246 symbol_set_symtab (sym, fe->symtab);
22247 }
22248
22249 switch (die->tag)
22250 {
22251 case DW_TAG_label:
22252 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22253 if (attr != nullptr)
22254 {
22255 CORE_ADDR addr;
22256
22257 addr = attr->as_address ();
22258 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22259 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22260 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22261 }
22262 else
22263 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22264 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22265 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22266 add_symbol_to_list (sym, cu->list_in_scope);
22267 break;
22268 case DW_TAG_subprogram:
22269 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22270 finish_block. */
22271 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22272 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22273 if ((attr2 != nullptr && attr2->as_boolean ())
22274 || cu->language == language_ada
22275 || cu->language == language_fortran)
22276 {
22277 /* Subprograms marked external are stored as a global symbol.
22278 Ada and Fortran subprograms, whether marked external or
22279 not, are always stored as a global symbol, because we want
22280 to be able to access them globally. For instance, we want
22281 to be able to break on a nested subprogram without having
22282 to specify the context. */
22283 list_to_add = cu->get_builder ()->get_global_symbols ();
22284 }
22285 else
22286 {
22287 list_to_add = cu->list_in_scope;
22288 }
22289 break;
22290 case DW_TAG_inlined_subroutine:
22291 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22292 finish_block. */
22293 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22294 SYMBOL_INLINED (sym) = 1;
22295 list_to_add = cu->list_in_scope;
22296 break;
22297 case DW_TAG_template_value_param:
22298 suppress_add = 1;
22299 /* Fall through. */
22300 case DW_TAG_constant:
22301 case DW_TAG_variable:
22302 case DW_TAG_member:
22303 /* Compilation with minimal debug info may result in
22304 variables with missing type entries. Change the
22305 misleading `void' type to something sensible. */
22306 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22307 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22308
22309 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22310 /* In the case of DW_TAG_member, we should only be called for
22311 static const members. */
22312 if (die->tag == DW_TAG_member)
22313 {
22314 /* dwarf2_add_field uses die_is_declaration,
22315 so we do the same. */
22316 gdb_assert (die_is_declaration (die, cu));
22317 gdb_assert (attr);
22318 }
22319 if (attr != nullptr)
22320 {
22321 dwarf2_const_value (attr, sym, cu);
22322 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22323 if (!suppress_add)
22324 {
22325 if (attr2 != nullptr && attr2->as_boolean ())
22326 list_to_add = cu->get_builder ()->get_global_symbols ();
22327 else
22328 list_to_add = cu->list_in_scope;
22329 }
22330 break;
22331 }
22332 attr = dwarf2_attr (die, DW_AT_location, cu);
22333 if (attr != nullptr)
22334 {
22335 var_decode_location (attr, sym, cu);
22336 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22337
22338 /* Fortran explicitly imports any global symbols to the local
22339 scope by DW_TAG_common_block. */
22340 if (cu->language == language_fortran && die->parent
22341 && die->parent->tag == DW_TAG_common_block)
22342 attr2 = NULL;
22343
22344 if (SYMBOL_CLASS (sym) == LOC_STATIC
22345 && SYMBOL_VALUE_ADDRESS (sym) == 0
22346 && !per_objfile->per_bfd->has_section_at_zero)
22347 {
22348 /* When a static variable is eliminated by the linker,
22349 the corresponding debug information is not stripped
22350 out, but the variable address is set to null;
22351 do not add such variables into symbol table. */
22352 }
22353 else if (attr2 != nullptr && attr2->as_boolean ())
22354 {
22355 if (SYMBOL_CLASS (sym) == LOC_STATIC
22356 && (objfile->flags & OBJF_MAINLINE) == 0
22357 && per_objfile->per_bfd->can_copy)
22358 {
22359 /* A global static variable might be subject to
22360 copy relocation. We first check for a local
22361 minsym, though, because maybe the symbol was
22362 marked hidden, in which case this would not
22363 apply. */
22364 bound_minimal_symbol found
22365 = (lookup_minimal_symbol_linkage
22366 (sym->linkage_name (), objfile));
22367 if (found.minsym != nullptr)
22368 sym->maybe_copied = 1;
22369 }
22370
22371 /* A variable with DW_AT_external is never static,
22372 but it may be block-scoped. */
22373 list_to_add
22374 = ((cu->list_in_scope
22375 == cu->get_builder ()->get_file_symbols ())
22376 ? cu->get_builder ()->get_global_symbols ()
22377 : cu->list_in_scope);
22378 }
22379 else
22380 list_to_add = cu->list_in_scope;
22381 }
22382 else
22383 {
22384 /* We do not know the address of this symbol.
22385 If it is an external symbol and we have type information
22386 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22387 The address of the variable will then be determined from
22388 the minimal symbol table whenever the variable is
22389 referenced. */
22390 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22391
22392 /* Fortran explicitly imports any global symbols to the local
22393 scope by DW_TAG_common_block. */
22394 if (cu->language == language_fortran && die->parent
22395 && die->parent->tag == DW_TAG_common_block)
22396 {
22397 /* SYMBOL_CLASS doesn't matter here because
22398 read_common_block is going to reset it. */
22399 if (!suppress_add)
22400 list_to_add = cu->list_in_scope;
22401 }
22402 else if (attr2 != nullptr && attr2->as_boolean ()
22403 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22404 {
22405 /* A variable with DW_AT_external is never static, but it
22406 may be block-scoped. */
22407 list_to_add
22408 = ((cu->list_in_scope
22409 == cu->get_builder ()->get_file_symbols ())
22410 ? cu->get_builder ()->get_global_symbols ()
22411 : cu->list_in_scope);
22412
22413 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22414 }
22415 else if (!die_is_declaration (die, cu))
22416 {
22417 /* Use the default LOC_OPTIMIZED_OUT class. */
22418 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22419 if (!suppress_add)
22420 list_to_add = cu->list_in_scope;
22421 }
22422 }
22423 break;
22424 case DW_TAG_formal_parameter:
22425 {
22426 /* If we are inside a function, mark this as an argument. If
22427 not, we might be looking at an argument to an inlined function
22428 when we do not have enough information to show inlined frames;
22429 pretend it's a local variable in that case so that the user can
22430 still see it. */
22431 struct context_stack *curr
22432 = cu->get_builder ()->get_current_context_stack ();
22433 if (curr != nullptr && curr->name != nullptr)
22434 SYMBOL_IS_ARGUMENT (sym) = 1;
22435 attr = dwarf2_attr (die, DW_AT_location, cu);
22436 if (attr != nullptr)
22437 {
22438 var_decode_location (attr, sym, cu);
22439 }
22440 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22441 if (attr != nullptr)
22442 {
22443 dwarf2_const_value (attr, sym, cu);
22444 }
22445
22446 list_to_add = cu->list_in_scope;
22447 }
22448 break;
22449 case DW_TAG_unspecified_parameters:
22450 /* From varargs functions; gdb doesn't seem to have any
22451 interest in this information, so just ignore it for now.
22452 (FIXME?) */
22453 break;
22454 case DW_TAG_template_type_param:
22455 suppress_add = 1;
22456 /* Fall through. */
22457 case DW_TAG_class_type:
22458 case DW_TAG_interface_type:
22459 case DW_TAG_structure_type:
22460 case DW_TAG_union_type:
22461 case DW_TAG_set_type:
22462 case DW_TAG_enumeration_type:
22463 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22464 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22465
22466 {
22467 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22468 really ever be static objects: otherwise, if you try
22469 to, say, break of a class's method and you're in a file
22470 which doesn't mention that class, it won't work unless
22471 the check for all static symbols in lookup_symbol_aux
22472 saves you. See the OtherFileClass tests in
22473 gdb.c++/namespace.exp. */
22474
22475 if (!suppress_add)
22476 {
22477 buildsym_compunit *builder = cu->get_builder ();
22478 list_to_add
22479 = (cu->list_in_scope == builder->get_file_symbols ()
22480 && cu->language == language_cplus
22481 ? builder->get_global_symbols ()
22482 : cu->list_in_scope);
22483
22484 /* The semantics of C++ state that "struct foo {
22485 ... }" also defines a typedef for "foo". */
22486 if (cu->language == language_cplus
22487 || cu->language == language_ada
22488 || cu->language == language_d
22489 || cu->language == language_rust)
22490 {
22491 /* The symbol's name is already allocated along
22492 with this objfile, so we don't need to
22493 duplicate it for the type. */
22494 if (SYMBOL_TYPE (sym)->name () == 0)
22495 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22496 }
22497 }
22498 }
22499 break;
22500 case DW_TAG_typedef:
22501 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22502 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22503 list_to_add = cu->list_in_scope;
22504 break;
22505 case DW_TAG_array_type:
22506 case DW_TAG_base_type:
22507 case DW_TAG_subrange_type:
22508 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22509 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22510 list_to_add = cu->list_in_scope;
22511 break;
22512 case DW_TAG_enumerator:
22513 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22514 if (attr != nullptr)
22515 {
22516 dwarf2_const_value (attr, sym, cu);
22517 }
22518 {
22519 /* NOTE: carlton/2003-11-10: See comment above in the
22520 DW_TAG_class_type, etc. block. */
22521
22522 list_to_add
22523 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22524 && cu->language == language_cplus
22525 ? cu->get_builder ()->get_global_symbols ()
22526 : cu->list_in_scope);
22527 }
22528 break;
22529 case DW_TAG_imported_declaration:
22530 case DW_TAG_namespace:
22531 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22532 list_to_add = cu->get_builder ()->get_global_symbols ();
22533 break;
22534 case DW_TAG_module:
22535 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22536 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22537 list_to_add = cu->get_builder ()->get_global_symbols ();
22538 break;
22539 case DW_TAG_common_block:
22540 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22541 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22542 add_symbol_to_list (sym, cu->list_in_scope);
22543 break;
22544 default:
22545 /* Not a tag we recognize. Hopefully we aren't processing
22546 trash data, but since we must specifically ignore things
22547 we don't recognize, there is nothing else we should do at
22548 this point. */
22549 complaint (_("unsupported tag: '%s'"),
22550 dwarf_tag_name (die->tag));
22551 break;
22552 }
22553
22554 if (suppress_add)
22555 {
22556 sym->hash_next = objfile->template_symbols;
22557 objfile->template_symbols = sym;
22558 list_to_add = NULL;
22559 }
22560
22561 if (list_to_add != NULL)
22562 add_symbol_to_list (sym, list_to_add);
22563
22564 /* For the benefit of old versions of GCC, check for anonymous
22565 namespaces based on the demangled name. */
22566 if (!cu->processing_has_namespace_info
22567 && cu->language == language_cplus)
22568 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22569 }
22570 return (sym);
22571 }
22572
22573 /* Given an attr with a DW_FORM_dataN value in host byte order,
22574 zero-extend it as appropriate for the symbol's type. The DWARF
22575 standard (v4) is not entirely clear about the meaning of using
22576 DW_FORM_dataN for a constant with a signed type, where the type is
22577 wider than the data. The conclusion of a discussion on the DWARF
22578 list was that this is unspecified. We choose to always zero-extend
22579 because that is the interpretation long in use by GCC. */
22580
22581 static gdb_byte *
22582 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22583 struct dwarf2_cu *cu, LONGEST *value, int bits)
22584 {
22585 struct objfile *objfile = cu->per_objfile->objfile;
22586 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22587 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22588 LONGEST l = attr->constant_value (0);
22589
22590 if (bits < sizeof (*value) * 8)
22591 {
22592 l &= ((LONGEST) 1 << bits) - 1;
22593 *value = l;
22594 }
22595 else if (bits == sizeof (*value) * 8)
22596 *value = l;
22597 else
22598 {
22599 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22600 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22601 return bytes;
22602 }
22603
22604 return NULL;
22605 }
22606
22607 /* Read a constant value from an attribute. Either set *VALUE, or if
22608 the value does not fit in *VALUE, set *BYTES - either already
22609 allocated on the objfile obstack, or newly allocated on OBSTACK,
22610 or, set *BATON, if we translated the constant to a location
22611 expression. */
22612
22613 static void
22614 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22615 const char *name, struct obstack *obstack,
22616 struct dwarf2_cu *cu,
22617 LONGEST *value, const gdb_byte **bytes,
22618 struct dwarf2_locexpr_baton **baton)
22619 {
22620 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22621 struct objfile *objfile = per_objfile->objfile;
22622 struct comp_unit_head *cu_header = &cu->header;
22623 struct dwarf_block *blk;
22624 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22625 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22626
22627 *value = 0;
22628 *bytes = NULL;
22629 *baton = NULL;
22630
22631 switch (attr->form)
22632 {
22633 case DW_FORM_addr:
22634 case DW_FORM_addrx:
22635 case DW_FORM_GNU_addr_index:
22636 {
22637 gdb_byte *data;
22638
22639 if (TYPE_LENGTH (type) != cu_header->addr_size)
22640 dwarf2_const_value_length_mismatch_complaint (name,
22641 cu_header->addr_size,
22642 TYPE_LENGTH (type));
22643 /* Symbols of this form are reasonably rare, so we just
22644 piggyback on the existing location code rather than writing
22645 a new implementation of symbol_computed_ops. */
22646 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22647 (*baton)->per_objfile = per_objfile;
22648 (*baton)->per_cu = cu->per_cu;
22649 gdb_assert ((*baton)->per_cu);
22650
22651 (*baton)->size = 2 + cu_header->addr_size;
22652 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22653 (*baton)->data = data;
22654
22655 data[0] = DW_OP_addr;
22656 store_unsigned_integer (&data[1], cu_header->addr_size,
22657 byte_order, attr->as_address ());
22658 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22659 }
22660 break;
22661 case DW_FORM_string:
22662 case DW_FORM_strp:
22663 case DW_FORM_strx:
22664 case DW_FORM_GNU_str_index:
22665 case DW_FORM_GNU_strp_alt:
22666 /* The string is already allocated on the objfile obstack, point
22667 directly to it. */
22668 *bytes = (const gdb_byte *) attr->as_string ();
22669 break;
22670 case DW_FORM_block1:
22671 case DW_FORM_block2:
22672 case DW_FORM_block4:
22673 case DW_FORM_block:
22674 case DW_FORM_exprloc:
22675 case DW_FORM_data16:
22676 blk = attr->as_block ();
22677 if (TYPE_LENGTH (type) != blk->size)
22678 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22679 TYPE_LENGTH (type));
22680 *bytes = blk->data;
22681 break;
22682
22683 /* The DW_AT_const_value attributes are supposed to carry the
22684 symbol's value "represented as it would be on the target
22685 architecture." By the time we get here, it's already been
22686 converted to host endianness, so we just need to sign- or
22687 zero-extend it as appropriate. */
22688 case DW_FORM_data1:
22689 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22690 break;
22691 case DW_FORM_data2:
22692 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22693 break;
22694 case DW_FORM_data4:
22695 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22696 break;
22697 case DW_FORM_data8:
22698 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22699 break;
22700
22701 case DW_FORM_sdata:
22702 case DW_FORM_implicit_const:
22703 *value = attr->as_signed ();
22704 break;
22705
22706 case DW_FORM_udata:
22707 *value = attr->as_unsigned ();
22708 break;
22709
22710 default:
22711 complaint (_("unsupported const value attribute form: '%s'"),
22712 dwarf_form_name (attr->form));
22713 *value = 0;
22714 break;
22715 }
22716 }
22717
22718
22719 /* Copy constant value from an attribute to a symbol. */
22720
22721 static void
22722 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22723 struct dwarf2_cu *cu)
22724 {
22725 struct objfile *objfile = cu->per_objfile->objfile;
22726 LONGEST value;
22727 const gdb_byte *bytes;
22728 struct dwarf2_locexpr_baton *baton;
22729
22730 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22731 sym->print_name (),
22732 &objfile->objfile_obstack, cu,
22733 &value, &bytes, &baton);
22734
22735 if (baton != NULL)
22736 {
22737 SYMBOL_LOCATION_BATON (sym) = baton;
22738 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22739 }
22740 else if (bytes != NULL)
22741 {
22742 SYMBOL_VALUE_BYTES (sym) = bytes;
22743 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22744 }
22745 else
22746 {
22747 SYMBOL_VALUE (sym) = value;
22748 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22749 }
22750 }
22751
22752 /* Return the type of the die in question using its DW_AT_type attribute. */
22753
22754 static struct type *
22755 die_type (struct die_info *die, struct dwarf2_cu *cu)
22756 {
22757 struct attribute *type_attr;
22758
22759 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22760 if (!type_attr)
22761 {
22762 struct objfile *objfile = cu->per_objfile->objfile;
22763 /* A missing DW_AT_type represents a void type. */
22764 return objfile_type (objfile)->builtin_void;
22765 }
22766
22767 return lookup_die_type (die, type_attr, cu);
22768 }
22769
22770 /* True iff CU's producer generates GNAT Ada auxiliary information
22771 that allows to find parallel types through that information instead
22772 of having to do expensive parallel lookups by type name. */
22773
22774 static int
22775 need_gnat_info (struct dwarf2_cu *cu)
22776 {
22777 /* Assume that the Ada compiler was GNAT, which always produces
22778 the auxiliary information. */
22779 return (cu->language == language_ada);
22780 }
22781
22782 /* Return the auxiliary type of the die in question using its
22783 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22784 attribute is not present. */
22785
22786 static struct type *
22787 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22788 {
22789 struct attribute *type_attr;
22790
22791 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22792 if (!type_attr)
22793 return NULL;
22794
22795 return lookup_die_type (die, type_attr, cu);
22796 }
22797
22798 /* If DIE has a descriptive_type attribute, then set the TYPE's
22799 descriptive type accordingly. */
22800
22801 static void
22802 set_descriptive_type (struct type *type, struct die_info *die,
22803 struct dwarf2_cu *cu)
22804 {
22805 struct type *descriptive_type = die_descriptive_type (die, cu);
22806
22807 if (descriptive_type)
22808 {
22809 ALLOCATE_GNAT_AUX_TYPE (type);
22810 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22811 }
22812 }
22813
22814 /* Return the containing type of the die in question using its
22815 DW_AT_containing_type attribute. */
22816
22817 static struct type *
22818 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22819 {
22820 struct attribute *type_attr;
22821 struct objfile *objfile = cu->per_objfile->objfile;
22822
22823 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22824 if (!type_attr)
22825 error (_("Dwarf Error: Problem turning containing type into gdb type "
22826 "[in module %s]"), objfile_name (objfile));
22827
22828 return lookup_die_type (die, type_attr, cu);
22829 }
22830
22831 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22832
22833 static struct type *
22834 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22835 {
22836 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22837 struct objfile *objfile = per_objfile->objfile;
22838 char *saved;
22839
22840 std::string message
22841 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22842 objfile_name (objfile),
22843 sect_offset_str (cu->header.sect_off),
22844 sect_offset_str (die->sect_off));
22845 saved = obstack_strdup (&objfile->objfile_obstack, message);
22846
22847 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22848 }
22849
22850 /* Look up the type of DIE in CU using its type attribute ATTR.
22851 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22852 DW_AT_containing_type.
22853 If there is no type substitute an error marker. */
22854
22855 static struct type *
22856 lookup_die_type (struct die_info *die, const struct attribute *attr,
22857 struct dwarf2_cu *cu)
22858 {
22859 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22860 struct objfile *objfile = per_objfile->objfile;
22861 struct type *this_type;
22862
22863 gdb_assert (attr->name == DW_AT_type
22864 || attr->name == DW_AT_GNAT_descriptive_type
22865 || attr->name == DW_AT_containing_type);
22866
22867 /* First see if we have it cached. */
22868
22869 if (attr->form == DW_FORM_GNU_ref_alt)
22870 {
22871 struct dwarf2_per_cu_data *per_cu;
22872 sect_offset sect_off = attr->get_ref_die_offset ();
22873
22874 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22875 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22876 }
22877 else if (attr->form_is_ref ())
22878 {
22879 sect_offset sect_off = attr->get_ref_die_offset ();
22880
22881 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22882 }
22883 else if (attr->form == DW_FORM_ref_sig8)
22884 {
22885 ULONGEST signature = attr->as_signature ();
22886
22887 return get_signatured_type (die, signature, cu);
22888 }
22889 else
22890 {
22891 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22892 " at %s [in module %s]"),
22893 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22894 objfile_name (objfile));
22895 return build_error_marker_type (cu, die);
22896 }
22897
22898 /* If not cached we need to read it in. */
22899
22900 if (this_type == NULL)
22901 {
22902 struct die_info *type_die = NULL;
22903 struct dwarf2_cu *type_cu = cu;
22904
22905 if (attr->form_is_ref ())
22906 type_die = follow_die_ref (die, attr, &type_cu);
22907 if (type_die == NULL)
22908 return build_error_marker_type (cu, die);
22909 /* If we find the type now, it's probably because the type came
22910 from an inter-CU reference and the type's CU got expanded before
22911 ours. */
22912 this_type = read_type_die (type_die, type_cu);
22913 }
22914
22915 /* If we still don't have a type use an error marker. */
22916
22917 if (this_type == NULL)
22918 return build_error_marker_type (cu, die);
22919
22920 return this_type;
22921 }
22922
22923 /* Return the type in DIE, CU.
22924 Returns NULL for invalid types.
22925
22926 This first does a lookup in die_type_hash,
22927 and only reads the die in if necessary.
22928
22929 NOTE: This can be called when reading in partial or full symbols. */
22930
22931 static struct type *
22932 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22933 {
22934 struct type *this_type;
22935
22936 this_type = get_die_type (die, cu);
22937 if (this_type)
22938 return this_type;
22939
22940 return read_type_die_1 (die, cu);
22941 }
22942
22943 /* Read the type in DIE, CU.
22944 Returns NULL for invalid types. */
22945
22946 static struct type *
22947 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22948 {
22949 struct type *this_type = NULL;
22950
22951 switch (die->tag)
22952 {
22953 case DW_TAG_class_type:
22954 case DW_TAG_interface_type:
22955 case DW_TAG_structure_type:
22956 case DW_TAG_union_type:
22957 this_type = read_structure_type (die, cu);
22958 break;
22959 case DW_TAG_enumeration_type:
22960 this_type = read_enumeration_type (die, cu);
22961 break;
22962 case DW_TAG_subprogram:
22963 case DW_TAG_subroutine_type:
22964 case DW_TAG_inlined_subroutine:
22965 this_type = read_subroutine_type (die, cu);
22966 break;
22967 case DW_TAG_array_type:
22968 this_type = read_array_type (die, cu);
22969 break;
22970 case DW_TAG_set_type:
22971 this_type = read_set_type (die, cu);
22972 break;
22973 case DW_TAG_pointer_type:
22974 this_type = read_tag_pointer_type (die, cu);
22975 break;
22976 case DW_TAG_ptr_to_member_type:
22977 this_type = read_tag_ptr_to_member_type (die, cu);
22978 break;
22979 case DW_TAG_reference_type:
22980 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22981 break;
22982 case DW_TAG_rvalue_reference_type:
22983 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22984 break;
22985 case DW_TAG_const_type:
22986 this_type = read_tag_const_type (die, cu);
22987 break;
22988 case DW_TAG_volatile_type:
22989 this_type = read_tag_volatile_type (die, cu);
22990 break;
22991 case DW_TAG_restrict_type:
22992 this_type = read_tag_restrict_type (die, cu);
22993 break;
22994 case DW_TAG_string_type:
22995 this_type = read_tag_string_type (die, cu);
22996 break;
22997 case DW_TAG_typedef:
22998 this_type = read_typedef (die, cu);
22999 break;
23000 case DW_TAG_subrange_type:
23001 this_type = read_subrange_type (die, cu);
23002 break;
23003 case DW_TAG_base_type:
23004 this_type = read_base_type (die, cu);
23005 break;
23006 case DW_TAG_unspecified_type:
23007 this_type = read_unspecified_type (die, cu);
23008 break;
23009 case DW_TAG_namespace:
23010 this_type = read_namespace_type (die, cu);
23011 break;
23012 case DW_TAG_module:
23013 this_type = read_module_type (die, cu);
23014 break;
23015 case DW_TAG_atomic_type:
23016 this_type = read_tag_atomic_type (die, cu);
23017 break;
23018 default:
23019 complaint (_("unexpected tag in read_type_die: '%s'"),
23020 dwarf_tag_name (die->tag));
23021 break;
23022 }
23023
23024 return this_type;
23025 }
23026
23027 /* See if we can figure out if the class lives in a namespace. We do
23028 this by looking for a member function; its demangled name will
23029 contain namespace info, if there is any.
23030 Return the computed name or NULL.
23031 Space for the result is allocated on the objfile's obstack.
23032 This is the full-die version of guess_partial_die_structure_name.
23033 In this case we know DIE has no useful parent. */
23034
23035 static const char *
23036 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23037 {
23038 struct die_info *spec_die;
23039 struct dwarf2_cu *spec_cu;
23040 struct die_info *child;
23041 struct objfile *objfile = cu->per_objfile->objfile;
23042
23043 spec_cu = cu;
23044 spec_die = die_specification (die, &spec_cu);
23045 if (spec_die != NULL)
23046 {
23047 die = spec_die;
23048 cu = spec_cu;
23049 }
23050
23051 for (child = die->child;
23052 child != NULL;
23053 child = child->sibling)
23054 {
23055 if (child->tag == DW_TAG_subprogram)
23056 {
23057 const char *linkage_name = dw2_linkage_name (child, cu);
23058
23059 if (linkage_name != NULL)
23060 {
23061 gdb::unique_xmalloc_ptr<char> actual_name
23062 (cu->language_defn->class_name_from_physname (linkage_name));
23063 const char *name = NULL;
23064
23065 if (actual_name != NULL)
23066 {
23067 const char *die_name = dwarf2_name (die, cu);
23068
23069 if (die_name != NULL
23070 && strcmp (die_name, actual_name.get ()) != 0)
23071 {
23072 /* Strip off the class name from the full name.
23073 We want the prefix. */
23074 int die_name_len = strlen (die_name);
23075 int actual_name_len = strlen (actual_name.get ());
23076 const char *ptr = actual_name.get ();
23077
23078 /* Test for '::' as a sanity check. */
23079 if (actual_name_len > die_name_len + 2
23080 && ptr[actual_name_len - die_name_len - 1] == ':')
23081 name = obstack_strndup (
23082 &objfile->per_bfd->storage_obstack,
23083 ptr, actual_name_len - die_name_len - 2);
23084 }
23085 }
23086 return name;
23087 }
23088 }
23089 }
23090
23091 return NULL;
23092 }
23093
23094 /* GCC might emit a nameless typedef that has a linkage name. Determine the
23095 prefix part in such case. See
23096 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23097
23098 static const char *
23099 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23100 {
23101 struct attribute *attr;
23102 const char *base;
23103
23104 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23105 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23106 return NULL;
23107
23108 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
23109 return NULL;
23110
23111 attr = dw2_linkage_name_attr (die, cu);
23112 const char *attr_name = attr->as_string ();
23113 if (attr == NULL || attr_name == NULL)
23114 return NULL;
23115
23116 /* dwarf2_name had to be already called. */
23117 gdb_assert (attr->canonical_string_p ());
23118
23119 /* Strip the base name, keep any leading namespaces/classes. */
23120 base = strrchr (attr_name, ':');
23121 if (base == NULL || base == attr_name || base[-1] != ':')
23122 return "";
23123
23124 struct objfile *objfile = cu->per_objfile->objfile;
23125 return obstack_strndup (&objfile->per_bfd->storage_obstack,
23126 attr_name,
23127 &base[-1] - attr_name);
23128 }
23129
23130 /* Return the name of the namespace/class that DIE is defined within,
23131 or "" if we can't tell. The caller should not xfree the result.
23132
23133 For example, if we're within the method foo() in the following
23134 code:
23135
23136 namespace N {
23137 class C {
23138 void foo () {
23139 }
23140 };
23141 }
23142
23143 then determine_prefix on foo's die will return "N::C". */
23144
23145 static const char *
23146 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23147 {
23148 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23149 struct die_info *parent, *spec_die;
23150 struct dwarf2_cu *spec_cu;
23151 struct type *parent_type;
23152 const char *retval;
23153
23154 if (cu->language != language_cplus
23155 && cu->language != language_fortran && cu->language != language_d
23156 && cu->language != language_rust)
23157 return "";
23158
23159 retval = anonymous_struct_prefix (die, cu);
23160 if (retval)
23161 return retval;
23162
23163 /* We have to be careful in the presence of DW_AT_specification.
23164 For example, with GCC 3.4, given the code
23165
23166 namespace N {
23167 void foo() {
23168 // Definition of N::foo.
23169 }
23170 }
23171
23172 then we'll have a tree of DIEs like this:
23173
23174 1: DW_TAG_compile_unit
23175 2: DW_TAG_namespace // N
23176 3: DW_TAG_subprogram // declaration of N::foo
23177 4: DW_TAG_subprogram // definition of N::foo
23178 DW_AT_specification // refers to die #3
23179
23180 Thus, when processing die #4, we have to pretend that we're in
23181 the context of its DW_AT_specification, namely the contex of die
23182 #3. */
23183 spec_cu = cu;
23184 spec_die = die_specification (die, &spec_cu);
23185 if (spec_die == NULL)
23186 parent = die->parent;
23187 else
23188 {
23189 parent = spec_die->parent;
23190 cu = spec_cu;
23191 }
23192
23193 if (parent == NULL)
23194 return "";
23195 else if (parent->building_fullname)
23196 {
23197 const char *name;
23198 const char *parent_name;
23199
23200 /* It has been seen on RealView 2.2 built binaries,
23201 DW_TAG_template_type_param types actually _defined_ as
23202 children of the parent class:
23203
23204 enum E {};
23205 template class <class Enum> Class{};
23206 Class<enum E> class_e;
23207
23208 1: DW_TAG_class_type (Class)
23209 2: DW_TAG_enumeration_type (E)
23210 3: DW_TAG_enumerator (enum1:0)
23211 3: DW_TAG_enumerator (enum2:1)
23212 ...
23213 2: DW_TAG_template_type_param
23214 DW_AT_type DW_FORM_ref_udata (E)
23215
23216 Besides being broken debug info, it can put GDB into an
23217 infinite loop. Consider:
23218
23219 When we're building the full name for Class<E>, we'll start
23220 at Class, and go look over its template type parameters,
23221 finding E. We'll then try to build the full name of E, and
23222 reach here. We're now trying to build the full name of E,
23223 and look over the parent DIE for containing scope. In the
23224 broken case, if we followed the parent DIE of E, we'd again
23225 find Class, and once again go look at its template type
23226 arguments, etc., etc. Simply don't consider such parent die
23227 as source-level parent of this die (it can't be, the language
23228 doesn't allow it), and break the loop here. */
23229 name = dwarf2_name (die, cu);
23230 parent_name = dwarf2_name (parent, cu);
23231 complaint (_("template param type '%s' defined within parent '%s'"),
23232 name ? name : "<unknown>",
23233 parent_name ? parent_name : "<unknown>");
23234 return "";
23235 }
23236 else
23237 switch (parent->tag)
23238 {
23239 case DW_TAG_namespace:
23240 parent_type = read_type_die (parent, cu);
23241 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23242 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23243 Work around this problem here. */
23244 if (cu->language == language_cplus
23245 && strcmp (parent_type->name (), "::") == 0)
23246 return "";
23247 /* We give a name to even anonymous namespaces. */
23248 return parent_type->name ();
23249 case DW_TAG_class_type:
23250 case DW_TAG_interface_type:
23251 case DW_TAG_structure_type:
23252 case DW_TAG_union_type:
23253 case DW_TAG_module:
23254 parent_type = read_type_die (parent, cu);
23255 if (parent_type->name () != NULL)
23256 return parent_type->name ();
23257 else
23258 /* An anonymous structure is only allowed non-static data
23259 members; no typedefs, no member functions, et cetera.
23260 So it does not need a prefix. */
23261 return "";
23262 case DW_TAG_compile_unit:
23263 case DW_TAG_partial_unit:
23264 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23265 if (cu->language == language_cplus
23266 && !per_objfile->per_bfd->types.empty ()
23267 && die->child != NULL
23268 && (die->tag == DW_TAG_class_type
23269 || die->tag == DW_TAG_structure_type
23270 || die->tag == DW_TAG_union_type))
23271 {
23272 const char *name = guess_full_die_structure_name (die, cu);
23273 if (name != NULL)
23274 return name;
23275 }
23276 return "";
23277 case DW_TAG_subprogram:
23278 /* Nested subroutines in Fortran get a prefix with the name
23279 of the parent's subroutine. */
23280 if (cu->language == language_fortran)
23281 {
23282 if ((die->tag == DW_TAG_subprogram)
23283 && (dwarf2_name (parent, cu) != NULL))
23284 return dwarf2_name (parent, cu);
23285 }
23286 return determine_prefix (parent, cu);
23287 case DW_TAG_enumeration_type:
23288 parent_type = read_type_die (parent, cu);
23289 if (TYPE_DECLARED_CLASS (parent_type))
23290 {
23291 if (parent_type->name () != NULL)
23292 return parent_type->name ();
23293 return "";
23294 }
23295 /* Fall through. */
23296 default:
23297 return determine_prefix (parent, cu);
23298 }
23299 }
23300
23301 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23302 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23303 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23304 an obconcat, otherwise allocate storage for the result. The CU argument is
23305 used to determine the language and hence, the appropriate separator. */
23306
23307 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23308
23309 static char *
23310 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23311 int physname, struct dwarf2_cu *cu)
23312 {
23313 const char *lead = "";
23314 const char *sep;
23315
23316 if (suffix == NULL || suffix[0] == '\0'
23317 || prefix == NULL || prefix[0] == '\0')
23318 sep = "";
23319 else if (cu->language == language_d)
23320 {
23321 /* For D, the 'main' function could be defined in any module, but it
23322 should never be prefixed. */
23323 if (strcmp (suffix, "D main") == 0)
23324 {
23325 prefix = "";
23326 sep = "";
23327 }
23328 else
23329 sep = ".";
23330 }
23331 else if (cu->language == language_fortran && physname)
23332 {
23333 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23334 DW_AT_MIPS_linkage_name is preferred and used instead. */
23335
23336 lead = "__";
23337 sep = "_MOD_";
23338 }
23339 else
23340 sep = "::";
23341
23342 if (prefix == NULL)
23343 prefix = "";
23344 if (suffix == NULL)
23345 suffix = "";
23346
23347 if (obs == NULL)
23348 {
23349 char *retval
23350 = ((char *)
23351 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23352
23353 strcpy (retval, lead);
23354 strcat (retval, prefix);
23355 strcat (retval, sep);
23356 strcat (retval, suffix);
23357 return retval;
23358 }
23359 else
23360 {
23361 /* We have an obstack. */
23362 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23363 }
23364 }
23365
23366 /* Get name of a die, return NULL if not found. */
23367
23368 static const char *
23369 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23370 struct objfile *objfile)
23371 {
23372 if (name && cu->language == language_cplus)
23373 {
23374 gdb::unique_xmalloc_ptr<char> canon_name
23375 = cp_canonicalize_string (name);
23376
23377 if (canon_name != nullptr)
23378 name = objfile->intern (canon_name.get ());
23379 }
23380
23381 return name;
23382 }
23383
23384 /* Get name of a die, return NULL if not found.
23385 Anonymous namespaces are converted to their magic string. */
23386
23387 static const char *
23388 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23389 {
23390 struct attribute *attr;
23391 struct objfile *objfile = cu->per_objfile->objfile;
23392
23393 attr = dwarf2_attr (die, DW_AT_name, cu);
23394 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23395 if (attr_name == nullptr
23396 && die->tag != DW_TAG_namespace
23397 && die->tag != DW_TAG_class_type
23398 && die->tag != DW_TAG_interface_type
23399 && die->tag != DW_TAG_structure_type
23400 && die->tag != DW_TAG_union_type)
23401 return NULL;
23402
23403 switch (die->tag)
23404 {
23405 case DW_TAG_compile_unit:
23406 case DW_TAG_partial_unit:
23407 /* Compilation units have a DW_AT_name that is a filename, not
23408 a source language identifier. */
23409 case DW_TAG_enumeration_type:
23410 case DW_TAG_enumerator:
23411 /* These tags always have simple identifiers already; no need
23412 to canonicalize them. */
23413 return attr_name;
23414
23415 case DW_TAG_namespace:
23416 if (attr_name != nullptr)
23417 return attr_name;
23418 return CP_ANONYMOUS_NAMESPACE_STR;
23419
23420 case DW_TAG_class_type:
23421 case DW_TAG_interface_type:
23422 case DW_TAG_structure_type:
23423 case DW_TAG_union_type:
23424 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23425 structures or unions. These were of the form "._%d" in GCC 4.1,
23426 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23427 and GCC 4.4. We work around this problem by ignoring these. */
23428 if (attr_name != nullptr
23429 && (startswith (attr_name, "._")
23430 || startswith (attr_name, "<anonymous")))
23431 return NULL;
23432
23433 /* GCC might emit a nameless typedef that has a linkage name. See
23434 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23435 if (!attr || attr_name == NULL)
23436 {
23437 attr = dw2_linkage_name_attr (die, cu);
23438 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23439 if (attr == NULL || attr_name == NULL)
23440 return NULL;
23441
23442 /* Avoid demangling attr_name the second time on a second
23443 call for the same DIE. */
23444 if (!attr->canonical_string_p ())
23445 {
23446 gdb::unique_xmalloc_ptr<char> demangled
23447 (gdb_demangle (attr_name, DMGL_TYPES));
23448 if (demangled == nullptr)
23449 return nullptr;
23450
23451 attr->set_string_canonical (objfile->intern (demangled.get ()));
23452 attr_name = attr->as_string ();
23453 }
23454
23455 /* Strip any leading namespaces/classes, keep only the
23456 base name. DW_AT_name for named DIEs does not
23457 contain the prefixes. */
23458 const char *base = strrchr (attr_name, ':');
23459 if (base && base > attr_name && base[-1] == ':')
23460 return &base[1];
23461 else
23462 return attr_name;
23463 }
23464 break;
23465
23466 default:
23467 break;
23468 }
23469
23470 if (!attr->canonical_string_p ())
23471 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23472 objfile));
23473 return attr->as_string ();
23474 }
23475
23476 /* Return the die that this die in an extension of, or NULL if there
23477 is none. *EXT_CU is the CU containing DIE on input, and the CU
23478 containing the return value on output. */
23479
23480 static struct die_info *
23481 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23482 {
23483 struct attribute *attr;
23484
23485 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23486 if (attr == NULL)
23487 return NULL;
23488
23489 return follow_die_ref (die, attr, ext_cu);
23490 }
23491
23492 static void
23493 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23494 {
23495 unsigned int i;
23496
23497 print_spaces (indent, f);
23498 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23499 dwarf_tag_name (die->tag), die->abbrev,
23500 sect_offset_str (die->sect_off));
23501
23502 if (die->parent != NULL)
23503 {
23504 print_spaces (indent, f);
23505 fprintf_unfiltered (f, " parent at offset: %s\n",
23506 sect_offset_str (die->parent->sect_off));
23507 }
23508
23509 print_spaces (indent, f);
23510 fprintf_unfiltered (f, " has children: %s\n",
23511 dwarf_bool_name (die->child != NULL));
23512
23513 print_spaces (indent, f);
23514 fprintf_unfiltered (f, " attributes:\n");
23515
23516 for (i = 0; i < die->num_attrs; ++i)
23517 {
23518 print_spaces (indent, f);
23519 fprintf_unfiltered (f, " %s (%s) ",
23520 dwarf_attr_name (die->attrs[i].name),
23521 dwarf_form_name (die->attrs[i].form));
23522
23523 switch (die->attrs[i].form)
23524 {
23525 case DW_FORM_addr:
23526 case DW_FORM_addrx:
23527 case DW_FORM_GNU_addr_index:
23528 fprintf_unfiltered (f, "address: ");
23529 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23530 break;
23531 case DW_FORM_block2:
23532 case DW_FORM_block4:
23533 case DW_FORM_block:
23534 case DW_FORM_block1:
23535 fprintf_unfiltered (f, "block: size %s",
23536 pulongest (die->attrs[i].as_block ()->size));
23537 break;
23538 case DW_FORM_exprloc:
23539 fprintf_unfiltered (f, "expression: size %s",
23540 pulongest (die->attrs[i].as_block ()->size));
23541 break;
23542 case DW_FORM_data16:
23543 fprintf_unfiltered (f, "constant of 16 bytes");
23544 break;
23545 case DW_FORM_ref_addr:
23546 fprintf_unfiltered (f, "ref address: ");
23547 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23548 break;
23549 case DW_FORM_GNU_ref_alt:
23550 fprintf_unfiltered (f, "alt ref address: ");
23551 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23552 break;
23553 case DW_FORM_ref1:
23554 case DW_FORM_ref2:
23555 case DW_FORM_ref4:
23556 case DW_FORM_ref8:
23557 case DW_FORM_ref_udata:
23558 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23559 (long) (die->attrs[i].as_unsigned ()));
23560 break;
23561 case DW_FORM_data1:
23562 case DW_FORM_data2:
23563 case DW_FORM_data4:
23564 case DW_FORM_data8:
23565 case DW_FORM_udata:
23566 fprintf_unfiltered (f, "constant: %s",
23567 pulongest (die->attrs[i].as_unsigned ()));
23568 break;
23569 case DW_FORM_sec_offset:
23570 fprintf_unfiltered (f, "section offset: %s",
23571 pulongest (die->attrs[i].as_unsigned ()));
23572 break;
23573 case DW_FORM_ref_sig8:
23574 fprintf_unfiltered (f, "signature: %s",
23575 hex_string (die->attrs[i].as_signature ()));
23576 break;
23577 case DW_FORM_string:
23578 case DW_FORM_strp:
23579 case DW_FORM_line_strp:
23580 case DW_FORM_strx:
23581 case DW_FORM_GNU_str_index:
23582 case DW_FORM_GNU_strp_alt:
23583 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23584 die->attrs[i].as_string ()
23585 ? die->attrs[i].as_string () : "",
23586 die->attrs[i].canonical_string_p () ? "is" : "not");
23587 break;
23588 case DW_FORM_flag:
23589 if (die->attrs[i].as_boolean ())
23590 fprintf_unfiltered (f, "flag: TRUE");
23591 else
23592 fprintf_unfiltered (f, "flag: FALSE");
23593 break;
23594 case DW_FORM_flag_present:
23595 fprintf_unfiltered (f, "flag: TRUE");
23596 break;
23597 case DW_FORM_indirect:
23598 /* The reader will have reduced the indirect form to
23599 the "base form" so this form should not occur. */
23600 fprintf_unfiltered (f,
23601 "unexpected attribute form: DW_FORM_indirect");
23602 break;
23603 case DW_FORM_sdata:
23604 case DW_FORM_implicit_const:
23605 fprintf_unfiltered (f, "constant: %s",
23606 plongest (die->attrs[i].as_signed ()));
23607 break;
23608 default:
23609 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23610 die->attrs[i].form);
23611 break;
23612 }
23613 fprintf_unfiltered (f, "\n");
23614 }
23615 }
23616
23617 static void
23618 dump_die_for_error (struct die_info *die)
23619 {
23620 dump_die_shallow (gdb_stderr, 0, die);
23621 }
23622
23623 static void
23624 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23625 {
23626 int indent = level * 4;
23627
23628 gdb_assert (die != NULL);
23629
23630 if (level >= max_level)
23631 return;
23632
23633 dump_die_shallow (f, indent, die);
23634
23635 if (die->child != NULL)
23636 {
23637 print_spaces (indent, f);
23638 fprintf_unfiltered (f, " Children:");
23639 if (level + 1 < max_level)
23640 {
23641 fprintf_unfiltered (f, "\n");
23642 dump_die_1 (f, level + 1, max_level, die->child);
23643 }
23644 else
23645 {
23646 fprintf_unfiltered (f,
23647 " [not printed, max nesting level reached]\n");
23648 }
23649 }
23650
23651 if (die->sibling != NULL && level > 0)
23652 {
23653 dump_die_1 (f, level, max_level, die->sibling);
23654 }
23655 }
23656
23657 /* This is called from the pdie macro in gdbinit.in.
23658 It's not static so gcc will keep a copy callable from gdb. */
23659
23660 void
23661 dump_die (struct die_info *die, int max_level)
23662 {
23663 dump_die_1 (gdb_stdlog, 0, max_level, die);
23664 }
23665
23666 static void
23667 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23668 {
23669 void **slot;
23670
23671 slot = htab_find_slot_with_hash (cu->die_hash, die,
23672 to_underlying (die->sect_off),
23673 INSERT);
23674
23675 *slot = die;
23676 }
23677
23678 /* Follow reference or signature attribute ATTR of SRC_DIE.
23679 On entry *REF_CU is the CU of SRC_DIE.
23680 On exit *REF_CU is the CU of the result. */
23681
23682 static struct die_info *
23683 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23684 struct dwarf2_cu **ref_cu)
23685 {
23686 struct die_info *die;
23687
23688 if (attr->form_is_ref ())
23689 die = follow_die_ref (src_die, attr, ref_cu);
23690 else if (attr->form == DW_FORM_ref_sig8)
23691 die = follow_die_sig (src_die, attr, ref_cu);
23692 else
23693 {
23694 dump_die_for_error (src_die);
23695 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23696 objfile_name ((*ref_cu)->per_objfile->objfile));
23697 }
23698
23699 return die;
23700 }
23701
23702 /* Follow reference OFFSET.
23703 On entry *REF_CU is the CU of the source die referencing OFFSET.
23704 On exit *REF_CU is the CU of the result.
23705 Returns NULL if OFFSET is invalid. */
23706
23707 static struct die_info *
23708 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23709 struct dwarf2_cu **ref_cu)
23710 {
23711 struct die_info temp_die;
23712 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23713 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23714
23715 gdb_assert (cu->per_cu != NULL);
23716
23717 target_cu = cu;
23718
23719 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23720 "source CU contains target offset: %d",
23721 sect_offset_str (cu->per_cu->sect_off),
23722 sect_offset_str (sect_off),
23723 cu->header.offset_in_cu_p (sect_off));
23724
23725 if (cu->per_cu->is_debug_types)
23726 {
23727 /* .debug_types CUs cannot reference anything outside their CU.
23728 If they need to, they have to reference a signatured type via
23729 DW_FORM_ref_sig8. */
23730 if (!cu->header.offset_in_cu_p (sect_off))
23731 return NULL;
23732 }
23733 else if (offset_in_dwz != cu->per_cu->is_dwz
23734 || !cu->header.offset_in_cu_p (sect_off))
23735 {
23736 struct dwarf2_per_cu_data *per_cu;
23737
23738 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23739 per_objfile);
23740
23741 dwarf_read_debug_printf_v ("target CU offset: %s, "
23742 "target CU DIEs loaded: %d",
23743 sect_offset_str (per_cu->sect_off),
23744 per_objfile->get_cu (per_cu) != nullptr);
23745
23746 /* If necessary, add it to the queue and load its DIEs.
23747
23748 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23749 it doesn't mean they are currently loaded. Since we require them
23750 to be loaded, we must check for ourselves. */
23751 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23752 || per_objfile->get_cu (per_cu) == nullptr)
23753 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23754 false, cu->language);
23755
23756 target_cu = per_objfile->get_cu (per_cu);
23757 gdb_assert (target_cu != nullptr);
23758 }
23759 else if (cu->dies == NULL)
23760 {
23761 /* We're loading full DIEs during partial symbol reading. */
23762 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23763 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23764 language_minimal);
23765 }
23766
23767 *ref_cu = target_cu;
23768 temp_die.sect_off = sect_off;
23769
23770 if (target_cu != cu)
23771 target_cu->ancestor = cu;
23772
23773 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23774 &temp_die,
23775 to_underlying (sect_off));
23776 }
23777
23778 /* Follow reference attribute ATTR of SRC_DIE.
23779 On entry *REF_CU is the CU of SRC_DIE.
23780 On exit *REF_CU is the CU of the result. */
23781
23782 static struct die_info *
23783 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23784 struct dwarf2_cu **ref_cu)
23785 {
23786 sect_offset sect_off = attr->get_ref_die_offset ();
23787 struct dwarf2_cu *cu = *ref_cu;
23788 struct die_info *die;
23789
23790 die = follow_die_offset (sect_off,
23791 (attr->form == DW_FORM_GNU_ref_alt
23792 || cu->per_cu->is_dwz),
23793 ref_cu);
23794 if (!die)
23795 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23796 "at %s [in module %s]"),
23797 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23798 objfile_name (cu->per_objfile->objfile));
23799
23800 return die;
23801 }
23802
23803 /* See read.h. */
23804
23805 struct dwarf2_locexpr_baton
23806 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23807 dwarf2_per_cu_data *per_cu,
23808 dwarf2_per_objfile *per_objfile,
23809 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23810 bool resolve_abstract_p)
23811 {
23812 struct die_info *die;
23813 struct attribute *attr;
23814 struct dwarf2_locexpr_baton retval;
23815 struct objfile *objfile = per_objfile->objfile;
23816
23817 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23818 if (cu == nullptr)
23819 cu = load_cu (per_cu, per_objfile, false);
23820
23821 if (cu == nullptr)
23822 {
23823 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23824 Instead just throw an error, not much else we can do. */
23825 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23826 sect_offset_str (sect_off), objfile_name (objfile));
23827 }
23828
23829 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23830 if (!die)
23831 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23832 sect_offset_str (sect_off), objfile_name (objfile));
23833
23834 attr = dwarf2_attr (die, DW_AT_location, cu);
23835 if (!attr && resolve_abstract_p
23836 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23837 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23838 {
23839 CORE_ADDR pc = get_frame_pc ();
23840 CORE_ADDR baseaddr = objfile->text_section_offset ();
23841 struct gdbarch *gdbarch = objfile->arch ();
23842
23843 for (const auto &cand_off
23844 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23845 {
23846 struct dwarf2_cu *cand_cu = cu;
23847 struct die_info *cand
23848 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23849 if (!cand
23850 || !cand->parent
23851 || cand->parent->tag != DW_TAG_subprogram)
23852 continue;
23853
23854 CORE_ADDR pc_low, pc_high;
23855 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23856 if (pc_low == ((CORE_ADDR) -1))
23857 continue;
23858 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23859 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23860 if (!(pc_low <= pc && pc < pc_high))
23861 continue;
23862
23863 die = cand;
23864 attr = dwarf2_attr (die, DW_AT_location, cu);
23865 break;
23866 }
23867 }
23868
23869 if (!attr)
23870 {
23871 /* DWARF: "If there is no such attribute, then there is no effect.".
23872 DATA is ignored if SIZE is 0. */
23873
23874 retval.data = NULL;
23875 retval.size = 0;
23876 }
23877 else if (attr->form_is_section_offset ())
23878 {
23879 struct dwarf2_loclist_baton loclist_baton;
23880 CORE_ADDR pc = get_frame_pc ();
23881 size_t size;
23882
23883 fill_in_loclist_baton (cu, &loclist_baton, attr);
23884
23885 retval.data = dwarf2_find_location_expression (&loclist_baton,
23886 &size, pc);
23887 retval.size = size;
23888 }
23889 else
23890 {
23891 if (!attr->form_is_block ())
23892 error (_("Dwarf Error: DIE at %s referenced in module %s "
23893 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23894 sect_offset_str (sect_off), objfile_name (objfile));
23895
23896 struct dwarf_block *block = attr->as_block ();
23897 retval.data = block->data;
23898 retval.size = block->size;
23899 }
23900 retval.per_objfile = per_objfile;
23901 retval.per_cu = cu->per_cu;
23902
23903 per_objfile->age_comp_units ();
23904
23905 return retval;
23906 }
23907
23908 /* See read.h. */
23909
23910 struct dwarf2_locexpr_baton
23911 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23912 dwarf2_per_cu_data *per_cu,
23913 dwarf2_per_objfile *per_objfile,
23914 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23915 {
23916 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23917
23918 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23919 get_frame_pc);
23920 }
23921
23922 /* Write a constant of a given type as target-ordered bytes into
23923 OBSTACK. */
23924
23925 static const gdb_byte *
23926 write_constant_as_bytes (struct obstack *obstack,
23927 enum bfd_endian byte_order,
23928 struct type *type,
23929 ULONGEST value,
23930 LONGEST *len)
23931 {
23932 gdb_byte *result;
23933
23934 *len = TYPE_LENGTH (type);
23935 result = (gdb_byte *) obstack_alloc (obstack, *len);
23936 store_unsigned_integer (result, *len, byte_order, value);
23937
23938 return result;
23939 }
23940
23941 /* See read.h. */
23942
23943 const gdb_byte *
23944 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23945 dwarf2_per_cu_data *per_cu,
23946 dwarf2_per_objfile *per_objfile,
23947 obstack *obstack,
23948 LONGEST *len)
23949 {
23950 struct die_info *die;
23951 struct attribute *attr;
23952 const gdb_byte *result = NULL;
23953 struct type *type;
23954 LONGEST value;
23955 enum bfd_endian byte_order;
23956 struct objfile *objfile = per_objfile->objfile;
23957
23958 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23959 if (cu == nullptr)
23960 cu = load_cu (per_cu, per_objfile, false);
23961
23962 if (cu == nullptr)
23963 {
23964 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23965 Instead just throw an error, not much else we can do. */
23966 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23967 sect_offset_str (sect_off), objfile_name (objfile));
23968 }
23969
23970 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23971 if (!die)
23972 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23973 sect_offset_str (sect_off), objfile_name (objfile));
23974
23975 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23976 if (attr == NULL)
23977 return NULL;
23978
23979 byte_order = (bfd_big_endian (objfile->obfd)
23980 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23981
23982 switch (attr->form)
23983 {
23984 case DW_FORM_addr:
23985 case DW_FORM_addrx:
23986 case DW_FORM_GNU_addr_index:
23987 {
23988 gdb_byte *tem;
23989
23990 *len = cu->header.addr_size;
23991 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23992 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23993 result = tem;
23994 }
23995 break;
23996 case DW_FORM_string:
23997 case DW_FORM_strp:
23998 case DW_FORM_strx:
23999 case DW_FORM_GNU_str_index:
24000 case DW_FORM_GNU_strp_alt:
24001 /* The string is already allocated on the objfile obstack, point
24002 directly to it. */
24003 {
24004 const char *attr_name = attr->as_string ();
24005 result = (const gdb_byte *) attr_name;
24006 *len = strlen (attr_name);
24007 }
24008 break;
24009 case DW_FORM_block1:
24010 case DW_FORM_block2:
24011 case DW_FORM_block4:
24012 case DW_FORM_block:
24013 case DW_FORM_exprloc:
24014 case DW_FORM_data16:
24015 {
24016 struct dwarf_block *block = attr->as_block ();
24017 result = block->data;
24018 *len = block->size;
24019 }
24020 break;
24021
24022 /* The DW_AT_const_value attributes are supposed to carry the
24023 symbol's value "represented as it would be on the target
24024 architecture." By the time we get here, it's already been
24025 converted to host endianness, so we just need to sign- or
24026 zero-extend it as appropriate. */
24027 case DW_FORM_data1:
24028 type = die_type (die, cu);
24029 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24030 if (result == NULL)
24031 result = write_constant_as_bytes (obstack, byte_order,
24032 type, value, len);
24033 break;
24034 case DW_FORM_data2:
24035 type = die_type (die, cu);
24036 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24037 if (result == NULL)
24038 result = write_constant_as_bytes (obstack, byte_order,
24039 type, value, len);
24040 break;
24041 case DW_FORM_data4:
24042 type = die_type (die, cu);
24043 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24044 if (result == NULL)
24045 result = write_constant_as_bytes (obstack, byte_order,
24046 type, value, len);
24047 break;
24048 case DW_FORM_data8:
24049 type = die_type (die, cu);
24050 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24051 if (result == NULL)
24052 result = write_constant_as_bytes (obstack, byte_order,
24053 type, value, len);
24054 break;
24055
24056 case DW_FORM_sdata:
24057 case DW_FORM_implicit_const:
24058 type = die_type (die, cu);
24059 result = write_constant_as_bytes (obstack, byte_order,
24060 type, attr->as_signed (), len);
24061 break;
24062
24063 case DW_FORM_udata:
24064 type = die_type (die, cu);
24065 result = write_constant_as_bytes (obstack, byte_order,
24066 type, attr->as_unsigned (), len);
24067 break;
24068
24069 default:
24070 complaint (_("unsupported const value attribute form: '%s'"),
24071 dwarf_form_name (attr->form));
24072 break;
24073 }
24074
24075 return result;
24076 }
24077
24078 /* See read.h. */
24079
24080 struct type *
24081 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
24082 dwarf2_per_cu_data *per_cu,
24083 dwarf2_per_objfile *per_objfile)
24084 {
24085 struct die_info *die;
24086
24087 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24088 if (cu == nullptr)
24089 cu = load_cu (per_cu, per_objfile, false);
24090
24091 if (cu == nullptr)
24092 return nullptr;
24093
24094 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24095 if (!die)
24096 return NULL;
24097
24098 return die_type (die, cu);
24099 }
24100
24101 /* See read.h. */
24102
24103 struct type *
24104 dwarf2_get_die_type (cu_offset die_offset,
24105 dwarf2_per_cu_data *per_cu,
24106 dwarf2_per_objfile *per_objfile)
24107 {
24108 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
24109 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
24110 }
24111
24112 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
24113 On entry *REF_CU is the CU of SRC_DIE.
24114 On exit *REF_CU is the CU of the result.
24115 Returns NULL if the referenced DIE isn't found. */
24116
24117 static struct die_info *
24118 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24119 struct dwarf2_cu **ref_cu)
24120 {
24121 struct die_info temp_die;
24122 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
24123 struct die_info *die;
24124 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
24125
24126
24127 /* While it might be nice to assert sig_type->type == NULL here,
24128 we can get here for DW_AT_imported_declaration where we need
24129 the DIE not the type. */
24130
24131 /* If necessary, add it to the queue and load its DIEs.
24132
24133 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24134 it doesn't mean they are currently loaded. Since we require them
24135 to be loaded, we must check for ourselves. */
24136 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
24137 language_minimal)
24138 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
24139 read_signatured_type (sig_type, per_objfile);
24140
24141 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
24142 gdb_assert (sig_cu != NULL);
24143 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24144 temp_die.sect_off = sig_type->type_offset_in_section;
24145 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24146 to_underlying (temp_die.sect_off));
24147 if (die)
24148 {
24149 /* For .gdb_index version 7 keep track of included TUs.
24150 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24151 if (per_objfile->per_bfd->index_table != NULL
24152 && per_objfile->per_bfd->index_table->version <= 7)
24153 {
24154 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24155 }
24156
24157 *ref_cu = sig_cu;
24158 if (sig_cu != cu)
24159 sig_cu->ancestor = cu;
24160
24161 return die;
24162 }
24163
24164 return NULL;
24165 }
24166
24167 /* Follow signatured type referenced by ATTR in SRC_DIE.
24168 On entry *REF_CU is the CU of SRC_DIE.
24169 On exit *REF_CU is the CU of the result.
24170 The result is the DIE of the type.
24171 If the referenced type cannot be found an error is thrown. */
24172
24173 static struct die_info *
24174 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24175 struct dwarf2_cu **ref_cu)
24176 {
24177 ULONGEST signature = attr->as_signature ();
24178 struct signatured_type *sig_type;
24179 struct die_info *die;
24180
24181 gdb_assert (attr->form == DW_FORM_ref_sig8);
24182
24183 sig_type = lookup_signatured_type (*ref_cu, signature);
24184 /* sig_type will be NULL if the signatured type is missing from
24185 the debug info. */
24186 if (sig_type == NULL)
24187 {
24188 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24189 " from DIE at %s [in module %s]"),
24190 hex_string (signature), sect_offset_str (src_die->sect_off),
24191 objfile_name ((*ref_cu)->per_objfile->objfile));
24192 }
24193
24194 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24195 if (die == NULL)
24196 {
24197 dump_die_for_error (src_die);
24198 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24199 " from DIE at %s [in module %s]"),
24200 hex_string (signature), sect_offset_str (src_die->sect_off),
24201 objfile_name ((*ref_cu)->per_objfile->objfile));
24202 }
24203
24204 return die;
24205 }
24206
24207 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24208 reading in and processing the type unit if necessary. */
24209
24210 static struct type *
24211 get_signatured_type (struct die_info *die, ULONGEST signature,
24212 struct dwarf2_cu *cu)
24213 {
24214 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24215 struct signatured_type *sig_type;
24216 struct dwarf2_cu *type_cu;
24217 struct die_info *type_die;
24218 struct type *type;
24219
24220 sig_type = lookup_signatured_type (cu, signature);
24221 /* sig_type will be NULL if the signatured type is missing from
24222 the debug info. */
24223 if (sig_type == NULL)
24224 {
24225 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24226 " from DIE at %s [in module %s]"),
24227 hex_string (signature), sect_offset_str (die->sect_off),
24228 objfile_name (per_objfile->objfile));
24229 return build_error_marker_type (cu, die);
24230 }
24231
24232 /* If we already know the type we're done. */
24233 type = per_objfile->get_type_for_signatured_type (sig_type);
24234 if (type != nullptr)
24235 return type;
24236
24237 type_cu = cu;
24238 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24239 if (type_die != NULL)
24240 {
24241 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24242 is created. This is important, for example, because for c++ classes
24243 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24244 type = read_type_die (type_die, type_cu);
24245 if (type == NULL)
24246 {
24247 complaint (_("Dwarf Error: Cannot build signatured type %s"
24248 " referenced from DIE at %s [in module %s]"),
24249 hex_string (signature), sect_offset_str (die->sect_off),
24250 objfile_name (per_objfile->objfile));
24251 type = build_error_marker_type (cu, die);
24252 }
24253 }
24254 else
24255 {
24256 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24257 " from DIE at %s [in module %s]"),
24258 hex_string (signature), sect_offset_str (die->sect_off),
24259 objfile_name (per_objfile->objfile));
24260 type = build_error_marker_type (cu, die);
24261 }
24262
24263 per_objfile->set_type_for_signatured_type (sig_type, type);
24264
24265 return type;
24266 }
24267
24268 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24269 reading in and processing the type unit if necessary. */
24270
24271 static struct type *
24272 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24273 struct dwarf2_cu *cu) /* ARI: editCase function */
24274 {
24275 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24276 if (attr->form_is_ref ())
24277 {
24278 struct dwarf2_cu *type_cu = cu;
24279 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24280
24281 return read_type_die (type_die, type_cu);
24282 }
24283 else if (attr->form == DW_FORM_ref_sig8)
24284 {
24285 return get_signatured_type (die, attr->as_signature (), cu);
24286 }
24287 else
24288 {
24289 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24290
24291 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24292 " at %s [in module %s]"),
24293 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24294 objfile_name (per_objfile->objfile));
24295 return build_error_marker_type (cu, die);
24296 }
24297 }
24298
24299 /* Load the DIEs associated with type unit PER_CU into memory. */
24300
24301 static void
24302 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24303 dwarf2_per_objfile *per_objfile)
24304 {
24305 struct signatured_type *sig_type;
24306
24307 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24308 gdb_assert (! per_cu->type_unit_group_p ());
24309
24310 /* We have the per_cu, but we need the signatured_type.
24311 Fortunately this is an easy translation. */
24312 gdb_assert (per_cu->is_debug_types);
24313 sig_type = (struct signatured_type *) per_cu;
24314
24315 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24316
24317 read_signatured_type (sig_type, per_objfile);
24318
24319 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24320 }
24321
24322 /* Read in a signatured type and build its CU and DIEs.
24323 If the type is a stub for the real type in a DWO file,
24324 read in the real type from the DWO file as well. */
24325
24326 static void
24327 read_signatured_type (signatured_type *sig_type,
24328 dwarf2_per_objfile *per_objfile)
24329 {
24330 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24331
24332 gdb_assert (per_cu->is_debug_types);
24333 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24334
24335 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24336
24337 if (!reader.dummy_p)
24338 {
24339 struct dwarf2_cu *cu = reader.cu;
24340 const gdb_byte *info_ptr = reader.info_ptr;
24341
24342 gdb_assert (cu->die_hash == NULL);
24343 cu->die_hash =
24344 htab_create_alloc_ex (cu->header.length / 12,
24345 die_hash,
24346 die_eq,
24347 NULL,
24348 &cu->comp_unit_obstack,
24349 hashtab_obstack_allocate,
24350 dummy_obstack_deallocate);
24351
24352 if (reader.comp_unit_die->has_children)
24353 reader.comp_unit_die->child
24354 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24355 reader.comp_unit_die);
24356 cu->dies = reader.comp_unit_die;
24357 /* comp_unit_die is not stored in die_hash, no need. */
24358
24359 /* We try not to read any attributes in this function, because
24360 not all CUs needed for references have been loaded yet, and
24361 symbol table processing isn't initialized. But we have to
24362 set the CU language, or we won't be able to build types
24363 correctly. Similarly, if we do not read the producer, we can
24364 not apply producer-specific interpretation. */
24365 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24366
24367 reader.keep ();
24368 }
24369
24370 sig_type->per_cu.tu_read = 1;
24371 }
24372
24373 /* Decode simple location descriptions.
24374 Given a pointer to a dwarf block that defines a location, compute
24375 the location and return the value. If COMPUTED is non-null, it is
24376 set to true to indicate that decoding was successful, and false
24377 otherwise. If COMPUTED is null, then this function may emit a
24378 complaint. */
24379
24380 static CORE_ADDR
24381 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24382 {
24383 struct objfile *objfile = cu->per_objfile->objfile;
24384 size_t i;
24385 size_t size = blk->size;
24386 const gdb_byte *data = blk->data;
24387 CORE_ADDR stack[64];
24388 int stacki;
24389 unsigned int bytes_read, unsnd;
24390 gdb_byte op;
24391
24392 if (computed != nullptr)
24393 *computed = false;
24394
24395 i = 0;
24396 stacki = 0;
24397 stack[stacki] = 0;
24398 stack[++stacki] = 0;
24399
24400 while (i < size)
24401 {
24402 op = data[i++];
24403 switch (op)
24404 {
24405 case DW_OP_lit0:
24406 case DW_OP_lit1:
24407 case DW_OP_lit2:
24408 case DW_OP_lit3:
24409 case DW_OP_lit4:
24410 case DW_OP_lit5:
24411 case DW_OP_lit6:
24412 case DW_OP_lit7:
24413 case DW_OP_lit8:
24414 case DW_OP_lit9:
24415 case DW_OP_lit10:
24416 case DW_OP_lit11:
24417 case DW_OP_lit12:
24418 case DW_OP_lit13:
24419 case DW_OP_lit14:
24420 case DW_OP_lit15:
24421 case DW_OP_lit16:
24422 case DW_OP_lit17:
24423 case DW_OP_lit18:
24424 case DW_OP_lit19:
24425 case DW_OP_lit20:
24426 case DW_OP_lit21:
24427 case DW_OP_lit22:
24428 case DW_OP_lit23:
24429 case DW_OP_lit24:
24430 case DW_OP_lit25:
24431 case DW_OP_lit26:
24432 case DW_OP_lit27:
24433 case DW_OP_lit28:
24434 case DW_OP_lit29:
24435 case DW_OP_lit30:
24436 case DW_OP_lit31:
24437 stack[++stacki] = op - DW_OP_lit0;
24438 break;
24439
24440 case DW_OP_reg0:
24441 case DW_OP_reg1:
24442 case DW_OP_reg2:
24443 case DW_OP_reg3:
24444 case DW_OP_reg4:
24445 case DW_OP_reg5:
24446 case DW_OP_reg6:
24447 case DW_OP_reg7:
24448 case DW_OP_reg8:
24449 case DW_OP_reg9:
24450 case DW_OP_reg10:
24451 case DW_OP_reg11:
24452 case DW_OP_reg12:
24453 case DW_OP_reg13:
24454 case DW_OP_reg14:
24455 case DW_OP_reg15:
24456 case DW_OP_reg16:
24457 case DW_OP_reg17:
24458 case DW_OP_reg18:
24459 case DW_OP_reg19:
24460 case DW_OP_reg20:
24461 case DW_OP_reg21:
24462 case DW_OP_reg22:
24463 case DW_OP_reg23:
24464 case DW_OP_reg24:
24465 case DW_OP_reg25:
24466 case DW_OP_reg26:
24467 case DW_OP_reg27:
24468 case DW_OP_reg28:
24469 case DW_OP_reg29:
24470 case DW_OP_reg30:
24471 case DW_OP_reg31:
24472 stack[++stacki] = op - DW_OP_reg0;
24473 if (i < size)
24474 {
24475 if (computed == nullptr)
24476 dwarf2_complex_location_expr_complaint ();
24477 else
24478 return 0;
24479 }
24480 break;
24481
24482 case DW_OP_regx:
24483 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24484 i += bytes_read;
24485 stack[++stacki] = unsnd;
24486 if (i < size)
24487 {
24488 if (computed == nullptr)
24489 dwarf2_complex_location_expr_complaint ();
24490 else
24491 return 0;
24492 }
24493 break;
24494
24495 case DW_OP_addr:
24496 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24497 &bytes_read);
24498 i += bytes_read;
24499 break;
24500
24501 case DW_OP_const1u:
24502 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24503 i += 1;
24504 break;
24505
24506 case DW_OP_const1s:
24507 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24508 i += 1;
24509 break;
24510
24511 case DW_OP_const2u:
24512 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24513 i += 2;
24514 break;
24515
24516 case DW_OP_const2s:
24517 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24518 i += 2;
24519 break;
24520
24521 case DW_OP_const4u:
24522 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24523 i += 4;
24524 break;
24525
24526 case DW_OP_const4s:
24527 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24528 i += 4;
24529 break;
24530
24531 case DW_OP_const8u:
24532 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24533 i += 8;
24534 break;
24535
24536 case DW_OP_constu:
24537 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24538 &bytes_read);
24539 i += bytes_read;
24540 break;
24541
24542 case DW_OP_consts:
24543 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24544 i += bytes_read;
24545 break;
24546
24547 case DW_OP_dup:
24548 stack[stacki + 1] = stack[stacki];
24549 stacki++;
24550 break;
24551
24552 case DW_OP_plus:
24553 stack[stacki - 1] += stack[stacki];
24554 stacki--;
24555 break;
24556
24557 case DW_OP_plus_uconst:
24558 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24559 &bytes_read);
24560 i += bytes_read;
24561 break;
24562
24563 case DW_OP_minus:
24564 stack[stacki - 1] -= stack[stacki];
24565 stacki--;
24566 break;
24567
24568 case DW_OP_deref:
24569 /* If we're not the last op, then we definitely can't encode
24570 this using GDB's address_class enum. This is valid for partial
24571 global symbols, although the variable's address will be bogus
24572 in the psymtab. */
24573 if (i < size)
24574 {
24575 if (computed == nullptr)
24576 dwarf2_complex_location_expr_complaint ();
24577 else
24578 return 0;
24579 }
24580 break;
24581
24582 case DW_OP_GNU_push_tls_address:
24583 case DW_OP_form_tls_address:
24584 /* The top of the stack has the offset from the beginning
24585 of the thread control block at which the variable is located. */
24586 /* Nothing should follow this operator, so the top of stack would
24587 be returned. */
24588 /* This is valid for partial global symbols, but the variable's
24589 address will be bogus in the psymtab. Make it always at least
24590 non-zero to not look as a variable garbage collected by linker
24591 which have DW_OP_addr 0. */
24592 if (i < size)
24593 {
24594 if (computed == nullptr)
24595 dwarf2_complex_location_expr_complaint ();
24596 else
24597 return 0;
24598 }
24599 stack[stacki]++;
24600 break;
24601
24602 case DW_OP_GNU_uninit:
24603 if (computed != nullptr)
24604 return 0;
24605 break;
24606
24607 case DW_OP_addrx:
24608 case DW_OP_GNU_addr_index:
24609 case DW_OP_GNU_const_index:
24610 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24611 &bytes_read);
24612 i += bytes_read;
24613 break;
24614
24615 default:
24616 if (computed == nullptr)
24617 {
24618 const char *name = get_DW_OP_name (op);
24619
24620 if (name)
24621 complaint (_("unsupported stack op: '%s'"),
24622 name);
24623 else
24624 complaint (_("unsupported stack op: '%02x'"),
24625 op);
24626 }
24627
24628 return (stack[stacki]);
24629 }
24630
24631 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24632 outside of the allocated space. Also enforce minimum>0. */
24633 if (stacki >= ARRAY_SIZE (stack) - 1)
24634 {
24635 if (computed == nullptr)
24636 complaint (_("location description stack overflow"));
24637 return 0;
24638 }
24639
24640 if (stacki <= 0)
24641 {
24642 if (computed == nullptr)
24643 complaint (_("location description stack underflow"));
24644 return 0;
24645 }
24646 }
24647
24648 if (computed != nullptr)
24649 *computed = true;
24650 return (stack[stacki]);
24651 }
24652
24653 /* memory allocation interface */
24654
24655 static struct dwarf_block *
24656 dwarf_alloc_block (struct dwarf2_cu *cu)
24657 {
24658 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24659 }
24660
24661 static struct die_info *
24662 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24663 {
24664 struct die_info *die;
24665 size_t size = sizeof (struct die_info);
24666
24667 if (num_attrs > 1)
24668 size += (num_attrs - 1) * sizeof (struct attribute);
24669
24670 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24671 memset (die, 0, sizeof (struct die_info));
24672 return (die);
24673 }
24674
24675 \f
24676
24677 /* Macro support. */
24678
24679 /* An overload of dwarf_decode_macros that finds the correct section
24680 and ensures it is read in before calling the other overload. */
24681
24682 static void
24683 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24684 int section_is_gnu)
24685 {
24686 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24687 struct objfile *objfile = per_objfile->objfile;
24688 const struct line_header *lh = cu->line_header;
24689 unsigned int offset_size = cu->header.offset_size;
24690 struct dwarf2_section_info *section;
24691 const char *section_name;
24692
24693 if (cu->dwo_unit != nullptr)
24694 {
24695 if (section_is_gnu)
24696 {
24697 section = &cu->dwo_unit->dwo_file->sections.macro;
24698 section_name = ".debug_macro.dwo";
24699 }
24700 else
24701 {
24702 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24703 section_name = ".debug_macinfo.dwo";
24704 }
24705 }
24706 else
24707 {
24708 if (section_is_gnu)
24709 {
24710 section = &per_objfile->per_bfd->macro;
24711 section_name = ".debug_macro";
24712 }
24713 else
24714 {
24715 section = &per_objfile->per_bfd->macinfo;
24716 section_name = ".debug_macinfo";
24717 }
24718 }
24719
24720 section->read (objfile);
24721 if (section->buffer == nullptr)
24722 {
24723 complaint (_("missing %s section"), section_name);
24724 return;
24725 }
24726
24727 buildsym_compunit *builder = cu->get_builder ();
24728
24729 struct dwarf2_section_info *str_offsets_section;
24730 struct dwarf2_section_info *str_section;
24731 ULONGEST str_offsets_base;
24732
24733 if (cu->dwo_unit != nullptr)
24734 {
24735 str_offsets_section = &cu->dwo_unit->dwo_file
24736 ->sections.str_offsets;
24737 str_section = &cu->dwo_unit->dwo_file->sections.str;
24738 str_offsets_base = cu->header.addr_size;
24739 }
24740 else
24741 {
24742 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24743 str_section = &per_objfile->per_bfd->str;
24744 str_offsets_base = *cu->str_offsets_base;
24745 }
24746
24747 dwarf_decode_macros (per_objfile, builder, section, lh,
24748 offset_size, offset, str_section, str_offsets_section,
24749 str_offsets_base, section_is_gnu);
24750 }
24751
24752 /* Return the .debug_loc section to use for CU.
24753 For DWO files use .debug_loc.dwo. */
24754
24755 static struct dwarf2_section_info *
24756 cu_debug_loc_section (struct dwarf2_cu *cu)
24757 {
24758 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24759
24760 if (cu->dwo_unit)
24761 {
24762 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24763
24764 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24765 }
24766 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24767 : &per_objfile->per_bfd->loc);
24768 }
24769
24770 /* Return the .debug_rnglists section to use for CU. */
24771 static struct dwarf2_section_info *
24772 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24773 {
24774 if (cu->header.version < 5)
24775 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24776 cu->header.version);
24777 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24778
24779 /* Make sure we read the .debug_rnglists section from the file that
24780 contains the DW_AT_ranges attribute we are reading. Normally that
24781 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24782 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24783 program. */
24784 if (cu->dwo_unit != nullptr
24785 && tag != DW_TAG_compile_unit
24786 && tag != DW_TAG_skeleton_unit)
24787 {
24788 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24789
24790 if (sections->rnglists.size > 0)
24791 return &sections->rnglists;
24792 else
24793 error (_(".debug_rnglists section is missing from .dwo file."));
24794 }
24795 return &dwarf2_per_objfile->per_bfd->rnglists;
24796 }
24797
24798 /* A helper function that fills in a dwarf2_loclist_baton. */
24799
24800 static void
24801 fill_in_loclist_baton (struct dwarf2_cu *cu,
24802 struct dwarf2_loclist_baton *baton,
24803 const struct attribute *attr)
24804 {
24805 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24806 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24807
24808 section->read (per_objfile->objfile);
24809
24810 baton->per_objfile = per_objfile;
24811 baton->per_cu = cu->per_cu;
24812 gdb_assert (baton->per_cu);
24813 /* We don't know how long the location list is, but make sure we
24814 don't run off the edge of the section. */
24815 baton->size = section->size - attr->as_unsigned ();
24816 baton->data = section->buffer + attr->as_unsigned ();
24817 if (cu->base_address.has_value ())
24818 baton->base_address = *cu->base_address;
24819 else
24820 baton->base_address = 0;
24821 baton->from_dwo = cu->dwo_unit != NULL;
24822 }
24823
24824 static void
24825 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24826 struct dwarf2_cu *cu, int is_block)
24827 {
24828 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24829 struct objfile *objfile = per_objfile->objfile;
24830 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24831
24832 if (attr->form_is_section_offset ()
24833 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24834 the section. If so, fall through to the complaint in the
24835 other branch. */
24836 && attr->as_unsigned () < section->get_size (objfile))
24837 {
24838 struct dwarf2_loclist_baton *baton;
24839
24840 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24841
24842 fill_in_loclist_baton (cu, baton, attr);
24843
24844 if (!cu->base_address.has_value ())
24845 complaint (_("Location list used without "
24846 "specifying the CU base address."));
24847
24848 SYMBOL_ACLASS_INDEX (sym) = (is_block
24849 ? dwarf2_loclist_block_index
24850 : dwarf2_loclist_index);
24851 SYMBOL_LOCATION_BATON (sym) = baton;
24852 }
24853 else
24854 {
24855 struct dwarf2_locexpr_baton *baton;
24856
24857 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24858 baton->per_objfile = per_objfile;
24859 baton->per_cu = cu->per_cu;
24860 gdb_assert (baton->per_cu);
24861
24862 if (attr->form_is_block ())
24863 {
24864 /* Note that we're just copying the block's data pointer
24865 here, not the actual data. We're still pointing into the
24866 info_buffer for SYM's objfile; right now we never release
24867 that buffer, but when we do clean up properly this may
24868 need to change. */
24869 struct dwarf_block *block = attr->as_block ();
24870 baton->size = block->size;
24871 baton->data = block->data;
24872 }
24873 else
24874 {
24875 dwarf2_invalid_attrib_class_complaint ("location description",
24876 sym->natural_name ());
24877 baton->size = 0;
24878 }
24879
24880 SYMBOL_ACLASS_INDEX (sym) = (is_block
24881 ? dwarf2_locexpr_block_index
24882 : dwarf2_locexpr_index);
24883 SYMBOL_LOCATION_BATON (sym) = baton;
24884 }
24885 }
24886
24887 /* See read.h. */
24888
24889 const comp_unit_head *
24890 dwarf2_per_cu_data::get_header () const
24891 {
24892 if (!m_header_read_in)
24893 {
24894 const gdb_byte *info_ptr
24895 = this->section->buffer + to_underlying (this->sect_off);
24896
24897 memset (&m_header, 0, sizeof (m_header));
24898
24899 read_comp_unit_head (&m_header, info_ptr, this->section,
24900 rcuh_kind::COMPILE);
24901
24902 m_header_read_in = true;
24903 }
24904
24905 return &m_header;
24906 }
24907
24908 /* See read.h. */
24909
24910 int
24911 dwarf2_per_cu_data::addr_size () const
24912 {
24913 return this->get_header ()->addr_size;
24914 }
24915
24916 /* See read.h. */
24917
24918 int
24919 dwarf2_per_cu_data::offset_size () const
24920 {
24921 return this->get_header ()->offset_size;
24922 }
24923
24924 /* See read.h. */
24925
24926 int
24927 dwarf2_per_cu_data::ref_addr_size () const
24928 {
24929 const comp_unit_head *header = this->get_header ();
24930
24931 if (header->version == 2)
24932 return header->addr_size;
24933 else
24934 return header->offset_size;
24935 }
24936
24937 /* See read.h. */
24938
24939 struct type *
24940 dwarf2_cu::addr_type () const
24941 {
24942 struct objfile *objfile = this->per_objfile->objfile;
24943 struct type *void_type = objfile_type (objfile)->builtin_void;
24944 struct type *addr_type = lookup_pointer_type (void_type);
24945 int addr_size = this->per_cu->addr_size ();
24946
24947 if (TYPE_LENGTH (addr_type) == addr_size)
24948 return addr_type;
24949
24950 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24951 return addr_type;
24952 }
24953
24954 /* A helper function for dwarf2_find_containing_comp_unit that returns
24955 the index of the result, and that searches a vector. It will
24956 return a result even if the offset in question does not actually
24957 occur in any CU. This is separate so that it can be unit
24958 tested. */
24959
24960 static int
24961 dwarf2_find_containing_comp_unit
24962 (sect_offset sect_off,
24963 unsigned int offset_in_dwz,
24964 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24965 {
24966 int low, high;
24967
24968 low = 0;
24969 high = all_comp_units.size () - 1;
24970 while (high > low)
24971 {
24972 struct dwarf2_per_cu_data *mid_cu;
24973 int mid = low + (high - low) / 2;
24974
24975 mid_cu = all_comp_units[mid];
24976 if (mid_cu->is_dwz > offset_in_dwz
24977 || (mid_cu->is_dwz == offset_in_dwz
24978 && mid_cu->sect_off + mid_cu->length > sect_off))
24979 high = mid;
24980 else
24981 low = mid + 1;
24982 }
24983 gdb_assert (low == high);
24984 return low;
24985 }
24986
24987 /* Locate the .debug_info compilation unit from CU's objfile which contains
24988 the DIE at OFFSET. Raises an error on failure. */
24989
24990 static struct dwarf2_per_cu_data *
24991 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24992 unsigned int offset_in_dwz,
24993 dwarf2_per_objfile *per_objfile)
24994 {
24995 int low = dwarf2_find_containing_comp_unit
24996 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24997 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24998
24999 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25000 {
25001 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25002 error (_("Dwarf Error: could not find partial DIE containing "
25003 "offset %s [in module %s]"),
25004 sect_offset_str (sect_off),
25005 bfd_get_filename (per_objfile->objfile->obfd));
25006
25007 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
25008 <= sect_off);
25009 return per_objfile->per_bfd->all_comp_units[low-1];
25010 }
25011 else
25012 {
25013 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
25014 && sect_off >= this_cu->sect_off + this_cu->length)
25015 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25016 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25017 return this_cu;
25018 }
25019 }
25020
25021 #if GDB_SELF_TEST
25022
25023 namespace selftests {
25024 namespace find_containing_comp_unit {
25025
25026 static void
25027 run_test ()
25028 {
25029 struct dwarf2_per_cu_data one {};
25030 struct dwarf2_per_cu_data two {};
25031 struct dwarf2_per_cu_data three {};
25032 struct dwarf2_per_cu_data four {};
25033
25034 one.length = 5;
25035 two.sect_off = sect_offset (one.length);
25036 two.length = 7;
25037
25038 three.length = 5;
25039 three.is_dwz = 1;
25040 four.sect_off = sect_offset (three.length);
25041 four.length = 7;
25042 four.is_dwz = 1;
25043
25044 std::vector<dwarf2_per_cu_data *> units;
25045 units.push_back (&one);
25046 units.push_back (&two);
25047 units.push_back (&three);
25048 units.push_back (&four);
25049
25050 int result;
25051
25052 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25053 SELF_CHECK (units[result] == &one);
25054 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25055 SELF_CHECK (units[result] == &one);
25056 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25057 SELF_CHECK (units[result] == &two);
25058
25059 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25060 SELF_CHECK (units[result] == &three);
25061 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25062 SELF_CHECK (units[result] == &three);
25063 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25064 SELF_CHECK (units[result] == &four);
25065 }
25066
25067 }
25068 }
25069
25070 #endif /* GDB_SELF_TEST */
25071
25072 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
25073
25074 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25075 dwarf2_per_objfile *per_objfile)
25076 : per_cu (per_cu),
25077 per_objfile (per_objfile),
25078 mark (false),
25079 has_loclist (false),
25080 checked_producer (false),
25081 producer_is_gxx_lt_4_6 (false),
25082 producer_is_gcc_lt_4_3 (false),
25083 producer_is_icc (false),
25084 producer_is_icc_lt_14 (false),
25085 producer_is_codewarrior (false),
25086 processing_has_namespace_info (false)
25087 {
25088 }
25089
25090 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25091
25092 static void
25093 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25094 enum language pretend_language)
25095 {
25096 struct attribute *attr;
25097
25098 /* Set the language we're debugging. */
25099 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25100 if (attr != nullptr)
25101 set_cu_language (attr->constant_value (0), cu);
25102 else
25103 {
25104 cu->language = pretend_language;
25105 cu->language_defn = language_def (cu->language);
25106 }
25107
25108 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25109 }
25110
25111 /* See read.h. */
25112
25113 dwarf2_cu *
25114 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
25115 {
25116 auto it = m_dwarf2_cus.find (per_cu);
25117 if (it == m_dwarf2_cus.end ())
25118 return nullptr;
25119
25120 return it->second;
25121 }
25122
25123 /* See read.h. */
25124
25125 void
25126 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25127 {
25128 gdb_assert (this->get_cu (per_cu) == nullptr);
25129
25130 m_dwarf2_cus[per_cu] = cu;
25131 }
25132
25133 /* See read.h. */
25134
25135 void
25136 dwarf2_per_objfile::age_comp_units ()
25137 {
25138 dwarf_read_debug_printf_v ("running");
25139
25140 /* This is not expected to be called in the middle of CU expansion. There is
25141 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25142 loaded in memory. Calling age_comp_units while the queue is in use could
25143 make us free the DIEs for a CU that is in the queue and therefore break
25144 that invariant. */
25145 gdb_assert (!this->per_bfd->queue.has_value ());
25146
25147 /* Start by clearing all marks. */
25148 for (auto pair : m_dwarf2_cus)
25149 pair.second->mark = false;
25150
25151 /* Traverse all CUs, mark them and their dependencies if used recently
25152 enough. */
25153 for (auto pair : m_dwarf2_cus)
25154 {
25155 dwarf2_cu *cu = pair.second;
25156
25157 cu->last_used++;
25158 if (cu->last_used <= dwarf_max_cache_age)
25159 dwarf2_mark (cu);
25160 }
25161
25162 /* Delete all CUs still not marked. */
25163 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
25164 {
25165 dwarf2_cu *cu = it->second;
25166
25167 if (!cu->mark)
25168 {
25169 dwarf_read_debug_printf_v ("deleting old CU %s",
25170 sect_offset_str (cu->per_cu->sect_off));
25171 delete cu;
25172 it = m_dwarf2_cus.erase (it);
25173 }
25174 else
25175 it++;
25176 }
25177 }
25178
25179 /* See read.h. */
25180
25181 void
25182 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25183 {
25184 auto it = m_dwarf2_cus.find (per_cu);
25185 if (it == m_dwarf2_cus.end ())
25186 return;
25187
25188 delete it->second;
25189
25190 m_dwarf2_cus.erase (it);
25191 }
25192
25193 dwarf2_per_objfile::~dwarf2_per_objfile ()
25194 {
25195 remove_all_cus ();
25196 }
25197
25198 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25199 We store these in a hash table separate from the DIEs, and preserve them
25200 when the DIEs are flushed out of cache.
25201
25202 The CU "per_cu" pointer is needed because offset alone is not enough to
25203 uniquely identify the type. A file may have multiple .debug_types sections,
25204 or the type may come from a DWO file. Furthermore, while it's more logical
25205 to use per_cu->section+offset, with Fission the section with the data is in
25206 the DWO file but we don't know that section at the point we need it.
25207 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25208 because we can enter the lookup routine, get_die_type_at_offset, from
25209 outside this file, and thus won't necessarily have PER_CU->cu.
25210 Fortunately, PER_CU is stable for the life of the objfile. */
25211
25212 struct dwarf2_per_cu_offset_and_type
25213 {
25214 const struct dwarf2_per_cu_data *per_cu;
25215 sect_offset sect_off;
25216 struct type *type;
25217 };
25218
25219 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25220
25221 static hashval_t
25222 per_cu_offset_and_type_hash (const void *item)
25223 {
25224 const struct dwarf2_per_cu_offset_and_type *ofs
25225 = (const struct dwarf2_per_cu_offset_and_type *) item;
25226
25227 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25228 }
25229
25230 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25231
25232 static int
25233 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25234 {
25235 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25236 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25237 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25238 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25239
25240 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25241 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25242 }
25243
25244 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25245 table if necessary. For convenience, return TYPE.
25246
25247 The DIEs reading must have careful ordering to:
25248 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25249 reading current DIE.
25250 * Not trying to dereference contents of still incompletely read in types
25251 while reading in other DIEs.
25252 * Enable referencing still incompletely read in types just by a pointer to
25253 the type without accessing its fields.
25254
25255 Therefore caller should follow these rules:
25256 * Try to fetch any prerequisite types we may need to build this DIE type
25257 before building the type and calling set_die_type.
25258 * After building type call set_die_type for current DIE as soon as
25259 possible before fetching more types to complete the current type.
25260 * Make the type as complete as possible before fetching more types. */
25261
25262 static struct type *
25263 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25264 bool skip_data_location)
25265 {
25266 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25267 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25268 struct objfile *objfile = per_objfile->objfile;
25269 struct attribute *attr;
25270 struct dynamic_prop prop;
25271
25272 /* For Ada types, make sure that the gnat-specific data is always
25273 initialized (if not already set). There are a few types where
25274 we should not be doing so, because the type-specific area is
25275 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25276 where the type-specific area is used to store the floatformat).
25277 But this is not a problem, because the gnat-specific information
25278 is actually not needed for these types. */
25279 if (need_gnat_info (cu)
25280 && type->code () != TYPE_CODE_FUNC
25281 && type->code () != TYPE_CODE_FLT
25282 && type->code () != TYPE_CODE_METHODPTR
25283 && type->code () != TYPE_CODE_MEMBERPTR
25284 && type->code () != TYPE_CODE_METHOD
25285 && type->code () != TYPE_CODE_FIXED_POINT
25286 && !HAVE_GNAT_AUX_INFO (type))
25287 INIT_GNAT_SPECIFIC (type);
25288
25289 /* Read DW_AT_allocated and set in type. */
25290 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25291 if (attr != NULL)
25292 {
25293 struct type *prop_type = cu->addr_sized_int_type (false);
25294 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25295 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25296 }
25297
25298 /* Read DW_AT_associated and set in type. */
25299 attr = dwarf2_attr (die, DW_AT_associated, cu);
25300 if (attr != NULL)
25301 {
25302 struct type *prop_type = cu->addr_sized_int_type (false);
25303 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25304 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25305 }
25306
25307 /* Read DW_AT_data_location and set in type. */
25308 if (!skip_data_location)
25309 {
25310 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25311 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25312 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25313 }
25314
25315 if (per_objfile->die_type_hash == NULL)
25316 per_objfile->die_type_hash
25317 = htab_up (htab_create_alloc (127,
25318 per_cu_offset_and_type_hash,
25319 per_cu_offset_and_type_eq,
25320 NULL, xcalloc, xfree));
25321
25322 ofs.per_cu = cu->per_cu;
25323 ofs.sect_off = die->sect_off;
25324 ofs.type = type;
25325 slot = (struct dwarf2_per_cu_offset_and_type **)
25326 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25327 if (*slot)
25328 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25329 sect_offset_str (die->sect_off));
25330 *slot = XOBNEW (&objfile->objfile_obstack,
25331 struct dwarf2_per_cu_offset_and_type);
25332 **slot = ofs;
25333 return type;
25334 }
25335
25336 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25337 or return NULL if the die does not have a saved type. */
25338
25339 static struct type *
25340 get_die_type_at_offset (sect_offset sect_off,
25341 dwarf2_per_cu_data *per_cu,
25342 dwarf2_per_objfile *per_objfile)
25343 {
25344 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25345
25346 if (per_objfile->die_type_hash == NULL)
25347 return NULL;
25348
25349 ofs.per_cu = per_cu;
25350 ofs.sect_off = sect_off;
25351 slot = ((struct dwarf2_per_cu_offset_and_type *)
25352 htab_find (per_objfile->die_type_hash.get (), &ofs));
25353 if (slot)
25354 return slot->type;
25355 else
25356 return NULL;
25357 }
25358
25359 /* Look up the type for DIE in CU in die_type_hash,
25360 or return NULL if DIE does not have a saved type. */
25361
25362 static struct type *
25363 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25364 {
25365 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25366 }
25367
25368 /* Add a dependence relationship from CU to REF_PER_CU. */
25369
25370 static void
25371 dwarf2_add_dependence (struct dwarf2_cu *cu,
25372 struct dwarf2_per_cu_data *ref_per_cu)
25373 {
25374 void **slot;
25375
25376 if (cu->dependencies == NULL)
25377 cu->dependencies
25378 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25379 NULL, &cu->comp_unit_obstack,
25380 hashtab_obstack_allocate,
25381 dummy_obstack_deallocate);
25382
25383 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25384 if (*slot == NULL)
25385 *slot = ref_per_cu;
25386 }
25387
25388 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25389 Set the mark field in every compilation unit in the
25390 cache that we must keep because we are keeping CU.
25391
25392 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25393
25394 static int
25395 dwarf2_mark_helper (void **slot, void *data)
25396 {
25397 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25398 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25399 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25400
25401 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25402 reading of the chain. As such dependencies remain valid it is not much
25403 useful to track and undo them during QUIT cleanups. */
25404 if (cu == nullptr)
25405 return 1;
25406
25407 if (cu->mark)
25408 return 1;
25409
25410 cu->mark = true;
25411
25412 if (cu->dependencies != nullptr)
25413 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25414
25415 return 1;
25416 }
25417
25418 /* Set the mark field in CU and in every other compilation unit in the
25419 cache that we must keep because we are keeping CU. */
25420
25421 static void
25422 dwarf2_mark (struct dwarf2_cu *cu)
25423 {
25424 if (cu->mark)
25425 return;
25426
25427 cu->mark = true;
25428
25429 if (cu->dependencies != nullptr)
25430 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25431 }
25432
25433 /* Trivial hash function for partial_die_info: the hash value of a DIE
25434 is its offset in .debug_info for this objfile. */
25435
25436 static hashval_t
25437 partial_die_hash (const void *item)
25438 {
25439 const struct partial_die_info *part_die
25440 = (const struct partial_die_info *) item;
25441
25442 return to_underlying (part_die->sect_off);
25443 }
25444
25445 /* Trivial comparison function for partial_die_info structures: two DIEs
25446 are equal if they have the same offset. */
25447
25448 static int
25449 partial_die_eq (const void *item_lhs, const void *item_rhs)
25450 {
25451 const struct partial_die_info *part_die_lhs
25452 = (const struct partial_die_info *) item_lhs;
25453 const struct partial_die_info *part_die_rhs
25454 = (const struct partial_die_info *) item_rhs;
25455
25456 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25457 }
25458
25459 struct cmd_list_element *set_dwarf_cmdlist;
25460 struct cmd_list_element *show_dwarf_cmdlist;
25461
25462 static void
25463 show_check_physname (struct ui_file *file, int from_tty,
25464 struct cmd_list_element *c, const char *value)
25465 {
25466 fprintf_filtered (file,
25467 _("Whether to check \"physname\" is %s.\n"),
25468 value);
25469 }
25470
25471 void _initialize_dwarf2_read ();
25472 void
25473 _initialize_dwarf2_read ()
25474 {
25475 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25476 Set DWARF specific variables.\n\
25477 Configure DWARF variables such as the cache size."),
25478 &set_dwarf_cmdlist, "maintenance set dwarf ",
25479 0/*allow-unknown*/, &maintenance_set_cmdlist);
25480
25481 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25482 Show DWARF specific variables.\n\
25483 Show DWARF variables such as the cache size."),
25484 &show_dwarf_cmdlist, "maintenance show dwarf ",
25485 0/*allow-unknown*/, &maintenance_show_cmdlist);
25486
25487 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25488 &dwarf_max_cache_age, _("\
25489 Set the upper bound on the age of cached DWARF compilation units."), _("\
25490 Show the upper bound on the age of cached DWARF compilation units."), _("\
25491 A higher limit means that cached compilation units will be stored\n\
25492 in memory longer, and more total memory will be used. Zero disables\n\
25493 caching, which can slow down startup."),
25494 NULL,
25495 show_dwarf_max_cache_age,
25496 &set_dwarf_cmdlist,
25497 &show_dwarf_cmdlist);
25498
25499 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25500 Set debugging of the DWARF reader."), _("\
25501 Show debugging of the DWARF reader."), _("\
25502 When enabled (non-zero), debugging messages are printed during DWARF\n\
25503 reading and symtab expansion. A value of 1 (one) provides basic\n\
25504 information. A value greater than 1 provides more verbose information."),
25505 NULL,
25506 NULL,
25507 &setdebuglist, &showdebuglist);
25508
25509 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25510 Set debugging of the DWARF DIE reader."), _("\
25511 Show debugging of the DWARF DIE reader."), _("\
25512 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25513 The value is the maximum depth to print."),
25514 NULL,
25515 NULL,
25516 &setdebuglist, &showdebuglist);
25517
25518 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25519 Set debugging of the dwarf line reader."), _("\
25520 Show debugging of the dwarf line reader."), _("\
25521 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25522 A value of 1 (one) provides basic information.\n\
25523 A value greater than 1 provides more verbose information."),
25524 NULL,
25525 NULL,
25526 &setdebuglist, &showdebuglist);
25527
25528 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25529 Set cross-checking of \"physname\" code against demangler."), _("\
25530 Show cross-checking of \"physname\" code against demangler."), _("\
25531 When enabled, GDB's internal \"physname\" code is checked against\n\
25532 the demangler."),
25533 NULL, show_check_physname,
25534 &setdebuglist, &showdebuglist);
25535
25536 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25537 no_class, &use_deprecated_index_sections, _("\
25538 Set whether to use deprecated gdb_index sections."), _("\
25539 Show whether to use deprecated gdb_index sections."), _("\
25540 When enabled, deprecated .gdb_index sections are used anyway.\n\
25541 Normally they are ignored either because of a missing feature or\n\
25542 performance issue.\n\
25543 Warning: This option must be enabled before gdb reads the file."),
25544 NULL,
25545 NULL,
25546 &setlist, &showlist);
25547
25548 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25549 &dwarf2_locexpr_funcs);
25550 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25551 &dwarf2_loclist_funcs);
25552
25553 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25554 &dwarf2_block_frame_base_locexpr_funcs);
25555 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25556 &dwarf2_block_frame_base_loclist_funcs);
25557
25558 #if GDB_SELF_TEST
25559 selftests::register_test ("dw2_expand_symtabs_matching",
25560 selftests::dw2_expand_symtabs_matching::run_test);
25561 selftests::register_test ("dwarf2_find_containing_comp_unit",
25562 selftests::find_containing_comp_unit::run_test);
25563 #endif
25564 }