Attach partial symtab storage to psymbol_functions
[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 and if there aren't partial symbols
1956 from some other reader. */
1957 if (!objfile->has_partial_symbols ()
1958 && !gdb_bfd_requires_relocations (objfile->obfd))
1959 {
1960 /* See if one has been created for this BFD yet. */
1961 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1962
1963 if (per_bfd == nullptr)
1964 {
1965 /* No, create it now. */
1966 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1967 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1968 }
1969 }
1970 else
1971 {
1972 /* No sharing possible, create one specifically for this objfile. */
1973 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1974 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1975 }
1976
1977 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1978 }
1979
1980 return (!per_objfile->per_bfd->info.is_virtual
1981 && per_objfile->per_bfd->info.s.section != NULL
1982 && !per_objfile->per_bfd->abbrev.is_virtual
1983 && per_objfile->per_bfd->abbrev.s.section != NULL);
1984 }
1985
1986 /* See declaration. */
1987
1988 void
1989 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1990 const dwarf2_debug_sections &names)
1991 {
1992 flagword aflag = bfd_section_flags (sectp);
1993
1994 if ((aflag & SEC_HAS_CONTENTS) == 0)
1995 {
1996 }
1997 else if (elf_section_data (sectp)->this_hdr.sh_size
1998 > bfd_get_file_size (abfd))
1999 {
2000 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2001 warning (_("Discarding section %s which has a section size (%s"
2002 ") larger than the file size [in module %s]"),
2003 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2004 bfd_get_filename (abfd));
2005 }
2006 else if (names.info.matches (sectp->name))
2007 {
2008 this->info.s.section = sectp;
2009 this->info.size = bfd_section_size (sectp);
2010 }
2011 else if (names.abbrev.matches (sectp->name))
2012 {
2013 this->abbrev.s.section = sectp;
2014 this->abbrev.size = bfd_section_size (sectp);
2015 }
2016 else if (names.line.matches (sectp->name))
2017 {
2018 this->line.s.section = sectp;
2019 this->line.size = bfd_section_size (sectp);
2020 }
2021 else if (names.loc.matches (sectp->name))
2022 {
2023 this->loc.s.section = sectp;
2024 this->loc.size = bfd_section_size (sectp);
2025 }
2026 else if (names.loclists.matches (sectp->name))
2027 {
2028 this->loclists.s.section = sectp;
2029 this->loclists.size = bfd_section_size (sectp);
2030 }
2031 else if (names.macinfo.matches (sectp->name))
2032 {
2033 this->macinfo.s.section = sectp;
2034 this->macinfo.size = bfd_section_size (sectp);
2035 }
2036 else if (names.macro.matches (sectp->name))
2037 {
2038 this->macro.s.section = sectp;
2039 this->macro.size = bfd_section_size (sectp);
2040 }
2041 else if (names.str.matches (sectp->name))
2042 {
2043 this->str.s.section = sectp;
2044 this->str.size = bfd_section_size (sectp);
2045 }
2046 else if (names.str_offsets.matches (sectp->name))
2047 {
2048 this->str_offsets.s.section = sectp;
2049 this->str_offsets.size = bfd_section_size (sectp);
2050 }
2051 else if (names.line_str.matches (sectp->name))
2052 {
2053 this->line_str.s.section = sectp;
2054 this->line_str.size = bfd_section_size (sectp);
2055 }
2056 else if (names.addr.matches (sectp->name))
2057 {
2058 this->addr.s.section = sectp;
2059 this->addr.size = bfd_section_size (sectp);
2060 }
2061 else if (names.frame.matches (sectp->name))
2062 {
2063 this->frame.s.section = sectp;
2064 this->frame.size = bfd_section_size (sectp);
2065 }
2066 else if (names.eh_frame.matches (sectp->name))
2067 {
2068 this->eh_frame.s.section = sectp;
2069 this->eh_frame.size = bfd_section_size (sectp);
2070 }
2071 else if (names.ranges.matches (sectp->name))
2072 {
2073 this->ranges.s.section = sectp;
2074 this->ranges.size = bfd_section_size (sectp);
2075 }
2076 else if (names.rnglists.matches (sectp->name))
2077 {
2078 this->rnglists.s.section = sectp;
2079 this->rnglists.size = bfd_section_size (sectp);
2080 }
2081 else if (names.types.matches (sectp->name))
2082 {
2083 struct dwarf2_section_info type_section;
2084
2085 memset (&type_section, 0, sizeof (type_section));
2086 type_section.s.section = sectp;
2087 type_section.size = bfd_section_size (sectp);
2088
2089 this->types.push_back (type_section);
2090 }
2091 else if (names.gdb_index.matches (sectp->name))
2092 {
2093 this->gdb_index.s.section = sectp;
2094 this->gdb_index.size = bfd_section_size (sectp);
2095 }
2096 else if (names.debug_names.matches (sectp->name))
2097 {
2098 this->debug_names.s.section = sectp;
2099 this->debug_names.size = bfd_section_size (sectp);
2100 }
2101 else if (names.debug_aranges.matches (sectp->name))
2102 {
2103 this->debug_aranges.s.section = sectp;
2104 this->debug_aranges.size = bfd_section_size (sectp);
2105 }
2106
2107 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2108 && bfd_section_vma (sectp) == 0)
2109 this->has_section_at_zero = true;
2110 }
2111
2112 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2113 SECTION_NAME. */
2114
2115 void
2116 dwarf2_get_section_info (struct objfile *objfile,
2117 enum dwarf2_section_enum sect,
2118 asection **sectp, const gdb_byte **bufp,
2119 bfd_size_type *sizep)
2120 {
2121 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2122 struct dwarf2_section_info *info;
2123
2124 /* We may see an objfile without any DWARF, in which case we just
2125 return nothing. */
2126 if (per_objfile == NULL)
2127 {
2128 *sectp = NULL;
2129 *bufp = NULL;
2130 *sizep = 0;
2131 return;
2132 }
2133 switch (sect)
2134 {
2135 case DWARF2_DEBUG_FRAME:
2136 info = &per_objfile->per_bfd->frame;
2137 break;
2138 case DWARF2_EH_FRAME:
2139 info = &per_objfile->per_bfd->eh_frame;
2140 break;
2141 default:
2142 gdb_assert_not_reached ("unexpected section");
2143 }
2144
2145 info->read (objfile);
2146
2147 *sectp = info->get_bfd_section ();
2148 *bufp = info->buffer;
2149 *sizep = info->size;
2150 }
2151
2152 \f
2153 /* DWARF quick_symbol_functions support. */
2154
2155 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2156 unique line tables, so we maintain a separate table of all .debug_line
2157 derived entries to support the sharing.
2158 All the quick functions need is the list of file names. We discard the
2159 line_header when we're done and don't need to record it here. */
2160 struct quick_file_names
2161 {
2162 /* The data used to construct the hash key. */
2163 struct stmt_list_hash hash;
2164
2165 /* The number of entries in file_names, real_names. */
2166 unsigned int num_file_names;
2167
2168 /* The file names from the line table, after being run through
2169 file_full_name. */
2170 const char **file_names;
2171
2172 /* The file names from the line table after being run through
2173 gdb_realpath. These are computed lazily. */
2174 const char **real_names;
2175 };
2176
2177 /* When using the index (and thus not using psymtabs), each CU has an
2178 object of this type. This is used to hold information needed by
2179 the various "quick" methods. */
2180 struct dwarf2_per_cu_quick_data
2181 {
2182 /* The file table. This can be NULL if there was no file table
2183 or it's currently not read in.
2184 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2185 struct quick_file_names *file_names;
2186
2187 /* A temporary mark bit used when iterating over all CUs in
2188 expand_symtabs_matching. */
2189 unsigned int mark : 1;
2190
2191 /* True if we've tried to read the file table and found there isn't one.
2192 There will be no point in trying to read it again next time. */
2193 unsigned int no_file_data : 1;
2194 };
2195
2196 struct dwarf2_base_index_functions : public quick_symbol_functions
2197 {
2198 bool has_symbols (struct objfile *objfile) override;
2199
2200 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2201
2202 void forget_cached_source_info (struct objfile *objfile) override;
2203
2204 bool map_symtabs_matching_filename
2205 (struct objfile *objfile, const char *name, const char *real_path,
2206 gdb::function_view<bool (symtab *)> callback) override;
2207
2208 enum language lookup_global_symbol_language (struct objfile *objfile,
2209 const char *name,
2210 domain_enum domain,
2211 bool *symbol_found_p) override
2212 {
2213 *symbol_found_p = false;
2214 return language_unknown;
2215 }
2216
2217 void print_stats (struct objfile *objfile, bool print_bcache) override;
2218
2219 void expand_all_symtabs (struct objfile *objfile) override;
2220
2221 void expand_symtabs_with_fullname (struct objfile *objfile,
2222 const char *fullname) override;
2223
2224 struct compunit_symtab *find_pc_sect_compunit_symtab
2225 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2226 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2227
2228 struct compunit_symtab *find_compunit_symtab_by_address
2229 (struct objfile *objfile, CORE_ADDR address) override
2230 {
2231 return nullptr;
2232 }
2233
2234 void map_symbol_filenames (struct objfile *objfile,
2235 symbol_filename_ftype *fun, void *data,
2236 int need_fullname) override;
2237 };
2238
2239 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2240 {
2241 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2242 block_enum block_index,
2243 const char *name,
2244 domain_enum domain) override;
2245
2246 void dump (struct objfile *objfile) override;
2247
2248 void expand_symtabs_for_function (struct objfile *objfile,
2249 const char *func_name) override;
2250
2251 void map_matching_symbols
2252 (struct objfile *,
2253 const lookup_name_info &lookup_name,
2254 domain_enum domain,
2255 int global,
2256 gdb::function_view<symbol_found_callback_ftype> callback,
2257 symbol_compare_ftype *ordered_compare) override;
2258
2259 void expand_symtabs_matching
2260 (struct objfile *objfile,
2261 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2262 const lookup_name_info *lookup_name,
2263 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2264 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2265 enum search_domain kind) override;
2266 };
2267
2268 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2269 {
2270 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2271 block_enum block_index,
2272 const char *name,
2273 domain_enum domain) override;
2274
2275 void dump (struct objfile *objfile) override;
2276
2277 void expand_symtabs_for_function (struct objfile *objfile,
2278 const char *func_name) override;
2279
2280 void map_matching_symbols
2281 (struct objfile *,
2282 const lookup_name_info &lookup_name,
2283 domain_enum domain,
2284 int global,
2285 gdb::function_view<symbol_found_callback_ftype> callback,
2286 symbol_compare_ftype *ordered_compare) override;
2287
2288 void expand_symtabs_matching
2289 (struct objfile *objfile,
2290 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2291 const lookup_name_info *lookup_name,
2292 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2293 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2294 enum search_domain kind) override;
2295 };
2296
2297 quick_symbol_functions_up
2298 make_dwarf_gdb_index ()
2299 {
2300 return quick_symbol_functions_up (new dwarf2_gdb_index);
2301 }
2302
2303 quick_symbol_functions_up
2304 make_dwarf_debug_names ()
2305 {
2306 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2307 }
2308
2309 /* Utility hash function for a stmt_list_hash. */
2310
2311 static hashval_t
2312 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2313 {
2314 hashval_t v = 0;
2315
2316 if (stmt_list_hash->dwo_unit != NULL)
2317 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2318 v += to_underlying (stmt_list_hash->line_sect_off);
2319 return v;
2320 }
2321
2322 /* Utility equality function for a stmt_list_hash. */
2323
2324 static int
2325 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2326 const struct stmt_list_hash *rhs)
2327 {
2328 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2329 return 0;
2330 if (lhs->dwo_unit != NULL
2331 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2332 return 0;
2333
2334 return lhs->line_sect_off == rhs->line_sect_off;
2335 }
2336
2337 /* Hash function for a quick_file_names. */
2338
2339 static hashval_t
2340 hash_file_name_entry (const void *e)
2341 {
2342 const struct quick_file_names *file_data
2343 = (const struct quick_file_names *) e;
2344
2345 return hash_stmt_list_entry (&file_data->hash);
2346 }
2347
2348 /* Equality function for a quick_file_names. */
2349
2350 static int
2351 eq_file_name_entry (const void *a, const void *b)
2352 {
2353 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2354 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2355
2356 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2357 }
2358
2359 /* Delete function for a quick_file_names. */
2360
2361 static void
2362 delete_file_name_entry (void *e)
2363 {
2364 struct quick_file_names *file_data = (struct quick_file_names *) e;
2365 int i;
2366
2367 for (i = 0; i < file_data->num_file_names; ++i)
2368 {
2369 xfree ((void*) file_data->file_names[i]);
2370 if (file_data->real_names)
2371 xfree ((void*) file_data->real_names[i]);
2372 }
2373
2374 /* The space for the struct itself lives on the obstack, so we don't
2375 free it here. */
2376 }
2377
2378 /* Create a quick_file_names hash table. */
2379
2380 static htab_up
2381 create_quick_file_names_table (unsigned int nr_initial_entries)
2382 {
2383 return htab_up (htab_create_alloc (nr_initial_entries,
2384 hash_file_name_entry, eq_file_name_entry,
2385 delete_file_name_entry, xcalloc, xfree));
2386 }
2387
2388 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2389 function is unrelated to symtabs, symtab would have to be created afterwards.
2390 You should call age_cached_comp_units after processing the CU. */
2391
2392 static dwarf2_cu *
2393 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2394 bool skip_partial)
2395 {
2396 if (per_cu->is_debug_types)
2397 load_full_type_unit (per_cu, per_objfile);
2398 else
2399 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2400 skip_partial, language_minimal);
2401
2402 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2403 if (cu == nullptr)
2404 return nullptr; /* Dummy CU. */
2405
2406 dwarf2_find_base_address (cu->dies, cu);
2407
2408 return cu;
2409 }
2410
2411 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2412
2413 static void
2414 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2415 dwarf2_per_objfile *per_objfile, bool skip_partial)
2416 {
2417 /* Skip type_unit_groups, reading the type units they contain
2418 is handled elsewhere. */
2419 if (per_cu->type_unit_group_p ())
2420 return;
2421
2422 {
2423 /* The destructor of dwarf2_queue_guard frees any entries left on
2424 the queue. After this point we're guaranteed to leave this function
2425 with the dwarf queue empty. */
2426 dwarf2_queue_guard q_guard (per_objfile);
2427
2428 if (!per_objfile->symtab_set_p (per_cu))
2429 {
2430 queue_comp_unit (per_cu, per_objfile, language_minimal);
2431 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2432
2433 /* If we just loaded a CU from a DWO, and we're working with an index
2434 that may badly handle TUs, load all the TUs in that DWO as well.
2435 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2436 if (!per_cu->is_debug_types
2437 && cu != NULL
2438 && cu->dwo_unit != NULL
2439 && per_objfile->per_bfd->index_table != NULL
2440 && per_objfile->per_bfd->index_table->version <= 7
2441 /* DWP files aren't supported yet. */
2442 && get_dwp_file (per_objfile) == NULL)
2443 queue_and_load_all_dwo_tus (cu);
2444 }
2445
2446 process_queue (per_objfile);
2447 }
2448
2449 /* Age the cache, releasing compilation units that have not
2450 been used recently. */
2451 per_objfile->age_comp_units ();
2452 }
2453
2454 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2455 the per-objfile for which this symtab is instantiated.
2456
2457 Returns the resulting symbol table. */
2458
2459 static struct compunit_symtab *
2460 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2461 dwarf2_per_objfile *per_objfile,
2462 bool skip_partial)
2463 {
2464 gdb_assert (per_objfile->per_bfd->using_index);
2465
2466 if (!per_objfile->symtab_set_p (per_cu))
2467 {
2468 free_cached_comp_units freer (per_objfile);
2469 scoped_restore decrementer = increment_reading_symtab ();
2470 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2471 process_cu_includes (per_objfile);
2472 }
2473
2474 return per_objfile->get_symtab (per_cu);
2475 }
2476
2477 /* See declaration. */
2478
2479 dwarf2_per_cu_data *
2480 dwarf2_per_bfd::get_cutu (int index)
2481 {
2482 if (index >= this->all_comp_units.size ())
2483 {
2484 index -= this->all_comp_units.size ();
2485 gdb_assert (index < this->all_type_units.size ());
2486 return &this->all_type_units[index]->per_cu;
2487 }
2488
2489 return this->all_comp_units[index];
2490 }
2491
2492 /* See declaration. */
2493
2494 dwarf2_per_cu_data *
2495 dwarf2_per_bfd::get_cu (int index)
2496 {
2497 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2498
2499 return this->all_comp_units[index];
2500 }
2501
2502 /* See declaration. */
2503
2504 signatured_type *
2505 dwarf2_per_bfd::get_tu (int index)
2506 {
2507 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2508
2509 return this->all_type_units[index];
2510 }
2511
2512 /* See read.h. */
2513
2514 dwarf2_per_cu_data *
2515 dwarf2_per_bfd::allocate_per_cu ()
2516 {
2517 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2518 result->per_bfd = this;
2519 result->index = m_num_psymtabs++;
2520 return result;
2521 }
2522
2523 /* See read.h. */
2524
2525 signatured_type *
2526 dwarf2_per_bfd::allocate_signatured_type ()
2527 {
2528 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2529 result->per_cu.per_bfd = this;
2530 result->per_cu.index = m_num_psymtabs++;
2531 return result;
2532 }
2533
2534 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2535 obstack, and constructed with the specified field values. */
2536
2537 static dwarf2_per_cu_data *
2538 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2539 struct dwarf2_section_info *section,
2540 int is_dwz,
2541 sect_offset sect_off, ULONGEST length)
2542 {
2543 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2544 the_cu->sect_off = sect_off;
2545 the_cu->length = length;
2546 the_cu->section = section;
2547 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2548 struct dwarf2_per_cu_quick_data);
2549 the_cu->is_dwz = is_dwz;
2550 return the_cu;
2551 }
2552
2553 /* A helper for create_cus_from_index that handles a given list of
2554 CUs. */
2555
2556 static void
2557 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2558 const gdb_byte *cu_list, offset_type n_elements,
2559 struct dwarf2_section_info *section,
2560 int is_dwz)
2561 {
2562 for (offset_type i = 0; i < n_elements; i += 2)
2563 {
2564 gdb_static_assert (sizeof (ULONGEST) >= 8);
2565
2566 sect_offset sect_off
2567 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2568 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2569 cu_list += 2 * 8;
2570
2571 dwarf2_per_cu_data *per_cu
2572 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2573 length);
2574 per_bfd->all_comp_units.push_back (per_cu);
2575 }
2576 }
2577
2578 /* Read the CU list from the mapped index, and use it to create all
2579 the CU objects for PER_BFD. */
2580
2581 static void
2582 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2583 const gdb_byte *cu_list, offset_type cu_list_elements,
2584 const gdb_byte *dwz_list, offset_type dwz_elements)
2585 {
2586 gdb_assert (per_bfd->all_comp_units.empty ());
2587 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2588
2589 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2590 &per_bfd->info, 0);
2591
2592 if (dwz_elements == 0)
2593 return;
2594
2595 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2596 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2597 &dwz->info, 1);
2598 }
2599
2600 /* Create the signatured type hash table from the index. */
2601
2602 static void
2603 create_signatured_type_table_from_index
2604 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2605 const gdb_byte *bytes, offset_type elements)
2606 {
2607 gdb_assert (per_bfd->all_type_units.empty ());
2608 per_bfd->all_type_units.reserve (elements / 3);
2609
2610 htab_up sig_types_hash = allocate_signatured_type_table ();
2611
2612 for (offset_type i = 0; i < elements; i += 3)
2613 {
2614 struct signatured_type *sig_type;
2615 ULONGEST signature;
2616 void **slot;
2617 cu_offset type_offset_in_tu;
2618
2619 gdb_static_assert (sizeof (ULONGEST) >= 8);
2620 sect_offset sect_off
2621 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2622 type_offset_in_tu
2623 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2624 BFD_ENDIAN_LITTLE);
2625 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2626 bytes += 3 * 8;
2627
2628 sig_type = per_bfd->allocate_signatured_type ();
2629 sig_type->signature = signature;
2630 sig_type->type_offset_in_tu = type_offset_in_tu;
2631 sig_type->per_cu.is_debug_types = 1;
2632 sig_type->per_cu.section = section;
2633 sig_type->per_cu.sect_off = sect_off;
2634 sig_type->per_cu.v.quick
2635 = OBSTACK_ZALLOC (&per_bfd->obstack,
2636 struct dwarf2_per_cu_quick_data);
2637
2638 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2639 *slot = sig_type;
2640
2641 per_bfd->all_type_units.push_back (sig_type);
2642 }
2643
2644 per_bfd->signatured_types = std::move (sig_types_hash);
2645 }
2646
2647 /* Create the signatured type hash table from .debug_names. */
2648
2649 static void
2650 create_signatured_type_table_from_debug_names
2651 (dwarf2_per_objfile *per_objfile,
2652 const mapped_debug_names &map,
2653 struct dwarf2_section_info *section,
2654 struct dwarf2_section_info *abbrev_section)
2655 {
2656 struct objfile *objfile = per_objfile->objfile;
2657
2658 section->read (objfile);
2659 abbrev_section->read (objfile);
2660
2661 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2662 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2663
2664 htab_up sig_types_hash = allocate_signatured_type_table ();
2665
2666 for (uint32_t i = 0; i < map.tu_count; ++i)
2667 {
2668 struct signatured_type *sig_type;
2669 void **slot;
2670
2671 sect_offset sect_off
2672 = (sect_offset) (extract_unsigned_integer
2673 (map.tu_table_reordered + i * map.offset_size,
2674 map.offset_size,
2675 map.dwarf5_byte_order));
2676
2677 comp_unit_head cu_header;
2678 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2679 abbrev_section,
2680 section->buffer + to_underlying (sect_off),
2681 rcuh_kind::TYPE);
2682
2683 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2684 sig_type->signature = cu_header.signature;
2685 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2686 sig_type->per_cu.is_debug_types = 1;
2687 sig_type->per_cu.section = section;
2688 sig_type->per_cu.sect_off = sect_off;
2689 sig_type->per_cu.v.quick
2690 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2691 struct dwarf2_per_cu_quick_data);
2692
2693 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2694 *slot = sig_type;
2695
2696 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2697 }
2698
2699 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2700 }
2701
2702 /* Read the address map data from the mapped index, and use it to
2703 populate the psymtabs_addrmap. */
2704
2705 static void
2706 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2707 struct mapped_index *index)
2708 {
2709 struct objfile *objfile = per_objfile->objfile;
2710 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2711 struct gdbarch *gdbarch = objfile->arch ();
2712 const gdb_byte *iter, *end;
2713 struct addrmap *mutable_map;
2714 CORE_ADDR baseaddr;
2715
2716 auto_obstack temp_obstack;
2717
2718 mutable_map = addrmap_create_mutable (&temp_obstack);
2719
2720 iter = index->address_table.data ();
2721 end = iter + index->address_table.size ();
2722
2723 baseaddr = objfile->text_section_offset ();
2724
2725 while (iter < end)
2726 {
2727 ULONGEST hi, lo, cu_index;
2728 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2729 iter += 8;
2730 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2731 iter += 8;
2732 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2733 iter += 4;
2734
2735 if (lo > hi)
2736 {
2737 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2738 hex_string (lo), hex_string (hi));
2739 continue;
2740 }
2741
2742 if (cu_index >= per_bfd->all_comp_units.size ())
2743 {
2744 complaint (_(".gdb_index address table has invalid CU number %u"),
2745 (unsigned) cu_index);
2746 continue;
2747 }
2748
2749 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2750 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2751 addrmap_set_empty (mutable_map, lo, hi - 1,
2752 per_bfd->get_cu (cu_index));
2753 }
2754
2755 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2756 &per_bfd->obstack);
2757 }
2758
2759 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2760 populate the psymtabs_addrmap. */
2761
2762 static void
2763 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2764 struct dwarf2_section_info *section)
2765 {
2766 struct objfile *objfile = per_objfile->objfile;
2767 bfd *abfd = objfile->obfd;
2768 struct gdbarch *gdbarch = objfile->arch ();
2769 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2770 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2771
2772 auto_obstack temp_obstack;
2773 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2774
2775 std::unordered_map<sect_offset,
2776 dwarf2_per_cu_data *,
2777 gdb::hash_enum<sect_offset>>
2778 debug_info_offset_to_per_cu;
2779 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2780 {
2781 const auto insertpair
2782 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2783 if (!insertpair.second)
2784 {
2785 warning (_("Section .debug_aranges in %s has duplicate "
2786 "debug_info_offset %s, ignoring .debug_aranges."),
2787 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2788 return;
2789 }
2790 }
2791
2792 section->read (objfile);
2793
2794 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2795
2796 const gdb_byte *addr = section->buffer;
2797
2798 while (addr < section->buffer + section->size)
2799 {
2800 const gdb_byte *const entry_addr = addr;
2801 unsigned int bytes_read;
2802
2803 const LONGEST entry_length = read_initial_length (abfd, addr,
2804 &bytes_read);
2805 addr += bytes_read;
2806
2807 const gdb_byte *const entry_end = addr + entry_length;
2808 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2809 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2810 if (addr + entry_length > section->buffer + section->size)
2811 {
2812 warning (_("Section .debug_aranges in %s entry at offset %s "
2813 "length %s exceeds section length %s, "
2814 "ignoring .debug_aranges."),
2815 objfile_name (objfile),
2816 plongest (entry_addr - section->buffer),
2817 plongest (bytes_read + entry_length),
2818 pulongest (section->size));
2819 return;
2820 }
2821
2822 /* The version number. */
2823 const uint16_t version = read_2_bytes (abfd, addr);
2824 addr += 2;
2825 if (version != 2)
2826 {
2827 warning (_("Section .debug_aranges in %s entry at offset %s "
2828 "has unsupported version %d, ignoring .debug_aranges."),
2829 objfile_name (objfile),
2830 plongest (entry_addr - section->buffer), version);
2831 return;
2832 }
2833
2834 const uint64_t debug_info_offset
2835 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2836 addr += offset_size;
2837 const auto per_cu_it
2838 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2839 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2840 {
2841 warning (_("Section .debug_aranges in %s entry at offset %s "
2842 "debug_info_offset %s does not exists, "
2843 "ignoring .debug_aranges."),
2844 objfile_name (objfile),
2845 plongest (entry_addr - section->buffer),
2846 pulongest (debug_info_offset));
2847 return;
2848 }
2849 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2850
2851 const uint8_t address_size = *addr++;
2852 if (address_size < 1 || address_size > 8)
2853 {
2854 warning (_("Section .debug_aranges in %s entry at offset %s "
2855 "address_size %u is invalid, ignoring .debug_aranges."),
2856 objfile_name (objfile),
2857 plongest (entry_addr - section->buffer), address_size);
2858 return;
2859 }
2860
2861 const uint8_t segment_selector_size = *addr++;
2862 if (segment_selector_size != 0)
2863 {
2864 warning (_("Section .debug_aranges in %s entry at offset %s "
2865 "segment_selector_size %u is not supported, "
2866 "ignoring .debug_aranges."),
2867 objfile_name (objfile),
2868 plongest (entry_addr - section->buffer),
2869 segment_selector_size);
2870 return;
2871 }
2872
2873 /* Must pad to an alignment boundary that is twice the address
2874 size. It is undocumented by the DWARF standard but GCC does
2875 use it. */
2876 for (size_t padding = ((-(addr - section->buffer))
2877 & (2 * address_size - 1));
2878 padding > 0; padding--)
2879 if (*addr++ != 0)
2880 {
2881 warning (_("Section .debug_aranges in %s entry at offset %s "
2882 "padding is not zero, ignoring .debug_aranges."),
2883 objfile_name (objfile),
2884 plongest (entry_addr - section->buffer));
2885 return;
2886 }
2887
2888 for (;;)
2889 {
2890 if (addr + 2 * address_size > entry_end)
2891 {
2892 warning (_("Section .debug_aranges in %s entry at offset %s "
2893 "address list is not properly terminated, "
2894 "ignoring .debug_aranges."),
2895 objfile_name (objfile),
2896 plongest (entry_addr - section->buffer));
2897 return;
2898 }
2899 ULONGEST start = extract_unsigned_integer (addr, address_size,
2900 dwarf5_byte_order);
2901 addr += address_size;
2902 ULONGEST length = extract_unsigned_integer (addr, address_size,
2903 dwarf5_byte_order);
2904 addr += address_size;
2905 if (start == 0 && length == 0)
2906 break;
2907 if (start == 0 && !per_bfd->has_section_at_zero)
2908 {
2909 /* Symbol was eliminated due to a COMDAT group. */
2910 continue;
2911 }
2912 ULONGEST end = start + length;
2913 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2914 - baseaddr);
2915 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2916 - baseaddr);
2917 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2918 }
2919 }
2920
2921 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2922 &per_bfd->obstack);
2923 }
2924
2925 /* Find a slot in the mapped index INDEX for the object named NAME.
2926 If NAME is found, set *VEC_OUT to point to the CU vector in the
2927 constant pool and return true. If NAME cannot be found, return
2928 false. */
2929
2930 static bool
2931 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2932 offset_type **vec_out)
2933 {
2934 offset_type hash;
2935 offset_type slot, step;
2936 int (*cmp) (const char *, const char *);
2937
2938 gdb::unique_xmalloc_ptr<char> without_params;
2939 if (current_language->la_language == language_cplus
2940 || current_language->la_language == language_fortran
2941 || current_language->la_language == language_d)
2942 {
2943 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2944 not contain any. */
2945
2946 if (strchr (name, '(') != NULL)
2947 {
2948 without_params = cp_remove_params (name);
2949
2950 if (without_params != NULL)
2951 name = without_params.get ();
2952 }
2953 }
2954
2955 /* Index version 4 did not support case insensitive searches. But the
2956 indices for case insensitive languages are built in lowercase, therefore
2957 simulate our NAME being searched is also lowercased. */
2958 hash = mapped_index_string_hash ((index->version == 4
2959 && case_sensitivity == case_sensitive_off
2960 ? 5 : index->version),
2961 name);
2962
2963 slot = hash & (index->symbol_table.size () - 1);
2964 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2965 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2966
2967 for (;;)
2968 {
2969 const char *str;
2970
2971 const auto &bucket = index->symbol_table[slot];
2972 if (bucket.name == 0 && bucket.vec == 0)
2973 return false;
2974
2975 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2976 if (!cmp (name, str))
2977 {
2978 *vec_out = (offset_type *) (index->constant_pool
2979 + MAYBE_SWAP (bucket.vec));
2980 return true;
2981 }
2982
2983 slot = (slot + step) & (index->symbol_table.size () - 1);
2984 }
2985 }
2986
2987 /* A helper function that reads the .gdb_index from BUFFER and fills
2988 in MAP. FILENAME is the name of the file containing the data;
2989 it is used for error reporting. DEPRECATED_OK is true if it is
2990 ok to use deprecated sections.
2991
2992 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2993 out parameters that are filled in with information about the CU and
2994 TU lists in the section.
2995
2996 Returns true if all went well, false otherwise. */
2997
2998 static bool
2999 read_gdb_index_from_buffer (const char *filename,
3000 bool deprecated_ok,
3001 gdb::array_view<const gdb_byte> buffer,
3002 struct mapped_index *map,
3003 const gdb_byte **cu_list,
3004 offset_type *cu_list_elements,
3005 const gdb_byte **types_list,
3006 offset_type *types_list_elements)
3007 {
3008 const gdb_byte *addr = &buffer[0];
3009
3010 /* Version check. */
3011 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3012 /* Versions earlier than 3 emitted every copy of a psymbol. This
3013 causes the index to behave very poorly for certain requests. Version 3
3014 contained incomplete addrmap. So, it seems better to just ignore such
3015 indices. */
3016 if (version < 4)
3017 {
3018 static int warning_printed = 0;
3019 if (!warning_printed)
3020 {
3021 warning (_("Skipping obsolete .gdb_index section in %s."),
3022 filename);
3023 warning_printed = 1;
3024 }
3025 return 0;
3026 }
3027 /* Index version 4 uses a different hash function than index version
3028 5 and later.
3029
3030 Versions earlier than 6 did not emit psymbols for inlined
3031 functions. Using these files will cause GDB not to be able to
3032 set breakpoints on inlined functions by name, so we ignore these
3033 indices unless the user has done
3034 "set use-deprecated-index-sections on". */
3035 if (version < 6 && !deprecated_ok)
3036 {
3037 static int warning_printed = 0;
3038 if (!warning_printed)
3039 {
3040 warning (_("\
3041 Skipping deprecated .gdb_index section in %s.\n\
3042 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3043 to use the section anyway."),
3044 filename);
3045 warning_printed = 1;
3046 }
3047 return 0;
3048 }
3049 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3050 of the TU (for symbols coming from TUs),
3051 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3052 Plus gold-generated indices can have duplicate entries for global symbols,
3053 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3054 These are just performance bugs, and we can't distinguish gdb-generated
3055 indices from gold-generated ones, so issue no warning here. */
3056
3057 /* Indexes with higher version than the one supported by GDB may be no
3058 longer backward compatible. */
3059 if (version > 8)
3060 return 0;
3061
3062 map->version = version;
3063
3064 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3065
3066 int i = 0;
3067 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3068 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3069 / 8);
3070 ++i;
3071
3072 *types_list = addr + MAYBE_SWAP (metadata[i]);
3073 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3074 - MAYBE_SWAP (metadata[i]))
3075 / 8);
3076 ++i;
3077
3078 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3079 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3080 map->address_table
3081 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3082 ++i;
3083
3084 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3085 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3086 map->symbol_table
3087 = gdb::array_view<mapped_index::symbol_table_slot>
3088 ((mapped_index::symbol_table_slot *) symbol_table,
3089 (mapped_index::symbol_table_slot *) symbol_table_end);
3090
3091 ++i;
3092 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3093
3094 return 1;
3095 }
3096
3097 /* Callback types for dwarf2_read_gdb_index. */
3098
3099 typedef gdb::function_view
3100 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3101 get_gdb_index_contents_ftype;
3102 typedef gdb::function_view
3103 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3104 get_gdb_index_contents_dwz_ftype;
3105
3106 /* Read .gdb_index. If everything went ok, initialize the "quick"
3107 elements of all the CUs and return 1. Otherwise, return 0. */
3108
3109 static int
3110 dwarf2_read_gdb_index
3111 (dwarf2_per_objfile *per_objfile,
3112 get_gdb_index_contents_ftype get_gdb_index_contents,
3113 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3114 {
3115 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3116 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3117 struct dwz_file *dwz;
3118 struct objfile *objfile = per_objfile->objfile;
3119 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3120
3121 gdb::array_view<const gdb_byte> main_index_contents
3122 = get_gdb_index_contents (objfile, per_bfd);
3123
3124 if (main_index_contents.empty ())
3125 return 0;
3126
3127 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3128 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3129 use_deprecated_index_sections,
3130 main_index_contents, map.get (), &cu_list,
3131 &cu_list_elements, &types_list,
3132 &types_list_elements))
3133 return 0;
3134
3135 /* Don't use the index if it's empty. */
3136 if (map->symbol_table.empty ())
3137 return 0;
3138
3139 /* If there is a .dwz file, read it so we can get its CU list as
3140 well. */
3141 dwz = dwarf2_get_dwz_file (per_bfd);
3142 if (dwz != NULL)
3143 {
3144 struct mapped_index dwz_map;
3145 const gdb_byte *dwz_types_ignore;
3146 offset_type dwz_types_elements_ignore;
3147
3148 gdb::array_view<const gdb_byte> dwz_index_content
3149 = get_gdb_index_contents_dwz (objfile, dwz);
3150
3151 if (dwz_index_content.empty ())
3152 return 0;
3153
3154 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3155 1, dwz_index_content, &dwz_map,
3156 &dwz_list, &dwz_list_elements,
3157 &dwz_types_ignore,
3158 &dwz_types_elements_ignore))
3159 {
3160 warning (_("could not read '.gdb_index' section from %s; skipping"),
3161 bfd_get_filename (dwz->dwz_bfd.get ()));
3162 return 0;
3163 }
3164 }
3165
3166 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3167 dwz_list_elements);
3168
3169 if (types_list_elements)
3170 {
3171 /* We can only handle a single .debug_types when we have an
3172 index. */
3173 if (per_bfd->types.size () != 1)
3174 return 0;
3175
3176 dwarf2_section_info *section = &per_bfd->types[0];
3177
3178 create_signatured_type_table_from_index (per_bfd, section, types_list,
3179 types_list_elements);
3180 }
3181
3182 create_addrmap_from_index (per_objfile, map.get ());
3183
3184 per_bfd->index_table = std::move (map);
3185 per_bfd->using_index = 1;
3186 per_bfd->quick_file_names_table =
3187 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3188
3189 return 1;
3190 }
3191
3192 /* die_reader_func for dw2_get_file_names. */
3193
3194 static void
3195 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3196 struct die_info *comp_unit_die)
3197 {
3198 struct dwarf2_cu *cu = reader->cu;
3199 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3200 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3201 struct dwarf2_per_cu_data *lh_cu;
3202 struct attribute *attr;
3203 void **slot;
3204 struct quick_file_names *qfn;
3205
3206 gdb_assert (! this_cu->is_debug_types);
3207
3208 /* Our callers never want to match partial units -- instead they
3209 will match the enclosing full CU. */
3210 if (comp_unit_die->tag == DW_TAG_partial_unit)
3211 {
3212 this_cu->v.quick->no_file_data = 1;
3213 return;
3214 }
3215
3216 lh_cu = this_cu;
3217 slot = NULL;
3218
3219 line_header_up lh;
3220 sect_offset line_offset {};
3221
3222 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3223 if (attr != nullptr && attr->form_is_unsigned ())
3224 {
3225 struct quick_file_names find_entry;
3226
3227 line_offset = (sect_offset) attr->as_unsigned ();
3228
3229 /* We may have already read in this line header (TU line header sharing).
3230 If we have we're done. */
3231 find_entry.hash.dwo_unit = cu->dwo_unit;
3232 find_entry.hash.line_sect_off = line_offset;
3233 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3234 &find_entry, INSERT);
3235 if (*slot != NULL)
3236 {
3237 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3238 return;
3239 }
3240
3241 lh = dwarf_decode_line_header (line_offset, cu);
3242 }
3243 if (lh == NULL)
3244 {
3245 lh_cu->v.quick->no_file_data = 1;
3246 return;
3247 }
3248
3249 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3250 qfn->hash.dwo_unit = cu->dwo_unit;
3251 qfn->hash.line_sect_off = line_offset;
3252 gdb_assert (slot != NULL);
3253 *slot = qfn;
3254
3255 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3256
3257 int offset = 0;
3258 if (strcmp (fnd.name, "<unknown>") != 0)
3259 ++offset;
3260
3261 qfn->num_file_names = offset + lh->file_names_size ();
3262 qfn->file_names =
3263 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3264 qfn->num_file_names);
3265 if (offset != 0)
3266 qfn->file_names[0] = xstrdup (fnd.name);
3267 for (int i = 0; i < lh->file_names_size (); ++i)
3268 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3269 fnd.comp_dir).release ();
3270 qfn->real_names = NULL;
3271
3272 lh_cu->v.quick->file_names = qfn;
3273 }
3274
3275 /* A helper for the "quick" functions which attempts to read the line
3276 table for THIS_CU. */
3277
3278 static struct quick_file_names *
3279 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3280 dwarf2_per_objfile *per_objfile)
3281 {
3282 /* This should never be called for TUs. */
3283 gdb_assert (! this_cu->is_debug_types);
3284 /* Nor type unit groups. */
3285 gdb_assert (! this_cu->type_unit_group_p ());
3286
3287 if (this_cu->v.quick->file_names != NULL)
3288 return this_cu->v.quick->file_names;
3289 /* If we know there is no line data, no point in looking again. */
3290 if (this_cu->v.quick->no_file_data)
3291 return NULL;
3292
3293 cutu_reader reader (this_cu, per_objfile);
3294 if (!reader.dummy_p)
3295 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3296
3297 if (this_cu->v.quick->no_file_data)
3298 return NULL;
3299 return this_cu->v.quick->file_names;
3300 }
3301
3302 /* A helper for the "quick" functions which computes and caches the
3303 real path for a given file name from the line table. */
3304
3305 static const char *
3306 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3307 struct quick_file_names *qfn, int index)
3308 {
3309 if (qfn->real_names == NULL)
3310 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3311 qfn->num_file_names, const char *);
3312
3313 if (qfn->real_names[index] == NULL)
3314 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3315
3316 return qfn->real_names[index];
3317 }
3318
3319 struct symtab *
3320 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3321 {
3322 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3323 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3324 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3325
3326 if (cust == NULL)
3327 return NULL;
3328
3329 return compunit_primary_filetab (cust);
3330 }
3331
3332 /* Traversal function for dw2_forget_cached_source_info. */
3333
3334 static int
3335 dw2_free_cached_file_names (void **slot, void *info)
3336 {
3337 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3338
3339 if (file_data->real_names)
3340 {
3341 int i;
3342
3343 for (i = 0; i < file_data->num_file_names; ++i)
3344 {
3345 xfree ((void*) file_data->real_names[i]);
3346 file_data->real_names[i] = NULL;
3347 }
3348 }
3349
3350 return 1;
3351 }
3352
3353 void
3354 dwarf2_base_index_functions::forget_cached_source_info
3355 (struct objfile *objfile)
3356 {
3357 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3358
3359 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3360 dw2_free_cached_file_names, NULL);
3361 }
3362
3363 /* Helper function for dw2_map_symtabs_matching_filename that expands
3364 the symtabs and calls the iterator. */
3365
3366 static int
3367 dw2_map_expand_apply (struct objfile *objfile,
3368 struct dwarf2_per_cu_data *per_cu,
3369 const char *name, const char *real_path,
3370 gdb::function_view<bool (symtab *)> callback)
3371 {
3372 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3373
3374 /* Don't visit already-expanded CUs. */
3375 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3376 if (per_objfile->symtab_set_p (per_cu))
3377 return 0;
3378
3379 /* This may expand more than one symtab, and we want to iterate over
3380 all of them. */
3381 dw2_instantiate_symtab (per_cu, per_objfile, false);
3382
3383 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3384 last_made, callback);
3385 }
3386
3387 /* Implementation of the map_symtabs_matching_filename method. */
3388
3389 bool
3390 dwarf2_base_index_functions::map_symtabs_matching_filename
3391 (struct objfile *objfile, const char *name, const char *real_path,
3392 gdb::function_view<bool (symtab *)> callback)
3393 {
3394 const char *name_basename = lbasename (name);
3395 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3396
3397 /* The rule is CUs specify all the files, including those used by
3398 any TU, so there's no need to scan TUs here. */
3399
3400 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3401 {
3402 /* We only need to look at symtabs not already expanded. */
3403 if (per_objfile->symtab_set_p (per_cu))
3404 continue;
3405
3406 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3407 if (file_data == NULL)
3408 continue;
3409
3410 for (int j = 0; j < file_data->num_file_names; ++j)
3411 {
3412 const char *this_name = file_data->file_names[j];
3413 const char *this_real_name;
3414
3415 if (compare_filenames_for_search (this_name, name))
3416 {
3417 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3418 callback))
3419 return true;
3420 continue;
3421 }
3422
3423 /* Before we invoke realpath, which can get expensive when many
3424 files are involved, do a quick comparison of the basenames. */
3425 if (! basenames_may_differ
3426 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3427 continue;
3428
3429 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3430 if (compare_filenames_for_search (this_real_name, name))
3431 {
3432 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3433 callback))
3434 return true;
3435 continue;
3436 }
3437
3438 if (real_path != NULL)
3439 {
3440 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3441 gdb_assert (IS_ABSOLUTE_PATH (name));
3442 if (this_real_name != NULL
3443 && FILENAME_CMP (real_path, this_real_name) == 0)
3444 {
3445 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3446 callback))
3447 return true;
3448 continue;
3449 }
3450 }
3451 }
3452 }
3453
3454 return false;
3455 }
3456
3457 /* Struct used to manage iterating over all CUs looking for a symbol. */
3458
3459 struct dw2_symtab_iterator
3460 {
3461 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3462 dwarf2_per_objfile *per_objfile;
3463 /* If set, only look for symbols that match that block. Valid values are
3464 GLOBAL_BLOCK and STATIC_BLOCK. */
3465 gdb::optional<block_enum> block_index;
3466 /* The kind of symbol we're looking for. */
3467 domain_enum domain;
3468 /* The list of CUs from the index entry of the symbol,
3469 or NULL if not found. */
3470 offset_type *vec;
3471 /* The next element in VEC to look at. */
3472 int next;
3473 /* The number of elements in VEC, or zero if there is no match. */
3474 int length;
3475 /* Have we seen a global version of the symbol?
3476 If so we can ignore all further global instances.
3477 This is to work around gold/15646, inefficient gold-generated
3478 indices. */
3479 int global_seen;
3480 };
3481
3482 /* Initialize the index symtab iterator ITER, common part. */
3483
3484 static void
3485 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3486 dwarf2_per_objfile *per_objfile,
3487 gdb::optional<block_enum> block_index,
3488 domain_enum domain)
3489 {
3490 iter->per_objfile = per_objfile;
3491 iter->block_index = block_index;
3492 iter->domain = domain;
3493 iter->next = 0;
3494 iter->global_seen = 0;
3495 iter->vec = NULL;
3496 iter->length = 0;
3497 }
3498
3499 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3500
3501 static void
3502 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3503 dwarf2_per_objfile *per_objfile,
3504 gdb::optional<block_enum> block_index,
3505 domain_enum domain,
3506 const char *name)
3507 {
3508 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3509
3510 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3511 /* index is NULL if OBJF_READNOW. */
3512 if (index == NULL)
3513 return;
3514
3515 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3516 iter->length = MAYBE_SWAP (*iter->vec);
3517 }
3518
3519 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3520
3521 static void
3522 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3523 dwarf2_per_objfile *per_objfile,
3524 gdb::optional<block_enum> block_index,
3525 domain_enum domain, offset_type namei)
3526 {
3527 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3528
3529 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3530 /* index is NULL if OBJF_READNOW. */
3531 if (index == NULL)
3532 return;
3533
3534 gdb_assert (!index->symbol_name_slot_invalid (namei));
3535 const auto &bucket = index->symbol_table[namei];
3536
3537 iter->vec = (offset_type *) (index->constant_pool
3538 + MAYBE_SWAP (bucket.vec));
3539 iter->length = MAYBE_SWAP (*iter->vec);
3540 }
3541
3542 /* Return the next matching CU or NULL if there are no more. */
3543
3544 static struct dwarf2_per_cu_data *
3545 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3546 {
3547 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3548
3549 for ( ; iter->next < iter->length; ++iter->next)
3550 {
3551 offset_type cu_index_and_attrs =
3552 MAYBE_SWAP (iter->vec[iter->next + 1]);
3553 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3554 gdb_index_symbol_kind symbol_kind =
3555 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3556 /* Only check the symbol attributes if they're present.
3557 Indices prior to version 7 don't record them,
3558 and indices >= 7 may elide them for certain symbols
3559 (gold does this). */
3560 int attrs_valid =
3561 (per_objfile->per_bfd->index_table->version >= 7
3562 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3563
3564 /* Don't crash on bad data. */
3565 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3566 + per_objfile->per_bfd->all_type_units.size ()))
3567 {
3568 complaint (_(".gdb_index entry has bad CU index"
3569 " [in module %s]"), objfile_name (per_objfile->objfile));
3570 continue;
3571 }
3572
3573 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3574
3575 /* Skip if already read in. */
3576 if (per_objfile->symtab_set_p (per_cu))
3577 continue;
3578
3579 /* Check static vs global. */
3580 if (attrs_valid)
3581 {
3582 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3583
3584 if (iter->block_index.has_value ())
3585 {
3586 bool want_static = *iter->block_index == STATIC_BLOCK;
3587
3588 if (is_static != want_static)
3589 continue;
3590 }
3591
3592 /* Work around gold/15646. */
3593 if (!is_static
3594 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3595 {
3596 if (iter->global_seen)
3597 continue;
3598
3599 iter->global_seen = 1;
3600 }
3601 }
3602
3603 /* Only check the symbol's kind if it has one. */
3604 if (attrs_valid)
3605 {
3606 switch (iter->domain)
3607 {
3608 case VAR_DOMAIN:
3609 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3610 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3611 /* Some types are also in VAR_DOMAIN. */
3612 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3613 continue;
3614 break;
3615 case STRUCT_DOMAIN:
3616 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3617 continue;
3618 break;
3619 case LABEL_DOMAIN:
3620 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3621 continue;
3622 break;
3623 case MODULE_DOMAIN:
3624 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3625 continue;
3626 break;
3627 default:
3628 break;
3629 }
3630 }
3631
3632 ++iter->next;
3633 return per_cu;
3634 }
3635
3636 return NULL;
3637 }
3638
3639 struct compunit_symtab *
3640 dwarf2_gdb_index::lookup_symbol (struct objfile *objfile,
3641 block_enum block_index,
3642 const char *name, domain_enum domain)
3643 {
3644 struct compunit_symtab *stab_best = NULL;
3645 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3646
3647 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3648
3649 struct dw2_symtab_iterator iter;
3650 struct dwarf2_per_cu_data *per_cu;
3651
3652 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3653
3654 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3655 {
3656 struct symbol *sym, *with_opaque = NULL;
3657 struct compunit_symtab *stab
3658 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3659 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3660 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3661
3662 sym = block_find_symbol (block, name, domain,
3663 block_find_non_opaque_type_preferred,
3664 &with_opaque);
3665
3666 /* Some caution must be observed with overloaded functions
3667 and methods, since the index will not contain any overload
3668 information (but NAME might contain it). */
3669
3670 if (sym != NULL
3671 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3672 return stab;
3673 if (with_opaque != NULL
3674 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3675 stab_best = stab;
3676
3677 /* Keep looking through other CUs. */
3678 }
3679
3680 return stab_best;
3681 }
3682
3683 void
3684 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3685 bool print_bcache)
3686 {
3687 if (print_bcache)
3688 return;
3689
3690 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3691 int total = (per_objfile->per_bfd->all_comp_units.size ()
3692 + per_objfile->per_bfd->all_type_units.size ());
3693 int count = 0;
3694
3695 for (int i = 0; i < total; ++i)
3696 {
3697 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3698
3699 if (!per_objfile->symtab_set_p (per_cu))
3700 ++count;
3701 }
3702 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3703 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3704 }
3705
3706 /* This dumps minimal information about the index.
3707 It is called via "mt print objfiles".
3708 One use is to verify .gdb_index has been loaded by the
3709 gdb.dwarf2/gdb-index.exp testcase. */
3710
3711 void
3712 dwarf2_gdb_index::dump (struct objfile *objfile)
3713 {
3714 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3715
3716 gdb_assert (per_objfile->per_bfd->using_index);
3717 printf_filtered (".gdb_index:");
3718 if (per_objfile->per_bfd->index_table != NULL)
3719 {
3720 printf_filtered (" version %d\n",
3721 per_objfile->per_bfd->index_table->version);
3722 }
3723 else
3724 printf_filtered (" faked for \"readnow\"\n");
3725 printf_filtered ("\n");
3726 }
3727
3728 void
3729 dwarf2_gdb_index::expand_symtabs_for_function (struct objfile *objfile,
3730 const char *func_name)
3731 {
3732 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3733
3734 struct dw2_symtab_iterator iter;
3735 struct dwarf2_per_cu_data *per_cu;
3736
3737 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3738
3739 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3740 dw2_instantiate_symtab (per_cu, per_objfile, false);
3741
3742 }
3743
3744 void
3745 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3746 {
3747 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3748 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3749 + per_objfile->per_bfd->all_type_units.size ());
3750
3751 for (int i = 0; i < total_units; ++i)
3752 {
3753 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3754
3755 /* We don't want to directly expand a partial CU, because if we
3756 read it with the wrong language, then assertion failures can
3757 be triggered later on. See PR symtab/23010. So, tell
3758 dw2_instantiate_symtab to skip partial CUs -- any important
3759 partial CU will be read via DW_TAG_imported_unit anyway. */
3760 dw2_instantiate_symtab (per_cu, per_objfile, true);
3761 }
3762 }
3763
3764 void
3765 dwarf2_base_index_functions::expand_symtabs_with_fullname
3766 (struct objfile *objfile, const char *fullname)
3767 {
3768 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3769
3770 /* We don't need to consider type units here.
3771 This is only called for examining code, e.g. expand_line_sal.
3772 There can be an order of magnitude (or more) more type units
3773 than comp units, and we avoid them if we can. */
3774
3775 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3776 {
3777 /* We only need to look at symtabs not already expanded. */
3778 if (per_objfile->symtab_set_p (per_cu))
3779 continue;
3780
3781 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3782 if (file_data == NULL)
3783 continue;
3784
3785 for (int j = 0; j < file_data->num_file_names; ++j)
3786 {
3787 const char *this_fullname = file_data->file_names[j];
3788
3789 if (filename_cmp (this_fullname, fullname) == 0)
3790 {
3791 dw2_instantiate_symtab (per_cu, per_objfile, false);
3792 break;
3793 }
3794 }
3795 }
3796 }
3797
3798 static void
3799 dw2_expand_symtabs_matching_symbol
3800 (mapped_index_base &index,
3801 const lookup_name_info &lookup_name_in,
3802 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3803 enum search_domain kind,
3804 gdb::function_view<bool (offset_type)> match_callback,
3805 dwarf2_per_objfile *per_objfile);
3806
3807 static void
3808 dw2_expand_symtabs_matching_one
3809 (dwarf2_per_cu_data *per_cu,
3810 dwarf2_per_objfile *per_objfile,
3811 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3812 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3813
3814 static void
3815 dw2_map_matching_symbols
3816 (struct objfile *objfile,
3817 const lookup_name_info &name, domain_enum domain,
3818 int global,
3819 gdb::function_view<symbol_found_callback_ftype> callback,
3820 symbol_compare_ftype *ordered_compare)
3821 {
3822 /* Used for Ada. */
3823 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3824
3825 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3826
3827 if (per_objfile->per_bfd->index_table != nullptr)
3828 {
3829 mapped_index &index = *per_objfile->per_bfd->index_table;
3830
3831 const char *match_name = name.ada ().lookup_name ().c_str ();
3832 auto matcher = [&] (const char *symname)
3833 {
3834 if (ordered_compare == nullptr)
3835 return true;
3836 return ordered_compare (symname, match_name) == 0;
3837 };
3838
3839 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3840 [&] (offset_type namei)
3841 {
3842 struct dw2_symtab_iterator iter;
3843 struct dwarf2_per_cu_data *per_cu;
3844
3845 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3846 namei);
3847 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3848 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3849 nullptr);
3850 return true;
3851 }, per_objfile);
3852 }
3853 else
3854 {
3855 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3856 proceed assuming all symtabs have been read in. */
3857 }
3858
3859 for (compunit_symtab *cust : objfile->compunits ())
3860 {
3861 const struct block *block;
3862
3863 if (cust == NULL)
3864 continue;
3865 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3866 if (!iterate_over_symbols_terminated (block, name,
3867 domain, callback))
3868 return;
3869 }
3870 }
3871
3872 void
3873 dwarf2_gdb_index::map_matching_symbols
3874 (struct objfile *objfile,
3875 const lookup_name_info &name, domain_enum domain,
3876 int global,
3877 gdb::function_view<symbol_found_callback_ftype> callback,
3878 symbol_compare_ftype *ordered_compare)
3879 {
3880 dw2_map_matching_symbols (objfile, name, domain, global, callback,
3881 ordered_compare);
3882 }
3883
3884 /* Starting from a search name, return the string that finds the upper
3885 bound of all strings that start with SEARCH_NAME in a sorted name
3886 list. Returns the empty string to indicate that the upper bound is
3887 the end of the list. */
3888
3889 static std::string
3890 make_sort_after_prefix_name (const char *search_name)
3891 {
3892 /* When looking to complete "func", we find the upper bound of all
3893 symbols that start with "func" by looking for where we'd insert
3894 the closest string that would follow "func" in lexicographical
3895 order. Usually, that's "func"-with-last-character-incremented,
3896 i.e. "fund". Mind non-ASCII characters, though. Usually those
3897 will be UTF-8 multi-byte sequences, but we can't be certain.
3898 Especially mind the 0xff character, which is a valid character in
3899 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3900 rule out compilers allowing it in identifiers. Note that
3901 conveniently, strcmp/strcasecmp are specified to compare
3902 characters interpreted as unsigned char. So what we do is treat
3903 the whole string as a base 256 number composed of a sequence of
3904 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3905 to 0, and carries 1 to the following more-significant position.
3906 If the very first character in SEARCH_NAME ends up incremented
3907 and carries/overflows, then the upper bound is the end of the
3908 list. The string after the empty string is also the empty
3909 string.
3910
3911 Some examples of this operation:
3912
3913 SEARCH_NAME => "+1" RESULT
3914
3915 "abc" => "abd"
3916 "ab\xff" => "ac"
3917 "\xff" "a" "\xff" => "\xff" "b"
3918 "\xff" => ""
3919 "\xff\xff" => ""
3920 "" => ""
3921
3922 Then, with these symbols for example:
3923
3924 func
3925 func1
3926 fund
3927
3928 completing "func" looks for symbols between "func" and
3929 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3930 which finds "func" and "func1", but not "fund".
3931
3932 And with:
3933
3934 funcÿ (Latin1 'ÿ' [0xff])
3935 funcÿ1
3936 fund
3937
3938 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3939 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3940
3941 And with:
3942
3943 ÿÿ (Latin1 'ÿ' [0xff])
3944 ÿÿ1
3945
3946 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3947 the end of the list.
3948 */
3949 std::string after = search_name;
3950 while (!after.empty () && (unsigned char) after.back () == 0xff)
3951 after.pop_back ();
3952 if (!after.empty ())
3953 after.back () = (unsigned char) after.back () + 1;
3954 return after;
3955 }
3956
3957 /* See declaration. */
3958
3959 std::pair<std::vector<name_component>::const_iterator,
3960 std::vector<name_component>::const_iterator>
3961 mapped_index_base::find_name_components_bounds
3962 (const lookup_name_info &lookup_name_without_params, language lang,
3963 dwarf2_per_objfile *per_objfile) const
3964 {
3965 auto *name_cmp
3966 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3967
3968 const char *lang_name
3969 = lookup_name_without_params.language_lookup_name (lang);
3970
3971 /* Comparison function object for lower_bound that matches against a
3972 given symbol name. */
3973 auto lookup_compare_lower = [&] (const name_component &elem,
3974 const char *name)
3975 {
3976 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3977 const char *elem_name = elem_qualified + elem.name_offset;
3978 return name_cmp (elem_name, name) < 0;
3979 };
3980
3981 /* Comparison function object for upper_bound that matches against a
3982 given symbol name. */
3983 auto lookup_compare_upper = [&] (const char *name,
3984 const name_component &elem)
3985 {
3986 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3987 const char *elem_name = elem_qualified + elem.name_offset;
3988 return name_cmp (name, elem_name) < 0;
3989 };
3990
3991 auto begin = this->name_components.begin ();
3992 auto end = this->name_components.end ();
3993
3994 /* Find the lower bound. */
3995 auto lower = [&] ()
3996 {
3997 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3998 return begin;
3999 else
4000 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4001 } ();
4002
4003 /* Find the upper bound. */
4004 auto upper = [&] ()
4005 {
4006 if (lookup_name_without_params.completion_mode ())
4007 {
4008 /* In completion mode, we want UPPER to point past all
4009 symbols names that have the same prefix. I.e., with
4010 these symbols, and completing "func":
4011
4012 function << lower bound
4013 function1
4014 other_function << upper bound
4015
4016 We find the upper bound by looking for the insertion
4017 point of "func"-with-last-character-incremented,
4018 i.e. "fund". */
4019 std::string after = make_sort_after_prefix_name (lang_name);
4020 if (after.empty ())
4021 return end;
4022 return std::lower_bound (lower, end, after.c_str (),
4023 lookup_compare_lower);
4024 }
4025 else
4026 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4027 } ();
4028
4029 return {lower, upper};
4030 }
4031
4032 /* See declaration. */
4033
4034 void
4035 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4036 {
4037 if (!this->name_components.empty ())
4038 return;
4039
4040 this->name_components_casing = case_sensitivity;
4041 auto *name_cmp
4042 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4043
4044 /* The code below only knows how to break apart components of C++
4045 symbol names (and other languages that use '::' as
4046 namespace/module separator) and Ada symbol names. */
4047 auto count = this->symbol_name_count ();
4048 for (offset_type idx = 0; idx < count; idx++)
4049 {
4050 if (this->symbol_name_slot_invalid (idx))
4051 continue;
4052
4053 const char *name = this->symbol_name_at (idx, per_objfile);
4054
4055 /* Add each name component to the name component table. */
4056 unsigned int previous_len = 0;
4057
4058 if (strstr (name, "::") != nullptr)
4059 {
4060 for (unsigned int current_len = cp_find_first_component (name);
4061 name[current_len] != '\0';
4062 current_len += cp_find_first_component (name + current_len))
4063 {
4064 gdb_assert (name[current_len] == ':');
4065 this->name_components.push_back ({previous_len, idx});
4066 /* Skip the '::'. */
4067 current_len += 2;
4068 previous_len = current_len;
4069 }
4070 }
4071 else
4072 {
4073 /* Handle the Ada encoded (aka mangled) form here. */
4074 for (const char *iter = strstr (name, "__");
4075 iter != nullptr;
4076 iter = strstr (iter, "__"))
4077 {
4078 this->name_components.push_back ({previous_len, idx});
4079 iter += 2;
4080 previous_len = iter - name;
4081 }
4082 }
4083
4084 this->name_components.push_back ({previous_len, idx});
4085 }
4086
4087 /* Sort name_components elements by name. */
4088 auto name_comp_compare = [&] (const name_component &left,
4089 const name_component &right)
4090 {
4091 const char *left_qualified
4092 = this->symbol_name_at (left.idx, per_objfile);
4093 const char *right_qualified
4094 = this->symbol_name_at (right.idx, per_objfile);
4095
4096 const char *left_name = left_qualified + left.name_offset;
4097 const char *right_name = right_qualified + right.name_offset;
4098
4099 return name_cmp (left_name, right_name) < 0;
4100 };
4101
4102 std::sort (this->name_components.begin (),
4103 this->name_components.end (),
4104 name_comp_compare);
4105 }
4106
4107 /* Helper for dw2_expand_symtabs_matching that works with a
4108 mapped_index_base instead of the containing objfile. This is split
4109 to a separate function in order to be able to unit test the
4110 name_components matching using a mock mapped_index_base. For each
4111 symbol name that matches, calls MATCH_CALLBACK, passing it the
4112 symbol's index in the mapped_index_base symbol table. */
4113
4114 static void
4115 dw2_expand_symtabs_matching_symbol
4116 (mapped_index_base &index,
4117 const lookup_name_info &lookup_name_in,
4118 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4119 enum search_domain kind,
4120 gdb::function_view<bool (offset_type)> match_callback,
4121 dwarf2_per_objfile *per_objfile)
4122 {
4123 lookup_name_info lookup_name_without_params
4124 = lookup_name_in.make_ignore_params ();
4125
4126 /* Build the symbol name component sorted vector, if we haven't
4127 yet. */
4128 index.build_name_components (per_objfile);
4129
4130 /* The same symbol may appear more than once in the range though.
4131 E.g., if we're looking for symbols that complete "w", and we have
4132 a symbol named "w1::w2", we'll find the two name components for
4133 that same symbol in the range. To be sure we only call the
4134 callback once per symbol, we first collect the symbol name
4135 indexes that matched in a temporary vector and ignore
4136 duplicates. */
4137 std::vector<offset_type> matches;
4138
4139 struct name_and_matcher
4140 {
4141 symbol_name_matcher_ftype *matcher;
4142 const char *name;
4143
4144 bool operator== (const name_and_matcher &other) const
4145 {
4146 return matcher == other.matcher && strcmp (name, other.name) == 0;
4147 }
4148 };
4149
4150 /* A vector holding all the different symbol name matchers, for all
4151 languages. */
4152 std::vector<name_and_matcher> matchers;
4153
4154 for (int i = 0; i < nr_languages; i++)
4155 {
4156 enum language lang_e = (enum language) i;
4157
4158 const language_defn *lang = language_def (lang_e);
4159 symbol_name_matcher_ftype *name_matcher
4160 = lang->get_symbol_name_matcher (lookup_name_without_params);
4161
4162 name_and_matcher key {
4163 name_matcher,
4164 lookup_name_without_params.language_lookup_name (lang_e)
4165 };
4166
4167 /* Don't insert the same comparison routine more than once.
4168 Note that we do this linear walk. This is not a problem in
4169 practice because the number of supported languages is
4170 low. */
4171 if (std::find (matchers.begin (), matchers.end (), key)
4172 != matchers.end ())
4173 continue;
4174 matchers.push_back (std::move (key));
4175
4176 auto bounds
4177 = index.find_name_components_bounds (lookup_name_without_params,
4178 lang_e, per_objfile);
4179
4180 /* Now for each symbol name in range, check to see if we have a name
4181 match, and if so, call the MATCH_CALLBACK callback. */
4182
4183 for (; bounds.first != bounds.second; ++bounds.first)
4184 {
4185 const char *qualified
4186 = index.symbol_name_at (bounds.first->idx, per_objfile);
4187
4188 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4189 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4190 continue;
4191
4192 matches.push_back (bounds.first->idx);
4193 }
4194 }
4195
4196 std::sort (matches.begin (), matches.end ());
4197
4198 /* Finally call the callback, once per match. */
4199 ULONGEST prev = -1;
4200 for (offset_type idx : matches)
4201 {
4202 if (prev != idx)
4203 {
4204 if (!match_callback (idx))
4205 break;
4206 prev = idx;
4207 }
4208 }
4209
4210 /* Above we use a type wider than idx's for 'prev', since 0 and
4211 (offset_type)-1 are both possible values. */
4212 static_assert (sizeof (prev) > sizeof (offset_type), "");
4213 }
4214
4215 #if GDB_SELF_TEST
4216
4217 namespace selftests { namespace dw2_expand_symtabs_matching {
4218
4219 /* A mock .gdb_index/.debug_names-like name index table, enough to
4220 exercise dw2_expand_symtabs_matching_symbol, which works with the
4221 mapped_index_base interface. Builds an index from the symbol list
4222 passed as parameter to the constructor. */
4223 class mock_mapped_index : public mapped_index_base
4224 {
4225 public:
4226 mock_mapped_index (gdb::array_view<const char *> symbols)
4227 : m_symbol_table (symbols)
4228 {}
4229
4230 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4231
4232 /* Return the number of names in the symbol table. */
4233 size_t symbol_name_count () const override
4234 {
4235 return m_symbol_table.size ();
4236 }
4237
4238 /* Get the name of the symbol at IDX in the symbol table. */
4239 const char *symbol_name_at
4240 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4241 {
4242 return m_symbol_table[idx];
4243 }
4244
4245 private:
4246 gdb::array_view<const char *> m_symbol_table;
4247 };
4248
4249 /* Convenience function that converts a NULL pointer to a "<null>"
4250 string, to pass to print routines. */
4251
4252 static const char *
4253 string_or_null (const char *str)
4254 {
4255 return str != NULL ? str : "<null>";
4256 }
4257
4258 /* Check if a lookup_name_info built from
4259 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4260 index. EXPECTED_LIST is the list of expected matches, in expected
4261 matching order. If no match expected, then an empty list is
4262 specified. Returns true on success. On failure prints a warning
4263 indicating the file:line that failed, and returns false. */
4264
4265 static bool
4266 check_match (const char *file, int line,
4267 mock_mapped_index &mock_index,
4268 const char *name, symbol_name_match_type match_type,
4269 bool completion_mode,
4270 std::initializer_list<const char *> expected_list,
4271 dwarf2_per_objfile *per_objfile)
4272 {
4273 lookup_name_info lookup_name (name, match_type, completion_mode);
4274
4275 bool matched = true;
4276
4277 auto mismatch = [&] (const char *expected_str,
4278 const char *got)
4279 {
4280 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4281 "expected=\"%s\", got=\"%s\"\n"),
4282 file, line,
4283 (match_type == symbol_name_match_type::FULL
4284 ? "FULL" : "WILD"),
4285 name, string_or_null (expected_str), string_or_null (got));
4286 matched = false;
4287 };
4288
4289 auto expected_it = expected_list.begin ();
4290 auto expected_end = expected_list.end ();
4291
4292 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4293 NULL, ALL_DOMAIN,
4294 [&] (offset_type idx)
4295 {
4296 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4297 const char *expected_str
4298 = expected_it == expected_end ? NULL : *expected_it++;
4299
4300 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4301 mismatch (expected_str, matched_name);
4302 return true;
4303 }, per_objfile);
4304
4305 const char *expected_str
4306 = expected_it == expected_end ? NULL : *expected_it++;
4307 if (expected_str != NULL)
4308 mismatch (expected_str, NULL);
4309
4310 return matched;
4311 }
4312
4313 /* The symbols added to the mock mapped_index for testing (in
4314 canonical form). */
4315 static const char *test_symbols[] = {
4316 "function",
4317 "std::bar",
4318 "std::zfunction",
4319 "std::zfunction2",
4320 "w1::w2",
4321 "ns::foo<char*>",
4322 "ns::foo<int>",
4323 "ns::foo<long>",
4324 "ns2::tmpl<int>::foo2",
4325 "(anonymous namespace)::A::B::C",
4326
4327 /* These are used to check that the increment-last-char in the
4328 matching algorithm for completion doesn't match "t1_fund" when
4329 completing "t1_func". */
4330 "t1_func",
4331 "t1_func1",
4332 "t1_fund",
4333 "t1_fund1",
4334
4335 /* A UTF-8 name with multi-byte sequences to make sure that
4336 cp-name-parser understands this as a single identifier ("função"
4337 is "function" in PT). */
4338 u8"u8função",
4339
4340 /* \377 (0xff) is Latin1 'ÿ'. */
4341 "yfunc\377",
4342
4343 /* \377 (0xff) is Latin1 'ÿ'. */
4344 "\377",
4345 "\377\377123",
4346
4347 /* A name with all sorts of complications. Starts with "z" to make
4348 it easier for the completion tests below. */
4349 #define Z_SYM_NAME \
4350 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4351 "::tuple<(anonymous namespace)::ui*, " \
4352 "std::default_delete<(anonymous namespace)::ui>, void>"
4353
4354 Z_SYM_NAME
4355 };
4356
4357 /* Returns true if the mapped_index_base::find_name_component_bounds
4358 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4359 in completion mode. */
4360
4361 static bool
4362 check_find_bounds_finds (mapped_index_base &index,
4363 const char *search_name,
4364 gdb::array_view<const char *> expected_syms,
4365 dwarf2_per_objfile *per_objfile)
4366 {
4367 lookup_name_info lookup_name (search_name,
4368 symbol_name_match_type::FULL, true);
4369
4370 auto bounds = index.find_name_components_bounds (lookup_name,
4371 language_cplus,
4372 per_objfile);
4373
4374 size_t distance = std::distance (bounds.first, bounds.second);
4375 if (distance != expected_syms.size ())
4376 return false;
4377
4378 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4379 {
4380 auto nc_elem = bounds.first + exp_elem;
4381 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4382 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4383 return false;
4384 }
4385
4386 return true;
4387 }
4388
4389 /* Test the lower-level mapped_index::find_name_component_bounds
4390 method. */
4391
4392 static void
4393 test_mapped_index_find_name_component_bounds ()
4394 {
4395 mock_mapped_index mock_index (test_symbols);
4396
4397 mock_index.build_name_components (NULL /* per_objfile */);
4398
4399 /* Test the lower-level mapped_index::find_name_component_bounds
4400 method in completion mode. */
4401 {
4402 static const char *expected_syms[] = {
4403 "t1_func",
4404 "t1_func1",
4405 };
4406
4407 SELF_CHECK (check_find_bounds_finds
4408 (mock_index, "t1_func", expected_syms,
4409 NULL /* per_objfile */));
4410 }
4411
4412 /* Check that the increment-last-char in the name matching algorithm
4413 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4414 {
4415 static const char *expected_syms1[] = {
4416 "\377",
4417 "\377\377123",
4418 };
4419 SELF_CHECK (check_find_bounds_finds
4420 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4421
4422 static const char *expected_syms2[] = {
4423 "\377\377123",
4424 };
4425 SELF_CHECK (check_find_bounds_finds
4426 (mock_index, "\377\377", expected_syms2,
4427 NULL /* per_objfile */));
4428 }
4429 }
4430
4431 /* Test dw2_expand_symtabs_matching_symbol. */
4432
4433 static void
4434 test_dw2_expand_symtabs_matching_symbol ()
4435 {
4436 mock_mapped_index mock_index (test_symbols);
4437
4438 /* We let all tests run until the end even if some fails, for debug
4439 convenience. */
4440 bool any_mismatch = false;
4441
4442 /* Create the expected symbols list (an initializer_list). Needed
4443 because lists have commas, and we need to pass them to CHECK,
4444 which is a macro. */
4445 #define EXPECT(...) { __VA_ARGS__ }
4446
4447 /* Wrapper for check_match that passes down the current
4448 __FILE__/__LINE__. */
4449 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4450 any_mismatch |= !check_match (__FILE__, __LINE__, \
4451 mock_index, \
4452 NAME, MATCH_TYPE, COMPLETION_MODE, \
4453 EXPECTED_LIST, NULL)
4454
4455 /* Identity checks. */
4456 for (const char *sym : test_symbols)
4457 {
4458 /* Should be able to match all existing symbols. */
4459 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4460 EXPECT (sym));
4461
4462 /* Should be able to match all existing symbols with
4463 parameters. */
4464 std::string with_params = std::string (sym) + "(int)";
4465 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4466 EXPECT (sym));
4467
4468 /* Should be able to match all existing symbols with
4469 parameters and qualifiers. */
4470 with_params = std::string (sym) + " ( int ) const";
4471 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4472 EXPECT (sym));
4473
4474 /* This should really find sym, but cp-name-parser.y doesn't
4475 know about lvalue/rvalue qualifiers yet. */
4476 with_params = std::string (sym) + " ( int ) &&";
4477 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4478 {});
4479 }
4480
4481 /* Check that the name matching algorithm for completion doesn't get
4482 confused with Latin1 'ÿ' / 0xff. */
4483 {
4484 static const char str[] = "\377";
4485 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4486 EXPECT ("\377", "\377\377123"));
4487 }
4488
4489 /* Check that the increment-last-char in the matching algorithm for
4490 completion doesn't match "t1_fund" when completing "t1_func". */
4491 {
4492 static const char str[] = "t1_func";
4493 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4494 EXPECT ("t1_func", "t1_func1"));
4495 }
4496
4497 /* Check that completion mode works at each prefix of the expected
4498 symbol name. */
4499 {
4500 static const char str[] = "function(int)";
4501 size_t len = strlen (str);
4502 std::string lookup;
4503
4504 for (size_t i = 1; i < len; i++)
4505 {
4506 lookup.assign (str, i);
4507 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4508 EXPECT ("function"));
4509 }
4510 }
4511
4512 /* While "w" is a prefix of both components, the match function
4513 should still only be called once. */
4514 {
4515 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4516 EXPECT ("w1::w2"));
4517 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4518 EXPECT ("w1::w2"));
4519 }
4520
4521 /* Same, with a "complicated" symbol. */
4522 {
4523 static const char str[] = Z_SYM_NAME;
4524 size_t len = strlen (str);
4525 std::string lookup;
4526
4527 for (size_t i = 1; i < len; i++)
4528 {
4529 lookup.assign (str, i);
4530 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4531 EXPECT (Z_SYM_NAME));
4532 }
4533 }
4534
4535 /* In FULL mode, an incomplete symbol doesn't match. */
4536 {
4537 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4538 {});
4539 }
4540
4541 /* A complete symbol with parameters matches any overload, since the
4542 index has no overload info. */
4543 {
4544 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4545 EXPECT ("std::zfunction", "std::zfunction2"));
4546 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4547 EXPECT ("std::zfunction", "std::zfunction2"));
4548 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4549 EXPECT ("std::zfunction", "std::zfunction2"));
4550 }
4551
4552 /* Check that whitespace is ignored appropriately. A symbol with a
4553 template argument list. */
4554 {
4555 static const char expected[] = "ns::foo<int>";
4556 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4557 EXPECT (expected));
4558 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4559 EXPECT (expected));
4560 }
4561
4562 /* Check that whitespace is ignored appropriately. A symbol with a
4563 template argument list that includes a pointer. */
4564 {
4565 static const char expected[] = "ns::foo<char*>";
4566 /* Try both completion and non-completion modes. */
4567 static const bool completion_mode[2] = {false, true};
4568 for (size_t i = 0; i < 2; i++)
4569 {
4570 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4571 completion_mode[i], EXPECT (expected));
4572 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4573 completion_mode[i], EXPECT (expected));
4574
4575 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4576 completion_mode[i], EXPECT (expected));
4577 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4578 completion_mode[i], EXPECT (expected));
4579 }
4580 }
4581
4582 {
4583 /* Check method qualifiers are ignored. */
4584 static const char expected[] = "ns::foo<char*>";
4585 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4586 symbol_name_match_type::FULL, true, EXPECT (expected));
4587 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4588 symbol_name_match_type::FULL, true, EXPECT (expected));
4589 CHECK_MATCH ("foo < char * > ( int ) const",
4590 symbol_name_match_type::WILD, true, EXPECT (expected));
4591 CHECK_MATCH ("foo < char * > ( int ) &&",
4592 symbol_name_match_type::WILD, true, EXPECT (expected));
4593 }
4594
4595 /* Test lookup names that don't match anything. */
4596 {
4597 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4598 {});
4599
4600 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4601 {});
4602 }
4603
4604 /* Some wild matching tests, exercising "(anonymous namespace)",
4605 which should not be confused with a parameter list. */
4606 {
4607 static const char *syms[] = {
4608 "A::B::C",
4609 "B::C",
4610 "C",
4611 "A :: B :: C ( int )",
4612 "B :: C ( int )",
4613 "C ( int )",
4614 };
4615
4616 for (const char *s : syms)
4617 {
4618 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4619 EXPECT ("(anonymous namespace)::A::B::C"));
4620 }
4621 }
4622
4623 {
4624 static const char expected[] = "ns2::tmpl<int>::foo2";
4625 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4626 EXPECT (expected));
4627 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4628 EXPECT (expected));
4629 }
4630
4631 SELF_CHECK (!any_mismatch);
4632
4633 #undef EXPECT
4634 #undef CHECK_MATCH
4635 }
4636
4637 static void
4638 run_test ()
4639 {
4640 test_mapped_index_find_name_component_bounds ();
4641 test_dw2_expand_symtabs_matching_symbol ();
4642 }
4643
4644 }} // namespace selftests::dw2_expand_symtabs_matching
4645
4646 #endif /* GDB_SELF_TEST */
4647
4648 /* If FILE_MATCHER is NULL or if PER_CU has
4649 dwarf2_per_cu_quick_data::MARK set (see
4650 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4651 EXPANSION_NOTIFY on it. */
4652
4653 static void
4654 dw2_expand_symtabs_matching_one
4655 (dwarf2_per_cu_data *per_cu,
4656 dwarf2_per_objfile *per_objfile,
4657 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4658 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4659 {
4660 if (file_matcher == NULL || per_cu->v.quick->mark)
4661 {
4662 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4663
4664 compunit_symtab *symtab
4665 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4666 gdb_assert (symtab != nullptr);
4667
4668 if (expansion_notify != NULL && symtab_was_null)
4669 expansion_notify (symtab);
4670 }
4671 }
4672
4673 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4674 matched, to expand corresponding CUs that were marked. IDX is the
4675 index of the symbol name that matched. */
4676
4677 static void
4678 dw2_expand_marked_cus
4679 (dwarf2_per_objfile *per_objfile, offset_type idx,
4680 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4681 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4682 search_domain kind)
4683 {
4684 offset_type *vec, vec_len, vec_idx;
4685 bool global_seen = false;
4686 mapped_index &index = *per_objfile->per_bfd->index_table;
4687
4688 vec = (offset_type *) (index.constant_pool
4689 + MAYBE_SWAP (index.symbol_table[idx].vec));
4690 vec_len = MAYBE_SWAP (vec[0]);
4691 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4692 {
4693 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4694 /* This value is only valid for index versions >= 7. */
4695 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4696 gdb_index_symbol_kind symbol_kind =
4697 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4698 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4699 /* Only check the symbol attributes if they're present.
4700 Indices prior to version 7 don't record them,
4701 and indices >= 7 may elide them for certain symbols
4702 (gold does this). */
4703 int attrs_valid =
4704 (index.version >= 7
4705 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4706
4707 /* Work around gold/15646. */
4708 if (attrs_valid
4709 && !is_static
4710 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4711 {
4712 if (global_seen)
4713 continue;
4714
4715 global_seen = true;
4716 }
4717
4718 /* Only check the symbol's kind if it has one. */
4719 if (attrs_valid)
4720 {
4721 switch (kind)
4722 {
4723 case VARIABLES_DOMAIN:
4724 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4725 continue;
4726 break;
4727 case FUNCTIONS_DOMAIN:
4728 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4729 continue;
4730 break;
4731 case TYPES_DOMAIN:
4732 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4733 continue;
4734 break;
4735 case MODULES_DOMAIN:
4736 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4737 continue;
4738 break;
4739 default:
4740 break;
4741 }
4742 }
4743
4744 /* Don't crash on bad data. */
4745 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4746 + per_objfile->per_bfd->all_type_units.size ()))
4747 {
4748 complaint (_(".gdb_index entry has bad CU index"
4749 " [in module %s]"), objfile_name (per_objfile->objfile));
4750 continue;
4751 }
4752
4753 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4754 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4755 expansion_notify);
4756 }
4757 }
4758
4759 /* If FILE_MATCHER is non-NULL, set all the
4760 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4761 that match FILE_MATCHER. */
4762
4763 static void
4764 dw_expand_symtabs_matching_file_matcher
4765 (dwarf2_per_objfile *per_objfile,
4766 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4767 {
4768 if (file_matcher == NULL)
4769 return;
4770
4771 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4772 htab_eq_pointer,
4773 NULL, xcalloc, xfree));
4774 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4775 htab_eq_pointer,
4776 NULL, xcalloc, xfree));
4777
4778 /* The rule is CUs specify all the files, including those used by
4779 any TU, so there's no need to scan TUs here. */
4780
4781 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4782 {
4783 QUIT;
4784
4785 per_cu->v.quick->mark = 0;
4786
4787 /* We only need to look at symtabs not already expanded. */
4788 if (per_objfile->symtab_set_p (per_cu))
4789 continue;
4790
4791 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4792 if (file_data == NULL)
4793 continue;
4794
4795 if (htab_find (visited_not_found.get (), file_data) != NULL)
4796 continue;
4797 else if (htab_find (visited_found.get (), file_data) != NULL)
4798 {
4799 per_cu->v.quick->mark = 1;
4800 continue;
4801 }
4802
4803 for (int j = 0; j < file_data->num_file_names; ++j)
4804 {
4805 const char *this_real_name;
4806
4807 if (file_matcher (file_data->file_names[j], false))
4808 {
4809 per_cu->v.quick->mark = 1;
4810 break;
4811 }
4812
4813 /* Before we invoke realpath, which can get expensive when many
4814 files are involved, do a quick comparison of the basenames. */
4815 if (!basenames_may_differ
4816 && !file_matcher (lbasename (file_data->file_names[j]),
4817 true))
4818 continue;
4819
4820 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4821 if (file_matcher (this_real_name, false))
4822 {
4823 per_cu->v.quick->mark = 1;
4824 break;
4825 }
4826 }
4827
4828 void **slot = htab_find_slot (per_cu->v.quick->mark
4829 ? visited_found.get ()
4830 : visited_not_found.get (),
4831 file_data, INSERT);
4832 *slot = file_data;
4833 }
4834 }
4835
4836 static void
4837 dw2_expand_symtabs_matching
4838 (struct objfile *objfile,
4839 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4840 const lookup_name_info *lookup_name,
4841 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4842 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4843 enum search_domain kind)
4844 {
4845 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4846
4847 /* index_table is NULL if OBJF_READNOW. */
4848 if (!per_objfile->per_bfd->index_table)
4849 return;
4850
4851 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4852
4853 if (symbol_matcher == NULL && lookup_name == NULL)
4854 {
4855 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4856 {
4857 QUIT;
4858
4859 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4860 file_matcher, expansion_notify);
4861 }
4862 return;
4863 }
4864
4865 mapped_index &index = *per_objfile->per_bfd->index_table;
4866
4867 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4868 symbol_matcher,
4869 kind, [&] (offset_type idx)
4870 {
4871 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4872 kind);
4873 return true;
4874 }, per_objfile);
4875 }
4876
4877 void
4878 dwarf2_gdb_index::expand_symtabs_matching
4879 (struct objfile *objfile,
4880 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4881 const lookup_name_info *lookup_name,
4882 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4883 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4884 enum search_domain kind)
4885 {
4886 dw2_expand_symtabs_matching (objfile, file_matcher, lookup_name,
4887 symbol_matcher, expansion_notify, kind);
4888 }
4889
4890 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4891 symtab. */
4892
4893 static struct compunit_symtab *
4894 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4895 CORE_ADDR pc)
4896 {
4897 int i;
4898
4899 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4900 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4901 return cust;
4902
4903 if (cust->includes == NULL)
4904 return NULL;
4905
4906 for (i = 0; cust->includes[i]; ++i)
4907 {
4908 struct compunit_symtab *s = cust->includes[i];
4909
4910 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4911 if (s != NULL)
4912 return s;
4913 }
4914
4915 return NULL;
4916 }
4917
4918 struct compunit_symtab *
4919 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4920 (struct objfile *objfile,
4921 struct bound_minimal_symbol msymbol,
4922 CORE_ADDR pc,
4923 struct obj_section *section,
4924 int warn_if_readin)
4925 {
4926 struct dwarf2_per_cu_data *data;
4927 struct compunit_symtab *result;
4928
4929 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4930 if (per_objfile->per_bfd->index_addrmap == nullptr)
4931 return NULL;
4932
4933 CORE_ADDR baseaddr = objfile->text_section_offset ();
4934 data = ((struct dwarf2_per_cu_data *)
4935 addrmap_find (per_objfile->per_bfd->index_addrmap,
4936 pc - baseaddr));
4937 if (!data)
4938 return NULL;
4939
4940 if (warn_if_readin && per_objfile->symtab_set_p (data))
4941 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4942 paddress (objfile->arch (), pc));
4943
4944 result = recursively_find_pc_sect_compunit_symtab
4945 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4946
4947 gdb_assert (result != NULL);
4948 return result;
4949 }
4950
4951 void
4952 dwarf2_base_index_functions::map_symbol_filenames (struct objfile *objfile,
4953 symbol_filename_ftype *fun,
4954 void *data,
4955 int need_fullname)
4956 {
4957 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4958
4959 if (!per_objfile->per_bfd->filenames_cache)
4960 {
4961 per_objfile->per_bfd->filenames_cache.emplace ();
4962
4963 htab_up visited (htab_create_alloc (10,
4964 htab_hash_pointer, htab_eq_pointer,
4965 NULL, xcalloc, xfree));
4966
4967 /* The rule is CUs specify all the files, including those used
4968 by any TU, so there's no need to scan TUs here. We can
4969 ignore file names coming from already-expanded CUs. */
4970
4971 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4972 {
4973 if (per_objfile->symtab_set_p (per_cu))
4974 {
4975 void **slot = htab_find_slot (visited.get (),
4976 per_cu->v.quick->file_names,
4977 INSERT);
4978
4979 *slot = per_cu->v.quick->file_names;
4980 }
4981 }
4982
4983 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4984 {
4985 /* We only need to look at symtabs not already expanded. */
4986 if (per_objfile->symtab_set_p (per_cu))
4987 continue;
4988
4989 quick_file_names *file_data
4990 = dw2_get_file_names (per_cu, per_objfile);
4991 if (file_data == NULL)
4992 continue;
4993
4994 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4995 if (*slot)
4996 {
4997 /* Already visited. */
4998 continue;
4999 }
5000 *slot = file_data;
5001
5002 for (int j = 0; j < file_data->num_file_names; ++j)
5003 {
5004 const char *filename = file_data->file_names[j];
5005 per_objfile->per_bfd->filenames_cache->seen (filename);
5006 }
5007 }
5008 }
5009
5010 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
5011 {
5012 gdb::unique_xmalloc_ptr<char> this_real_name;
5013
5014 if (need_fullname)
5015 this_real_name = gdb_realpath (filename);
5016 (*fun) (filename, this_real_name.get (), data);
5017 });
5018 }
5019
5020 bool
5021 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
5022 {
5023 return true;
5024 }
5025
5026 /* DWARF-5 debug_names reader. */
5027
5028 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5029 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5030
5031 /* A helper function that reads the .debug_names section in SECTION
5032 and fills in MAP. FILENAME is the name of the file containing the
5033 section; it is used for error reporting.
5034
5035 Returns true if all went well, false otherwise. */
5036
5037 static bool
5038 read_debug_names_from_section (struct objfile *objfile,
5039 const char *filename,
5040 struct dwarf2_section_info *section,
5041 mapped_debug_names &map)
5042 {
5043 if (section->empty ())
5044 return false;
5045
5046 /* Older elfutils strip versions could keep the section in the main
5047 executable while splitting it for the separate debug info file. */
5048 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5049 return false;
5050
5051 section->read (objfile);
5052
5053 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5054
5055 const gdb_byte *addr = section->buffer;
5056
5057 bfd *const abfd = section->get_bfd_owner ();
5058
5059 unsigned int bytes_read;
5060 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5061 addr += bytes_read;
5062
5063 map.dwarf5_is_dwarf64 = bytes_read != 4;
5064 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5065 if (bytes_read + length != section->size)
5066 {
5067 /* There may be multiple per-CU indices. */
5068 warning (_("Section .debug_names in %s length %s does not match "
5069 "section length %s, ignoring .debug_names."),
5070 filename, plongest (bytes_read + length),
5071 pulongest (section->size));
5072 return false;
5073 }
5074
5075 /* The version number. */
5076 uint16_t version = read_2_bytes (abfd, addr);
5077 addr += 2;
5078 if (version != 5)
5079 {
5080 warning (_("Section .debug_names in %s has unsupported version %d, "
5081 "ignoring .debug_names."),
5082 filename, version);
5083 return false;
5084 }
5085
5086 /* Padding. */
5087 uint16_t padding = read_2_bytes (abfd, addr);
5088 addr += 2;
5089 if (padding != 0)
5090 {
5091 warning (_("Section .debug_names in %s has unsupported padding %d, "
5092 "ignoring .debug_names."),
5093 filename, padding);
5094 return false;
5095 }
5096
5097 /* comp_unit_count - The number of CUs in the CU list. */
5098 map.cu_count = read_4_bytes (abfd, addr);
5099 addr += 4;
5100
5101 /* local_type_unit_count - The number of TUs in the local TU
5102 list. */
5103 map.tu_count = read_4_bytes (abfd, addr);
5104 addr += 4;
5105
5106 /* foreign_type_unit_count - The number of TUs in the foreign TU
5107 list. */
5108 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5109 addr += 4;
5110 if (foreign_tu_count != 0)
5111 {
5112 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5113 "ignoring .debug_names."),
5114 filename, static_cast<unsigned long> (foreign_tu_count));
5115 return false;
5116 }
5117
5118 /* bucket_count - The number of hash buckets in the hash lookup
5119 table. */
5120 map.bucket_count = read_4_bytes (abfd, addr);
5121 addr += 4;
5122
5123 /* name_count - The number of unique names in the index. */
5124 map.name_count = read_4_bytes (abfd, addr);
5125 addr += 4;
5126
5127 /* abbrev_table_size - The size in bytes of the abbreviations
5128 table. */
5129 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5130 addr += 4;
5131
5132 /* augmentation_string_size - The size in bytes of the augmentation
5133 string. This value is rounded up to a multiple of 4. */
5134 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5135 addr += 4;
5136 map.augmentation_is_gdb = ((augmentation_string_size
5137 == sizeof (dwarf5_augmentation))
5138 && memcmp (addr, dwarf5_augmentation,
5139 sizeof (dwarf5_augmentation)) == 0);
5140 augmentation_string_size += (-augmentation_string_size) & 3;
5141 addr += augmentation_string_size;
5142
5143 /* List of CUs */
5144 map.cu_table_reordered = addr;
5145 addr += map.cu_count * map.offset_size;
5146
5147 /* List of Local TUs */
5148 map.tu_table_reordered = addr;
5149 addr += map.tu_count * map.offset_size;
5150
5151 /* Hash Lookup Table */
5152 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5153 addr += map.bucket_count * 4;
5154 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5155 addr += map.name_count * 4;
5156
5157 /* Name Table */
5158 map.name_table_string_offs_reordered = addr;
5159 addr += map.name_count * map.offset_size;
5160 map.name_table_entry_offs_reordered = addr;
5161 addr += map.name_count * map.offset_size;
5162
5163 const gdb_byte *abbrev_table_start = addr;
5164 for (;;)
5165 {
5166 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5167 addr += bytes_read;
5168 if (index_num == 0)
5169 break;
5170
5171 const auto insertpair
5172 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5173 if (!insertpair.second)
5174 {
5175 warning (_("Section .debug_names in %s has duplicate index %s, "
5176 "ignoring .debug_names."),
5177 filename, pulongest (index_num));
5178 return false;
5179 }
5180 mapped_debug_names::index_val &indexval = insertpair.first->second;
5181 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5182 addr += bytes_read;
5183
5184 for (;;)
5185 {
5186 mapped_debug_names::index_val::attr attr;
5187 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5188 addr += bytes_read;
5189 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5190 addr += bytes_read;
5191 if (attr.form == DW_FORM_implicit_const)
5192 {
5193 attr.implicit_const = read_signed_leb128 (abfd, addr,
5194 &bytes_read);
5195 addr += bytes_read;
5196 }
5197 if (attr.dw_idx == 0 && attr.form == 0)
5198 break;
5199 indexval.attr_vec.push_back (std::move (attr));
5200 }
5201 }
5202 if (addr != abbrev_table_start + abbrev_table_size)
5203 {
5204 warning (_("Section .debug_names in %s has abbreviation_table "
5205 "of size %s vs. written as %u, ignoring .debug_names."),
5206 filename, plongest (addr - abbrev_table_start),
5207 abbrev_table_size);
5208 return false;
5209 }
5210 map.entry_pool = addr;
5211
5212 return true;
5213 }
5214
5215 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5216 list. */
5217
5218 static void
5219 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5220 const mapped_debug_names &map,
5221 dwarf2_section_info &section,
5222 bool is_dwz)
5223 {
5224 if (!map.augmentation_is_gdb)
5225 {
5226 for (uint32_t i = 0; i < map.cu_count; ++i)
5227 {
5228 sect_offset sect_off
5229 = (sect_offset) (extract_unsigned_integer
5230 (map.cu_table_reordered + i * map.offset_size,
5231 map.offset_size,
5232 map.dwarf5_byte_order));
5233 /* We don't know the length of the CU, because the CU list in a
5234 .debug_names index can be incomplete, so we can't use the start
5235 of the next CU as end of this CU. We create the CUs here with
5236 length 0, and in cutu_reader::cutu_reader we'll fill in the
5237 actual length. */
5238 dwarf2_per_cu_data *per_cu
5239 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5240 sect_off, 0);
5241 per_bfd->all_comp_units.push_back (per_cu);
5242 }
5243 return;
5244 }
5245
5246 sect_offset sect_off_prev;
5247 for (uint32_t i = 0; i <= map.cu_count; ++i)
5248 {
5249 sect_offset sect_off_next;
5250 if (i < map.cu_count)
5251 {
5252 sect_off_next
5253 = (sect_offset) (extract_unsigned_integer
5254 (map.cu_table_reordered + i * map.offset_size,
5255 map.offset_size,
5256 map.dwarf5_byte_order));
5257 }
5258 else
5259 sect_off_next = (sect_offset) section.size;
5260 if (i >= 1)
5261 {
5262 const ULONGEST length = sect_off_next - sect_off_prev;
5263 dwarf2_per_cu_data *per_cu
5264 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5265 sect_off_prev, length);
5266 per_bfd->all_comp_units.push_back (per_cu);
5267 }
5268 sect_off_prev = sect_off_next;
5269 }
5270 }
5271
5272 /* Read the CU list from the mapped index, and use it to create all
5273 the CU objects for this dwarf2_per_objfile. */
5274
5275 static void
5276 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5277 const mapped_debug_names &map,
5278 const mapped_debug_names &dwz_map)
5279 {
5280 gdb_assert (per_bfd->all_comp_units.empty ());
5281 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5282
5283 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5284 false /* is_dwz */);
5285
5286 if (dwz_map.cu_count == 0)
5287 return;
5288
5289 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5290 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5291 true /* is_dwz */);
5292 }
5293
5294 /* Read .debug_names. If everything went ok, initialize the "quick"
5295 elements of all the CUs and return true. Otherwise, return false. */
5296
5297 static bool
5298 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5299 {
5300 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5301 mapped_debug_names dwz_map;
5302 struct objfile *objfile = per_objfile->objfile;
5303 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5304
5305 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5306 &per_objfile->per_bfd->debug_names, *map))
5307 return false;
5308
5309 /* Don't use the index if it's empty. */
5310 if (map->name_count == 0)
5311 return false;
5312
5313 /* If there is a .dwz file, read it so we can get its CU list as
5314 well. */
5315 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5316 if (dwz != NULL)
5317 {
5318 if (!read_debug_names_from_section (objfile,
5319 bfd_get_filename (dwz->dwz_bfd.get ()),
5320 &dwz->debug_names, dwz_map))
5321 {
5322 warning (_("could not read '.debug_names' section from %s; skipping"),
5323 bfd_get_filename (dwz->dwz_bfd.get ()));
5324 return false;
5325 }
5326 }
5327
5328 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5329
5330 if (map->tu_count != 0)
5331 {
5332 /* We can only handle a single .debug_types when we have an
5333 index. */
5334 if (per_bfd->types.size () != 1)
5335 return false;
5336
5337 dwarf2_section_info *section = &per_bfd->types[0];
5338
5339 create_signatured_type_table_from_debug_names
5340 (per_objfile, *map, section, &per_bfd->abbrev);
5341 }
5342
5343 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5344
5345 per_bfd->debug_names_table = std::move (map);
5346 per_bfd->using_index = 1;
5347 per_bfd->quick_file_names_table =
5348 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5349
5350 return true;
5351 }
5352
5353 /* Type used to manage iterating over all CUs looking for a symbol for
5354 .debug_names. */
5355
5356 class dw2_debug_names_iterator
5357 {
5358 public:
5359 dw2_debug_names_iterator (const mapped_debug_names &map,
5360 gdb::optional<block_enum> block_index,
5361 domain_enum domain,
5362 const char *name, dwarf2_per_objfile *per_objfile)
5363 : m_map (map), m_block_index (block_index), m_domain (domain),
5364 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5365 m_per_objfile (per_objfile)
5366 {}
5367
5368 dw2_debug_names_iterator (const mapped_debug_names &map,
5369 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5370 : m_map (map),
5371 m_search (search),
5372 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5373 m_per_objfile (per_objfile)
5374 {}
5375
5376 dw2_debug_names_iterator (const mapped_debug_names &map,
5377 block_enum block_index, domain_enum domain,
5378 uint32_t namei, dwarf2_per_objfile *per_objfile)
5379 : m_map (map), m_block_index (block_index), m_domain (domain),
5380 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5381 m_per_objfile (per_objfile)
5382 {}
5383
5384 /* Return the next matching CU or NULL if there are no more. */
5385 dwarf2_per_cu_data *next ();
5386
5387 private:
5388 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5389 const char *name,
5390 dwarf2_per_objfile *per_objfile);
5391 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5392 uint32_t namei,
5393 dwarf2_per_objfile *per_objfile);
5394
5395 /* The internalized form of .debug_names. */
5396 const mapped_debug_names &m_map;
5397
5398 /* If set, only look for symbols that match that block. Valid values are
5399 GLOBAL_BLOCK and STATIC_BLOCK. */
5400 const gdb::optional<block_enum> m_block_index;
5401
5402 /* The kind of symbol we're looking for. */
5403 const domain_enum m_domain = UNDEF_DOMAIN;
5404 const search_domain m_search = ALL_DOMAIN;
5405
5406 /* The list of CUs from the index entry of the symbol, or NULL if
5407 not found. */
5408 const gdb_byte *m_addr;
5409
5410 dwarf2_per_objfile *m_per_objfile;
5411 };
5412
5413 const char *
5414 mapped_debug_names::namei_to_name
5415 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5416 {
5417 const ULONGEST namei_string_offs
5418 = extract_unsigned_integer ((name_table_string_offs_reordered
5419 + namei * offset_size),
5420 offset_size,
5421 dwarf5_byte_order);
5422 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5423 }
5424
5425 /* Find a slot in .debug_names for the object named NAME. If NAME is
5426 found, return pointer to its pool data. If NAME cannot be found,
5427 return NULL. */
5428
5429 const gdb_byte *
5430 dw2_debug_names_iterator::find_vec_in_debug_names
5431 (const mapped_debug_names &map, const char *name,
5432 dwarf2_per_objfile *per_objfile)
5433 {
5434 int (*cmp) (const char *, const char *);
5435
5436 gdb::unique_xmalloc_ptr<char> without_params;
5437 if (current_language->la_language == language_cplus
5438 || current_language->la_language == language_fortran
5439 || current_language->la_language == language_d)
5440 {
5441 /* NAME is already canonical. Drop any qualifiers as
5442 .debug_names does not contain any. */
5443
5444 if (strchr (name, '(') != NULL)
5445 {
5446 without_params = cp_remove_params (name);
5447 if (without_params != NULL)
5448 name = without_params.get ();
5449 }
5450 }
5451
5452 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5453
5454 const uint32_t full_hash = dwarf5_djb_hash (name);
5455 uint32_t namei
5456 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5457 (map.bucket_table_reordered
5458 + (full_hash % map.bucket_count)), 4,
5459 map.dwarf5_byte_order);
5460 if (namei == 0)
5461 return NULL;
5462 --namei;
5463 if (namei >= map.name_count)
5464 {
5465 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5466 "[in module %s]"),
5467 namei, map.name_count,
5468 objfile_name (per_objfile->objfile));
5469 return NULL;
5470 }
5471
5472 for (;;)
5473 {
5474 const uint32_t namei_full_hash
5475 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5476 (map.hash_table_reordered + namei), 4,
5477 map.dwarf5_byte_order);
5478 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5479 return NULL;
5480
5481 if (full_hash == namei_full_hash)
5482 {
5483 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5484
5485 #if 0 /* An expensive sanity check. */
5486 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5487 {
5488 complaint (_("Wrong .debug_names hash for string at index %u "
5489 "[in module %s]"),
5490 namei, objfile_name (dwarf2_per_objfile->objfile));
5491 return NULL;
5492 }
5493 #endif
5494
5495 if (cmp (namei_string, name) == 0)
5496 {
5497 const ULONGEST namei_entry_offs
5498 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5499 + namei * map.offset_size),
5500 map.offset_size, map.dwarf5_byte_order);
5501 return map.entry_pool + namei_entry_offs;
5502 }
5503 }
5504
5505 ++namei;
5506 if (namei >= map.name_count)
5507 return NULL;
5508 }
5509 }
5510
5511 const gdb_byte *
5512 dw2_debug_names_iterator::find_vec_in_debug_names
5513 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5514 {
5515 if (namei >= map.name_count)
5516 {
5517 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5518 "[in module %s]"),
5519 namei, map.name_count,
5520 objfile_name (per_objfile->objfile));
5521 return NULL;
5522 }
5523
5524 const ULONGEST namei_entry_offs
5525 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5526 + namei * map.offset_size),
5527 map.offset_size, map.dwarf5_byte_order);
5528 return map.entry_pool + namei_entry_offs;
5529 }
5530
5531 /* See dw2_debug_names_iterator. */
5532
5533 dwarf2_per_cu_data *
5534 dw2_debug_names_iterator::next ()
5535 {
5536 if (m_addr == NULL)
5537 return NULL;
5538
5539 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5540 struct objfile *objfile = m_per_objfile->objfile;
5541 bfd *const abfd = objfile->obfd;
5542
5543 again:
5544
5545 unsigned int bytes_read;
5546 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5547 m_addr += bytes_read;
5548 if (abbrev == 0)
5549 return NULL;
5550
5551 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5552 if (indexval_it == m_map.abbrev_map.cend ())
5553 {
5554 complaint (_("Wrong .debug_names undefined abbrev code %s "
5555 "[in module %s]"),
5556 pulongest (abbrev), objfile_name (objfile));
5557 return NULL;
5558 }
5559 const mapped_debug_names::index_val &indexval = indexval_it->second;
5560 enum class symbol_linkage {
5561 unknown,
5562 static_,
5563 extern_,
5564 } symbol_linkage_ = symbol_linkage::unknown;
5565 dwarf2_per_cu_data *per_cu = NULL;
5566 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5567 {
5568 ULONGEST ull;
5569 switch (attr.form)
5570 {
5571 case DW_FORM_implicit_const:
5572 ull = attr.implicit_const;
5573 break;
5574 case DW_FORM_flag_present:
5575 ull = 1;
5576 break;
5577 case DW_FORM_udata:
5578 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5579 m_addr += bytes_read;
5580 break;
5581 case DW_FORM_ref4:
5582 ull = read_4_bytes (abfd, m_addr);
5583 m_addr += 4;
5584 break;
5585 case DW_FORM_ref8:
5586 ull = read_8_bytes (abfd, m_addr);
5587 m_addr += 8;
5588 break;
5589 case DW_FORM_ref_sig8:
5590 ull = read_8_bytes (abfd, m_addr);
5591 m_addr += 8;
5592 break;
5593 default:
5594 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5595 dwarf_form_name (attr.form),
5596 objfile_name (objfile));
5597 return NULL;
5598 }
5599 switch (attr.dw_idx)
5600 {
5601 case DW_IDX_compile_unit:
5602 /* Don't crash on bad data. */
5603 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5604 {
5605 complaint (_(".debug_names entry has bad CU index %s"
5606 " [in module %s]"),
5607 pulongest (ull),
5608 objfile_name (objfile));
5609 continue;
5610 }
5611 per_cu = per_bfd->get_cutu (ull);
5612 break;
5613 case DW_IDX_type_unit:
5614 /* Don't crash on bad data. */
5615 if (ull >= per_bfd->all_type_units.size ())
5616 {
5617 complaint (_(".debug_names entry has bad TU index %s"
5618 " [in module %s]"),
5619 pulongest (ull),
5620 objfile_name (objfile));
5621 continue;
5622 }
5623 per_cu = &per_bfd->get_tu (ull)->per_cu;
5624 break;
5625 case DW_IDX_die_offset:
5626 /* In a per-CU index (as opposed to a per-module index), index
5627 entries without CU attribute implicitly refer to the single CU. */
5628 if (per_cu == NULL)
5629 per_cu = per_bfd->get_cu (0);
5630 break;
5631 case DW_IDX_GNU_internal:
5632 if (!m_map.augmentation_is_gdb)
5633 break;
5634 symbol_linkage_ = symbol_linkage::static_;
5635 break;
5636 case DW_IDX_GNU_external:
5637 if (!m_map.augmentation_is_gdb)
5638 break;
5639 symbol_linkage_ = symbol_linkage::extern_;
5640 break;
5641 }
5642 }
5643
5644 /* Skip if already read in. */
5645 if (m_per_objfile->symtab_set_p (per_cu))
5646 goto again;
5647
5648 /* Check static vs global. */
5649 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5650 {
5651 const bool want_static = *m_block_index == STATIC_BLOCK;
5652 const bool symbol_is_static =
5653 symbol_linkage_ == symbol_linkage::static_;
5654 if (want_static != symbol_is_static)
5655 goto again;
5656 }
5657
5658 /* Match dw2_symtab_iter_next, symbol_kind
5659 and debug_names::psymbol_tag. */
5660 switch (m_domain)
5661 {
5662 case VAR_DOMAIN:
5663 switch (indexval.dwarf_tag)
5664 {
5665 case DW_TAG_variable:
5666 case DW_TAG_subprogram:
5667 /* Some types are also in VAR_DOMAIN. */
5668 case DW_TAG_typedef:
5669 case DW_TAG_structure_type:
5670 break;
5671 default:
5672 goto again;
5673 }
5674 break;
5675 case STRUCT_DOMAIN:
5676 switch (indexval.dwarf_tag)
5677 {
5678 case DW_TAG_typedef:
5679 case DW_TAG_structure_type:
5680 break;
5681 default:
5682 goto again;
5683 }
5684 break;
5685 case LABEL_DOMAIN:
5686 switch (indexval.dwarf_tag)
5687 {
5688 case 0:
5689 case DW_TAG_variable:
5690 break;
5691 default:
5692 goto again;
5693 }
5694 break;
5695 case MODULE_DOMAIN:
5696 switch (indexval.dwarf_tag)
5697 {
5698 case DW_TAG_module:
5699 break;
5700 default:
5701 goto again;
5702 }
5703 break;
5704 default:
5705 break;
5706 }
5707
5708 /* Match dw2_expand_symtabs_matching, symbol_kind and
5709 debug_names::psymbol_tag. */
5710 switch (m_search)
5711 {
5712 case VARIABLES_DOMAIN:
5713 switch (indexval.dwarf_tag)
5714 {
5715 case DW_TAG_variable:
5716 break;
5717 default:
5718 goto again;
5719 }
5720 break;
5721 case FUNCTIONS_DOMAIN:
5722 switch (indexval.dwarf_tag)
5723 {
5724 case DW_TAG_subprogram:
5725 break;
5726 default:
5727 goto again;
5728 }
5729 break;
5730 case TYPES_DOMAIN:
5731 switch (indexval.dwarf_tag)
5732 {
5733 case DW_TAG_typedef:
5734 case DW_TAG_structure_type:
5735 break;
5736 default:
5737 goto again;
5738 }
5739 break;
5740 case MODULES_DOMAIN:
5741 switch (indexval.dwarf_tag)
5742 {
5743 case DW_TAG_module:
5744 break;
5745 default:
5746 goto again;
5747 }
5748 default:
5749 break;
5750 }
5751
5752 return per_cu;
5753 }
5754
5755 struct compunit_symtab *
5756 dwarf2_debug_names_index::lookup_symbol
5757 (struct objfile *objfile, block_enum block_index,
5758 const char *name, domain_enum domain)
5759 {
5760 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5761
5762 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5763 if (!mapp)
5764 {
5765 /* index is NULL if OBJF_READNOW. */
5766 return NULL;
5767 }
5768 const auto &map = *mapp;
5769
5770 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5771
5772 struct compunit_symtab *stab_best = NULL;
5773 struct dwarf2_per_cu_data *per_cu;
5774 while ((per_cu = iter.next ()) != NULL)
5775 {
5776 struct symbol *sym, *with_opaque = NULL;
5777 compunit_symtab *stab
5778 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5779 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5780 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5781
5782 sym = block_find_symbol (block, name, domain,
5783 block_find_non_opaque_type_preferred,
5784 &with_opaque);
5785
5786 /* Some caution must be observed with overloaded functions and
5787 methods, since the index will not contain any overload
5788 information (but NAME might contain it). */
5789
5790 if (sym != NULL
5791 && strcmp_iw (sym->search_name (), name) == 0)
5792 return stab;
5793 if (with_opaque != NULL
5794 && strcmp_iw (with_opaque->search_name (), name) == 0)
5795 stab_best = stab;
5796
5797 /* Keep looking through other CUs. */
5798 }
5799
5800 return stab_best;
5801 }
5802
5803 /* This dumps minimal information about .debug_names. It is called
5804 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5805 uses this to verify that .debug_names has been loaded. */
5806
5807 void
5808 dwarf2_debug_names_index::dump (struct objfile *objfile)
5809 {
5810 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5811
5812 gdb_assert (per_objfile->per_bfd->using_index);
5813 printf_filtered (".debug_names:");
5814 if (per_objfile->per_bfd->debug_names_table)
5815 printf_filtered (" exists\n");
5816 else
5817 printf_filtered (" faked for \"readnow\"\n");
5818 printf_filtered ("\n");
5819 }
5820
5821 void
5822 dwarf2_debug_names_index::expand_symtabs_for_function
5823 (struct objfile *objfile, const char *func_name)
5824 {
5825 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5826
5827 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5828 if (per_objfile->per_bfd->debug_names_table)
5829 {
5830 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5831
5832 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5833 per_objfile);
5834
5835 struct dwarf2_per_cu_data *per_cu;
5836 while ((per_cu = iter.next ()) != NULL)
5837 dw2_instantiate_symtab (per_cu, per_objfile, false);
5838 }
5839 }
5840
5841 void
5842 dwarf2_debug_names_index::map_matching_symbols
5843 (struct objfile *objfile,
5844 const lookup_name_info &name, domain_enum domain,
5845 int global,
5846 gdb::function_view<symbol_found_callback_ftype> callback,
5847 symbol_compare_ftype *ordered_compare)
5848 {
5849 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5850
5851 /* debug_names_table is NULL if OBJF_READNOW. */
5852 if (!per_objfile->per_bfd->debug_names_table)
5853 return;
5854
5855 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5856 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5857
5858 const char *match_name = name.ada ().lookup_name ().c_str ();
5859 auto matcher = [&] (const char *symname)
5860 {
5861 if (ordered_compare == nullptr)
5862 return true;
5863 return ordered_compare (symname, match_name) == 0;
5864 };
5865
5866 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5867 [&] (offset_type namei)
5868 {
5869 /* The name was matched, now expand corresponding CUs that were
5870 marked. */
5871 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5872 per_objfile);
5873
5874 struct dwarf2_per_cu_data *per_cu;
5875 while ((per_cu = iter.next ()) != NULL)
5876 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5877 nullptr);
5878 return true;
5879 }, per_objfile);
5880
5881 /* It's a shame we couldn't do this inside the
5882 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5883 that have already been expanded. Instead, this loop matches what
5884 the psymtab code does. */
5885 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5886 {
5887 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5888 if (symtab != nullptr)
5889 {
5890 const struct block *block
5891 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5892 if (!iterate_over_symbols_terminated (block, name,
5893 domain, callback))
5894 break;
5895 }
5896 }
5897 }
5898
5899 void
5900 dwarf2_debug_names_index::expand_symtabs_matching
5901 (struct objfile *objfile,
5902 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5903 const lookup_name_info *lookup_name,
5904 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5905 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5906 enum search_domain kind)
5907 {
5908 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5909
5910 /* debug_names_table is NULL if OBJF_READNOW. */
5911 if (!per_objfile->per_bfd->debug_names_table)
5912 return;
5913
5914 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5915
5916 if (symbol_matcher == NULL && lookup_name == NULL)
5917 {
5918 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5919 {
5920 QUIT;
5921
5922 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5923 expansion_notify);
5924 }
5925 return;
5926 }
5927
5928 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5929
5930 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5931 symbol_matcher,
5932 kind, [&] (offset_type namei)
5933 {
5934 /* The name was matched, now expand corresponding CUs that were
5935 marked. */
5936 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5937
5938 struct dwarf2_per_cu_data *per_cu;
5939 while ((per_cu = iter.next ()) != NULL)
5940 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5941 expansion_notify);
5942 return true;
5943 }, per_objfile);
5944 }
5945
5946 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5947 to either a dwarf2_per_bfd or dwz_file object. */
5948
5949 template <typename T>
5950 static gdb::array_view<const gdb_byte>
5951 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5952 {
5953 dwarf2_section_info *section = &section_owner->gdb_index;
5954
5955 if (section->empty ())
5956 return {};
5957
5958 /* Older elfutils strip versions could keep the section in the main
5959 executable while splitting it for the separate debug info file. */
5960 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5961 return {};
5962
5963 section->read (obj);
5964
5965 /* dwarf2_section_info::size is a bfd_size_type, while
5966 gdb::array_view works with size_t. On 32-bit hosts, with
5967 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5968 is 32-bit. So we need an explicit narrowing conversion here.
5969 This is fine, because it's impossible to allocate or mmap an
5970 array/buffer larger than what size_t can represent. */
5971 return gdb::make_array_view (section->buffer, section->size);
5972 }
5973
5974 /* Lookup the index cache for the contents of the index associated to
5975 DWARF2_OBJ. */
5976
5977 static gdb::array_view<const gdb_byte>
5978 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5979 {
5980 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5981 if (build_id == nullptr)
5982 return {};
5983
5984 return global_index_cache.lookup_gdb_index (build_id,
5985 &dwarf2_per_bfd->index_cache_res);
5986 }
5987
5988 /* Same as the above, but for DWZ. */
5989
5990 static gdb::array_view<const gdb_byte>
5991 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5992 {
5993 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5994 if (build_id == nullptr)
5995 return {};
5996
5997 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5998 }
5999
6000 /* See symfile.h. */
6001
6002 bool
6003 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6004 {
6005 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6006 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6007
6008 dwarf_read_debug_printf ("called");
6009
6010 /* If we're about to read full symbols, don't bother with the
6011 indices. In this case we also don't care if some other debug
6012 format is making psymtabs, because they are all about to be
6013 expanded anyway. */
6014 if ((objfile->flags & OBJF_READNOW))
6015 {
6016 dwarf_read_debug_printf ("readnow requested");
6017
6018 /* When using READNOW, the using_index flag (set below) indicates that
6019 PER_BFD was already initialized, when we loaded some other objfile. */
6020 if (per_bfd->using_index)
6021 {
6022 dwarf_read_debug_printf ("using_index already set");
6023 *index_kind = dw_index_kind::GDB_INDEX;
6024 per_objfile->resize_symtabs ();
6025 return true;
6026 }
6027
6028 per_bfd->using_index = 1;
6029 create_all_comp_units (per_objfile);
6030 create_all_type_units (per_objfile);
6031 per_bfd->quick_file_names_table
6032 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6033 per_objfile->resize_symtabs ();
6034
6035 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6036 + per_bfd->all_type_units.size ()); ++i)
6037 {
6038 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6039
6040 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6041 struct dwarf2_per_cu_quick_data);
6042 }
6043
6044 /* Return 1 so that gdb sees the "quick" functions. However,
6045 these functions will be no-ops because we will have expanded
6046 all symtabs. */
6047 *index_kind = dw_index_kind::GDB_INDEX;
6048 return true;
6049 }
6050
6051 /* Was a debug names index already read when we processed an objfile sharing
6052 PER_BFD? */
6053 if (per_bfd->debug_names_table != nullptr)
6054 {
6055 dwarf_read_debug_printf ("re-using shared debug names table");
6056 *index_kind = dw_index_kind::DEBUG_NAMES;
6057 per_objfile->resize_symtabs ();
6058 return true;
6059 }
6060
6061 /* Was a GDB index already read when we processed an objfile sharing
6062 PER_BFD? */
6063 if (per_bfd->index_table != nullptr)
6064 {
6065 dwarf_read_debug_printf ("re-using shared index table");
6066 *index_kind = dw_index_kind::GDB_INDEX;
6067 per_objfile->resize_symtabs ();
6068 return true;
6069 }
6070
6071 /* There might already be partial symtabs built for this BFD. This happens
6072 when loading the same binary twice with the index-cache enabled. If so,
6073 don't try to read an index. The objfile / per_objfile initialization will
6074 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6075 code path. */
6076 if (per_bfd->partial_symtabs != nullptr)
6077 {
6078 dwarf_read_debug_printf ("re-using shared partial symtabs");
6079 return false;
6080 }
6081
6082 if (dwarf2_read_debug_names (per_objfile))
6083 {
6084 dwarf_read_debug_printf ("found debug names");
6085 *index_kind = dw_index_kind::DEBUG_NAMES;
6086 per_objfile->resize_symtabs ();
6087 return true;
6088 }
6089
6090 if (dwarf2_read_gdb_index (per_objfile,
6091 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6092 get_gdb_index_contents_from_section<dwz_file>))
6093 {
6094 dwarf_read_debug_printf ("found gdb index from file");
6095 *index_kind = dw_index_kind::GDB_INDEX;
6096 per_objfile->resize_symtabs ();
6097 return true;
6098 }
6099
6100 /* ... otherwise, try to find the index in the index cache. */
6101 if (dwarf2_read_gdb_index (per_objfile,
6102 get_gdb_index_contents_from_cache,
6103 get_gdb_index_contents_from_cache_dwz))
6104 {
6105 dwarf_read_debug_printf ("found gdb index from cache");
6106 global_index_cache.hit ();
6107 *index_kind = dw_index_kind::GDB_INDEX;
6108 per_objfile->resize_symtabs ();
6109 return true;
6110 }
6111
6112 global_index_cache.miss ();
6113 return false;
6114 }
6115
6116 \f
6117
6118 /* Build a partial symbol table. */
6119
6120 void
6121 dwarf2_build_psymtabs (struct objfile *objfile)
6122 {
6123 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6124 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6125
6126 if (per_bfd->partial_symtabs != nullptr)
6127 {
6128 /* Partial symbols were already read, so now we can simply
6129 attach them. */
6130 objfile->partial_symtabs = per_bfd->partial_symtabs;
6131 /* This is a temporary hack to ensure that the objfile and 'qf'
6132 psymtabs are identical. */
6133 psymbol_functions *psf
6134 = dynamic_cast<psymbol_functions *> (objfile->qf.get ());
6135 gdb_assert (psf != nullptr);
6136 psf->set_partial_symtabs (per_bfd->partial_symtabs);
6137 per_objfile->resize_symtabs ();
6138 return;
6139 }
6140
6141 /* Set the local reference to partial symtabs, so that we don't try
6142 to read them again if reading another objfile with the same BFD.
6143 If we can't in fact share, this won't make a difference anyway as
6144 the dwarf2_per_bfd object won't be shared. */
6145 per_bfd->partial_symtabs = objfile->partial_symtabs;
6146
6147 try
6148 {
6149 /* This isn't really ideal: all the data we allocate on the
6150 objfile's obstack is still uselessly kept around. However,
6151 freeing it seems unsafe. */
6152 psymtab_discarder psymtabs (objfile->partial_symtabs.get ());
6153 dwarf2_build_psymtabs_hard (per_objfile);
6154 psymtabs.keep ();
6155
6156 per_objfile->resize_symtabs ();
6157
6158 /* (maybe) store an index in the cache. */
6159 global_index_cache.store (per_objfile);
6160 }
6161 catch (const gdb_exception_error &except)
6162 {
6163 exception_print (gdb_stderr, except);
6164 }
6165 }
6166
6167 /* Find the base address of the compilation unit for range lists and
6168 location lists. It will normally be specified by DW_AT_low_pc.
6169 In DWARF-3 draft 4, the base address could be overridden by
6170 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6171 compilation units with discontinuous ranges. */
6172
6173 static void
6174 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6175 {
6176 struct attribute *attr;
6177
6178 cu->base_address.reset ();
6179
6180 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6181 if (attr != nullptr)
6182 cu->base_address = attr->as_address ();
6183 else
6184 {
6185 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6186 if (attr != nullptr)
6187 cu->base_address = attr->as_address ();
6188 }
6189 }
6190
6191 /* Helper function that returns the proper abbrev section for
6192 THIS_CU. */
6193
6194 static struct dwarf2_section_info *
6195 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6196 {
6197 struct dwarf2_section_info *abbrev;
6198 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6199
6200 if (this_cu->is_dwz)
6201 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
6202 else
6203 abbrev = &per_bfd->abbrev;
6204
6205 return abbrev;
6206 }
6207
6208 /* Fetch the abbreviation table offset from a comp or type unit header. */
6209
6210 static sect_offset
6211 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6212 struct dwarf2_section_info *section,
6213 sect_offset sect_off)
6214 {
6215 bfd *abfd = section->get_bfd_owner ();
6216 const gdb_byte *info_ptr;
6217 unsigned int initial_length_size, offset_size;
6218 uint16_t version;
6219
6220 section->read (per_objfile->objfile);
6221 info_ptr = section->buffer + to_underlying (sect_off);
6222 read_initial_length (abfd, info_ptr, &initial_length_size);
6223 offset_size = initial_length_size == 4 ? 4 : 8;
6224 info_ptr += initial_length_size;
6225
6226 version = read_2_bytes (abfd, info_ptr);
6227 info_ptr += 2;
6228 if (version >= 5)
6229 {
6230 /* Skip unit type and address size. */
6231 info_ptr += 2;
6232 }
6233
6234 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6235 }
6236
6237 /* A partial symtab that is used only for include files. */
6238 struct dwarf2_include_psymtab : public partial_symtab
6239 {
6240 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6241 : partial_symtab (filename, objfile)
6242 {
6243 }
6244
6245 void read_symtab (struct objfile *objfile) override
6246 {
6247 /* It's an include file, no symbols to read for it.
6248 Everything is in the includer symtab. */
6249
6250 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6251 expansion of the includer psymtab. We use the dependencies[0] field to
6252 model the includer. But if we go the regular route of calling
6253 expand_psymtab here, and having expand_psymtab call expand_dependencies
6254 to expand the includer, we'll only use expand_psymtab on the includer
6255 (making it a non-toplevel psymtab), while if we expand the includer via
6256 another path, we'll use read_symtab (making it a toplevel psymtab).
6257 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6258 psymtab, and trigger read_symtab on the includer here directly. */
6259 includer ()->read_symtab (objfile);
6260 }
6261
6262 void expand_psymtab (struct objfile *objfile) override
6263 {
6264 /* This is not called by read_symtab, and should not be called by any
6265 expand_dependencies. */
6266 gdb_assert (false);
6267 }
6268
6269 bool readin_p (struct objfile *objfile) const override
6270 {
6271 return includer ()->readin_p (objfile);
6272 }
6273
6274 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6275 {
6276 return nullptr;
6277 }
6278
6279 private:
6280 partial_symtab *includer () const
6281 {
6282 /* An include psymtab has exactly one dependency: the psymtab that
6283 includes it. */
6284 gdb_assert (this->number_of_dependencies == 1);
6285 return this->dependencies[0];
6286 }
6287 };
6288
6289 /* Allocate a new partial symtab for file named NAME and mark this new
6290 partial symtab as being an include of PST. */
6291
6292 static void
6293 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
6294 const char *name, dwarf2_psymtab *pst,
6295 struct objfile *objfile)
6296 {
6297 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6298
6299 if (!IS_ABSOLUTE_PATH (subpst->filename))
6300 subpst->dirname = pst->dirname;
6301
6302 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
6303 subpst->dependencies[0] = pst;
6304 subpst->number_of_dependencies = 1;
6305 }
6306
6307 /* Read the Line Number Program data and extract the list of files
6308 included by the source file represented by PST. Build an include
6309 partial symtab for each of these included files. */
6310
6311 static void
6312 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6313 struct die_info *die,
6314 dwarf2_psymtab *pst)
6315 {
6316 line_header_up lh;
6317 struct attribute *attr;
6318
6319 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6320 if (attr != nullptr && attr->form_is_unsigned ())
6321 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6322 if (lh == NULL)
6323 return; /* No linetable, so no includes. */
6324
6325 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6326 that we pass in the raw text_low here; that is ok because we're
6327 only decoding the line table to make include partial symtabs, and
6328 so the addresses aren't really used. */
6329 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6330 pst->raw_text_low (), 1);
6331 }
6332
6333 static hashval_t
6334 hash_signatured_type (const void *item)
6335 {
6336 const struct signatured_type *sig_type
6337 = (const struct signatured_type *) item;
6338
6339 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6340 return sig_type->signature;
6341 }
6342
6343 static int
6344 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6345 {
6346 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6347 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6348
6349 return lhs->signature == rhs->signature;
6350 }
6351
6352 /* Allocate a hash table for signatured types. */
6353
6354 static htab_up
6355 allocate_signatured_type_table ()
6356 {
6357 return htab_up (htab_create_alloc (41,
6358 hash_signatured_type,
6359 eq_signatured_type,
6360 NULL, xcalloc, xfree));
6361 }
6362
6363 /* A helper function to add a signatured type CU to a table. */
6364
6365 static int
6366 add_signatured_type_cu_to_table (void **slot, void *datum)
6367 {
6368 struct signatured_type *sigt = (struct signatured_type *) *slot;
6369 std::vector<signatured_type *> *all_type_units
6370 = (std::vector<signatured_type *> *) datum;
6371
6372 all_type_units->push_back (sigt);
6373
6374 return 1;
6375 }
6376
6377 /* A helper for create_debug_types_hash_table. Read types from SECTION
6378 and fill them into TYPES_HTAB. It will process only type units,
6379 therefore DW_UT_type. */
6380
6381 static void
6382 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6383 struct dwo_file *dwo_file,
6384 dwarf2_section_info *section, htab_up &types_htab,
6385 rcuh_kind section_kind)
6386 {
6387 struct objfile *objfile = per_objfile->objfile;
6388 struct dwarf2_section_info *abbrev_section;
6389 bfd *abfd;
6390 const gdb_byte *info_ptr, *end_ptr;
6391
6392 abbrev_section = (dwo_file != NULL
6393 ? &dwo_file->sections.abbrev
6394 : &per_objfile->per_bfd->abbrev);
6395
6396 dwarf_read_debug_printf ("Reading %s for %s",
6397 section->get_name (),
6398 abbrev_section->get_file_name ());
6399
6400 section->read (objfile);
6401 info_ptr = section->buffer;
6402
6403 if (info_ptr == NULL)
6404 return;
6405
6406 /* We can't set abfd until now because the section may be empty or
6407 not present, in which case the bfd is unknown. */
6408 abfd = section->get_bfd_owner ();
6409
6410 /* We don't use cutu_reader here because we don't need to read
6411 any dies: the signature is in the header. */
6412
6413 end_ptr = info_ptr + section->size;
6414 while (info_ptr < end_ptr)
6415 {
6416 struct signatured_type *sig_type;
6417 struct dwo_unit *dwo_tu;
6418 void **slot;
6419 const gdb_byte *ptr = info_ptr;
6420 struct comp_unit_head header;
6421 unsigned int length;
6422
6423 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6424
6425 /* Initialize it due to a false compiler warning. */
6426 header.signature = -1;
6427 header.type_cu_offset_in_tu = (cu_offset) -1;
6428
6429 /* We need to read the type's signature in order to build the hash
6430 table, but we don't need anything else just yet. */
6431
6432 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6433 abbrev_section, ptr, section_kind);
6434
6435 length = header.get_length ();
6436
6437 /* Skip dummy type units. */
6438 if (ptr >= info_ptr + length
6439 || peek_abbrev_code (abfd, ptr) == 0
6440 || (header.unit_type != DW_UT_type
6441 && header.unit_type != DW_UT_split_type))
6442 {
6443 info_ptr += length;
6444 continue;
6445 }
6446
6447 if (types_htab == NULL)
6448 {
6449 if (dwo_file)
6450 types_htab = allocate_dwo_unit_table ();
6451 else
6452 types_htab = allocate_signatured_type_table ();
6453 }
6454
6455 if (dwo_file)
6456 {
6457 sig_type = NULL;
6458 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6459 dwo_tu->dwo_file = dwo_file;
6460 dwo_tu->signature = header.signature;
6461 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6462 dwo_tu->section = section;
6463 dwo_tu->sect_off = sect_off;
6464 dwo_tu->length = length;
6465 }
6466 else
6467 {
6468 /* N.B.: type_offset is not usable if this type uses a DWO file.
6469 The real type_offset is in the DWO file. */
6470 dwo_tu = NULL;
6471 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6472 sig_type->signature = header.signature;
6473 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6474 sig_type->per_cu.is_debug_types = 1;
6475 sig_type->per_cu.section = section;
6476 sig_type->per_cu.sect_off = sect_off;
6477 sig_type->per_cu.length = length;
6478 }
6479
6480 slot = htab_find_slot (types_htab.get (),
6481 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6482 INSERT);
6483 gdb_assert (slot != NULL);
6484 if (*slot != NULL)
6485 {
6486 sect_offset dup_sect_off;
6487
6488 if (dwo_file)
6489 {
6490 const struct dwo_unit *dup_tu
6491 = (const struct dwo_unit *) *slot;
6492
6493 dup_sect_off = dup_tu->sect_off;
6494 }
6495 else
6496 {
6497 const struct signatured_type *dup_tu
6498 = (const struct signatured_type *) *slot;
6499
6500 dup_sect_off = dup_tu->per_cu.sect_off;
6501 }
6502
6503 complaint (_("debug type entry at offset %s is duplicate to"
6504 " the entry at offset %s, signature %s"),
6505 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6506 hex_string (header.signature));
6507 }
6508 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6509
6510 dwarf_read_debug_printf_v (" offset %s, signature %s",
6511 sect_offset_str (sect_off),
6512 hex_string (header.signature));
6513
6514 info_ptr += length;
6515 }
6516 }
6517
6518 /* Create the hash table of all entries in the .debug_types
6519 (or .debug_types.dwo) section(s).
6520 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6521 otherwise it is NULL.
6522
6523 The result is a pointer to the hash table or NULL if there are no types.
6524
6525 Note: This function processes DWO files only, not DWP files. */
6526
6527 static void
6528 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6529 struct dwo_file *dwo_file,
6530 gdb::array_view<dwarf2_section_info> type_sections,
6531 htab_up &types_htab)
6532 {
6533 for (dwarf2_section_info &section : type_sections)
6534 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6535 rcuh_kind::TYPE);
6536 }
6537
6538 /* Create the hash table of all entries in the .debug_types section,
6539 and initialize all_type_units.
6540 The result is zero if there is an error (e.g. missing .debug_types section),
6541 otherwise non-zero. */
6542
6543 static int
6544 create_all_type_units (dwarf2_per_objfile *per_objfile)
6545 {
6546 htab_up types_htab;
6547
6548 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6549 types_htab, rcuh_kind::COMPILE);
6550 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6551 types_htab);
6552 if (types_htab == NULL)
6553 {
6554 per_objfile->per_bfd->signatured_types = NULL;
6555 return 0;
6556 }
6557
6558 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6559
6560 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6561 per_objfile->per_bfd->all_type_units.reserve
6562 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6563
6564 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6565 add_signatured_type_cu_to_table,
6566 &per_objfile->per_bfd->all_type_units);
6567
6568 return 1;
6569 }
6570
6571 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6572 If SLOT is non-NULL, it is the entry to use in the hash table.
6573 Otherwise we find one. */
6574
6575 static struct signatured_type *
6576 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6577 {
6578 if (per_objfile->per_bfd->all_type_units.size ()
6579 == per_objfile->per_bfd->all_type_units.capacity ())
6580 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6581
6582 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6583
6584 per_objfile->resize_symtabs ();
6585
6586 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6587 sig_type->signature = sig;
6588 sig_type->per_cu.is_debug_types = 1;
6589 if (per_objfile->per_bfd->using_index)
6590 {
6591 sig_type->per_cu.v.quick =
6592 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6593 struct dwarf2_per_cu_quick_data);
6594 }
6595
6596 if (slot == NULL)
6597 {
6598 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6599 sig_type, INSERT);
6600 }
6601 gdb_assert (*slot == NULL);
6602 *slot = sig_type;
6603 /* The rest of sig_type must be filled in by the caller. */
6604 return sig_type;
6605 }
6606
6607 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6608 Fill in SIG_ENTRY with DWO_ENTRY. */
6609
6610 static void
6611 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6612 struct signatured_type *sig_entry,
6613 struct dwo_unit *dwo_entry)
6614 {
6615 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6616
6617 /* Make sure we're not clobbering something we don't expect to. */
6618 gdb_assert (! sig_entry->per_cu.queued);
6619 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6620 if (per_bfd->using_index)
6621 {
6622 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6623 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6624 }
6625 else
6626 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6627 gdb_assert (sig_entry->signature == dwo_entry->signature);
6628 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6629 gdb_assert (sig_entry->type_unit_group == NULL);
6630 gdb_assert (sig_entry->dwo_unit == NULL);
6631
6632 sig_entry->per_cu.section = dwo_entry->section;
6633 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6634 sig_entry->per_cu.length = dwo_entry->length;
6635 sig_entry->per_cu.reading_dwo_directly = 1;
6636 sig_entry->per_cu.per_bfd = per_bfd;
6637 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6638 sig_entry->dwo_unit = dwo_entry;
6639 }
6640
6641 /* Subroutine of lookup_signatured_type.
6642 If we haven't read the TU yet, create the signatured_type data structure
6643 for a TU to be read in directly from a DWO file, bypassing the stub.
6644 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6645 using .gdb_index, then when reading a CU we want to stay in the DWO file
6646 containing that CU. Otherwise we could end up reading several other DWO
6647 files (due to comdat folding) to process the transitive closure of all the
6648 mentioned TUs, and that can be slow. The current DWO file will have every
6649 type signature that it needs.
6650 We only do this for .gdb_index because in the psymtab case we already have
6651 to read all the DWOs to build the type unit groups. */
6652
6653 static struct signatured_type *
6654 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6655 {
6656 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6657 struct dwo_file *dwo_file;
6658 struct dwo_unit find_dwo_entry, *dwo_entry;
6659 struct signatured_type find_sig_entry, *sig_entry;
6660 void **slot;
6661
6662 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6663
6664 /* If TU skeletons have been removed then we may not have read in any
6665 TUs yet. */
6666 if (per_objfile->per_bfd->signatured_types == NULL)
6667 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6668
6669 /* We only ever need to read in one copy of a signatured type.
6670 Use the global signatured_types array to do our own comdat-folding
6671 of types. If this is the first time we're reading this TU, and
6672 the TU has an entry in .gdb_index, replace the recorded data from
6673 .gdb_index with this TU. */
6674
6675 find_sig_entry.signature = sig;
6676 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6677 &find_sig_entry, INSERT);
6678 sig_entry = (struct signatured_type *) *slot;
6679
6680 /* We can get here with the TU already read, *or* in the process of being
6681 read. Don't reassign the global entry to point to this DWO if that's
6682 the case. Also note that if the TU is already being read, it may not
6683 have come from a DWO, the program may be a mix of Fission-compiled
6684 code and non-Fission-compiled code. */
6685
6686 /* Have we already tried to read this TU?
6687 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6688 needn't exist in the global table yet). */
6689 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6690 return sig_entry;
6691
6692 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6693 dwo_unit of the TU itself. */
6694 dwo_file = cu->dwo_unit->dwo_file;
6695
6696 /* Ok, this is the first time we're reading this TU. */
6697 if (dwo_file->tus == NULL)
6698 return NULL;
6699 find_dwo_entry.signature = sig;
6700 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6701 &find_dwo_entry);
6702 if (dwo_entry == NULL)
6703 return NULL;
6704
6705 /* If the global table doesn't have an entry for this TU, add one. */
6706 if (sig_entry == NULL)
6707 sig_entry = add_type_unit (per_objfile, sig, slot);
6708
6709 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6710 sig_entry->per_cu.tu_read = 1;
6711 return sig_entry;
6712 }
6713
6714 /* Subroutine of lookup_signatured_type.
6715 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6716 then try the DWP file. If the TU stub (skeleton) has been removed then
6717 it won't be in .gdb_index. */
6718
6719 static struct signatured_type *
6720 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6721 {
6722 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6723 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6724 struct dwo_unit *dwo_entry;
6725 struct signatured_type find_sig_entry, *sig_entry;
6726 void **slot;
6727
6728 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6729 gdb_assert (dwp_file != NULL);
6730
6731 /* If TU skeletons have been removed then we may not have read in any
6732 TUs yet. */
6733 if (per_objfile->per_bfd->signatured_types == NULL)
6734 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6735
6736 find_sig_entry.signature = sig;
6737 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6738 &find_sig_entry, INSERT);
6739 sig_entry = (struct signatured_type *) *slot;
6740
6741 /* Have we already tried to read this TU?
6742 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6743 needn't exist in the global table yet). */
6744 if (sig_entry != NULL)
6745 return sig_entry;
6746
6747 if (dwp_file->tus == NULL)
6748 return NULL;
6749 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6750 1 /* is_debug_types */);
6751 if (dwo_entry == NULL)
6752 return NULL;
6753
6754 sig_entry = add_type_unit (per_objfile, sig, slot);
6755 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6756
6757 return sig_entry;
6758 }
6759
6760 /* Lookup a signature based type for DW_FORM_ref_sig8.
6761 Returns NULL if signature SIG is not present in the table.
6762 It is up to the caller to complain about this. */
6763
6764 static struct signatured_type *
6765 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6766 {
6767 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6768
6769 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6770 {
6771 /* We're in a DWO/DWP file, and we're using .gdb_index.
6772 These cases require special processing. */
6773 if (get_dwp_file (per_objfile) == NULL)
6774 return lookup_dwo_signatured_type (cu, sig);
6775 else
6776 return lookup_dwp_signatured_type (cu, sig);
6777 }
6778 else
6779 {
6780 struct signatured_type find_entry, *entry;
6781
6782 if (per_objfile->per_bfd->signatured_types == NULL)
6783 return NULL;
6784 find_entry.signature = sig;
6785 entry = ((struct signatured_type *)
6786 htab_find (per_objfile->per_bfd->signatured_types.get (),
6787 &find_entry));
6788 return entry;
6789 }
6790 }
6791
6792 /* Low level DIE reading support. */
6793
6794 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6795
6796 static void
6797 init_cu_die_reader (struct die_reader_specs *reader,
6798 struct dwarf2_cu *cu,
6799 struct dwarf2_section_info *section,
6800 struct dwo_file *dwo_file,
6801 struct abbrev_table *abbrev_table)
6802 {
6803 gdb_assert (section->readin && section->buffer != NULL);
6804 reader->abfd = section->get_bfd_owner ();
6805 reader->cu = cu;
6806 reader->dwo_file = dwo_file;
6807 reader->die_section = section;
6808 reader->buffer = section->buffer;
6809 reader->buffer_end = section->buffer + section->size;
6810 reader->abbrev_table = abbrev_table;
6811 }
6812
6813 /* Subroutine of cutu_reader to simplify it.
6814 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6815 There's just a lot of work to do, and cutu_reader is big enough
6816 already.
6817
6818 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6819 from it to the DIE in the DWO. If NULL we are skipping the stub.
6820 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6821 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6822 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6823 STUB_COMP_DIR may be non-NULL.
6824 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6825 are filled in with the info of the DIE from the DWO file.
6826 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6827 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6828 kept around for at least as long as *RESULT_READER.
6829
6830 The result is non-zero if a valid (non-dummy) DIE was found. */
6831
6832 static int
6833 read_cutu_die_from_dwo (dwarf2_cu *cu,
6834 struct dwo_unit *dwo_unit,
6835 struct die_info *stub_comp_unit_die,
6836 const char *stub_comp_dir,
6837 struct die_reader_specs *result_reader,
6838 const gdb_byte **result_info_ptr,
6839 struct die_info **result_comp_unit_die,
6840 abbrev_table_up *result_dwo_abbrev_table)
6841 {
6842 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6843 dwarf2_per_cu_data *per_cu = cu->per_cu;
6844 struct objfile *objfile = per_objfile->objfile;
6845 bfd *abfd;
6846 const gdb_byte *begin_info_ptr, *info_ptr;
6847 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6848 int i,num_extra_attrs;
6849 struct dwarf2_section_info *dwo_abbrev_section;
6850 struct die_info *comp_unit_die;
6851
6852 /* At most one of these may be provided. */
6853 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6854
6855 /* These attributes aren't processed until later:
6856 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6857 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6858 referenced later. However, these attributes are found in the stub
6859 which we won't have later. In order to not impose this complication
6860 on the rest of the code, we read them here and copy them to the
6861 DWO CU/TU die. */
6862
6863 stmt_list = NULL;
6864 low_pc = NULL;
6865 high_pc = NULL;
6866 ranges = NULL;
6867 comp_dir = NULL;
6868
6869 if (stub_comp_unit_die != NULL)
6870 {
6871 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6872 DWO file. */
6873 if (!per_cu->is_debug_types)
6874 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6875 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6876 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6877 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6878 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6879
6880 cu->addr_base = stub_comp_unit_die->addr_base ();
6881
6882 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6883 We need the value before we can process DW_AT_ranges values from the
6884 DWO. */
6885 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6886
6887 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6888 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6889 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6890 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6891 section. */
6892 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6893 }
6894 else if (stub_comp_dir != NULL)
6895 {
6896 /* Reconstruct the comp_dir attribute to simplify the code below. */
6897 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6898 comp_dir->name = DW_AT_comp_dir;
6899 comp_dir->form = DW_FORM_string;
6900 comp_dir->set_string_noncanonical (stub_comp_dir);
6901 }
6902
6903 /* Set up for reading the DWO CU/TU. */
6904 cu->dwo_unit = dwo_unit;
6905 dwarf2_section_info *section = dwo_unit->section;
6906 section->read (objfile);
6907 abfd = section->get_bfd_owner ();
6908 begin_info_ptr = info_ptr = (section->buffer
6909 + to_underlying (dwo_unit->sect_off));
6910 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6911
6912 if (per_cu->is_debug_types)
6913 {
6914 signatured_type *sig_type = (struct signatured_type *) per_cu;
6915
6916 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6917 section, dwo_abbrev_section,
6918 info_ptr, rcuh_kind::TYPE);
6919 /* This is not an assert because it can be caused by bad debug info. */
6920 if (sig_type->signature != cu->header.signature)
6921 {
6922 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6923 " TU at offset %s [in module %s]"),
6924 hex_string (sig_type->signature),
6925 hex_string (cu->header.signature),
6926 sect_offset_str (dwo_unit->sect_off),
6927 bfd_get_filename (abfd));
6928 }
6929 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6930 /* For DWOs coming from DWP files, we don't know the CU length
6931 nor the type's offset in the TU until now. */
6932 dwo_unit->length = cu->header.get_length ();
6933 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6934
6935 /* Establish the type offset that can be used to lookup the type.
6936 For DWO files, we don't know it until now. */
6937 sig_type->type_offset_in_section
6938 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6939 }
6940 else
6941 {
6942 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6943 section, dwo_abbrev_section,
6944 info_ptr, rcuh_kind::COMPILE);
6945 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6946 /* For DWOs coming from DWP files, we don't know the CU length
6947 until now. */
6948 dwo_unit->length = cu->header.get_length ();
6949 }
6950
6951 dwo_abbrev_section->read (objfile);
6952 *result_dwo_abbrev_table
6953 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6954 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6955 result_dwo_abbrev_table->get ());
6956
6957 /* Read in the die, but leave space to copy over the attributes
6958 from the stub. This has the benefit of simplifying the rest of
6959 the code - all the work to maintain the illusion of a single
6960 DW_TAG_{compile,type}_unit DIE is done here. */
6961 num_extra_attrs = ((stmt_list != NULL)
6962 + (low_pc != NULL)
6963 + (high_pc != NULL)
6964 + (ranges != NULL)
6965 + (comp_dir != NULL));
6966 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6967 num_extra_attrs);
6968
6969 /* Copy over the attributes from the stub to the DIE we just read in. */
6970 comp_unit_die = *result_comp_unit_die;
6971 i = comp_unit_die->num_attrs;
6972 if (stmt_list != NULL)
6973 comp_unit_die->attrs[i++] = *stmt_list;
6974 if (low_pc != NULL)
6975 comp_unit_die->attrs[i++] = *low_pc;
6976 if (high_pc != NULL)
6977 comp_unit_die->attrs[i++] = *high_pc;
6978 if (ranges != NULL)
6979 comp_unit_die->attrs[i++] = *ranges;
6980 if (comp_dir != NULL)
6981 comp_unit_die->attrs[i++] = *comp_dir;
6982 comp_unit_die->num_attrs += num_extra_attrs;
6983
6984 if (dwarf_die_debug)
6985 {
6986 fprintf_unfiltered (gdb_stdlog,
6987 "Read die from %s@0x%x of %s:\n",
6988 section->get_name (),
6989 (unsigned) (begin_info_ptr - section->buffer),
6990 bfd_get_filename (abfd));
6991 dump_die (comp_unit_die, dwarf_die_debug);
6992 }
6993
6994 /* Skip dummy compilation units. */
6995 if (info_ptr >= begin_info_ptr + dwo_unit->length
6996 || peek_abbrev_code (abfd, info_ptr) == 0)
6997 return 0;
6998
6999 *result_info_ptr = info_ptr;
7000 return 1;
7001 }
7002
7003 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7004 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7005 signature is part of the header. */
7006 static gdb::optional<ULONGEST>
7007 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7008 {
7009 if (cu->header.version >= 5)
7010 return cu->header.signature;
7011 struct attribute *attr;
7012 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7013 if (attr == nullptr || !attr->form_is_unsigned ())
7014 return gdb::optional<ULONGEST> ();
7015 return attr->as_unsigned ();
7016 }
7017
7018 /* Subroutine of cutu_reader to simplify it.
7019 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7020 Returns NULL if the specified DWO unit cannot be found. */
7021
7022 static struct dwo_unit *
7023 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7024 {
7025 dwarf2_per_cu_data *per_cu = cu->per_cu;
7026 struct dwo_unit *dwo_unit;
7027 const char *comp_dir;
7028
7029 gdb_assert (cu != NULL);
7030
7031 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7032 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7033 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7034
7035 if (per_cu->is_debug_types)
7036 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7037 else
7038 {
7039 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7040
7041 if (!signature.has_value ())
7042 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7043 " [in module %s]"),
7044 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7045
7046 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7047 }
7048
7049 return dwo_unit;
7050 }
7051
7052 /* Subroutine of cutu_reader to simplify it.
7053 See it for a description of the parameters.
7054 Read a TU directly from a DWO file, bypassing the stub. */
7055
7056 void
7057 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7058 dwarf2_per_objfile *per_objfile,
7059 dwarf2_cu *existing_cu)
7060 {
7061 struct signatured_type *sig_type;
7062
7063 /* Verify we can do the following downcast, and that we have the
7064 data we need. */
7065 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7066 sig_type = (struct signatured_type *) this_cu;
7067 gdb_assert (sig_type->dwo_unit != NULL);
7068
7069 dwarf2_cu *cu;
7070
7071 if (existing_cu != nullptr)
7072 {
7073 cu = existing_cu;
7074 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7075 /* There's no need to do the rereading_dwo_cu handling that
7076 cutu_reader does since we don't read the stub. */
7077 }
7078 else
7079 {
7080 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7081 in per_objfile yet. */
7082 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7083 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7084 cu = m_new_cu.get ();
7085 }
7086
7087 /* A future optimization, if needed, would be to use an existing
7088 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7089 could share abbrev tables. */
7090
7091 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7092 NULL /* stub_comp_unit_die */,
7093 sig_type->dwo_unit->dwo_file->comp_dir,
7094 this, &info_ptr,
7095 &comp_unit_die,
7096 &m_dwo_abbrev_table) == 0)
7097 {
7098 /* Dummy die. */
7099 dummy_p = true;
7100 }
7101 }
7102
7103 /* Initialize a CU (or TU) and read its DIEs.
7104 If the CU defers to a DWO file, read the DWO file as well.
7105
7106 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7107 Otherwise the table specified in the comp unit header is read in and used.
7108 This is an optimization for when we already have the abbrev table.
7109
7110 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7111 allocated. */
7112
7113 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7114 dwarf2_per_objfile *per_objfile,
7115 struct abbrev_table *abbrev_table,
7116 dwarf2_cu *existing_cu,
7117 bool skip_partial)
7118 : die_reader_specs {},
7119 m_this_cu (this_cu)
7120 {
7121 struct objfile *objfile = per_objfile->objfile;
7122 struct dwarf2_section_info *section = this_cu->section;
7123 bfd *abfd = section->get_bfd_owner ();
7124 const gdb_byte *begin_info_ptr;
7125 struct signatured_type *sig_type = NULL;
7126 struct dwarf2_section_info *abbrev_section;
7127 /* Non-zero if CU currently points to a DWO file and we need to
7128 reread it. When this happens we need to reread the skeleton die
7129 before we can reread the DWO file (this only applies to CUs, not TUs). */
7130 int rereading_dwo_cu = 0;
7131
7132 if (dwarf_die_debug)
7133 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7134 this_cu->is_debug_types ? "type" : "comp",
7135 sect_offset_str (this_cu->sect_off));
7136
7137 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7138 file (instead of going through the stub), short-circuit all of this. */
7139 if (this_cu->reading_dwo_directly)
7140 {
7141 /* Narrow down the scope of possibilities to have to understand. */
7142 gdb_assert (this_cu->is_debug_types);
7143 gdb_assert (abbrev_table == NULL);
7144 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7145 return;
7146 }
7147
7148 /* This is cheap if the section is already read in. */
7149 section->read (objfile);
7150
7151 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7152
7153 abbrev_section = get_abbrev_section_for_cu (this_cu);
7154
7155 dwarf2_cu *cu;
7156
7157 if (existing_cu != nullptr)
7158 {
7159 cu = existing_cu;
7160 /* If this CU is from a DWO file we need to start over, we need to
7161 refetch the attributes from the skeleton CU.
7162 This could be optimized by retrieving those attributes from when we
7163 were here the first time: the previous comp_unit_die was stored in
7164 comp_unit_obstack. But there's no data yet that we need this
7165 optimization. */
7166 if (cu->dwo_unit != NULL)
7167 rereading_dwo_cu = 1;
7168 }
7169 else
7170 {
7171 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7172 in per_objfile yet. */
7173 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7174 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7175 cu = m_new_cu.get ();
7176 }
7177
7178 /* Get the header. */
7179 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7180 {
7181 /* We already have the header, there's no need to read it in again. */
7182 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7183 }
7184 else
7185 {
7186 if (this_cu->is_debug_types)
7187 {
7188 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7189 section, abbrev_section,
7190 info_ptr, rcuh_kind::TYPE);
7191
7192 /* Since per_cu is the first member of struct signatured_type,
7193 we can go from a pointer to one to a pointer to the other. */
7194 sig_type = (struct signatured_type *) this_cu;
7195 gdb_assert (sig_type->signature == cu->header.signature);
7196 gdb_assert (sig_type->type_offset_in_tu
7197 == cu->header.type_cu_offset_in_tu);
7198 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7199
7200 /* LENGTH has not been set yet for type units if we're
7201 using .gdb_index. */
7202 this_cu->length = cu->header.get_length ();
7203
7204 /* Establish the type offset that can be used to lookup the type. */
7205 sig_type->type_offset_in_section =
7206 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7207
7208 this_cu->dwarf_version = cu->header.version;
7209 }
7210 else
7211 {
7212 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7213 section, abbrev_section,
7214 info_ptr,
7215 rcuh_kind::COMPILE);
7216
7217 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7218 if (this_cu->length == 0)
7219 this_cu->length = cu->header.get_length ();
7220 else
7221 gdb_assert (this_cu->length == cu->header.get_length ());
7222 this_cu->dwarf_version = cu->header.version;
7223 }
7224 }
7225
7226 /* Skip dummy compilation units. */
7227 if (info_ptr >= begin_info_ptr + this_cu->length
7228 || peek_abbrev_code (abfd, info_ptr) == 0)
7229 {
7230 dummy_p = true;
7231 return;
7232 }
7233
7234 /* If we don't have them yet, read the abbrevs for this compilation unit.
7235 And if we need to read them now, make sure they're freed when we're
7236 done. */
7237 if (abbrev_table != NULL)
7238 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7239 else
7240 {
7241 abbrev_section->read (objfile);
7242 m_abbrev_table_holder
7243 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7244 abbrev_table = m_abbrev_table_holder.get ();
7245 }
7246
7247 /* Read the top level CU/TU die. */
7248 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7249 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7250
7251 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7252 {
7253 dummy_p = true;
7254 return;
7255 }
7256
7257 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7258 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7259 table from the DWO file and pass the ownership over to us. It will be
7260 referenced from READER, so we must make sure to free it after we're done
7261 with READER.
7262
7263 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7264 DWO CU, that this test will fail (the attribute will not be present). */
7265 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7266 if (dwo_name != nullptr)
7267 {
7268 struct dwo_unit *dwo_unit;
7269 struct die_info *dwo_comp_unit_die;
7270
7271 if (comp_unit_die->has_children)
7272 {
7273 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7274 " has children (offset %s) [in module %s]"),
7275 sect_offset_str (this_cu->sect_off),
7276 bfd_get_filename (abfd));
7277 }
7278 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7279 if (dwo_unit != NULL)
7280 {
7281 if (read_cutu_die_from_dwo (cu, dwo_unit,
7282 comp_unit_die, NULL,
7283 this, &info_ptr,
7284 &dwo_comp_unit_die,
7285 &m_dwo_abbrev_table) == 0)
7286 {
7287 /* Dummy die. */
7288 dummy_p = true;
7289 return;
7290 }
7291 comp_unit_die = dwo_comp_unit_die;
7292 }
7293 else
7294 {
7295 /* Yikes, we couldn't find the rest of the DIE, we only have
7296 the stub. A complaint has already been logged. There's
7297 not much more we can do except pass on the stub DIE to
7298 die_reader_func. We don't want to throw an error on bad
7299 debug info. */
7300 }
7301 }
7302 }
7303
7304 void
7305 cutu_reader::keep ()
7306 {
7307 /* Done, clean up. */
7308 gdb_assert (!dummy_p);
7309 if (m_new_cu != NULL)
7310 {
7311 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7312 now. */
7313 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7314 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7315 }
7316 }
7317
7318 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7319 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7320 assumed to have already done the lookup to find the DWO file).
7321
7322 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7323 THIS_CU->is_debug_types, but nothing else.
7324
7325 We fill in THIS_CU->length.
7326
7327 THIS_CU->cu is always freed when done.
7328 This is done in order to not leave THIS_CU->cu in a state where we have
7329 to care whether it refers to the "main" CU or the DWO CU.
7330
7331 When parent_cu is passed, it is used to provide a default value for
7332 str_offsets_base and addr_base from the parent. */
7333
7334 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7335 dwarf2_per_objfile *per_objfile,
7336 struct dwarf2_cu *parent_cu,
7337 struct dwo_file *dwo_file)
7338 : die_reader_specs {},
7339 m_this_cu (this_cu)
7340 {
7341 struct objfile *objfile = per_objfile->objfile;
7342 struct dwarf2_section_info *section = this_cu->section;
7343 bfd *abfd = section->get_bfd_owner ();
7344 struct dwarf2_section_info *abbrev_section;
7345 const gdb_byte *begin_info_ptr, *info_ptr;
7346
7347 if (dwarf_die_debug)
7348 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7349 this_cu->is_debug_types ? "type" : "comp",
7350 sect_offset_str (this_cu->sect_off));
7351
7352 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7353
7354 abbrev_section = (dwo_file != NULL
7355 ? &dwo_file->sections.abbrev
7356 : get_abbrev_section_for_cu (this_cu));
7357
7358 /* This is cheap if the section is already read in. */
7359 section->read (objfile);
7360
7361 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7362
7363 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7364 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7365 section, abbrev_section, info_ptr,
7366 (this_cu->is_debug_types
7367 ? rcuh_kind::TYPE
7368 : rcuh_kind::COMPILE));
7369
7370 if (parent_cu != nullptr)
7371 {
7372 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7373 m_new_cu->addr_base = parent_cu->addr_base;
7374 }
7375 this_cu->length = m_new_cu->header.get_length ();
7376
7377 /* Skip dummy compilation units. */
7378 if (info_ptr >= begin_info_ptr + this_cu->length
7379 || peek_abbrev_code (abfd, info_ptr) == 0)
7380 {
7381 dummy_p = true;
7382 return;
7383 }
7384
7385 abbrev_section->read (objfile);
7386 m_abbrev_table_holder
7387 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7388
7389 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7390 m_abbrev_table_holder.get ());
7391 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7392 }
7393
7394 \f
7395 /* Type Unit Groups.
7396
7397 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7398 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7399 so that all types coming from the same compilation (.o file) are grouped
7400 together. A future step could be to put the types in the same symtab as
7401 the CU the types ultimately came from. */
7402
7403 static hashval_t
7404 hash_type_unit_group (const void *item)
7405 {
7406 const struct type_unit_group *tu_group
7407 = (const struct type_unit_group *) item;
7408
7409 return hash_stmt_list_entry (&tu_group->hash);
7410 }
7411
7412 static int
7413 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7414 {
7415 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7416 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7417
7418 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7419 }
7420
7421 /* Allocate a hash table for type unit groups. */
7422
7423 static htab_up
7424 allocate_type_unit_groups_table ()
7425 {
7426 return htab_up (htab_create_alloc (3,
7427 hash_type_unit_group,
7428 eq_type_unit_group,
7429 NULL, xcalloc, xfree));
7430 }
7431
7432 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7433 partial symtabs. We combine several TUs per psymtab to not let the size
7434 of any one psymtab grow too big. */
7435 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7436 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7437
7438 /* Helper routine for get_type_unit_group.
7439 Create the type_unit_group object used to hold one or more TUs. */
7440
7441 static struct type_unit_group *
7442 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7443 {
7444 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7445 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7446 struct dwarf2_per_cu_data *per_cu;
7447 struct type_unit_group *tu_group;
7448
7449 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7450 per_cu = &tu_group->per_cu;
7451 per_cu->per_bfd = per_bfd;
7452
7453 if (per_bfd->using_index)
7454 {
7455 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7456 struct dwarf2_per_cu_quick_data);
7457 }
7458 else
7459 {
7460 unsigned int line_offset = to_underlying (line_offset_struct);
7461 dwarf2_psymtab *pst;
7462 std::string name;
7463
7464 /* Give the symtab a useful name for debug purposes. */
7465 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7466 name = string_printf ("<type_units_%d>",
7467 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7468 else
7469 name = string_printf ("<type_units_at_0x%x>", line_offset);
7470
7471 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7472 pst->anonymous = true;
7473 }
7474
7475 tu_group->hash.dwo_unit = cu->dwo_unit;
7476 tu_group->hash.line_sect_off = line_offset_struct;
7477
7478 return tu_group;
7479 }
7480
7481 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7482 STMT_LIST is a DW_AT_stmt_list attribute. */
7483
7484 static struct type_unit_group *
7485 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7486 {
7487 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7488 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7489 struct type_unit_group *tu_group;
7490 void **slot;
7491 unsigned int line_offset;
7492 struct type_unit_group type_unit_group_for_lookup;
7493
7494 if (per_objfile->per_bfd->type_unit_groups == NULL)
7495 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7496
7497 /* Do we need to create a new group, or can we use an existing one? */
7498
7499 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7500 {
7501 line_offset = stmt_list->as_unsigned ();
7502 ++tu_stats->nr_symtab_sharers;
7503 }
7504 else
7505 {
7506 /* Ugh, no stmt_list. Rare, but we have to handle it.
7507 We can do various things here like create one group per TU or
7508 spread them over multiple groups to split up the expansion work.
7509 To avoid worst case scenarios (too many groups or too large groups)
7510 we, umm, group them in bunches. */
7511 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7512 | (tu_stats->nr_stmt_less_type_units
7513 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7514 ++tu_stats->nr_stmt_less_type_units;
7515 }
7516
7517 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7518 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7519 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7520 &type_unit_group_for_lookup, INSERT);
7521 if (*slot != NULL)
7522 {
7523 tu_group = (struct type_unit_group *) *slot;
7524 gdb_assert (tu_group != NULL);
7525 }
7526 else
7527 {
7528 sect_offset line_offset_struct = (sect_offset) line_offset;
7529 tu_group = create_type_unit_group (cu, line_offset_struct);
7530 *slot = tu_group;
7531 ++tu_stats->nr_symtabs;
7532 }
7533
7534 return tu_group;
7535 }
7536 \f
7537 /* Partial symbol tables. */
7538
7539 /* Create a psymtab named NAME and assign it to PER_CU.
7540
7541 The caller must fill in the following details:
7542 dirname, textlow, texthigh. */
7543
7544 static dwarf2_psymtab *
7545 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7546 dwarf2_per_objfile *per_objfile,
7547 const char *name)
7548 {
7549 struct objfile *objfile = per_objfile->objfile;
7550 dwarf2_psymtab *pst;
7551
7552 pst = new dwarf2_psymtab (name, objfile, per_cu);
7553
7554 pst->psymtabs_addrmap_supported = true;
7555
7556 /* This is the glue that links PST into GDB's symbol API. */
7557 per_cu->v.psymtab = pst;
7558
7559 return pst;
7560 }
7561
7562 /* DIE reader function for process_psymtab_comp_unit. */
7563
7564 static void
7565 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7566 const gdb_byte *info_ptr,
7567 struct die_info *comp_unit_die,
7568 enum language pretend_language)
7569 {
7570 struct dwarf2_cu *cu = reader->cu;
7571 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7572 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7573 struct objfile *objfile = per_objfile->objfile;
7574 struct gdbarch *gdbarch = objfile->arch ();
7575 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7576 CORE_ADDR baseaddr;
7577 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7578 dwarf2_psymtab *pst;
7579 enum pc_bounds_kind cu_bounds_kind;
7580 const char *filename;
7581
7582 gdb_assert (! per_cu->is_debug_types);
7583
7584 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7585
7586 /* Allocate a new partial symbol table structure. */
7587 gdb::unique_xmalloc_ptr<char> debug_filename;
7588 static const char artificial[] = "<artificial>";
7589 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7590 if (filename == NULL)
7591 filename = "";
7592 else if (strcmp (filename, artificial) == 0)
7593 {
7594 debug_filename.reset (concat (artificial, "@",
7595 sect_offset_str (per_cu->sect_off),
7596 (char *) NULL));
7597 filename = debug_filename.get ();
7598 }
7599
7600 pst = create_partial_symtab (per_cu, per_objfile, filename);
7601
7602 /* This must be done before calling dwarf2_build_include_psymtabs. */
7603 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7604
7605 baseaddr = objfile->text_section_offset ();
7606
7607 dwarf2_find_base_address (comp_unit_die, cu);
7608
7609 /* Possibly set the default values of LOWPC and HIGHPC from
7610 `DW_AT_ranges'. */
7611 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7612 &best_highpc, cu, pst);
7613 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7614 {
7615 CORE_ADDR low
7616 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7617 - baseaddr);
7618 CORE_ADDR high
7619 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7620 - baseaddr - 1);
7621 /* Store the contiguous range if it is not empty; it can be
7622 empty for CUs with no code. */
7623 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7624 low, high, pst);
7625 }
7626
7627 /* Check if comp unit has_children.
7628 If so, read the rest of the partial symbols from this comp unit.
7629 If not, there's no more debug_info for this comp unit. */
7630 if (comp_unit_die->has_children)
7631 {
7632 struct partial_die_info *first_die;
7633 CORE_ADDR lowpc, highpc;
7634
7635 lowpc = ((CORE_ADDR) -1);
7636 highpc = ((CORE_ADDR) 0);
7637
7638 first_die = load_partial_dies (reader, info_ptr, 1);
7639
7640 scan_partial_symbols (first_die, &lowpc, &highpc,
7641 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7642
7643 /* If we didn't find a lowpc, set it to highpc to avoid
7644 complaints from `maint check'. */
7645 if (lowpc == ((CORE_ADDR) -1))
7646 lowpc = highpc;
7647
7648 /* If the compilation unit didn't have an explicit address range,
7649 then use the information extracted from its child dies. */
7650 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7651 {
7652 best_lowpc = lowpc;
7653 best_highpc = highpc;
7654 }
7655 }
7656 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7657 best_lowpc + baseaddr)
7658 - baseaddr);
7659 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7660 best_highpc + baseaddr)
7661 - baseaddr);
7662
7663 pst->end ();
7664
7665 if (!cu->per_cu->imported_symtabs_empty ())
7666 {
7667 int i;
7668 int len = cu->per_cu->imported_symtabs_size ();
7669
7670 /* Fill in 'dependencies' here; we fill in 'users' in a
7671 post-pass. */
7672 pst->number_of_dependencies = len;
7673 pst->dependencies
7674 = per_bfd->partial_symtabs->allocate_dependencies (len);
7675 for (i = 0; i < len; ++i)
7676 {
7677 pst->dependencies[i]
7678 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7679 }
7680
7681 cu->per_cu->imported_symtabs_free ();
7682 }
7683
7684 /* Get the list of files included in the current compilation unit,
7685 and build a psymtab for each of them. */
7686 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7687
7688 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7689 ", %d global, %d static syms",
7690 per_cu->is_debug_types ? "type" : "comp",
7691 sect_offset_str (per_cu->sect_off),
7692 paddress (gdbarch, pst->text_low (objfile)),
7693 paddress (gdbarch, pst->text_high (objfile)),
7694 (int) pst->global_psymbols.size (),
7695 (int) pst->static_psymbols.size ());
7696 }
7697
7698 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7699 Process compilation unit THIS_CU for a psymtab. */
7700
7701 static void
7702 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7703 dwarf2_per_objfile *per_objfile,
7704 bool want_partial_unit,
7705 enum language pretend_language)
7706 {
7707 /* If this compilation unit was already read in, free the
7708 cached copy in order to read it in again. This is
7709 necessary because we skipped some symbols when we first
7710 read in the compilation unit (see load_partial_dies).
7711 This problem could be avoided, but the benefit is unclear. */
7712 per_objfile->remove_cu (this_cu);
7713
7714 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7715
7716 switch (reader.comp_unit_die->tag)
7717 {
7718 case DW_TAG_compile_unit:
7719 this_cu->unit_type = DW_UT_compile;
7720 break;
7721 case DW_TAG_partial_unit:
7722 this_cu->unit_type = DW_UT_partial;
7723 break;
7724 case DW_TAG_type_unit:
7725 this_cu->unit_type = DW_UT_type;
7726 break;
7727 default:
7728 abort ();
7729 }
7730
7731 if (reader.dummy_p)
7732 {
7733 /* Nothing. */
7734 }
7735 else if (this_cu->is_debug_types)
7736 build_type_psymtabs_reader (&reader, reader.info_ptr,
7737 reader.comp_unit_die);
7738 else if (want_partial_unit
7739 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7740 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7741 reader.comp_unit_die,
7742 pretend_language);
7743
7744 this_cu->lang = reader.cu->language;
7745
7746 /* Age out any secondary CUs. */
7747 per_objfile->age_comp_units ();
7748 }
7749
7750 /* Reader function for build_type_psymtabs. */
7751
7752 static void
7753 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7754 const gdb_byte *info_ptr,
7755 struct die_info *type_unit_die)
7756 {
7757 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7758 struct dwarf2_cu *cu = reader->cu;
7759 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7760 struct signatured_type *sig_type;
7761 struct type_unit_group *tu_group;
7762 struct attribute *attr;
7763 struct partial_die_info *first_die;
7764 CORE_ADDR lowpc, highpc;
7765 dwarf2_psymtab *pst;
7766
7767 gdb_assert (per_cu->is_debug_types);
7768 sig_type = (struct signatured_type *) per_cu;
7769
7770 if (! type_unit_die->has_children)
7771 return;
7772
7773 attr = type_unit_die->attr (DW_AT_stmt_list);
7774 tu_group = get_type_unit_group (cu, attr);
7775
7776 if (tu_group->tus == nullptr)
7777 tu_group->tus = new std::vector<signatured_type *>;
7778 tu_group->tus->push_back (sig_type);
7779
7780 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7781 pst = create_partial_symtab (per_cu, per_objfile, "");
7782 pst->anonymous = true;
7783
7784 first_die = load_partial_dies (reader, info_ptr, 1);
7785
7786 lowpc = (CORE_ADDR) -1;
7787 highpc = (CORE_ADDR) 0;
7788 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7789
7790 pst->end ();
7791 }
7792
7793 /* Struct used to sort TUs by their abbreviation table offset. */
7794
7795 struct tu_abbrev_offset
7796 {
7797 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7798 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7799 {}
7800
7801 signatured_type *sig_type;
7802 sect_offset abbrev_offset;
7803 };
7804
7805 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7806
7807 static bool
7808 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7809 const struct tu_abbrev_offset &b)
7810 {
7811 return a.abbrev_offset < b.abbrev_offset;
7812 }
7813
7814 /* Efficiently read all the type units.
7815 This does the bulk of the work for build_type_psymtabs.
7816
7817 The efficiency is because we sort TUs by the abbrev table they use and
7818 only read each abbrev table once. In one program there are 200K TUs
7819 sharing 8K abbrev tables.
7820
7821 The main purpose of this function is to support building the
7822 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7823 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7824 can collapse the search space by grouping them by stmt_list.
7825 The savings can be significant, in the same program from above the 200K TUs
7826 share 8K stmt_list tables.
7827
7828 FUNC is expected to call get_type_unit_group, which will create the
7829 struct type_unit_group if necessary and add it to
7830 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7831
7832 static void
7833 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7834 {
7835 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7836 abbrev_table_up abbrev_table;
7837 sect_offset abbrev_offset;
7838
7839 /* It's up to the caller to not call us multiple times. */
7840 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7841
7842 if (per_objfile->per_bfd->all_type_units.empty ())
7843 return;
7844
7845 /* TUs typically share abbrev tables, and there can be way more TUs than
7846 abbrev tables. Sort by abbrev table to reduce the number of times we
7847 read each abbrev table in.
7848 Alternatives are to punt or to maintain a cache of abbrev tables.
7849 This is simpler and efficient enough for now.
7850
7851 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7852 symtab to use). Typically TUs with the same abbrev offset have the same
7853 stmt_list value too so in practice this should work well.
7854
7855 The basic algorithm here is:
7856
7857 sort TUs by abbrev table
7858 for each TU with same abbrev table:
7859 read abbrev table if first user
7860 read TU top level DIE
7861 [IWBN if DWO skeletons had DW_AT_stmt_list]
7862 call FUNC */
7863
7864 dwarf_read_debug_printf ("Building type unit groups ...");
7865
7866 /* Sort in a separate table to maintain the order of all_type_units
7867 for .gdb_index: TU indices directly index all_type_units. */
7868 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7869 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7870
7871 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7872 sorted_by_abbrev.emplace_back
7873 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7874 sig_type->per_cu.sect_off));
7875
7876 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7877 sort_tu_by_abbrev_offset);
7878
7879 abbrev_offset = (sect_offset) ~(unsigned) 0;
7880
7881 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7882 {
7883 /* Switch to the next abbrev table if necessary. */
7884 if (abbrev_table == NULL
7885 || tu.abbrev_offset != abbrev_offset)
7886 {
7887 abbrev_offset = tu.abbrev_offset;
7888 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7889 abbrev_table =
7890 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7891 ++tu_stats->nr_uniq_abbrev_tables;
7892 }
7893
7894 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7895 abbrev_table.get (), nullptr, false);
7896 if (!reader.dummy_p)
7897 build_type_psymtabs_reader (&reader, reader.info_ptr,
7898 reader.comp_unit_die);
7899 }
7900 }
7901
7902 /* Print collected type unit statistics. */
7903
7904 static void
7905 print_tu_stats (dwarf2_per_objfile *per_objfile)
7906 {
7907 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7908
7909 dwarf_read_debug_printf ("Type unit statistics:");
7910 dwarf_read_debug_printf (" %zu TUs",
7911 per_objfile->per_bfd->all_type_units.size ());
7912 dwarf_read_debug_printf (" %d uniq abbrev tables",
7913 tu_stats->nr_uniq_abbrev_tables);
7914 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7915 tu_stats->nr_symtabs);
7916 dwarf_read_debug_printf (" %d symtab sharers",
7917 tu_stats->nr_symtab_sharers);
7918 dwarf_read_debug_printf (" %d type units without a stmt_list",
7919 tu_stats->nr_stmt_less_type_units);
7920 dwarf_read_debug_printf (" %d all_type_units reallocs",
7921 tu_stats->nr_all_type_units_reallocs);
7922 }
7923
7924 /* Traversal function for build_type_psymtabs. */
7925
7926 static int
7927 build_type_psymtab_dependencies (void **slot, void *info)
7928 {
7929 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7930 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7931 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7932 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7933 dwarf2_psymtab *pst = per_cu->v.psymtab;
7934 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7935 int i;
7936
7937 gdb_assert (len > 0);
7938 gdb_assert (per_cu->type_unit_group_p ());
7939
7940 pst->number_of_dependencies = len;
7941 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7942 for (i = 0; i < len; ++i)
7943 {
7944 struct signatured_type *iter = tu_group->tus->at (i);
7945 gdb_assert (iter->per_cu.is_debug_types);
7946 pst->dependencies[i] = iter->per_cu.v.psymtab;
7947 iter->type_unit_group = tu_group;
7948 }
7949
7950 delete tu_group->tus;
7951 tu_group->tus = nullptr;
7952
7953 return 1;
7954 }
7955
7956 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7957 Build partial symbol tables for the .debug_types comp-units. */
7958
7959 static void
7960 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7961 {
7962 if (! create_all_type_units (per_objfile))
7963 return;
7964
7965 build_type_psymtabs_1 (per_objfile);
7966 }
7967
7968 /* Traversal function for process_skeletonless_type_unit.
7969 Read a TU in a DWO file and build partial symbols for it. */
7970
7971 static int
7972 process_skeletonless_type_unit (void **slot, void *info)
7973 {
7974 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7975 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7976 struct signatured_type find_entry, *entry;
7977
7978 /* If this TU doesn't exist in the global table, add it and read it in. */
7979
7980 if (per_objfile->per_bfd->signatured_types == NULL)
7981 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7982
7983 find_entry.signature = dwo_unit->signature;
7984 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7985 &find_entry, INSERT);
7986 /* If we've already seen this type there's nothing to do. What's happening
7987 is we're doing our own version of comdat-folding here. */
7988 if (*slot != NULL)
7989 return 1;
7990
7991 /* This does the job that create_all_type_units would have done for
7992 this TU. */
7993 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7994 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7995 *slot = entry;
7996
7997 /* This does the job that build_type_psymtabs_1 would have done. */
7998 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7999 if (!reader.dummy_p)
8000 build_type_psymtabs_reader (&reader, reader.info_ptr,
8001 reader.comp_unit_die);
8002
8003 return 1;
8004 }
8005
8006 /* Traversal function for process_skeletonless_type_units. */
8007
8008 static int
8009 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8010 {
8011 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8012
8013 if (dwo_file->tus != NULL)
8014 htab_traverse_noresize (dwo_file->tus.get (),
8015 process_skeletonless_type_unit, info);
8016
8017 return 1;
8018 }
8019
8020 /* Scan all TUs of DWO files, verifying we've processed them.
8021 This is needed in case a TU was emitted without its skeleton.
8022 Note: This can't be done until we know what all the DWO files are. */
8023
8024 static void
8025 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8026 {
8027 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8028 if (get_dwp_file (per_objfile) == NULL
8029 && per_objfile->per_bfd->dwo_files != NULL)
8030 {
8031 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8032 process_dwo_file_for_skeletonless_type_units,
8033 per_objfile);
8034 }
8035 }
8036
8037 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8038
8039 static void
8040 set_partial_user (dwarf2_per_objfile *per_objfile)
8041 {
8042 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8043 {
8044 dwarf2_psymtab *pst = per_cu->v.psymtab;
8045
8046 if (pst == NULL)
8047 continue;
8048
8049 for (int j = 0; j < pst->number_of_dependencies; ++j)
8050 {
8051 /* Set the 'user' field only if it is not already set. */
8052 if (pst->dependencies[j]->user == NULL)
8053 pst->dependencies[j]->user = pst;
8054 }
8055 }
8056 }
8057
8058 /* Build the partial symbol table by doing a quick pass through the
8059 .debug_info and .debug_abbrev sections. */
8060
8061 static void
8062 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8063 {
8064 struct objfile *objfile = per_objfile->objfile;
8065 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
8066
8067 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8068 objfile_name (objfile));
8069
8070 scoped_restore restore_reading_psyms
8071 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8072 true);
8073
8074 per_bfd->info.read (objfile);
8075
8076 /* Any cached compilation units will be linked by the per-objfile
8077 read_in_chain. Make sure to free them when we're done. */
8078 free_cached_comp_units freer (per_objfile);
8079
8080 build_type_psymtabs (per_objfile);
8081
8082 create_all_comp_units (per_objfile);
8083
8084 /* Create a temporary address map on a temporary obstack. We later
8085 copy this to the final obstack. */
8086 auto_obstack temp_obstack;
8087
8088 scoped_restore save_psymtabs_addrmap
8089 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
8090 addrmap_create_mutable (&temp_obstack));
8091
8092 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
8093 {
8094 if (per_cu->v.psymtab != NULL)
8095 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8096 continue;
8097 process_psymtab_comp_unit (per_cu, per_objfile, false,
8098 language_minimal);
8099 }
8100
8101 /* This has to wait until we read the CUs, we need the list of DWOs. */
8102 process_skeletonless_type_units (per_objfile);
8103
8104 /* Now that all TUs have been processed we can fill in the dependencies. */
8105 if (per_bfd->type_unit_groups != NULL)
8106 {
8107 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
8108 build_type_psymtab_dependencies, per_objfile);
8109 }
8110
8111 if (dwarf_read_debug > 0)
8112 print_tu_stats (per_objfile);
8113
8114 set_partial_user (per_objfile);
8115
8116 per_bfd->partial_symtabs->psymtabs_addrmap
8117 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
8118 per_bfd->partial_symtabs->obstack ());
8119 /* At this point we want to keep the address map. */
8120 save_psymtabs_addrmap.release ();
8121
8122 dwarf_read_debug_printf ("Done building psymtabs of %s",
8123 objfile_name (objfile));
8124 }
8125
8126 /* Load the partial DIEs for a secondary CU into memory.
8127 This is also used when rereading a primary CU with load_all_dies. */
8128
8129 static void
8130 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8131 dwarf2_per_objfile *per_objfile,
8132 dwarf2_cu *existing_cu)
8133 {
8134 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8135
8136 if (!reader.dummy_p)
8137 {
8138 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8139 language_minimal);
8140
8141 /* Check if comp unit has_children.
8142 If so, read the rest of the partial symbols from this comp unit.
8143 If not, there's no more debug_info for this comp unit. */
8144 if (reader.comp_unit_die->has_children)
8145 load_partial_dies (&reader, reader.info_ptr, 0);
8146
8147 reader.keep ();
8148 }
8149 }
8150
8151 static void
8152 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8153 struct dwarf2_section_info *section,
8154 struct dwarf2_section_info *abbrev_section,
8155 unsigned int is_dwz)
8156 {
8157 const gdb_byte *info_ptr;
8158 struct objfile *objfile = per_objfile->objfile;
8159
8160 dwarf_read_debug_printf ("Reading %s for %s",
8161 section->get_name (),
8162 section->get_file_name ());
8163
8164 section->read (objfile);
8165
8166 info_ptr = section->buffer;
8167
8168 while (info_ptr < section->buffer + section->size)
8169 {
8170 struct dwarf2_per_cu_data *this_cu;
8171
8172 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8173
8174 comp_unit_head cu_header;
8175 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8176 abbrev_section, info_ptr,
8177 rcuh_kind::COMPILE);
8178
8179 /* Save the compilation unit for later lookup. */
8180 if (cu_header.unit_type != DW_UT_type)
8181 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8182 else
8183 {
8184 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8185 sig_type->signature = cu_header.signature;
8186 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8187 this_cu = &sig_type->per_cu;
8188 }
8189 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8190 this_cu->sect_off = sect_off;
8191 this_cu->length = cu_header.length + cu_header.initial_length_size;
8192 this_cu->is_dwz = is_dwz;
8193 this_cu->section = section;
8194
8195 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8196
8197 info_ptr = info_ptr + this_cu->length;
8198 }
8199 }
8200
8201 /* Create a list of all compilation units in OBJFILE.
8202 This is only done for -readnow and building partial symtabs. */
8203
8204 static void
8205 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8206 {
8207 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8208 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8209 &per_objfile->per_bfd->abbrev, 0);
8210
8211 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8212 if (dwz != NULL)
8213 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8214 }
8215
8216 /* Process all loaded DIEs for compilation unit CU, starting at
8217 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8218 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8219 DW_AT_ranges). See the comments of add_partial_subprogram on how
8220 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8221
8222 static void
8223 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8224 CORE_ADDR *highpc, int set_addrmap,
8225 struct dwarf2_cu *cu)
8226 {
8227 struct partial_die_info *pdi;
8228
8229 /* Now, march along the PDI's, descending into ones which have
8230 interesting children but skipping the children of the other ones,
8231 until we reach the end of the compilation unit. */
8232
8233 pdi = first_die;
8234
8235 while (pdi != NULL)
8236 {
8237 pdi->fixup (cu);
8238
8239 /* Anonymous namespaces or modules have no name but have interesting
8240 children, so we need to look at them. Ditto for anonymous
8241 enums. */
8242
8243 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8244 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8245 || pdi->tag == DW_TAG_imported_unit
8246 || pdi->tag == DW_TAG_inlined_subroutine)
8247 {
8248 switch (pdi->tag)
8249 {
8250 case DW_TAG_subprogram:
8251 case DW_TAG_inlined_subroutine:
8252 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8253 if (cu->language == language_cplus)
8254 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8255 set_addrmap, cu);
8256 break;
8257 case DW_TAG_constant:
8258 case DW_TAG_variable:
8259 case DW_TAG_typedef:
8260 case DW_TAG_union_type:
8261 if (!pdi->is_declaration
8262 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8263 {
8264 add_partial_symbol (pdi, cu);
8265 }
8266 break;
8267 case DW_TAG_class_type:
8268 case DW_TAG_interface_type:
8269 case DW_TAG_structure_type:
8270 if (!pdi->is_declaration)
8271 {
8272 add_partial_symbol (pdi, cu);
8273 }
8274 if ((cu->language == language_rust
8275 || cu->language == language_cplus) && pdi->has_children)
8276 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8277 set_addrmap, cu);
8278 break;
8279 case DW_TAG_enumeration_type:
8280 if (!pdi->is_declaration)
8281 add_partial_enumeration (pdi, cu);
8282 break;
8283 case DW_TAG_base_type:
8284 case DW_TAG_subrange_type:
8285 /* File scope base type definitions are added to the partial
8286 symbol table. */
8287 add_partial_symbol (pdi, cu);
8288 break;
8289 case DW_TAG_namespace:
8290 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8291 break;
8292 case DW_TAG_module:
8293 if (!pdi->is_declaration)
8294 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8295 break;
8296 case DW_TAG_imported_unit:
8297 {
8298 struct dwarf2_per_cu_data *per_cu;
8299
8300 /* For now we don't handle imported units in type units. */
8301 if (cu->per_cu->is_debug_types)
8302 {
8303 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8304 " supported in type units [in module %s]"),
8305 objfile_name (cu->per_objfile->objfile));
8306 }
8307
8308 per_cu = dwarf2_find_containing_comp_unit
8309 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8310
8311 /* Go read the partial unit, if needed. */
8312 if (per_cu->v.psymtab == NULL)
8313 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8314 cu->language);
8315
8316 cu->per_cu->imported_symtabs_push (per_cu);
8317 }
8318 break;
8319 case DW_TAG_imported_declaration:
8320 add_partial_symbol (pdi, cu);
8321 break;
8322 default:
8323 break;
8324 }
8325 }
8326
8327 /* If the die has a sibling, skip to the sibling. */
8328
8329 pdi = pdi->die_sibling;
8330 }
8331 }
8332
8333 /* Functions used to compute the fully scoped name of a partial DIE.
8334
8335 Normally, this is simple. For C++, the parent DIE's fully scoped
8336 name is concatenated with "::" and the partial DIE's name.
8337 Enumerators are an exception; they use the scope of their parent
8338 enumeration type, i.e. the name of the enumeration type is not
8339 prepended to the enumerator.
8340
8341 There are two complexities. One is DW_AT_specification; in this
8342 case "parent" means the parent of the target of the specification,
8343 instead of the direct parent of the DIE. The other is compilers
8344 which do not emit DW_TAG_namespace; in this case we try to guess
8345 the fully qualified name of structure types from their members'
8346 linkage names. This must be done using the DIE's children rather
8347 than the children of any DW_AT_specification target. We only need
8348 to do this for structures at the top level, i.e. if the target of
8349 any DW_AT_specification (if any; otherwise the DIE itself) does not
8350 have a parent. */
8351
8352 /* Compute the scope prefix associated with PDI's parent, in
8353 compilation unit CU. The result will be allocated on CU's
8354 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8355 field. NULL is returned if no prefix is necessary. */
8356 static const char *
8357 partial_die_parent_scope (struct partial_die_info *pdi,
8358 struct dwarf2_cu *cu)
8359 {
8360 const char *grandparent_scope;
8361 struct partial_die_info *parent, *real_pdi;
8362
8363 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8364 then this means the parent of the specification DIE. */
8365
8366 real_pdi = pdi;
8367 while (real_pdi->has_specification)
8368 {
8369 auto res = find_partial_die (real_pdi->spec_offset,
8370 real_pdi->spec_is_dwz, cu);
8371 real_pdi = res.pdi;
8372 cu = res.cu;
8373 }
8374
8375 parent = real_pdi->die_parent;
8376 if (parent == NULL)
8377 return NULL;
8378
8379 if (parent->scope_set)
8380 return parent->scope;
8381
8382 parent->fixup (cu);
8383
8384 grandparent_scope = partial_die_parent_scope (parent, cu);
8385
8386 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8387 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8388 Work around this problem here. */
8389 if (cu->language == language_cplus
8390 && parent->tag == DW_TAG_namespace
8391 && strcmp (parent->name (cu), "::") == 0
8392 && grandparent_scope == NULL)
8393 {
8394 parent->scope = NULL;
8395 parent->scope_set = 1;
8396 return NULL;
8397 }
8398
8399 /* Nested subroutines in Fortran get a prefix. */
8400 if (pdi->tag == DW_TAG_enumerator)
8401 /* Enumerators should not get the name of the enumeration as a prefix. */
8402 parent->scope = grandparent_scope;
8403 else if (parent->tag == DW_TAG_namespace
8404 || parent->tag == DW_TAG_module
8405 || parent->tag == DW_TAG_structure_type
8406 || parent->tag == DW_TAG_class_type
8407 || parent->tag == DW_TAG_interface_type
8408 || parent->tag == DW_TAG_union_type
8409 || parent->tag == DW_TAG_enumeration_type
8410 || (cu->language == language_fortran
8411 && parent->tag == DW_TAG_subprogram
8412 && pdi->tag == DW_TAG_subprogram))
8413 {
8414 if (grandparent_scope == NULL)
8415 parent->scope = parent->name (cu);
8416 else
8417 parent->scope = typename_concat (&cu->comp_unit_obstack,
8418 grandparent_scope,
8419 parent->name (cu), 0, cu);
8420 }
8421 else
8422 {
8423 /* FIXME drow/2004-04-01: What should we be doing with
8424 function-local names? For partial symbols, we should probably be
8425 ignoring them. */
8426 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8427 dwarf_tag_name (parent->tag),
8428 sect_offset_str (pdi->sect_off));
8429 parent->scope = grandparent_scope;
8430 }
8431
8432 parent->scope_set = 1;
8433 return parent->scope;
8434 }
8435
8436 /* Return the fully scoped name associated with PDI, from compilation unit
8437 CU. The result will be allocated with malloc. */
8438
8439 static gdb::unique_xmalloc_ptr<char>
8440 partial_die_full_name (struct partial_die_info *pdi,
8441 struct dwarf2_cu *cu)
8442 {
8443 const char *parent_scope;
8444
8445 /* If this is a template instantiation, we can not work out the
8446 template arguments from partial DIEs. So, unfortunately, we have
8447 to go through the full DIEs. At least any work we do building
8448 types here will be reused if full symbols are loaded later. */
8449 if (pdi->has_template_arguments)
8450 {
8451 pdi->fixup (cu);
8452
8453 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8454 {
8455 struct die_info *die;
8456 struct attribute attr;
8457 struct dwarf2_cu *ref_cu = cu;
8458
8459 /* DW_FORM_ref_addr is using section offset. */
8460 attr.name = (enum dwarf_attribute) 0;
8461 attr.form = DW_FORM_ref_addr;
8462 attr.u.unsnd = to_underlying (pdi->sect_off);
8463 die = follow_die_ref (NULL, &attr, &ref_cu);
8464
8465 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8466 }
8467 }
8468
8469 parent_scope = partial_die_parent_scope (pdi, cu);
8470 if (parent_scope == NULL)
8471 return NULL;
8472 else
8473 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8474 pdi->name (cu),
8475 0, cu));
8476 }
8477
8478 static void
8479 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8480 {
8481 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8482 struct objfile *objfile = per_objfile->objfile;
8483 struct gdbarch *gdbarch = objfile->arch ();
8484 CORE_ADDR addr = 0;
8485 const char *actual_name = NULL;
8486 CORE_ADDR baseaddr;
8487
8488 baseaddr = objfile->text_section_offset ();
8489
8490 gdb::unique_xmalloc_ptr<char> built_actual_name
8491 = partial_die_full_name (pdi, cu);
8492 if (built_actual_name != NULL)
8493 actual_name = built_actual_name.get ();
8494
8495 if (actual_name == NULL)
8496 actual_name = pdi->name (cu);
8497
8498 partial_symbol psymbol;
8499 memset (&psymbol, 0, sizeof (psymbol));
8500 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8501 psymbol.ginfo.set_section_index (-1);
8502
8503 /* The code below indicates that the psymbol should be installed by
8504 setting this. */
8505 gdb::optional<psymbol_placement> where;
8506
8507 switch (pdi->tag)
8508 {
8509 case DW_TAG_inlined_subroutine:
8510 case DW_TAG_subprogram:
8511 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8512 - baseaddr);
8513 if (pdi->is_external
8514 || cu->language == language_ada
8515 || (cu->language == language_fortran
8516 && pdi->die_parent != NULL
8517 && pdi->die_parent->tag == DW_TAG_subprogram))
8518 {
8519 /* Normally, only "external" DIEs are part of the global scope.
8520 But in Ada and Fortran, we want to be able to access nested
8521 procedures globally. So all Ada and Fortran subprograms are
8522 stored in the global scope. */
8523 where = psymbol_placement::GLOBAL;
8524 }
8525 else
8526 where = psymbol_placement::STATIC;
8527
8528 psymbol.domain = VAR_DOMAIN;
8529 psymbol.aclass = LOC_BLOCK;
8530 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8531 psymbol.ginfo.value.address = addr;
8532
8533 if (pdi->main_subprogram && actual_name != NULL)
8534 set_objfile_main_name (objfile, actual_name, cu->language);
8535 break;
8536 case DW_TAG_constant:
8537 psymbol.domain = VAR_DOMAIN;
8538 psymbol.aclass = LOC_STATIC;
8539 where = (pdi->is_external
8540 ? psymbol_placement::GLOBAL
8541 : psymbol_placement::STATIC);
8542 break;
8543 case DW_TAG_variable:
8544 if (pdi->d.locdesc)
8545 addr = decode_locdesc (pdi->d.locdesc, cu);
8546
8547 if (pdi->d.locdesc
8548 && addr == 0
8549 && !per_objfile->per_bfd->has_section_at_zero)
8550 {
8551 /* A global or static variable may also have been stripped
8552 out by the linker if unused, in which case its address
8553 will be nullified; do not add such variables into partial
8554 symbol table then. */
8555 }
8556 else if (pdi->is_external)
8557 {
8558 /* Global Variable.
8559 Don't enter into the minimal symbol tables as there is
8560 a minimal symbol table entry from the ELF symbols already.
8561 Enter into partial symbol table if it has a location
8562 descriptor or a type.
8563 If the location descriptor is missing, new_symbol will create
8564 a LOC_UNRESOLVED symbol, the address of the variable will then
8565 be determined from the minimal symbol table whenever the variable
8566 is referenced.
8567 The address for the partial symbol table entry is not
8568 used by GDB, but it comes in handy for debugging partial symbol
8569 table building. */
8570
8571 if (pdi->d.locdesc || pdi->has_type)
8572 {
8573 psymbol.domain = VAR_DOMAIN;
8574 psymbol.aclass = LOC_STATIC;
8575 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8576 psymbol.ginfo.value.address = addr;
8577 where = psymbol_placement::GLOBAL;
8578 }
8579 }
8580 else
8581 {
8582 int has_loc = pdi->d.locdesc != NULL;
8583
8584 /* Static Variable. Skip symbols whose value we cannot know (those
8585 without location descriptors or constant values). */
8586 if (!has_loc && !pdi->has_const_value)
8587 return;
8588
8589 psymbol.domain = VAR_DOMAIN;
8590 psymbol.aclass = LOC_STATIC;
8591 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8592 if (has_loc)
8593 psymbol.ginfo.value.address = addr;
8594 where = psymbol_placement::STATIC;
8595 }
8596 break;
8597 case DW_TAG_array_type:
8598 case DW_TAG_typedef:
8599 case DW_TAG_base_type:
8600 case DW_TAG_subrange_type:
8601 psymbol.domain = VAR_DOMAIN;
8602 psymbol.aclass = LOC_TYPEDEF;
8603 where = psymbol_placement::STATIC;
8604 break;
8605 case DW_TAG_imported_declaration:
8606 case DW_TAG_namespace:
8607 psymbol.domain = VAR_DOMAIN;
8608 psymbol.aclass = LOC_TYPEDEF;
8609 where = psymbol_placement::GLOBAL;
8610 break;
8611 case DW_TAG_module:
8612 /* With Fortran 77 there might be a "BLOCK DATA" module
8613 available without any name. If so, we skip the module as it
8614 doesn't bring any value. */
8615 if (actual_name != nullptr)
8616 {
8617 psymbol.domain = MODULE_DOMAIN;
8618 psymbol.aclass = LOC_TYPEDEF;
8619 where = psymbol_placement::GLOBAL;
8620 }
8621 break;
8622 case DW_TAG_class_type:
8623 case DW_TAG_interface_type:
8624 case DW_TAG_structure_type:
8625 case DW_TAG_union_type:
8626 case DW_TAG_enumeration_type:
8627 /* Skip external references. The DWARF standard says in the section
8628 about "Structure, Union, and Class Type Entries": "An incomplete
8629 structure, union or class type is represented by a structure,
8630 union or class entry that does not have a byte size attribute
8631 and that has a DW_AT_declaration attribute." */
8632 if (!pdi->has_byte_size && pdi->is_declaration)
8633 return;
8634
8635 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8636 static vs. global. */
8637 psymbol.domain = STRUCT_DOMAIN;
8638 psymbol.aclass = LOC_TYPEDEF;
8639 where = (cu->language == language_cplus
8640 ? psymbol_placement::GLOBAL
8641 : psymbol_placement::STATIC);
8642 break;
8643 case DW_TAG_enumerator:
8644 psymbol.domain = VAR_DOMAIN;
8645 psymbol.aclass = LOC_CONST;
8646 where = (cu->language == language_cplus
8647 ? psymbol_placement::GLOBAL
8648 : psymbol_placement::STATIC);
8649 break;
8650 default:
8651 break;
8652 }
8653
8654 if (where.has_value ())
8655 {
8656 if (built_actual_name != nullptr)
8657 actual_name = objfile->intern (actual_name);
8658 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8659 psymbol.ginfo.set_linkage_name (actual_name);
8660 else
8661 {
8662 psymbol.ginfo.set_demangled_name (actual_name,
8663 &objfile->objfile_obstack);
8664 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8665 }
8666 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
8667 }
8668 }
8669
8670 /* Read a partial die corresponding to a namespace; also, add a symbol
8671 corresponding to that namespace to the symbol table. NAMESPACE is
8672 the name of the enclosing namespace. */
8673
8674 static void
8675 add_partial_namespace (struct partial_die_info *pdi,
8676 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8677 int set_addrmap, struct dwarf2_cu *cu)
8678 {
8679 /* Add a symbol for the namespace. */
8680
8681 add_partial_symbol (pdi, cu);
8682
8683 /* Now scan partial symbols in that namespace. */
8684
8685 if (pdi->has_children)
8686 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8687 }
8688
8689 /* Read a partial die corresponding to a Fortran module. */
8690
8691 static void
8692 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8693 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8694 {
8695 /* Add a symbol for the namespace. */
8696
8697 add_partial_symbol (pdi, cu);
8698
8699 /* Now scan partial symbols in that module. */
8700
8701 if (pdi->has_children)
8702 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8703 }
8704
8705 /* Read a partial die corresponding to a subprogram or an inlined
8706 subprogram and create a partial symbol for that subprogram.
8707 When the CU language allows it, this routine also defines a partial
8708 symbol for each nested subprogram that this subprogram contains.
8709 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8710 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8711
8712 PDI may also be a lexical block, in which case we simply search
8713 recursively for subprograms defined inside that lexical block.
8714 Again, this is only performed when the CU language allows this
8715 type of definitions. */
8716
8717 static void
8718 add_partial_subprogram (struct partial_die_info *pdi,
8719 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8720 int set_addrmap, struct dwarf2_cu *cu)
8721 {
8722 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8723 {
8724 if (pdi->has_pc_info)
8725 {
8726 if (pdi->lowpc < *lowpc)
8727 *lowpc = pdi->lowpc;
8728 if (pdi->highpc > *highpc)
8729 *highpc = pdi->highpc;
8730 if (set_addrmap)
8731 {
8732 struct objfile *objfile = cu->per_objfile->objfile;
8733 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8734 struct gdbarch *gdbarch = objfile->arch ();
8735 CORE_ADDR baseaddr;
8736 CORE_ADDR this_highpc;
8737 CORE_ADDR this_lowpc;
8738
8739 baseaddr = objfile->text_section_offset ();
8740 this_lowpc
8741 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8742 pdi->lowpc + baseaddr)
8743 - baseaddr);
8744 this_highpc
8745 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8746 pdi->highpc + baseaddr)
8747 - baseaddr);
8748 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8749 this_lowpc, this_highpc - 1,
8750 cu->per_cu->v.psymtab);
8751 }
8752 }
8753
8754 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8755 {
8756 if (!pdi->is_declaration)
8757 /* Ignore subprogram DIEs that do not have a name, they are
8758 illegal. Do not emit a complaint at this point, we will
8759 do so when we convert this psymtab into a symtab. */
8760 if (pdi->name (cu))
8761 add_partial_symbol (pdi, cu);
8762 }
8763 }
8764
8765 if (! pdi->has_children)
8766 return;
8767
8768 if (cu->language == language_ada || cu->language == language_fortran)
8769 {
8770 pdi = pdi->die_child;
8771 while (pdi != NULL)
8772 {
8773 pdi->fixup (cu);
8774 if (pdi->tag == DW_TAG_subprogram
8775 || pdi->tag == DW_TAG_inlined_subroutine
8776 || pdi->tag == DW_TAG_lexical_block)
8777 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8778 pdi = pdi->die_sibling;
8779 }
8780 }
8781 }
8782
8783 /* Read a partial die corresponding to an enumeration type. */
8784
8785 static void
8786 add_partial_enumeration (struct partial_die_info *enum_pdi,
8787 struct dwarf2_cu *cu)
8788 {
8789 struct partial_die_info *pdi;
8790
8791 if (enum_pdi->name (cu) != NULL)
8792 add_partial_symbol (enum_pdi, cu);
8793
8794 pdi = enum_pdi->die_child;
8795 while (pdi)
8796 {
8797 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8798 complaint (_("malformed enumerator DIE ignored"));
8799 else
8800 add_partial_symbol (pdi, cu);
8801 pdi = pdi->die_sibling;
8802 }
8803 }
8804
8805 /* Return the initial uleb128 in the die at INFO_PTR. */
8806
8807 static unsigned int
8808 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8809 {
8810 unsigned int bytes_read;
8811
8812 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8813 }
8814
8815 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8816 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8817
8818 Return the corresponding abbrev, or NULL if the number is zero (indicating
8819 an empty DIE). In either case *BYTES_READ will be set to the length of
8820 the initial number. */
8821
8822 static const struct abbrev_info *
8823 peek_die_abbrev (const die_reader_specs &reader,
8824 const gdb_byte *info_ptr, unsigned int *bytes_read)
8825 {
8826 dwarf2_cu *cu = reader.cu;
8827 bfd *abfd = reader.abfd;
8828 unsigned int abbrev_number
8829 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8830
8831 if (abbrev_number == 0)
8832 return NULL;
8833
8834 const abbrev_info *abbrev
8835 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8836 if (!abbrev)
8837 {
8838 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8839 " at offset %s [in module %s]"),
8840 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8841 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8842 }
8843
8844 return abbrev;
8845 }
8846
8847 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8848 Returns a pointer to the end of a series of DIEs, terminated by an empty
8849 DIE. Any children of the skipped DIEs will also be skipped. */
8850
8851 static const gdb_byte *
8852 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8853 {
8854 while (1)
8855 {
8856 unsigned int bytes_read;
8857 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8858 &bytes_read);
8859
8860 if (abbrev == NULL)
8861 return info_ptr + bytes_read;
8862 else
8863 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8864 }
8865 }
8866
8867 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8868 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8869 abbrev corresponding to that skipped uleb128 should be passed in
8870 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8871 children. */
8872
8873 static const gdb_byte *
8874 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8875 const struct abbrev_info *abbrev)
8876 {
8877 unsigned int bytes_read;
8878 struct attribute attr;
8879 bfd *abfd = reader->abfd;
8880 struct dwarf2_cu *cu = reader->cu;
8881 const gdb_byte *buffer = reader->buffer;
8882 const gdb_byte *buffer_end = reader->buffer_end;
8883 unsigned int form, i;
8884
8885 for (i = 0; i < abbrev->num_attrs; i++)
8886 {
8887 /* The only abbrev we care about is DW_AT_sibling. */
8888 if (abbrev->attrs[i].name == DW_AT_sibling)
8889 {
8890 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8891 if (attr.form == DW_FORM_ref_addr)
8892 complaint (_("ignoring absolute DW_AT_sibling"));
8893 else
8894 {
8895 sect_offset off = attr.get_ref_die_offset ();
8896 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8897
8898 if (sibling_ptr < info_ptr)
8899 complaint (_("DW_AT_sibling points backwards"));
8900 else if (sibling_ptr > reader->buffer_end)
8901 reader->die_section->overflow_complaint ();
8902 else
8903 return sibling_ptr;
8904 }
8905 }
8906
8907 /* If it isn't DW_AT_sibling, skip this attribute. */
8908 form = abbrev->attrs[i].form;
8909 skip_attribute:
8910 switch (form)
8911 {
8912 case DW_FORM_ref_addr:
8913 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8914 and later it is offset sized. */
8915 if (cu->header.version == 2)
8916 info_ptr += cu->header.addr_size;
8917 else
8918 info_ptr += cu->header.offset_size;
8919 break;
8920 case DW_FORM_GNU_ref_alt:
8921 info_ptr += cu->header.offset_size;
8922 break;
8923 case DW_FORM_addr:
8924 info_ptr += cu->header.addr_size;
8925 break;
8926 case DW_FORM_data1:
8927 case DW_FORM_ref1:
8928 case DW_FORM_flag:
8929 case DW_FORM_strx1:
8930 info_ptr += 1;
8931 break;
8932 case DW_FORM_flag_present:
8933 case DW_FORM_implicit_const:
8934 break;
8935 case DW_FORM_data2:
8936 case DW_FORM_ref2:
8937 case DW_FORM_strx2:
8938 info_ptr += 2;
8939 break;
8940 case DW_FORM_strx3:
8941 info_ptr += 3;
8942 break;
8943 case DW_FORM_data4:
8944 case DW_FORM_ref4:
8945 case DW_FORM_strx4:
8946 info_ptr += 4;
8947 break;
8948 case DW_FORM_data8:
8949 case DW_FORM_ref8:
8950 case DW_FORM_ref_sig8:
8951 info_ptr += 8;
8952 break;
8953 case DW_FORM_data16:
8954 info_ptr += 16;
8955 break;
8956 case DW_FORM_string:
8957 read_direct_string (abfd, info_ptr, &bytes_read);
8958 info_ptr += bytes_read;
8959 break;
8960 case DW_FORM_sec_offset:
8961 case DW_FORM_strp:
8962 case DW_FORM_GNU_strp_alt:
8963 info_ptr += cu->header.offset_size;
8964 break;
8965 case DW_FORM_exprloc:
8966 case DW_FORM_block:
8967 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8968 info_ptr += bytes_read;
8969 break;
8970 case DW_FORM_block1:
8971 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8972 break;
8973 case DW_FORM_block2:
8974 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8975 break;
8976 case DW_FORM_block4:
8977 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8978 break;
8979 case DW_FORM_addrx:
8980 case DW_FORM_strx:
8981 case DW_FORM_sdata:
8982 case DW_FORM_udata:
8983 case DW_FORM_ref_udata:
8984 case DW_FORM_GNU_addr_index:
8985 case DW_FORM_GNU_str_index:
8986 case DW_FORM_rnglistx:
8987 case DW_FORM_loclistx:
8988 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8989 break;
8990 case DW_FORM_indirect:
8991 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8992 info_ptr += bytes_read;
8993 /* We need to continue parsing from here, so just go back to
8994 the top. */
8995 goto skip_attribute;
8996
8997 default:
8998 error (_("Dwarf Error: Cannot handle %s "
8999 "in DWARF reader [in module %s]"),
9000 dwarf_form_name (form),
9001 bfd_get_filename (abfd));
9002 }
9003 }
9004
9005 if (abbrev->has_children)
9006 return skip_children (reader, info_ptr);
9007 else
9008 return info_ptr;
9009 }
9010
9011 /* Locate ORIG_PDI's sibling.
9012 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9013
9014 static const gdb_byte *
9015 locate_pdi_sibling (const struct die_reader_specs *reader,
9016 struct partial_die_info *orig_pdi,
9017 const gdb_byte *info_ptr)
9018 {
9019 /* Do we know the sibling already? */
9020
9021 if (orig_pdi->sibling)
9022 return orig_pdi->sibling;
9023
9024 /* Are there any children to deal with? */
9025
9026 if (!orig_pdi->has_children)
9027 return info_ptr;
9028
9029 /* Skip the children the long way. */
9030
9031 return skip_children (reader, info_ptr);
9032 }
9033
9034 /* Expand this partial symbol table into a full symbol table. SELF is
9035 not NULL. */
9036
9037 void
9038 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9039 {
9040 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9041
9042 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9043
9044 /* If this psymtab is constructed from a debug-only objfile, the
9045 has_section_at_zero flag will not necessarily be correct. We
9046 can get the correct value for this flag by looking at the data
9047 associated with the (presumably stripped) associated objfile. */
9048 if (objfile->separate_debug_objfile_backlink)
9049 {
9050 dwarf2_per_objfile *per_objfile_backlink
9051 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9052
9053 per_objfile->per_bfd->has_section_at_zero
9054 = per_objfile_backlink->per_bfd->has_section_at_zero;
9055 }
9056
9057 expand_psymtab (objfile);
9058
9059 process_cu_includes (per_objfile);
9060 }
9061 \f
9062 /* Reading in full CUs. */
9063
9064 /* Add PER_CU to the queue. */
9065
9066 static void
9067 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9068 dwarf2_per_objfile *per_objfile,
9069 enum language pretend_language)
9070 {
9071 per_cu->queued = 1;
9072
9073 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9074 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
9075 }
9076
9077 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9078
9079 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9080 dependency.
9081
9082 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9083 DIEs, false otherwise.
9084
9085 Explanation: there is an invariant that if a CU is queued for expansion
9086 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9087 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9088 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9089 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9090 invariant is respected.
9091
9092 The caller is therefore not required to load the CU's DIEs (we return false)
9093 if:
9094
9095 - the CU is already expanded, and therefore does not get enqueued
9096 - the CU gets enqueued for expansion, but its DIEs are already loaded
9097
9098 Note that the caller should not use this function's return value as an
9099 indicator of whether the CU's DIEs are loaded right now, it should check
9100 that by calling `dwarf2_per_objfile::get_cu` instead. */
9101
9102 static int
9103 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9104 dwarf2_per_cu_data *per_cu,
9105 dwarf2_per_objfile *per_objfile,
9106 enum language pretend_language)
9107 {
9108 /* We may arrive here during partial symbol reading, if we need full
9109 DIEs to process an unusual case (e.g. template arguments). Do
9110 not queue PER_CU, just tell our caller to load its DIEs. */
9111 if (per_cu->per_bfd->reading_partial_symbols)
9112 {
9113 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9114
9115 if (cu == NULL || cu->dies == NULL)
9116 return 1;
9117 return 0;
9118 }
9119
9120 /* Mark the dependence relation so that we don't flush PER_CU
9121 too early. */
9122 if (dependent_cu != NULL)
9123 dwarf2_add_dependence (dependent_cu, per_cu);
9124
9125 /* If it's already on the queue, we have nothing to do. */
9126 if (per_cu->queued)
9127 {
9128 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9129 loaded. */
9130 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9131
9132 /* If the CU is queued for expansion, it should not already be
9133 expanded. */
9134 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9135
9136 /* The DIEs are already loaded, the caller doesn't need to do it. */
9137 return 0;
9138 }
9139
9140 bool queued = false;
9141 if (!per_objfile->symtab_set_p (per_cu))
9142 {
9143 /* Add it to the queue. */
9144 queue_comp_unit (per_cu, per_objfile, pretend_language);
9145 queued = true;
9146 }
9147
9148 /* If the compilation unit is already loaded, just mark it as
9149 used. */
9150 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9151 if (cu != nullptr)
9152 cu->last_used = 0;
9153
9154 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9155 and the DIEs are not already loaded. */
9156 return queued && cu == nullptr;
9157 }
9158
9159 /* Process the queue. */
9160
9161 static void
9162 process_queue (dwarf2_per_objfile *per_objfile)
9163 {
9164 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9165 objfile_name (per_objfile->objfile));
9166
9167 /* The queue starts out with one item, but following a DIE reference
9168 may load a new CU, adding it to the end of the queue. */
9169 while (!per_objfile->per_bfd->queue->empty ())
9170 {
9171 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
9172 dwarf2_per_cu_data *per_cu = item.per_cu;
9173
9174 if (!per_objfile->symtab_set_p (per_cu))
9175 {
9176 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9177
9178 /* Skip dummy CUs. */
9179 if (cu != nullptr)
9180 {
9181 unsigned int debug_print_threshold;
9182 char buf[100];
9183
9184 if (per_cu->is_debug_types)
9185 {
9186 struct signatured_type *sig_type =
9187 (struct signatured_type *) per_cu;
9188
9189 sprintf (buf, "TU %s at offset %s",
9190 hex_string (sig_type->signature),
9191 sect_offset_str (per_cu->sect_off));
9192 /* There can be 100s of TUs.
9193 Only print them in verbose mode. */
9194 debug_print_threshold = 2;
9195 }
9196 else
9197 {
9198 sprintf (buf, "CU at offset %s",
9199 sect_offset_str (per_cu->sect_off));
9200 debug_print_threshold = 1;
9201 }
9202
9203 if (dwarf_read_debug >= debug_print_threshold)
9204 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9205
9206 if (per_cu->is_debug_types)
9207 process_full_type_unit (cu, item.pretend_language);
9208 else
9209 process_full_comp_unit (cu, item.pretend_language);
9210
9211 if (dwarf_read_debug >= debug_print_threshold)
9212 dwarf_read_debug_printf ("Done expanding %s", buf);
9213 }
9214 }
9215
9216 per_cu->queued = 0;
9217 per_objfile->per_bfd->queue->pop ();
9218 }
9219
9220 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9221 objfile_name (per_objfile->objfile));
9222 }
9223
9224 /* Read in full symbols for PST, and anything it depends on. */
9225
9226 void
9227 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9228 {
9229 gdb_assert (!readin_p (objfile));
9230
9231 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9232 free_cached_comp_units freer (per_objfile);
9233 expand_dependencies (objfile);
9234
9235 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9236 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9237 }
9238
9239 /* See psympriv.h. */
9240
9241 bool
9242 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9243 {
9244 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9245 return per_objfile->symtab_set_p (per_cu_data);
9246 }
9247
9248 /* See psympriv.h. */
9249
9250 compunit_symtab *
9251 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9252 {
9253 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9254 return per_objfile->get_symtab (per_cu_data);
9255 }
9256
9257 /* Trivial hash function for die_info: the hash value of a DIE
9258 is its offset in .debug_info for this objfile. */
9259
9260 static hashval_t
9261 die_hash (const void *item)
9262 {
9263 const struct die_info *die = (const struct die_info *) item;
9264
9265 return to_underlying (die->sect_off);
9266 }
9267
9268 /* Trivial comparison function for die_info structures: two DIEs
9269 are equal if they have the same offset. */
9270
9271 static int
9272 die_eq (const void *item_lhs, const void *item_rhs)
9273 {
9274 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9275 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9276
9277 return die_lhs->sect_off == die_rhs->sect_off;
9278 }
9279
9280 /* Load the DIEs associated with PER_CU into memory.
9281
9282 In some cases, the caller, while reading partial symbols, will need to load
9283 the full symbols for the CU for some reason. It will already have a
9284 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9285 rather than creating a new one. */
9286
9287 static void
9288 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9289 dwarf2_per_objfile *per_objfile,
9290 dwarf2_cu *existing_cu,
9291 bool skip_partial,
9292 enum language pretend_language)
9293 {
9294 gdb_assert (! this_cu->is_debug_types);
9295
9296 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9297 if (reader.dummy_p)
9298 return;
9299
9300 struct dwarf2_cu *cu = reader.cu;
9301 const gdb_byte *info_ptr = reader.info_ptr;
9302
9303 gdb_assert (cu->die_hash == NULL);
9304 cu->die_hash =
9305 htab_create_alloc_ex (cu->header.length / 12,
9306 die_hash,
9307 die_eq,
9308 NULL,
9309 &cu->comp_unit_obstack,
9310 hashtab_obstack_allocate,
9311 dummy_obstack_deallocate);
9312
9313 if (reader.comp_unit_die->has_children)
9314 reader.comp_unit_die->child
9315 = read_die_and_siblings (&reader, reader.info_ptr,
9316 &info_ptr, reader.comp_unit_die);
9317 cu->dies = reader.comp_unit_die;
9318 /* comp_unit_die is not stored in die_hash, no need. */
9319
9320 /* We try not to read any attributes in this function, because not
9321 all CUs needed for references have been loaded yet, and symbol
9322 table processing isn't initialized. But we have to set the CU language,
9323 or we won't be able to build types correctly.
9324 Similarly, if we do not read the producer, we can not apply
9325 producer-specific interpretation. */
9326 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9327
9328 reader.keep ();
9329 }
9330
9331 /* Add a DIE to the delayed physname list. */
9332
9333 static void
9334 add_to_method_list (struct type *type, int fnfield_index, int index,
9335 const char *name, struct die_info *die,
9336 struct dwarf2_cu *cu)
9337 {
9338 struct delayed_method_info mi;
9339 mi.type = type;
9340 mi.fnfield_index = fnfield_index;
9341 mi.index = index;
9342 mi.name = name;
9343 mi.die = die;
9344 cu->method_list.push_back (mi);
9345 }
9346
9347 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9348 "const" / "volatile". If so, decrements LEN by the length of the
9349 modifier and return true. Otherwise return false. */
9350
9351 template<size_t N>
9352 static bool
9353 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9354 {
9355 size_t mod_len = sizeof (mod) - 1;
9356 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9357 {
9358 len -= mod_len;
9359 return true;
9360 }
9361 return false;
9362 }
9363
9364 /* Compute the physnames of any methods on the CU's method list.
9365
9366 The computation of method physnames is delayed in order to avoid the
9367 (bad) condition that one of the method's formal parameters is of an as yet
9368 incomplete type. */
9369
9370 static void
9371 compute_delayed_physnames (struct dwarf2_cu *cu)
9372 {
9373 /* Only C++ delays computing physnames. */
9374 if (cu->method_list.empty ())
9375 return;
9376 gdb_assert (cu->language == language_cplus);
9377
9378 for (const delayed_method_info &mi : cu->method_list)
9379 {
9380 const char *physname;
9381 struct fn_fieldlist *fn_flp
9382 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9383 physname = dwarf2_physname (mi.name, mi.die, cu);
9384 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9385 = physname ? physname : "";
9386
9387 /* Since there's no tag to indicate whether a method is a
9388 const/volatile overload, extract that information out of the
9389 demangled name. */
9390 if (physname != NULL)
9391 {
9392 size_t len = strlen (physname);
9393
9394 while (1)
9395 {
9396 if (physname[len] == ')') /* shortcut */
9397 break;
9398 else if (check_modifier (physname, len, " const"))
9399 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9400 else if (check_modifier (physname, len, " volatile"))
9401 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9402 else
9403 break;
9404 }
9405 }
9406 }
9407
9408 /* The list is no longer needed. */
9409 cu->method_list.clear ();
9410 }
9411
9412 /* Go objects should be embedded in a DW_TAG_module DIE,
9413 and it's not clear if/how imported objects will appear.
9414 To keep Go support simple until that's worked out,
9415 go back through what we've read and create something usable.
9416 We could do this while processing each DIE, and feels kinda cleaner,
9417 but that way is more invasive.
9418 This is to, for example, allow the user to type "p var" or "b main"
9419 without having to specify the package name, and allow lookups
9420 of module.object to work in contexts that use the expression
9421 parser. */
9422
9423 static void
9424 fixup_go_packaging (struct dwarf2_cu *cu)
9425 {
9426 gdb::unique_xmalloc_ptr<char> package_name;
9427 struct pending *list;
9428 int i;
9429
9430 for (list = *cu->get_builder ()->get_global_symbols ();
9431 list != NULL;
9432 list = list->next)
9433 {
9434 for (i = 0; i < list->nsyms; ++i)
9435 {
9436 struct symbol *sym = list->symbol[i];
9437
9438 if (sym->language () == language_go
9439 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9440 {
9441 gdb::unique_xmalloc_ptr<char> this_package_name
9442 (go_symbol_package_name (sym));
9443
9444 if (this_package_name == NULL)
9445 continue;
9446 if (package_name == NULL)
9447 package_name = std::move (this_package_name);
9448 else
9449 {
9450 struct objfile *objfile = cu->per_objfile->objfile;
9451 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9452 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9453 (symbol_symtab (sym) != NULL
9454 ? symtab_to_filename_for_display
9455 (symbol_symtab (sym))
9456 : objfile_name (objfile)),
9457 this_package_name.get (), package_name.get ());
9458 }
9459 }
9460 }
9461 }
9462
9463 if (package_name != NULL)
9464 {
9465 struct objfile *objfile = cu->per_objfile->objfile;
9466 const char *saved_package_name = objfile->intern (package_name.get ());
9467 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9468 saved_package_name);
9469 struct symbol *sym;
9470
9471 sym = new (&objfile->objfile_obstack) symbol;
9472 sym->set_language (language_go, &objfile->objfile_obstack);
9473 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9474 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9475 e.g., "main" finds the "main" module and not C's main(). */
9476 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9477 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9478 SYMBOL_TYPE (sym) = type;
9479
9480 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9481 }
9482 }
9483
9484 /* Allocate a fully-qualified name consisting of the two parts on the
9485 obstack. */
9486
9487 static const char *
9488 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9489 {
9490 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9491 }
9492
9493 /* A helper that allocates a variant part to attach to a Rust enum
9494 type. OBSTACK is where the results should be allocated. TYPE is
9495 the type we're processing. DISCRIMINANT_INDEX is the index of the
9496 discriminant. It must be the index of one of the fields of TYPE,
9497 or -1 to mean there is no discriminant (univariant enum).
9498 DEFAULT_INDEX is the index of the default field; or -1 if there is
9499 no default. RANGES is indexed by "effective" field number (the
9500 field index, but omitting the discriminant and default fields) and
9501 must hold the discriminant values used by the variants. Note that
9502 RANGES must have a lifetime at least as long as OBSTACK -- either
9503 already allocated on it, or static. */
9504
9505 static void
9506 alloc_rust_variant (struct obstack *obstack, struct type *type,
9507 int discriminant_index, int default_index,
9508 gdb::array_view<discriminant_range> ranges)
9509 {
9510 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9511 gdb_assert (discriminant_index == -1
9512 || (discriminant_index >= 0
9513 && discriminant_index < type->num_fields ()));
9514 gdb_assert (default_index == -1
9515 || (default_index >= 0 && default_index < type->num_fields ()));
9516
9517 /* We have one variant for each non-discriminant field. */
9518 int n_variants = type->num_fields ();
9519 if (discriminant_index != -1)
9520 --n_variants;
9521
9522 variant *variants = new (obstack) variant[n_variants];
9523 int var_idx = 0;
9524 int range_idx = 0;
9525 for (int i = 0; i < type->num_fields (); ++i)
9526 {
9527 if (i == discriminant_index)
9528 continue;
9529
9530 variants[var_idx].first_field = i;
9531 variants[var_idx].last_field = i + 1;
9532
9533 /* The default field does not need a range, but other fields do.
9534 We skipped the discriminant above. */
9535 if (i != default_index)
9536 {
9537 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9538 ++range_idx;
9539 }
9540
9541 ++var_idx;
9542 }
9543
9544 gdb_assert (range_idx == ranges.size ());
9545 gdb_assert (var_idx == n_variants);
9546
9547 variant_part *part = new (obstack) variant_part;
9548 part->discriminant_index = discriminant_index;
9549 /* If there is no discriminant, then whether it is signed is of no
9550 consequence. */
9551 part->is_unsigned
9552 = (discriminant_index == -1
9553 ? false
9554 : type->field (discriminant_index).type ()->is_unsigned ());
9555 part->variants = gdb::array_view<variant> (variants, n_variants);
9556
9557 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9558 gdb::array_view<variant_part> *prop_value
9559 = new (storage) gdb::array_view<variant_part> (part, 1);
9560
9561 struct dynamic_prop prop;
9562 prop.set_variant_parts (prop_value);
9563
9564 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9565 }
9566
9567 /* Some versions of rustc emitted enums in an unusual way.
9568
9569 Ordinary enums were emitted as unions. The first element of each
9570 structure in the union was named "RUST$ENUM$DISR". This element
9571 held the discriminant.
9572
9573 These versions of Rust also implemented the "non-zero"
9574 optimization. When the enum had two values, and one is empty and
9575 the other holds a pointer that cannot be zero, the pointer is used
9576 as the discriminant, with a zero value meaning the empty variant.
9577 Here, the union's first member is of the form
9578 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9579 where the fieldnos are the indices of the fields that should be
9580 traversed in order to find the field (which may be several fields deep)
9581 and the variantname is the name of the variant of the case when the
9582 field is zero.
9583
9584 This function recognizes whether TYPE is of one of these forms,
9585 and, if so, smashes it to be a variant type. */
9586
9587 static void
9588 quirk_rust_enum (struct type *type, struct objfile *objfile)
9589 {
9590 gdb_assert (type->code () == TYPE_CODE_UNION);
9591
9592 /* We don't need to deal with empty enums. */
9593 if (type->num_fields () == 0)
9594 return;
9595
9596 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9597 if (type->num_fields () == 1
9598 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9599 {
9600 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9601
9602 /* Decode the field name to find the offset of the
9603 discriminant. */
9604 ULONGEST bit_offset = 0;
9605 struct type *field_type = type->field (0).type ();
9606 while (name[0] >= '0' && name[0] <= '9')
9607 {
9608 char *tail;
9609 unsigned long index = strtoul (name, &tail, 10);
9610 name = tail;
9611 if (*name != '$'
9612 || index >= field_type->num_fields ()
9613 || (TYPE_FIELD_LOC_KIND (field_type, index)
9614 != FIELD_LOC_KIND_BITPOS))
9615 {
9616 complaint (_("Could not parse Rust enum encoding string \"%s\""
9617 "[in module %s]"),
9618 TYPE_FIELD_NAME (type, 0),
9619 objfile_name (objfile));
9620 return;
9621 }
9622 ++name;
9623
9624 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9625 field_type = field_type->field (index).type ();
9626 }
9627
9628 /* Smash this type to be a structure type. We have to do this
9629 because the type has already been recorded. */
9630 type->set_code (TYPE_CODE_STRUCT);
9631 type->set_num_fields (3);
9632 /* Save the field we care about. */
9633 struct field saved_field = type->field (0);
9634 type->set_fields
9635 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9636
9637 /* Put the discriminant at index 0. */
9638 type->field (0).set_type (field_type);
9639 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9640 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9641 SET_FIELD_BITPOS (type->field (0), bit_offset);
9642
9643 /* The order of fields doesn't really matter, so put the real
9644 field at index 1 and the data-less field at index 2. */
9645 type->field (1) = saved_field;
9646 TYPE_FIELD_NAME (type, 1)
9647 = rust_last_path_segment (type->field (1).type ()->name ());
9648 type->field (1).type ()->set_name
9649 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9650 TYPE_FIELD_NAME (type, 1)));
9651
9652 const char *dataless_name
9653 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9654 name);
9655 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9656 dataless_name);
9657 type->field (2).set_type (dataless_type);
9658 /* NAME points into the original discriminant name, which
9659 already has the correct lifetime. */
9660 TYPE_FIELD_NAME (type, 2) = name;
9661 SET_FIELD_BITPOS (type->field (2), 0);
9662
9663 /* Indicate that this is a variant type. */
9664 static discriminant_range ranges[1] = { { 0, 0 } };
9665 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9666 }
9667 /* A union with a single anonymous field is probably an old-style
9668 univariant enum. */
9669 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9670 {
9671 /* Smash this type to be a structure type. We have to do this
9672 because the type has already been recorded. */
9673 type->set_code (TYPE_CODE_STRUCT);
9674
9675 struct type *field_type = type->field (0).type ();
9676 const char *variant_name
9677 = rust_last_path_segment (field_type->name ());
9678 TYPE_FIELD_NAME (type, 0) = variant_name;
9679 field_type->set_name
9680 (rust_fully_qualify (&objfile->objfile_obstack,
9681 type->name (), variant_name));
9682
9683 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9684 }
9685 else
9686 {
9687 struct type *disr_type = nullptr;
9688 for (int i = 0; i < type->num_fields (); ++i)
9689 {
9690 disr_type = type->field (i).type ();
9691
9692 if (disr_type->code () != TYPE_CODE_STRUCT)
9693 {
9694 /* All fields of a true enum will be structs. */
9695 return;
9696 }
9697 else if (disr_type->num_fields () == 0)
9698 {
9699 /* Could be data-less variant, so keep going. */
9700 disr_type = nullptr;
9701 }
9702 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9703 "RUST$ENUM$DISR") != 0)
9704 {
9705 /* Not a Rust enum. */
9706 return;
9707 }
9708 else
9709 {
9710 /* Found one. */
9711 break;
9712 }
9713 }
9714
9715 /* If we got here without a discriminant, then it's probably
9716 just a union. */
9717 if (disr_type == nullptr)
9718 return;
9719
9720 /* Smash this type to be a structure type. We have to do this
9721 because the type has already been recorded. */
9722 type->set_code (TYPE_CODE_STRUCT);
9723
9724 /* Make space for the discriminant field. */
9725 struct field *disr_field = &disr_type->field (0);
9726 field *new_fields
9727 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9728 * sizeof (struct field)));
9729 memcpy (new_fields + 1, type->fields (),
9730 type->num_fields () * sizeof (struct field));
9731 type->set_fields (new_fields);
9732 type->set_num_fields (type->num_fields () + 1);
9733
9734 /* Install the discriminant at index 0 in the union. */
9735 type->field (0) = *disr_field;
9736 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9737 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9738
9739 /* We need a way to find the correct discriminant given a
9740 variant name. For convenience we build a map here. */
9741 struct type *enum_type = disr_field->type ();
9742 std::unordered_map<std::string, ULONGEST> discriminant_map;
9743 for (int i = 0; i < enum_type->num_fields (); ++i)
9744 {
9745 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9746 {
9747 const char *name
9748 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9749 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9750 }
9751 }
9752
9753 int n_fields = type->num_fields ();
9754 /* We don't need a range entry for the discriminant, but we do
9755 need one for every other field, as there is no default
9756 variant. */
9757 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9758 discriminant_range,
9759 n_fields - 1);
9760 /* Skip the discriminant here. */
9761 for (int i = 1; i < n_fields; ++i)
9762 {
9763 /* Find the final word in the name of this variant's type.
9764 That name can be used to look up the correct
9765 discriminant. */
9766 const char *variant_name
9767 = rust_last_path_segment (type->field (i).type ()->name ());
9768
9769 auto iter = discriminant_map.find (variant_name);
9770 if (iter != discriminant_map.end ())
9771 {
9772 ranges[i - 1].low = iter->second;
9773 ranges[i - 1].high = iter->second;
9774 }
9775
9776 /* In Rust, each element should have the size of the
9777 enclosing enum. */
9778 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9779
9780 /* Remove the discriminant field, if it exists. */
9781 struct type *sub_type = type->field (i).type ();
9782 if (sub_type->num_fields () > 0)
9783 {
9784 sub_type->set_num_fields (sub_type->num_fields () - 1);
9785 sub_type->set_fields (sub_type->fields () + 1);
9786 }
9787 TYPE_FIELD_NAME (type, i) = variant_name;
9788 sub_type->set_name
9789 (rust_fully_qualify (&objfile->objfile_obstack,
9790 type->name (), variant_name));
9791 }
9792
9793 /* Indicate that this is a variant type. */
9794 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9795 gdb::array_view<discriminant_range> (ranges,
9796 n_fields - 1));
9797 }
9798 }
9799
9800 /* Rewrite some Rust unions to be structures with variants parts. */
9801
9802 static void
9803 rust_union_quirks (struct dwarf2_cu *cu)
9804 {
9805 gdb_assert (cu->language == language_rust);
9806 for (type *type_ : cu->rust_unions)
9807 quirk_rust_enum (type_, cu->per_objfile->objfile);
9808 /* We don't need this any more. */
9809 cu->rust_unions.clear ();
9810 }
9811
9812 /* See read.h. */
9813
9814 type_unit_group_unshareable *
9815 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9816 {
9817 auto iter = this->m_type_units.find (tu_group);
9818 if (iter != this->m_type_units.end ())
9819 return iter->second.get ();
9820
9821 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9822 type_unit_group_unshareable *result = uniq.get ();
9823 this->m_type_units[tu_group] = std::move (uniq);
9824 return result;
9825 }
9826
9827 struct type *
9828 dwarf2_per_objfile::get_type_for_signatured_type
9829 (signatured_type *sig_type) const
9830 {
9831 auto iter = this->m_type_map.find (sig_type);
9832 if (iter == this->m_type_map.end ())
9833 return nullptr;
9834
9835 return iter->second;
9836 }
9837
9838 void dwarf2_per_objfile::set_type_for_signatured_type
9839 (signatured_type *sig_type, struct type *type)
9840 {
9841 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9842
9843 this->m_type_map[sig_type] = type;
9844 }
9845
9846 /* A helper function for computing the list of all symbol tables
9847 included by PER_CU. */
9848
9849 static void
9850 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9851 htab_t all_children, htab_t all_type_symtabs,
9852 dwarf2_per_cu_data *per_cu,
9853 dwarf2_per_objfile *per_objfile,
9854 struct compunit_symtab *immediate_parent)
9855 {
9856 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9857 if (*slot != NULL)
9858 {
9859 /* This inclusion and its children have been processed. */
9860 return;
9861 }
9862
9863 *slot = per_cu;
9864
9865 /* Only add a CU if it has a symbol table. */
9866 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9867 if (cust != NULL)
9868 {
9869 /* If this is a type unit only add its symbol table if we haven't
9870 seen it yet (type unit per_cu's can share symtabs). */
9871 if (per_cu->is_debug_types)
9872 {
9873 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9874 if (*slot == NULL)
9875 {
9876 *slot = cust;
9877 result->push_back (cust);
9878 if (cust->user == NULL)
9879 cust->user = immediate_parent;
9880 }
9881 }
9882 else
9883 {
9884 result->push_back (cust);
9885 if (cust->user == NULL)
9886 cust->user = immediate_parent;
9887 }
9888 }
9889
9890 if (!per_cu->imported_symtabs_empty ())
9891 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9892 {
9893 recursively_compute_inclusions (result, all_children,
9894 all_type_symtabs, ptr, per_objfile,
9895 cust);
9896 }
9897 }
9898
9899 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9900 PER_CU. */
9901
9902 static void
9903 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9904 dwarf2_per_objfile *per_objfile)
9905 {
9906 gdb_assert (! per_cu->is_debug_types);
9907
9908 if (!per_cu->imported_symtabs_empty ())
9909 {
9910 int len;
9911 std::vector<compunit_symtab *> result_symtabs;
9912 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9913
9914 /* If we don't have a symtab, we can just skip this case. */
9915 if (cust == NULL)
9916 return;
9917
9918 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9919 htab_eq_pointer,
9920 NULL, xcalloc, xfree));
9921 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9922 htab_eq_pointer,
9923 NULL, xcalloc, xfree));
9924
9925 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9926 {
9927 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9928 all_type_symtabs.get (), ptr,
9929 per_objfile, cust);
9930 }
9931
9932 /* Now we have a transitive closure of all the included symtabs. */
9933 len = result_symtabs.size ();
9934 cust->includes
9935 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9936 struct compunit_symtab *, len + 1);
9937 memcpy (cust->includes, result_symtabs.data (),
9938 len * sizeof (compunit_symtab *));
9939 cust->includes[len] = NULL;
9940 }
9941 }
9942
9943 /* Compute the 'includes' field for the symtabs of all the CUs we just
9944 read. */
9945
9946 static void
9947 process_cu_includes (dwarf2_per_objfile *per_objfile)
9948 {
9949 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9950 {
9951 if (! iter->is_debug_types)
9952 compute_compunit_symtab_includes (iter, per_objfile);
9953 }
9954
9955 per_objfile->per_bfd->just_read_cus.clear ();
9956 }
9957
9958 /* Generate full symbol information for CU, whose DIEs have
9959 already been loaded into memory. */
9960
9961 static void
9962 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9963 {
9964 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9965 struct objfile *objfile = per_objfile->objfile;
9966 struct gdbarch *gdbarch = objfile->arch ();
9967 CORE_ADDR lowpc, highpc;
9968 struct compunit_symtab *cust;
9969 CORE_ADDR baseaddr;
9970 struct block *static_block;
9971 CORE_ADDR addr;
9972
9973 baseaddr = objfile->text_section_offset ();
9974
9975 /* Clear the list here in case something was left over. */
9976 cu->method_list.clear ();
9977
9978 cu->language = pretend_language;
9979 cu->language_defn = language_def (cu->language);
9980
9981 dwarf2_find_base_address (cu->dies, cu);
9982
9983 /* Do line number decoding in read_file_scope () */
9984 process_die (cu->dies, cu);
9985
9986 /* For now fudge the Go package. */
9987 if (cu->language == language_go)
9988 fixup_go_packaging (cu);
9989
9990 /* Now that we have processed all the DIEs in the CU, all the types
9991 should be complete, and it should now be safe to compute all of the
9992 physnames. */
9993 compute_delayed_physnames (cu);
9994
9995 if (cu->language == language_rust)
9996 rust_union_quirks (cu);
9997
9998 /* Some compilers don't define a DW_AT_high_pc attribute for the
9999 compilation unit. If the DW_AT_high_pc is missing, synthesize
10000 it, by scanning the DIE's below the compilation unit. */
10001 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10002
10003 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10004 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10005
10006 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10007 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10008 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10009 addrmap to help ensure it has an accurate map of pc values belonging to
10010 this comp unit. */
10011 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10012
10013 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10014 SECT_OFF_TEXT (objfile),
10015 0);
10016
10017 if (cust != NULL)
10018 {
10019 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10020
10021 /* Set symtab language to language from DW_AT_language. If the
10022 compilation is from a C file generated by language preprocessors, do
10023 not set the language if it was already deduced by start_subfile. */
10024 if (!(cu->language == language_c
10025 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10026 COMPUNIT_FILETABS (cust)->language = cu->language;
10027
10028 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10029 produce DW_AT_location with location lists but it can be possibly
10030 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10031 there were bugs in prologue debug info, fixed later in GCC-4.5
10032 by "unwind info for epilogues" patch (which is not directly related).
10033
10034 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10035 needed, it would be wrong due to missing DW_AT_producer there.
10036
10037 Still one can confuse GDB by using non-standard GCC compilation
10038 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10039 */
10040 if (cu->has_loclist && gcc_4_minor >= 5)
10041 cust->locations_valid = 1;
10042
10043 if (gcc_4_minor >= 5)
10044 cust->epilogue_unwind_valid = 1;
10045
10046 cust->call_site_htab = cu->call_site_htab;
10047 }
10048
10049 per_objfile->set_symtab (cu->per_cu, cust);
10050
10051 /* Push it for inclusion processing later. */
10052 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10053
10054 /* Not needed any more. */
10055 cu->reset_builder ();
10056 }
10057
10058 /* Generate full symbol information for type unit CU, whose DIEs have
10059 already been loaded into memory. */
10060
10061 static void
10062 process_full_type_unit (dwarf2_cu *cu,
10063 enum language pretend_language)
10064 {
10065 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10066 struct objfile *objfile = per_objfile->objfile;
10067 struct compunit_symtab *cust;
10068 struct signatured_type *sig_type;
10069
10070 gdb_assert (cu->per_cu->is_debug_types);
10071 sig_type = (struct signatured_type *) cu->per_cu;
10072
10073 /* Clear the list here in case something was left over. */
10074 cu->method_list.clear ();
10075
10076 cu->language = pretend_language;
10077 cu->language_defn = language_def (cu->language);
10078
10079 /* The symbol tables are set up in read_type_unit_scope. */
10080 process_die (cu->dies, cu);
10081
10082 /* For now fudge the Go package. */
10083 if (cu->language == language_go)
10084 fixup_go_packaging (cu);
10085
10086 /* Now that we have processed all the DIEs in the CU, all the types
10087 should be complete, and it should now be safe to compute all of the
10088 physnames. */
10089 compute_delayed_physnames (cu);
10090
10091 if (cu->language == language_rust)
10092 rust_union_quirks (cu);
10093
10094 /* TUs share symbol tables.
10095 If this is the first TU to use this symtab, complete the construction
10096 of it with end_expandable_symtab. Otherwise, complete the addition of
10097 this TU's symbols to the existing symtab. */
10098 type_unit_group_unshareable *tug_unshare =
10099 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10100 if (tug_unshare->compunit_symtab == NULL)
10101 {
10102 buildsym_compunit *builder = cu->get_builder ();
10103 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10104 tug_unshare->compunit_symtab = cust;
10105
10106 if (cust != NULL)
10107 {
10108 /* Set symtab language to language from DW_AT_language. If the
10109 compilation is from a C file generated by language preprocessors,
10110 do not set the language if it was already deduced by
10111 start_subfile. */
10112 if (!(cu->language == language_c
10113 && COMPUNIT_FILETABS (cust)->language != language_c))
10114 COMPUNIT_FILETABS (cust)->language = cu->language;
10115 }
10116 }
10117 else
10118 {
10119 cu->get_builder ()->augment_type_symtab ();
10120 cust = tug_unshare->compunit_symtab;
10121 }
10122
10123 per_objfile->set_symtab (cu->per_cu, cust);
10124
10125 /* Not needed any more. */
10126 cu->reset_builder ();
10127 }
10128
10129 /* Process an imported unit DIE. */
10130
10131 static void
10132 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10133 {
10134 struct attribute *attr;
10135
10136 /* For now we don't handle imported units in type units. */
10137 if (cu->per_cu->is_debug_types)
10138 {
10139 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10140 " supported in type units [in module %s]"),
10141 objfile_name (cu->per_objfile->objfile));
10142 }
10143
10144 attr = dwarf2_attr (die, DW_AT_import, cu);
10145 if (attr != NULL)
10146 {
10147 sect_offset sect_off = attr->get_ref_die_offset ();
10148 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10149 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10150 dwarf2_per_cu_data *per_cu
10151 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10152
10153 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10154 into another compilation unit, at root level. Regard this as a hint,
10155 and ignore it. */
10156 if (die->parent && die->parent->parent == NULL
10157 && per_cu->unit_type == DW_UT_compile
10158 && per_cu->lang == language_cplus)
10159 return;
10160
10161 /* If necessary, add it to the queue and load its DIEs. */
10162 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10163 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10164 false, cu->language);
10165
10166 cu->per_cu->imported_symtabs_push (per_cu);
10167 }
10168 }
10169
10170 /* RAII object that represents a process_die scope: i.e.,
10171 starts/finishes processing a DIE. */
10172 class process_die_scope
10173 {
10174 public:
10175 process_die_scope (die_info *die, dwarf2_cu *cu)
10176 : m_die (die), m_cu (cu)
10177 {
10178 /* We should only be processing DIEs not already in process. */
10179 gdb_assert (!m_die->in_process);
10180 m_die->in_process = true;
10181 }
10182
10183 ~process_die_scope ()
10184 {
10185 m_die->in_process = false;
10186
10187 /* If we're done processing the DIE for the CU that owns the line
10188 header, we don't need the line header anymore. */
10189 if (m_cu->line_header_die_owner == m_die)
10190 {
10191 delete m_cu->line_header;
10192 m_cu->line_header = NULL;
10193 m_cu->line_header_die_owner = NULL;
10194 }
10195 }
10196
10197 private:
10198 die_info *m_die;
10199 dwarf2_cu *m_cu;
10200 };
10201
10202 /* Process a die and its children. */
10203
10204 static void
10205 process_die (struct die_info *die, struct dwarf2_cu *cu)
10206 {
10207 process_die_scope scope (die, cu);
10208
10209 switch (die->tag)
10210 {
10211 case DW_TAG_padding:
10212 break;
10213 case DW_TAG_compile_unit:
10214 case DW_TAG_partial_unit:
10215 read_file_scope (die, cu);
10216 break;
10217 case DW_TAG_type_unit:
10218 read_type_unit_scope (die, cu);
10219 break;
10220 case DW_TAG_subprogram:
10221 /* Nested subprograms in Fortran get a prefix. */
10222 if (cu->language == language_fortran
10223 && die->parent != NULL
10224 && die->parent->tag == DW_TAG_subprogram)
10225 cu->processing_has_namespace_info = true;
10226 /* Fall through. */
10227 case DW_TAG_inlined_subroutine:
10228 read_func_scope (die, cu);
10229 break;
10230 case DW_TAG_lexical_block:
10231 case DW_TAG_try_block:
10232 case DW_TAG_catch_block:
10233 read_lexical_block_scope (die, cu);
10234 break;
10235 case DW_TAG_call_site:
10236 case DW_TAG_GNU_call_site:
10237 read_call_site_scope (die, cu);
10238 break;
10239 case DW_TAG_class_type:
10240 case DW_TAG_interface_type:
10241 case DW_TAG_structure_type:
10242 case DW_TAG_union_type:
10243 process_structure_scope (die, cu);
10244 break;
10245 case DW_TAG_enumeration_type:
10246 process_enumeration_scope (die, cu);
10247 break;
10248
10249 /* These dies have a type, but processing them does not create
10250 a symbol or recurse to process the children. Therefore we can
10251 read them on-demand through read_type_die. */
10252 case DW_TAG_subroutine_type:
10253 case DW_TAG_set_type:
10254 case DW_TAG_pointer_type:
10255 case DW_TAG_ptr_to_member_type:
10256 case DW_TAG_reference_type:
10257 case DW_TAG_rvalue_reference_type:
10258 case DW_TAG_string_type:
10259 break;
10260
10261 case DW_TAG_array_type:
10262 /* We only need to handle this case for Ada -- in other
10263 languages, it's normal for the compiler to emit a typedef
10264 instead. */
10265 if (cu->language != language_ada)
10266 break;
10267 /* FALLTHROUGH */
10268 case DW_TAG_base_type:
10269 case DW_TAG_subrange_type:
10270 case DW_TAG_typedef:
10271 /* Add a typedef symbol for the type definition, if it has a
10272 DW_AT_name. */
10273 new_symbol (die, read_type_die (die, cu), cu);
10274 break;
10275 case DW_TAG_common_block:
10276 read_common_block (die, cu);
10277 break;
10278 case DW_TAG_common_inclusion:
10279 break;
10280 case DW_TAG_namespace:
10281 cu->processing_has_namespace_info = true;
10282 read_namespace (die, cu);
10283 break;
10284 case DW_TAG_module:
10285 cu->processing_has_namespace_info = true;
10286 read_module (die, cu);
10287 break;
10288 case DW_TAG_imported_declaration:
10289 cu->processing_has_namespace_info = true;
10290 if (read_namespace_alias (die, cu))
10291 break;
10292 /* The declaration is not a global namespace alias. */
10293 /* Fall through. */
10294 case DW_TAG_imported_module:
10295 cu->processing_has_namespace_info = true;
10296 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10297 || cu->language != language_fortran))
10298 complaint (_("Tag '%s' has unexpected children"),
10299 dwarf_tag_name (die->tag));
10300 read_import_statement (die, cu);
10301 break;
10302
10303 case DW_TAG_imported_unit:
10304 process_imported_unit_die (die, cu);
10305 break;
10306
10307 case DW_TAG_variable:
10308 read_variable (die, cu);
10309 break;
10310
10311 default:
10312 new_symbol (die, NULL, cu);
10313 break;
10314 }
10315 }
10316 \f
10317 /* DWARF name computation. */
10318
10319 /* A helper function for dwarf2_compute_name which determines whether DIE
10320 needs to have the name of the scope prepended to the name listed in the
10321 die. */
10322
10323 static int
10324 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10325 {
10326 struct attribute *attr;
10327
10328 switch (die->tag)
10329 {
10330 case DW_TAG_namespace:
10331 case DW_TAG_typedef:
10332 case DW_TAG_class_type:
10333 case DW_TAG_interface_type:
10334 case DW_TAG_structure_type:
10335 case DW_TAG_union_type:
10336 case DW_TAG_enumeration_type:
10337 case DW_TAG_enumerator:
10338 case DW_TAG_subprogram:
10339 case DW_TAG_inlined_subroutine:
10340 case DW_TAG_member:
10341 case DW_TAG_imported_declaration:
10342 return 1;
10343
10344 case DW_TAG_variable:
10345 case DW_TAG_constant:
10346 /* We only need to prefix "globally" visible variables. These include
10347 any variable marked with DW_AT_external or any variable that
10348 lives in a namespace. [Variables in anonymous namespaces
10349 require prefixing, but they are not DW_AT_external.] */
10350
10351 if (dwarf2_attr (die, DW_AT_specification, cu))
10352 {
10353 struct dwarf2_cu *spec_cu = cu;
10354
10355 return die_needs_namespace (die_specification (die, &spec_cu),
10356 spec_cu);
10357 }
10358
10359 attr = dwarf2_attr (die, DW_AT_external, cu);
10360 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10361 && die->parent->tag != DW_TAG_module)
10362 return 0;
10363 /* A variable in a lexical block of some kind does not need a
10364 namespace, even though in C++ such variables may be external
10365 and have a mangled name. */
10366 if (die->parent->tag == DW_TAG_lexical_block
10367 || die->parent->tag == DW_TAG_try_block
10368 || die->parent->tag == DW_TAG_catch_block
10369 || die->parent->tag == DW_TAG_subprogram)
10370 return 0;
10371 return 1;
10372
10373 default:
10374 return 0;
10375 }
10376 }
10377
10378 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10379 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10380 defined for the given DIE. */
10381
10382 static struct attribute *
10383 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10384 {
10385 struct attribute *attr;
10386
10387 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10388 if (attr == NULL)
10389 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10390
10391 return attr;
10392 }
10393
10394 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10395 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10396 defined for the given DIE. */
10397
10398 static const char *
10399 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10400 {
10401 const char *linkage_name;
10402
10403 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10404 if (linkage_name == NULL)
10405 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10406
10407 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10408 See https://github.com/rust-lang/rust/issues/32925. */
10409 if (cu->language == language_rust && linkage_name != NULL
10410 && strchr (linkage_name, '{') != NULL)
10411 linkage_name = NULL;
10412
10413 return linkage_name;
10414 }
10415
10416 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10417 compute the physname for the object, which include a method's:
10418 - formal parameters (C++),
10419 - receiver type (Go),
10420
10421 The term "physname" is a bit confusing.
10422 For C++, for example, it is the demangled name.
10423 For Go, for example, it's the mangled name.
10424
10425 For Ada, return the DIE's linkage name rather than the fully qualified
10426 name. PHYSNAME is ignored..
10427
10428 The result is allocated on the objfile->per_bfd's obstack and
10429 canonicalized. */
10430
10431 static const char *
10432 dwarf2_compute_name (const char *name,
10433 struct die_info *die, struct dwarf2_cu *cu,
10434 int physname)
10435 {
10436 struct objfile *objfile = cu->per_objfile->objfile;
10437
10438 if (name == NULL)
10439 name = dwarf2_name (die, cu);
10440
10441 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10442 but otherwise compute it by typename_concat inside GDB.
10443 FIXME: Actually this is not really true, or at least not always true.
10444 It's all very confusing. compute_and_set_names doesn't try to demangle
10445 Fortran names because there is no mangling standard. So new_symbol
10446 will set the demangled name to the result of dwarf2_full_name, and it is
10447 the demangled name that GDB uses if it exists. */
10448 if (cu->language == language_ada
10449 || (cu->language == language_fortran && physname))
10450 {
10451 /* For Ada unit, we prefer the linkage name over the name, as
10452 the former contains the exported name, which the user expects
10453 to be able to reference. Ideally, we want the user to be able
10454 to reference this entity using either natural or linkage name,
10455 but we haven't started looking at this enhancement yet. */
10456 const char *linkage_name = dw2_linkage_name (die, cu);
10457
10458 if (linkage_name != NULL)
10459 return linkage_name;
10460 }
10461
10462 /* These are the only languages we know how to qualify names in. */
10463 if (name != NULL
10464 && (cu->language == language_cplus
10465 || cu->language == language_fortran || cu->language == language_d
10466 || cu->language == language_rust))
10467 {
10468 if (die_needs_namespace (die, cu))
10469 {
10470 const char *prefix;
10471 const char *canonical_name = NULL;
10472
10473 string_file buf;
10474
10475 prefix = determine_prefix (die, cu);
10476 if (*prefix != '\0')
10477 {
10478 gdb::unique_xmalloc_ptr<char> prefixed_name
10479 (typename_concat (NULL, prefix, name, physname, cu));
10480
10481 buf.puts (prefixed_name.get ());
10482 }
10483 else
10484 buf.puts (name);
10485
10486 /* Template parameters may be specified in the DIE's DW_AT_name, or
10487 as children with DW_TAG_template_type_param or
10488 DW_TAG_value_type_param. If the latter, add them to the name
10489 here. If the name already has template parameters, then
10490 skip this step; some versions of GCC emit both, and
10491 it is more efficient to use the pre-computed name.
10492
10493 Something to keep in mind about this process: it is very
10494 unlikely, or in some cases downright impossible, to produce
10495 something that will match the mangled name of a function.
10496 If the definition of the function has the same debug info,
10497 we should be able to match up with it anyway. But fallbacks
10498 using the minimal symbol, for instance to find a method
10499 implemented in a stripped copy of libstdc++, will not work.
10500 If we do not have debug info for the definition, we will have to
10501 match them up some other way.
10502
10503 When we do name matching there is a related problem with function
10504 templates; two instantiated function templates are allowed to
10505 differ only by their return types, which we do not add here. */
10506
10507 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10508 {
10509 struct attribute *attr;
10510 struct die_info *child;
10511 int first = 1;
10512 const language_defn *cplus_lang = language_def (cu->language);
10513
10514 die->building_fullname = 1;
10515
10516 for (child = die->child; child != NULL; child = child->sibling)
10517 {
10518 struct type *type;
10519 LONGEST value;
10520 const gdb_byte *bytes;
10521 struct dwarf2_locexpr_baton *baton;
10522 struct value *v;
10523
10524 if (child->tag != DW_TAG_template_type_param
10525 && child->tag != DW_TAG_template_value_param)
10526 continue;
10527
10528 if (first)
10529 {
10530 buf.puts ("<");
10531 first = 0;
10532 }
10533 else
10534 buf.puts (", ");
10535
10536 attr = dwarf2_attr (child, DW_AT_type, cu);
10537 if (attr == NULL)
10538 {
10539 complaint (_("template parameter missing DW_AT_type"));
10540 buf.puts ("UNKNOWN_TYPE");
10541 continue;
10542 }
10543 type = die_type (child, cu);
10544
10545 if (child->tag == DW_TAG_template_type_param)
10546 {
10547 cplus_lang->print_type (type, "", &buf, -1, 0,
10548 &type_print_raw_options);
10549 continue;
10550 }
10551
10552 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10553 if (attr == NULL)
10554 {
10555 complaint (_("template parameter missing "
10556 "DW_AT_const_value"));
10557 buf.puts ("UNKNOWN_VALUE");
10558 continue;
10559 }
10560
10561 dwarf2_const_value_attr (attr, type, name,
10562 &cu->comp_unit_obstack, cu,
10563 &value, &bytes, &baton);
10564
10565 if (type->has_no_signedness ())
10566 /* GDB prints characters as NUMBER 'CHAR'. If that's
10567 changed, this can use value_print instead. */
10568 cplus_lang->printchar (value, type, &buf);
10569 else
10570 {
10571 struct value_print_options opts;
10572
10573 if (baton != NULL)
10574 v = dwarf2_evaluate_loc_desc (type, NULL,
10575 baton->data,
10576 baton->size,
10577 baton->per_cu,
10578 baton->per_objfile);
10579 else if (bytes != NULL)
10580 {
10581 v = allocate_value (type);
10582 memcpy (value_contents_writeable (v), bytes,
10583 TYPE_LENGTH (type));
10584 }
10585 else
10586 v = value_from_longest (type, value);
10587
10588 /* Specify decimal so that we do not depend on
10589 the radix. */
10590 get_formatted_print_options (&opts, 'd');
10591 opts.raw = 1;
10592 value_print (v, &buf, &opts);
10593 release_value (v);
10594 }
10595 }
10596
10597 die->building_fullname = 0;
10598
10599 if (!first)
10600 {
10601 /* Close the argument list, with a space if necessary
10602 (nested templates). */
10603 if (!buf.empty () && buf.string ().back () == '>')
10604 buf.puts (" >");
10605 else
10606 buf.puts (">");
10607 }
10608 }
10609
10610 /* For C++ methods, append formal parameter type
10611 information, if PHYSNAME. */
10612
10613 if (physname && die->tag == DW_TAG_subprogram
10614 && cu->language == language_cplus)
10615 {
10616 struct type *type = read_type_die (die, cu);
10617
10618 c_type_print_args (type, &buf, 1, cu->language,
10619 &type_print_raw_options);
10620
10621 if (cu->language == language_cplus)
10622 {
10623 /* Assume that an artificial first parameter is
10624 "this", but do not crash if it is not. RealView
10625 marks unnamed (and thus unused) parameters as
10626 artificial; there is no way to differentiate
10627 the two cases. */
10628 if (type->num_fields () > 0
10629 && TYPE_FIELD_ARTIFICIAL (type, 0)
10630 && type->field (0).type ()->code () == TYPE_CODE_PTR
10631 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10632 buf.puts (" const");
10633 }
10634 }
10635
10636 const std::string &intermediate_name = buf.string ();
10637
10638 if (cu->language == language_cplus)
10639 canonical_name
10640 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10641 objfile);
10642
10643 /* If we only computed INTERMEDIATE_NAME, or if
10644 INTERMEDIATE_NAME is already canonical, then we need to
10645 intern it. */
10646 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10647 name = objfile->intern (intermediate_name);
10648 else
10649 name = canonical_name;
10650 }
10651 }
10652
10653 return name;
10654 }
10655
10656 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10657 If scope qualifiers are appropriate they will be added. The result
10658 will be allocated on the storage_obstack, or NULL if the DIE does
10659 not have a name. NAME may either be from a previous call to
10660 dwarf2_name or NULL.
10661
10662 The output string will be canonicalized (if C++). */
10663
10664 static const char *
10665 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10666 {
10667 return dwarf2_compute_name (name, die, cu, 0);
10668 }
10669
10670 /* Construct a physname for the given DIE in CU. NAME may either be
10671 from a previous call to dwarf2_name or NULL. The result will be
10672 allocated on the objfile_objstack or NULL if the DIE does not have a
10673 name.
10674
10675 The output string will be canonicalized (if C++). */
10676
10677 static const char *
10678 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10679 {
10680 struct objfile *objfile = cu->per_objfile->objfile;
10681 const char *retval, *mangled = NULL, *canon = NULL;
10682 int need_copy = 1;
10683
10684 /* In this case dwarf2_compute_name is just a shortcut not building anything
10685 on its own. */
10686 if (!die_needs_namespace (die, cu))
10687 return dwarf2_compute_name (name, die, cu, 1);
10688
10689 if (cu->language != language_rust)
10690 mangled = dw2_linkage_name (die, cu);
10691
10692 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10693 has computed. */
10694 gdb::unique_xmalloc_ptr<char> demangled;
10695 if (mangled != NULL)
10696 {
10697
10698 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10699 {
10700 /* Do nothing (do not demangle the symbol name). */
10701 }
10702 else
10703 {
10704 /* Use DMGL_RET_DROP for C++ template functions to suppress
10705 their return type. It is easier for GDB users to search
10706 for such functions as `name(params)' than `long name(params)'.
10707 In such case the minimal symbol names do not match the full
10708 symbol names but for template functions there is never a need
10709 to look up their definition from their declaration so
10710 the only disadvantage remains the minimal symbol variant
10711 `long name(params)' does not have the proper inferior type. */
10712 demangled.reset (gdb_demangle (mangled,
10713 (DMGL_PARAMS | DMGL_ANSI
10714 | DMGL_RET_DROP)));
10715 }
10716 if (demangled)
10717 canon = demangled.get ();
10718 else
10719 {
10720 canon = mangled;
10721 need_copy = 0;
10722 }
10723 }
10724
10725 if (canon == NULL || check_physname)
10726 {
10727 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10728
10729 if (canon != NULL && strcmp (physname, canon) != 0)
10730 {
10731 /* It may not mean a bug in GDB. The compiler could also
10732 compute DW_AT_linkage_name incorrectly. But in such case
10733 GDB would need to be bug-to-bug compatible. */
10734
10735 complaint (_("Computed physname <%s> does not match demangled <%s> "
10736 "(from linkage <%s>) - DIE at %s [in module %s]"),
10737 physname, canon, mangled, sect_offset_str (die->sect_off),
10738 objfile_name (objfile));
10739
10740 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10741 is available here - over computed PHYSNAME. It is safer
10742 against both buggy GDB and buggy compilers. */
10743
10744 retval = canon;
10745 }
10746 else
10747 {
10748 retval = physname;
10749 need_copy = 0;
10750 }
10751 }
10752 else
10753 retval = canon;
10754
10755 if (need_copy)
10756 retval = objfile->intern (retval);
10757
10758 return retval;
10759 }
10760
10761 /* Inspect DIE in CU for a namespace alias. If one exists, record
10762 a new symbol for it.
10763
10764 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10765
10766 static int
10767 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10768 {
10769 struct attribute *attr;
10770
10771 /* If the die does not have a name, this is not a namespace
10772 alias. */
10773 attr = dwarf2_attr (die, DW_AT_name, cu);
10774 if (attr != NULL)
10775 {
10776 int num;
10777 struct die_info *d = die;
10778 struct dwarf2_cu *imported_cu = cu;
10779
10780 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10781 keep inspecting DIEs until we hit the underlying import. */
10782 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10783 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10784 {
10785 attr = dwarf2_attr (d, DW_AT_import, cu);
10786 if (attr == NULL)
10787 break;
10788
10789 d = follow_die_ref (d, attr, &imported_cu);
10790 if (d->tag != DW_TAG_imported_declaration)
10791 break;
10792 }
10793
10794 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10795 {
10796 complaint (_("DIE at %s has too many recursively imported "
10797 "declarations"), sect_offset_str (d->sect_off));
10798 return 0;
10799 }
10800
10801 if (attr != NULL)
10802 {
10803 struct type *type;
10804 sect_offset sect_off = attr->get_ref_die_offset ();
10805
10806 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10807 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10808 {
10809 /* This declaration is a global namespace alias. Add
10810 a symbol for it whose type is the aliased namespace. */
10811 new_symbol (die, type, cu);
10812 return 1;
10813 }
10814 }
10815 }
10816
10817 return 0;
10818 }
10819
10820 /* Return the using directives repository (global or local?) to use in the
10821 current context for CU.
10822
10823 For Ada, imported declarations can materialize renamings, which *may* be
10824 global. However it is impossible (for now?) in DWARF to distinguish
10825 "external" imported declarations and "static" ones. As all imported
10826 declarations seem to be static in all other languages, make them all CU-wide
10827 global only in Ada. */
10828
10829 static struct using_direct **
10830 using_directives (struct dwarf2_cu *cu)
10831 {
10832 if (cu->language == language_ada
10833 && cu->get_builder ()->outermost_context_p ())
10834 return cu->get_builder ()->get_global_using_directives ();
10835 else
10836 return cu->get_builder ()->get_local_using_directives ();
10837 }
10838
10839 /* Read the import statement specified by the given die and record it. */
10840
10841 static void
10842 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10843 {
10844 struct objfile *objfile = cu->per_objfile->objfile;
10845 struct attribute *import_attr;
10846 struct die_info *imported_die, *child_die;
10847 struct dwarf2_cu *imported_cu;
10848 const char *imported_name;
10849 const char *imported_name_prefix;
10850 const char *canonical_name;
10851 const char *import_alias;
10852 const char *imported_declaration = NULL;
10853 const char *import_prefix;
10854 std::vector<const char *> excludes;
10855
10856 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10857 if (import_attr == NULL)
10858 {
10859 complaint (_("Tag '%s' has no DW_AT_import"),
10860 dwarf_tag_name (die->tag));
10861 return;
10862 }
10863
10864 imported_cu = cu;
10865 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10866 imported_name = dwarf2_name (imported_die, imported_cu);
10867 if (imported_name == NULL)
10868 {
10869 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10870
10871 The import in the following code:
10872 namespace A
10873 {
10874 typedef int B;
10875 }
10876
10877 int main ()
10878 {
10879 using A::B;
10880 B b;
10881 return b;
10882 }
10883
10884 ...
10885 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10886 <52> DW_AT_decl_file : 1
10887 <53> DW_AT_decl_line : 6
10888 <54> DW_AT_import : <0x75>
10889 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10890 <59> DW_AT_name : B
10891 <5b> DW_AT_decl_file : 1
10892 <5c> DW_AT_decl_line : 2
10893 <5d> DW_AT_type : <0x6e>
10894 ...
10895 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10896 <76> DW_AT_byte_size : 4
10897 <77> DW_AT_encoding : 5 (signed)
10898
10899 imports the wrong die ( 0x75 instead of 0x58 ).
10900 This case will be ignored until the gcc bug is fixed. */
10901 return;
10902 }
10903
10904 /* Figure out the local name after import. */
10905 import_alias = dwarf2_name (die, cu);
10906
10907 /* Figure out where the statement is being imported to. */
10908 import_prefix = determine_prefix (die, cu);
10909
10910 /* Figure out what the scope of the imported die is and prepend it
10911 to the name of the imported die. */
10912 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10913
10914 if (imported_die->tag != DW_TAG_namespace
10915 && imported_die->tag != DW_TAG_module)
10916 {
10917 imported_declaration = imported_name;
10918 canonical_name = imported_name_prefix;
10919 }
10920 else if (strlen (imported_name_prefix) > 0)
10921 canonical_name = obconcat (&objfile->objfile_obstack,
10922 imported_name_prefix,
10923 (cu->language == language_d ? "." : "::"),
10924 imported_name, (char *) NULL);
10925 else
10926 canonical_name = imported_name;
10927
10928 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10929 for (child_die = die->child; child_die && child_die->tag;
10930 child_die = child_die->sibling)
10931 {
10932 /* DWARF-4: A Fortran use statement with a “rename list” may be
10933 represented by an imported module entry with an import attribute
10934 referring to the module and owned entries corresponding to those
10935 entities that are renamed as part of being imported. */
10936
10937 if (child_die->tag != DW_TAG_imported_declaration)
10938 {
10939 complaint (_("child DW_TAG_imported_declaration expected "
10940 "- DIE at %s [in module %s]"),
10941 sect_offset_str (child_die->sect_off),
10942 objfile_name (objfile));
10943 continue;
10944 }
10945
10946 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10947 if (import_attr == NULL)
10948 {
10949 complaint (_("Tag '%s' has no DW_AT_import"),
10950 dwarf_tag_name (child_die->tag));
10951 continue;
10952 }
10953
10954 imported_cu = cu;
10955 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10956 &imported_cu);
10957 imported_name = dwarf2_name (imported_die, imported_cu);
10958 if (imported_name == NULL)
10959 {
10960 complaint (_("child DW_TAG_imported_declaration has unknown "
10961 "imported name - DIE at %s [in module %s]"),
10962 sect_offset_str (child_die->sect_off),
10963 objfile_name (objfile));
10964 continue;
10965 }
10966
10967 excludes.push_back (imported_name);
10968
10969 process_die (child_die, cu);
10970 }
10971
10972 add_using_directive (using_directives (cu),
10973 import_prefix,
10974 canonical_name,
10975 import_alias,
10976 imported_declaration,
10977 excludes,
10978 0,
10979 &objfile->objfile_obstack);
10980 }
10981
10982 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10983 types, but gives them a size of zero. Starting with version 14,
10984 ICC is compatible with GCC. */
10985
10986 static bool
10987 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10988 {
10989 if (!cu->checked_producer)
10990 check_producer (cu);
10991
10992 return cu->producer_is_icc_lt_14;
10993 }
10994
10995 /* ICC generates a DW_AT_type for C void functions. This was observed on
10996 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10997 which says that void functions should not have a DW_AT_type. */
10998
10999 static bool
11000 producer_is_icc (struct dwarf2_cu *cu)
11001 {
11002 if (!cu->checked_producer)
11003 check_producer (cu);
11004
11005 return cu->producer_is_icc;
11006 }
11007
11008 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11009 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11010 this, it was first present in GCC release 4.3.0. */
11011
11012 static bool
11013 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11014 {
11015 if (!cu->checked_producer)
11016 check_producer (cu);
11017
11018 return cu->producer_is_gcc_lt_4_3;
11019 }
11020
11021 static file_and_directory
11022 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11023 {
11024 file_and_directory res;
11025
11026 /* Find the filename. Do not use dwarf2_name here, since the filename
11027 is not a source language identifier. */
11028 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11029 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11030
11031 if (res.comp_dir == NULL
11032 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11033 && IS_ABSOLUTE_PATH (res.name))
11034 {
11035 res.comp_dir_storage = ldirname (res.name);
11036 if (!res.comp_dir_storage.empty ())
11037 res.comp_dir = res.comp_dir_storage.c_str ();
11038 }
11039 if (res.comp_dir != NULL)
11040 {
11041 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11042 directory, get rid of it. */
11043 const char *cp = strchr (res.comp_dir, ':');
11044
11045 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11046 res.comp_dir = cp + 1;
11047 }
11048
11049 if (res.name == NULL)
11050 res.name = "<unknown>";
11051
11052 return res;
11053 }
11054
11055 /* Handle DW_AT_stmt_list for a compilation unit.
11056 DIE is the DW_TAG_compile_unit die for CU.
11057 COMP_DIR is the compilation directory. LOWPC is passed to
11058 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11059
11060 static void
11061 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11062 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11063 {
11064 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11065 struct attribute *attr;
11066 struct line_header line_header_local;
11067 hashval_t line_header_local_hash;
11068 void **slot;
11069 int decode_mapping;
11070
11071 gdb_assert (! cu->per_cu->is_debug_types);
11072
11073 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11074 if (attr == NULL || !attr->form_is_unsigned ())
11075 return;
11076
11077 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11078
11079 /* The line header hash table is only created if needed (it exists to
11080 prevent redundant reading of the line table for partial_units).
11081 If we're given a partial_unit, we'll need it. If we're given a
11082 compile_unit, then use the line header hash table if it's already
11083 created, but don't create one just yet. */
11084
11085 if (per_objfile->line_header_hash == NULL
11086 && die->tag == DW_TAG_partial_unit)
11087 {
11088 per_objfile->line_header_hash
11089 .reset (htab_create_alloc (127, line_header_hash_voidp,
11090 line_header_eq_voidp,
11091 free_line_header_voidp,
11092 xcalloc, xfree));
11093 }
11094
11095 line_header_local.sect_off = line_offset;
11096 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11097 line_header_local_hash = line_header_hash (&line_header_local);
11098 if (per_objfile->line_header_hash != NULL)
11099 {
11100 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11101 &line_header_local,
11102 line_header_local_hash, NO_INSERT);
11103
11104 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11105 is not present in *SLOT (since if there is something in *SLOT then
11106 it will be for a partial_unit). */
11107 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11108 {
11109 gdb_assert (*slot != NULL);
11110 cu->line_header = (struct line_header *) *slot;
11111 return;
11112 }
11113 }
11114
11115 /* dwarf_decode_line_header does not yet provide sufficient information.
11116 We always have to call also dwarf_decode_lines for it. */
11117 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11118 if (lh == NULL)
11119 return;
11120
11121 cu->line_header = lh.release ();
11122 cu->line_header_die_owner = die;
11123
11124 if (per_objfile->line_header_hash == NULL)
11125 slot = NULL;
11126 else
11127 {
11128 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11129 &line_header_local,
11130 line_header_local_hash, INSERT);
11131 gdb_assert (slot != NULL);
11132 }
11133 if (slot != NULL && *slot == NULL)
11134 {
11135 /* This newly decoded line number information unit will be owned
11136 by line_header_hash hash table. */
11137 *slot = cu->line_header;
11138 cu->line_header_die_owner = NULL;
11139 }
11140 else
11141 {
11142 /* We cannot free any current entry in (*slot) as that struct line_header
11143 may be already used by multiple CUs. Create only temporary decoded
11144 line_header for this CU - it may happen at most once for each line
11145 number information unit. And if we're not using line_header_hash
11146 then this is what we want as well. */
11147 gdb_assert (die->tag != DW_TAG_partial_unit);
11148 }
11149 decode_mapping = (die->tag != DW_TAG_partial_unit);
11150 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11151 decode_mapping);
11152
11153 }
11154
11155 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11156
11157 static void
11158 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11159 {
11160 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11161 struct objfile *objfile = per_objfile->objfile;
11162 struct gdbarch *gdbarch = objfile->arch ();
11163 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11164 CORE_ADDR highpc = ((CORE_ADDR) 0);
11165 struct attribute *attr;
11166 struct die_info *child_die;
11167 CORE_ADDR baseaddr;
11168
11169 prepare_one_comp_unit (cu, die, cu->language);
11170 baseaddr = objfile->text_section_offset ();
11171
11172 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11173
11174 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11175 from finish_block. */
11176 if (lowpc == ((CORE_ADDR) -1))
11177 lowpc = highpc;
11178 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11179
11180 file_and_directory fnd = find_file_and_directory (die, cu);
11181
11182 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11183 standardised yet. As a workaround for the language detection we fall
11184 back to the DW_AT_producer string. */
11185 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11186 cu->language = language_opencl;
11187
11188 /* Similar hack for Go. */
11189 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11190 set_cu_language (DW_LANG_Go, cu);
11191
11192 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11193
11194 /* Decode line number information if present. We do this before
11195 processing child DIEs, so that the line header table is available
11196 for DW_AT_decl_file. */
11197 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11198
11199 /* Process all dies in compilation unit. */
11200 if (die->child != NULL)
11201 {
11202 child_die = die->child;
11203 while (child_die && child_die->tag)
11204 {
11205 process_die (child_die, cu);
11206 child_die = child_die->sibling;
11207 }
11208 }
11209
11210 /* Decode macro information, if present. Dwarf 2 macro information
11211 refers to information in the line number info statement program
11212 header, so we can only read it if we've read the header
11213 successfully. */
11214 attr = dwarf2_attr (die, DW_AT_macros, cu);
11215 if (attr == NULL)
11216 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11217 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11218 {
11219 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11220 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11221
11222 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11223 }
11224 else
11225 {
11226 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11227 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11228 {
11229 unsigned int macro_offset = attr->as_unsigned ();
11230
11231 dwarf_decode_macros (cu, macro_offset, 0);
11232 }
11233 }
11234 }
11235
11236 void
11237 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11238 {
11239 struct type_unit_group *tu_group;
11240 int first_time;
11241 struct attribute *attr;
11242 unsigned int i;
11243 struct signatured_type *sig_type;
11244
11245 gdb_assert (per_cu->is_debug_types);
11246 sig_type = (struct signatured_type *) per_cu;
11247
11248 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11249
11250 /* If we're using .gdb_index (includes -readnow) then
11251 per_cu->type_unit_group may not have been set up yet. */
11252 if (sig_type->type_unit_group == NULL)
11253 sig_type->type_unit_group = get_type_unit_group (this, attr);
11254 tu_group = sig_type->type_unit_group;
11255
11256 /* If we've already processed this stmt_list there's no real need to
11257 do it again, we could fake it and just recreate the part we need
11258 (file name,index -> symtab mapping). If data shows this optimization
11259 is useful we can do it then. */
11260 type_unit_group_unshareable *tug_unshare
11261 = per_objfile->get_type_unit_group_unshareable (tu_group);
11262 first_time = tug_unshare->compunit_symtab == NULL;
11263
11264 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11265 debug info. */
11266 line_header_up lh;
11267 if (attr != NULL && attr->form_is_unsigned ())
11268 {
11269 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11270 lh = dwarf_decode_line_header (line_offset, this);
11271 }
11272 if (lh == NULL)
11273 {
11274 if (first_time)
11275 start_symtab ("", NULL, 0);
11276 else
11277 {
11278 gdb_assert (tug_unshare->symtabs == NULL);
11279 gdb_assert (m_builder == nullptr);
11280 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11281 m_builder.reset (new struct buildsym_compunit
11282 (COMPUNIT_OBJFILE (cust), "",
11283 COMPUNIT_DIRNAME (cust),
11284 compunit_language (cust),
11285 0, cust));
11286 list_in_scope = get_builder ()->get_file_symbols ();
11287 }
11288 return;
11289 }
11290
11291 line_header = lh.release ();
11292 line_header_die_owner = die;
11293
11294 if (first_time)
11295 {
11296 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11297
11298 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11299 still initializing it, and our caller (a few levels up)
11300 process_full_type_unit still needs to know if this is the first
11301 time. */
11302
11303 tug_unshare->symtabs
11304 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11305 struct symtab *, line_header->file_names_size ());
11306
11307 auto &file_names = line_header->file_names ();
11308 for (i = 0; i < file_names.size (); ++i)
11309 {
11310 file_entry &fe = file_names[i];
11311 dwarf2_start_subfile (this, fe.name,
11312 fe.include_dir (line_header));
11313 buildsym_compunit *b = get_builder ();
11314 if (b->get_current_subfile ()->symtab == NULL)
11315 {
11316 /* NOTE: start_subfile will recognize when it's been
11317 passed a file it has already seen. So we can't
11318 assume there's a simple mapping from
11319 cu->line_header->file_names to subfiles, plus
11320 cu->line_header->file_names may contain dups. */
11321 b->get_current_subfile ()->symtab
11322 = allocate_symtab (cust, b->get_current_subfile ()->name);
11323 }
11324
11325 fe.symtab = b->get_current_subfile ()->symtab;
11326 tug_unshare->symtabs[i] = fe.symtab;
11327 }
11328 }
11329 else
11330 {
11331 gdb_assert (m_builder == nullptr);
11332 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11333 m_builder.reset (new struct buildsym_compunit
11334 (COMPUNIT_OBJFILE (cust), "",
11335 COMPUNIT_DIRNAME (cust),
11336 compunit_language (cust),
11337 0, cust));
11338 list_in_scope = get_builder ()->get_file_symbols ();
11339
11340 auto &file_names = line_header->file_names ();
11341 for (i = 0; i < file_names.size (); ++i)
11342 {
11343 file_entry &fe = file_names[i];
11344 fe.symtab = tug_unshare->symtabs[i];
11345 }
11346 }
11347
11348 /* The main symtab is allocated last. Type units don't have DW_AT_name
11349 so they don't have a "real" (so to speak) symtab anyway.
11350 There is later code that will assign the main symtab to all symbols
11351 that don't have one. We need to handle the case of a symbol with a
11352 missing symtab (DW_AT_decl_file) anyway. */
11353 }
11354
11355 /* Process DW_TAG_type_unit.
11356 For TUs we want to skip the first top level sibling if it's not the
11357 actual type being defined by this TU. In this case the first top
11358 level sibling is there to provide context only. */
11359
11360 static void
11361 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11362 {
11363 struct die_info *child_die;
11364
11365 prepare_one_comp_unit (cu, die, language_minimal);
11366
11367 /* Initialize (or reinitialize) the machinery for building symtabs.
11368 We do this before processing child DIEs, so that the line header table
11369 is available for DW_AT_decl_file. */
11370 cu->setup_type_unit_groups (die);
11371
11372 if (die->child != NULL)
11373 {
11374 child_die = die->child;
11375 while (child_die && child_die->tag)
11376 {
11377 process_die (child_die, cu);
11378 child_die = child_die->sibling;
11379 }
11380 }
11381 }
11382 \f
11383 /* DWO/DWP files.
11384
11385 http://gcc.gnu.org/wiki/DebugFission
11386 http://gcc.gnu.org/wiki/DebugFissionDWP
11387
11388 To simplify handling of both DWO files ("object" files with the DWARF info)
11389 and DWP files (a file with the DWOs packaged up into one file), we treat
11390 DWP files as having a collection of virtual DWO files. */
11391
11392 static hashval_t
11393 hash_dwo_file (const void *item)
11394 {
11395 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11396 hashval_t hash;
11397
11398 hash = htab_hash_string (dwo_file->dwo_name);
11399 if (dwo_file->comp_dir != NULL)
11400 hash += htab_hash_string (dwo_file->comp_dir);
11401 return hash;
11402 }
11403
11404 static int
11405 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11406 {
11407 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11408 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11409
11410 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11411 return 0;
11412 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11413 return lhs->comp_dir == rhs->comp_dir;
11414 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11415 }
11416
11417 /* Allocate a hash table for DWO files. */
11418
11419 static htab_up
11420 allocate_dwo_file_hash_table ()
11421 {
11422 auto delete_dwo_file = [] (void *item)
11423 {
11424 struct dwo_file *dwo_file = (struct dwo_file *) item;
11425
11426 delete dwo_file;
11427 };
11428
11429 return htab_up (htab_create_alloc (41,
11430 hash_dwo_file,
11431 eq_dwo_file,
11432 delete_dwo_file,
11433 xcalloc, xfree));
11434 }
11435
11436 /* Lookup DWO file DWO_NAME. */
11437
11438 static void **
11439 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11440 const char *dwo_name,
11441 const char *comp_dir)
11442 {
11443 struct dwo_file find_entry;
11444 void **slot;
11445
11446 if (per_objfile->per_bfd->dwo_files == NULL)
11447 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11448
11449 find_entry.dwo_name = dwo_name;
11450 find_entry.comp_dir = comp_dir;
11451 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11452 INSERT);
11453
11454 return slot;
11455 }
11456
11457 static hashval_t
11458 hash_dwo_unit (const void *item)
11459 {
11460 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11461
11462 /* This drops the top 32 bits of the id, but is ok for a hash. */
11463 return dwo_unit->signature;
11464 }
11465
11466 static int
11467 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11468 {
11469 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11470 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11471
11472 /* The signature is assumed to be unique within the DWO file.
11473 So while object file CU dwo_id's always have the value zero,
11474 that's OK, assuming each object file DWO file has only one CU,
11475 and that's the rule for now. */
11476 return lhs->signature == rhs->signature;
11477 }
11478
11479 /* Allocate a hash table for DWO CUs,TUs.
11480 There is one of these tables for each of CUs,TUs for each DWO file. */
11481
11482 static htab_up
11483 allocate_dwo_unit_table ()
11484 {
11485 /* Start out with a pretty small number.
11486 Generally DWO files contain only one CU and maybe some TUs. */
11487 return htab_up (htab_create_alloc (3,
11488 hash_dwo_unit,
11489 eq_dwo_unit,
11490 NULL, xcalloc, xfree));
11491 }
11492
11493 /* die_reader_func for create_dwo_cu. */
11494
11495 static void
11496 create_dwo_cu_reader (const struct die_reader_specs *reader,
11497 const gdb_byte *info_ptr,
11498 struct die_info *comp_unit_die,
11499 struct dwo_file *dwo_file,
11500 struct dwo_unit *dwo_unit)
11501 {
11502 struct dwarf2_cu *cu = reader->cu;
11503 sect_offset sect_off = cu->per_cu->sect_off;
11504 struct dwarf2_section_info *section = cu->per_cu->section;
11505
11506 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11507 if (!signature.has_value ())
11508 {
11509 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11510 " its dwo_id [in module %s]"),
11511 sect_offset_str (sect_off), dwo_file->dwo_name);
11512 return;
11513 }
11514
11515 dwo_unit->dwo_file = dwo_file;
11516 dwo_unit->signature = *signature;
11517 dwo_unit->section = section;
11518 dwo_unit->sect_off = sect_off;
11519 dwo_unit->length = cu->per_cu->length;
11520
11521 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11522 sect_offset_str (sect_off),
11523 hex_string (dwo_unit->signature));
11524 }
11525
11526 /* Create the dwo_units for the CUs in a DWO_FILE.
11527 Note: This function processes DWO files only, not DWP files. */
11528
11529 static void
11530 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11531 dwarf2_cu *cu, struct dwo_file &dwo_file,
11532 dwarf2_section_info &section, htab_up &cus_htab)
11533 {
11534 struct objfile *objfile = per_objfile->objfile;
11535 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11536 const gdb_byte *info_ptr, *end_ptr;
11537
11538 section.read (objfile);
11539 info_ptr = section.buffer;
11540
11541 if (info_ptr == NULL)
11542 return;
11543
11544 dwarf_read_debug_printf ("Reading %s for %s:",
11545 section.get_name (),
11546 section.get_file_name ());
11547
11548 end_ptr = info_ptr + section.size;
11549 while (info_ptr < end_ptr)
11550 {
11551 struct dwarf2_per_cu_data per_cu;
11552 struct dwo_unit read_unit {};
11553 struct dwo_unit *dwo_unit;
11554 void **slot;
11555 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11556
11557 memset (&per_cu, 0, sizeof (per_cu));
11558 per_cu.per_bfd = per_bfd;
11559 per_cu.is_debug_types = 0;
11560 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11561 per_cu.section = &section;
11562
11563 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11564 if (!reader.dummy_p)
11565 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11566 &dwo_file, &read_unit);
11567 info_ptr += per_cu.length;
11568
11569 // If the unit could not be parsed, skip it.
11570 if (read_unit.dwo_file == NULL)
11571 continue;
11572
11573 if (cus_htab == NULL)
11574 cus_htab = allocate_dwo_unit_table ();
11575
11576 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11577 struct dwo_unit);
11578 *dwo_unit = read_unit;
11579 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11580 gdb_assert (slot != NULL);
11581 if (*slot != NULL)
11582 {
11583 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11584 sect_offset dup_sect_off = dup_cu->sect_off;
11585
11586 complaint (_("debug cu entry at offset %s is duplicate to"
11587 " the entry at offset %s, signature %s"),
11588 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11589 hex_string (dwo_unit->signature));
11590 }
11591 *slot = (void *)dwo_unit;
11592 }
11593 }
11594
11595 /* DWP file .debug_{cu,tu}_index section format:
11596 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11597 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11598
11599 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11600 officially standard DWP format was published with DWARF v5 and is called
11601 Version 5. There are no versions 3 or 4.
11602
11603 DWP Version 1:
11604
11605 Both index sections have the same format, and serve to map a 64-bit
11606 signature to a set of section numbers. Each section begins with a header,
11607 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11608 indexes, and a pool of 32-bit section numbers. The index sections will be
11609 aligned at 8-byte boundaries in the file.
11610
11611 The index section header consists of:
11612
11613 V, 32 bit version number
11614 -, 32 bits unused
11615 N, 32 bit number of compilation units or type units in the index
11616 M, 32 bit number of slots in the hash table
11617
11618 Numbers are recorded using the byte order of the application binary.
11619
11620 The hash table begins at offset 16 in the section, and consists of an array
11621 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11622 order of the application binary). Unused slots in the hash table are 0.
11623 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11624
11625 The parallel table begins immediately after the hash table
11626 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11627 array of 32-bit indexes (using the byte order of the application binary),
11628 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11629 table contains a 32-bit index into the pool of section numbers. For unused
11630 hash table slots, the corresponding entry in the parallel table will be 0.
11631
11632 The pool of section numbers begins immediately following the hash table
11633 (at offset 16 + 12 * M from the beginning of the section). The pool of
11634 section numbers consists of an array of 32-bit words (using the byte order
11635 of the application binary). Each item in the array is indexed starting
11636 from 0. The hash table entry provides the index of the first section
11637 number in the set. Additional section numbers in the set follow, and the
11638 set is terminated by a 0 entry (section number 0 is not used in ELF).
11639
11640 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11641 section must be the first entry in the set, and the .debug_abbrev.dwo must
11642 be the second entry. Other members of the set may follow in any order.
11643
11644 ---
11645
11646 DWP Versions 2 and 5:
11647
11648 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11649 and the entries in the index tables are now offsets into these sections.
11650 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11651 section.
11652
11653 Index Section Contents:
11654 Header
11655 Hash Table of Signatures dwp_hash_table.hash_table
11656 Parallel Table of Indices dwp_hash_table.unit_table
11657 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11658 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11659
11660 The index section header consists of:
11661
11662 V, 32 bit version number
11663 L, 32 bit number of columns in the table of section offsets
11664 N, 32 bit number of compilation units or type units in the index
11665 M, 32 bit number of slots in the hash table
11666
11667 Numbers are recorded using the byte order of the application binary.
11668
11669 The hash table has the same format as version 1.
11670 The parallel table of indices has the same format as version 1,
11671 except that the entries are origin-1 indices into the table of sections
11672 offsets and the table of section sizes.
11673
11674 The table of offsets begins immediately following the parallel table
11675 (at offset 16 + 12 * M from the beginning of the section). The table is
11676 a two-dimensional array of 32-bit words (using the byte order of the
11677 application binary), with L columns and N+1 rows, in row-major order.
11678 Each row in the array is indexed starting from 0. The first row provides
11679 a key to the remaining rows: each column in this row provides an identifier
11680 for a debug section, and the offsets in the same column of subsequent rows
11681 refer to that section. The section identifiers for Version 2 are:
11682
11683 DW_SECT_INFO 1 .debug_info.dwo
11684 DW_SECT_TYPES 2 .debug_types.dwo
11685 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11686 DW_SECT_LINE 4 .debug_line.dwo
11687 DW_SECT_LOC 5 .debug_loc.dwo
11688 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11689 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11690 DW_SECT_MACRO 8 .debug_macro.dwo
11691
11692 The section identifiers for Version 5 are:
11693
11694 DW_SECT_INFO_V5 1 .debug_info.dwo
11695 DW_SECT_RESERVED_V5 2 --
11696 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11697 DW_SECT_LINE_V5 4 .debug_line.dwo
11698 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11699 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11700 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11701 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11702
11703 The offsets provided by the CU and TU index sections are the base offsets
11704 for the contributions made by each CU or TU to the corresponding section
11705 in the package file. Each CU and TU header contains an abbrev_offset
11706 field, used to find the abbreviations table for that CU or TU within the
11707 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11708 be interpreted as relative to the base offset given in the index section.
11709 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11710 should be interpreted as relative to the base offset for .debug_line.dwo,
11711 and offsets into other debug sections obtained from DWARF attributes should
11712 also be interpreted as relative to the corresponding base offset.
11713
11714 The table of sizes begins immediately following the table of offsets.
11715 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11716 with L columns and N rows, in row-major order. Each row in the array is
11717 indexed starting from 1 (row 0 is shared by the two tables).
11718
11719 ---
11720
11721 Hash table lookup is handled the same in version 1 and 2:
11722
11723 We assume that N and M will not exceed 2^32 - 1.
11724 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11725
11726 Given a 64-bit compilation unit signature or a type signature S, an entry
11727 in the hash table is located as follows:
11728
11729 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11730 the low-order k bits all set to 1.
11731
11732 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11733
11734 3) If the hash table entry at index H matches the signature, use that
11735 entry. If the hash table entry at index H is unused (all zeroes),
11736 terminate the search: the signature is not present in the table.
11737
11738 4) Let H = (H + H') modulo M. Repeat at Step 3.
11739
11740 Because M > N and H' and M are relatively prime, the search is guaranteed
11741 to stop at an unused slot or find the match. */
11742
11743 /* Create a hash table to map DWO IDs to their CU/TU entry in
11744 .debug_{info,types}.dwo in DWP_FILE.
11745 Returns NULL if there isn't one.
11746 Note: This function processes DWP files only, not DWO files. */
11747
11748 static struct dwp_hash_table *
11749 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11750 struct dwp_file *dwp_file, int is_debug_types)
11751 {
11752 struct objfile *objfile = per_objfile->objfile;
11753 bfd *dbfd = dwp_file->dbfd.get ();
11754 const gdb_byte *index_ptr, *index_end;
11755 struct dwarf2_section_info *index;
11756 uint32_t version, nr_columns, nr_units, nr_slots;
11757 struct dwp_hash_table *htab;
11758
11759 if (is_debug_types)
11760 index = &dwp_file->sections.tu_index;
11761 else
11762 index = &dwp_file->sections.cu_index;
11763
11764 if (index->empty ())
11765 return NULL;
11766 index->read (objfile);
11767
11768 index_ptr = index->buffer;
11769 index_end = index_ptr + index->size;
11770
11771 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11772 For now it's safe to just read 4 bytes (particularly as it's difficult to
11773 tell if you're dealing with Version 5 before you've read the version). */
11774 version = read_4_bytes (dbfd, index_ptr);
11775 index_ptr += 4;
11776 if (version == 2 || version == 5)
11777 nr_columns = read_4_bytes (dbfd, index_ptr);
11778 else
11779 nr_columns = 0;
11780 index_ptr += 4;
11781 nr_units = read_4_bytes (dbfd, index_ptr);
11782 index_ptr += 4;
11783 nr_slots = read_4_bytes (dbfd, index_ptr);
11784 index_ptr += 4;
11785
11786 if (version != 1 && version != 2 && version != 5)
11787 {
11788 error (_("Dwarf Error: unsupported DWP file version (%s)"
11789 " [in module %s]"),
11790 pulongest (version), dwp_file->name);
11791 }
11792 if (nr_slots != (nr_slots & -nr_slots))
11793 {
11794 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11795 " is not power of 2 [in module %s]"),
11796 pulongest (nr_slots), dwp_file->name);
11797 }
11798
11799 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11800 htab->version = version;
11801 htab->nr_columns = nr_columns;
11802 htab->nr_units = nr_units;
11803 htab->nr_slots = nr_slots;
11804 htab->hash_table = index_ptr;
11805 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11806
11807 /* Exit early if the table is empty. */
11808 if (nr_slots == 0 || nr_units == 0
11809 || (version == 2 && nr_columns == 0)
11810 || (version == 5 && nr_columns == 0))
11811 {
11812 /* All must be zero. */
11813 if (nr_slots != 0 || nr_units != 0
11814 || (version == 2 && nr_columns != 0)
11815 || (version == 5 && nr_columns != 0))
11816 {
11817 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11818 " all zero [in modules %s]"),
11819 dwp_file->name);
11820 }
11821 return htab;
11822 }
11823
11824 if (version == 1)
11825 {
11826 htab->section_pool.v1.indices =
11827 htab->unit_table + sizeof (uint32_t) * nr_slots;
11828 /* It's harder to decide whether the section is too small in v1.
11829 V1 is deprecated anyway so we punt. */
11830 }
11831 else if (version == 2)
11832 {
11833 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11834 int *ids = htab->section_pool.v2.section_ids;
11835 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11836 /* Reverse map for error checking. */
11837 int ids_seen[DW_SECT_MAX + 1];
11838 int i;
11839
11840 if (nr_columns < 2)
11841 {
11842 error (_("Dwarf Error: bad DWP hash table, too few columns"
11843 " in section table [in module %s]"),
11844 dwp_file->name);
11845 }
11846 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11847 {
11848 error (_("Dwarf Error: bad DWP hash table, too many columns"
11849 " in section table [in module %s]"),
11850 dwp_file->name);
11851 }
11852 memset (ids, 255, sizeof_ids);
11853 memset (ids_seen, 255, sizeof (ids_seen));
11854 for (i = 0; i < nr_columns; ++i)
11855 {
11856 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11857
11858 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11859 {
11860 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11861 " in section table [in module %s]"),
11862 id, dwp_file->name);
11863 }
11864 if (ids_seen[id] != -1)
11865 {
11866 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11867 " id %d in section table [in module %s]"),
11868 id, dwp_file->name);
11869 }
11870 ids_seen[id] = i;
11871 ids[i] = id;
11872 }
11873 /* Must have exactly one info or types section. */
11874 if (((ids_seen[DW_SECT_INFO] != -1)
11875 + (ids_seen[DW_SECT_TYPES] != -1))
11876 != 1)
11877 {
11878 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11879 " DWO info/types section [in module %s]"),
11880 dwp_file->name);
11881 }
11882 /* Must have an abbrev section. */
11883 if (ids_seen[DW_SECT_ABBREV] == -1)
11884 {
11885 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11886 " section [in module %s]"),
11887 dwp_file->name);
11888 }
11889 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11890 htab->section_pool.v2.sizes =
11891 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11892 * nr_units * nr_columns);
11893 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11894 * nr_units * nr_columns))
11895 > index_end)
11896 {
11897 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11898 " [in module %s]"),
11899 dwp_file->name);
11900 }
11901 }
11902 else /* version == 5 */
11903 {
11904 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11905 int *ids = htab->section_pool.v5.section_ids;
11906 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11907 /* Reverse map for error checking. */
11908 int ids_seen[DW_SECT_MAX_V5 + 1];
11909
11910 if (nr_columns < 2)
11911 {
11912 error (_("Dwarf Error: bad DWP hash table, too few columns"
11913 " in section table [in module %s]"),
11914 dwp_file->name);
11915 }
11916 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11917 {
11918 error (_("Dwarf Error: bad DWP hash table, too many columns"
11919 " in section table [in module %s]"),
11920 dwp_file->name);
11921 }
11922 memset (ids, 255, sizeof_ids);
11923 memset (ids_seen, 255, sizeof (ids_seen));
11924 for (int i = 0; i < nr_columns; ++i)
11925 {
11926 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11927
11928 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11929 {
11930 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11931 " in section table [in module %s]"),
11932 id, dwp_file->name);
11933 }
11934 if (ids_seen[id] != -1)
11935 {
11936 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11937 " id %d in section table [in module %s]"),
11938 id, dwp_file->name);
11939 }
11940 ids_seen[id] = i;
11941 ids[i] = id;
11942 }
11943 /* Must have seen an info section. */
11944 if (ids_seen[DW_SECT_INFO_V5] == -1)
11945 {
11946 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11947 " DWO info/types section [in module %s]"),
11948 dwp_file->name);
11949 }
11950 /* Must have an abbrev section. */
11951 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11952 {
11953 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11954 " section [in module %s]"),
11955 dwp_file->name);
11956 }
11957 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11958 htab->section_pool.v5.sizes
11959 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11960 * nr_units * nr_columns);
11961 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11962 * nr_units * nr_columns))
11963 > index_end)
11964 {
11965 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11966 " [in module %s]"),
11967 dwp_file->name);
11968 }
11969 }
11970
11971 return htab;
11972 }
11973
11974 /* Update SECTIONS with the data from SECTP.
11975
11976 This function is like the other "locate" section routines, but in
11977 this context the sections to read comes from the DWP V1 hash table,
11978 not the full ELF section table.
11979
11980 The result is non-zero for success, or zero if an error was found. */
11981
11982 static int
11983 locate_v1_virtual_dwo_sections (asection *sectp,
11984 struct virtual_v1_dwo_sections *sections)
11985 {
11986 const struct dwop_section_names *names = &dwop_section_names;
11987
11988 if (names->abbrev_dwo.matches (sectp->name))
11989 {
11990 /* There can be only one. */
11991 if (sections->abbrev.s.section != NULL)
11992 return 0;
11993 sections->abbrev.s.section = sectp;
11994 sections->abbrev.size = bfd_section_size (sectp);
11995 }
11996 else if (names->info_dwo.matches (sectp->name)
11997 || names->types_dwo.matches (sectp->name))
11998 {
11999 /* There can be only one. */
12000 if (sections->info_or_types.s.section != NULL)
12001 return 0;
12002 sections->info_or_types.s.section = sectp;
12003 sections->info_or_types.size = bfd_section_size (sectp);
12004 }
12005 else if (names->line_dwo.matches (sectp->name))
12006 {
12007 /* There can be only one. */
12008 if (sections->line.s.section != NULL)
12009 return 0;
12010 sections->line.s.section = sectp;
12011 sections->line.size = bfd_section_size (sectp);
12012 }
12013 else if (names->loc_dwo.matches (sectp->name))
12014 {
12015 /* There can be only one. */
12016 if (sections->loc.s.section != NULL)
12017 return 0;
12018 sections->loc.s.section = sectp;
12019 sections->loc.size = bfd_section_size (sectp);
12020 }
12021 else if (names->macinfo_dwo.matches (sectp->name))
12022 {
12023 /* There can be only one. */
12024 if (sections->macinfo.s.section != NULL)
12025 return 0;
12026 sections->macinfo.s.section = sectp;
12027 sections->macinfo.size = bfd_section_size (sectp);
12028 }
12029 else if (names->macro_dwo.matches (sectp->name))
12030 {
12031 /* There can be only one. */
12032 if (sections->macro.s.section != NULL)
12033 return 0;
12034 sections->macro.s.section = sectp;
12035 sections->macro.size = bfd_section_size (sectp);
12036 }
12037 else if (names->str_offsets_dwo.matches (sectp->name))
12038 {
12039 /* There can be only one. */
12040 if (sections->str_offsets.s.section != NULL)
12041 return 0;
12042 sections->str_offsets.s.section = sectp;
12043 sections->str_offsets.size = bfd_section_size (sectp);
12044 }
12045 else
12046 {
12047 /* No other kind of section is valid. */
12048 return 0;
12049 }
12050
12051 return 1;
12052 }
12053
12054 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12055 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12056 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12057 This is for DWP version 1 files. */
12058
12059 static struct dwo_unit *
12060 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12061 struct dwp_file *dwp_file,
12062 uint32_t unit_index,
12063 const char *comp_dir,
12064 ULONGEST signature, int is_debug_types)
12065 {
12066 const struct dwp_hash_table *dwp_htab =
12067 is_debug_types ? dwp_file->tus : dwp_file->cus;
12068 bfd *dbfd = dwp_file->dbfd.get ();
12069 const char *kind = is_debug_types ? "TU" : "CU";
12070 struct dwo_file *dwo_file;
12071 struct dwo_unit *dwo_unit;
12072 struct virtual_v1_dwo_sections sections;
12073 void **dwo_file_slot;
12074 int i;
12075
12076 gdb_assert (dwp_file->version == 1);
12077
12078 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12079 kind, pulongest (unit_index), hex_string (signature),
12080 dwp_file->name);
12081
12082 /* Fetch the sections of this DWO unit.
12083 Put a limit on the number of sections we look for so that bad data
12084 doesn't cause us to loop forever. */
12085
12086 #define MAX_NR_V1_DWO_SECTIONS \
12087 (1 /* .debug_info or .debug_types */ \
12088 + 1 /* .debug_abbrev */ \
12089 + 1 /* .debug_line */ \
12090 + 1 /* .debug_loc */ \
12091 + 1 /* .debug_str_offsets */ \
12092 + 1 /* .debug_macro or .debug_macinfo */ \
12093 + 1 /* trailing zero */)
12094
12095 memset (&sections, 0, sizeof (sections));
12096
12097 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12098 {
12099 asection *sectp;
12100 uint32_t section_nr =
12101 read_4_bytes (dbfd,
12102 dwp_htab->section_pool.v1.indices
12103 + (unit_index + i) * sizeof (uint32_t));
12104
12105 if (section_nr == 0)
12106 break;
12107 if (section_nr >= dwp_file->num_sections)
12108 {
12109 error (_("Dwarf Error: bad DWP hash table, section number too large"
12110 " [in module %s]"),
12111 dwp_file->name);
12112 }
12113
12114 sectp = dwp_file->elf_sections[section_nr];
12115 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12116 {
12117 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12118 " [in module %s]"),
12119 dwp_file->name);
12120 }
12121 }
12122
12123 if (i < 2
12124 || sections.info_or_types.empty ()
12125 || sections.abbrev.empty ())
12126 {
12127 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12128 " [in module %s]"),
12129 dwp_file->name);
12130 }
12131 if (i == MAX_NR_V1_DWO_SECTIONS)
12132 {
12133 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12134 " [in module %s]"),
12135 dwp_file->name);
12136 }
12137
12138 /* It's easier for the rest of the code if we fake a struct dwo_file and
12139 have dwo_unit "live" in that. At least for now.
12140
12141 The DWP file can be made up of a random collection of CUs and TUs.
12142 However, for each CU + set of TUs that came from the same original DWO
12143 file, we can combine them back into a virtual DWO file to save space
12144 (fewer struct dwo_file objects to allocate). Remember that for really
12145 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12146
12147 std::string virtual_dwo_name =
12148 string_printf ("virtual-dwo/%d-%d-%d-%d",
12149 sections.abbrev.get_id (),
12150 sections.line.get_id (),
12151 sections.loc.get_id (),
12152 sections.str_offsets.get_id ());
12153 /* Can we use an existing virtual DWO file? */
12154 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12155 comp_dir);
12156 /* Create one if necessary. */
12157 if (*dwo_file_slot == NULL)
12158 {
12159 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12160 virtual_dwo_name.c_str ());
12161
12162 dwo_file = new struct dwo_file;
12163 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12164 dwo_file->comp_dir = comp_dir;
12165 dwo_file->sections.abbrev = sections.abbrev;
12166 dwo_file->sections.line = sections.line;
12167 dwo_file->sections.loc = sections.loc;
12168 dwo_file->sections.macinfo = sections.macinfo;
12169 dwo_file->sections.macro = sections.macro;
12170 dwo_file->sections.str_offsets = sections.str_offsets;
12171 /* The "str" section is global to the entire DWP file. */
12172 dwo_file->sections.str = dwp_file->sections.str;
12173 /* The info or types section is assigned below to dwo_unit,
12174 there's no need to record it in dwo_file.
12175 Also, we can't simply record type sections in dwo_file because
12176 we record a pointer into the vector in dwo_unit. As we collect more
12177 types we'll grow the vector and eventually have to reallocate space
12178 for it, invalidating all copies of pointers into the previous
12179 contents. */
12180 *dwo_file_slot = dwo_file;
12181 }
12182 else
12183 {
12184 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12185 virtual_dwo_name.c_str ());
12186
12187 dwo_file = (struct dwo_file *) *dwo_file_slot;
12188 }
12189
12190 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12191 dwo_unit->dwo_file = dwo_file;
12192 dwo_unit->signature = signature;
12193 dwo_unit->section =
12194 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12195 *dwo_unit->section = sections.info_or_types;
12196 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12197
12198 return dwo_unit;
12199 }
12200
12201 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12202 simplify them. Given a pointer to the containing section SECTION, and
12203 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12204 virtual section of just that piece. */
12205
12206 static struct dwarf2_section_info
12207 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12208 struct dwarf2_section_info *section,
12209 bfd_size_type offset, bfd_size_type size)
12210 {
12211 struct dwarf2_section_info result;
12212 asection *sectp;
12213
12214 gdb_assert (section != NULL);
12215 gdb_assert (!section->is_virtual);
12216
12217 memset (&result, 0, sizeof (result));
12218 result.s.containing_section = section;
12219 result.is_virtual = true;
12220
12221 if (size == 0)
12222 return result;
12223
12224 sectp = section->get_bfd_section ();
12225
12226 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12227 bounds of the real section. This is a pretty-rare event, so just
12228 flag an error (easier) instead of a warning and trying to cope. */
12229 if (sectp == NULL
12230 || offset + size > bfd_section_size (sectp))
12231 {
12232 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12233 " in section %s [in module %s]"),
12234 sectp ? bfd_section_name (sectp) : "<unknown>",
12235 objfile_name (per_objfile->objfile));
12236 }
12237
12238 result.virtual_offset = offset;
12239 result.size = size;
12240 return result;
12241 }
12242
12243 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12244 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12245 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12246 This is for DWP version 2 files. */
12247
12248 static struct dwo_unit *
12249 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12250 struct dwp_file *dwp_file,
12251 uint32_t unit_index,
12252 const char *comp_dir,
12253 ULONGEST signature, int is_debug_types)
12254 {
12255 const struct dwp_hash_table *dwp_htab =
12256 is_debug_types ? dwp_file->tus : dwp_file->cus;
12257 bfd *dbfd = dwp_file->dbfd.get ();
12258 const char *kind = is_debug_types ? "TU" : "CU";
12259 struct dwo_file *dwo_file;
12260 struct dwo_unit *dwo_unit;
12261 struct virtual_v2_or_v5_dwo_sections sections;
12262 void **dwo_file_slot;
12263 int i;
12264
12265 gdb_assert (dwp_file->version == 2);
12266
12267 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12268 kind, pulongest (unit_index), hex_string (signature),
12269 dwp_file->name);
12270
12271 /* Fetch the section offsets of this DWO unit. */
12272
12273 memset (&sections, 0, sizeof (sections));
12274
12275 for (i = 0; i < dwp_htab->nr_columns; ++i)
12276 {
12277 uint32_t offset = read_4_bytes (dbfd,
12278 dwp_htab->section_pool.v2.offsets
12279 + (((unit_index - 1) * dwp_htab->nr_columns
12280 + i)
12281 * sizeof (uint32_t)));
12282 uint32_t size = read_4_bytes (dbfd,
12283 dwp_htab->section_pool.v2.sizes
12284 + (((unit_index - 1) * dwp_htab->nr_columns
12285 + i)
12286 * sizeof (uint32_t)));
12287
12288 switch (dwp_htab->section_pool.v2.section_ids[i])
12289 {
12290 case DW_SECT_INFO:
12291 case DW_SECT_TYPES:
12292 sections.info_or_types_offset = offset;
12293 sections.info_or_types_size = size;
12294 break;
12295 case DW_SECT_ABBREV:
12296 sections.abbrev_offset = offset;
12297 sections.abbrev_size = size;
12298 break;
12299 case DW_SECT_LINE:
12300 sections.line_offset = offset;
12301 sections.line_size = size;
12302 break;
12303 case DW_SECT_LOC:
12304 sections.loc_offset = offset;
12305 sections.loc_size = size;
12306 break;
12307 case DW_SECT_STR_OFFSETS:
12308 sections.str_offsets_offset = offset;
12309 sections.str_offsets_size = size;
12310 break;
12311 case DW_SECT_MACINFO:
12312 sections.macinfo_offset = offset;
12313 sections.macinfo_size = size;
12314 break;
12315 case DW_SECT_MACRO:
12316 sections.macro_offset = offset;
12317 sections.macro_size = size;
12318 break;
12319 }
12320 }
12321
12322 /* It's easier for the rest of the code if we fake a struct dwo_file and
12323 have dwo_unit "live" in that. At least for now.
12324
12325 The DWP file can be made up of a random collection of CUs and TUs.
12326 However, for each CU + set of TUs that came from the same original DWO
12327 file, we can combine them back into a virtual DWO file to save space
12328 (fewer struct dwo_file objects to allocate). Remember that for really
12329 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12330
12331 std::string virtual_dwo_name =
12332 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12333 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12334 (long) (sections.line_size ? sections.line_offset : 0),
12335 (long) (sections.loc_size ? sections.loc_offset : 0),
12336 (long) (sections.str_offsets_size
12337 ? sections.str_offsets_offset : 0));
12338 /* Can we use an existing virtual DWO file? */
12339 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12340 comp_dir);
12341 /* Create one if necessary. */
12342 if (*dwo_file_slot == NULL)
12343 {
12344 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12345 virtual_dwo_name.c_str ());
12346
12347 dwo_file = new struct dwo_file;
12348 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12349 dwo_file->comp_dir = comp_dir;
12350 dwo_file->sections.abbrev =
12351 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12352 sections.abbrev_offset,
12353 sections.abbrev_size);
12354 dwo_file->sections.line =
12355 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12356 sections.line_offset,
12357 sections.line_size);
12358 dwo_file->sections.loc =
12359 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12360 sections.loc_offset, sections.loc_size);
12361 dwo_file->sections.macinfo =
12362 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12363 sections.macinfo_offset,
12364 sections.macinfo_size);
12365 dwo_file->sections.macro =
12366 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12367 sections.macro_offset,
12368 sections.macro_size);
12369 dwo_file->sections.str_offsets =
12370 create_dwp_v2_or_v5_section (per_objfile,
12371 &dwp_file->sections.str_offsets,
12372 sections.str_offsets_offset,
12373 sections.str_offsets_size);
12374 /* The "str" section is global to the entire DWP file. */
12375 dwo_file->sections.str = dwp_file->sections.str;
12376 /* The info or types section is assigned below to dwo_unit,
12377 there's no need to record it in dwo_file.
12378 Also, we can't simply record type sections in dwo_file because
12379 we record a pointer into the vector in dwo_unit. As we collect more
12380 types we'll grow the vector and eventually have to reallocate space
12381 for it, invalidating all copies of pointers into the previous
12382 contents. */
12383 *dwo_file_slot = dwo_file;
12384 }
12385 else
12386 {
12387 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12388 virtual_dwo_name.c_str ());
12389
12390 dwo_file = (struct dwo_file *) *dwo_file_slot;
12391 }
12392
12393 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12394 dwo_unit->dwo_file = dwo_file;
12395 dwo_unit->signature = signature;
12396 dwo_unit->section =
12397 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12398 *dwo_unit->section = create_dwp_v2_or_v5_section
12399 (per_objfile,
12400 is_debug_types
12401 ? &dwp_file->sections.types
12402 : &dwp_file->sections.info,
12403 sections.info_or_types_offset,
12404 sections.info_or_types_size);
12405 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12406
12407 return dwo_unit;
12408 }
12409
12410 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12411 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12412 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12413 This is for DWP version 5 files. */
12414
12415 static struct dwo_unit *
12416 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12417 struct dwp_file *dwp_file,
12418 uint32_t unit_index,
12419 const char *comp_dir,
12420 ULONGEST signature, int is_debug_types)
12421 {
12422 const struct dwp_hash_table *dwp_htab
12423 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12424 bfd *dbfd = dwp_file->dbfd.get ();
12425 const char *kind = is_debug_types ? "TU" : "CU";
12426 struct dwo_file *dwo_file;
12427 struct dwo_unit *dwo_unit;
12428 struct virtual_v2_or_v5_dwo_sections sections {};
12429 void **dwo_file_slot;
12430
12431 gdb_assert (dwp_file->version == 5);
12432
12433 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12434 kind, pulongest (unit_index), hex_string (signature),
12435 dwp_file->name);
12436
12437 /* Fetch the section offsets of this DWO unit. */
12438
12439 /* memset (&sections, 0, sizeof (sections)); */
12440
12441 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12442 {
12443 uint32_t offset = read_4_bytes (dbfd,
12444 dwp_htab->section_pool.v5.offsets
12445 + (((unit_index - 1)
12446 * dwp_htab->nr_columns
12447 + i)
12448 * sizeof (uint32_t)));
12449 uint32_t size = read_4_bytes (dbfd,
12450 dwp_htab->section_pool.v5.sizes
12451 + (((unit_index - 1) * dwp_htab->nr_columns
12452 + i)
12453 * sizeof (uint32_t)));
12454
12455 switch (dwp_htab->section_pool.v5.section_ids[i])
12456 {
12457 case DW_SECT_ABBREV_V5:
12458 sections.abbrev_offset = offset;
12459 sections.abbrev_size = size;
12460 break;
12461 case DW_SECT_INFO_V5:
12462 sections.info_or_types_offset = offset;
12463 sections.info_or_types_size = size;
12464 break;
12465 case DW_SECT_LINE_V5:
12466 sections.line_offset = offset;
12467 sections.line_size = size;
12468 break;
12469 case DW_SECT_LOCLISTS_V5:
12470 sections.loclists_offset = offset;
12471 sections.loclists_size = size;
12472 break;
12473 case DW_SECT_MACRO_V5:
12474 sections.macro_offset = offset;
12475 sections.macro_size = size;
12476 break;
12477 case DW_SECT_RNGLISTS_V5:
12478 sections.rnglists_offset = offset;
12479 sections.rnglists_size = size;
12480 break;
12481 case DW_SECT_STR_OFFSETS_V5:
12482 sections.str_offsets_offset = offset;
12483 sections.str_offsets_size = size;
12484 break;
12485 case DW_SECT_RESERVED_V5:
12486 default:
12487 break;
12488 }
12489 }
12490
12491 /* It's easier for the rest of the code if we fake a struct dwo_file and
12492 have dwo_unit "live" in that. At least for now.
12493
12494 The DWP file can be made up of a random collection of CUs and TUs.
12495 However, for each CU + set of TUs that came from the same original DWO
12496 file, we can combine them back into a virtual DWO file to save space
12497 (fewer struct dwo_file objects to allocate). Remember that for really
12498 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12499
12500 std::string virtual_dwo_name =
12501 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12502 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12503 (long) (sections.line_size ? sections.line_offset : 0),
12504 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12505 (long) (sections.str_offsets_size
12506 ? sections.str_offsets_offset : 0),
12507 (long) (sections.macro_size ? sections.macro_offset : 0),
12508 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12509 /* Can we use an existing virtual DWO file? */
12510 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12511 virtual_dwo_name.c_str (),
12512 comp_dir);
12513 /* Create one if necessary. */
12514 if (*dwo_file_slot == NULL)
12515 {
12516 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12517 virtual_dwo_name.c_str ());
12518
12519 dwo_file = new struct dwo_file;
12520 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12521 dwo_file->comp_dir = comp_dir;
12522 dwo_file->sections.abbrev =
12523 create_dwp_v2_or_v5_section (per_objfile,
12524 &dwp_file->sections.abbrev,
12525 sections.abbrev_offset,
12526 sections.abbrev_size);
12527 dwo_file->sections.line =
12528 create_dwp_v2_or_v5_section (per_objfile,
12529 &dwp_file->sections.line,
12530 sections.line_offset, sections.line_size);
12531 dwo_file->sections.macro =
12532 create_dwp_v2_or_v5_section (per_objfile,
12533 &dwp_file->sections.macro,
12534 sections.macro_offset,
12535 sections.macro_size);
12536 dwo_file->sections.loclists =
12537 create_dwp_v2_or_v5_section (per_objfile,
12538 &dwp_file->sections.loclists,
12539 sections.loclists_offset,
12540 sections.loclists_size);
12541 dwo_file->sections.rnglists =
12542 create_dwp_v2_or_v5_section (per_objfile,
12543 &dwp_file->sections.rnglists,
12544 sections.rnglists_offset,
12545 sections.rnglists_size);
12546 dwo_file->sections.str_offsets =
12547 create_dwp_v2_or_v5_section (per_objfile,
12548 &dwp_file->sections.str_offsets,
12549 sections.str_offsets_offset,
12550 sections.str_offsets_size);
12551 /* The "str" section is global to the entire DWP file. */
12552 dwo_file->sections.str = dwp_file->sections.str;
12553 /* The info or types section is assigned below to dwo_unit,
12554 there's no need to record it in dwo_file.
12555 Also, we can't simply record type sections in dwo_file because
12556 we record a pointer into the vector in dwo_unit. As we collect more
12557 types we'll grow the vector and eventually have to reallocate space
12558 for it, invalidating all copies of pointers into the previous
12559 contents. */
12560 *dwo_file_slot = dwo_file;
12561 }
12562 else
12563 {
12564 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12565 virtual_dwo_name.c_str ());
12566
12567 dwo_file = (struct dwo_file *) *dwo_file_slot;
12568 }
12569
12570 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12571 dwo_unit->dwo_file = dwo_file;
12572 dwo_unit->signature = signature;
12573 dwo_unit->section
12574 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12575 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12576 &dwp_file->sections.info,
12577 sections.info_or_types_offset,
12578 sections.info_or_types_size);
12579 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12580
12581 return dwo_unit;
12582 }
12583
12584 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12585 Returns NULL if the signature isn't found. */
12586
12587 static struct dwo_unit *
12588 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12589 struct dwp_file *dwp_file, const char *comp_dir,
12590 ULONGEST signature, int is_debug_types)
12591 {
12592 const struct dwp_hash_table *dwp_htab =
12593 is_debug_types ? dwp_file->tus : dwp_file->cus;
12594 bfd *dbfd = dwp_file->dbfd.get ();
12595 uint32_t mask = dwp_htab->nr_slots - 1;
12596 uint32_t hash = signature & mask;
12597 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12598 unsigned int i;
12599 void **slot;
12600 struct dwo_unit find_dwo_cu;
12601
12602 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12603 find_dwo_cu.signature = signature;
12604 slot = htab_find_slot (is_debug_types
12605 ? dwp_file->loaded_tus.get ()
12606 : dwp_file->loaded_cus.get (),
12607 &find_dwo_cu, INSERT);
12608
12609 if (*slot != NULL)
12610 return (struct dwo_unit *) *slot;
12611
12612 /* Use a for loop so that we don't loop forever on bad debug info. */
12613 for (i = 0; i < dwp_htab->nr_slots; ++i)
12614 {
12615 ULONGEST signature_in_table;
12616
12617 signature_in_table =
12618 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12619 if (signature_in_table == signature)
12620 {
12621 uint32_t unit_index =
12622 read_4_bytes (dbfd,
12623 dwp_htab->unit_table + hash * sizeof (uint32_t));
12624
12625 if (dwp_file->version == 1)
12626 {
12627 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12628 unit_index, comp_dir,
12629 signature, is_debug_types);
12630 }
12631 else if (dwp_file->version == 2)
12632 {
12633 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12634 unit_index, comp_dir,
12635 signature, is_debug_types);
12636 }
12637 else /* version == 5 */
12638 {
12639 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12640 unit_index, comp_dir,
12641 signature, is_debug_types);
12642 }
12643 return (struct dwo_unit *) *slot;
12644 }
12645 if (signature_in_table == 0)
12646 return NULL;
12647 hash = (hash + hash2) & mask;
12648 }
12649
12650 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12651 " [in module %s]"),
12652 dwp_file->name);
12653 }
12654
12655 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12656 Open the file specified by FILE_NAME and hand it off to BFD for
12657 preliminary analysis. Return a newly initialized bfd *, which
12658 includes a canonicalized copy of FILE_NAME.
12659 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12660 SEARCH_CWD is true if the current directory is to be searched.
12661 It will be searched before debug-file-directory.
12662 If successful, the file is added to the bfd include table of the
12663 objfile's bfd (see gdb_bfd_record_inclusion).
12664 If unable to find/open the file, return NULL.
12665 NOTE: This function is derived from symfile_bfd_open. */
12666
12667 static gdb_bfd_ref_ptr
12668 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12669 const char *file_name, int is_dwp, int search_cwd)
12670 {
12671 int desc;
12672 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12673 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12674 to debug_file_directory. */
12675 const char *search_path;
12676 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12677
12678 gdb::unique_xmalloc_ptr<char> search_path_holder;
12679 if (search_cwd)
12680 {
12681 if (*debug_file_directory != '\0')
12682 {
12683 search_path_holder.reset (concat (".", dirname_separator_string,
12684 debug_file_directory,
12685 (char *) NULL));
12686 search_path = search_path_holder.get ();
12687 }
12688 else
12689 search_path = ".";
12690 }
12691 else
12692 search_path = debug_file_directory;
12693
12694 openp_flags flags = OPF_RETURN_REALPATH;
12695 if (is_dwp)
12696 flags |= OPF_SEARCH_IN_PATH;
12697
12698 gdb::unique_xmalloc_ptr<char> absolute_name;
12699 desc = openp (search_path, flags, file_name,
12700 O_RDONLY | O_BINARY, &absolute_name);
12701 if (desc < 0)
12702 return NULL;
12703
12704 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12705 gnutarget, desc));
12706 if (sym_bfd == NULL)
12707 return NULL;
12708 bfd_set_cacheable (sym_bfd.get (), 1);
12709
12710 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12711 return NULL;
12712
12713 /* Success. Record the bfd as having been included by the objfile's bfd.
12714 This is important because things like demangled_names_hash lives in the
12715 objfile's per_bfd space and may have references to things like symbol
12716 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12717 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12718
12719 return sym_bfd;
12720 }
12721
12722 /* Try to open DWO file FILE_NAME.
12723 COMP_DIR is the DW_AT_comp_dir attribute.
12724 The result is the bfd handle of the file.
12725 If there is a problem finding or opening the file, return NULL.
12726 Upon success, the canonicalized path of the file is stored in the bfd,
12727 same as symfile_bfd_open. */
12728
12729 static gdb_bfd_ref_ptr
12730 open_dwo_file (dwarf2_per_objfile *per_objfile,
12731 const char *file_name, const char *comp_dir)
12732 {
12733 if (IS_ABSOLUTE_PATH (file_name))
12734 return try_open_dwop_file (per_objfile, file_name,
12735 0 /*is_dwp*/, 0 /*search_cwd*/);
12736
12737 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12738
12739 if (comp_dir != NULL)
12740 {
12741 gdb::unique_xmalloc_ptr<char> path_to_try
12742 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12743
12744 /* NOTE: If comp_dir is a relative path, this will also try the
12745 search path, which seems useful. */
12746 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12747 0 /*is_dwp*/,
12748 1 /*search_cwd*/));
12749 if (abfd != NULL)
12750 return abfd;
12751 }
12752
12753 /* That didn't work, try debug-file-directory, which, despite its name,
12754 is a list of paths. */
12755
12756 if (*debug_file_directory == '\0')
12757 return NULL;
12758
12759 return try_open_dwop_file (per_objfile, file_name,
12760 0 /*is_dwp*/, 1 /*search_cwd*/);
12761 }
12762
12763 /* This function is mapped across the sections and remembers the offset and
12764 size of each of the DWO debugging sections we are interested in. */
12765
12766 static void
12767 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12768 dwo_sections *dwo_sections)
12769 {
12770 const struct dwop_section_names *names = &dwop_section_names;
12771
12772 if (names->abbrev_dwo.matches (sectp->name))
12773 {
12774 dwo_sections->abbrev.s.section = sectp;
12775 dwo_sections->abbrev.size = bfd_section_size (sectp);
12776 }
12777 else if (names->info_dwo.matches (sectp->name))
12778 {
12779 dwo_sections->info.s.section = sectp;
12780 dwo_sections->info.size = bfd_section_size (sectp);
12781 }
12782 else if (names->line_dwo.matches (sectp->name))
12783 {
12784 dwo_sections->line.s.section = sectp;
12785 dwo_sections->line.size = bfd_section_size (sectp);
12786 }
12787 else if (names->loc_dwo.matches (sectp->name))
12788 {
12789 dwo_sections->loc.s.section = sectp;
12790 dwo_sections->loc.size = bfd_section_size (sectp);
12791 }
12792 else if (names->loclists_dwo.matches (sectp->name))
12793 {
12794 dwo_sections->loclists.s.section = sectp;
12795 dwo_sections->loclists.size = bfd_section_size (sectp);
12796 }
12797 else if (names->macinfo_dwo.matches (sectp->name))
12798 {
12799 dwo_sections->macinfo.s.section = sectp;
12800 dwo_sections->macinfo.size = bfd_section_size (sectp);
12801 }
12802 else if (names->macro_dwo.matches (sectp->name))
12803 {
12804 dwo_sections->macro.s.section = sectp;
12805 dwo_sections->macro.size = bfd_section_size (sectp);
12806 }
12807 else if (names->rnglists_dwo.matches (sectp->name))
12808 {
12809 dwo_sections->rnglists.s.section = sectp;
12810 dwo_sections->rnglists.size = bfd_section_size (sectp);
12811 }
12812 else if (names->str_dwo.matches (sectp->name))
12813 {
12814 dwo_sections->str.s.section = sectp;
12815 dwo_sections->str.size = bfd_section_size (sectp);
12816 }
12817 else if (names->str_offsets_dwo.matches (sectp->name))
12818 {
12819 dwo_sections->str_offsets.s.section = sectp;
12820 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12821 }
12822 else if (names->types_dwo.matches (sectp->name))
12823 {
12824 struct dwarf2_section_info type_section;
12825
12826 memset (&type_section, 0, sizeof (type_section));
12827 type_section.s.section = sectp;
12828 type_section.size = bfd_section_size (sectp);
12829 dwo_sections->types.push_back (type_section);
12830 }
12831 }
12832
12833 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12834 by PER_CU. This is for the non-DWP case.
12835 The result is NULL if DWO_NAME can't be found. */
12836
12837 static struct dwo_file *
12838 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12839 const char *comp_dir)
12840 {
12841 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12842
12843 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12844 if (dbfd == NULL)
12845 {
12846 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12847
12848 return NULL;
12849 }
12850
12851 dwo_file_up dwo_file (new struct dwo_file);
12852 dwo_file->dwo_name = dwo_name;
12853 dwo_file->comp_dir = comp_dir;
12854 dwo_file->dbfd = std::move (dbfd);
12855
12856 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12857 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12858 &dwo_file->sections);
12859
12860 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12861 dwo_file->cus);
12862
12863 if (cu->per_cu->dwarf_version < 5)
12864 {
12865 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12866 dwo_file->sections.types, dwo_file->tus);
12867 }
12868 else
12869 {
12870 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12871 &dwo_file->sections.info, dwo_file->tus,
12872 rcuh_kind::COMPILE);
12873 }
12874
12875 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12876
12877 return dwo_file.release ();
12878 }
12879
12880 /* This function is mapped across the sections and remembers the offset and
12881 size of each of the DWP debugging sections common to version 1 and 2 that
12882 we are interested in. */
12883
12884 static void
12885 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12886 dwp_file *dwp_file)
12887 {
12888 const struct dwop_section_names *names = &dwop_section_names;
12889 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12890
12891 /* Record the ELF section number for later lookup: this is what the
12892 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12893 gdb_assert (elf_section_nr < dwp_file->num_sections);
12894 dwp_file->elf_sections[elf_section_nr] = sectp;
12895
12896 /* Look for specific sections that we need. */
12897 if (names->str_dwo.matches (sectp->name))
12898 {
12899 dwp_file->sections.str.s.section = sectp;
12900 dwp_file->sections.str.size = bfd_section_size (sectp);
12901 }
12902 else if (names->cu_index.matches (sectp->name))
12903 {
12904 dwp_file->sections.cu_index.s.section = sectp;
12905 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12906 }
12907 else if (names->tu_index.matches (sectp->name))
12908 {
12909 dwp_file->sections.tu_index.s.section = sectp;
12910 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12911 }
12912 }
12913
12914 /* This function is mapped across the sections and remembers the offset and
12915 size of each of the DWP version 2 debugging sections that we are interested
12916 in. This is split into a separate function because we don't know if we
12917 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12918
12919 static void
12920 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12921 {
12922 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12923 const struct dwop_section_names *names = &dwop_section_names;
12924 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12925
12926 /* Record the ELF section number for later lookup: this is what the
12927 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12928 gdb_assert (elf_section_nr < dwp_file->num_sections);
12929 dwp_file->elf_sections[elf_section_nr] = sectp;
12930
12931 /* Look for specific sections that we need. */
12932 if (names->abbrev_dwo.matches (sectp->name))
12933 {
12934 dwp_file->sections.abbrev.s.section = sectp;
12935 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12936 }
12937 else if (names->info_dwo.matches (sectp->name))
12938 {
12939 dwp_file->sections.info.s.section = sectp;
12940 dwp_file->sections.info.size = bfd_section_size (sectp);
12941 }
12942 else if (names->line_dwo.matches (sectp->name))
12943 {
12944 dwp_file->sections.line.s.section = sectp;
12945 dwp_file->sections.line.size = bfd_section_size (sectp);
12946 }
12947 else if (names->loc_dwo.matches (sectp->name))
12948 {
12949 dwp_file->sections.loc.s.section = sectp;
12950 dwp_file->sections.loc.size = bfd_section_size (sectp);
12951 }
12952 else if (names->macinfo_dwo.matches (sectp->name))
12953 {
12954 dwp_file->sections.macinfo.s.section = sectp;
12955 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12956 }
12957 else if (names->macro_dwo.matches (sectp->name))
12958 {
12959 dwp_file->sections.macro.s.section = sectp;
12960 dwp_file->sections.macro.size = bfd_section_size (sectp);
12961 }
12962 else if (names->str_offsets_dwo.matches (sectp->name))
12963 {
12964 dwp_file->sections.str_offsets.s.section = sectp;
12965 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12966 }
12967 else if (names->types_dwo.matches (sectp->name))
12968 {
12969 dwp_file->sections.types.s.section = sectp;
12970 dwp_file->sections.types.size = bfd_section_size (sectp);
12971 }
12972 }
12973
12974 /* This function is mapped across the sections and remembers the offset and
12975 size of each of the DWP version 5 debugging sections that we are interested
12976 in. This is split into a separate function because we don't know if we
12977 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12978
12979 static void
12980 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12981 {
12982 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12983 const struct dwop_section_names *names = &dwop_section_names;
12984 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12985
12986 /* Record the ELF section number for later lookup: this is what the
12987 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12988 gdb_assert (elf_section_nr < dwp_file->num_sections);
12989 dwp_file->elf_sections[elf_section_nr] = sectp;
12990
12991 /* Look for specific sections that we need. */
12992 if (names->abbrev_dwo.matches (sectp->name))
12993 {
12994 dwp_file->sections.abbrev.s.section = sectp;
12995 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12996 }
12997 else if (names->info_dwo.matches (sectp->name))
12998 {
12999 dwp_file->sections.info.s.section = sectp;
13000 dwp_file->sections.info.size = bfd_section_size (sectp);
13001 }
13002 else if (names->line_dwo.matches (sectp->name))
13003 {
13004 dwp_file->sections.line.s.section = sectp;
13005 dwp_file->sections.line.size = bfd_section_size (sectp);
13006 }
13007 else if (names->loclists_dwo.matches (sectp->name))
13008 {
13009 dwp_file->sections.loclists.s.section = sectp;
13010 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13011 }
13012 else if (names->macro_dwo.matches (sectp->name))
13013 {
13014 dwp_file->sections.macro.s.section = sectp;
13015 dwp_file->sections.macro.size = bfd_section_size (sectp);
13016 }
13017 else if (names->rnglists_dwo.matches (sectp->name))
13018 {
13019 dwp_file->sections.rnglists.s.section = sectp;
13020 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13021 }
13022 else if (names->str_offsets_dwo.matches (sectp->name))
13023 {
13024 dwp_file->sections.str_offsets.s.section = sectp;
13025 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13026 }
13027 }
13028
13029 /* Hash function for dwp_file loaded CUs/TUs. */
13030
13031 static hashval_t
13032 hash_dwp_loaded_cutus (const void *item)
13033 {
13034 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13035
13036 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13037 return dwo_unit->signature;
13038 }
13039
13040 /* Equality function for dwp_file loaded CUs/TUs. */
13041
13042 static int
13043 eq_dwp_loaded_cutus (const void *a, const void *b)
13044 {
13045 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13046 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13047
13048 return dua->signature == dub->signature;
13049 }
13050
13051 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13052
13053 static htab_up
13054 allocate_dwp_loaded_cutus_table ()
13055 {
13056 return htab_up (htab_create_alloc (3,
13057 hash_dwp_loaded_cutus,
13058 eq_dwp_loaded_cutus,
13059 NULL, xcalloc, xfree));
13060 }
13061
13062 /* Try to open DWP file FILE_NAME.
13063 The result is the bfd handle of the file.
13064 If there is a problem finding or opening the file, return NULL.
13065 Upon success, the canonicalized path of the file is stored in the bfd,
13066 same as symfile_bfd_open. */
13067
13068 static gdb_bfd_ref_ptr
13069 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13070 {
13071 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13072 1 /*is_dwp*/,
13073 1 /*search_cwd*/));
13074 if (abfd != NULL)
13075 return abfd;
13076
13077 /* Work around upstream bug 15652.
13078 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13079 [Whether that's a "bug" is debatable, but it is getting in our way.]
13080 We have no real idea where the dwp file is, because gdb's realpath-ing
13081 of the executable's path may have discarded the needed info.
13082 [IWBN if the dwp file name was recorded in the executable, akin to
13083 .gnu_debuglink, but that doesn't exist yet.]
13084 Strip the directory from FILE_NAME and search again. */
13085 if (*debug_file_directory != '\0')
13086 {
13087 /* Don't implicitly search the current directory here.
13088 If the user wants to search "." to handle this case,
13089 it must be added to debug-file-directory. */
13090 return try_open_dwop_file (per_objfile, lbasename (file_name),
13091 1 /*is_dwp*/,
13092 0 /*search_cwd*/);
13093 }
13094
13095 return NULL;
13096 }
13097
13098 /* Initialize the use of the DWP file for the current objfile.
13099 By convention the name of the DWP file is ${objfile}.dwp.
13100 The result is NULL if it can't be found. */
13101
13102 static std::unique_ptr<struct dwp_file>
13103 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13104 {
13105 struct objfile *objfile = per_objfile->objfile;
13106
13107 /* Try to find first .dwp for the binary file before any symbolic links
13108 resolving. */
13109
13110 /* If the objfile is a debug file, find the name of the real binary
13111 file and get the name of dwp file from there. */
13112 std::string dwp_name;
13113 if (objfile->separate_debug_objfile_backlink != NULL)
13114 {
13115 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13116 const char *backlink_basename = lbasename (backlink->original_name);
13117
13118 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13119 }
13120 else
13121 dwp_name = objfile->original_name;
13122
13123 dwp_name += ".dwp";
13124
13125 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13126 if (dbfd == NULL
13127 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13128 {
13129 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13130 dwp_name = objfile_name (objfile);
13131 dwp_name += ".dwp";
13132 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13133 }
13134
13135 if (dbfd == NULL)
13136 {
13137 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13138
13139 return std::unique_ptr<dwp_file> ();
13140 }
13141
13142 const char *name = bfd_get_filename (dbfd.get ());
13143 std::unique_ptr<struct dwp_file> dwp_file
13144 (new struct dwp_file (name, std::move (dbfd)));
13145
13146 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13147 dwp_file->elf_sections =
13148 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13149 dwp_file->num_sections, asection *);
13150
13151 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13152 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13153 dwp_file.get ());
13154
13155 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13156
13157 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13158
13159 /* The DWP file version is stored in the hash table. Oh well. */
13160 if (dwp_file->cus && dwp_file->tus
13161 && dwp_file->cus->version != dwp_file->tus->version)
13162 {
13163 /* Technically speaking, we should try to limp along, but this is
13164 pretty bizarre. We use pulongest here because that's the established
13165 portability solution (e.g, we cannot use %u for uint32_t). */
13166 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13167 " TU version %s [in DWP file %s]"),
13168 pulongest (dwp_file->cus->version),
13169 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13170 }
13171
13172 if (dwp_file->cus)
13173 dwp_file->version = dwp_file->cus->version;
13174 else if (dwp_file->tus)
13175 dwp_file->version = dwp_file->tus->version;
13176 else
13177 dwp_file->version = 2;
13178
13179 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13180 {
13181 if (dwp_file->version == 2)
13182 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13183 dwp_file.get ());
13184 else
13185 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13186 dwp_file.get ());
13187 }
13188
13189 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13190 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13191
13192 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13193 dwarf_read_debug_printf (" %s CUs, %s TUs",
13194 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13195 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13196
13197 return dwp_file;
13198 }
13199
13200 /* Wrapper around open_and_init_dwp_file, only open it once. */
13201
13202 static struct dwp_file *
13203 get_dwp_file (dwarf2_per_objfile *per_objfile)
13204 {
13205 if (!per_objfile->per_bfd->dwp_checked)
13206 {
13207 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13208 per_objfile->per_bfd->dwp_checked = 1;
13209 }
13210 return per_objfile->per_bfd->dwp_file.get ();
13211 }
13212
13213 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13214 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13215 or in the DWP file for the objfile, referenced by THIS_UNIT.
13216 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13217 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13218
13219 This is called, for example, when wanting to read a variable with a
13220 complex location. Therefore we don't want to do file i/o for every call.
13221 Therefore we don't want to look for a DWO file on every call.
13222 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13223 then we check if we've already seen DWO_NAME, and only THEN do we check
13224 for a DWO file.
13225
13226 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13227 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13228
13229 static struct dwo_unit *
13230 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13231 ULONGEST signature, int is_debug_types)
13232 {
13233 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13234 struct objfile *objfile = per_objfile->objfile;
13235 const char *kind = is_debug_types ? "TU" : "CU";
13236 void **dwo_file_slot;
13237 struct dwo_file *dwo_file;
13238 struct dwp_file *dwp_file;
13239
13240 /* First see if there's a DWP file.
13241 If we have a DWP file but didn't find the DWO inside it, don't
13242 look for the original DWO file. It makes gdb behave differently
13243 depending on whether one is debugging in the build tree. */
13244
13245 dwp_file = get_dwp_file (per_objfile);
13246 if (dwp_file != NULL)
13247 {
13248 const struct dwp_hash_table *dwp_htab =
13249 is_debug_types ? dwp_file->tus : dwp_file->cus;
13250
13251 if (dwp_htab != NULL)
13252 {
13253 struct dwo_unit *dwo_cutu =
13254 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13255 is_debug_types);
13256
13257 if (dwo_cutu != NULL)
13258 {
13259 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13260 kind, hex_string (signature),
13261 host_address_to_string (dwo_cutu));
13262
13263 return dwo_cutu;
13264 }
13265 }
13266 }
13267 else
13268 {
13269 /* No DWP file, look for the DWO file. */
13270
13271 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13272 if (*dwo_file_slot == NULL)
13273 {
13274 /* Read in the file and build a table of the CUs/TUs it contains. */
13275 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13276 }
13277 /* NOTE: This will be NULL if unable to open the file. */
13278 dwo_file = (struct dwo_file *) *dwo_file_slot;
13279
13280 if (dwo_file != NULL)
13281 {
13282 struct dwo_unit *dwo_cutu = NULL;
13283
13284 if (is_debug_types && dwo_file->tus)
13285 {
13286 struct dwo_unit find_dwo_cutu;
13287
13288 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13289 find_dwo_cutu.signature = signature;
13290 dwo_cutu
13291 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13292 &find_dwo_cutu);
13293 }
13294 else if (!is_debug_types && dwo_file->cus)
13295 {
13296 struct dwo_unit find_dwo_cutu;
13297
13298 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13299 find_dwo_cutu.signature = signature;
13300 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13301 &find_dwo_cutu);
13302 }
13303
13304 if (dwo_cutu != NULL)
13305 {
13306 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13307 kind, dwo_name, hex_string (signature),
13308 host_address_to_string (dwo_cutu));
13309
13310 return dwo_cutu;
13311 }
13312 }
13313 }
13314
13315 /* We didn't find it. This could mean a dwo_id mismatch, or
13316 someone deleted the DWO/DWP file, or the search path isn't set up
13317 correctly to find the file. */
13318
13319 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13320 kind, dwo_name, hex_string (signature));
13321
13322 /* This is a warning and not a complaint because it can be caused by
13323 pilot error (e.g., user accidentally deleting the DWO). */
13324 {
13325 /* Print the name of the DWP file if we looked there, helps the user
13326 better diagnose the problem. */
13327 std::string dwp_text;
13328
13329 if (dwp_file != NULL)
13330 dwp_text = string_printf (" [in DWP file %s]",
13331 lbasename (dwp_file->name));
13332
13333 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13334 " [in module %s]"),
13335 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13336 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13337 }
13338 return NULL;
13339 }
13340
13341 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13342 See lookup_dwo_cutu_unit for details. */
13343
13344 static struct dwo_unit *
13345 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13346 ULONGEST signature)
13347 {
13348 gdb_assert (!cu->per_cu->is_debug_types);
13349
13350 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13351 }
13352
13353 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13354 See lookup_dwo_cutu_unit for details. */
13355
13356 static struct dwo_unit *
13357 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13358 {
13359 gdb_assert (cu->per_cu->is_debug_types);
13360
13361 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13362
13363 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13364 }
13365
13366 /* Traversal function for queue_and_load_all_dwo_tus. */
13367
13368 static int
13369 queue_and_load_dwo_tu (void **slot, void *info)
13370 {
13371 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13372 dwarf2_cu *cu = (dwarf2_cu *) info;
13373 ULONGEST signature = dwo_unit->signature;
13374 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13375
13376 if (sig_type != NULL)
13377 {
13378 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13379
13380 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13381 a real dependency of PER_CU on SIG_TYPE. That is detected later
13382 while processing PER_CU. */
13383 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13384 load_full_type_unit (sig_cu, cu->per_objfile);
13385 cu->per_cu->imported_symtabs_push (sig_cu);
13386 }
13387
13388 return 1;
13389 }
13390
13391 /* Queue all TUs contained in the DWO of CU to be read in.
13392 The DWO may have the only definition of the type, though it may not be
13393 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13394 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13395
13396 static void
13397 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13398 {
13399 struct dwo_unit *dwo_unit;
13400 struct dwo_file *dwo_file;
13401
13402 gdb_assert (cu != nullptr);
13403 gdb_assert (!cu->per_cu->is_debug_types);
13404 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13405
13406 dwo_unit = cu->dwo_unit;
13407 gdb_assert (dwo_unit != NULL);
13408
13409 dwo_file = dwo_unit->dwo_file;
13410 if (dwo_file->tus != NULL)
13411 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13412 }
13413
13414 /* Read in various DIEs. */
13415
13416 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13417 Inherit only the children of the DW_AT_abstract_origin DIE not being
13418 already referenced by DW_AT_abstract_origin from the children of the
13419 current DIE. */
13420
13421 static void
13422 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13423 {
13424 struct die_info *child_die;
13425 sect_offset *offsetp;
13426 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13427 struct die_info *origin_die;
13428 /* Iterator of the ORIGIN_DIE children. */
13429 struct die_info *origin_child_die;
13430 struct attribute *attr;
13431 struct dwarf2_cu *origin_cu;
13432 struct pending **origin_previous_list_in_scope;
13433
13434 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13435 if (!attr)
13436 return;
13437
13438 /* Note that following die references may follow to a die in a
13439 different cu. */
13440
13441 origin_cu = cu;
13442 origin_die = follow_die_ref (die, attr, &origin_cu);
13443
13444 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13445 symbols in. */
13446 origin_previous_list_in_scope = origin_cu->list_in_scope;
13447 origin_cu->list_in_scope = cu->list_in_scope;
13448
13449 if (die->tag != origin_die->tag
13450 && !(die->tag == DW_TAG_inlined_subroutine
13451 && origin_die->tag == DW_TAG_subprogram))
13452 complaint (_("DIE %s and its abstract origin %s have different tags"),
13453 sect_offset_str (die->sect_off),
13454 sect_offset_str (origin_die->sect_off));
13455
13456 std::vector<sect_offset> offsets;
13457
13458 for (child_die = die->child;
13459 child_die && child_die->tag;
13460 child_die = child_die->sibling)
13461 {
13462 struct die_info *child_origin_die;
13463 struct dwarf2_cu *child_origin_cu;
13464
13465 /* We are trying to process concrete instance entries:
13466 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13467 it's not relevant to our analysis here. i.e. detecting DIEs that are
13468 present in the abstract instance but not referenced in the concrete
13469 one. */
13470 if (child_die->tag == DW_TAG_call_site
13471 || child_die->tag == DW_TAG_GNU_call_site)
13472 continue;
13473
13474 /* For each CHILD_DIE, find the corresponding child of
13475 ORIGIN_DIE. If there is more than one layer of
13476 DW_AT_abstract_origin, follow them all; there shouldn't be,
13477 but GCC versions at least through 4.4 generate this (GCC PR
13478 40573). */
13479 child_origin_die = child_die;
13480 child_origin_cu = cu;
13481 while (1)
13482 {
13483 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13484 child_origin_cu);
13485 if (attr == NULL)
13486 break;
13487 child_origin_die = follow_die_ref (child_origin_die, attr,
13488 &child_origin_cu);
13489 }
13490
13491 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13492 counterpart may exist. */
13493 if (child_origin_die != child_die)
13494 {
13495 if (child_die->tag != child_origin_die->tag
13496 && !(child_die->tag == DW_TAG_inlined_subroutine
13497 && child_origin_die->tag == DW_TAG_subprogram))
13498 complaint (_("Child DIE %s and its abstract origin %s have "
13499 "different tags"),
13500 sect_offset_str (child_die->sect_off),
13501 sect_offset_str (child_origin_die->sect_off));
13502 if (child_origin_die->parent != origin_die)
13503 complaint (_("Child DIE %s and its abstract origin %s have "
13504 "different parents"),
13505 sect_offset_str (child_die->sect_off),
13506 sect_offset_str (child_origin_die->sect_off));
13507 else
13508 offsets.push_back (child_origin_die->sect_off);
13509 }
13510 }
13511 std::sort (offsets.begin (), offsets.end ());
13512 sect_offset *offsets_end = offsets.data () + offsets.size ();
13513 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13514 if (offsetp[-1] == *offsetp)
13515 complaint (_("Multiple children of DIE %s refer "
13516 "to DIE %s as their abstract origin"),
13517 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13518
13519 offsetp = offsets.data ();
13520 origin_child_die = origin_die->child;
13521 while (origin_child_die && origin_child_die->tag)
13522 {
13523 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13524 while (offsetp < offsets_end
13525 && *offsetp < origin_child_die->sect_off)
13526 offsetp++;
13527 if (offsetp >= offsets_end
13528 || *offsetp > origin_child_die->sect_off)
13529 {
13530 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13531 Check whether we're already processing ORIGIN_CHILD_DIE.
13532 This can happen with mutually referenced abstract_origins.
13533 PR 16581. */
13534 if (!origin_child_die->in_process)
13535 process_die (origin_child_die, origin_cu);
13536 }
13537 origin_child_die = origin_child_die->sibling;
13538 }
13539 origin_cu->list_in_scope = origin_previous_list_in_scope;
13540
13541 if (cu != origin_cu)
13542 compute_delayed_physnames (origin_cu);
13543 }
13544
13545 static void
13546 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13547 {
13548 struct objfile *objfile = cu->per_objfile->objfile;
13549 struct gdbarch *gdbarch = objfile->arch ();
13550 struct context_stack *newobj;
13551 CORE_ADDR lowpc;
13552 CORE_ADDR highpc;
13553 struct die_info *child_die;
13554 struct attribute *attr, *call_line, *call_file;
13555 const char *name;
13556 CORE_ADDR baseaddr;
13557 struct block *block;
13558 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13559 std::vector<struct symbol *> template_args;
13560 struct template_symbol *templ_func = NULL;
13561
13562 if (inlined_func)
13563 {
13564 /* If we do not have call site information, we can't show the
13565 caller of this inlined function. That's too confusing, so
13566 only use the scope for local variables. */
13567 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13568 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13569 if (call_line == NULL || call_file == NULL)
13570 {
13571 read_lexical_block_scope (die, cu);
13572 return;
13573 }
13574 }
13575
13576 baseaddr = objfile->text_section_offset ();
13577
13578 name = dwarf2_name (die, cu);
13579
13580 /* Ignore functions with missing or empty names. These are actually
13581 illegal according to the DWARF standard. */
13582 if (name == NULL)
13583 {
13584 complaint (_("missing name for subprogram DIE at %s"),
13585 sect_offset_str (die->sect_off));
13586 return;
13587 }
13588
13589 /* Ignore functions with missing or invalid low and high pc attributes. */
13590 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13591 <= PC_BOUNDS_INVALID)
13592 {
13593 attr = dwarf2_attr (die, DW_AT_external, cu);
13594 if (attr == nullptr || !attr->as_boolean ())
13595 complaint (_("cannot get low and high bounds "
13596 "for subprogram DIE at %s"),
13597 sect_offset_str (die->sect_off));
13598 return;
13599 }
13600
13601 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13602 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13603
13604 /* If we have any template arguments, then we must allocate a
13605 different sort of symbol. */
13606 for (child_die = die->child; child_die; child_die = child_die->sibling)
13607 {
13608 if (child_die->tag == DW_TAG_template_type_param
13609 || child_die->tag == DW_TAG_template_value_param)
13610 {
13611 templ_func = new (&objfile->objfile_obstack) template_symbol;
13612 templ_func->subclass = SYMBOL_TEMPLATE;
13613 break;
13614 }
13615 }
13616
13617 newobj = cu->get_builder ()->push_context (0, lowpc);
13618 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13619 (struct symbol *) templ_func);
13620
13621 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13622 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13623 cu->language);
13624
13625 /* If there is a location expression for DW_AT_frame_base, record
13626 it. */
13627 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13628 if (attr != nullptr)
13629 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13630
13631 /* If there is a location for the static link, record it. */
13632 newobj->static_link = NULL;
13633 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13634 if (attr != nullptr)
13635 {
13636 newobj->static_link
13637 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13638 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13639 cu->addr_type ());
13640 }
13641
13642 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13643
13644 if (die->child != NULL)
13645 {
13646 child_die = die->child;
13647 while (child_die && child_die->tag)
13648 {
13649 if (child_die->tag == DW_TAG_template_type_param
13650 || child_die->tag == DW_TAG_template_value_param)
13651 {
13652 struct symbol *arg = new_symbol (child_die, NULL, cu);
13653
13654 if (arg != NULL)
13655 template_args.push_back (arg);
13656 }
13657 else
13658 process_die (child_die, cu);
13659 child_die = child_die->sibling;
13660 }
13661 }
13662
13663 inherit_abstract_dies (die, cu);
13664
13665 /* If we have a DW_AT_specification, we might need to import using
13666 directives from the context of the specification DIE. See the
13667 comment in determine_prefix. */
13668 if (cu->language == language_cplus
13669 && dwarf2_attr (die, DW_AT_specification, cu))
13670 {
13671 struct dwarf2_cu *spec_cu = cu;
13672 struct die_info *spec_die = die_specification (die, &spec_cu);
13673
13674 while (spec_die)
13675 {
13676 child_die = spec_die->child;
13677 while (child_die && child_die->tag)
13678 {
13679 if (child_die->tag == DW_TAG_imported_module)
13680 process_die (child_die, spec_cu);
13681 child_die = child_die->sibling;
13682 }
13683
13684 /* In some cases, GCC generates specification DIEs that
13685 themselves contain DW_AT_specification attributes. */
13686 spec_die = die_specification (spec_die, &spec_cu);
13687 }
13688 }
13689
13690 struct context_stack cstk = cu->get_builder ()->pop_context ();
13691 /* Make a block for the local symbols within. */
13692 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13693 cstk.static_link, lowpc, highpc);
13694
13695 /* For C++, set the block's scope. */
13696 if ((cu->language == language_cplus
13697 || cu->language == language_fortran
13698 || cu->language == language_d
13699 || cu->language == language_rust)
13700 && cu->processing_has_namespace_info)
13701 block_set_scope (block, determine_prefix (die, cu),
13702 &objfile->objfile_obstack);
13703
13704 /* If we have address ranges, record them. */
13705 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13706
13707 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13708
13709 /* Attach template arguments to function. */
13710 if (!template_args.empty ())
13711 {
13712 gdb_assert (templ_func != NULL);
13713
13714 templ_func->n_template_arguments = template_args.size ();
13715 templ_func->template_arguments
13716 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13717 templ_func->n_template_arguments);
13718 memcpy (templ_func->template_arguments,
13719 template_args.data (),
13720 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13721
13722 /* Make sure that the symtab is set on the new symbols. Even
13723 though they don't appear in this symtab directly, other parts
13724 of gdb assume that symbols do, and this is reasonably
13725 true. */
13726 for (symbol *sym : template_args)
13727 symbol_set_symtab (sym, symbol_symtab (templ_func));
13728 }
13729
13730 /* In C++, we can have functions nested inside functions (e.g., when
13731 a function declares a class that has methods). This means that
13732 when we finish processing a function scope, we may need to go
13733 back to building a containing block's symbol lists. */
13734 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13735 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13736
13737 /* If we've finished processing a top-level function, subsequent
13738 symbols go in the file symbol list. */
13739 if (cu->get_builder ()->outermost_context_p ())
13740 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13741 }
13742
13743 /* Process all the DIES contained within a lexical block scope. Start
13744 a new scope, process the dies, and then close the scope. */
13745
13746 static void
13747 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13748 {
13749 struct objfile *objfile = cu->per_objfile->objfile;
13750 struct gdbarch *gdbarch = objfile->arch ();
13751 CORE_ADDR lowpc, highpc;
13752 struct die_info *child_die;
13753 CORE_ADDR baseaddr;
13754
13755 baseaddr = objfile->text_section_offset ();
13756
13757 /* Ignore blocks with missing or invalid low and high pc attributes. */
13758 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13759 as multiple lexical blocks? Handling children in a sane way would
13760 be nasty. Might be easier to properly extend generic blocks to
13761 describe ranges. */
13762 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13763 {
13764 case PC_BOUNDS_NOT_PRESENT:
13765 /* DW_TAG_lexical_block has no attributes, process its children as if
13766 there was no wrapping by that DW_TAG_lexical_block.
13767 GCC does no longer produces such DWARF since GCC r224161. */
13768 for (child_die = die->child;
13769 child_die != NULL && child_die->tag;
13770 child_die = child_die->sibling)
13771 {
13772 /* We might already be processing this DIE. This can happen
13773 in an unusual circumstance -- where a subroutine A
13774 appears lexically in another subroutine B, but A actually
13775 inlines B. The recursion is broken here, rather than in
13776 inherit_abstract_dies, because it seems better to simply
13777 drop concrete children here. */
13778 if (!child_die->in_process)
13779 process_die (child_die, cu);
13780 }
13781 return;
13782 case PC_BOUNDS_INVALID:
13783 return;
13784 }
13785 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13786 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13787
13788 cu->get_builder ()->push_context (0, lowpc);
13789 if (die->child != NULL)
13790 {
13791 child_die = die->child;
13792 while (child_die && child_die->tag)
13793 {
13794 process_die (child_die, cu);
13795 child_die = child_die->sibling;
13796 }
13797 }
13798 inherit_abstract_dies (die, cu);
13799 struct context_stack cstk = cu->get_builder ()->pop_context ();
13800
13801 if (*cu->get_builder ()->get_local_symbols () != NULL
13802 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13803 {
13804 struct block *block
13805 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13806 cstk.start_addr, highpc);
13807
13808 /* Note that recording ranges after traversing children, as we
13809 do here, means that recording a parent's ranges entails
13810 walking across all its children's ranges as they appear in
13811 the address map, which is quadratic behavior.
13812
13813 It would be nicer to record the parent's ranges before
13814 traversing its children, simply overriding whatever you find
13815 there. But since we don't even decide whether to create a
13816 block until after we've traversed its children, that's hard
13817 to do. */
13818 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13819 }
13820 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13821 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13822 }
13823
13824 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13825
13826 static void
13827 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13828 {
13829 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13830 struct objfile *objfile = per_objfile->objfile;
13831 struct gdbarch *gdbarch = objfile->arch ();
13832 CORE_ADDR pc, baseaddr;
13833 struct attribute *attr;
13834 struct call_site *call_site, call_site_local;
13835 void **slot;
13836 int nparams;
13837 struct die_info *child_die;
13838
13839 baseaddr = objfile->text_section_offset ();
13840
13841 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13842 if (attr == NULL)
13843 {
13844 /* This was a pre-DWARF-5 GNU extension alias
13845 for DW_AT_call_return_pc. */
13846 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13847 }
13848 if (!attr)
13849 {
13850 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13851 "DIE %s [in module %s]"),
13852 sect_offset_str (die->sect_off), objfile_name (objfile));
13853 return;
13854 }
13855 pc = attr->as_address () + baseaddr;
13856 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13857
13858 if (cu->call_site_htab == NULL)
13859 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13860 NULL, &objfile->objfile_obstack,
13861 hashtab_obstack_allocate, NULL);
13862 call_site_local.pc = pc;
13863 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13864 if (*slot != NULL)
13865 {
13866 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13867 "DIE %s [in module %s]"),
13868 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13869 objfile_name (objfile));
13870 return;
13871 }
13872
13873 /* Count parameters at the caller. */
13874
13875 nparams = 0;
13876 for (child_die = die->child; child_die && child_die->tag;
13877 child_die = child_die->sibling)
13878 {
13879 if (child_die->tag != DW_TAG_call_site_parameter
13880 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13881 {
13882 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13883 "DW_TAG_call_site child DIE %s [in module %s]"),
13884 child_die->tag, sect_offset_str (child_die->sect_off),
13885 objfile_name (objfile));
13886 continue;
13887 }
13888
13889 nparams++;
13890 }
13891
13892 call_site
13893 = ((struct call_site *)
13894 obstack_alloc (&objfile->objfile_obstack,
13895 sizeof (*call_site)
13896 + (sizeof (*call_site->parameter) * (nparams - 1))));
13897 *slot = call_site;
13898 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13899 call_site->pc = pc;
13900
13901 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13902 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13903 {
13904 struct die_info *func_die;
13905
13906 /* Skip also over DW_TAG_inlined_subroutine. */
13907 for (func_die = die->parent;
13908 func_die && func_die->tag != DW_TAG_subprogram
13909 && func_die->tag != DW_TAG_subroutine_type;
13910 func_die = func_die->parent);
13911
13912 /* DW_AT_call_all_calls is a superset
13913 of DW_AT_call_all_tail_calls. */
13914 if (func_die
13915 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13916 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13917 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13918 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13919 {
13920 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13921 not complete. But keep CALL_SITE for look ups via call_site_htab,
13922 both the initial caller containing the real return address PC and
13923 the final callee containing the current PC of a chain of tail
13924 calls do not need to have the tail call list complete. But any
13925 function candidate for a virtual tail call frame searched via
13926 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13927 determined unambiguously. */
13928 }
13929 else
13930 {
13931 struct type *func_type = NULL;
13932
13933 if (func_die)
13934 func_type = get_die_type (func_die, cu);
13935 if (func_type != NULL)
13936 {
13937 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13938
13939 /* Enlist this call site to the function. */
13940 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13941 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13942 }
13943 else
13944 complaint (_("Cannot find function owning DW_TAG_call_site "
13945 "DIE %s [in module %s]"),
13946 sect_offset_str (die->sect_off), objfile_name (objfile));
13947 }
13948 }
13949
13950 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13951 if (attr == NULL)
13952 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13953 if (attr == NULL)
13954 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13955 if (attr == NULL)
13956 {
13957 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13958 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13959 }
13960 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13961 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13962 /* Keep NULL DWARF_BLOCK. */;
13963 else if (attr->form_is_block ())
13964 {
13965 struct dwarf2_locexpr_baton *dlbaton;
13966 struct dwarf_block *block = attr->as_block ();
13967
13968 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13969 dlbaton->data = block->data;
13970 dlbaton->size = block->size;
13971 dlbaton->per_objfile = per_objfile;
13972 dlbaton->per_cu = cu->per_cu;
13973
13974 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13975 }
13976 else if (attr->form_is_ref ())
13977 {
13978 struct dwarf2_cu *target_cu = cu;
13979 struct die_info *target_die;
13980
13981 target_die = follow_die_ref (die, attr, &target_cu);
13982 gdb_assert (target_cu->per_objfile->objfile == objfile);
13983 if (die_is_declaration (target_die, target_cu))
13984 {
13985 const char *target_physname;
13986
13987 /* Prefer the mangled name; otherwise compute the demangled one. */
13988 target_physname = dw2_linkage_name (target_die, target_cu);
13989 if (target_physname == NULL)
13990 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13991 if (target_physname == NULL)
13992 complaint (_("DW_AT_call_target target DIE has invalid "
13993 "physname, for referencing DIE %s [in module %s]"),
13994 sect_offset_str (die->sect_off), objfile_name (objfile));
13995 else
13996 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13997 }
13998 else
13999 {
14000 CORE_ADDR lowpc;
14001
14002 /* DW_AT_entry_pc should be preferred. */
14003 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14004 <= PC_BOUNDS_INVALID)
14005 complaint (_("DW_AT_call_target target DIE has invalid "
14006 "low pc, for referencing DIE %s [in module %s]"),
14007 sect_offset_str (die->sect_off), objfile_name (objfile));
14008 else
14009 {
14010 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14011 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14012 }
14013 }
14014 }
14015 else
14016 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14017 "block nor reference, for DIE %s [in module %s]"),
14018 sect_offset_str (die->sect_off), objfile_name (objfile));
14019
14020 call_site->per_cu = cu->per_cu;
14021 call_site->per_objfile = per_objfile;
14022
14023 for (child_die = die->child;
14024 child_die && child_die->tag;
14025 child_die = child_die->sibling)
14026 {
14027 struct call_site_parameter *parameter;
14028 struct attribute *loc, *origin;
14029
14030 if (child_die->tag != DW_TAG_call_site_parameter
14031 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14032 {
14033 /* Already printed the complaint above. */
14034 continue;
14035 }
14036
14037 gdb_assert (call_site->parameter_count < nparams);
14038 parameter = &call_site->parameter[call_site->parameter_count];
14039
14040 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14041 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14042 register is contained in DW_AT_call_value. */
14043
14044 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14045 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14046 if (origin == NULL)
14047 {
14048 /* This was a pre-DWARF-5 GNU extension alias
14049 for DW_AT_call_parameter. */
14050 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14051 }
14052 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14053 {
14054 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14055
14056 sect_offset sect_off = origin->get_ref_die_offset ();
14057 if (!cu->header.offset_in_cu_p (sect_off))
14058 {
14059 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14060 binding can be done only inside one CU. Such referenced DIE
14061 therefore cannot be even moved to DW_TAG_partial_unit. */
14062 complaint (_("DW_AT_call_parameter offset is not in CU for "
14063 "DW_TAG_call_site child DIE %s [in module %s]"),
14064 sect_offset_str (child_die->sect_off),
14065 objfile_name (objfile));
14066 continue;
14067 }
14068 parameter->u.param_cu_off
14069 = (cu_offset) (sect_off - cu->header.sect_off);
14070 }
14071 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14072 {
14073 complaint (_("No DW_FORM_block* DW_AT_location for "
14074 "DW_TAG_call_site child DIE %s [in module %s]"),
14075 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14076 continue;
14077 }
14078 else
14079 {
14080 struct dwarf_block *block = loc->as_block ();
14081
14082 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14083 (block->data, &block->data[block->size]);
14084 if (parameter->u.dwarf_reg != -1)
14085 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14086 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14087 &block->data[block->size],
14088 &parameter->u.fb_offset))
14089 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14090 else
14091 {
14092 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14093 "for DW_FORM_block* DW_AT_location is supported for "
14094 "DW_TAG_call_site child DIE %s "
14095 "[in module %s]"),
14096 sect_offset_str (child_die->sect_off),
14097 objfile_name (objfile));
14098 continue;
14099 }
14100 }
14101
14102 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14103 if (attr == NULL)
14104 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14105 if (attr == NULL || !attr->form_is_block ())
14106 {
14107 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14108 "DW_TAG_call_site child DIE %s [in module %s]"),
14109 sect_offset_str (child_die->sect_off),
14110 objfile_name (objfile));
14111 continue;
14112 }
14113
14114 struct dwarf_block *block = attr->as_block ();
14115 parameter->value = block->data;
14116 parameter->value_size = block->size;
14117
14118 /* Parameters are not pre-cleared by memset above. */
14119 parameter->data_value = NULL;
14120 parameter->data_value_size = 0;
14121 call_site->parameter_count++;
14122
14123 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14124 if (attr == NULL)
14125 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14126 if (attr != nullptr)
14127 {
14128 if (!attr->form_is_block ())
14129 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14130 "DW_TAG_call_site child DIE %s [in module %s]"),
14131 sect_offset_str (child_die->sect_off),
14132 objfile_name (objfile));
14133 else
14134 {
14135 block = attr->as_block ();
14136 parameter->data_value = block->data;
14137 parameter->data_value_size = block->size;
14138 }
14139 }
14140 }
14141 }
14142
14143 /* Helper function for read_variable. If DIE represents a virtual
14144 table, then return the type of the concrete object that is
14145 associated with the virtual table. Otherwise, return NULL. */
14146
14147 static struct type *
14148 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14149 {
14150 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14151 if (attr == NULL)
14152 return NULL;
14153
14154 /* Find the type DIE. */
14155 struct die_info *type_die = NULL;
14156 struct dwarf2_cu *type_cu = cu;
14157
14158 if (attr->form_is_ref ())
14159 type_die = follow_die_ref (die, attr, &type_cu);
14160 if (type_die == NULL)
14161 return NULL;
14162
14163 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14164 return NULL;
14165 return die_containing_type (type_die, type_cu);
14166 }
14167
14168 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14169
14170 static void
14171 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14172 {
14173 struct rust_vtable_symbol *storage = NULL;
14174
14175 if (cu->language == language_rust)
14176 {
14177 struct type *containing_type = rust_containing_type (die, cu);
14178
14179 if (containing_type != NULL)
14180 {
14181 struct objfile *objfile = cu->per_objfile->objfile;
14182
14183 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14184 storage->concrete_type = containing_type;
14185 storage->subclass = SYMBOL_RUST_VTABLE;
14186 }
14187 }
14188
14189 struct symbol *res = new_symbol (die, NULL, cu, storage);
14190 struct attribute *abstract_origin
14191 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14192 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14193 if (res == NULL && loc && abstract_origin)
14194 {
14195 /* We have a variable without a name, but with a location and an abstract
14196 origin. This may be a concrete instance of an abstract variable
14197 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14198 later. */
14199 struct dwarf2_cu *origin_cu = cu;
14200 struct die_info *origin_die
14201 = follow_die_ref (die, abstract_origin, &origin_cu);
14202 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14203 per_objfile->per_bfd->abstract_to_concrete
14204 [origin_die->sect_off].push_back (die->sect_off);
14205 }
14206 }
14207
14208 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14209 reading .debug_rnglists.
14210 Callback's type should be:
14211 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14212 Return true if the attributes are present and valid, otherwise,
14213 return false. */
14214
14215 template <typename Callback>
14216 static bool
14217 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14218 dwarf_tag tag, Callback &&callback)
14219 {
14220 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14221 struct objfile *objfile = per_objfile->objfile;
14222 bfd *obfd = objfile->obfd;
14223 /* Base address selection entry. */
14224 gdb::optional<CORE_ADDR> base;
14225 const gdb_byte *buffer;
14226 CORE_ADDR baseaddr;
14227 bool overflow = false;
14228 ULONGEST addr_index;
14229 struct dwarf2_section_info *rnglists_section;
14230
14231 base = cu->base_address;
14232 rnglists_section = cu_debug_rnglists_section (cu, tag);
14233 rnglists_section->read (objfile);
14234
14235 if (offset >= rnglists_section->size)
14236 {
14237 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14238 offset);
14239 return false;
14240 }
14241 buffer = rnglists_section->buffer + offset;
14242
14243 baseaddr = objfile->text_section_offset ();
14244
14245 while (1)
14246 {
14247 /* Initialize it due to a false compiler warning. */
14248 CORE_ADDR range_beginning = 0, range_end = 0;
14249 const gdb_byte *buf_end = (rnglists_section->buffer
14250 + rnglists_section->size);
14251 unsigned int bytes_read;
14252
14253 if (buffer == buf_end)
14254 {
14255 overflow = true;
14256 break;
14257 }
14258 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14259 switch (rlet)
14260 {
14261 case DW_RLE_end_of_list:
14262 break;
14263 case DW_RLE_base_address:
14264 if (buffer + cu->header.addr_size > buf_end)
14265 {
14266 overflow = true;
14267 break;
14268 }
14269 base = cu->header.read_address (obfd, buffer, &bytes_read);
14270 buffer += bytes_read;
14271 break;
14272 case DW_RLE_base_addressx:
14273 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14274 buffer += bytes_read;
14275 base = read_addr_index (cu, addr_index);
14276 break;
14277 case DW_RLE_start_length:
14278 if (buffer + cu->header.addr_size > buf_end)
14279 {
14280 overflow = true;
14281 break;
14282 }
14283 range_beginning = cu->header.read_address (obfd, buffer,
14284 &bytes_read);
14285 buffer += bytes_read;
14286 range_end = (range_beginning
14287 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14288 buffer += bytes_read;
14289 if (buffer > buf_end)
14290 {
14291 overflow = true;
14292 break;
14293 }
14294 break;
14295 case DW_RLE_startx_length:
14296 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14297 buffer += bytes_read;
14298 range_beginning = read_addr_index (cu, addr_index);
14299 if (buffer > buf_end)
14300 {
14301 overflow = true;
14302 break;
14303 }
14304 range_end = (range_beginning
14305 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14306 buffer += bytes_read;
14307 break;
14308 case DW_RLE_offset_pair:
14309 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14310 buffer += bytes_read;
14311 if (buffer > buf_end)
14312 {
14313 overflow = true;
14314 break;
14315 }
14316 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14317 buffer += bytes_read;
14318 if (buffer > buf_end)
14319 {
14320 overflow = true;
14321 break;
14322 }
14323 break;
14324 case DW_RLE_start_end:
14325 if (buffer + 2 * cu->header.addr_size > buf_end)
14326 {
14327 overflow = true;
14328 break;
14329 }
14330 range_beginning = cu->header.read_address (obfd, buffer,
14331 &bytes_read);
14332 buffer += bytes_read;
14333 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14334 buffer += bytes_read;
14335 break;
14336 case DW_RLE_startx_endx:
14337 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14338 buffer += bytes_read;
14339 range_beginning = read_addr_index (cu, addr_index);
14340 if (buffer > buf_end)
14341 {
14342 overflow = true;
14343 break;
14344 }
14345 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14346 buffer += bytes_read;
14347 range_end = read_addr_index (cu, addr_index);
14348 break;
14349 default:
14350 complaint (_("Invalid .debug_rnglists data (no base address)"));
14351 return false;
14352 }
14353 if (rlet == DW_RLE_end_of_list || overflow)
14354 break;
14355 if (rlet == DW_RLE_base_address)
14356 continue;
14357
14358 if (range_beginning > range_end)
14359 {
14360 /* Inverted range entries are invalid. */
14361 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14362 return false;
14363 }
14364
14365 /* Empty range entries have no effect. */
14366 if (range_beginning == range_end)
14367 continue;
14368
14369 /* Only DW_RLE_offset_pair needs the base address added. */
14370 if (rlet == DW_RLE_offset_pair)
14371 {
14372 if (!base.has_value ())
14373 {
14374 /* We have no valid base address for the DW_RLE_offset_pair. */
14375 complaint (_("Invalid .debug_rnglists data (no base address for "
14376 "DW_RLE_offset_pair)"));
14377 return false;
14378 }
14379
14380 range_beginning += *base;
14381 range_end += *base;
14382 }
14383
14384 /* A not-uncommon case of bad debug info.
14385 Don't pollute the addrmap with bad data. */
14386 if (range_beginning + baseaddr == 0
14387 && !per_objfile->per_bfd->has_section_at_zero)
14388 {
14389 complaint (_(".debug_rnglists entry has start address of zero"
14390 " [in module %s]"), objfile_name (objfile));
14391 continue;
14392 }
14393
14394 callback (range_beginning, range_end);
14395 }
14396
14397 if (overflow)
14398 {
14399 complaint (_("Offset %d is not terminated "
14400 "for DW_AT_ranges attribute"),
14401 offset);
14402 return false;
14403 }
14404
14405 return true;
14406 }
14407
14408 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14409 Callback's type should be:
14410 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14411 Return 1 if the attributes are present and valid, otherwise, return 0. */
14412
14413 template <typename Callback>
14414 static int
14415 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14416 Callback &&callback)
14417 {
14418 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14419 struct objfile *objfile = per_objfile->objfile;
14420 struct comp_unit_head *cu_header = &cu->header;
14421 bfd *obfd = objfile->obfd;
14422 unsigned int addr_size = cu_header->addr_size;
14423 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14424 /* Base address selection entry. */
14425 gdb::optional<CORE_ADDR> base;
14426 unsigned int dummy;
14427 const gdb_byte *buffer;
14428 CORE_ADDR baseaddr;
14429
14430 if (cu_header->version >= 5)
14431 return dwarf2_rnglists_process (offset, cu, tag, callback);
14432
14433 base = cu->base_address;
14434
14435 per_objfile->per_bfd->ranges.read (objfile);
14436 if (offset >= per_objfile->per_bfd->ranges.size)
14437 {
14438 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14439 offset);
14440 return 0;
14441 }
14442 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14443
14444 baseaddr = objfile->text_section_offset ();
14445
14446 while (1)
14447 {
14448 CORE_ADDR range_beginning, range_end;
14449
14450 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14451 buffer += addr_size;
14452 range_end = cu->header.read_address (obfd, buffer, &dummy);
14453 buffer += addr_size;
14454 offset += 2 * addr_size;
14455
14456 /* An end of list marker is a pair of zero addresses. */
14457 if (range_beginning == 0 && range_end == 0)
14458 /* Found the end of list entry. */
14459 break;
14460
14461 /* Each base address selection entry is a pair of 2 values.
14462 The first is the largest possible address, the second is
14463 the base address. Check for a base address here. */
14464 if ((range_beginning & mask) == mask)
14465 {
14466 /* If we found the largest possible address, then we already
14467 have the base address in range_end. */
14468 base = range_end;
14469 continue;
14470 }
14471
14472 if (!base.has_value ())
14473 {
14474 /* We have no valid base address for the ranges
14475 data. */
14476 complaint (_("Invalid .debug_ranges data (no base address)"));
14477 return 0;
14478 }
14479
14480 if (range_beginning > range_end)
14481 {
14482 /* Inverted range entries are invalid. */
14483 complaint (_("Invalid .debug_ranges data (inverted range)"));
14484 return 0;
14485 }
14486
14487 /* Empty range entries have no effect. */
14488 if (range_beginning == range_end)
14489 continue;
14490
14491 range_beginning += *base;
14492 range_end += *base;
14493
14494 /* A not-uncommon case of bad debug info.
14495 Don't pollute the addrmap with bad data. */
14496 if (range_beginning + baseaddr == 0
14497 && !per_objfile->per_bfd->has_section_at_zero)
14498 {
14499 complaint (_(".debug_ranges entry has start address of zero"
14500 " [in module %s]"), objfile_name (objfile));
14501 continue;
14502 }
14503
14504 callback (range_beginning, range_end);
14505 }
14506
14507 return 1;
14508 }
14509
14510 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14511 Return 1 if the attributes are present and valid, otherwise, return 0.
14512 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14513
14514 static int
14515 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14516 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14517 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14518 {
14519 struct objfile *objfile = cu->per_objfile->objfile;
14520 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14521 struct gdbarch *gdbarch = objfile->arch ();
14522 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14523 int low_set = 0;
14524 CORE_ADDR low = 0;
14525 CORE_ADDR high = 0;
14526 int retval;
14527
14528 retval = dwarf2_ranges_process (offset, cu, tag,
14529 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14530 {
14531 if (ranges_pst != NULL)
14532 {
14533 CORE_ADDR lowpc;
14534 CORE_ADDR highpc;
14535
14536 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14537 range_beginning + baseaddr)
14538 - baseaddr);
14539 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14540 range_end + baseaddr)
14541 - baseaddr);
14542 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14543 lowpc, highpc - 1, ranges_pst);
14544 }
14545
14546 /* FIXME: This is recording everything as a low-high
14547 segment of consecutive addresses. We should have a
14548 data structure for discontiguous block ranges
14549 instead. */
14550 if (! low_set)
14551 {
14552 low = range_beginning;
14553 high = range_end;
14554 low_set = 1;
14555 }
14556 else
14557 {
14558 if (range_beginning < low)
14559 low = range_beginning;
14560 if (range_end > high)
14561 high = range_end;
14562 }
14563 });
14564 if (!retval)
14565 return 0;
14566
14567 if (! low_set)
14568 /* If the first entry is an end-of-list marker, the range
14569 describes an empty scope, i.e. no instructions. */
14570 return 0;
14571
14572 if (low_return)
14573 *low_return = low;
14574 if (high_return)
14575 *high_return = high;
14576 return 1;
14577 }
14578
14579 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14580 definition for the return value. *LOWPC and *HIGHPC are set iff
14581 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14582
14583 static enum pc_bounds_kind
14584 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14585 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14586 dwarf2_psymtab *pst)
14587 {
14588 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14589 struct attribute *attr;
14590 struct attribute *attr_high;
14591 CORE_ADDR low = 0;
14592 CORE_ADDR high = 0;
14593 enum pc_bounds_kind ret;
14594
14595 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14596 if (attr_high)
14597 {
14598 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14599 if (attr != nullptr)
14600 {
14601 low = attr->as_address ();
14602 high = attr_high->as_address ();
14603 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14604 high += low;
14605 }
14606 else
14607 /* Found high w/o low attribute. */
14608 return PC_BOUNDS_INVALID;
14609
14610 /* Found consecutive range of addresses. */
14611 ret = PC_BOUNDS_HIGH_LOW;
14612 }
14613 else
14614 {
14615 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14616 if (attr != nullptr && attr->form_is_unsigned ())
14617 {
14618 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14619 on DWARF version). */
14620 ULONGEST ranges_offset = attr->as_unsigned ();
14621
14622 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14623 this value. */
14624 if (die->tag != DW_TAG_compile_unit)
14625 ranges_offset += cu->gnu_ranges_base;
14626
14627 /* Value of the DW_AT_ranges attribute is the offset in the
14628 .debug_ranges section. */
14629 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14630 die->tag))
14631 return PC_BOUNDS_INVALID;
14632 /* Found discontinuous range of addresses. */
14633 ret = PC_BOUNDS_RANGES;
14634 }
14635 else
14636 return PC_BOUNDS_NOT_PRESENT;
14637 }
14638
14639 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14640 if (high <= low)
14641 return PC_BOUNDS_INVALID;
14642
14643 /* When using the GNU linker, .gnu.linkonce. sections are used to
14644 eliminate duplicate copies of functions and vtables and such.
14645 The linker will arbitrarily choose one and discard the others.
14646 The AT_*_pc values for such functions refer to local labels in
14647 these sections. If the section from that file was discarded, the
14648 labels are not in the output, so the relocs get a value of 0.
14649 If this is a discarded function, mark the pc bounds as invalid,
14650 so that GDB will ignore it. */
14651 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14652 return PC_BOUNDS_INVALID;
14653
14654 *lowpc = low;
14655 if (highpc)
14656 *highpc = high;
14657 return ret;
14658 }
14659
14660 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14661 its low and high PC addresses. Do nothing if these addresses could not
14662 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14663 and HIGHPC to the high address if greater than HIGHPC. */
14664
14665 static void
14666 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14667 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14668 struct dwarf2_cu *cu)
14669 {
14670 CORE_ADDR low, high;
14671 struct die_info *child = die->child;
14672
14673 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14674 {
14675 *lowpc = std::min (*lowpc, low);
14676 *highpc = std::max (*highpc, high);
14677 }
14678
14679 /* If the language does not allow nested subprograms (either inside
14680 subprograms or lexical blocks), we're done. */
14681 if (cu->language != language_ada)
14682 return;
14683
14684 /* Check all the children of the given DIE. If it contains nested
14685 subprograms, then check their pc bounds. Likewise, we need to
14686 check lexical blocks as well, as they may also contain subprogram
14687 definitions. */
14688 while (child && child->tag)
14689 {
14690 if (child->tag == DW_TAG_subprogram
14691 || child->tag == DW_TAG_lexical_block)
14692 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14693 child = child->sibling;
14694 }
14695 }
14696
14697 /* Get the low and high pc's represented by the scope DIE, and store
14698 them in *LOWPC and *HIGHPC. If the correct values can't be
14699 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14700
14701 static void
14702 get_scope_pc_bounds (struct die_info *die,
14703 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14704 struct dwarf2_cu *cu)
14705 {
14706 CORE_ADDR best_low = (CORE_ADDR) -1;
14707 CORE_ADDR best_high = (CORE_ADDR) 0;
14708 CORE_ADDR current_low, current_high;
14709
14710 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14711 >= PC_BOUNDS_RANGES)
14712 {
14713 best_low = current_low;
14714 best_high = current_high;
14715 }
14716 else
14717 {
14718 struct die_info *child = die->child;
14719
14720 while (child && child->tag)
14721 {
14722 switch (child->tag) {
14723 case DW_TAG_subprogram:
14724 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14725 break;
14726 case DW_TAG_namespace:
14727 case DW_TAG_module:
14728 /* FIXME: carlton/2004-01-16: Should we do this for
14729 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14730 that current GCC's always emit the DIEs corresponding
14731 to definitions of methods of classes as children of a
14732 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14733 the DIEs giving the declarations, which could be
14734 anywhere). But I don't see any reason why the
14735 standards says that they have to be there. */
14736 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14737
14738 if (current_low != ((CORE_ADDR) -1))
14739 {
14740 best_low = std::min (best_low, current_low);
14741 best_high = std::max (best_high, current_high);
14742 }
14743 break;
14744 default:
14745 /* Ignore. */
14746 break;
14747 }
14748
14749 child = child->sibling;
14750 }
14751 }
14752
14753 *lowpc = best_low;
14754 *highpc = best_high;
14755 }
14756
14757 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14758 in DIE. */
14759
14760 static void
14761 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14762 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14763 {
14764 struct objfile *objfile = cu->per_objfile->objfile;
14765 struct gdbarch *gdbarch = objfile->arch ();
14766 struct attribute *attr;
14767 struct attribute *attr_high;
14768
14769 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14770 if (attr_high)
14771 {
14772 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14773 if (attr != nullptr)
14774 {
14775 CORE_ADDR low = attr->as_address ();
14776 CORE_ADDR high = attr_high->as_address ();
14777
14778 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14779 high += low;
14780
14781 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14782 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14783 cu->get_builder ()->record_block_range (block, low, high - 1);
14784 }
14785 }
14786
14787 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14788 if (attr != nullptr && attr->form_is_unsigned ())
14789 {
14790 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14791 on DWARF version). */
14792 ULONGEST ranges_offset = attr->as_unsigned ();
14793
14794 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14795 this value. */
14796 if (die->tag != DW_TAG_compile_unit)
14797 ranges_offset += cu->gnu_ranges_base;
14798
14799 std::vector<blockrange> blockvec;
14800 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14801 [&] (CORE_ADDR start, CORE_ADDR end)
14802 {
14803 start += baseaddr;
14804 end += baseaddr;
14805 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14806 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14807 cu->get_builder ()->record_block_range (block, start, end - 1);
14808 blockvec.emplace_back (start, end);
14809 });
14810
14811 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14812 }
14813 }
14814
14815 /* Check whether the producer field indicates either of GCC < 4.6, or the
14816 Intel C/C++ compiler, and cache the result in CU. */
14817
14818 static void
14819 check_producer (struct dwarf2_cu *cu)
14820 {
14821 int major, minor;
14822
14823 if (cu->producer == NULL)
14824 {
14825 /* For unknown compilers expect their behavior is DWARF version
14826 compliant.
14827
14828 GCC started to support .debug_types sections by -gdwarf-4 since
14829 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14830 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14831 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14832 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14833 }
14834 else if (producer_is_gcc (cu->producer, &major, &minor))
14835 {
14836 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14837 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14838 }
14839 else if (producer_is_icc (cu->producer, &major, &minor))
14840 {
14841 cu->producer_is_icc = true;
14842 cu->producer_is_icc_lt_14 = major < 14;
14843 }
14844 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14845 cu->producer_is_codewarrior = true;
14846 else
14847 {
14848 /* For other non-GCC compilers, expect their behavior is DWARF version
14849 compliant. */
14850 }
14851
14852 cu->checked_producer = true;
14853 }
14854
14855 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14856 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14857 during 4.6.0 experimental. */
14858
14859 static bool
14860 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14861 {
14862 if (!cu->checked_producer)
14863 check_producer (cu);
14864
14865 return cu->producer_is_gxx_lt_4_6;
14866 }
14867
14868
14869 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14870 with incorrect is_stmt attributes. */
14871
14872 static bool
14873 producer_is_codewarrior (struct dwarf2_cu *cu)
14874 {
14875 if (!cu->checked_producer)
14876 check_producer (cu);
14877
14878 return cu->producer_is_codewarrior;
14879 }
14880
14881 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14882 If that attribute is not available, return the appropriate
14883 default. */
14884
14885 static enum dwarf_access_attribute
14886 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14887 {
14888 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14889 if (attr != nullptr)
14890 {
14891 LONGEST value = attr->constant_value (-1);
14892 if (value == DW_ACCESS_public
14893 || value == DW_ACCESS_protected
14894 || value == DW_ACCESS_private)
14895 return (dwarf_access_attribute) value;
14896 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14897 plongest (value));
14898 }
14899
14900 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14901 {
14902 /* The default DWARF 2 accessibility for members is public, the default
14903 accessibility for inheritance is private. */
14904
14905 if (die->tag != DW_TAG_inheritance)
14906 return DW_ACCESS_public;
14907 else
14908 return DW_ACCESS_private;
14909 }
14910 else
14911 {
14912 /* DWARF 3+ defines the default accessibility a different way. The same
14913 rules apply now for DW_TAG_inheritance as for the members and it only
14914 depends on the container kind. */
14915
14916 if (die->parent->tag == DW_TAG_class_type)
14917 return DW_ACCESS_private;
14918 else
14919 return DW_ACCESS_public;
14920 }
14921 }
14922
14923 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14924 offset. If the attribute was not found return 0, otherwise return
14925 1. If it was found but could not properly be handled, set *OFFSET
14926 to 0. */
14927
14928 static int
14929 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14930 LONGEST *offset)
14931 {
14932 struct attribute *attr;
14933
14934 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14935 if (attr != NULL)
14936 {
14937 *offset = 0;
14938
14939 /* Note that we do not check for a section offset first here.
14940 This is because DW_AT_data_member_location is new in DWARF 4,
14941 so if we see it, we can assume that a constant form is really
14942 a constant and not a section offset. */
14943 if (attr->form_is_constant ())
14944 *offset = attr->constant_value (0);
14945 else if (attr->form_is_section_offset ())
14946 dwarf2_complex_location_expr_complaint ();
14947 else if (attr->form_is_block ())
14948 *offset = decode_locdesc (attr->as_block (), cu);
14949 else
14950 dwarf2_complex_location_expr_complaint ();
14951
14952 return 1;
14953 }
14954
14955 return 0;
14956 }
14957
14958 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14959
14960 static void
14961 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14962 struct field *field)
14963 {
14964 struct attribute *attr;
14965
14966 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14967 if (attr != NULL)
14968 {
14969 if (attr->form_is_constant ())
14970 {
14971 LONGEST offset = attr->constant_value (0);
14972 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14973 }
14974 else if (attr->form_is_section_offset ())
14975 dwarf2_complex_location_expr_complaint ();
14976 else if (attr->form_is_block ())
14977 {
14978 bool handled;
14979 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14980 if (handled)
14981 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14982 else
14983 {
14984 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14985 struct objfile *objfile = per_objfile->objfile;
14986 struct dwarf2_locexpr_baton *dlbaton
14987 = XOBNEW (&objfile->objfile_obstack,
14988 struct dwarf2_locexpr_baton);
14989 dlbaton->data = attr->as_block ()->data;
14990 dlbaton->size = attr->as_block ()->size;
14991 /* When using this baton, we want to compute the address
14992 of the field, not the value. This is why
14993 is_reference is set to false here. */
14994 dlbaton->is_reference = false;
14995 dlbaton->per_objfile = per_objfile;
14996 dlbaton->per_cu = cu->per_cu;
14997
14998 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14999 }
15000 }
15001 else
15002 dwarf2_complex_location_expr_complaint ();
15003 }
15004 }
15005
15006 /* Add an aggregate field to the field list. */
15007
15008 static void
15009 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15010 struct dwarf2_cu *cu)
15011 {
15012 struct objfile *objfile = cu->per_objfile->objfile;
15013 struct gdbarch *gdbarch = objfile->arch ();
15014 struct nextfield *new_field;
15015 struct attribute *attr;
15016 struct field *fp;
15017 const char *fieldname = "";
15018
15019 if (die->tag == DW_TAG_inheritance)
15020 {
15021 fip->baseclasses.emplace_back ();
15022 new_field = &fip->baseclasses.back ();
15023 }
15024 else
15025 {
15026 fip->fields.emplace_back ();
15027 new_field = &fip->fields.back ();
15028 }
15029
15030 new_field->offset = die->sect_off;
15031
15032 new_field->accessibility = dwarf2_access_attribute (die, cu);
15033 if (new_field->accessibility != DW_ACCESS_public)
15034 fip->non_public_fields = true;
15035
15036 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15037 if (attr != nullptr)
15038 new_field->virtuality = attr->as_virtuality ();
15039 else
15040 new_field->virtuality = DW_VIRTUALITY_none;
15041
15042 fp = &new_field->field;
15043
15044 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15045 {
15046 /* Data member other than a C++ static data member. */
15047
15048 /* Get type of field. */
15049 fp->set_type (die_type (die, cu));
15050
15051 SET_FIELD_BITPOS (*fp, 0);
15052
15053 /* Get bit size of field (zero if none). */
15054 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15055 if (attr != nullptr)
15056 {
15057 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15058 }
15059 else
15060 {
15061 FIELD_BITSIZE (*fp) = 0;
15062 }
15063
15064 /* Get bit offset of field. */
15065 handle_data_member_location (die, cu, fp);
15066 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15067 if (attr != nullptr && attr->form_is_constant ())
15068 {
15069 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15070 {
15071 /* For big endian bits, the DW_AT_bit_offset gives the
15072 additional bit offset from the MSB of the containing
15073 anonymous object to the MSB of the field. We don't
15074 have to do anything special since we don't need to
15075 know the size of the anonymous object. */
15076 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15077 + attr->constant_value (0)));
15078 }
15079 else
15080 {
15081 /* For little endian bits, compute the bit offset to the
15082 MSB of the anonymous object, subtract off the number of
15083 bits from the MSB of the field to the MSB of the
15084 object, and then subtract off the number of bits of
15085 the field itself. The result is the bit offset of
15086 the LSB of the field. */
15087 int anonymous_size;
15088 int bit_offset = attr->constant_value (0);
15089
15090 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15091 if (attr != nullptr && attr->form_is_constant ())
15092 {
15093 /* The size of the anonymous object containing
15094 the bit field is explicit, so use the
15095 indicated size (in bytes). */
15096 anonymous_size = attr->constant_value (0);
15097 }
15098 else
15099 {
15100 /* The size of the anonymous object containing
15101 the bit field must be inferred from the type
15102 attribute of the data member containing the
15103 bit field. */
15104 anonymous_size = TYPE_LENGTH (fp->type ());
15105 }
15106 SET_FIELD_BITPOS (*fp,
15107 (FIELD_BITPOS (*fp)
15108 + anonymous_size * bits_per_byte
15109 - bit_offset - FIELD_BITSIZE (*fp)));
15110 }
15111 }
15112 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15113 if (attr != NULL)
15114 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15115 + attr->constant_value (0)));
15116
15117 /* Get name of field. */
15118 fieldname = dwarf2_name (die, cu);
15119 if (fieldname == NULL)
15120 fieldname = "";
15121
15122 /* The name is already allocated along with this objfile, so we don't
15123 need to duplicate it for the type. */
15124 fp->name = fieldname;
15125
15126 /* Change accessibility for artificial fields (e.g. virtual table
15127 pointer or virtual base class pointer) to private. */
15128 if (dwarf2_attr (die, DW_AT_artificial, cu))
15129 {
15130 FIELD_ARTIFICIAL (*fp) = 1;
15131 new_field->accessibility = DW_ACCESS_private;
15132 fip->non_public_fields = true;
15133 }
15134 }
15135 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15136 {
15137 /* C++ static member. */
15138
15139 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15140 is a declaration, but all versions of G++ as of this writing
15141 (so through at least 3.2.1) incorrectly generate
15142 DW_TAG_variable tags. */
15143
15144 const char *physname;
15145
15146 /* Get name of field. */
15147 fieldname = dwarf2_name (die, cu);
15148 if (fieldname == NULL)
15149 return;
15150
15151 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15152 if (attr
15153 /* Only create a symbol if this is an external value.
15154 new_symbol checks this and puts the value in the global symbol
15155 table, which we want. If it is not external, new_symbol
15156 will try to put the value in cu->list_in_scope which is wrong. */
15157 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15158 {
15159 /* A static const member, not much different than an enum as far as
15160 we're concerned, except that we can support more types. */
15161 new_symbol (die, NULL, cu);
15162 }
15163
15164 /* Get physical name. */
15165 physname = dwarf2_physname (fieldname, die, cu);
15166
15167 /* The name is already allocated along with this objfile, so we don't
15168 need to duplicate it for the type. */
15169 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15170 fp->set_type (die_type (die, cu));
15171 FIELD_NAME (*fp) = fieldname;
15172 }
15173 else if (die->tag == DW_TAG_inheritance)
15174 {
15175 /* C++ base class field. */
15176 handle_data_member_location (die, cu, fp);
15177 FIELD_BITSIZE (*fp) = 0;
15178 fp->set_type (die_type (die, cu));
15179 FIELD_NAME (*fp) = fp->type ()->name ();
15180 }
15181 else
15182 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15183 }
15184
15185 /* Can the type given by DIE define another type? */
15186
15187 static bool
15188 type_can_define_types (const struct die_info *die)
15189 {
15190 switch (die->tag)
15191 {
15192 case DW_TAG_typedef:
15193 case DW_TAG_class_type:
15194 case DW_TAG_structure_type:
15195 case DW_TAG_union_type:
15196 case DW_TAG_enumeration_type:
15197 return true;
15198
15199 default:
15200 return false;
15201 }
15202 }
15203
15204 /* Add a type definition defined in the scope of the FIP's class. */
15205
15206 static void
15207 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15208 struct dwarf2_cu *cu)
15209 {
15210 struct decl_field fp;
15211 memset (&fp, 0, sizeof (fp));
15212
15213 gdb_assert (type_can_define_types (die));
15214
15215 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15216 fp.name = dwarf2_name (die, cu);
15217 fp.type = read_type_die (die, cu);
15218
15219 /* Save accessibility. */
15220 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15221 switch (accessibility)
15222 {
15223 case DW_ACCESS_public:
15224 /* The assumed value if neither private nor protected. */
15225 break;
15226 case DW_ACCESS_private:
15227 fp.is_private = 1;
15228 break;
15229 case DW_ACCESS_protected:
15230 fp.is_protected = 1;
15231 break;
15232 }
15233
15234 if (die->tag == DW_TAG_typedef)
15235 fip->typedef_field_list.push_back (fp);
15236 else
15237 fip->nested_types_list.push_back (fp);
15238 }
15239
15240 /* A convenience typedef that's used when finding the discriminant
15241 field for a variant part. */
15242 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15243 offset_map_type;
15244
15245 /* Compute the discriminant range for a given variant. OBSTACK is
15246 where the results will be stored. VARIANT is the variant to
15247 process. IS_UNSIGNED indicates whether the discriminant is signed
15248 or unsigned. */
15249
15250 static const gdb::array_view<discriminant_range>
15251 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15252 bool is_unsigned)
15253 {
15254 std::vector<discriminant_range> ranges;
15255
15256 if (variant.default_branch)
15257 return {};
15258
15259 if (variant.discr_list_data == nullptr)
15260 {
15261 discriminant_range r
15262 = {variant.discriminant_value, variant.discriminant_value};
15263 ranges.push_back (r);
15264 }
15265 else
15266 {
15267 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15268 variant.discr_list_data->size);
15269 while (!data.empty ())
15270 {
15271 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15272 {
15273 complaint (_("invalid discriminant marker: %d"), data[0]);
15274 break;
15275 }
15276 bool is_range = data[0] == DW_DSC_range;
15277 data = data.slice (1);
15278
15279 ULONGEST low, high;
15280 unsigned int bytes_read;
15281
15282 if (data.empty ())
15283 {
15284 complaint (_("DW_AT_discr_list missing low value"));
15285 break;
15286 }
15287 if (is_unsigned)
15288 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15289 else
15290 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15291 &bytes_read);
15292 data = data.slice (bytes_read);
15293
15294 if (is_range)
15295 {
15296 if (data.empty ())
15297 {
15298 complaint (_("DW_AT_discr_list missing high value"));
15299 break;
15300 }
15301 if (is_unsigned)
15302 high = read_unsigned_leb128 (nullptr, data.data (),
15303 &bytes_read);
15304 else
15305 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15306 &bytes_read);
15307 data = data.slice (bytes_read);
15308 }
15309 else
15310 high = low;
15311
15312 ranges.push_back ({ low, high });
15313 }
15314 }
15315
15316 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15317 ranges.size ());
15318 std::copy (ranges.begin (), ranges.end (), result);
15319 return gdb::array_view<discriminant_range> (result, ranges.size ());
15320 }
15321
15322 static const gdb::array_view<variant_part> create_variant_parts
15323 (struct obstack *obstack,
15324 const offset_map_type &offset_map,
15325 struct field_info *fi,
15326 const std::vector<variant_part_builder> &variant_parts);
15327
15328 /* Fill in a "struct variant" for a given variant field. RESULT is
15329 the variant to fill in. OBSTACK is where any needed allocations
15330 will be done. OFFSET_MAP holds the mapping from section offsets to
15331 fields for the type. FI describes the fields of the type we're
15332 processing. FIELD is the variant field we're converting. */
15333
15334 static void
15335 create_one_variant (variant &result, struct obstack *obstack,
15336 const offset_map_type &offset_map,
15337 struct field_info *fi, const variant_field &field)
15338 {
15339 result.discriminants = convert_variant_range (obstack, field, false);
15340 result.first_field = field.first_field + fi->baseclasses.size ();
15341 result.last_field = field.last_field + fi->baseclasses.size ();
15342 result.parts = create_variant_parts (obstack, offset_map, fi,
15343 field.variant_parts);
15344 }
15345
15346 /* Fill in a "struct variant_part" for a given variant part. RESULT
15347 is the variant part to fill in. OBSTACK is where any needed
15348 allocations will be done. OFFSET_MAP holds the mapping from
15349 section offsets to fields for the type. FI describes the fields of
15350 the type we're processing. BUILDER is the variant part to be
15351 converted. */
15352
15353 static void
15354 create_one_variant_part (variant_part &result,
15355 struct obstack *obstack,
15356 const offset_map_type &offset_map,
15357 struct field_info *fi,
15358 const variant_part_builder &builder)
15359 {
15360 auto iter = offset_map.find (builder.discriminant_offset);
15361 if (iter == offset_map.end ())
15362 {
15363 result.discriminant_index = -1;
15364 /* Doesn't matter. */
15365 result.is_unsigned = false;
15366 }
15367 else
15368 {
15369 result.discriminant_index = iter->second;
15370 result.is_unsigned
15371 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15372 }
15373
15374 size_t n = builder.variants.size ();
15375 variant *output = new (obstack) variant[n];
15376 for (size_t i = 0; i < n; ++i)
15377 create_one_variant (output[i], obstack, offset_map, fi,
15378 builder.variants[i]);
15379
15380 result.variants = gdb::array_view<variant> (output, n);
15381 }
15382
15383 /* Create a vector of variant parts that can be attached to a type.
15384 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15385 holds the mapping from section offsets to fields for the type. FI
15386 describes the fields of the type we're processing. VARIANT_PARTS
15387 is the vector to convert. */
15388
15389 static const gdb::array_view<variant_part>
15390 create_variant_parts (struct obstack *obstack,
15391 const offset_map_type &offset_map,
15392 struct field_info *fi,
15393 const std::vector<variant_part_builder> &variant_parts)
15394 {
15395 if (variant_parts.empty ())
15396 return {};
15397
15398 size_t n = variant_parts.size ();
15399 variant_part *result = new (obstack) variant_part[n];
15400 for (size_t i = 0; i < n; ++i)
15401 create_one_variant_part (result[i], obstack, offset_map, fi,
15402 variant_parts[i]);
15403
15404 return gdb::array_view<variant_part> (result, n);
15405 }
15406
15407 /* Compute the variant part vector for FIP, attaching it to TYPE when
15408 done. */
15409
15410 static void
15411 add_variant_property (struct field_info *fip, struct type *type,
15412 struct dwarf2_cu *cu)
15413 {
15414 /* Map section offsets of fields to their field index. Note the
15415 field index here does not take the number of baseclasses into
15416 account. */
15417 offset_map_type offset_map;
15418 for (int i = 0; i < fip->fields.size (); ++i)
15419 offset_map[fip->fields[i].offset] = i;
15420
15421 struct objfile *objfile = cu->per_objfile->objfile;
15422 gdb::array_view<variant_part> parts
15423 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15424 fip->variant_parts);
15425
15426 struct dynamic_prop prop;
15427 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15428 obstack_copy (&objfile->objfile_obstack, &parts,
15429 sizeof (parts)));
15430
15431 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15432 }
15433
15434 /* Create the vector of fields, and attach it to the type. */
15435
15436 static void
15437 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15438 struct dwarf2_cu *cu)
15439 {
15440 int nfields = fip->nfields ();
15441
15442 /* Record the field count, allocate space for the array of fields,
15443 and create blank accessibility bitfields if necessary. */
15444 type->set_num_fields (nfields);
15445 type->set_fields
15446 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15447
15448 if (fip->non_public_fields && cu->language != language_ada)
15449 {
15450 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15451
15452 TYPE_FIELD_PRIVATE_BITS (type) =
15453 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15454 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15455
15456 TYPE_FIELD_PROTECTED_BITS (type) =
15457 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15458 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15459
15460 TYPE_FIELD_IGNORE_BITS (type) =
15461 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15462 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15463 }
15464
15465 /* If the type has baseclasses, allocate and clear a bit vector for
15466 TYPE_FIELD_VIRTUAL_BITS. */
15467 if (!fip->baseclasses.empty () && cu->language != language_ada)
15468 {
15469 int num_bytes = B_BYTES (fip->baseclasses.size ());
15470 unsigned char *pointer;
15471
15472 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15473 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15474 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15475 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15476 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15477 }
15478
15479 if (!fip->variant_parts.empty ())
15480 add_variant_property (fip, type, cu);
15481
15482 /* Copy the saved-up fields into the field vector. */
15483 for (int i = 0; i < nfields; ++i)
15484 {
15485 struct nextfield &field
15486 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15487 : fip->fields[i - fip->baseclasses.size ()]);
15488
15489 type->field (i) = field.field;
15490 switch (field.accessibility)
15491 {
15492 case DW_ACCESS_private:
15493 if (cu->language != language_ada)
15494 SET_TYPE_FIELD_PRIVATE (type, i);
15495 break;
15496
15497 case DW_ACCESS_protected:
15498 if (cu->language != language_ada)
15499 SET_TYPE_FIELD_PROTECTED (type, i);
15500 break;
15501
15502 case DW_ACCESS_public:
15503 break;
15504
15505 default:
15506 /* Unknown accessibility. Complain and treat it as public. */
15507 {
15508 complaint (_("unsupported accessibility %d"),
15509 field.accessibility);
15510 }
15511 break;
15512 }
15513 if (i < fip->baseclasses.size ())
15514 {
15515 switch (field.virtuality)
15516 {
15517 case DW_VIRTUALITY_virtual:
15518 case DW_VIRTUALITY_pure_virtual:
15519 if (cu->language == language_ada)
15520 error (_("unexpected virtuality in component of Ada type"));
15521 SET_TYPE_FIELD_VIRTUAL (type, i);
15522 break;
15523 }
15524 }
15525 }
15526 }
15527
15528 /* Return true if this member function is a constructor, false
15529 otherwise. */
15530
15531 static int
15532 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15533 {
15534 const char *fieldname;
15535 const char *type_name;
15536 int len;
15537
15538 if (die->parent == NULL)
15539 return 0;
15540
15541 if (die->parent->tag != DW_TAG_structure_type
15542 && die->parent->tag != DW_TAG_union_type
15543 && die->parent->tag != DW_TAG_class_type)
15544 return 0;
15545
15546 fieldname = dwarf2_name (die, cu);
15547 type_name = dwarf2_name (die->parent, cu);
15548 if (fieldname == NULL || type_name == NULL)
15549 return 0;
15550
15551 len = strlen (fieldname);
15552 return (strncmp (fieldname, type_name, len) == 0
15553 && (type_name[len] == '\0' || type_name[len] == '<'));
15554 }
15555
15556 /* Add a member function to the proper fieldlist. */
15557
15558 static void
15559 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15560 struct type *type, struct dwarf2_cu *cu)
15561 {
15562 struct objfile *objfile = cu->per_objfile->objfile;
15563 struct attribute *attr;
15564 int i;
15565 struct fnfieldlist *flp = nullptr;
15566 struct fn_field *fnp;
15567 const char *fieldname;
15568 struct type *this_type;
15569
15570 if (cu->language == language_ada)
15571 error (_("unexpected member function in Ada type"));
15572
15573 /* Get name of member function. */
15574 fieldname = dwarf2_name (die, cu);
15575 if (fieldname == NULL)
15576 return;
15577
15578 /* Look up member function name in fieldlist. */
15579 for (i = 0; i < fip->fnfieldlists.size (); i++)
15580 {
15581 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15582 {
15583 flp = &fip->fnfieldlists[i];
15584 break;
15585 }
15586 }
15587
15588 /* Create a new fnfieldlist if necessary. */
15589 if (flp == nullptr)
15590 {
15591 fip->fnfieldlists.emplace_back ();
15592 flp = &fip->fnfieldlists.back ();
15593 flp->name = fieldname;
15594 i = fip->fnfieldlists.size () - 1;
15595 }
15596
15597 /* Create a new member function field and add it to the vector of
15598 fnfieldlists. */
15599 flp->fnfields.emplace_back ();
15600 fnp = &flp->fnfields.back ();
15601
15602 /* Delay processing of the physname until later. */
15603 if (cu->language == language_cplus)
15604 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15605 die, cu);
15606 else
15607 {
15608 const char *physname = dwarf2_physname (fieldname, die, cu);
15609 fnp->physname = physname ? physname : "";
15610 }
15611
15612 fnp->type = alloc_type (objfile);
15613 this_type = read_type_die (die, cu);
15614 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15615 {
15616 int nparams = this_type->num_fields ();
15617
15618 /* TYPE is the domain of this method, and THIS_TYPE is the type
15619 of the method itself (TYPE_CODE_METHOD). */
15620 smash_to_method_type (fnp->type, type,
15621 TYPE_TARGET_TYPE (this_type),
15622 this_type->fields (),
15623 this_type->num_fields (),
15624 this_type->has_varargs ());
15625
15626 /* Handle static member functions.
15627 Dwarf2 has no clean way to discern C++ static and non-static
15628 member functions. G++ helps GDB by marking the first
15629 parameter for non-static member functions (which is the this
15630 pointer) as artificial. We obtain this information from
15631 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15632 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15633 fnp->voffset = VOFFSET_STATIC;
15634 }
15635 else
15636 complaint (_("member function type missing for '%s'"),
15637 dwarf2_full_name (fieldname, die, cu));
15638
15639 /* Get fcontext from DW_AT_containing_type if present. */
15640 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15641 fnp->fcontext = die_containing_type (die, cu);
15642
15643 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15644 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15645
15646 /* Get accessibility. */
15647 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15648 switch (accessibility)
15649 {
15650 case DW_ACCESS_private:
15651 fnp->is_private = 1;
15652 break;
15653 case DW_ACCESS_protected:
15654 fnp->is_protected = 1;
15655 break;
15656 }
15657
15658 /* Check for artificial methods. */
15659 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15660 if (attr && attr->as_boolean ())
15661 fnp->is_artificial = 1;
15662
15663 /* Check for defaulted methods. */
15664 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15665 if (attr != nullptr)
15666 fnp->defaulted = attr->defaulted ();
15667
15668 /* Check for deleted methods. */
15669 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15670 if (attr != nullptr && attr->as_boolean ())
15671 fnp->is_deleted = 1;
15672
15673 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15674
15675 /* Get index in virtual function table if it is a virtual member
15676 function. For older versions of GCC, this is an offset in the
15677 appropriate virtual table, as specified by DW_AT_containing_type.
15678 For everyone else, it is an expression to be evaluated relative
15679 to the object address. */
15680
15681 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15682 if (attr != nullptr)
15683 {
15684 if (attr->form_is_block () && attr->as_block ()->size > 0)
15685 {
15686 struct dwarf_block *block = attr->as_block ();
15687
15688 if (block->data[0] == DW_OP_constu)
15689 {
15690 /* Old-style GCC. */
15691 fnp->voffset = decode_locdesc (block, cu) + 2;
15692 }
15693 else if (block->data[0] == DW_OP_deref
15694 || (block->size > 1
15695 && block->data[0] == DW_OP_deref_size
15696 && block->data[1] == cu->header.addr_size))
15697 {
15698 fnp->voffset = decode_locdesc (block, cu);
15699 if ((fnp->voffset % cu->header.addr_size) != 0)
15700 dwarf2_complex_location_expr_complaint ();
15701 else
15702 fnp->voffset /= cu->header.addr_size;
15703 fnp->voffset += 2;
15704 }
15705 else
15706 dwarf2_complex_location_expr_complaint ();
15707
15708 if (!fnp->fcontext)
15709 {
15710 /* If there is no `this' field and no DW_AT_containing_type,
15711 we cannot actually find a base class context for the
15712 vtable! */
15713 if (this_type->num_fields () == 0
15714 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15715 {
15716 complaint (_("cannot determine context for virtual member "
15717 "function \"%s\" (offset %s)"),
15718 fieldname, sect_offset_str (die->sect_off));
15719 }
15720 else
15721 {
15722 fnp->fcontext
15723 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15724 }
15725 }
15726 }
15727 else if (attr->form_is_section_offset ())
15728 {
15729 dwarf2_complex_location_expr_complaint ();
15730 }
15731 else
15732 {
15733 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15734 fieldname);
15735 }
15736 }
15737 else
15738 {
15739 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15740 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15741 {
15742 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15743 complaint (_("Member function \"%s\" (offset %s) is virtual "
15744 "but the vtable offset is not specified"),
15745 fieldname, sect_offset_str (die->sect_off));
15746 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15747 TYPE_CPLUS_DYNAMIC (type) = 1;
15748 }
15749 }
15750 }
15751
15752 /* Create the vector of member function fields, and attach it to the type. */
15753
15754 static void
15755 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15756 struct dwarf2_cu *cu)
15757 {
15758 if (cu->language == language_ada)
15759 error (_("unexpected member functions in Ada type"));
15760
15761 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15762 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15763 TYPE_ALLOC (type,
15764 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15765
15766 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15767 {
15768 struct fnfieldlist &nf = fip->fnfieldlists[i];
15769 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15770
15771 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15772 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15773 fn_flp->fn_fields = (struct fn_field *)
15774 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15775
15776 for (int k = 0; k < nf.fnfields.size (); ++k)
15777 fn_flp->fn_fields[k] = nf.fnfields[k];
15778 }
15779
15780 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15781 }
15782
15783 /* Returns non-zero if NAME is the name of a vtable member in CU's
15784 language, zero otherwise. */
15785 static int
15786 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15787 {
15788 static const char vptr[] = "_vptr";
15789
15790 /* Look for the C++ form of the vtable. */
15791 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15792 return 1;
15793
15794 return 0;
15795 }
15796
15797 /* GCC outputs unnamed structures that are really pointers to member
15798 functions, with the ABI-specified layout. If TYPE describes
15799 such a structure, smash it into a member function type.
15800
15801 GCC shouldn't do this; it should just output pointer to member DIEs.
15802 This is GCC PR debug/28767. */
15803
15804 static void
15805 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15806 {
15807 struct type *pfn_type, *self_type, *new_type;
15808
15809 /* Check for a structure with no name and two children. */
15810 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15811 return;
15812
15813 /* Check for __pfn and __delta members. */
15814 if (TYPE_FIELD_NAME (type, 0) == NULL
15815 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15816 || TYPE_FIELD_NAME (type, 1) == NULL
15817 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15818 return;
15819
15820 /* Find the type of the method. */
15821 pfn_type = type->field (0).type ();
15822 if (pfn_type == NULL
15823 || pfn_type->code () != TYPE_CODE_PTR
15824 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15825 return;
15826
15827 /* Look for the "this" argument. */
15828 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15829 if (pfn_type->num_fields () == 0
15830 /* || pfn_type->field (0).type () == NULL */
15831 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15832 return;
15833
15834 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15835 new_type = alloc_type (objfile);
15836 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15837 pfn_type->fields (), pfn_type->num_fields (),
15838 pfn_type->has_varargs ());
15839 smash_to_methodptr_type (type, new_type);
15840 }
15841
15842 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15843 requires rewriting, then copy it and return the updated copy.
15844 Otherwise return nullptr. */
15845
15846 static struct type *
15847 rewrite_array_type (struct type *type)
15848 {
15849 if (type->code () != TYPE_CODE_ARRAY)
15850 return nullptr;
15851
15852 struct type *index_type = type->index_type ();
15853 range_bounds *current_bounds = index_type->bounds ();
15854
15855 /* Handle multi-dimensional arrays. */
15856 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15857 if (new_target == nullptr)
15858 {
15859 /* Maybe we don't need to rewrite this array. */
15860 if (current_bounds->low.kind () == PROP_CONST
15861 && current_bounds->high.kind () == PROP_CONST)
15862 return nullptr;
15863 }
15864
15865 /* Either the target type was rewritten, or the bounds have to be
15866 updated. Either way we want to copy the type and update
15867 everything. */
15868 struct type *copy = copy_type (type);
15869 int nfields = copy->num_fields ();
15870 field *new_fields
15871 = ((struct field *) TYPE_ZALLOC (copy,
15872 nfields * sizeof (struct field)));
15873 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15874 copy->set_fields (new_fields);
15875 if (new_target != nullptr)
15876 TYPE_TARGET_TYPE (copy) = new_target;
15877
15878 struct type *index_copy = copy_type (index_type);
15879 range_bounds *bounds
15880 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15881 sizeof (range_bounds));
15882 *bounds = *current_bounds;
15883 bounds->low.set_const_val (1);
15884 bounds->high.set_const_val (0);
15885 index_copy->set_bounds (bounds);
15886 copy->set_index_type (index_copy);
15887
15888 return copy;
15889 }
15890
15891 /* While some versions of GCC will generate complicated DWARF for an
15892 array (see quirk_ada_thick_pointer), more recent versions were
15893 modified to emit an explicit thick pointer structure. However, in
15894 this case, the array still has DWARF expressions for its ranges,
15895 and these must be ignored. */
15896
15897 static void
15898 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15899 struct type *type)
15900 {
15901 gdb_assert (cu->language == language_ada);
15902
15903 /* Check for a structure with two children. */
15904 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15905 return;
15906
15907 /* Check for P_ARRAY and P_BOUNDS members. */
15908 if (TYPE_FIELD_NAME (type, 0) == NULL
15909 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15910 || TYPE_FIELD_NAME (type, 1) == NULL
15911 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15912 return;
15913
15914 /* Make sure we're looking at a pointer to an array. */
15915 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15916 return;
15917
15918 /* The Ada code already knows how to handle these types, so all that
15919 we need to do is turn the bounds into static bounds. However, we
15920 don't want to rewrite existing array or index types in-place,
15921 because those may be referenced in other contexts where this
15922 rewriting is undesirable. */
15923 struct type *new_ary_type
15924 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15925 if (new_ary_type != nullptr)
15926 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15927 }
15928
15929 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15930 appropriate error checking and issuing complaints if there is a
15931 problem. */
15932
15933 static ULONGEST
15934 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15935 {
15936 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15937
15938 if (attr == nullptr)
15939 return 0;
15940
15941 if (!attr->form_is_constant ())
15942 {
15943 complaint (_("DW_AT_alignment must have constant form"
15944 " - DIE at %s [in module %s]"),
15945 sect_offset_str (die->sect_off),
15946 objfile_name (cu->per_objfile->objfile));
15947 return 0;
15948 }
15949
15950 LONGEST val = attr->constant_value (0);
15951 if (val < 0)
15952 {
15953 complaint (_("DW_AT_alignment value must not be negative"
15954 " - DIE at %s [in module %s]"),
15955 sect_offset_str (die->sect_off),
15956 objfile_name (cu->per_objfile->objfile));
15957 return 0;
15958 }
15959 ULONGEST align = val;
15960
15961 if (align == 0)
15962 {
15963 complaint (_("DW_AT_alignment value must not be zero"
15964 " - DIE at %s [in module %s]"),
15965 sect_offset_str (die->sect_off),
15966 objfile_name (cu->per_objfile->objfile));
15967 return 0;
15968 }
15969 if ((align & (align - 1)) != 0)
15970 {
15971 complaint (_("DW_AT_alignment value must be a power of 2"
15972 " - DIE at %s [in module %s]"),
15973 sect_offset_str (die->sect_off),
15974 objfile_name (cu->per_objfile->objfile));
15975 return 0;
15976 }
15977
15978 return align;
15979 }
15980
15981 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15982 the alignment for TYPE. */
15983
15984 static void
15985 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15986 struct type *type)
15987 {
15988 if (!set_type_align (type, get_alignment (cu, die)))
15989 complaint (_("DW_AT_alignment value too large"
15990 " - DIE at %s [in module %s]"),
15991 sect_offset_str (die->sect_off),
15992 objfile_name (cu->per_objfile->objfile));
15993 }
15994
15995 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15996 constant for a type, according to DWARF5 spec, Table 5.5. */
15997
15998 static bool
15999 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16000 {
16001 switch (value)
16002 {
16003 case DW_CC_normal:
16004 case DW_CC_pass_by_reference:
16005 case DW_CC_pass_by_value:
16006 return true;
16007
16008 default:
16009 complaint (_("unrecognized DW_AT_calling_convention value "
16010 "(%s) for a type"), pulongest (value));
16011 return false;
16012 }
16013 }
16014
16015 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16016 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16017 also according to GNU-specific values (see include/dwarf2.h). */
16018
16019 static bool
16020 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16021 {
16022 switch (value)
16023 {
16024 case DW_CC_normal:
16025 case DW_CC_program:
16026 case DW_CC_nocall:
16027 return true;
16028
16029 case DW_CC_GNU_renesas_sh:
16030 case DW_CC_GNU_borland_fastcall_i386:
16031 case DW_CC_GDB_IBM_OpenCL:
16032 return true;
16033
16034 default:
16035 complaint (_("unrecognized DW_AT_calling_convention value "
16036 "(%s) for a subroutine"), pulongest (value));
16037 return false;
16038 }
16039 }
16040
16041 /* Called when we find the DIE that starts a structure or union scope
16042 (definition) to create a type for the structure or union. Fill in
16043 the type's name and general properties; the members will not be
16044 processed until process_structure_scope. A symbol table entry for
16045 the type will also not be done until process_structure_scope (assuming
16046 the type has a name).
16047
16048 NOTE: we need to call these functions regardless of whether or not the
16049 DIE has a DW_AT_name attribute, since it might be an anonymous
16050 structure or union. This gets the type entered into our set of
16051 user defined types. */
16052
16053 static struct type *
16054 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16055 {
16056 struct objfile *objfile = cu->per_objfile->objfile;
16057 struct type *type;
16058 struct attribute *attr;
16059 const char *name;
16060
16061 /* If the definition of this type lives in .debug_types, read that type.
16062 Don't follow DW_AT_specification though, that will take us back up
16063 the chain and we want to go down. */
16064 attr = die->attr (DW_AT_signature);
16065 if (attr != nullptr)
16066 {
16067 type = get_DW_AT_signature_type (die, attr, cu);
16068
16069 /* The type's CU may not be the same as CU.
16070 Ensure TYPE is recorded with CU in die_type_hash. */
16071 return set_die_type (die, type, cu);
16072 }
16073
16074 type = alloc_type (objfile);
16075 INIT_CPLUS_SPECIFIC (type);
16076
16077 name = dwarf2_name (die, cu);
16078 if (name != NULL)
16079 {
16080 if (cu->language == language_cplus
16081 || cu->language == language_d
16082 || cu->language == language_rust)
16083 {
16084 const char *full_name = dwarf2_full_name (name, die, cu);
16085
16086 /* dwarf2_full_name might have already finished building the DIE's
16087 type. If so, there is no need to continue. */
16088 if (get_die_type (die, cu) != NULL)
16089 return get_die_type (die, cu);
16090
16091 type->set_name (full_name);
16092 }
16093 else
16094 {
16095 /* The name is already allocated along with this objfile, so
16096 we don't need to duplicate it for the type. */
16097 type->set_name (name);
16098 }
16099 }
16100
16101 if (die->tag == DW_TAG_structure_type)
16102 {
16103 type->set_code (TYPE_CODE_STRUCT);
16104 }
16105 else if (die->tag == DW_TAG_union_type)
16106 {
16107 type->set_code (TYPE_CODE_UNION);
16108 }
16109 else
16110 {
16111 type->set_code (TYPE_CODE_STRUCT);
16112 }
16113
16114 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16115 TYPE_DECLARED_CLASS (type) = 1;
16116
16117 /* Store the calling convention in the type if it's available in
16118 the die. Otherwise the calling convention remains set to
16119 the default value DW_CC_normal. */
16120 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16121 if (attr != nullptr
16122 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16123 {
16124 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16125 TYPE_CPLUS_CALLING_CONVENTION (type)
16126 = (enum dwarf_calling_convention) (attr->constant_value (0));
16127 }
16128
16129 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16130 if (attr != nullptr)
16131 {
16132 if (attr->form_is_constant ())
16133 TYPE_LENGTH (type) = attr->constant_value (0);
16134 else
16135 {
16136 struct dynamic_prop prop;
16137 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16138 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16139 TYPE_LENGTH (type) = 0;
16140 }
16141 }
16142 else
16143 {
16144 TYPE_LENGTH (type) = 0;
16145 }
16146
16147 maybe_set_alignment (cu, die, type);
16148
16149 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16150 {
16151 /* ICC<14 does not output the required DW_AT_declaration on
16152 incomplete types, but gives them a size of zero. */
16153 type->set_is_stub (true);
16154 }
16155 else
16156 type->set_stub_is_supported (true);
16157
16158 if (die_is_declaration (die, cu))
16159 type->set_is_stub (true);
16160 else if (attr == NULL && die->child == NULL
16161 && producer_is_realview (cu->producer))
16162 /* RealView does not output the required DW_AT_declaration
16163 on incomplete types. */
16164 type->set_is_stub (true);
16165
16166 /* We need to add the type field to the die immediately so we don't
16167 infinitely recurse when dealing with pointers to the structure
16168 type within the structure itself. */
16169 set_die_type (die, type, cu);
16170
16171 /* set_die_type should be already done. */
16172 set_descriptive_type (type, die, cu);
16173
16174 return type;
16175 }
16176
16177 static void handle_struct_member_die
16178 (struct die_info *child_die,
16179 struct type *type,
16180 struct field_info *fi,
16181 std::vector<struct symbol *> *template_args,
16182 struct dwarf2_cu *cu);
16183
16184 /* A helper for handle_struct_member_die that handles
16185 DW_TAG_variant_part. */
16186
16187 static void
16188 handle_variant_part (struct die_info *die, struct type *type,
16189 struct field_info *fi,
16190 std::vector<struct symbol *> *template_args,
16191 struct dwarf2_cu *cu)
16192 {
16193 variant_part_builder *new_part;
16194 if (fi->current_variant_part == nullptr)
16195 {
16196 fi->variant_parts.emplace_back ();
16197 new_part = &fi->variant_parts.back ();
16198 }
16199 else if (!fi->current_variant_part->processing_variant)
16200 {
16201 complaint (_("nested DW_TAG_variant_part seen "
16202 "- DIE at %s [in module %s]"),
16203 sect_offset_str (die->sect_off),
16204 objfile_name (cu->per_objfile->objfile));
16205 return;
16206 }
16207 else
16208 {
16209 variant_field &current = fi->current_variant_part->variants.back ();
16210 current.variant_parts.emplace_back ();
16211 new_part = &current.variant_parts.back ();
16212 }
16213
16214 /* When we recurse, we want callees to add to this new variant
16215 part. */
16216 scoped_restore save_current_variant_part
16217 = make_scoped_restore (&fi->current_variant_part, new_part);
16218
16219 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16220 if (discr == NULL)
16221 {
16222 /* It's a univariant form, an extension we support. */
16223 }
16224 else if (discr->form_is_ref ())
16225 {
16226 struct dwarf2_cu *target_cu = cu;
16227 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16228
16229 new_part->discriminant_offset = target_die->sect_off;
16230 }
16231 else
16232 {
16233 complaint (_("DW_AT_discr does not have DIE reference form"
16234 " - DIE at %s [in module %s]"),
16235 sect_offset_str (die->sect_off),
16236 objfile_name (cu->per_objfile->objfile));
16237 }
16238
16239 for (die_info *child_die = die->child;
16240 child_die != NULL;
16241 child_die = child_die->sibling)
16242 handle_struct_member_die (child_die, type, fi, template_args, cu);
16243 }
16244
16245 /* A helper for handle_struct_member_die that handles
16246 DW_TAG_variant. */
16247
16248 static void
16249 handle_variant (struct die_info *die, struct type *type,
16250 struct field_info *fi,
16251 std::vector<struct symbol *> *template_args,
16252 struct dwarf2_cu *cu)
16253 {
16254 if (fi->current_variant_part == nullptr)
16255 {
16256 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16257 "- DIE at %s [in module %s]"),
16258 sect_offset_str (die->sect_off),
16259 objfile_name (cu->per_objfile->objfile));
16260 return;
16261 }
16262 if (fi->current_variant_part->processing_variant)
16263 {
16264 complaint (_("nested DW_TAG_variant seen "
16265 "- DIE at %s [in module %s]"),
16266 sect_offset_str (die->sect_off),
16267 objfile_name (cu->per_objfile->objfile));
16268 return;
16269 }
16270
16271 scoped_restore save_processing_variant
16272 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16273 true);
16274
16275 fi->current_variant_part->variants.emplace_back ();
16276 variant_field &variant = fi->current_variant_part->variants.back ();
16277 variant.first_field = fi->fields.size ();
16278
16279 /* In a variant we want to get the discriminant and also add a
16280 field for our sole member child. */
16281 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16282 if (discr == nullptr || !discr->form_is_constant ())
16283 {
16284 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16285 if (discr == nullptr || discr->as_block ()->size == 0)
16286 variant.default_branch = true;
16287 else
16288 variant.discr_list_data = discr->as_block ();
16289 }
16290 else
16291 variant.discriminant_value = discr->constant_value (0);
16292
16293 for (die_info *variant_child = die->child;
16294 variant_child != NULL;
16295 variant_child = variant_child->sibling)
16296 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16297
16298 variant.last_field = fi->fields.size ();
16299 }
16300
16301 /* A helper for process_structure_scope that handles a single member
16302 DIE. */
16303
16304 static void
16305 handle_struct_member_die (struct die_info *child_die, struct type *type,
16306 struct field_info *fi,
16307 std::vector<struct symbol *> *template_args,
16308 struct dwarf2_cu *cu)
16309 {
16310 if (child_die->tag == DW_TAG_member
16311 || child_die->tag == DW_TAG_variable)
16312 {
16313 /* NOTE: carlton/2002-11-05: A C++ static data member
16314 should be a DW_TAG_member that is a declaration, but
16315 all versions of G++ as of this writing (so through at
16316 least 3.2.1) incorrectly generate DW_TAG_variable
16317 tags for them instead. */
16318 dwarf2_add_field (fi, child_die, cu);
16319 }
16320 else if (child_die->tag == DW_TAG_subprogram)
16321 {
16322 /* Rust doesn't have member functions in the C++ sense.
16323 However, it does emit ordinary functions as children
16324 of a struct DIE. */
16325 if (cu->language == language_rust)
16326 read_func_scope (child_die, cu);
16327 else
16328 {
16329 /* C++ member function. */
16330 dwarf2_add_member_fn (fi, child_die, type, cu);
16331 }
16332 }
16333 else if (child_die->tag == DW_TAG_inheritance)
16334 {
16335 /* C++ base class field. */
16336 dwarf2_add_field (fi, child_die, cu);
16337 }
16338 else if (type_can_define_types (child_die))
16339 dwarf2_add_type_defn (fi, child_die, cu);
16340 else if (child_die->tag == DW_TAG_template_type_param
16341 || child_die->tag == DW_TAG_template_value_param)
16342 {
16343 struct symbol *arg = new_symbol (child_die, NULL, cu);
16344
16345 if (arg != NULL)
16346 template_args->push_back (arg);
16347 }
16348 else if (child_die->tag == DW_TAG_variant_part)
16349 handle_variant_part (child_die, type, fi, template_args, cu);
16350 else if (child_die->tag == DW_TAG_variant)
16351 handle_variant (child_die, type, fi, template_args, cu);
16352 }
16353
16354 /* Finish creating a structure or union type, including filling in
16355 its members and creating a symbol for it. */
16356
16357 static void
16358 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16359 {
16360 struct objfile *objfile = cu->per_objfile->objfile;
16361 struct die_info *child_die;
16362 struct type *type;
16363
16364 type = get_die_type (die, cu);
16365 if (type == NULL)
16366 type = read_structure_type (die, cu);
16367
16368 bool has_template_parameters = false;
16369 if (die->child != NULL && ! die_is_declaration (die, cu))
16370 {
16371 struct field_info fi;
16372 std::vector<struct symbol *> template_args;
16373
16374 child_die = die->child;
16375
16376 while (child_die && child_die->tag)
16377 {
16378 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16379 child_die = child_die->sibling;
16380 }
16381
16382 /* Attach template arguments to type. */
16383 if (!template_args.empty ())
16384 {
16385 has_template_parameters = true;
16386 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16387 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16388 TYPE_TEMPLATE_ARGUMENTS (type)
16389 = XOBNEWVEC (&objfile->objfile_obstack,
16390 struct symbol *,
16391 TYPE_N_TEMPLATE_ARGUMENTS (type));
16392 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16393 template_args.data (),
16394 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16395 * sizeof (struct symbol *)));
16396 }
16397
16398 /* Attach fields and member functions to the type. */
16399 if (fi.nfields () > 0)
16400 dwarf2_attach_fields_to_type (&fi, type, cu);
16401 if (!fi.fnfieldlists.empty ())
16402 {
16403 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16404
16405 /* Get the type which refers to the base class (possibly this
16406 class itself) which contains the vtable pointer for the current
16407 class from the DW_AT_containing_type attribute. This use of
16408 DW_AT_containing_type is a GNU extension. */
16409
16410 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16411 {
16412 struct type *t = die_containing_type (die, cu);
16413
16414 set_type_vptr_basetype (type, t);
16415 if (type == t)
16416 {
16417 int i;
16418
16419 /* Our own class provides vtbl ptr. */
16420 for (i = t->num_fields () - 1;
16421 i >= TYPE_N_BASECLASSES (t);
16422 --i)
16423 {
16424 const char *fieldname = TYPE_FIELD_NAME (t, i);
16425
16426 if (is_vtable_name (fieldname, cu))
16427 {
16428 set_type_vptr_fieldno (type, i);
16429 break;
16430 }
16431 }
16432
16433 /* Complain if virtual function table field not found. */
16434 if (i < TYPE_N_BASECLASSES (t))
16435 complaint (_("virtual function table pointer "
16436 "not found when defining class '%s'"),
16437 type->name () ? type->name () : "");
16438 }
16439 else
16440 {
16441 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16442 }
16443 }
16444 else if (cu->producer
16445 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16446 {
16447 /* The IBM XLC compiler does not provide direct indication
16448 of the containing type, but the vtable pointer is
16449 always named __vfp. */
16450
16451 int i;
16452
16453 for (i = type->num_fields () - 1;
16454 i >= TYPE_N_BASECLASSES (type);
16455 --i)
16456 {
16457 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16458 {
16459 set_type_vptr_fieldno (type, i);
16460 set_type_vptr_basetype (type, type);
16461 break;
16462 }
16463 }
16464 }
16465 }
16466
16467 /* Copy fi.typedef_field_list linked list elements content into the
16468 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16469 if (!fi.typedef_field_list.empty ())
16470 {
16471 int count = fi.typedef_field_list.size ();
16472
16473 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16474 TYPE_TYPEDEF_FIELD_ARRAY (type)
16475 = ((struct decl_field *)
16476 TYPE_ALLOC (type,
16477 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16478 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16479
16480 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16481 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16482 }
16483
16484 /* Copy fi.nested_types_list linked list elements content into the
16485 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16486 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16487 {
16488 int count = fi.nested_types_list.size ();
16489
16490 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16491 TYPE_NESTED_TYPES_ARRAY (type)
16492 = ((struct decl_field *)
16493 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16494 TYPE_NESTED_TYPES_COUNT (type) = count;
16495
16496 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16497 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16498 }
16499 }
16500
16501 quirk_gcc_member_function_pointer (type, objfile);
16502 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16503 cu->rust_unions.push_back (type);
16504 else if (cu->language == language_ada)
16505 quirk_ada_thick_pointer_struct (die, cu, type);
16506
16507 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16508 snapshots) has been known to create a die giving a declaration
16509 for a class that has, as a child, a die giving a definition for a
16510 nested class. So we have to process our children even if the
16511 current die is a declaration. Normally, of course, a declaration
16512 won't have any children at all. */
16513
16514 child_die = die->child;
16515
16516 while (child_die != NULL && child_die->tag)
16517 {
16518 if (child_die->tag == DW_TAG_member
16519 || child_die->tag == DW_TAG_variable
16520 || child_die->tag == DW_TAG_inheritance
16521 || child_die->tag == DW_TAG_template_value_param
16522 || child_die->tag == DW_TAG_template_type_param)
16523 {
16524 /* Do nothing. */
16525 }
16526 else
16527 process_die (child_die, cu);
16528
16529 child_die = child_die->sibling;
16530 }
16531
16532 /* Do not consider external references. According to the DWARF standard,
16533 these DIEs are identified by the fact that they have no byte_size
16534 attribute, and a declaration attribute. */
16535 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16536 || !die_is_declaration (die, cu)
16537 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16538 {
16539 struct symbol *sym = new_symbol (die, type, cu);
16540
16541 if (has_template_parameters)
16542 {
16543 struct symtab *symtab;
16544 if (sym != nullptr)
16545 symtab = symbol_symtab (sym);
16546 else if (cu->line_header != nullptr)
16547 {
16548 /* Any related symtab will do. */
16549 symtab
16550 = cu->line_header->file_names ()[0].symtab;
16551 }
16552 else
16553 {
16554 symtab = nullptr;
16555 complaint (_("could not find suitable "
16556 "symtab for template parameter"
16557 " - DIE at %s [in module %s]"),
16558 sect_offset_str (die->sect_off),
16559 objfile_name (objfile));
16560 }
16561
16562 if (symtab != nullptr)
16563 {
16564 /* Make sure that the symtab is set on the new symbols.
16565 Even though they don't appear in this symtab directly,
16566 other parts of gdb assume that symbols do, and this is
16567 reasonably true. */
16568 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16569 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16570 }
16571 }
16572 }
16573 }
16574
16575 /* Assuming DIE is an enumeration type, and TYPE is its associated
16576 type, update TYPE using some information only available in DIE's
16577 children. In particular, the fields are computed. */
16578
16579 static void
16580 update_enumeration_type_from_children (struct die_info *die,
16581 struct type *type,
16582 struct dwarf2_cu *cu)
16583 {
16584 struct die_info *child_die;
16585 int unsigned_enum = 1;
16586 int flag_enum = 1;
16587
16588 auto_obstack obstack;
16589 std::vector<struct field> fields;
16590
16591 for (child_die = die->child;
16592 child_die != NULL && child_die->tag;
16593 child_die = child_die->sibling)
16594 {
16595 struct attribute *attr;
16596 LONGEST value;
16597 const gdb_byte *bytes;
16598 struct dwarf2_locexpr_baton *baton;
16599 const char *name;
16600
16601 if (child_die->tag != DW_TAG_enumerator)
16602 continue;
16603
16604 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16605 if (attr == NULL)
16606 continue;
16607
16608 name = dwarf2_name (child_die, cu);
16609 if (name == NULL)
16610 name = "<anonymous enumerator>";
16611
16612 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16613 &value, &bytes, &baton);
16614 if (value < 0)
16615 {
16616 unsigned_enum = 0;
16617 flag_enum = 0;
16618 }
16619 else
16620 {
16621 if (count_one_bits_ll (value) >= 2)
16622 flag_enum = 0;
16623 }
16624
16625 fields.emplace_back ();
16626 struct field &field = fields.back ();
16627 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16628 SET_FIELD_ENUMVAL (field, value);
16629 }
16630
16631 if (!fields.empty ())
16632 {
16633 type->set_num_fields (fields.size ());
16634 type->set_fields
16635 ((struct field *)
16636 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16637 memcpy (type->fields (), fields.data (),
16638 sizeof (struct field) * fields.size ());
16639 }
16640
16641 if (unsigned_enum)
16642 type->set_is_unsigned (true);
16643
16644 if (flag_enum)
16645 TYPE_FLAG_ENUM (type) = 1;
16646 }
16647
16648 /* Given a DW_AT_enumeration_type die, set its type. We do not
16649 complete the type's fields yet, or create any symbols. */
16650
16651 static struct type *
16652 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16653 {
16654 struct objfile *objfile = cu->per_objfile->objfile;
16655 struct type *type;
16656 struct attribute *attr;
16657 const char *name;
16658
16659 /* If the definition of this type lives in .debug_types, read that type.
16660 Don't follow DW_AT_specification though, that will take us back up
16661 the chain and we want to go down. */
16662 attr = die->attr (DW_AT_signature);
16663 if (attr != nullptr)
16664 {
16665 type = get_DW_AT_signature_type (die, attr, cu);
16666
16667 /* The type's CU may not be the same as CU.
16668 Ensure TYPE is recorded with CU in die_type_hash. */
16669 return set_die_type (die, type, cu);
16670 }
16671
16672 type = alloc_type (objfile);
16673
16674 type->set_code (TYPE_CODE_ENUM);
16675 name = dwarf2_full_name (NULL, die, cu);
16676 if (name != NULL)
16677 type->set_name (name);
16678
16679 attr = dwarf2_attr (die, DW_AT_type, cu);
16680 if (attr != NULL)
16681 {
16682 struct type *underlying_type = die_type (die, cu);
16683
16684 TYPE_TARGET_TYPE (type) = underlying_type;
16685 }
16686
16687 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16688 if (attr != nullptr)
16689 {
16690 TYPE_LENGTH (type) = attr->constant_value (0);
16691 }
16692 else
16693 {
16694 TYPE_LENGTH (type) = 0;
16695 }
16696
16697 maybe_set_alignment (cu, die, type);
16698
16699 /* The enumeration DIE can be incomplete. In Ada, any type can be
16700 declared as private in the package spec, and then defined only
16701 inside the package body. Such types are known as Taft Amendment
16702 Types. When another package uses such a type, an incomplete DIE
16703 may be generated by the compiler. */
16704 if (die_is_declaration (die, cu))
16705 type->set_is_stub (true);
16706
16707 /* If this type has an underlying type that is not a stub, then we
16708 may use its attributes. We always use the "unsigned" attribute
16709 in this situation, because ordinarily we guess whether the type
16710 is unsigned -- but the guess can be wrong and the underlying type
16711 can tell us the reality. However, we defer to a local size
16712 attribute if one exists, because this lets the compiler override
16713 the underlying type if needed. */
16714 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16715 {
16716 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16717 underlying_type = check_typedef (underlying_type);
16718
16719 type->set_is_unsigned (underlying_type->is_unsigned ());
16720
16721 if (TYPE_LENGTH (type) == 0)
16722 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16723
16724 if (TYPE_RAW_ALIGN (type) == 0
16725 && TYPE_RAW_ALIGN (underlying_type) != 0)
16726 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16727 }
16728
16729 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16730
16731 set_die_type (die, type, cu);
16732
16733 /* Finish the creation of this type by using the enum's children.
16734 Note that, as usual, this must come after set_die_type to avoid
16735 infinite recursion when trying to compute the names of the
16736 enumerators. */
16737 update_enumeration_type_from_children (die, type, cu);
16738
16739 return type;
16740 }
16741
16742 /* Given a pointer to a die which begins an enumeration, process all
16743 the dies that define the members of the enumeration, and create the
16744 symbol for the enumeration type.
16745
16746 NOTE: We reverse the order of the element list. */
16747
16748 static void
16749 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16750 {
16751 struct type *this_type;
16752
16753 this_type = get_die_type (die, cu);
16754 if (this_type == NULL)
16755 this_type = read_enumeration_type (die, cu);
16756
16757 if (die->child != NULL)
16758 {
16759 struct die_info *child_die;
16760 const char *name;
16761
16762 child_die = die->child;
16763 while (child_die && child_die->tag)
16764 {
16765 if (child_die->tag != DW_TAG_enumerator)
16766 {
16767 process_die (child_die, cu);
16768 }
16769 else
16770 {
16771 name = dwarf2_name (child_die, cu);
16772 if (name)
16773 new_symbol (child_die, this_type, cu);
16774 }
16775
16776 child_die = child_die->sibling;
16777 }
16778 }
16779
16780 /* If we are reading an enum from a .debug_types unit, and the enum
16781 is a declaration, and the enum is not the signatured type in the
16782 unit, then we do not want to add a symbol for it. Adding a
16783 symbol would in some cases obscure the true definition of the
16784 enum, giving users an incomplete type when the definition is
16785 actually available. Note that we do not want to do this for all
16786 enums which are just declarations, because C++0x allows forward
16787 enum declarations. */
16788 if (cu->per_cu->is_debug_types
16789 && die_is_declaration (die, cu))
16790 {
16791 struct signatured_type *sig_type;
16792
16793 sig_type = (struct signatured_type *) cu->per_cu;
16794 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16795 if (sig_type->type_offset_in_section != die->sect_off)
16796 return;
16797 }
16798
16799 new_symbol (die, this_type, cu);
16800 }
16801
16802 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16803 expression for an index type and finds the corresponding field
16804 offset in the hidden "P_BOUNDS" structure. Returns true on success
16805 and updates *FIELD, false if it fails to recognize an
16806 expression. */
16807
16808 static bool
16809 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16810 int *bounds_offset, struct field *field,
16811 struct dwarf2_cu *cu)
16812 {
16813 struct attribute *attr = dwarf2_attr (die, name, cu);
16814 if (attr == nullptr || !attr->form_is_block ())
16815 return false;
16816
16817 const struct dwarf_block *block = attr->as_block ();
16818 const gdb_byte *start = block->data;
16819 const gdb_byte *end = block->data + block->size;
16820
16821 /* The expression to recognize generally looks like:
16822
16823 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16824 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16825
16826 However, the second "plus_uconst" may be missing:
16827
16828 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16829 DW_OP_deref_size: 4)
16830
16831 This happens when the field is at the start of the structure.
16832
16833 Also, the final deref may not be sized:
16834
16835 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16836 DW_OP_deref)
16837
16838 This happens when the size of the index type happens to be the
16839 same as the architecture's word size. This can occur with or
16840 without the second plus_uconst. */
16841
16842 if (end - start < 2)
16843 return false;
16844 if (*start++ != DW_OP_push_object_address)
16845 return false;
16846 if (*start++ != DW_OP_plus_uconst)
16847 return false;
16848
16849 uint64_t this_bound_off;
16850 start = gdb_read_uleb128 (start, end, &this_bound_off);
16851 if (start == nullptr || (int) this_bound_off != this_bound_off)
16852 return false;
16853 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16854 is consistent among all bounds. */
16855 if (*bounds_offset == -1)
16856 *bounds_offset = this_bound_off;
16857 else if (*bounds_offset != this_bound_off)
16858 return false;
16859
16860 if (start == end || *start++ != DW_OP_deref)
16861 return false;
16862
16863 int offset = 0;
16864 if (start ==end)
16865 return false;
16866 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16867 {
16868 /* This means an offset of 0. */
16869 }
16870 else if (*start++ != DW_OP_plus_uconst)
16871 return false;
16872 else
16873 {
16874 /* The size is the parameter to DW_OP_plus_uconst. */
16875 uint64_t val;
16876 start = gdb_read_uleb128 (start, end, &val);
16877 if (start == nullptr)
16878 return false;
16879 if ((int) val != val)
16880 return false;
16881 offset = val;
16882 }
16883
16884 if (start == end)
16885 return false;
16886
16887 uint64_t size;
16888 if (*start == DW_OP_deref_size)
16889 {
16890 start = gdb_read_uleb128 (start + 1, end, &size);
16891 if (start == nullptr)
16892 return false;
16893 }
16894 else if (*start == DW_OP_deref)
16895 {
16896 size = cu->header.addr_size;
16897 ++start;
16898 }
16899 else
16900 return false;
16901
16902 SET_FIELD_BITPOS (*field, 8 * offset);
16903 if (size != TYPE_LENGTH (field->type ()))
16904 FIELD_BITSIZE (*field) = 8 * size;
16905
16906 return true;
16907 }
16908
16909 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16910 some kinds of Ada arrays:
16911
16912 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16913 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16914 <11e0> DW_AT_data_location: 2 byte block: 97 6
16915 (DW_OP_push_object_address; DW_OP_deref)
16916 <11e3> DW_AT_type : <0x1173>
16917 <11e7> DW_AT_sibling : <0x1201>
16918 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16919 <11ec> DW_AT_type : <0x1206>
16920 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16921 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16922 DW_OP_deref_size: 4)
16923 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16924 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16925 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16926
16927 This actually represents a "thick pointer", which is a structure
16928 with two elements: one that is a pointer to the array data, and one
16929 that is a pointer to another structure; this second structure holds
16930 the array bounds.
16931
16932 This returns a new type on success, or nullptr if this didn't
16933 recognize the type. */
16934
16935 static struct type *
16936 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16937 struct type *type)
16938 {
16939 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16940 /* So far we've only seen this with block form. */
16941 if (attr == nullptr || !attr->form_is_block ())
16942 return nullptr;
16943
16944 /* Note that this will fail if the structure layout is changed by
16945 the compiler. However, we have no good way to recognize some
16946 other layout, because we don't know what expression the compiler
16947 might choose to emit should this happen. */
16948 struct dwarf_block *blk = attr->as_block ();
16949 if (blk->size != 2
16950 || blk->data[0] != DW_OP_push_object_address
16951 || blk->data[1] != DW_OP_deref)
16952 return nullptr;
16953
16954 int bounds_offset = -1;
16955 int max_align = -1;
16956 std::vector<struct field> range_fields;
16957 for (struct die_info *child_die = die->child;
16958 child_die;
16959 child_die = child_die->sibling)
16960 {
16961 if (child_die->tag == DW_TAG_subrange_type)
16962 {
16963 struct type *underlying = read_subrange_index_type (child_die, cu);
16964
16965 int this_align = type_align (underlying);
16966 if (this_align > max_align)
16967 max_align = this_align;
16968
16969 range_fields.emplace_back ();
16970 range_fields.emplace_back ();
16971
16972 struct field &lower = range_fields[range_fields.size () - 2];
16973 struct field &upper = range_fields[range_fields.size () - 1];
16974
16975 lower.set_type (underlying);
16976 FIELD_ARTIFICIAL (lower) = 1;
16977
16978 upper.set_type (underlying);
16979 FIELD_ARTIFICIAL (upper) = 1;
16980
16981 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16982 &bounds_offset, &lower, cu)
16983 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16984 &bounds_offset, &upper, cu))
16985 return nullptr;
16986 }
16987 }
16988
16989 /* This shouldn't really happen, but double-check that we found
16990 where the bounds are stored. */
16991 if (bounds_offset == -1)
16992 return nullptr;
16993
16994 struct objfile *objfile = cu->per_objfile->objfile;
16995 for (int i = 0; i < range_fields.size (); i += 2)
16996 {
16997 char name[20];
16998
16999 /* Set the name of each field in the bounds. */
17000 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17001 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17002 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17003 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17004 }
17005
17006 struct type *bounds = alloc_type (objfile);
17007 bounds->set_code (TYPE_CODE_STRUCT);
17008
17009 bounds->set_num_fields (range_fields.size ());
17010 bounds->set_fields
17011 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17012 * sizeof (struct field))));
17013 memcpy (bounds->fields (), range_fields.data (),
17014 bounds->num_fields () * sizeof (struct field));
17015
17016 int last_fieldno = range_fields.size () - 1;
17017 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17018 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17019 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17020
17021 /* Rewrite the existing array type in place. Specifically, we
17022 remove any dynamic properties we might have read, and we replace
17023 the index types. */
17024 struct type *iter = type;
17025 for (int i = 0; i < range_fields.size (); i += 2)
17026 {
17027 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17028 iter->main_type->dyn_prop_list = nullptr;
17029 iter->set_index_type
17030 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17031 iter = TYPE_TARGET_TYPE (iter);
17032 }
17033
17034 struct type *result = alloc_type (objfile);
17035 result->set_code (TYPE_CODE_STRUCT);
17036
17037 result->set_num_fields (2);
17038 result->set_fields
17039 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17040 * sizeof (struct field))));
17041
17042 /* The names are chosen to coincide with what the compiler does with
17043 -fgnat-encodings=all, which the Ada code in gdb already
17044 understands. */
17045 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17046 result->field (0).set_type (lookup_pointer_type (type));
17047
17048 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17049 result->field (1).set_type (lookup_pointer_type (bounds));
17050 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17051
17052 result->set_name (type->name ());
17053 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17054 + TYPE_LENGTH (result->field (1).type ()));
17055
17056 return result;
17057 }
17058
17059 /* Extract all information from a DW_TAG_array_type DIE and put it in
17060 the DIE's type field. For now, this only handles one dimensional
17061 arrays. */
17062
17063 static struct type *
17064 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17065 {
17066 struct objfile *objfile = cu->per_objfile->objfile;
17067 struct die_info *child_die;
17068 struct type *type;
17069 struct type *element_type, *range_type, *index_type;
17070 struct attribute *attr;
17071 const char *name;
17072 struct dynamic_prop *byte_stride_prop = NULL;
17073 unsigned int bit_stride = 0;
17074
17075 element_type = die_type (die, cu);
17076
17077 /* The die_type call above may have already set the type for this DIE. */
17078 type = get_die_type (die, cu);
17079 if (type)
17080 return type;
17081
17082 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17083 if (attr != NULL)
17084 {
17085 int stride_ok;
17086 struct type *prop_type = cu->addr_sized_int_type (false);
17087
17088 byte_stride_prop
17089 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17090 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17091 prop_type);
17092 if (!stride_ok)
17093 {
17094 complaint (_("unable to read array DW_AT_byte_stride "
17095 " - DIE at %s [in module %s]"),
17096 sect_offset_str (die->sect_off),
17097 objfile_name (cu->per_objfile->objfile));
17098 /* Ignore this attribute. We will likely not be able to print
17099 arrays of this type correctly, but there is little we can do
17100 to help if we cannot read the attribute's value. */
17101 byte_stride_prop = NULL;
17102 }
17103 }
17104
17105 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17106 if (attr != NULL)
17107 bit_stride = attr->constant_value (0);
17108
17109 /* Irix 6.2 native cc creates array types without children for
17110 arrays with unspecified length. */
17111 if (die->child == NULL)
17112 {
17113 index_type = objfile_type (objfile)->builtin_int;
17114 range_type = create_static_range_type (NULL, index_type, 0, -1);
17115 type = create_array_type_with_stride (NULL, element_type, range_type,
17116 byte_stride_prop, bit_stride);
17117 return set_die_type (die, type, cu);
17118 }
17119
17120 std::vector<struct type *> range_types;
17121 child_die = die->child;
17122 while (child_die && child_die->tag)
17123 {
17124 if (child_die->tag == DW_TAG_subrange_type)
17125 {
17126 struct type *child_type = read_type_die (child_die, cu);
17127
17128 if (child_type != NULL)
17129 {
17130 /* The range type was succesfully read. Save it for the
17131 array type creation. */
17132 range_types.push_back (child_type);
17133 }
17134 }
17135 child_die = child_die->sibling;
17136 }
17137
17138 if (range_types.empty ())
17139 {
17140 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17141 sect_offset_str (die->sect_off),
17142 objfile_name (cu->per_objfile->objfile));
17143 return NULL;
17144 }
17145
17146 /* Dwarf2 dimensions are output from left to right, create the
17147 necessary array types in backwards order. */
17148
17149 type = element_type;
17150
17151 if (read_array_order (die, cu) == DW_ORD_col_major)
17152 {
17153 int i = 0;
17154
17155 while (i < range_types.size ())
17156 {
17157 type = create_array_type_with_stride (NULL, type, range_types[i++],
17158 byte_stride_prop, bit_stride);
17159 bit_stride = 0;
17160 byte_stride_prop = nullptr;
17161 }
17162 }
17163 else
17164 {
17165 size_t ndim = range_types.size ();
17166 while (ndim-- > 0)
17167 {
17168 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17169 byte_stride_prop, bit_stride);
17170 bit_stride = 0;
17171 byte_stride_prop = nullptr;
17172 }
17173 }
17174
17175 gdb_assert (type != element_type);
17176
17177 /* Understand Dwarf2 support for vector types (like they occur on
17178 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17179 array type. This is not part of the Dwarf2/3 standard yet, but a
17180 custom vendor extension. The main difference between a regular
17181 array and the vector variant is that vectors are passed by value
17182 to functions. */
17183 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17184 if (attr != nullptr)
17185 make_vector_type (type);
17186
17187 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17188 implementation may choose to implement triple vectors using this
17189 attribute. */
17190 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17191 if (attr != nullptr && attr->form_is_unsigned ())
17192 {
17193 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17194 TYPE_LENGTH (type) = attr->as_unsigned ();
17195 else
17196 complaint (_("DW_AT_byte_size for array type smaller "
17197 "than the total size of elements"));
17198 }
17199
17200 name = dwarf2_name (die, cu);
17201 if (name)
17202 type->set_name (name);
17203
17204 maybe_set_alignment (cu, die, type);
17205
17206 struct type *replacement_type = nullptr;
17207 if (cu->language == language_ada)
17208 {
17209 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17210 if (replacement_type != nullptr)
17211 type = replacement_type;
17212 }
17213
17214 /* Install the type in the die. */
17215 set_die_type (die, type, cu, replacement_type != nullptr);
17216
17217 /* set_die_type should be already done. */
17218 set_descriptive_type (type, die, cu);
17219
17220 return type;
17221 }
17222
17223 static enum dwarf_array_dim_ordering
17224 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17225 {
17226 struct attribute *attr;
17227
17228 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17229
17230 if (attr != nullptr)
17231 {
17232 LONGEST val = attr->constant_value (-1);
17233 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17234 return (enum dwarf_array_dim_ordering) val;
17235 }
17236
17237 /* GNU F77 is a special case, as at 08/2004 array type info is the
17238 opposite order to the dwarf2 specification, but data is still
17239 laid out as per normal fortran.
17240
17241 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17242 version checking. */
17243
17244 if (cu->language == language_fortran
17245 && cu->producer && strstr (cu->producer, "GNU F77"))
17246 {
17247 return DW_ORD_row_major;
17248 }
17249
17250 switch (cu->language_defn->array_ordering ())
17251 {
17252 case array_column_major:
17253 return DW_ORD_col_major;
17254 case array_row_major:
17255 default:
17256 return DW_ORD_row_major;
17257 };
17258 }
17259
17260 /* Extract all information from a DW_TAG_set_type DIE and put it in
17261 the DIE's type field. */
17262
17263 static struct type *
17264 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17265 {
17266 struct type *domain_type, *set_type;
17267 struct attribute *attr;
17268
17269 domain_type = die_type (die, cu);
17270
17271 /* The die_type call above may have already set the type for this DIE. */
17272 set_type = get_die_type (die, cu);
17273 if (set_type)
17274 return set_type;
17275
17276 set_type = create_set_type (NULL, domain_type);
17277
17278 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17279 if (attr != nullptr && attr->form_is_unsigned ())
17280 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17281
17282 maybe_set_alignment (cu, die, set_type);
17283
17284 return set_die_type (die, set_type, cu);
17285 }
17286
17287 /* A helper for read_common_block that creates a locexpr baton.
17288 SYM is the symbol which we are marking as computed.
17289 COMMON_DIE is the DIE for the common block.
17290 COMMON_LOC is the location expression attribute for the common
17291 block itself.
17292 MEMBER_LOC is the location expression attribute for the particular
17293 member of the common block that we are processing.
17294 CU is the CU from which the above come. */
17295
17296 static void
17297 mark_common_block_symbol_computed (struct symbol *sym,
17298 struct die_info *common_die,
17299 struct attribute *common_loc,
17300 struct attribute *member_loc,
17301 struct dwarf2_cu *cu)
17302 {
17303 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17304 struct objfile *objfile = per_objfile->objfile;
17305 struct dwarf2_locexpr_baton *baton;
17306 gdb_byte *ptr;
17307 unsigned int cu_off;
17308 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17309 LONGEST offset = 0;
17310
17311 gdb_assert (common_loc && member_loc);
17312 gdb_assert (common_loc->form_is_block ());
17313 gdb_assert (member_loc->form_is_block ()
17314 || member_loc->form_is_constant ());
17315
17316 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17317 baton->per_objfile = per_objfile;
17318 baton->per_cu = cu->per_cu;
17319 gdb_assert (baton->per_cu);
17320
17321 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17322
17323 if (member_loc->form_is_constant ())
17324 {
17325 offset = member_loc->constant_value (0);
17326 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17327 }
17328 else
17329 baton->size += member_loc->as_block ()->size;
17330
17331 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17332 baton->data = ptr;
17333
17334 *ptr++ = DW_OP_call4;
17335 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17336 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17337 ptr += 4;
17338
17339 if (member_loc->form_is_constant ())
17340 {
17341 *ptr++ = DW_OP_addr;
17342 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17343 ptr += cu->header.addr_size;
17344 }
17345 else
17346 {
17347 /* We have to copy the data here, because DW_OP_call4 will only
17348 use a DW_AT_location attribute. */
17349 struct dwarf_block *block = member_loc->as_block ();
17350 memcpy (ptr, block->data, block->size);
17351 ptr += block->size;
17352 }
17353
17354 *ptr++ = DW_OP_plus;
17355 gdb_assert (ptr - baton->data == baton->size);
17356
17357 SYMBOL_LOCATION_BATON (sym) = baton;
17358 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17359 }
17360
17361 /* Create appropriate locally-scoped variables for all the
17362 DW_TAG_common_block entries. Also create a struct common_block
17363 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17364 is used to separate the common blocks name namespace from regular
17365 variable names. */
17366
17367 static void
17368 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17369 {
17370 struct attribute *attr;
17371
17372 attr = dwarf2_attr (die, DW_AT_location, cu);
17373 if (attr != nullptr)
17374 {
17375 /* Support the .debug_loc offsets. */
17376 if (attr->form_is_block ())
17377 {
17378 /* Ok. */
17379 }
17380 else if (attr->form_is_section_offset ())
17381 {
17382 dwarf2_complex_location_expr_complaint ();
17383 attr = NULL;
17384 }
17385 else
17386 {
17387 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17388 "common block member");
17389 attr = NULL;
17390 }
17391 }
17392
17393 if (die->child != NULL)
17394 {
17395 struct objfile *objfile = cu->per_objfile->objfile;
17396 struct die_info *child_die;
17397 size_t n_entries = 0, size;
17398 struct common_block *common_block;
17399 struct symbol *sym;
17400
17401 for (child_die = die->child;
17402 child_die && child_die->tag;
17403 child_die = child_die->sibling)
17404 ++n_entries;
17405
17406 size = (sizeof (struct common_block)
17407 + (n_entries - 1) * sizeof (struct symbol *));
17408 common_block
17409 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17410 size);
17411 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17412 common_block->n_entries = 0;
17413
17414 for (child_die = die->child;
17415 child_die && child_die->tag;
17416 child_die = child_die->sibling)
17417 {
17418 /* Create the symbol in the DW_TAG_common_block block in the current
17419 symbol scope. */
17420 sym = new_symbol (child_die, NULL, cu);
17421 if (sym != NULL)
17422 {
17423 struct attribute *member_loc;
17424
17425 common_block->contents[common_block->n_entries++] = sym;
17426
17427 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17428 cu);
17429 if (member_loc)
17430 {
17431 /* GDB has handled this for a long time, but it is
17432 not specified by DWARF. It seems to have been
17433 emitted by gfortran at least as recently as:
17434 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17435 complaint (_("Variable in common block has "
17436 "DW_AT_data_member_location "
17437 "- DIE at %s [in module %s]"),
17438 sect_offset_str (child_die->sect_off),
17439 objfile_name (objfile));
17440
17441 if (member_loc->form_is_section_offset ())
17442 dwarf2_complex_location_expr_complaint ();
17443 else if (member_loc->form_is_constant ()
17444 || member_loc->form_is_block ())
17445 {
17446 if (attr != nullptr)
17447 mark_common_block_symbol_computed (sym, die, attr,
17448 member_loc, cu);
17449 }
17450 else
17451 dwarf2_complex_location_expr_complaint ();
17452 }
17453 }
17454 }
17455
17456 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17457 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17458 }
17459 }
17460
17461 /* Create a type for a C++ namespace. */
17462
17463 static struct type *
17464 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17465 {
17466 struct objfile *objfile = cu->per_objfile->objfile;
17467 const char *previous_prefix, *name;
17468 int is_anonymous;
17469 struct type *type;
17470
17471 /* For extensions, reuse the type of the original namespace. */
17472 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17473 {
17474 struct die_info *ext_die;
17475 struct dwarf2_cu *ext_cu = cu;
17476
17477 ext_die = dwarf2_extension (die, &ext_cu);
17478 type = read_type_die (ext_die, ext_cu);
17479
17480 /* EXT_CU may not be the same as CU.
17481 Ensure TYPE is recorded with CU in die_type_hash. */
17482 return set_die_type (die, type, cu);
17483 }
17484
17485 name = namespace_name (die, &is_anonymous, cu);
17486
17487 /* Now build the name of the current namespace. */
17488
17489 previous_prefix = determine_prefix (die, cu);
17490 if (previous_prefix[0] != '\0')
17491 name = typename_concat (&objfile->objfile_obstack,
17492 previous_prefix, name, 0, cu);
17493
17494 /* Create the type. */
17495 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17496
17497 return set_die_type (die, type, cu);
17498 }
17499
17500 /* Read a namespace scope. */
17501
17502 static void
17503 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17504 {
17505 struct objfile *objfile = cu->per_objfile->objfile;
17506 int is_anonymous;
17507
17508 /* Add a symbol associated to this if we haven't seen the namespace
17509 before. Also, add a using directive if it's an anonymous
17510 namespace. */
17511
17512 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17513 {
17514 struct type *type;
17515
17516 type = read_type_die (die, cu);
17517 new_symbol (die, type, cu);
17518
17519 namespace_name (die, &is_anonymous, cu);
17520 if (is_anonymous)
17521 {
17522 const char *previous_prefix = determine_prefix (die, cu);
17523
17524 std::vector<const char *> excludes;
17525 add_using_directive (using_directives (cu),
17526 previous_prefix, type->name (), NULL,
17527 NULL, excludes, 0, &objfile->objfile_obstack);
17528 }
17529 }
17530
17531 if (die->child != NULL)
17532 {
17533 struct die_info *child_die = die->child;
17534
17535 while (child_die && child_die->tag)
17536 {
17537 process_die (child_die, cu);
17538 child_die = child_die->sibling;
17539 }
17540 }
17541 }
17542
17543 /* Read a Fortran module as type. This DIE can be only a declaration used for
17544 imported module. Still we need that type as local Fortran "use ... only"
17545 declaration imports depend on the created type in determine_prefix. */
17546
17547 static struct type *
17548 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17549 {
17550 struct objfile *objfile = cu->per_objfile->objfile;
17551 const char *module_name;
17552 struct type *type;
17553
17554 module_name = dwarf2_name (die, cu);
17555 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17556
17557 return set_die_type (die, type, cu);
17558 }
17559
17560 /* Read a Fortran module. */
17561
17562 static void
17563 read_module (struct die_info *die, struct dwarf2_cu *cu)
17564 {
17565 struct die_info *child_die = die->child;
17566 struct type *type;
17567
17568 type = read_type_die (die, cu);
17569 new_symbol (die, type, cu);
17570
17571 while (child_die && child_die->tag)
17572 {
17573 process_die (child_die, cu);
17574 child_die = child_die->sibling;
17575 }
17576 }
17577
17578 /* Return the name of the namespace represented by DIE. Set
17579 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17580 namespace. */
17581
17582 static const char *
17583 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17584 {
17585 struct die_info *current_die;
17586 const char *name = NULL;
17587
17588 /* Loop through the extensions until we find a name. */
17589
17590 for (current_die = die;
17591 current_die != NULL;
17592 current_die = dwarf2_extension (die, &cu))
17593 {
17594 /* We don't use dwarf2_name here so that we can detect the absence
17595 of a name -> anonymous namespace. */
17596 name = dwarf2_string_attr (die, DW_AT_name, cu);
17597
17598 if (name != NULL)
17599 break;
17600 }
17601
17602 /* Is it an anonymous namespace? */
17603
17604 *is_anonymous = (name == NULL);
17605 if (*is_anonymous)
17606 name = CP_ANONYMOUS_NAMESPACE_STR;
17607
17608 return name;
17609 }
17610
17611 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17612 the user defined type vector. */
17613
17614 static struct type *
17615 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17616 {
17617 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17618 struct comp_unit_head *cu_header = &cu->header;
17619 struct type *type;
17620 struct attribute *attr_byte_size;
17621 struct attribute *attr_address_class;
17622 int byte_size, addr_class;
17623 struct type *target_type;
17624
17625 target_type = die_type (die, cu);
17626
17627 /* The die_type call above may have already set the type for this DIE. */
17628 type = get_die_type (die, cu);
17629 if (type)
17630 return type;
17631
17632 type = lookup_pointer_type (target_type);
17633
17634 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17635 if (attr_byte_size)
17636 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17637 else
17638 byte_size = cu_header->addr_size;
17639
17640 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17641 if (attr_address_class)
17642 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17643 else
17644 addr_class = DW_ADDR_none;
17645
17646 ULONGEST alignment = get_alignment (cu, die);
17647
17648 /* If the pointer size, alignment, or address class is different
17649 than the default, create a type variant marked as such and set
17650 the length accordingly. */
17651 if (TYPE_LENGTH (type) != byte_size
17652 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17653 && alignment != TYPE_RAW_ALIGN (type))
17654 || addr_class != DW_ADDR_none)
17655 {
17656 if (gdbarch_address_class_type_flags_p (gdbarch))
17657 {
17658 type_instance_flags type_flags
17659 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17660 addr_class);
17661 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17662 == 0);
17663 type = make_type_with_address_space (type, type_flags);
17664 }
17665 else if (TYPE_LENGTH (type) != byte_size)
17666 {
17667 complaint (_("invalid pointer size %d"), byte_size);
17668 }
17669 else if (TYPE_RAW_ALIGN (type) != alignment)
17670 {
17671 complaint (_("Invalid DW_AT_alignment"
17672 " - DIE at %s [in module %s]"),
17673 sect_offset_str (die->sect_off),
17674 objfile_name (cu->per_objfile->objfile));
17675 }
17676 else
17677 {
17678 /* Should we also complain about unhandled address classes? */
17679 }
17680 }
17681
17682 TYPE_LENGTH (type) = byte_size;
17683 set_type_align (type, alignment);
17684 return set_die_type (die, type, cu);
17685 }
17686
17687 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17688 the user defined type vector. */
17689
17690 static struct type *
17691 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17692 {
17693 struct type *type;
17694 struct type *to_type;
17695 struct type *domain;
17696
17697 to_type = die_type (die, cu);
17698 domain = die_containing_type (die, cu);
17699
17700 /* The calls above may have already set the type for this DIE. */
17701 type = get_die_type (die, cu);
17702 if (type)
17703 return type;
17704
17705 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17706 type = lookup_methodptr_type (to_type);
17707 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17708 {
17709 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17710
17711 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17712 to_type->fields (), to_type->num_fields (),
17713 to_type->has_varargs ());
17714 type = lookup_methodptr_type (new_type);
17715 }
17716 else
17717 type = lookup_memberptr_type (to_type, domain);
17718
17719 return set_die_type (die, type, cu);
17720 }
17721
17722 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17723 the user defined type vector. */
17724
17725 static struct type *
17726 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17727 enum type_code refcode)
17728 {
17729 struct comp_unit_head *cu_header = &cu->header;
17730 struct type *type, *target_type;
17731 struct attribute *attr;
17732
17733 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17734
17735 target_type = die_type (die, cu);
17736
17737 /* The die_type call above may have already set the type for this DIE. */
17738 type = get_die_type (die, cu);
17739 if (type)
17740 return type;
17741
17742 type = lookup_reference_type (target_type, refcode);
17743 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17744 if (attr != nullptr)
17745 {
17746 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17747 }
17748 else
17749 {
17750 TYPE_LENGTH (type) = cu_header->addr_size;
17751 }
17752 maybe_set_alignment (cu, die, type);
17753 return set_die_type (die, type, cu);
17754 }
17755
17756 /* Add the given cv-qualifiers to the element type of the array. GCC
17757 outputs DWARF type qualifiers that apply to an array, not the
17758 element type. But GDB relies on the array element type to carry
17759 the cv-qualifiers. This mimics section 6.7.3 of the C99
17760 specification. */
17761
17762 static struct type *
17763 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17764 struct type *base_type, int cnst, int voltl)
17765 {
17766 struct type *el_type, *inner_array;
17767
17768 base_type = copy_type (base_type);
17769 inner_array = base_type;
17770
17771 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17772 {
17773 TYPE_TARGET_TYPE (inner_array) =
17774 copy_type (TYPE_TARGET_TYPE (inner_array));
17775 inner_array = TYPE_TARGET_TYPE (inner_array);
17776 }
17777
17778 el_type = TYPE_TARGET_TYPE (inner_array);
17779 cnst |= TYPE_CONST (el_type);
17780 voltl |= TYPE_VOLATILE (el_type);
17781 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17782
17783 return set_die_type (die, base_type, cu);
17784 }
17785
17786 static struct type *
17787 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17788 {
17789 struct type *base_type, *cv_type;
17790
17791 base_type = die_type (die, cu);
17792
17793 /* The die_type call above may have already set the type for this DIE. */
17794 cv_type = get_die_type (die, cu);
17795 if (cv_type)
17796 return cv_type;
17797
17798 /* In case the const qualifier is applied to an array type, the element type
17799 is so qualified, not the array type (section 6.7.3 of C99). */
17800 if (base_type->code () == TYPE_CODE_ARRAY)
17801 return add_array_cv_type (die, cu, base_type, 1, 0);
17802
17803 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17804 return set_die_type (die, cv_type, cu);
17805 }
17806
17807 static struct type *
17808 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17809 {
17810 struct type *base_type, *cv_type;
17811
17812 base_type = die_type (die, cu);
17813
17814 /* The die_type call above may have already set the type for this DIE. */
17815 cv_type = get_die_type (die, cu);
17816 if (cv_type)
17817 return cv_type;
17818
17819 /* In case the volatile qualifier is applied to an array type, the
17820 element type is so qualified, not the array type (section 6.7.3
17821 of C99). */
17822 if (base_type->code () == TYPE_CODE_ARRAY)
17823 return add_array_cv_type (die, cu, base_type, 0, 1);
17824
17825 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17826 return set_die_type (die, cv_type, cu);
17827 }
17828
17829 /* Handle DW_TAG_restrict_type. */
17830
17831 static struct type *
17832 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17833 {
17834 struct type *base_type, *cv_type;
17835
17836 base_type = die_type (die, cu);
17837
17838 /* The die_type call above may have already set the type for this DIE. */
17839 cv_type = get_die_type (die, cu);
17840 if (cv_type)
17841 return cv_type;
17842
17843 cv_type = make_restrict_type (base_type);
17844 return set_die_type (die, cv_type, cu);
17845 }
17846
17847 /* Handle DW_TAG_atomic_type. */
17848
17849 static struct type *
17850 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17851 {
17852 struct type *base_type, *cv_type;
17853
17854 base_type = die_type (die, cu);
17855
17856 /* The die_type call above may have already set the type for this DIE. */
17857 cv_type = get_die_type (die, cu);
17858 if (cv_type)
17859 return cv_type;
17860
17861 cv_type = make_atomic_type (base_type);
17862 return set_die_type (die, cv_type, cu);
17863 }
17864
17865 /* Extract all information from a DW_TAG_string_type DIE and add to
17866 the user defined type vector. It isn't really a user defined type,
17867 but it behaves like one, with other DIE's using an AT_user_def_type
17868 attribute to reference it. */
17869
17870 static struct type *
17871 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17872 {
17873 struct objfile *objfile = cu->per_objfile->objfile;
17874 struct gdbarch *gdbarch = objfile->arch ();
17875 struct type *type, *range_type, *index_type, *char_type;
17876 struct attribute *attr;
17877 struct dynamic_prop prop;
17878 bool length_is_constant = true;
17879 LONGEST length;
17880
17881 /* There are a couple of places where bit sizes might be made use of
17882 when parsing a DW_TAG_string_type, however, no producer that we know
17883 of make use of these. Handling bit sizes that are a multiple of the
17884 byte size is easy enough, but what about other bit sizes? Lets deal
17885 with that problem when we have to. Warn about these attributes being
17886 unsupported, then parse the type and ignore them like we always
17887 have. */
17888 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17889 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17890 {
17891 static bool warning_printed = false;
17892 if (!warning_printed)
17893 {
17894 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17895 "currently supported on DW_TAG_string_type."));
17896 warning_printed = true;
17897 }
17898 }
17899
17900 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17901 if (attr != nullptr && !attr->form_is_constant ())
17902 {
17903 /* The string length describes the location at which the length of
17904 the string can be found. The size of the length field can be
17905 specified with one of the attributes below. */
17906 struct type *prop_type;
17907 struct attribute *len
17908 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17909 if (len == nullptr)
17910 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17911 if (len != nullptr && len->form_is_constant ())
17912 {
17913 /* Pass 0 as the default as we know this attribute is constant
17914 and the default value will not be returned. */
17915 LONGEST sz = len->constant_value (0);
17916 prop_type = cu->per_objfile->int_type (sz, true);
17917 }
17918 else
17919 {
17920 /* If the size is not specified then we assume it is the size of
17921 an address on this target. */
17922 prop_type = cu->addr_sized_int_type (true);
17923 }
17924
17925 /* Convert the attribute into a dynamic property. */
17926 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17927 length = 1;
17928 else
17929 length_is_constant = false;
17930 }
17931 else if (attr != nullptr)
17932 {
17933 /* This DW_AT_string_length just contains the length with no
17934 indirection. There's no need to create a dynamic property in this
17935 case. Pass 0 for the default value as we know it will not be
17936 returned in this case. */
17937 length = attr->constant_value (0);
17938 }
17939 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17940 {
17941 /* We don't currently support non-constant byte sizes for strings. */
17942 length = attr->constant_value (1);
17943 }
17944 else
17945 {
17946 /* Use 1 as a fallback length if we have nothing else. */
17947 length = 1;
17948 }
17949
17950 index_type = objfile_type (objfile)->builtin_int;
17951 if (length_is_constant)
17952 range_type = create_static_range_type (NULL, index_type, 1, length);
17953 else
17954 {
17955 struct dynamic_prop low_bound;
17956
17957 low_bound.set_const_val (1);
17958 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17959 }
17960 char_type = language_string_char_type (cu->language_defn, gdbarch);
17961 type = create_string_type (NULL, char_type, range_type);
17962
17963 return set_die_type (die, type, cu);
17964 }
17965
17966 /* Assuming that DIE corresponds to a function, returns nonzero
17967 if the function is prototyped. */
17968
17969 static int
17970 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17971 {
17972 struct attribute *attr;
17973
17974 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17975 if (attr && attr->as_boolean ())
17976 return 1;
17977
17978 /* The DWARF standard implies that the DW_AT_prototyped attribute
17979 is only meaningful for C, but the concept also extends to other
17980 languages that allow unprototyped functions (Eg: Objective C).
17981 For all other languages, assume that functions are always
17982 prototyped. */
17983 if (cu->language != language_c
17984 && cu->language != language_objc
17985 && cu->language != language_opencl)
17986 return 1;
17987
17988 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17989 prototyped and unprototyped functions; default to prototyped,
17990 since that is more common in modern code (and RealView warns
17991 about unprototyped functions). */
17992 if (producer_is_realview (cu->producer))
17993 return 1;
17994
17995 return 0;
17996 }
17997
17998 /* Handle DIES due to C code like:
17999
18000 struct foo
18001 {
18002 int (*funcp)(int a, long l);
18003 int b;
18004 };
18005
18006 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18007
18008 static struct type *
18009 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18010 {
18011 struct objfile *objfile = cu->per_objfile->objfile;
18012 struct type *type; /* Type that this function returns. */
18013 struct type *ftype; /* Function that returns above type. */
18014 struct attribute *attr;
18015
18016 type = die_type (die, cu);
18017
18018 /* The die_type call above may have already set the type for this DIE. */
18019 ftype = get_die_type (die, cu);
18020 if (ftype)
18021 return ftype;
18022
18023 ftype = lookup_function_type (type);
18024
18025 if (prototyped_function_p (die, cu))
18026 ftype->set_is_prototyped (true);
18027
18028 /* Store the calling convention in the type if it's available in
18029 the subroutine die. Otherwise set the calling convention to
18030 the default value DW_CC_normal. */
18031 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18032 if (attr != nullptr
18033 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18034 TYPE_CALLING_CONVENTION (ftype)
18035 = (enum dwarf_calling_convention) attr->constant_value (0);
18036 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18037 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18038 else
18039 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18040
18041 /* Record whether the function returns normally to its caller or not
18042 if the DWARF producer set that information. */
18043 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18044 if (attr && attr->as_boolean ())
18045 TYPE_NO_RETURN (ftype) = 1;
18046
18047 /* We need to add the subroutine type to the die immediately so
18048 we don't infinitely recurse when dealing with parameters
18049 declared as the same subroutine type. */
18050 set_die_type (die, ftype, cu);
18051
18052 if (die->child != NULL)
18053 {
18054 struct type *void_type = objfile_type (objfile)->builtin_void;
18055 struct die_info *child_die;
18056 int nparams, iparams;
18057
18058 /* Count the number of parameters.
18059 FIXME: GDB currently ignores vararg functions, but knows about
18060 vararg member functions. */
18061 nparams = 0;
18062 child_die = die->child;
18063 while (child_die && child_die->tag)
18064 {
18065 if (child_die->tag == DW_TAG_formal_parameter)
18066 nparams++;
18067 else if (child_die->tag == DW_TAG_unspecified_parameters)
18068 ftype->set_has_varargs (true);
18069
18070 child_die = child_die->sibling;
18071 }
18072
18073 /* Allocate storage for parameters and fill them in. */
18074 ftype->set_num_fields (nparams);
18075 ftype->set_fields
18076 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18077
18078 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18079 even if we error out during the parameters reading below. */
18080 for (iparams = 0; iparams < nparams; iparams++)
18081 ftype->field (iparams).set_type (void_type);
18082
18083 iparams = 0;
18084 child_die = die->child;
18085 while (child_die && child_die->tag)
18086 {
18087 if (child_die->tag == DW_TAG_formal_parameter)
18088 {
18089 struct type *arg_type;
18090
18091 /* DWARF version 2 has no clean way to discern C++
18092 static and non-static member functions. G++ helps
18093 GDB by marking the first parameter for non-static
18094 member functions (which is the this pointer) as
18095 artificial. We pass this information to
18096 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18097
18098 DWARF version 3 added DW_AT_object_pointer, which GCC
18099 4.5 does not yet generate. */
18100 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18101 if (attr != nullptr)
18102 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18103 else
18104 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18105 arg_type = die_type (child_die, cu);
18106
18107 /* RealView does not mark THIS as const, which the testsuite
18108 expects. GCC marks THIS as const in method definitions,
18109 but not in the class specifications (GCC PR 43053). */
18110 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18111 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18112 {
18113 int is_this = 0;
18114 struct dwarf2_cu *arg_cu = cu;
18115 const char *name = dwarf2_name (child_die, cu);
18116
18117 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18118 if (attr != nullptr)
18119 {
18120 /* If the compiler emits this, use it. */
18121 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18122 is_this = 1;
18123 }
18124 else if (name && strcmp (name, "this") == 0)
18125 /* Function definitions will have the argument names. */
18126 is_this = 1;
18127 else if (name == NULL && iparams == 0)
18128 /* Declarations may not have the names, so like
18129 elsewhere in GDB, assume an artificial first
18130 argument is "this". */
18131 is_this = 1;
18132
18133 if (is_this)
18134 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18135 arg_type, 0);
18136 }
18137
18138 ftype->field (iparams).set_type (arg_type);
18139 iparams++;
18140 }
18141 child_die = child_die->sibling;
18142 }
18143 }
18144
18145 return ftype;
18146 }
18147
18148 static struct type *
18149 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18150 {
18151 struct objfile *objfile = cu->per_objfile->objfile;
18152 const char *name = NULL;
18153 struct type *this_type, *target_type;
18154
18155 name = dwarf2_full_name (NULL, die, cu);
18156 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18157 this_type->set_target_is_stub (true);
18158 set_die_type (die, this_type, cu);
18159 target_type = die_type (die, cu);
18160 if (target_type != this_type)
18161 TYPE_TARGET_TYPE (this_type) = target_type;
18162 else
18163 {
18164 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18165 spec and cause infinite loops in GDB. */
18166 complaint (_("Self-referential DW_TAG_typedef "
18167 "- DIE at %s [in module %s]"),
18168 sect_offset_str (die->sect_off), objfile_name (objfile));
18169 TYPE_TARGET_TYPE (this_type) = NULL;
18170 }
18171 if (name == NULL)
18172 {
18173 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18174 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18175 Handle these by just returning the target type, rather than
18176 constructing an anonymous typedef type and trying to handle this
18177 elsewhere. */
18178 set_die_type (die, target_type, cu);
18179 return target_type;
18180 }
18181 return this_type;
18182 }
18183
18184 /* Helper for get_dwarf2_rational_constant that computes the value of
18185 a given gmp_mpz given an attribute. */
18186
18187 static void
18188 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18189 {
18190 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18191 location expression that pushes an implicit value. */
18192 if (attr->form == DW_FORM_exprloc)
18193 {
18194 dwarf_block *blk = attr->as_block ();
18195 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18196 {
18197 uint64_t len;
18198 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18199 blk->data + blk->size,
18200 &len);
18201 if (ptr - blk->data + len <= blk->size)
18202 {
18203 mpz_import (value->val, len,
18204 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18205 1, 0, 0, ptr);
18206 return;
18207 }
18208 }
18209
18210 /* On failure set it to 1. */
18211 *value = gdb_mpz (1);
18212 }
18213 else if (attr->form_is_block ())
18214 {
18215 dwarf_block *blk = attr->as_block ();
18216 mpz_import (value->val, blk->size,
18217 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18218 1, 0, 0, blk->data);
18219 }
18220 else
18221 *value = gdb_mpz (attr->constant_value (1));
18222 }
18223
18224 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18225 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18226
18227 If the numerator and/or numerator attribute is missing,
18228 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18229 untouched. */
18230
18231 static void
18232 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18233 gdb_mpz *numerator, gdb_mpz *denominator)
18234 {
18235 struct attribute *num_attr, *denom_attr;
18236
18237 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18238 if (num_attr == nullptr)
18239 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18240 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18241
18242 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18243 if (denom_attr == nullptr)
18244 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18245 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18246
18247 if (num_attr == nullptr || denom_attr == nullptr)
18248 return;
18249
18250 get_mpz (cu, numerator, num_attr);
18251 get_mpz (cu, denominator, denom_attr);
18252 }
18253
18254 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18255 rational constant, rather than a signed one.
18256
18257 If the rational constant has a negative value, a complaint
18258 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18259
18260 static void
18261 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18262 struct dwarf2_cu *cu,
18263 gdb_mpz *numerator,
18264 gdb_mpz *denominator)
18265 {
18266 gdb_mpz num (1);
18267 gdb_mpz denom (1);
18268
18269 get_dwarf2_rational_constant (die, cu, &num, &denom);
18270 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18271 {
18272 mpz_neg (num.val, num.val);
18273 mpz_neg (denom.val, denom.val);
18274 }
18275 else if (mpz_sgn (num.val) == -1)
18276 {
18277 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18278 " in DIE at %s"),
18279 sect_offset_str (die->sect_off));
18280 return;
18281 }
18282 else if (mpz_sgn (denom.val) == -1)
18283 {
18284 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18285 " in DIE at %s"),
18286 sect_offset_str (die->sect_off));
18287 return;
18288 }
18289
18290 *numerator = std::move (num);
18291 *denominator = std::move (denom);
18292 }
18293
18294 /* Assuming that ENCODING is a string whose contents starting at the
18295 K'th character is "_nn" where "nn" is a decimal number, scan that
18296 number and set RESULT to the value. K is updated to point to the
18297 character immediately following the number.
18298
18299 If the string does not conform to the format described above, false
18300 is returned, and K may or may not be changed. */
18301
18302 static bool
18303 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18304 {
18305 /* The next character should be an underscore ('_') followed
18306 by a digit. */
18307 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18308 return false;
18309
18310 /* Skip the underscore. */
18311 k++;
18312 int start = k;
18313
18314 /* Determine the number of digits for our number. */
18315 while (isdigit (encoding[k]))
18316 k++;
18317 if (k == start)
18318 return false;
18319
18320 std::string copy (&encoding[start], k - start);
18321 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18322 return false;
18323
18324 return true;
18325 }
18326
18327 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18328 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18329 DENOM, update OFFSET, and return true on success. Return false on
18330 failure. */
18331
18332 static bool
18333 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18334 gdb_mpz *num, gdb_mpz *denom)
18335 {
18336 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18337 return false;
18338 return ada_get_gnat_encoded_number (encoding, offset, denom);
18339 }
18340
18341 /* Assuming DIE corresponds to a fixed point type, finish the creation
18342 of the corresponding TYPE by setting its type-specific data. CU is
18343 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18344 encodings. It is nullptr if the GNAT encoding should be
18345 ignored. */
18346
18347 static void
18348 finish_fixed_point_type (struct type *type, const char *suffix,
18349 struct die_info *die, struct dwarf2_cu *cu)
18350 {
18351 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18352 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18353
18354 /* If GNAT encodings are preferred, don't examine the
18355 attributes. */
18356 struct attribute *attr = nullptr;
18357 if (suffix == nullptr)
18358 {
18359 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18360 if (attr == nullptr)
18361 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18362 if (attr == nullptr)
18363 attr = dwarf2_attr (die, DW_AT_small, cu);
18364 }
18365
18366 /* Numerator and denominator of our fixed-point type's scaling factor.
18367 The default is a scaling factor of 1, which we use as a fallback
18368 when we are not able to decode it (problem with the debugging info,
18369 unsupported forms, bug in GDB, etc...). Using that as the default
18370 allows us to at least print the unscaled value, which might still
18371 be useful to a user. */
18372 gdb_mpz scale_num (1);
18373 gdb_mpz scale_denom (1);
18374
18375 if (attr == nullptr)
18376 {
18377 int offset = 0;
18378 if (suffix != nullptr
18379 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18380 &scale_denom)
18381 /* The number might be encoded as _nn_dd_nn_dd, where the
18382 second ratio is the 'small value. In this situation, we
18383 want the second value. */
18384 && (suffix[offset] != '_'
18385 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18386 &scale_denom)))
18387 {
18388 /* Found it. */
18389 }
18390 else
18391 {
18392 /* Scaling factor not found. Assume a scaling factor of 1,
18393 and hope for the best. At least the user will be able to
18394 see the encoded value. */
18395 scale_num = 1;
18396 scale_denom = 1;
18397 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18398 sect_offset_str (die->sect_off));
18399 }
18400 }
18401 else if (attr->name == DW_AT_binary_scale)
18402 {
18403 LONGEST scale_exp = attr->constant_value (0);
18404 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18405
18406 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18407 }
18408 else if (attr->name == DW_AT_decimal_scale)
18409 {
18410 LONGEST scale_exp = attr->constant_value (0);
18411 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18412
18413 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18414 }
18415 else if (attr->name == DW_AT_small)
18416 {
18417 struct die_info *scale_die;
18418 struct dwarf2_cu *scale_cu = cu;
18419
18420 scale_die = follow_die_ref (die, attr, &scale_cu);
18421 if (scale_die->tag == DW_TAG_constant)
18422 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18423 &scale_num, &scale_denom);
18424 else
18425 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18426 " (DIE at %s)"),
18427 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18428 }
18429 else
18430 {
18431 complaint (_("unsupported scale attribute %s for fixed-point type"
18432 " (DIE at %s)"),
18433 dwarf_attr_name (attr->name),
18434 sect_offset_str (die->sect_off));
18435 }
18436
18437 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18438 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18439 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18440 mpq_canonicalize (scaling_factor.val);
18441 }
18442
18443 /* The gnat-encoding suffix for fixed point. */
18444
18445 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18446
18447 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18448 "XF" suffix of the name. The text after this is what encodes the
18449 'small and 'delta information. Otherwise, return nullptr. */
18450
18451 static const char *
18452 gnat_encoded_fixed_point_type_info (const char *name)
18453 {
18454 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18455 }
18456
18457 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18458 (which may be different from NAME) to the architecture back-end to allow
18459 it to guess the correct format if necessary. */
18460
18461 static struct type *
18462 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18463 const char *name_hint, enum bfd_endian byte_order)
18464 {
18465 struct gdbarch *gdbarch = objfile->arch ();
18466 const struct floatformat **format;
18467 struct type *type;
18468
18469 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18470 if (format)
18471 type = init_float_type (objfile, bits, name, format, byte_order);
18472 else
18473 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18474
18475 return type;
18476 }
18477
18478 /* Allocate an integer type of size BITS and name NAME. */
18479
18480 static struct type *
18481 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18482 int bits, int unsigned_p, const char *name)
18483 {
18484 struct type *type;
18485
18486 /* Versions of Intel's C Compiler generate an integer type called "void"
18487 instead of using DW_TAG_unspecified_type. This has been seen on
18488 at least versions 14, 17, and 18. */
18489 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18490 && strcmp (name, "void") == 0)
18491 type = objfile_type (objfile)->builtin_void;
18492 else
18493 type = init_integer_type (objfile, bits, unsigned_p, name);
18494
18495 return type;
18496 }
18497
18498 /* Return true if DIE has a DW_AT_small attribute whose value is
18499 a constant rational, where both the numerator and denominator
18500 are equal to zero.
18501
18502 CU is the DIE's Compilation Unit. */
18503
18504 static bool
18505 has_zero_over_zero_small_attribute (struct die_info *die,
18506 struct dwarf2_cu *cu)
18507 {
18508 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18509 if (attr == nullptr)
18510 return false;
18511
18512 struct dwarf2_cu *scale_cu = cu;
18513 struct die_info *scale_die
18514 = follow_die_ref (die, attr, &scale_cu);
18515
18516 if (scale_die->tag != DW_TAG_constant)
18517 return false;
18518
18519 gdb_mpz num (1), denom (1);
18520 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18521 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18522 }
18523
18524 /* Initialise and return a floating point type of size BITS suitable for
18525 use as a component of a complex number. The NAME_HINT is passed through
18526 when initialising the floating point type and is the name of the complex
18527 type.
18528
18529 As DWARF doesn't currently provide an explicit name for the components
18530 of a complex number, but it can be helpful to have these components
18531 named, we try to select a suitable name based on the size of the
18532 component. */
18533 static struct type *
18534 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18535 struct objfile *objfile,
18536 int bits, const char *name_hint,
18537 enum bfd_endian byte_order)
18538 {
18539 gdbarch *gdbarch = objfile->arch ();
18540 struct type *tt = nullptr;
18541
18542 /* Try to find a suitable floating point builtin type of size BITS.
18543 We're going to use the name of this type as the name for the complex
18544 target type that we are about to create. */
18545 switch (cu->language)
18546 {
18547 case language_fortran:
18548 switch (bits)
18549 {
18550 case 32:
18551 tt = builtin_f_type (gdbarch)->builtin_real;
18552 break;
18553 case 64:
18554 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18555 break;
18556 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18557 case 128:
18558 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18559 break;
18560 }
18561 break;
18562 default:
18563 switch (bits)
18564 {
18565 case 32:
18566 tt = builtin_type (gdbarch)->builtin_float;
18567 break;
18568 case 64:
18569 tt = builtin_type (gdbarch)->builtin_double;
18570 break;
18571 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18572 case 128:
18573 tt = builtin_type (gdbarch)->builtin_long_double;
18574 break;
18575 }
18576 break;
18577 }
18578
18579 /* If the type we found doesn't match the size we were looking for, then
18580 pretend we didn't find a type at all, the complex target type we
18581 create will then be nameless. */
18582 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18583 tt = nullptr;
18584
18585 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18586 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18587 }
18588
18589 /* Find a representation of a given base type and install
18590 it in the TYPE field of the die. */
18591
18592 static struct type *
18593 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18594 {
18595 struct objfile *objfile = cu->per_objfile->objfile;
18596 struct type *type;
18597 struct attribute *attr;
18598 int encoding = 0, bits = 0;
18599 const char *name;
18600 gdbarch *arch;
18601
18602 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18603 if (attr != nullptr && attr->form_is_constant ())
18604 encoding = attr->constant_value (0);
18605 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18606 if (attr != nullptr)
18607 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18608 name = dwarf2_name (die, cu);
18609 if (!name)
18610 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18611
18612 arch = objfile->arch ();
18613 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18614
18615 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18616 if (attr != nullptr && attr->form_is_constant ())
18617 {
18618 int endianity = attr->constant_value (0);
18619
18620 switch (endianity)
18621 {
18622 case DW_END_big:
18623 byte_order = BFD_ENDIAN_BIG;
18624 break;
18625 case DW_END_little:
18626 byte_order = BFD_ENDIAN_LITTLE;
18627 break;
18628 default:
18629 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18630 break;
18631 }
18632 }
18633
18634 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18635 && cu->language == language_ada
18636 && has_zero_over_zero_small_attribute (die, cu))
18637 {
18638 /* brobecker/2018-02-24: This is a fixed point type for which
18639 the scaling factor is represented as fraction whose value
18640 does not make sense (zero divided by zero), so we should
18641 normally never see these. However, there is a small category
18642 of fixed point types for which GNAT is unable to provide
18643 the scaling factor via the standard DWARF mechanisms, and
18644 for which the info is provided via the GNAT encodings instead.
18645 This is likely what this DIE is about. */
18646 encoding = (encoding == DW_ATE_signed_fixed
18647 ? DW_ATE_signed
18648 : DW_ATE_unsigned);
18649 }
18650
18651 /* With GNAT encodings, fixed-point information will be encoded in
18652 the type name. Note that this can also occur with the above
18653 zero-over-zero case, which is why this is a separate "if" rather
18654 than an "else if". */
18655 const char *gnat_encoding_suffix = nullptr;
18656 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18657 && cu->language == language_ada
18658 && name != nullptr)
18659 {
18660 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18661 if (gnat_encoding_suffix != nullptr)
18662 {
18663 gdb_assert (startswith (gnat_encoding_suffix,
18664 GNAT_FIXED_POINT_SUFFIX));
18665 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18666 name, gnat_encoding_suffix - name);
18667 /* Use -1 here so that SUFFIX points at the "_" after the
18668 "XF". */
18669 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18670
18671 encoding = (encoding == DW_ATE_signed
18672 ? DW_ATE_signed_fixed
18673 : DW_ATE_unsigned_fixed);
18674 }
18675 }
18676
18677 switch (encoding)
18678 {
18679 case DW_ATE_address:
18680 /* Turn DW_ATE_address into a void * pointer. */
18681 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18682 type = init_pointer_type (objfile, bits, name, type);
18683 break;
18684 case DW_ATE_boolean:
18685 type = init_boolean_type (objfile, bits, 1, name);
18686 break;
18687 case DW_ATE_complex_float:
18688 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18689 byte_order);
18690 if (type->code () == TYPE_CODE_ERROR)
18691 {
18692 if (name == nullptr)
18693 {
18694 struct obstack *obstack
18695 = &cu->per_objfile->objfile->objfile_obstack;
18696 name = obconcat (obstack, "_Complex ", type->name (),
18697 nullptr);
18698 }
18699 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18700 }
18701 else
18702 type = init_complex_type (name, type);
18703 break;
18704 case DW_ATE_decimal_float:
18705 type = init_decfloat_type (objfile, bits, name);
18706 break;
18707 case DW_ATE_float:
18708 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18709 break;
18710 case DW_ATE_signed:
18711 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18712 break;
18713 case DW_ATE_unsigned:
18714 if (cu->language == language_fortran
18715 && name
18716 && startswith (name, "character("))
18717 type = init_character_type (objfile, bits, 1, name);
18718 else
18719 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18720 break;
18721 case DW_ATE_signed_char:
18722 if (cu->language == language_ada || cu->language == language_m2
18723 || cu->language == language_pascal
18724 || cu->language == language_fortran)
18725 type = init_character_type (objfile, bits, 0, name);
18726 else
18727 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18728 break;
18729 case DW_ATE_unsigned_char:
18730 if (cu->language == language_ada || cu->language == language_m2
18731 || cu->language == language_pascal
18732 || cu->language == language_fortran
18733 || cu->language == language_rust)
18734 type = init_character_type (objfile, bits, 1, name);
18735 else
18736 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18737 break;
18738 case DW_ATE_UTF:
18739 {
18740 if (bits == 16)
18741 type = builtin_type (arch)->builtin_char16;
18742 else if (bits == 32)
18743 type = builtin_type (arch)->builtin_char32;
18744 else
18745 {
18746 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18747 bits);
18748 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18749 }
18750 return set_die_type (die, type, cu);
18751 }
18752 break;
18753 case DW_ATE_signed_fixed:
18754 type = init_fixed_point_type (objfile, bits, 0, name);
18755 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18756 break;
18757 case DW_ATE_unsigned_fixed:
18758 type = init_fixed_point_type (objfile, bits, 1, name);
18759 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18760 break;
18761
18762 default:
18763 complaint (_("unsupported DW_AT_encoding: '%s'"),
18764 dwarf_type_encoding_name (encoding));
18765 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18766 break;
18767 }
18768
18769 if (name && strcmp (name, "char") == 0)
18770 type->set_has_no_signedness (true);
18771
18772 maybe_set_alignment (cu, die, type);
18773
18774 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18775
18776 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18777 {
18778 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18779 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18780 {
18781 unsigned real_bit_size = attr->as_unsigned ();
18782 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18783 /* Only use the attributes if they make sense together. */
18784 if (attr == nullptr
18785 || (attr->as_unsigned () + real_bit_size
18786 <= 8 * TYPE_LENGTH (type)))
18787 {
18788 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18789 = real_bit_size;
18790 if (attr != nullptr)
18791 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18792 = attr->as_unsigned ();
18793 }
18794 }
18795 }
18796
18797 return set_die_type (die, type, cu);
18798 }
18799
18800 /* Parse dwarf attribute if it's a block, reference or constant and put the
18801 resulting value of the attribute into struct bound_prop.
18802 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18803
18804 static int
18805 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18806 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18807 struct type *default_type)
18808 {
18809 struct dwarf2_property_baton *baton;
18810 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18811 struct objfile *objfile = per_objfile->objfile;
18812 struct obstack *obstack = &objfile->objfile_obstack;
18813
18814 gdb_assert (default_type != NULL);
18815
18816 if (attr == NULL || prop == NULL)
18817 return 0;
18818
18819 if (attr->form_is_block ())
18820 {
18821 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18822 baton->property_type = default_type;
18823 baton->locexpr.per_cu = cu->per_cu;
18824 baton->locexpr.per_objfile = per_objfile;
18825
18826 struct dwarf_block *block = attr->as_block ();
18827 baton->locexpr.size = block->size;
18828 baton->locexpr.data = block->data;
18829 switch (attr->name)
18830 {
18831 case DW_AT_string_length:
18832 baton->locexpr.is_reference = true;
18833 break;
18834 default:
18835 baton->locexpr.is_reference = false;
18836 break;
18837 }
18838
18839 prop->set_locexpr (baton);
18840 gdb_assert (prop->baton () != NULL);
18841 }
18842 else if (attr->form_is_ref ())
18843 {
18844 struct dwarf2_cu *target_cu = cu;
18845 struct die_info *target_die;
18846 struct attribute *target_attr;
18847
18848 target_die = follow_die_ref (die, attr, &target_cu);
18849 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18850 if (target_attr == NULL)
18851 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18852 target_cu);
18853 if (target_attr == NULL)
18854 return 0;
18855
18856 switch (target_attr->name)
18857 {
18858 case DW_AT_location:
18859 if (target_attr->form_is_section_offset ())
18860 {
18861 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18862 baton->property_type = die_type (target_die, target_cu);
18863 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18864 prop->set_loclist (baton);
18865 gdb_assert (prop->baton () != NULL);
18866 }
18867 else if (target_attr->form_is_block ())
18868 {
18869 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18870 baton->property_type = die_type (target_die, target_cu);
18871 baton->locexpr.per_cu = cu->per_cu;
18872 baton->locexpr.per_objfile = per_objfile;
18873 struct dwarf_block *block = target_attr->as_block ();
18874 baton->locexpr.size = block->size;
18875 baton->locexpr.data = block->data;
18876 baton->locexpr.is_reference = true;
18877 prop->set_locexpr (baton);
18878 gdb_assert (prop->baton () != NULL);
18879 }
18880 else
18881 {
18882 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18883 "dynamic property");
18884 return 0;
18885 }
18886 break;
18887 case DW_AT_data_member_location:
18888 {
18889 LONGEST offset;
18890
18891 if (!handle_data_member_location (target_die, target_cu,
18892 &offset))
18893 return 0;
18894
18895 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18896 baton->property_type = read_type_die (target_die->parent,
18897 target_cu);
18898 baton->offset_info.offset = offset;
18899 baton->offset_info.type = die_type (target_die, target_cu);
18900 prop->set_addr_offset (baton);
18901 break;
18902 }
18903 }
18904 }
18905 else if (attr->form_is_constant ())
18906 prop->set_const_val (attr->constant_value (0));
18907 else
18908 {
18909 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18910 dwarf2_name (die, cu));
18911 return 0;
18912 }
18913
18914 return 1;
18915 }
18916
18917 /* See read.h. */
18918
18919 struct type *
18920 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18921 {
18922 struct type *int_type;
18923
18924 /* Helper macro to examine the various builtin types. */
18925 #define TRY_TYPE(F) \
18926 int_type = (unsigned_p \
18927 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18928 : objfile_type (objfile)->builtin_ ## F); \
18929 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18930 return int_type
18931
18932 TRY_TYPE (char);
18933 TRY_TYPE (short);
18934 TRY_TYPE (int);
18935 TRY_TYPE (long);
18936 TRY_TYPE (long_long);
18937
18938 #undef TRY_TYPE
18939
18940 gdb_assert_not_reached ("unable to find suitable integer type");
18941 }
18942
18943 /* See read.h. */
18944
18945 struct type *
18946 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18947 {
18948 int addr_size = this->per_cu->addr_size ();
18949 return this->per_objfile->int_type (addr_size, unsigned_p);
18950 }
18951
18952 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18953 present (which is valid) then compute the default type based on the
18954 compilation units address size. */
18955
18956 static struct type *
18957 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18958 {
18959 struct type *index_type = die_type (die, cu);
18960
18961 /* Dwarf-2 specifications explicitly allows to create subrange types
18962 without specifying a base type.
18963 In that case, the base type must be set to the type of
18964 the lower bound, upper bound or count, in that order, if any of these
18965 three attributes references an object that has a type.
18966 If no base type is found, the Dwarf-2 specifications say that
18967 a signed integer type of size equal to the size of an address should
18968 be used.
18969 For the following C code: `extern char gdb_int [];'
18970 GCC produces an empty range DIE.
18971 FIXME: muller/2010-05-28: Possible references to object for low bound,
18972 high bound or count are not yet handled by this code. */
18973 if (index_type->code () == TYPE_CODE_VOID)
18974 index_type = cu->addr_sized_int_type (false);
18975
18976 return index_type;
18977 }
18978
18979 /* Read the given DW_AT_subrange DIE. */
18980
18981 static struct type *
18982 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18983 {
18984 struct type *base_type, *orig_base_type;
18985 struct type *range_type;
18986 struct attribute *attr;
18987 struct dynamic_prop low, high;
18988 int low_default_is_valid;
18989 int high_bound_is_count = 0;
18990 const char *name;
18991 ULONGEST negative_mask;
18992
18993 orig_base_type = read_subrange_index_type (die, cu);
18994
18995 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18996 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18997 creating the range type, but we use the result of check_typedef
18998 when examining properties of the type. */
18999 base_type = check_typedef (orig_base_type);
19000
19001 /* The die_type call above may have already set the type for this DIE. */
19002 range_type = get_die_type (die, cu);
19003 if (range_type)
19004 return range_type;
19005
19006 high.set_const_val (0);
19007
19008 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19009 omitting DW_AT_lower_bound. */
19010 switch (cu->language)
19011 {
19012 case language_c:
19013 case language_cplus:
19014 low.set_const_val (0);
19015 low_default_is_valid = 1;
19016 break;
19017 case language_fortran:
19018 low.set_const_val (1);
19019 low_default_is_valid = 1;
19020 break;
19021 case language_d:
19022 case language_objc:
19023 case language_rust:
19024 low.set_const_val (0);
19025 low_default_is_valid = (cu->header.version >= 4);
19026 break;
19027 case language_ada:
19028 case language_m2:
19029 case language_pascal:
19030 low.set_const_val (1);
19031 low_default_is_valid = (cu->header.version >= 4);
19032 break;
19033 default:
19034 low.set_const_val (0);
19035 low_default_is_valid = 0;
19036 break;
19037 }
19038
19039 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
19040 if (attr != nullptr)
19041 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
19042 else if (!low_default_is_valid)
19043 complaint (_("Missing DW_AT_lower_bound "
19044 "- DIE at %s [in module %s]"),
19045 sect_offset_str (die->sect_off),
19046 objfile_name (cu->per_objfile->objfile));
19047
19048 struct attribute *attr_ub, *attr_count;
19049 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
19050 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19051 {
19052 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
19053 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19054 {
19055 /* If bounds are constant do the final calculation here. */
19056 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19057 high.set_const_val (low.const_val () + high.const_val () - 1);
19058 else
19059 high_bound_is_count = 1;
19060 }
19061 else
19062 {
19063 if (attr_ub != NULL)
19064 complaint (_("Unresolved DW_AT_upper_bound "
19065 "- DIE at %s [in module %s]"),
19066 sect_offset_str (die->sect_off),
19067 objfile_name (cu->per_objfile->objfile));
19068 if (attr_count != NULL)
19069 complaint (_("Unresolved DW_AT_count "
19070 "- DIE at %s [in module %s]"),
19071 sect_offset_str (die->sect_off),
19072 objfile_name (cu->per_objfile->objfile));
19073 }
19074 }
19075
19076 LONGEST bias = 0;
19077 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
19078 if (bias_attr != nullptr && bias_attr->form_is_constant ())
19079 bias = bias_attr->constant_value (0);
19080
19081 /* Normally, the DWARF producers are expected to use a signed
19082 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19083 But this is unfortunately not always the case, as witnessed
19084 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19085 is used instead. To work around that ambiguity, we treat
19086 the bounds as signed, and thus sign-extend their values, when
19087 the base type is signed. */
19088 negative_mask =
19089 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19090 if (low.kind () == PROP_CONST
19091 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19092 low.set_const_val (low.const_val () | negative_mask);
19093 if (high.kind () == PROP_CONST
19094 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19095 high.set_const_val (high.const_val () | negative_mask);
19096
19097 /* Check for bit and byte strides. */
19098 struct dynamic_prop byte_stride_prop;
19099 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19100 if (attr_byte_stride != nullptr)
19101 {
19102 struct type *prop_type = cu->addr_sized_int_type (false);
19103 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19104 prop_type);
19105 }
19106
19107 struct dynamic_prop bit_stride_prop;
19108 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19109 if (attr_bit_stride != nullptr)
19110 {
19111 /* It only makes sense to have either a bit or byte stride. */
19112 if (attr_byte_stride != nullptr)
19113 {
19114 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19115 "- DIE at %s [in module %s]"),
19116 sect_offset_str (die->sect_off),
19117 objfile_name (cu->per_objfile->objfile));
19118 attr_bit_stride = nullptr;
19119 }
19120 else
19121 {
19122 struct type *prop_type = cu->addr_sized_int_type (false);
19123 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19124 prop_type);
19125 }
19126 }
19127
19128 if (attr_byte_stride != nullptr
19129 || attr_bit_stride != nullptr)
19130 {
19131 bool byte_stride_p = (attr_byte_stride != nullptr);
19132 struct dynamic_prop *stride
19133 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19134
19135 range_type
19136 = create_range_type_with_stride (NULL, orig_base_type, &low,
19137 &high, bias, stride, byte_stride_p);
19138 }
19139 else
19140 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19141
19142 if (high_bound_is_count)
19143 range_type->bounds ()->flag_upper_bound_is_count = 1;
19144
19145 /* Ada expects an empty array on no boundary attributes. */
19146 if (attr == NULL && cu->language != language_ada)
19147 range_type->bounds ()->high.set_undefined ();
19148
19149 name = dwarf2_name (die, cu);
19150 if (name)
19151 range_type->set_name (name);
19152
19153 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19154 if (attr != nullptr)
19155 TYPE_LENGTH (range_type) = attr->constant_value (0);
19156
19157 maybe_set_alignment (cu, die, range_type);
19158
19159 set_die_type (die, range_type, cu);
19160
19161 /* set_die_type should be already done. */
19162 set_descriptive_type (range_type, die, cu);
19163
19164 return range_type;
19165 }
19166
19167 static struct type *
19168 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19169 {
19170 struct type *type;
19171
19172 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19173 type->set_name (dwarf2_name (die, cu));
19174
19175 /* In Ada, an unspecified type is typically used when the description
19176 of the type is deferred to a different unit. When encountering
19177 such a type, we treat it as a stub, and try to resolve it later on,
19178 when needed. */
19179 if (cu->language == language_ada)
19180 type->set_is_stub (true);
19181
19182 return set_die_type (die, type, cu);
19183 }
19184
19185 /* Read a single die and all its descendents. Set the die's sibling
19186 field to NULL; set other fields in the die correctly, and set all
19187 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19188 location of the info_ptr after reading all of those dies. PARENT
19189 is the parent of the die in question. */
19190
19191 static struct die_info *
19192 read_die_and_children (const struct die_reader_specs *reader,
19193 const gdb_byte *info_ptr,
19194 const gdb_byte **new_info_ptr,
19195 struct die_info *parent)
19196 {
19197 struct die_info *die;
19198 const gdb_byte *cur_ptr;
19199
19200 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19201 if (die == NULL)
19202 {
19203 *new_info_ptr = cur_ptr;
19204 return NULL;
19205 }
19206 store_in_ref_table (die, reader->cu);
19207
19208 if (die->has_children)
19209 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19210 else
19211 {
19212 die->child = NULL;
19213 *new_info_ptr = cur_ptr;
19214 }
19215
19216 die->sibling = NULL;
19217 die->parent = parent;
19218 return die;
19219 }
19220
19221 /* Read a die, all of its descendents, and all of its siblings; set
19222 all of the fields of all of the dies correctly. Arguments are as
19223 in read_die_and_children. */
19224
19225 static struct die_info *
19226 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19227 const gdb_byte *info_ptr,
19228 const gdb_byte **new_info_ptr,
19229 struct die_info *parent)
19230 {
19231 struct die_info *first_die, *last_sibling;
19232 const gdb_byte *cur_ptr;
19233
19234 cur_ptr = info_ptr;
19235 first_die = last_sibling = NULL;
19236
19237 while (1)
19238 {
19239 struct die_info *die
19240 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19241
19242 if (die == NULL)
19243 {
19244 *new_info_ptr = cur_ptr;
19245 return first_die;
19246 }
19247
19248 if (!first_die)
19249 first_die = die;
19250 else
19251 last_sibling->sibling = die;
19252
19253 last_sibling = die;
19254 }
19255 }
19256
19257 /* Read a die, all of its descendents, and all of its siblings; set
19258 all of the fields of all of the dies correctly. Arguments are as
19259 in read_die_and_children.
19260 This the main entry point for reading a DIE and all its children. */
19261
19262 static struct die_info *
19263 read_die_and_siblings (const struct die_reader_specs *reader,
19264 const gdb_byte *info_ptr,
19265 const gdb_byte **new_info_ptr,
19266 struct die_info *parent)
19267 {
19268 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19269 new_info_ptr, parent);
19270
19271 if (dwarf_die_debug)
19272 {
19273 fprintf_unfiltered (gdb_stdlog,
19274 "Read die from %s@0x%x of %s:\n",
19275 reader->die_section->get_name (),
19276 (unsigned) (info_ptr - reader->die_section->buffer),
19277 bfd_get_filename (reader->abfd));
19278 dump_die (die, dwarf_die_debug);
19279 }
19280
19281 return die;
19282 }
19283
19284 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19285 attributes.
19286 The caller is responsible for filling in the extra attributes
19287 and updating (*DIEP)->num_attrs.
19288 Set DIEP to point to a newly allocated die with its information,
19289 except for its child, sibling, and parent fields. */
19290
19291 static const gdb_byte *
19292 read_full_die_1 (const struct die_reader_specs *reader,
19293 struct die_info **diep, const gdb_byte *info_ptr,
19294 int num_extra_attrs)
19295 {
19296 unsigned int abbrev_number, bytes_read, i;
19297 const struct abbrev_info *abbrev;
19298 struct die_info *die;
19299 struct dwarf2_cu *cu = reader->cu;
19300 bfd *abfd = reader->abfd;
19301
19302 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19303 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19304 info_ptr += bytes_read;
19305 if (!abbrev_number)
19306 {
19307 *diep = NULL;
19308 return info_ptr;
19309 }
19310
19311 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19312 if (!abbrev)
19313 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19314 abbrev_number,
19315 bfd_get_filename (abfd));
19316
19317 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19318 die->sect_off = sect_off;
19319 die->tag = abbrev->tag;
19320 die->abbrev = abbrev_number;
19321 die->has_children = abbrev->has_children;
19322
19323 /* Make the result usable.
19324 The caller needs to update num_attrs after adding the extra
19325 attributes. */
19326 die->num_attrs = abbrev->num_attrs;
19327
19328 bool any_need_reprocess = false;
19329 for (i = 0; i < abbrev->num_attrs; ++i)
19330 {
19331 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19332 info_ptr);
19333 if (die->attrs[i].requires_reprocessing_p ())
19334 any_need_reprocess = true;
19335 }
19336
19337 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19338 if (attr != nullptr && attr->form_is_unsigned ())
19339 cu->str_offsets_base = attr->as_unsigned ();
19340
19341 attr = die->attr (DW_AT_loclists_base);
19342 if (attr != nullptr)
19343 cu->loclist_base = attr->as_unsigned ();
19344
19345 auto maybe_addr_base = die->addr_base ();
19346 if (maybe_addr_base.has_value ())
19347 cu->addr_base = *maybe_addr_base;
19348
19349 attr = die->attr (DW_AT_rnglists_base);
19350 if (attr != nullptr)
19351 cu->rnglists_base = attr->as_unsigned ();
19352
19353 if (any_need_reprocess)
19354 {
19355 for (i = 0; i < abbrev->num_attrs; ++i)
19356 {
19357 if (die->attrs[i].requires_reprocessing_p ())
19358 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19359 }
19360 }
19361 *diep = die;
19362 return info_ptr;
19363 }
19364
19365 /* Read a die and all its attributes.
19366 Set DIEP to point to a newly allocated die with its information,
19367 except for its child, sibling, and parent fields. */
19368
19369 static const gdb_byte *
19370 read_full_die (const struct die_reader_specs *reader,
19371 struct die_info **diep, const gdb_byte *info_ptr)
19372 {
19373 const gdb_byte *result;
19374
19375 result = read_full_die_1 (reader, diep, info_ptr, 0);
19376
19377 if (dwarf_die_debug)
19378 {
19379 fprintf_unfiltered (gdb_stdlog,
19380 "Read die from %s@0x%x of %s:\n",
19381 reader->die_section->get_name (),
19382 (unsigned) (info_ptr - reader->die_section->buffer),
19383 bfd_get_filename (reader->abfd));
19384 dump_die (*diep, dwarf_die_debug);
19385 }
19386
19387 return result;
19388 }
19389 \f
19390
19391 /* Returns nonzero if TAG represents a type that we might generate a partial
19392 symbol for. */
19393
19394 static int
19395 is_type_tag_for_partial (int tag, enum language lang)
19396 {
19397 switch (tag)
19398 {
19399 #if 0
19400 /* Some types that would be reasonable to generate partial symbols for,
19401 that we don't at present. Note that normally this does not
19402 matter, mainly because C compilers don't give names to these
19403 types, but instead emit DW_TAG_typedef. */
19404 case DW_TAG_file_type:
19405 case DW_TAG_ptr_to_member_type:
19406 case DW_TAG_set_type:
19407 case DW_TAG_string_type:
19408 case DW_TAG_subroutine_type:
19409 #endif
19410
19411 /* GNAT may emit an array with a name, but no typedef, so we
19412 need to make a symbol in this case. */
19413 case DW_TAG_array_type:
19414 return lang == language_ada;
19415
19416 case DW_TAG_base_type:
19417 case DW_TAG_class_type:
19418 case DW_TAG_interface_type:
19419 case DW_TAG_enumeration_type:
19420 case DW_TAG_structure_type:
19421 case DW_TAG_subrange_type:
19422 case DW_TAG_typedef:
19423 case DW_TAG_union_type:
19424 return 1;
19425 default:
19426 return 0;
19427 }
19428 }
19429
19430 /* Load all DIEs that are interesting for partial symbols into memory. */
19431
19432 static struct partial_die_info *
19433 load_partial_dies (const struct die_reader_specs *reader,
19434 const gdb_byte *info_ptr, int building_psymtab)
19435 {
19436 struct dwarf2_cu *cu = reader->cu;
19437 struct objfile *objfile = cu->per_objfile->objfile;
19438 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19439 unsigned int bytes_read;
19440 unsigned int load_all = 0;
19441 int nesting_level = 1;
19442
19443 parent_die = NULL;
19444 last_die = NULL;
19445
19446 gdb_assert (cu->per_cu != NULL);
19447 if (cu->per_cu->load_all_dies)
19448 load_all = 1;
19449
19450 cu->partial_dies
19451 = htab_create_alloc_ex (cu->header.length / 12,
19452 partial_die_hash,
19453 partial_die_eq,
19454 NULL,
19455 &cu->comp_unit_obstack,
19456 hashtab_obstack_allocate,
19457 dummy_obstack_deallocate);
19458
19459 while (1)
19460 {
19461 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19462 &bytes_read);
19463
19464 /* A NULL abbrev means the end of a series of children. */
19465 if (abbrev == NULL)
19466 {
19467 if (--nesting_level == 0)
19468 return first_die;
19469
19470 info_ptr += bytes_read;
19471 last_die = parent_die;
19472 parent_die = parent_die->die_parent;
19473 continue;
19474 }
19475
19476 /* Check for template arguments. We never save these; if
19477 they're seen, we just mark the parent, and go on our way. */
19478 if (parent_die != NULL
19479 && cu->language == language_cplus
19480 && (abbrev->tag == DW_TAG_template_type_param
19481 || abbrev->tag == DW_TAG_template_value_param))
19482 {
19483 parent_die->has_template_arguments = 1;
19484
19485 if (!load_all)
19486 {
19487 /* We don't need a partial DIE for the template argument. */
19488 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19489 continue;
19490 }
19491 }
19492
19493 /* We only recurse into c++ subprograms looking for template arguments.
19494 Skip their other children. */
19495 if (!load_all
19496 && cu->language == language_cplus
19497 && parent_die != NULL
19498 && parent_die->tag == DW_TAG_subprogram
19499 && abbrev->tag != DW_TAG_inlined_subroutine)
19500 {
19501 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19502 continue;
19503 }
19504
19505 /* Check whether this DIE is interesting enough to save. Normally
19506 we would not be interested in members here, but there may be
19507 later variables referencing them via DW_AT_specification (for
19508 static members). */
19509 if (!load_all
19510 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19511 && abbrev->tag != DW_TAG_constant
19512 && abbrev->tag != DW_TAG_enumerator
19513 && abbrev->tag != DW_TAG_subprogram
19514 && abbrev->tag != DW_TAG_inlined_subroutine
19515 && abbrev->tag != DW_TAG_lexical_block
19516 && abbrev->tag != DW_TAG_variable
19517 && abbrev->tag != DW_TAG_namespace
19518 && abbrev->tag != DW_TAG_module
19519 && abbrev->tag != DW_TAG_member
19520 && abbrev->tag != DW_TAG_imported_unit
19521 && abbrev->tag != DW_TAG_imported_declaration)
19522 {
19523 /* Otherwise we skip to the next sibling, if any. */
19524 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19525 continue;
19526 }
19527
19528 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19529 abbrev);
19530
19531 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19532
19533 /* This two-pass algorithm for processing partial symbols has a
19534 high cost in cache pressure. Thus, handle some simple cases
19535 here which cover the majority of C partial symbols. DIEs
19536 which neither have specification tags in them, nor could have
19537 specification tags elsewhere pointing at them, can simply be
19538 processed and discarded.
19539
19540 This segment is also optional; scan_partial_symbols and
19541 add_partial_symbol will handle these DIEs if we chain
19542 them in normally. When compilers which do not emit large
19543 quantities of duplicate debug information are more common,
19544 this code can probably be removed. */
19545
19546 /* Any complete simple types at the top level (pretty much all
19547 of them, for a language without namespaces), can be processed
19548 directly. */
19549 if (parent_die == NULL
19550 && pdi.has_specification == 0
19551 && pdi.is_declaration == 0
19552 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19553 || pdi.tag == DW_TAG_base_type
19554 || pdi.tag == DW_TAG_array_type
19555 || pdi.tag == DW_TAG_subrange_type))
19556 {
19557 if (building_psymtab && pdi.raw_name != NULL)
19558 add_partial_symbol (&pdi, cu);
19559
19560 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19561 continue;
19562 }
19563
19564 /* The exception for DW_TAG_typedef with has_children above is
19565 a workaround of GCC PR debug/47510. In the case of this complaint
19566 type_name_or_error will error on such types later.
19567
19568 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19569 it could not find the child DIEs referenced later, this is checked
19570 above. In correct DWARF DW_TAG_typedef should have no children. */
19571
19572 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19573 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19574 "- DIE at %s [in module %s]"),
19575 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19576
19577 /* If we're at the second level, and we're an enumerator, and
19578 our parent has no specification (meaning possibly lives in a
19579 namespace elsewhere), then we can add the partial symbol now
19580 instead of queueing it. */
19581 if (pdi.tag == DW_TAG_enumerator
19582 && parent_die != NULL
19583 && parent_die->die_parent == NULL
19584 && parent_die->tag == DW_TAG_enumeration_type
19585 && parent_die->has_specification == 0)
19586 {
19587 if (pdi.raw_name == NULL)
19588 complaint (_("malformed enumerator DIE ignored"));
19589 else if (building_psymtab)
19590 add_partial_symbol (&pdi, cu);
19591
19592 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19593 continue;
19594 }
19595
19596 struct partial_die_info *part_die
19597 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19598
19599 /* We'll save this DIE so link it in. */
19600 part_die->die_parent = parent_die;
19601 part_die->die_sibling = NULL;
19602 part_die->die_child = NULL;
19603
19604 if (last_die && last_die == parent_die)
19605 last_die->die_child = part_die;
19606 else if (last_die)
19607 last_die->die_sibling = part_die;
19608
19609 last_die = part_die;
19610
19611 if (first_die == NULL)
19612 first_die = part_die;
19613
19614 /* Maybe add the DIE to the hash table. Not all DIEs that we
19615 find interesting need to be in the hash table, because we
19616 also have the parent/sibling/child chains; only those that we
19617 might refer to by offset later during partial symbol reading.
19618
19619 For now this means things that might have be the target of a
19620 DW_AT_specification, DW_AT_abstract_origin, or
19621 DW_AT_extension. DW_AT_extension will refer only to
19622 namespaces; DW_AT_abstract_origin refers to functions (and
19623 many things under the function DIE, but we do not recurse
19624 into function DIEs during partial symbol reading) and
19625 possibly variables as well; DW_AT_specification refers to
19626 declarations. Declarations ought to have the DW_AT_declaration
19627 flag. It happens that GCC forgets to put it in sometimes, but
19628 only for functions, not for types.
19629
19630 Adding more things than necessary to the hash table is harmless
19631 except for the performance cost. Adding too few will result in
19632 wasted time in find_partial_die, when we reread the compilation
19633 unit with load_all_dies set. */
19634
19635 if (load_all
19636 || abbrev->tag == DW_TAG_constant
19637 || abbrev->tag == DW_TAG_subprogram
19638 || abbrev->tag == DW_TAG_variable
19639 || abbrev->tag == DW_TAG_namespace
19640 || part_die->is_declaration)
19641 {
19642 void **slot;
19643
19644 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19645 to_underlying (part_die->sect_off),
19646 INSERT);
19647 *slot = part_die;
19648 }
19649
19650 /* For some DIEs we want to follow their children (if any). For C
19651 we have no reason to follow the children of structures; for other
19652 languages we have to, so that we can get at method physnames
19653 to infer fully qualified class names, for DW_AT_specification,
19654 and for C++ template arguments. For C++, we also look one level
19655 inside functions to find template arguments (if the name of the
19656 function does not already contain the template arguments).
19657
19658 For Ada and Fortran, we need to scan the children of subprograms
19659 and lexical blocks as well because these languages allow the
19660 definition of nested entities that could be interesting for the
19661 debugger, such as nested subprograms for instance. */
19662 if (last_die->has_children
19663 && (load_all
19664 || last_die->tag == DW_TAG_namespace
19665 || last_die->tag == DW_TAG_module
19666 || last_die->tag == DW_TAG_enumeration_type
19667 || (cu->language == language_cplus
19668 && last_die->tag == DW_TAG_subprogram
19669 && (last_die->raw_name == NULL
19670 || strchr (last_die->raw_name, '<') == NULL))
19671 || (cu->language != language_c
19672 && (last_die->tag == DW_TAG_class_type
19673 || last_die->tag == DW_TAG_interface_type
19674 || last_die->tag == DW_TAG_structure_type
19675 || last_die->tag == DW_TAG_union_type))
19676 || ((cu->language == language_ada
19677 || cu->language == language_fortran)
19678 && (last_die->tag == DW_TAG_subprogram
19679 || last_die->tag == DW_TAG_lexical_block))))
19680 {
19681 nesting_level++;
19682 parent_die = last_die;
19683 continue;
19684 }
19685
19686 /* Otherwise we skip to the next sibling, if any. */
19687 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19688
19689 /* Back to the top, do it again. */
19690 }
19691 }
19692
19693 partial_die_info::partial_die_info (sect_offset sect_off_,
19694 const struct abbrev_info *abbrev)
19695 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19696 {
19697 }
19698
19699 /* See class definition. */
19700
19701 const char *
19702 partial_die_info::name (dwarf2_cu *cu)
19703 {
19704 if (!canonical_name && raw_name != nullptr)
19705 {
19706 struct objfile *objfile = cu->per_objfile->objfile;
19707 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19708 canonical_name = 1;
19709 }
19710
19711 return raw_name;
19712 }
19713
19714 /* Read a minimal amount of information into the minimal die structure.
19715 INFO_PTR should point just after the initial uleb128 of a DIE. */
19716
19717 const gdb_byte *
19718 partial_die_info::read (const struct die_reader_specs *reader,
19719 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19720 {
19721 struct dwarf2_cu *cu = reader->cu;
19722 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19723 unsigned int i;
19724 int has_low_pc_attr = 0;
19725 int has_high_pc_attr = 0;
19726 int high_pc_relative = 0;
19727
19728 for (i = 0; i < abbrev.num_attrs; ++i)
19729 {
19730 attribute attr;
19731 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19732 /* String and address offsets that need to do the reprocessing have
19733 already been read at this point, so there is no need to wait until
19734 the loop terminates to do the reprocessing. */
19735 if (attr.requires_reprocessing_p ())
19736 read_attribute_reprocess (reader, &attr, tag);
19737 /* Store the data if it is of an attribute we want to keep in a
19738 partial symbol table. */
19739 switch (attr.name)
19740 {
19741 case DW_AT_name:
19742 switch (tag)
19743 {
19744 case DW_TAG_compile_unit:
19745 case DW_TAG_partial_unit:
19746 case DW_TAG_type_unit:
19747 /* Compilation units have a DW_AT_name that is a filename, not
19748 a source language identifier. */
19749 case DW_TAG_enumeration_type:
19750 case DW_TAG_enumerator:
19751 /* These tags always have simple identifiers already; no need
19752 to canonicalize them. */
19753 canonical_name = 1;
19754 raw_name = attr.as_string ();
19755 break;
19756 default:
19757 canonical_name = 0;
19758 raw_name = attr.as_string ();
19759 break;
19760 }
19761 break;
19762 case DW_AT_linkage_name:
19763 case DW_AT_MIPS_linkage_name:
19764 /* Note that both forms of linkage name might appear. We
19765 assume they will be the same, and we only store the last
19766 one we see. */
19767 linkage_name = attr.as_string ();
19768 break;
19769 case DW_AT_low_pc:
19770 has_low_pc_attr = 1;
19771 lowpc = attr.as_address ();
19772 break;
19773 case DW_AT_high_pc:
19774 has_high_pc_attr = 1;
19775 highpc = attr.as_address ();
19776 if (cu->header.version >= 4 && attr.form_is_constant ())
19777 high_pc_relative = 1;
19778 break;
19779 case DW_AT_location:
19780 /* Support the .debug_loc offsets. */
19781 if (attr.form_is_block ())
19782 {
19783 d.locdesc = attr.as_block ();
19784 }
19785 else if (attr.form_is_section_offset ())
19786 {
19787 dwarf2_complex_location_expr_complaint ();
19788 }
19789 else
19790 {
19791 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19792 "partial symbol information");
19793 }
19794 break;
19795 case DW_AT_external:
19796 is_external = attr.as_boolean ();
19797 break;
19798 case DW_AT_declaration:
19799 is_declaration = attr.as_boolean ();
19800 break;
19801 case DW_AT_type:
19802 has_type = 1;
19803 break;
19804 case DW_AT_abstract_origin:
19805 case DW_AT_specification:
19806 case DW_AT_extension:
19807 has_specification = 1;
19808 spec_offset = attr.get_ref_die_offset ();
19809 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19810 || cu->per_cu->is_dwz);
19811 break;
19812 case DW_AT_sibling:
19813 /* Ignore absolute siblings, they might point outside of
19814 the current compile unit. */
19815 if (attr.form == DW_FORM_ref_addr)
19816 complaint (_("ignoring absolute DW_AT_sibling"));
19817 else
19818 {
19819 const gdb_byte *buffer = reader->buffer;
19820 sect_offset off = attr.get_ref_die_offset ();
19821 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19822
19823 if (sibling_ptr < info_ptr)
19824 complaint (_("DW_AT_sibling points backwards"));
19825 else if (sibling_ptr > reader->buffer_end)
19826 reader->die_section->overflow_complaint ();
19827 else
19828 sibling = sibling_ptr;
19829 }
19830 break;
19831 case DW_AT_byte_size:
19832 has_byte_size = 1;
19833 break;
19834 case DW_AT_const_value:
19835 has_const_value = 1;
19836 break;
19837 case DW_AT_calling_convention:
19838 /* DWARF doesn't provide a way to identify a program's source-level
19839 entry point. DW_AT_calling_convention attributes are only meant
19840 to describe functions' calling conventions.
19841
19842 However, because it's a necessary piece of information in
19843 Fortran, and before DWARF 4 DW_CC_program was the only
19844 piece of debugging information whose definition refers to
19845 a 'main program' at all, several compilers marked Fortran
19846 main programs with DW_CC_program --- even when those
19847 functions use the standard calling conventions.
19848
19849 Although DWARF now specifies a way to provide this
19850 information, we support this practice for backward
19851 compatibility. */
19852 if (attr.constant_value (0) == DW_CC_program
19853 && cu->language == language_fortran)
19854 main_subprogram = 1;
19855 break;
19856 case DW_AT_inline:
19857 {
19858 LONGEST value = attr.constant_value (-1);
19859 if (value == DW_INL_inlined
19860 || value == DW_INL_declared_inlined)
19861 may_be_inlined = 1;
19862 }
19863 break;
19864
19865 case DW_AT_import:
19866 if (tag == DW_TAG_imported_unit)
19867 {
19868 d.sect_off = attr.get_ref_die_offset ();
19869 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19870 || cu->per_cu->is_dwz);
19871 }
19872 break;
19873
19874 case DW_AT_main_subprogram:
19875 main_subprogram = attr.as_boolean ();
19876 break;
19877
19878 case DW_AT_ranges:
19879 {
19880 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19881 on DWARF version). */
19882 ULONGEST ranges_offset = attr.as_unsigned ();
19883
19884 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19885 this value. */
19886 if (tag != DW_TAG_compile_unit)
19887 ranges_offset += cu->gnu_ranges_base;
19888
19889 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19890 nullptr, tag))
19891 has_pc_info = 1;
19892 }
19893 break;
19894
19895 default:
19896 break;
19897 }
19898 }
19899
19900 /* For Ada, if both the name and the linkage name appear, we prefer
19901 the latter. This lets "catch exception" work better, regardless
19902 of the order in which the name and linkage name were emitted.
19903 Really, though, this is just a workaround for the fact that gdb
19904 doesn't store both the name and the linkage name. */
19905 if (cu->language == language_ada && linkage_name != nullptr)
19906 raw_name = linkage_name;
19907
19908 if (high_pc_relative)
19909 highpc += lowpc;
19910
19911 if (has_low_pc_attr && has_high_pc_attr)
19912 {
19913 /* When using the GNU linker, .gnu.linkonce. sections are used to
19914 eliminate duplicate copies of functions and vtables and such.
19915 The linker will arbitrarily choose one and discard the others.
19916 The AT_*_pc values for such functions refer to local labels in
19917 these sections. If the section from that file was discarded, the
19918 labels are not in the output, so the relocs get a value of 0.
19919 If this is a discarded function, mark the pc bounds as invalid,
19920 so that GDB will ignore it. */
19921 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19922 {
19923 struct objfile *objfile = per_objfile->objfile;
19924 struct gdbarch *gdbarch = objfile->arch ();
19925
19926 complaint (_("DW_AT_low_pc %s is zero "
19927 "for DIE at %s [in module %s]"),
19928 paddress (gdbarch, lowpc),
19929 sect_offset_str (sect_off),
19930 objfile_name (objfile));
19931 }
19932 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19933 else if (lowpc >= highpc)
19934 {
19935 struct objfile *objfile = per_objfile->objfile;
19936 struct gdbarch *gdbarch = objfile->arch ();
19937
19938 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19939 "for DIE at %s [in module %s]"),
19940 paddress (gdbarch, lowpc),
19941 paddress (gdbarch, highpc),
19942 sect_offset_str (sect_off),
19943 objfile_name (objfile));
19944 }
19945 else
19946 has_pc_info = 1;
19947 }
19948
19949 return info_ptr;
19950 }
19951
19952 /* Find a cached partial DIE at OFFSET in CU. */
19953
19954 struct partial_die_info *
19955 dwarf2_cu::find_partial_die (sect_offset sect_off)
19956 {
19957 struct partial_die_info *lookup_die = NULL;
19958 struct partial_die_info part_die (sect_off);
19959
19960 lookup_die = ((struct partial_die_info *)
19961 htab_find_with_hash (partial_dies, &part_die,
19962 to_underlying (sect_off)));
19963
19964 return lookup_die;
19965 }
19966
19967 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19968 except in the case of .debug_types DIEs which do not reference
19969 outside their CU (they do however referencing other types via
19970 DW_FORM_ref_sig8). */
19971
19972 static const struct cu_partial_die_info
19973 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19974 {
19975 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19976 struct objfile *objfile = per_objfile->objfile;
19977 struct partial_die_info *pd = NULL;
19978
19979 if (offset_in_dwz == cu->per_cu->is_dwz
19980 && cu->header.offset_in_cu_p (sect_off))
19981 {
19982 pd = cu->find_partial_die (sect_off);
19983 if (pd != NULL)
19984 return { cu, pd };
19985 /* We missed recording what we needed.
19986 Load all dies and try again. */
19987 }
19988 else
19989 {
19990 /* TUs don't reference other CUs/TUs (except via type signatures). */
19991 if (cu->per_cu->is_debug_types)
19992 {
19993 error (_("Dwarf Error: Type Unit at offset %s contains"
19994 " external reference to offset %s [in module %s].\n"),
19995 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19996 bfd_get_filename (objfile->obfd));
19997 }
19998 dwarf2_per_cu_data *per_cu
19999 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20000 per_objfile);
20001
20002 cu = per_objfile->get_cu (per_cu);
20003 if (cu == NULL || cu->partial_dies == NULL)
20004 load_partial_comp_unit (per_cu, per_objfile, nullptr);
20005
20006 cu = per_objfile->get_cu (per_cu);
20007
20008 cu->last_used = 0;
20009 pd = cu->find_partial_die (sect_off);
20010 }
20011
20012 /* If we didn't find it, and not all dies have been loaded,
20013 load them all and try again. */
20014
20015 if (pd == NULL && cu->per_cu->load_all_dies == 0)
20016 {
20017 cu->per_cu->load_all_dies = 1;
20018
20019 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20020 THIS_CU->cu may already be in use. So we can't just free it and
20021 replace its DIEs with the ones we read in. Instead, we leave those
20022 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20023 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20024 set. */
20025 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
20026
20027 pd = cu->find_partial_die (sect_off);
20028 }
20029
20030 if (pd == NULL)
20031 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
20032 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
20033 return { cu, pd };
20034 }
20035
20036 /* See if we can figure out if the class lives in a namespace. We do
20037 this by looking for a member function; its demangled name will
20038 contain namespace info, if there is any. */
20039
20040 static void
20041 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20042 struct dwarf2_cu *cu)
20043 {
20044 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20045 what template types look like, because the demangler
20046 frequently doesn't give the same name as the debug info. We
20047 could fix this by only using the demangled name to get the
20048 prefix (but see comment in read_structure_type). */
20049
20050 struct partial_die_info *real_pdi;
20051 struct partial_die_info *child_pdi;
20052
20053 /* If this DIE (this DIE's specification, if any) has a parent, then
20054 we should not do this. We'll prepend the parent's fully qualified
20055 name when we create the partial symbol. */
20056
20057 real_pdi = struct_pdi;
20058 while (real_pdi->has_specification)
20059 {
20060 auto res = find_partial_die (real_pdi->spec_offset,
20061 real_pdi->spec_is_dwz, cu);
20062 real_pdi = res.pdi;
20063 cu = res.cu;
20064 }
20065
20066 if (real_pdi->die_parent != NULL)
20067 return;
20068
20069 for (child_pdi = struct_pdi->die_child;
20070 child_pdi != NULL;
20071 child_pdi = child_pdi->die_sibling)
20072 {
20073 if (child_pdi->tag == DW_TAG_subprogram
20074 && child_pdi->linkage_name != NULL)
20075 {
20076 gdb::unique_xmalloc_ptr<char> actual_class_name
20077 (cu->language_defn->class_name_from_physname
20078 (child_pdi->linkage_name));
20079 if (actual_class_name != NULL)
20080 {
20081 struct objfile *objfile = cu->per_objfile->objfile;
20082 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20083 struct_pdi->canonical_name = 1;
20084 }
20085 break;
20086 }
20087 }
20088 }
20089
20090 /* Return true if a DIE with TAG may have the DW_AT_const_value
20091 attribute. */
20092
20093 static bool
20094 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20095 {
20096 switch (tag)
20097 {
20098 case DW_TAG_constant:
20099 case DW_TAG_enumerator:
20100 case DW_TAG_formal_parameter:
20101 case DW_TAG_template_value_param:
20102 case DW_TAG_variable:
20103 return true;
20104 }
20105
20106 return false;
20107 }
20108
20109 void
20110 partial_die_info::fixup (struct dwarf2_cu *cu)
20111 {
20112 /* Once we've fixed up a die, there's no point in doing so again.
20113 This also avoids a memory leak if we were to call
20114 guess_partial_die_structure_name multiple times. */
20115 if (fixup_called)
20116 return;
20117
20118 /* If we found a reference attribute and the DIE has no name, try
20119 to find a name in the referred to DIE. */
20120
20121 if (raw_name == NULL && has_specification)
20122 {
20123 struct partial_die_info *spec_die;
20124
20125 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20126 spec_die = res.pdi;
20127 cu = res.cu;
20128
20129 spec_die->fixup (cu);
20130
20131 if (spec_die->raw_name)
20132 {
20133 raw_name = spec_die->raw_name;
20134 canonical_name = spec_die->canonical_name;
20135
20136 /* Copy DW_AT_external attribute if it is set. */
20137 if (spec_die->is_external)
20138 is_external = spec_die->is_external;
20139 }
20140 }
20141
20142 if (!has_const_value && has_specification
20143 && can_have_DW_AT_const_value_p (tag))
20144 {
20145 struct partial_die_info *spec_die;
20146
20147 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20148 spec_die = res.pdi;
20149 cu = res.cu;
20150
20151 spec_die->fixup (cu);
20152
20153 if (spec_die->has_const_value)
20154 {
20155 /* Copy DW_AT_const_value attribute if it is set. */
20156 has_const_value = spec_die->has_const_value;
20157 }
20158 }
20159
20160 /* Set default names for some unnamed DIEs. */
20161
20162 if (raw_name == NULL && tag == DW_TAG_namespace)
20163 {
20164 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20165 canonical_name = 1;
20166 }
20167
20168 /* If there is no parent die to provide a namespace, and there are
20169 children, see if we can determine the namespace from their linkage
20170 name. */
20171 if (cu->language == language_cplus
20172 && !cu->per_objfile->per_bfd->types.empty ()
20173 && die_parent == NULL
20174 && has_children
20175 && (tag == DW_TAG_class_type
20176 || tag == DW_TAG_structure_type
20177 || tag == DW_TAG_union_type))
20178 guess_partial_die_structure_name (this, cu);
20179
20180 /* GCC might emit a nameless struct or union that has a linkage
20181 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20182 if (raw_name == NULL
20183 && (tag == DW_TAG_class_type
20184 || tag == DW_TAG_interface_type
20185 || tag == DW_TAG_structure_type
20186 || tag == DW_TAG_union_type)
20187 && linkage_name != NULL)
20188 {
20189 gdb::unique_xmalloc_ptr<char> demangled
20190 (gdb_demangle (linkage_name, DMGL_TYPES));
20191 if (demangled != nullptr)
20192 {
20193 const char *base;
20194
20195 /* Strip any leading namespaces/classes, keep only the base name.
20196 DW_AT_name for named DIEs does not contain the prefixes. */
20197 base = strrchr (demangled.get (), ':');
20198 if (base && base > demangled.get () && base[-1] == ':')
20199 base++;
20200 else
20201 base = demangled.get ();
20202
20203 struct objfile *objfile = cu->per_objfile->objfile;
20204 raw_name = objfile->intern (base);
20205 canonical_name = 1;
20206 }
20207 }
20208
20209 fixup_called = 1;
20210 }
20211
20212 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20213 contents from the given SECTION in the HEADER.
20214
20215 HEADER_OFFSET is the offset of the header in the section. */
20216 static void
20217 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20218 struct dwarf2_section_info *section,
20219 sect_offset header_offset)
20220 {
20221 unsigned int bytes_read;
20222 bfd *abfd = section->get_bfd_owner ();
20223 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20224
20225 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20226 info_ptr += bytes_read;
20227
20228 header->version = read_2_bytes (abfd, info_ptr);
20229 info_ptr += 2;
20230
20231 header->addr_size = read_1_byte (abfd, info_ptr);
20232 info_ptr += 1;
20233
20234 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20235 info_ptr += 1;
20236
20237 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20238 }
20239
20240 /* Return the DW_AT_loclists_base value for the CU. */
20241 static ULONGEST
20242 lookup_loclist_base (struct dwarf2_cu *cu)
20243 {
20244 /* For the .dwo unit, the loclist_base points to the first offset following
20245 the header. The header consists of the following entities-
20246 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20247 bit format)
20248 2. version (2 bytes)
20249 3. address size (1 byte)
20250 4. segment selector size (1 byte)
20251 5. offset entry count (4 bytes)
20252 These sizes are derived as per the DWARFv5 standard. */
20253 if (cu->dwo_unit != nullptr)
20254 {
20255 if (cu->header.initial_length_size == 4)
20256 return LOCLIST_HEADER_SIZE32;
20257 return LOCLIST_HEADER_SIZE64;
20258 }
20259 return cu->loclist_base;
20260 }
20261
20262 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20263 array of offsets in the .debug_loclists section. */
20264
20265 static sect_offset
20266 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20267 {
20268 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20269 struct objfile *objfile = per_objfile->objfile;
20270 bfd *abfd = objfile->obfd;
20271 ULONGEST loclist_header_size =
20272 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20273 : LOCLIST_HEADER_SIZE64);
20274 ULONGEST loclist_base = lookup_loclist_base (cu);
20275
20276 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20277 ULONGEST start_offset =
20278 loclist_base + loclist_index * cu->header.offset_size;
20279
20280 /* Get loclists section. */
20281 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20282
20283 /* Read the loclists section content. */
20284 section->read (objfile);
20285 if (section->buffer == NULL)
20286 error (_("DW_FORM_loclistx used without .debug_loclists "
20287 "section [in module %s]"), objfile_name (objfile));
20288
20289 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20290 so if loclist_base is smaller than the header size, we have a problem. */
20291 if (loclist_base < loclist_header_size)
20292 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20293 objfile_name (objfile));
20294
20295 /* Read the header of the loclists contribution. */
20296 struct loclists_rnglists_header header;
20297 read_loclists_rnglists_header (&header, section,
20298 (sect_offset) (loclist_base - loclist_header_size));
20299
20300 /* Verify the loclist index is valid. */
20301 if (loclist_index >= header.offset_entry_count)
20302 error (_("DW_FORM_loclistx pointing outside of "
20303 ".debug_loclists offset array [in module %s]"),
20304 objfile_name (objfile));
20305
20306 /* Validate that reading won't go beyond the end of the section. */
20307 if (start_offset + cu->header.offset_size > section->size)
20308 error (_("Reading DW_FORM_loclistx index beyond end of"
20309 ".debug_loclists section [in module %s]"),
20310 objfile_name (objfile));
20311
20312 const gdb_byte *info_ptr = section->buffer + start_offset;
20313
20314 if (cu->header.offset_size == 4)
20315 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20316 else
20317 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20318 }
20319
20320 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20321 array of offsets in the .debug_rnglists section. */
20322
20323 static sect_offset
20324 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20325 dwarf_tag tag)
20326 {
20327 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20328 struct objfile *objfile = dwarf2_per_objfile->objfile;
20329 bfd *abfd = objfile->obfd;
20330 ULONGEST rnglist_header_size =
20331 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20332 : RNGLIST_HEADER_SIZE64);
20333
20334 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20335 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20336 doesn't apply. */
20337 ULONGEST rnglist_base =
20338 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20339
20340 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20341 ULONGEST start_offset =
20342 rnglist_base + rnglist_index * cu->header.offset_size;
20343
20344 /* Get rnglists section. */
20345 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20346
20347 /* Read the rnglists section content. */
20348 section->read (objfile);
20349 if (section->buffer == nullptr)
20350 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20351 "[in module %s]"),
20352 objfile_name (objfile));
20353
20354 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20355 so if rnglist_base is smaller than the header size, we have a problem. */
20356 if (rnglist_base < rnglist_header_size)
20357 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20358 objfile_name (objfile));
20359
20360 /* Read the header of the rnglists contribution. */
20361 struct loclists_rnglists_header header;
20362 read_loclists_rnglists_header (&header, section,
20363 (sect_offset) (rnglist_base - rnglist_header_size));
20364
20365 /* Verify the rnglist index is valid. */
20366 if (rnglist_index >= header.offset_entry_count)
20367 error (_("DW_FORM_rnglistx index pointing outside of "
20368 ".debug_rnglists offset array [in module %s]"),
20369 objfile_name (objfile));
20370
20371 /* Validate that reading won't go beyond the end of the section. */
20372 if (start_offset + cu->header.offset_size > section->size)
20373 error (_("Reading DW_FORM_rnglistx index beyond end of"
20374 ".debug_rnglists section [in module %s]"),
20375 objfile_name (objfile));
20376
20377 const gdb_byte *info_ptr = section->buffer + start_offset;
20378
20379 if (cu->header.offset_size == 4)
20380 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20381 else
20382 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20383 }
20384
20385 /* Process the attributes that had to be skipped in the first round. These
20386 attributes are the ones that need str_offsets_base or addr_base attributes.
20387 They could not have been processed in the first round, because at the time
20388 the values of str_offsets_base or addr_base may not have been known. */
20389 static void
20390 read_attribute_reprocess (const struct die_reader_specs *reader,
20391 struct attribute *attr, dwarf_tag tag)
20392 {
20393 struct dwarf2_cu *cu = reader->cu;
20394 switch (attr->form)
20395 {
20396 case DW_FORM_addrx:
20397 case DW_FORM_GNU_addr_index:
20398 attr->set_address (read_addr_index (cu,
20399 attr->as_unsigned_reprocess ()));
20400 break;
20401 case DW_FORM_loclistx:
20402 {
20403 sect_offset loclists_sect_off
20404 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20405
20406 attr->set_unsigned (to_underlying (loclists_sect_off));
20407 }
20408 break;
20409 case DW_FORM_rnglistx:
20410 {
20411 sect_offset rnglists_sect_off
20412 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20413
20414 attr->set_unsigned (to_underlying (rnglists_sect_off));
20415 }
20416 break;
20417 case DW_FORM_strx:
20418 case DW_FORM_strx1:
20419 case DW_FORM_strx2:
20420 case DW_FORM_strx3:
20421 case DW_FORM_strx4:
20422 case DW_FORM_GNU_str_index:
20423 {
20424 unsigned int str_index = attr->as_unsigned_reprocess ();
20425 gdb_assert (!attr->canonical_string_p ());
20426 if (reader->dwo_file != NULL)
20427 attr->set_string_noncanonical (read_dwo_str_index (reader,
20428 str_index));
20429 else
20430 attr->set_string_noncanonical (read_stub_str_index (cu,
20431 str_index));
20432 break;
20433 }
20434 default:
20435 gdb_assert_not_reached (_("Unexpected DWARF form."));
20436 }
20437 }
20438
20439 /* Read an attribute value described by an attribute form. */
20440
20441 static const gdb_byte *
20442 read_attribute_value (const struct die_reader_specs *reader,
20443 struct attribute *attr, unsigned form,
20444 LONGEST implicit_const, const gdb_byte *info_ptr)
20445 {
20446 struct dwarf2_cu *cu = reader->cu;
20447 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20448 struct objfile *objfile = per_objfile->objfile;
20449 bfd *abfd = reader->abfd;
20450 struct comp_unit_head *cu_header = &cu->header;
20451 unsigned int bytes_read;
20452 struct dwarf_block *blk;
20453
20454 attr->form = (enum dwarf_form) form;
20455 switch (form)
20456 {
20457 case DW_FORM_ref_addr:
20458 if (cu_header->version == 2)
20459 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20460 &bytes_read));
20461 else
20462 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20463 &bytes_read));
20464 info_ptr += bytes_read;
20465 break;
20466 case DW_FORM_GNU_ref_alt:
20467 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20468 &bytes_read));
20469 info_ptr += bytes_read;
20470 break;
20471 case DW_FORM_addr:
20472 {
20473 struct gdbarch *gdbarch = objfile->arch ();
20474 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20475 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20476 attr->set_address (addr);
20477 info_ptr += bytes_read;
20478 }
20479 break;
20480 case DW_FORM_block2:
20481 blk = dwarf_alloc_block (cu);
20482 blk->size = read_2_bytes (abfd, info_ptr);
20483 info_ptr += 2;
20484 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20485 info_ptr += blk->size;
20486 attr->set_block (blk);
20487 break;
20488 case DW_FORM_block4:
20489 blk = dwarf_alloc_block (cu);
20490 blk->size = read_4_bytes (abfd, info_ptr);
20491 info_ptr += 4;
20492 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20493 info_ptr += blk->size;
20494 attr->set_block (blk);
20495 break;
20496 case DW_FORM_data2:
20497 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20498 info_ptr += 2;
20499 break;
20500 case DW_FORM_data4:
20501 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20502 info_ptr += 4;
20503 break;
20504 case DW_FORM_data8:
20505 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20506 info_ptr += 8;
20507 break;
20508 case DW_FORM_data16:
20509 blk = dwarf_alloc_block (cu);
20510 blk->size = 16;
20511 blk->data = read_n_bytes (abfd, info_ptr, 16);
20512 info_ptr += 16;
20513 attr->set_block (blk);
20514 break;
20515 case DW_FORM_sec_offset:
20516 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20517 &bytes_read));
20518 info_ptr += bytes_read;
20519 break;
20520 case DW_FORM_loclistx:
20521 {
20522 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20523 &bytes_read));
20524 info_ptr += bytes_read;
20525 }
20526 break;
20527 case DW_FORM_string:
20528 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20529 &bytes_read));
20530 info_ptr += bytes_read;
20531 break;
20532 case DW_FORM_strp:
20533 if (!cu->per_cu->is_dwz)
20534 {
20535 attr->set_string_noncanonical
20536 (read_indirect_string (per_objfile,
20537 abfd, info_ptr, cu_header,
20538 &bytes_read));
20539 info_ptr += bytes_read;
20540 break;
20541 }
20542 /* FALLTHROUGH */
20543 case DW_FORM_line_strp:
20544 if (!cu->per_cu->is_dwz)
20545 {
20546 attr->set_string_noncanonical
20547 (per_objfile->read_line_string (info_ptr, cu_header,
20548 &bytes_read));
20549 info_ptr += bytes_read;
20550 break;
20551 }
20552 /* FALLTHROUGH */
20553 case DW_FORM_GNU_strp_alt:
20554 {
20555 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20556 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20557 &bytes_read);
20558
20559 attr->set_string_noncanonical
20560 (dwz->read_string (objfile, str_offset));
20561 info_ptr += bytes_read;
20562 }
20563 break;
20564 case DW_FORM_exprloc:
20565 case DW_FORM_block:
20566 blk = dwarf_alloc_block (cu);
20567 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20568 info_ptr += bytes_read;
20569 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20570 info_ptr += blk->size;
20571 attr->set_block (blk);
20572 break;
20573 case DW_FORM_block1:
20574 blk = dwarf_alloc_block (cu);
20575 blk->size = read_1_byte (abfd, info_ptr);
20576 info_ptr += 1;
20577 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20578 info_ptr += blk->size;
20579 attr->set_block (blk);
20580 break;
20581 case DW_FORM_data1:
20582 case DW_FORM_flag:
20583 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20584 info_ptr += 1;
20585 break;
20586 case DW_FORM_flag_present:
20587 attr->set_unsigned (1);
20588 break;
20589 case DW_FORM_sdata:
20590 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20591 info_ptr += bytes_read;
20592 break;
20593 case DW_FORM_rnglistx:
20594 {
20595 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20596 &bytes_read));
20597 info_ptr += bytes_read;
20598 }
20599 break;
20600 case DW_FORM_udata:
20601 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20602 info_ptr += bytes_read;
20603 break;
20604 case DW_FORM_ref1:
20605 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20606 + read_1_byte (abfd, info_ptr)));
20607 info_ptr += 1;
20608 break;
20609 case DW_FORM_ref2:
20610 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20611 + read_2_bytes (abfd, info_ptr)));
20612 info_ptr += 2;
20613 break;
20614 case DW_FORM_ref4:
20615 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20616 + read_4_bytes (abfd, info_ptr)));
20617 info_ptr += 4;
20618 break;
20619 case DW_FORM_ref8:
20620 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20621 + read_8_bytes (abfd, info_ptr)));
20622 info_ptr += 8;
20623 break;
20624 case DW_FORM_ref_sig8:
20625 attr->set_signature (read_8_bytes (abfd, info_ptr));
20626 info_ptr += 8;
20627 break;
20628 case DW_FORM_ref_udata:
20629 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20630 + read_unsigned_leb128 (abfd, info_ptr,
20631 &bytes_read)));
20632 info_ptr += bytes_read;
20633 break;
20634 case DW_FORM_indirect:
20635 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20636 info_ptr += bytes_read;
20637 if (form == DW_FORM_implicit_const)
20638 {
20639 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20640 info_ptr += bytes_read;
20641 }
20642 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20643 info_ptr);
20644 break;
20645 case DW_FORM_implicit_const:
20646 attr->set_signed (implicit_const);
20647 break;
20648 case DW_FORM_addrx:
20649 case DW_FORM_GNU_addr_index:
20650 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20651 &bytes_read));
20652 info_ptr += bytes_read;
20653 break;
20654 case DW_FORM_strx:
20655 case DW_FORM_strx1:
20656 case DW_FORM_strx2:
20657 case DW_FORM_strx3:
20658 case DW_FORM_strx4:
20659 case DW_FORM_GNU_str_index:
20660 {
20661 ULONGEST str_index;
20662 if (form == DW_FORM_strx1)
20663 {
20664 str_index = read_1_byte (abfd, info_ptr);
20665 info_ptr += 1;
20666 }
20667 else if (form == DW_FORM_strx2)
20668 {
20669 str_index = read_2_bytes (abfd, info_ptr);
20670 info_ptr += 2;
20671 }
20672 else if (form == DW_FORM_strx3)
20673 {
20674 str_index = read_3_bytes (abfd, info_ptr);
20675 info_ptr += 3;
20676 }
20677 else if (form == DW_FORM_strx4)
20678 {
20679 str_index = read_4_bytes (abfd, info_ptr);
20680 info_ptr += 4;
20681 }
20682 else
20683 {
20684 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20685 info_ptr += bytes_read;
20686 }
20687 attr->set_unsigned_reprocess (str_index);
20688 }
20689 break;
20690 default:
20691 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20692 dwarf_form_name (form),
20693 bfd_get_filename (abfd));
20694 }
20695
20696 /* Super hack. */
20697 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20698 attr->form = DW_FORM_GNU_ref_alt;
20699
20700 /* We have seen instances where the compiler tried to emit a byte
20701 size attribute of -1 which ended up being encoded as an unsigned
20702 0xffffffff. Although 0xffffffff is technically a valid size value,
20703 an object of this size seems pretty unlikely so we can relatively
20704 safely treat these cases as if the size attribute was invalid and
20705 treat them as zero by default. */
20706 if (attr->name == DW_AT_byte_size
20707 && form == DW_FORM_data4
20708 && attr->as_unsigned () >= 0xffffffff)
20709 {
20710 complaint
20711 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20712 hex_string (attr->as_unsigned ()));
20713 attr->set_unsigned (0);
20714 }
20715
20716 return info_ptr;
20717 }
20718
20719 /* Read an attribute described by an abbreviated attribute. */
20720
20721 static const gdb_byte *
20722 read_attribute (const struct die_reader_specs *reader,
20723 struct attribute *attr, const struct attr_abbrev *abbrev,
20724 const gdb_byte *info_ptr)
20725 {
20726 attr->name = abbrev->name;
20727 attr->string_is_canonical = 0;
20728 attr->requires_reprocessing = 0;
20729 return read_attribute_value (reader, attr, abbrev->form,
20730 abbrev->implicit_const, info_ptr);
20731 }
20732
20733 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20734
20735 static const char *
20736 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20737 LONGEST str_offset)
20738 {
20739 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20740 str_offset, "DW_FORM_strp");
20741 }
20742
20743 /* Return pointer to string at .debug_str offset as read from BUF.
20744 BUF is assumed to be in a compilation unit described by CU_HEADER.
20745 Return *BYTES_READ_PTR count of bytes read from BUF. */
20746
20747 static const char *
20748 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20749 const gdb_byte *buf,
20750 const struct comp_unit_head *cu_header,
20751 unsigned int *bytes_read_ptr)
20752 {
20753 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20754
20755 return read_indirect_string_at_offset (per_objfile, str_offset);
20756 }
20757
20758 /* See read.h. */
20759
20760 const char *
20761 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20762 const struct comp_unit_head *cu_header,
20763 unsigned int *bytes_read_ptr)
20764 {
20765 bfd *abfd = objfile->obfd;
20766 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20767
20768 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20769 }
20770
20771 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20772 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20773 ADDR_SIZE is the size of addresses from the CU header. */
20774
20775 static CORE_ADDR
20776 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20777 gdb::optional<ULONGEST> addr_base, int addr_size)
20778 {
20779 struct objfile *objfile = per_objfile->objfile;
20780 bfd *abfd = objfile->obfd;
20781 const gdb_byte *info_ptr;
20782 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20783
20784 per_objfile->per_bfd->addr.read (objfile);
20785 if (per_objfile->per_bfd->addr.buffer == NULL)
20786 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20787 objfile_name (objfile));
20788 if (addr_base_or_zero + addr_index * addr_size
20789 >= per_objfile->per_bfd->addr.size)
20790 error (_("DW_FORM_addr_index pointing outside of "
20791 ".debug_addr section [in module %s]"),
20792 objfile_name (objfile));
20793 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20794 + addr_index * addr_size);
20795 if (addr_size == 4)
20796 return bfd_get_32 (abfd, info_ptr);
20797 else
20798 return bfd_get_64 (abfd, info_ptr);
20799 }
20800
20801 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20802
20803 static CORE_ADDR
20804 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20805 {
20806 return read_addr_index_1 (cu->per_objfile, addr_index,
20807 cu->addr_base, cu->header.addr_size);
20808 }
20809
20810 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20811
20812 static CORE_ADDR
20813 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20814 unsigned int *bytes_read)
20815 {
20816 bfd *abfd = cu->per_objfile->objfile->obfd;
20817 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20818
20819 return read_addr_index (cu, addr_index);
20820 }
20821
20822 /* See read.h. */
20823
20824 CORE_ADDR
20825 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20826 dwarf2_per_objfile *per_objfile,
20827 unsigned int addr_index)
20828 {
20829 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20830 gdb::optional<ULONGEST> addr_base;
20831 int addr_size;
20832
20833 /* We need addr_base and addr_size.
20834 If we don't have PER_CU->cu, we have to get it.
20835 Nasty, but the alternative is storing the needed info in PER_CU,
20836 which at this point doesn't seem justified: it's not clear how frequently
20837 it would get used and it would increase the size of every PER_CU.
20838 Entry points like dwarf2_per_cu_addr_size do a similar thing
20839 so we're not in uncharted territory here.
20840 Alas we need to be a bit more complicated as addr_base is contained
20841 in the DIE.
20842
20843 We don't need to read the entire CU(/TU).
20844 We just need the header and top level die.
20845
20846 IWBN to use the aging mechanism to let us lazily later discard the CU.
20847 For now we skip this optimization. */
20848
20849 if (cu != NULL)
20850 {
20851 addr_base = cu->addr_base;
20852 addr_size = cu->header.addr_size;
20853 }
20854 else
20855 {
20856 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20857 addr_base = reader.cu->addr_base;
20858 addr_size = reader.cu->header.addr_size;
20859 }
20860
20861 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20862 }
20863
20864 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20865 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20866 DWO file. */
20867
20868 static const char *
20869 read_str_index (struct dwarf2_cu *cu,
20870 struct dwarf2_section_info *str_section,
20871 struct dwarf2_section_info *str_offsets_section,
20872 ULONGEST str_offsets_base, ULONGEST str_index)
20873 {
20874 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20875 struct objfile *objfile = per_objfile->objfile;
20876 const char *objf_name = objfile_name (objfile);
20877 bfd *abfd = objfile->obfd;
20878 const gdb_byte *info_ptr;
20879 ULONGEST str_offset;
20880 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20881
20882 str_section->read (objfile);
20883 str_offsets_section->read (objfile);
20884 if (str_section->buffer == NULL)
20885 error (_("%s used without %s section"
20886 " in CU at offset %s [in module %s]"),
20887 form_name, str_section->get_name (),
20888 sect_offset_str (cu->header.sect_off), objf_name);
20889 if (str_offsets_section->buffer == NULL)
20890 error (_("%s used without %s section"
20891 " in CU at offset %s [in module %s]"),
20892 form_name, str_section->get_name (),
20893 sect_offset_str (cu->header.sect_off), objf_name);
20894 info_ptr = (str_offsets_section->buffer
20895 + str_offsets_base
20896 + str_index * cu->header.offset_size);
20897 if (cu->header.offset_size == 4)
20898 str_offset = bfd_get_32 (abfd, info_ptr);
20899 else
20900 str_offset = bfd_get_64 (abfd, info_ptr);
20901 if (str_offset >= str_section->size)
20902 error (_("Offset from %s pointing outside of"
20903 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20904 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20905 return (const char *) (str_section->buffer + str_offset);
20906 }
20907
20908 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20909
20910 static const char *
20911 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20912 {
20913 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20914 ? reader->cu->header.addr_size : 0;
20915 return read_str_index (reader->cu,
20916 &reader->dwo_file->sections.str,
20917 &reader->dwo_file->sections.str_offsets,
20918 str_offsets_base, str_index);
20919 }
20920
20921 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20922
20923 static const char *
20924 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20925 {
20926 struct objfile *objfile = cu->per_objfile->objfile;
20927 const char *objf_name = objfile_name (objfile);
20928 static const char form_name[] = "DW_FORM_GNU_str_index";
20929 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20930
20931 if (!cu->str_offsets_base.has_value ())
20932 error (_("%s used in Fission stub without %s"
20933 " in CU at offset 0x%lx [in module %s]"),
20934 form_name, str_offsets_attr_name,
20935 (long) cu->header.offset_size, objf_name);
20936
20937 return read_str_index (cu,
20938 &cu->per_objfile->per_bfd->str,
20939 &cu->per_objfile->per_bfd->str_offsets,
20940 *cu->str_offsets_base, str_index);
20941 }
20942
20943 /* Return the length of an LEB128 number in BUF. */
20944
20945 static int
20946 leb128_size (const gdb_byte *buf)
20947 {
20948 const gdb_byte *begin = buf;
20949 gdb_byte byte;
20950
20951 while (1)
20952 {
20953 byte = *buf++;
20954 if ((byte & 128) == 0)
20955 return buf - begin;
20956 }
20957 }
20958
20959 static void
20960 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20961 {
20962 switch (lang)
20963 {
20964 case DW_LANG_C89:
20965 case DW_LANG_C99:
20966 case DW_LANG_C11:
20967 case DW_LANG_C:
20968 case DW_LANG_UPC:
20969 cu->language = language_c;
20970 break;
20971 case DW_LANG_Java:
20972 case DW_LANG_C_plus_plus:
20973 case DW_LANG_C_plus_plus_11:
20974 case DW_LANG_C_plus_plus_14:
20975 cu->language = language_cplus;
20976 break;
20977 case DW_LANG_D:
20978 cu->language = language_d;
20979 break;
20980 case DW_LANG_Fortran77:
20981 case DW_LANG_Fortran90:
20982 case DW_LANG_Fortran95:
20983 case DW_LANG_Fortran03:
20984 case DW_LANG_Fortran08:
20985 cu->language = language_fortran;
20986 break;
20987 case DW_LANG_Go:
20988 cu->language = language_go;
20989 break;
20990 case DW_LANG_Mips_Assembler:
20991 cu->language = language_asm;
20992 break;
20993 case DW_LANG_Ada83:
20994 case DW_LANG_Ada95:
20995 cu->language = language_ada;
20996 break;
20997 case DW_LANG_Modula2:
20998 cu->language = language_m2;
20999 break;
21000 case DW_LANG_Pascal83:
21001 cu->language = language_pascal;
21002 break;
21003 case DW_LANG_ObjC:
21004 cu->language = language_objc;
21005 break;
21006 case DW_LANG_Rust:
21007 case DW_LANG_Rust_old:
21008 cu->language = language_rust;
21009 break;
21010 case DW_LANG_Cobol74:
21011 case DW_LANG_Cobol85:
21012 default:
21013 cu->language = language_minimal;
21014 break;
21015 }
21016 cu->language_defn = language_def (cu->language);
21017 }
21018
21019 /* Return the named attribute or NULL if not there. */
21020
21021 static struct attribute *
21022 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21023 {
21024 for (;;)
21025 {
21026 unsigned int i;
21027 struct attribute *spec = NULL;
21028
21029 for (i = 0; i < die->num_attrs; ++i)
21030 {
21031 if (die->attrs[i].name == name)
21032 return &die->attrs[i];
21033 if (die->attrs[i].name == DW_AT_specification
21034 || die->attrs[i].name == DW_AT_abstract_origin)
21035 spec = &die->attrs[i];
21036 }
21037
21038 if (!spec)
21039 break;
21040
21041 die = follow_die_ref (die, spec, &cu);
21042 }
21043
21044 return NULL;
21045 }
21046
21047 /* Return the string associated with a string-typed attribute, or NULL if it
21048 is either not found or is of an incorrect type. */
21049
21050 static const char *
21051 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21052 {
21053 struct attribute *attr;
21054 const char *str = NULL;
21055
21056 attr = dwarf2_attr (die, name, cu);
21057
21058 if (attr != NULL)
21059 {
21060 str = attr->as_string ();
21061 if (str == nullptr)
21062 complaint (_("string type expected for attribute %s for "
21063 "DIE at %s in module %s"),
21064 dwarf_attr_name (name), sect_offset_str (die->sect_off),
21065 objfile_name (cu->per_objfile->objfile));
21066 }
21067
21068 return str;
21069 }
21070
21071 /* Return the dwo name or NULL if not present. If present, it is in either
21072 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
21073 static const char *
21074 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21075 {
21076 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21077 if (dwo_name == nullptr)
21078 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21079 return dwo_name;
21080 }
21081
21082 /* Return non-zero iff the attribute NAME is defined for the given DIE,
21083 and holds a non-zero value. This function should only be used for
21084 DW_FORM_flag or DW_FORM_flag_present attributes. */
21085
21086 static int
21087 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21088 {
21089 struct attribute *attr = dwarf2_attr (die, name, cu);
21090
21091 return attr != nullptr && attr->as_boolean ();
21092 }
21093
21094 static int
21095 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
21096 {
21097 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21098 which value is non-zero. However, we have to be careful with
21099 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21100 (via dwarf2_flag_true_p) follows this attribute. So we may
21101 end up accidently finding a declaration attribute that belongs
21102 to a different DIE referenced by the specification attribute,
21103 even though the given DIE does not have a declaration attribute. */
21104 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21105 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
21106 }
21107
21108 /* Return the die giving the specification for DIE, if there is
21109 one. *SPEC_CU is the CU containing DIE on input, and the CU
21110 containing the return value on output. If there is no
21111 specification, but there is an abstract origin, that is
21112 returned. */
21113
21114 static struct die_info *
21115 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
21116 {
21117 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21118 *spec_cu);
21119
21120 if (spec_attr == NULL)
21121 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21122
21123 if (spec_attr == NULL)
21124 return NULL;
21125 else
21126 return follow_die_ref (die, spec_attr, spec_cu);
21127 }
21128
21129 /* Stub for free_line_header to match void * callback types. */
21130
21131 static void
21132 free_line_header_voidp (void *arg)
21133 {
21134 struct line_header *lh = (struct line_header *) arg;
21135
21136 delete lh;
21137 }
21138
21139 /* A convenience function to find the proper .debug_line section for a CU. */
21140
21141 static struct dwarf2_section_info *
21142 get_debug_line_section (struct dwarf2_cu *cu)
21143 {
21144 struct dwarf2_section_info *section;
21145 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21146
21147 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21148 DWO file. */
21149 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21150 section = &cu->dwo_unit->dwo_file->sections.line;
21151 else if (cu->per_cu->is_dwz)
21152 {
21153 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
21154
21155 section = &dwz->line;
21156 }
21157 else
21158 section = &per_objfile->per_bfd->line;
21159
21160 return section;
21161 }
21162
21163 /* Read the statement program header starting at OFFSET in
21164 .debug_line, or .debug_line.dwo. Return a pointer
21165 to a struct line_header, allocated using xmalloc.
21166 Returns NULL if there is a problem reading the header, e.g., if it
21167 has a version we don't understand.
21168
21169 NOTE: the strings in the include directory and file name tables of
21170 the returned object point into the dwarf line section buffer,
21171 and must not be freed. */
21172
21173 static line_header_up
21174 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21175 {
21176 struct dwarf2_section_info *section;
21177 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21178
21179 section = get_debug_line_section (cu);
21180 section->read (per_objfile->objfile);
21181 if (section->buffer == NULL)
21182 {
21183 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21184 complaint (_("missing .debug_line.dwo section"));
21185 else
21186 complaint (_("missing .debug_line section"));
21187 return 0;
21188 }
21189
21190 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21191 per_objfile, section, &cu->header);
21192 }
21193
21194 /* Subroutine of dwarf_decode_lines to simplify it.
21195 Return the file name of the psymtab for the given file_entry.
21196 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21197 If space for the result is malloc'd, *NAME_HOLDER will be set.
21198 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21199
21200 static const char *
21201 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21202 const dwarf2_psymtab *pst,
21203 const char *comp_dir,
21204 gdb::unique_xmalloc_ptr<char> *name_holder)
21205 {
21206 const char *include_name = fe.name;
21207 const char *include_name_to_compare = include_name;
21208 const char *pst_filename;
21209 int file_is_pst;
21210
21211 const char *dir_name = fe.include_dir (lh);
21212
21213 gdb::unique_xmalloc_ptr<char> hold_compare;
21214 if (!IS_ABSOLUTE_PATH (include_name)
21215 && (dir_name != NULL || comp_dir != NULL))
21216 {
21217 /* Avoid creating a duplicate psymtab for PST.
21218 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21219 Before we do the comparison, however, we need to account
21220 for DIR_NAME and COMP_DIR.
21221 First prepend dir_name (if non-NULL). If we still don't
21222 have an absolute path prepend comp_dir (if non-NULL).
21223 However, the directory we record in the include-file's
21224 psymtab does not contain COMP_DIR (to match the
21225 corresponding symtab(s)).
21226
21227 Example:
21228
21229 bash$ cd /tmp
21230 bash$ gcc -g ./hello.c
21231 include_name = "hello.c"
21232 dir_name = "."
21233 DW_AT_comp_dir = comp_dir = "/tmp"
21234 DW_AT_name = "./hello.c"
21235
21236 */
21237
21238 if (dir_name != NULL)
21239 {
21240 name_holder->reset (concat (dir_name, SLASH_STRING,
21241 include_name, (char *) NULL));
21242 include_name = name_holder->get ();
21243 include_name_to_compare = include_name;
21244 }
21245 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21246 {
21247 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21248 include_name, (char *) NULL));
21249 include_name_to_compare = hold_compare.get ();
21250 }
21251 }
21252
21253 pst_filename = pst->filename;
21254 gdb::unique_xmalloc_ptr<char> copied_name;
21255 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21256 {
21257 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21258 pst_filename, (char *) NULL));
21259 pst_filename = copied_name.get ();
21260 }
21261
21262 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21263
21264 if (file_is_pst)
21265 return NULL;
21266 return include_name;
21267 }
21268
21269 /* State machine to track the state of the line number program. */
21270
21271 class lnp_state_machine
21272 {
21273 public:
21274 /* Initialize a machine state for the start of a line number
21275 program. */
21276 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21277 bool record_lines_p);
21278
21279 file_entry *current_file ()
21280 {
21281 /* lh->file_names is 0-based, but the file name numbers in the
21282 statement program are 1-based. */
21283 return m_line_header->file_name_at (m_file);
21284 }
21285
21286 /* Record the line in the state machine. END_SEQUENCE is true if
21287 we're processing the end of a sequence. */
21288 void record_line (bool end_sequence);
21289
21290 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21291 nop-out rest of the lines in this sequence. */
21292 void check_line_address (struct dwarf2_cu *cu,
21293 const gdb_byte *line_ptr,
21294 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21295
21296 void handle_set_discriminator (unsigned int discriminator)
21297 {
21298 m_discriminator = discriminator;
21299 m_line_has_non_zero_discriminator |= discriminator != 0;
21300 }
21301
21302 /* Handle DW_LNE_set_address. */
21303 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21304 {
21305 m_op_index = 0;
21306 address += baseaddr;
21307 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21308 }
21309
21310 /* Handle DW_LNS_advance_pc. */
21311 void handle_advance_pc (CORE_ADDR adjust);
21312
21313 /* Handle a special opcode. */
21314 void handle_special_opcode (unsigned char op_code);
21315
21316 /* Handle DW_LNS_advance_line. */
21317 void handle_advance_line (int line_delta)
21318 {
21319 advance_line (line_delta);
21320 }
21321
21322 /* Handle DW_LNS_set_file. */
21323 void handle_set_file (file_name_index file);
21324
21325 /* Handle DW_LNS_negate_stmt. */
21326 void handle_negate_stmt ()
21327 {
21328 m_is_stmt = !m_is_stmt;
21329 }
21330
21331 /* Handle DW_LNS_const_add_pc. */
21332 void handle_const_add_pc ();
21333
21334 /* Handle DW_LNS_fixed_advance_pc. */
21335 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21336 {
21337 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21338 m_op_index = 0;
21339 }
21340
21341 /* Handle DW_LNS_copy. */
21342 void handle_copy ()
21343 {
21344 record_line (false);
21345 m_discriminator = 0;
21346 }
21347
21348 /* Handle DW_LNE_end_sequence. */
21349 void handle_end_sequence ()
21350 {
21351 m_currently_recording_lines = true;
21352 }
21353
21354 private:
21355 /* Advance the line by LINE_DELTA. */
21356 void advance_line (int line_delta)
21357 {
21358 m_line += line_delta;
21359
21360 if (line_delta != 0)
21361 m_line_has_non_zero_discriminator = m_discriminator != 0;
21362 }
21363
21364 struct dwarf2_cu *m_cu;
21365
21366 gdbarch *m_gdbarch;
21367
21368 /* True if we're recording lines.
21369 Otherwise we're building partial symtabs and are just interested in
21370 finding include files mentioned by the line number program. */
21371 bool m_record_lines_p;
21372
21373 /* The line number header. */
21374 line_header *m_line_header;
21375
21376 /* These are part of the standard DWARF line number state machine,
21377 and initialized according to the DWARF spec. */
21378
21379 unsigned char m_op_index = 0;
21380 /* The line table index of the current file. */
21381 file_name_index m_file = 1;
21382 unsigned int m_line = 1;
21383
21384 /* These are initialized in the constructor. */
21385
21386 CORE_ADDR m_address;
21387 bool m_is_stmt;
21388 unsigned int m_discriminator;
21389
21390 /* Additional bits of state we need to track. */
21391
21392 /* The last file that we called dwarf2_start_subfile for.
21393 This is only used for TLLs. */
21394 unsigned int m_last_file = 0;
21395 /* The last file a line number was recorded for. */
21396 struct subfile *m_last_subfile = NULL;
21397
21398 /* The address of the last line entry. */
21399 CORE_ADDR m_last_address;
21400
21401 /* Set to true when a previous line at the same address (using
21402 m_last_address) had m_is_stmt true. This is reset to false when a
21403 line entry at a new address (m_address different to m_last_address) is
21404 processed. */
21405 bool m_stmt_at_address = false;
21406
21407 /* When true, record the lines we decode. */
21408 bool m_currently_recording_lines = false;
21409
21410 /* The last line number that was recorded, used to coalesce
21411 consecutive entries for the same line. This can happen, for
21412 example, when discriminators are present. PR 17276. */
21413 unsigned int m_last_line = 0;
21414 bool m_line_has_non_zero_discriminator = false;
21415 };
21416
21417 void
21418 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21419 {
21420 CORE_ADDR addr_adj = (((m_op_index + adjust)
21421 / m_line_header->maximum_ops_per_instruction)
21422 * m_line_header->minimum_instruction_length);
21423 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21424 m_op_index = ((m_op_index + adjust)
21425 % m_line_header->maximum_ops_per_instruction);
21426 }
21427
21428 void
21429 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21430 {
21431 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21432 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21433 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21434 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
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 + adj_opcode_d)
21439 % m_line_header->maximum_ops_per_instruction);
21440
21441 int line_delta = m_line_header->line_base + adj_opcode_r;
21442 advance_line (line_delta);
21443 record_line (false);
21444 m_discriminator = 0;
21445 }
21446
21447 void
21448 lnp_state_machine::handle_set_file (file_name_index file)
21449 {
21450 m_file = file;
21451
21452 const file_entry *fe = current_file ();
21453 if (fe == NULL)
21454 dwarf2_debug_line_missing_file_complaint ();
21455 else if (m_record_lines_p)
21456 {
21457 const char *dir = fe->include_dir (m_line_header);
21458
21459 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21460 m_line_has_non_zero_discriminator = m_discriminator != 0;
21461 dwarf2_start_subfile (m_cu, fe->name, dir);
21462 }
21463 }
21464
21465 void
21466 lnp_state_machine::handle_const_add_pc ()
21467 {
21468 CORE_ADDR adjust
21469 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21470
21471 CORE_ADDR addr_adj
21472 = (((m_op_index + adjust)
21473 / m_line_header->maximum_ops_per_instruction)
21474 * m_line_header->minimum_instruction_length);
21475
21476 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21477 m_op_index = ((m_op_index + adjust)
21478 % m_line_header->maximum_ops_per_instruction);
21479 }
21480
21481 /* Return non-zero if we should add LINE to the line number table.
21482 LINE is the line to add, LAST_LINE is the last line that was added,
21483 LAST_SUBFILE is the subfile for LAST_LINE.
21484 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21485 had a non-zero discriminator.
21486
21487 We have to be careful in the presence of discriminators.
21488 E.g., for this line:
21489
21490 for (i = 0; i < 100000; i++);
21491
21492 clang can emit four line number entries for that one line,
21493 each with a different discriminator.
21494 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21495
21496 However, we want gdb to coalesce all four entries into one.
21497 Otherwise the user could stepi into the middle of the line and
21498 gdb would get confused about whether the pc really was in the
21499 middle of the line.
21500
21501 Things are further complicated by the fact that two consecutive
21502 line number entries for the same line is a heuristic used by gcc
21503 to denote the end of the prologue. So we can't just discard duplicate
21504 entries, we have to be selective about it. The heuristic we use is
21505 that we only collapse consecutive entries for the same line if at least
21506 one of those entries has a non-zero discriminator. PR 17276.
21507
21508 Note: Addresses in the line number state machine can never go backwards
21509 within one sequence, thus this coalescing is ok. */
21510
21511 static int
21512 dwarf_record_line_p (struct dwarf2_cu *cu,
21513 unsigned int line, unsigned int last_line,
21514 int line_has_non_zero_discriminator,
21515 struct subfile *last_subfile)
21516 {
21517 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21518 return 1;
21519 if (line != last_line)
21520 return 1;
21521 /* Same line for the same file that we've seen already.
21522 As a last check, for pr 17276, only record the line if the line
21523 has never had a non-zero discriminator. */
21524 if (!line_has_non_zero_discriminator)
21525 return 1;
21526 return 0;
21527 }
21528
21529 /* Use the CU's builder to record line number LINE beginning at
21530 address ADDRESS in the line table of subfile SUBFILE. */
21531
21532 static void
21533 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21534 unsigned int line, CORE_ADDR address, bool is_stmt,
21535 struct dwarf2_cu *cu)
21536 {
21537 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21538
21539 if (dwarf_line_debug)
21540 {
21541 fprintf_unfiltered (gdb_stdlog,
21542 "Recording line %u, file %s, address %s\n",
21543 line, lbasename (subfile->name),
21544 paddress (gdbarch, address));
21545 }
21546
21547 if (cu != nullptr)
21548 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21549 }
21550
21551 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21552 Mark the end of a set of line number records.
21553 The arguments are the same as for dwarf_record_line_1.
21554 If SUBFILE is NULL the request is ignored. */
21555
21556 static void
21557 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21558 CORE_ADDR address, struct dwarf2_cu *cu)
21559 {
21560 if (subfile == NULL)
21561 return;
21562
21563 if (dwarf_line_debug)
21564 {
21565 fprintf_unfiltered (gdb_stdlog,
21566 "Finishing current line, file %s, address %s\n",
21567 lbasename (subfile->name),
21568 paddress (gdbarch, address));
21569 }
21570
21571 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21572 }
21573
21574 void
21575 lnp_state_machine::record_line (bool end_sequence)
21576 {
21577 if (dwarf_line_debug)
21578 {
21579 fprintf_unfiltered (gdb_stdlog,
21580 "Processing actual line %u: file %u,"
21581 " address %s, is_stmt %u, discrim %u%s\n",
21582 m_line, m_file,
21583 paddress (m_gdbarch, m_address),
21584 m_is_stmt, m_discriminator,
21585 (end_sequence ? "\t(end sequence)" : ""));
21586 }
21587
21588 file_entry *fe = current_file ();
21589
21590 if (fe == NULL)
21591 dwarf2_debug_line_missing_file_complaint ();
21592 /* For now we ignore lines not starting on an instruction boundary.
21593 But not when processing end_sequence for compatibility with the
21594 previous version of the code. */
21595 else if (m_op_index == 0 || end_sequence)
21596 {
21597 fe->included_p = 1;
21598 if (m_record_lines_p)
21599 {
21600 /* When we switch files we insert an end maker in the first file,
21601 switch to the second file and add a new line entry. The
21602 problem is that the end marker inserted in the first file will
21603 discard any previous line entries at the same address. If the
21604 line entries in the first file are marked as is-stmt, while
21605 the new line in the second file is non-stmt, then this means
21606 the end marker will discard is-stmt lines so we can have a
21607 non-stmt line. This means that there are less addresses at
21608 which the user can insert a breakpoint.
21609
21610 To improve this we track the last address in m_last_address,
21611 and whether we have seen an is-stmt at this address. Then
21612 when switching files, if we have seen a stmt at the current
21613 address, and we are switching to create a non-stmt line, then
21614 discard the new line. */
21615 bool file_changed
21616 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21617 bool ignore_this_line
21618 = ((file_changed && !end_sequence && m_last_address == m_address
21619 && !m_is_stmt && m_stmt_at_address)
21620 || (!end_sequence && m_line == 0));
21621
21622 if ((file_changed && !ignore_this_line) || end_sequence)
21623 {
21624 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21625 m_currently_recording_lines ? m_cu : nullptr);
21626 }
21627
21628 if (!end_sequence && !ignore_this_line)
21629 {
21630 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21631
21632 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21633 m_line_has_non_zero_discriminator,
21634 m_last_subfile))
21635 {
21636 buildsym_compunit *builder = m_cu->get_builder ();
21637 dwarf_record_line_1 (m_gdbarch,
21638 builder->get_current_subfile (),
21639 m_line, m_address, is_stmt,
21640 m_currently_recording_lines ? m_cu : nullptr);
21641 }
21642 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21643 m_last_line = m_line;
21644 }
21645 }
21646 }
21647
21648 /* Track whether we have seen any m_is_stmt true at m_address in case we
21649 have multiple line table entries all at m_address. */
21650 if (m_last_address != m_address)
21651 {
21652 m_stmt_at_address = false;
21653 m_last_address = m_address;
21654 }
21655 m_stmt_at_address |= m_is_stmt;
21656 }
21657
21658 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21659 line_header *lh, bool record_lines_p)
21660 {
21661 m_cu = cu;
21662 m_gdbarch = arch;
21663 m_record_lines_p = record_lines_p;
21664 m_line_header = lh;
21665
21666 m_currently_recording_lines = true;
21667
21668 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21669 was a line entry for it so that the backend has a chance to adjust it
21670 and also record it in case it needs it. This is currently used by MIPS
21671 code, cf. `mips_adjust_dwarf2_line'. */
21672 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21673 m_is_stmt = lh->default_is_stmt;
21674 m_discriminator = 0;
21675
21676 m_last_address = m_address;
21677 m_stmt_at_address = false;
21678 }
21679
21680 void
21681 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21682 const gdb_byte *line_ptr,
21683 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21684 {
21685 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21686 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21687 located at 0x0. In this case, additionally check that if
21688 ADDRESS < UNRELOCATED_LOWPC. */
21689
21690 if ((address == 0 && address < unrelocated_lowpc)
21691 || address == (CORE_ADDR) -1)
21692 {
21693 /* This line table is for a function which has been
21694 GCd by the linker. Ignore it. PR gdb/12528 */
21695
21696 struct objfile *objfile = cu->per_objfile->objfile;
21697 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21698
21699 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21700 line_offset, objfile_name (objfile));
21701 m_currently_recording_lines = false;
21702 /* Note: m_currently_recording_lines is left as false until we see
21703 DW_LNE_end_sequence. */
21704 }
21705 }
21706
21707 /* Subroutine of dwarf_decode_lines to simplify it.
21708 Process the line number information in LH.
21709 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21710 program in order to set included_p for every referenced header. */
21711
21712 static void
21713 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21714 const int decode_for_pst_p, CORE_ADDR lowpc)
21715 {
21716 const gdb_byte *line_ptr, *extended_end;
21717 const gdb_byte *line_end;
21718 unsigned int bytes_read, extended_len;
21719 unsigned char op_code, extended_op;
21720 CORE_ADDR baseaddr;
21721 struct objfile *objfile = cu->per_objfile->objfile;
21722 bfd *abfd = objfile->obfd;
21723 struct gdbarch *gdbarch = objfile->arch ();
21724 /* True if we're recording line info (as opposed to building partial
21725 symtabs and just interested in finding include files mentioned by
21726 the line number program). */
21727 bool record_lines_p = !decode_for_pst_p;
21728
21729 baseaddr = objfile->text_section_offset ();
21730
21731 line_ptr = lh->statement_program_start;
21732 line_end = lh->statement_program_end;
21733
21734 /* Read the statement sequences until there's nothing left. */
21735 while (line_ptr < line_end)
21736 {
21737 /* The DWARF line number program state machine. Reset the state
21738 machine at the start of each sequence. */
21739 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21740 bool end_sequence = false;
21741
21742 if (record_lines_p)
21743 {
21744 /* Start a subfile for the current file of the state
21745 machine. */
21746 const file_entry *fe = state_machine.current_file ();
21747
21748 if (fe != NULL)
21749 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21750 }
21751
21752 /* Decode the table. */
21753 while (line_ptr < line_end && !end_sequence)
21754 {
21755 op_code = read_1_byte (abfd, line_ptr);
21756 line_ptr += 1;
21757
21758 if (op_code >= lh->opcode_base)
21759 {
21760 /* Special opcode. */
21761 state_machine.handle_special_opcode (op_code);
21762 }
21763 else switch (op_code)
21764 {
21765 case DW_LNS_extended_op:
21766 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21767 &bytes_read);
21768 line_ptr += bytes_read;
21769 extended_end = line_ptr + extended_len;
21770 extended_op = read_1_byte (abfd, line_ptr);
21771 line_ptr += 1;
21772 if (DW_LNE_lo_user <= extended_op
21773 && extended_op <= DW_LNE_hi_user)
21774 {
21775 /* Vendor extension, ignore. */
21776 line_ptr = extended_end;
21777 break;
21778 }
21779 switch (extended_op)
21780 {
21781 case DW_LNE_end_sequence:
21782 state_machine.handle_end_sequence ();
21783 end_sequence = true;
21784 break;
21785 case DW_LNE_set_address:
21786 {
21787 CORE_ADDR address
21788 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21789 line_ptr += bytes_read;
21790
21791 state_machine.check_line_address (cu, line_ptr,
21792 lowpc - baseaddr, address);
21793 state_machine.handle_set_address (baseaddr, address);
21794 }
21795 break;
21796 case DW_LNE_define_file:
21797 {
21798 const char *cur_file;
21799 unsigned int mod_time, length;
21800 dir_index dindex;
21801
21802 cur_file = read_direct_string (abfd, line_ptr,
21803 &bytes_read);
21804 line_ptr += bytes_read;
21805 dindex = (dir_index)
21806 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21807 line_ptr += bytes_read;
21808 mod_time =
21809 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21810 line_ptr += bytes_read;
21811 length =
21812 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21813 line_ptr += bytes_read;
21814 lh->add_file_name (cur_file, dindex, mod_time, length);
21815 }
21816 break;
21817 case DW_LNE_set_discriminator:
21818 {
21819 /* The discriminator is not interesting to the
21820 debugger; just ignore it. We still need to
21821 check its value though:
21822 if there are consecutive entries for the same
21823 (non-prologue) line we want to coalesce them.
21824 PR 17276. */
21825 unsigned int discr
21826 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21827 line_ptr += bytes_read;
21828
21829 state_machine.handle_set_discriminator (discr);
21830 }
21831 break;
21832 default:
21833 complaint (_("mangled .debug_line section"));
21834 return;
21835 }
21836 /* Make sure that we parsed the extended op correctly. If e.g.
21837 we expected a different address size than the producer used,
21838 we may have read the wrong number of bytes. */
21839 if (line_ptr != extended_end)
21840 {
21841 complaint (_("mangled .debug_line section"));
21842 return;
21843 }
21844 break;
21845 case DW_LNS_copy:
21846 state_machine.handle_copy ();
21847 break;
21848 case DW_LNS_advance_pc:
21849 {
21850 CORE_ADDR adjust
21851 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21852 line_ptr += bytes_read;
21853
21854 state_machine.handle_advance_pc (adjust);
21855 }
21856 break;
21857 case DW_LNS_advance_line:
21858 {
21859 int line_delta
21860 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21861 line_ptr += bytes_read;
21862
21863 state_machine.handle_advance_line (line_delta);
21864 }
21865 break;
21866 case DW_LNS_set_file:
21867 {
21868 file_name_index file
21869 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21870 &bytes_read);
21871 line_ptr += bytes_read;
21872
21873 state_machine.handle_set_file (file);
21874 }
21875 break;
21876 case DW_LNS_set_column:
21877 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21878 line_ptr += bytes_read;
21879 break;
21880 case DW_LNS_negate_stmt:
21881 state_machine.handle_negate_stmt ();
21882 break;
21883 case DW_LNS_set_basic_block:
21884 break;
21885 /* Add to the address register of the state machine the
21886 address increment value corresponding to special opcode
21887 255. I.e., this value is scaled by the minimum
21888 instruction length since special opcode 255 would have
21889 scaled the increment. */
21890 case DW_LNS_const_add_pc:
21891 state_machine.handle_const_add_pc ();
21892 break;
21893 case DW_LNS_fixed_advance_pc:
21894 {
21895 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21896 line_ptr += 2;
21897
21898 state_machine.handle_fixed_advance_pc (addr_adj);
21899 }
21900 break;
21901 default:
21902 {
21903 /* Unknown standard opcode, ignore it. */
21904 int i;
21905
21906 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21907 {
21908 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21909 line_ptr += bytes_read;
21910 }
21911 }
21912 }
21913 }
21914
21915 if (!end_sequence)
21916 dwarf2_debug_line_missing_end_sequence_complaint ();
21917
21918 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21919 in which case we still finish recording the last line). */
21920 state_machine.record_line (true);
21921 }
21922 }
21923
21924 /* Decode the Line Number Program (LNP) for the given line_header
21925 structure and CU. The actual information extracted and the type
21926 of structures created from the LNP depends on the value of PST.
21927
21928 1. If PST is NULL, then this procedure uses the data from the program
21929 to create all necessary symbol tables, and their linetables.
21930
21931 2. If PST is not NULL, this procedure reads the program to determine
21932 the list of files included by the unit represented by PST, and
21933 builds all the associated partial symbol tables.
21934
21935 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21936 It is used for relative paths in the line table.
21937 NOTE: When processing partial symtabs (pst != NULL),
21938 comp_dir == pst->dirname.
21939
21940 NOTE: It is important that psymtabs have the same file name (via strcmp)
21941 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21942 symtab we don't use it in the name of the psymtabs we create.
21943 E.g. expand_line_sal requires this when finding psymtabs to expand.
21944 A good testcase for this is mb-inline.exp.
21945
21946 LOWPC is the lowest address in CU (or 0 if not known).
21947
21948 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21949 for its PC<->lines mapping information. Otherwise only the filename
21950 table is read in. */
21951
21952 static void
21953 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21954 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21955 CORE_ADDR lowpc, int decode_mapping)
21956 {
21957 struct objfile *objfile = cu->per_objfile->objfile;
21958 const int decode_for_pst_p = (pst != NULL);
21959
21960 if (decode_mapping)
21961 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21962
21963 if (decode_for_pst_p)
21964 {
21965 /* Now that we're done scanning the Line Header Program, we can
21966 create the psymtab of each included file. */
21967 for (auto &file_entry : lh->file_names ())
21968 if (file_entry.included_p == 1)
21969 {
21970 gdb::unique_xmalloc_ptr<char> name_holder;
21971 const char *include_name =
21972 psymtab_include_file_name (lh, file_entry, pst,
21973 comp_dir, &name_holder);
21974 if (include_name != NULL)
21975 dwarf2_create_include_psymtab (cu->per_objfile->per_bfd,
21976 include_name, pst, objfile);
21977 }
21978 }
21979 else
21980 {
21981 /* Make sure a symtab is created for every file, even files
21982 which contain only variables (i.e. no code with associated
21983 line numbers). */
21984 buildsym_compunit *builder = cu->get_builder ();
21985 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21986
21987 for (auto &fe : lh->file_names ())
21988 {
21989 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21990 if (builder->get_current_subfile ()->symtab == NULL)
21991 {
21992 builder->get_current_subfile ()->symtab
21993 = allocate_symtab (cust,
21994 builder->get_current_subfile ()->name);
21995 }
21996 fe.symtab = builder->get_current_subfile ()->symtab;
21997 }
21998 }
21999 }
22000
22001 /* Start a subfile for DWARF. FILENAME is the name of the file and
22002 DIRNAME the name of the source directory which contains FILENAME
22003 or NULL if not known.
22004 This routine tries to keep line numbers from identical absolute and
22005 relative file names in a common subfile.
22006
22007 Using the `list' example from the GDB testsuite, which resides in
22008 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22009 of /srcdir/list0.c yields the following debugging information for list0.c:
22010
22011 DW_AT_name: /srcdir/list0.c
22012 DW_AT_comp_dir: /compdir
22013 files.files[0].name: list0.h
22014 files.files[0].dir: /srcdir
22015 files.files[1].name: list0.c
22016 files.files[1].dir: /srcdir
22017
22018 The line number information for list0.c has to end up in a single
22019 subfile, so that `break /srcdir/list0.c:1' works as expected.
22020 start_subfile will ensure that this happens provided that we pass the
22021 concatenation of files.files[1].dir and files.files[1].name as the
22022 subfile's name. */
22023
22024 static void
22025 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22026 const char *dirname)
22027 {
22028 gdb::unique_xmalloc_ptr<char> copy;
22029
22030 /* In order not to lose the line information directory,
22031 we concatenate it to the filename when it makes sense.
22032 Note that the Dwarf3 standard says (speaking of filenames in line
22033 information): ``The directory index is ignored for file names
22034 that represent full path names''. Thus ignoring dirname in the
22035 `else' branch below isn't an issue. */
22036
22037 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
22038 {
22039 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22040 filename = copy.get ();
22041 }
22042
22043 cu->get_builder ()->start_subfile (filename);
22044 }
22045
22046 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22047 buildsym_compunit constructor. */
22048
22049 struct compunit_symtab *
22050 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22051 CORE_ADDR low_pc)
22052 {
22053 gdb_assert (m_builder == nullptr);
22054
22055 m_builder.reset (new struct buildsym_compunit
22056 (this->per_objfile->objfile,
22057 name, comp_dir, language, low_pc));
22058
22059 list_in_scope = get_builder ()->get_file_symbols ();
22060
22061 get_builder ()->record_debugformat ("DWARF 2");
22062 get_builder ()->record_producer (producer);
22063
22064 processing_has_namespace_info = false;
22065
22066 return get_builder ()->get_compunit_symtab ();
22067 }
22068
22069 static void
22070 var_decode_location (struct attribute *attr, struct symbol *sym,
22071 struct dwarf2_cu *cu)
22072 {
22073 struct objfile *objfile = cu->per_objfile->objfile;
22074 struct comp_unit_head *cu_header = &cu->header;
22075
22076 /* NOTE drow/2003-01-30: There used to be a comment and some special
22077 code here to turn a symbol with DW_AT_external and a
22078 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22079 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22080 with some versions of binutils) where shared libraries could have
22081 relocations against symbols in their debug information - the
22082 minimal symbol would have the right address, but the debug info
22083 would not. It's no longer necessary, because we will explicitly
22084 apply relocations when we read in the debug information now. */
22085
22086 /* A DW_AT_location attribute with no contents indicates that a
22087 variable has been optimized away. */
22088 if (attr->form_is_block () && attr->as_block ()->size == 0)
22089 {
22090 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22091 return;
22092 }
22093
22094 /* Handle one degenerate form of location expression specially, to
22095 preserve GDB's previous behavior when section offsets are
22096 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22097 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
22098
22099 if (attr->form_is_block ())
22100 {
22101 struct dwarf_block *block = attr->as_block ();
22102
22103 if ((block->data[0] == DW_OP_addr
22104 && block->size == 1 + cu_header->addr_size)
22105 || ((block->data[0] == DW_OP_GNU_addr_index
22106 || block->data[0] == DW_OP_addrx)
22107 && (block->size
22108 == 1 + leb128_size (&block->data[1]))))
22109 {
22110 unsigned int dummy;
22111
22112 if (block->data[0] == DW_OP_addr)
22113 SET_SYMBOL_VALUE_ADDRESS
22114 (sym, cu->header.read_address (objfile->obfd,
22115 block->data + 1,
22116 &dummy));
22117 else
22118 SET_SYMBOL_VALUE_ADDRESS
22119 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22120 &dummy));
22121 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22122 fixup_symbol_section (sym, objfile);
22123 SET_SYMBOL_VALUE_ADDRESS
22124 (sym,
22125 SYMBOL_VALUE_ADDRESS (sym)
22126 + objfile->section_offsets[sym->section_index ()]);
22127 return;
22128 }
22129 }
22130
22131 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22132 expression evaluator, and use LOC_COMPUTED only when necessary
22133 (i.e. when the value of a register or memory location is
22134 referenced, or a thread-local block, etc.). Then again, it might
22135 not be worthwhile. I'm assuming that it isn't unless performance
22136 or memory numbers show me otherwise. */
22137
22138 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22139
22140 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22141 cu->has_loclist = true;
22142 }
22143
22144 /* Given a pointer to a DWARF information entry, figure out if we need
22145 to make a symbol table entry for it, and if so, create a new entry
22146 and return a pointer to it.
22147 If TYPE is NULL, determine symbol type from the die, otherwise
22148 used the passed type.
22149 If SPACE is not NULL, use it to hold the new symbol. If it is
22150 NULL, allocate a new symbol on the objfile's obstack. */
22151
22152 static struct symbol *
22153 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22154 struct symbol *space)
22155 {
22156 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22157 struct objfile *objfile = per_objfile->objfile;
22158 struct gdbarch *gdbarch = objfile->arch ();
22159 struct symbol *sym = NULL;
22160 const char *name;
22161 struct attribute *attr = NULL;
22162 struct attribute *attr2 = NULL;
22163 CORE_ADDR baseaddr;
22164 struct pending **list_to_add = NULL;
22165
22166 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22167
22168 baseaddr = objfile->text_section_offset ();
22169
22170 name = dwarf2_name (die, cu);
22171 if (name)
22172 {
22173 int suppress_add = 0;
22174
22175 if (space)
22176 sym = space;
22177 else
22178 sym = new (&objfile->objfile_obstack) symbol;
22179 OBJSTAT (objfile, n_syms++);
22180
22181 /* Cache this symbol's name and the name's demangled form (if any). */
22182 sym->set_language (cu->language, &objfile->objfile_obstack);
22183 /* Fortran does not have mangling standard and the mangling does differ
22184 between gfortran, iFort etc. */
22185 const char *physname
22186 = (cu->language == language_fortran
22187 ? dwarf2_full_name (name, die, cu)
22188 : dwarf2_physname (name, die, cu));
22189 const char *linkagename = dw2_linkage_name (die, cu);
22190
22191 if (linkagename == nullptr || cu->language == language_ada)
22192 sym->set_linkage_name (physname);
22193 else
22194 {
22195 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22196 sym->set_linkage_name (linkagename);
22197 }
22198
22199 /* Default assumptions.
22200 Use the passed type or decode it from the die. */
22201 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22202 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22203 if (type != NULL)
22204 SYMBOL_TYPE (sym) = type;
22205 else
22206 SYMBOL_TYPE (sym) = die_type (die, cu);
22207 attr = dwarf2_attr (die,
22208 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22209 cu);
22210 if (attr != nullptr)
22211 SYMBOL_LINE (sym) = attr->constant_value (0);
22212
22213 attr = dwarf2_attr (die,
22214 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22215 cu);
22216 if (attr != nullptr && attr->is_nonnegative ())
22217 {
22218 file_name_index file_index
22219 = (file_name_index) attr->as_nonnegative ();
22220 struct file_entry *fe;
22221
22222 if (cu->line_header != NULL)
22223 fe = cu->line_header->file_name_at (file_index);
22224 else
22225 fe = NULL;
22226
22227 if (fe == NULL)
22228 complaint (_("file index out of range"));
22229 else
22230 symbol_set_symtab (sym, fe->symtab);
22231 }
22232
22233 switch (die->tag)
22234 {
22235 case DW_TAG_label:
22236 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22237 if (attr != nullptr)
22238 {
22239 CORE_ADDR addr;
22240
22241 addr = attr->as_address ();
22242 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22243 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22244 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22245 }
22246 else
22247 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22248 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22249 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22250 add_symbol_to_list (sym, cu->list_in_scope);
22251 break;
22252 case DW_TAG_subprogram:
22253 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22254 finish_block. */
22255 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22256 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22257 if ((attr2 != nullptr && attr2->as_boolean ())
22258 || cu->language == language_ada
22259 || cu->language == language_fortran)
22260 {
22261 /* Subprograms marked external are stored as a global symbol.
22262 Ada and Fortran subprograms, whether marked external or
22263 not, are always stored as a global symbol, because we want
22264 to be able to access them globally. For instance, we want
22265 to be able to break on a nested subprogram without having
22266 to specify the context. */
22267 list_to_add = cu->get_builder ()->get_global_symbols ();
22268 }
22269 else
22270 {
22271 list_to_add = cu->list_in_scope;
22272 }
22273 break;
22274 case DW_TAG_inlined_subroutine:
22275 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22276 finish_block. */
22277 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22278 SYMBOL_INLINED (sym) = 1;
22279 list_to_add = cu->list_in_scope;
22280 break;
22281 case DW_TAG_template_value_param:
22282 suppress_add = 1;
22283 /* Fall through. */
22284 case DW_TAG_constant:
22285 case DW_TAG_variable:
22286 case DW_TAG_member:
22287 /* Compilation with minimal debug info may result in
22288 variables with missing type entries. Change the
22289 misleading `void' type to something sensible. */
22290 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22291 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22292
22293 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22294 /* In the case of DW_TAG_member, we should only be called for
22295 static const members. */
22296 if (die->tag == DW_TAG_member)
22297 {
22298 /* dwarf2_add_field uses die_is_declaration,
22299 so we do the same. */
22300 gdb_assert (die_is_declaration (die, cu));
22301 gdb_assert (attr);
22302 }
22303 if (attr != nullptr)
22304 {
22305 dwarf2_const_value (attr, sym, cu);
22306 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22307 if (!suppress_add)
22308 {
22309 if (attr2 != nullptr && attr2->as_boolean ())
22310 list_to_add = cu->get_builder ()->get_global_symbols ();
22311 else
22312 list_to_add = cu->list_in_scope;
22313 }
22314 break;
22315 }
22316 attr = dwarf2_attr (die, DW_AT_location, cu);
22317 if (attr != nullptr)
22318 {
22319 var_decode_location (attr, sym, cu);
22320 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22321
22322 /* Fortran explicitly imports any global symbols to the local
22323 scope by DW_TAG_common_block. */
22324 if (cu->language == language_fortran && die->parent
22325 && die->parent->tag == DW_TAG_common_block)
22326 attr2 = NULL;
22327
22328 if (SYMBOL_CLASS (sym) == LOC_STATIC
22329 && SYMBOL_VALUE_ADDRESS (sym) == 0
22330 && !per_objfile->per_bfd->has_section_at_zero)
22331 {
22332 /* When a static variable is eliminated by the linker,
22333 the corresponding debug information is not stripped
22334 out, but the variable address is set to null;
22335 do not add such variables into symbol table. */
22336 }
22337 else if (attr2 != nullptr && attr2->as_boolean ())
22338 {
22339 if (SYMBOL_CLASS (sym) == LOC_STATIC
22340 && (objfile->flags & OBJF_MAINLINE) == 0
22341 && per_objfile->per_bfd->can_copy)
22342 {
22343 /* A global static variable might be subject to
22344 copy relocation. We first check for a local
22345 minsym, though, because maybe the symbol was
22346 marked hidden, in which case this would not
22347 apply. */
22348 bound_minimal_symbol found
22349 = (lookup_minimal_symbol_linkage
22350 (sym->linkage_name (), objfile));
22351 if (found.minsym != nullptr)
22352 sym->maybe_copied = 1;
22353 }
22354
22355 /* A variable with DW_AT_external is never static,
22356 but it may be block-scoped. */
22357 list_to_add
22358 = ((cu->list_in_scope
22359 == cu->get_builder ()->get_file_symbols ())
22360 ? cu->get_builder ()->get_global_symbols ()
22361 : cu->list_in_scope);
22362 }
22363 else
22364 list_to_add = cu->list_in_scope;
22365 }
22366 else
22367 {
22368 /* We do not know the address of this symbol.
22369 If it is an external symbol and we have type information
22370 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22371 The address of the variable will then be determined from
22372 the minimal symbol table whenever the variable is
22373 referenced. */
22374 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22375
22376 /* Fortran explicitly imports any global symbols to the local
22377 scope by DW_TAG_common_block. */
22378 if (cu->language == language_fortran && die->parent
22379 && die->parent->tag == DW_TAG_common_block)
22380 {
22381 /* SYMBOL_CLASS doesn't matter here because
22382 read_common_block is going to reset it. */
22383 if (!suppress_add)
22384 list_to_add = cu->list_in_scope;
22385 }
22386 else if (attr2 != nullptr && attr2->as_boolean ()
22387 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22388 {
22389 /* A variable with DW_AT_external is never static, but it
22390 may be block-scoped. */
22391 list_to_add
22392 = ((cu->list_in_scope
22393 == cu->get_builder ()->get_file_symbols ())
22394 ? cu->get_builder ()->get_global_symbols ()
22395 : cu->list_in_scope);
22396
22397 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22398 }
22399 else if (!die_is_declaration (die, cu))
22400 {
22401 /* Use the default LOC_OPTIMIZED_OUT class. */
22402 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22403 if (!suppress_add)
22404 list_to_add = cu->list_in_scope;
22405 }
22406 }
22407 break;
22408 case DW_TAG_formal_parameter:
22409 {
22410 /* If we are inside a function, mark this as an argument. If
22411 not, we might be looking at an argument to an inlined function
22412 when we do not have enough information to show inlined frames;
22413 pretend it's a local variable in that case so that the user can
22414 still see it. */
22415 struct context_stack *curr
22416 = cu->get_builder ()->get_current_context_stack ();
22417 if (curr != nullptr && curr->name != nullptr)
22418 SYMBOL_IS_ARGUMENT (sym) = 1;
22419 attr = dwarf2_attr (die, DW_AT_location, cu);
22420 if (attr != nullptr)
22421 {
22422 var_decode_location (attr, sym, cu);
22423 }
22424 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22425 if (attr != nullptr)
22426 {
22427 dwarf2_const_value (attr, sym, cu);
22428 }
22429
22430 list_to_add = cu->list_in_scope;
22431 }
22432 break;
22433 case DW_TAG_unspecified_parameters:
22434 /* From varargs functions; gdb doesn't seem to have any
22435 interest in this information, so just ignore it for now.
22436 (FIXME?) */
22437 break;
22438 case DW_TAG_template_type_param:
22439 suppress_add = 1;
22440 /* Fall through. */
22441 case DW_TAG_class_type:
22442 case DW_TAG_interface_type:
22443 case DW_TAG_structure_type:
22444 case DW_TAG_union_type:
22445 case DW_TAG_set_type:
22446 case DW_TAG_enumeration_type:
22447 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22448 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22449
22450 {
22451 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22452 really ever be static objects: otherwise, if you try
22453 to, say, break of a class's method and you're in a file
22454 which doesn't mention that class, it won't work unless
22455 the check for all static symbols in lookup_symbol_aux
22456 saves you. See the OtherFileClass tests in
22457 gdb.c++/namespace.exp. */
22458
22459 if (!suppress_add)
22460 {
22461 buildsym_compunit *builder = cu->get_builder ();
22462 list_to_add
22463 = (cu->list_in_scope == builder->get_file_symbols ()
22464 && cu->language == language_cplus
22465 ? builder->get_global_symbols ()
22466 : cu->list_in_scope);
22467
22468 /* The semantics of C++ state that "struct foo {
22469 ... }" also defines a typedef for "foo". */
22470 if (cu->language == language_cplus
22471 || cu->language == language_ada
22472 || cu->language == language_d
22473 || cu->language == language_rust)
22474 {
22475 /* The symbol's name is already allocated along
22476 with this objfile, so we don't need to
22477 duplicate it for the type. */
22478 if (SYMBOL_TYPE (sym)->name () == 0)
22479 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22480 }
22481 }
22482 }
22483 break;
22484 case DW_TAG_typedef:
22485 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22486 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22487 list_to_add = cu->list_in_scope;
22488 break;
22489 case DW_TAG_array_type:
22490 case DW_TAG_base_type:
22491 case DW_TAG_subrange_type:
22492 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22493 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22494 list_to_add = cu->list_in_scope;
22495 break;
22496 case DW_TAG_enumerator:
22497 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22498 if (attr != nullptr)
22499 {
22500 dwarf2_const_value (attr, sym, cu);
22501 }
22502 {
22503 /* NOTE: carlton/2003-11-10: See comment above in the
22504 DW_TAG_class_type, etc. block. */
22505
22506 list_to_add
22507 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22508 && cu->language == language_cplus
22509 ? cu->get_builder ()->get_global_symbols ()
22510 : cu->list_in_scope);
22511 }
22512 break;
22513 case DW_TAG_imported_declaration:
22514 case DW_TAG_namespace:
22515 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22516 list_to_add = cu->get_builder ()->get_global_symbols ();
22517 break;
22518 case DW_TAG_module:
22519 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22520 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22521 list_to_add = cu->get_builder ()->get_global_symbols ();
22522 break;
22523 case DW_TAG_common_block:
22524 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22525 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22526 add_symbol_to_list (sym, cu->list_in_scope);
22527 break;
22528 default:
22529 /* Not a tag we recognize. Hopefully we aren't processing
22530 trash data, but since we must specifically ignore things
22531 we don't recognize, there is nothing else we should do at
22532 this point. */
22533 complaint (_("unsupported tag: '%s'"),
22534 dwarf_tag_name (die->tag));
22535 break;
22536 }
22537
22538 if (suppress_add)
22539 {
22540 sym->hash_next = objfile->template_symbols;
22541 objfile->template_symbols = sym;
22542 list_to_add = NULL;
22543 }
22544
22545 if (list_to_add != NULL)
22546 add_symbol_to_list (sym, list_to_add);
22547
22548 /* For the benefit of old versions of GCC, check for anonymous
22549 namespaces based on the demangled name. */
22550 if (!cu->processing_has_namespace_info
22551 && cu->language == language_cplus)
22552 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22553 }
22554 return (sym);
22555 }
22556
22557 /* Given an attr with a DW_FORM_dataN value in host byte order,
22558 zero-extend it as appropriate for the symbol's type. The DWARF
22559 standard (v4) is not entirely clear about the meaning of using
22560 DW_FORM_dataN for a constant with a signed type, where the type is
22561 wider than the data. The conclusion of a discussion on the DWARF
22562 list was that this is unspecified. We choose to always zero-extend
22563 because that is the interpretation long in use by GCC. */
22564
22565 static gdb_byte *
22566 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22567 struct dwarf2_cu *cu, LONGEST *value, int bits)
22568 {
22569 struct objfile *objfile = cu->per_objfile->objfile;
22570 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22571 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22572 LONGEST l = attr->constant_value (0);
22573
22574 if (bits < sizeof (*value) * 8)
22575 {
22576 l &= ((LONGEST) 1 << bits) - 1;
22577 *value = l;
22578 }
22579 else if (bits == sizeof (*value) * 8)
22580 *value = l;
22581 else
22582 {
22583 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22584 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22585 return bytes;
22586 }
22587
22588 return NULL;
22589 }
22590
22591 /* Read a constant value from an attribute. Either set *VALUE, or if
22592 the value does not fit in *VALUE, set *BYTES - either already
22593 allocated on the objfile obstack, or newly allocated on OBSTACK,
22594 or, set *BATON, if we translated the constant to a location
22595 expression. */
22596
22597 static void
22598 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22599 const char *name, struct obstack *obstack,
22600 struct dwarf2_cu *cu,
22601 LONGEST *value, const gdb_byte **bytes,
22602 struct dwarf2_locexpr_baton **baton)
22603 {
22604 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22605 struct objfile *objfile = per_objfile->objfile;
22606 struct comp_unit_head *cu_header = &cu->header;
22607 struct dwarf_block *blk;
22608 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22609 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22610
22611 *value = 0;
22612 *bytes = NULL;
22613 *baton = NULL;
22614
22615 switch (attr->form)
22616 {
22617 case DW_FORM_addr:
22618 case DW_FORM_addrx:
22619 case DW_FORM_GNU_addr_index:
22620 {
22621 gdb_byte *data;
22622
22623 if (TYPE_LENGTH (type) != cu_header->addr_size)
22624 dwarf2_const_value_length_mismatch_complaint (name,
22625 cu_header->addr_size,
22626 TYPE_LENGTH (type));
22627 /* Symbols of this form are reasonably rare, so we just
22628 piggyback on the existing location code rather than writing
22629 a new implementation of symbol_computed_ops. */
22630 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22631 (*baton)->per_objfile = per_objfile;
22632 (*baton)->per_cu = cu->per_cu;
22633 gdb_assert ((*baton)->per_cu);
22634
22635 (*baton)->size = 2 + cu_header->addr_size;
22636 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22637 (*baton)->data = data;
22638
22639 data[0] = DW_OP_addr;
22640 store_unsigned_integer (&data[1], cu_header->addr_size,
22641 byte_order, attr->as_address ());
22642 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22643 }
22644 break;
22645 case DW_FORM_string:
22646 case DW_FORM_strp:
22647 case DW_FORM_strx:
22648 case DW_FORM_GNU_str_index:
22649 case DW_FORM_GNU_strp_alt:
22650 /* The string is already allocated on the objfile obstack, point
22651 directly to it. */
22652 *bytes = (const gdb_byte *) attr->as_string ();
22653 break;
22654 case DW_FORM_block1:
22655 case DW_FORM_block2:
22656 case DW_FORM_block4:
22657 case DW_FORM_block:
22658 case DW_FORM_exprloc:
22659 case DW_FORM_data16:
22660 blk = attr->as_block ();
22661 if (TYPE_LENGTH (type) != blk->size)
22662 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22663 TYPE_LENGTH (type));
22664 *bytes = blk->data;
22665 break;
22666
22667 /* The DW_AT_const_value attributes are supposed to carry the
22668 symbol's value "represented as it would be on the target
22669 architecture." By the time we get here, it's already been
22670 converted to host endianness, so we just need to sign- or
22671 zero-extend it as appropriate. */
22672 case DW_FORM_data1:
22673 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22674 break;
22675 case DW_FORM_data2:
22676 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22677 break;
22678 case DW_FORM_data4:
22679 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22680 break;
22681 case DW_FORM_data8:
22682 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22683 break;
22684
22685 case DW_FORM_sdata:
22686 case DW_FORM_implicit_const:
22687 *value = attr->as_signed ();
22688 break;
22689
22690 case DW_FORM_udata:
22691 *value = attr->as_unsigned ();
22692 break;
22693
22694 default:
22695 complaint (_("unsupported const value attribute form: '%s'"),
22696 dwarf_form_name (attr->form));
22697 *value = 0;
22698 break;
22699 }
22700 }
22701
22702
22703 /* Copy constant value from an attribute to a symbol. */
22704
22705 static void
22706 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22707 struct dwarf2_cu *cu)
22708 {
22709 struct objfile *objfile = cu->per_objfile->objfile;
22710 LONGEST value;
22711 const gdb_byte *bytes;
22712 struct dwarf2_locexpr_baton *baton;
22713
22714 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22715 sym->print_name (),
22716 &objfile->objfile_obstack, cu,
22717 &value, &bytes, &baton);
22718
22719 if (baton != NULL)
22720 {
22721 SYMBOL_LOCATION_BATON (sym) = baton;
22722 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22723 }
22724 else if (bytes != NULL)
22725 {
22726 SYMBOL_VALUE_BYTES (sym) = bytes;
22727 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22728 }
22729 else
22730 {
22731 SYMBOL_VALUE (sym) = value;
22732 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22733 }
22734 }
22735
22736 /* Return the type of the die in question using its DW_AT_type attribute. */
22737
22738 static struct type *
22739 die_type (struct die_info *die, struct dwarf2_cu *cu)
22740 {
22741 struct attribute *type_attr;
22742
22743 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22744 if (!type_attr)
22745 {
22746 struct objfile *objfile = cu->per_objfile->objfile;
22747 /* A missing DW_AT_type represents a void type. */
22748 return objfile_type (objfile)->builtin_void;
22749 }
22750
22751 return lookup_die_type (die, type_attr, cu);
22752 }
22753
22754 /* True iff CU's producer generates GNAT Ada auxiliary information
22755 that allows to find parallel types through that information instead
22756 of having to do expensive parallel lookups by type name. */
22757
22758 static int
22759 need_gnat_info (struct dwarf2_cu *cu)
22760 {
22761 /* Assume that the Ada compiler was GNAT, which always produces
22762 the auxiliary information. */
22763 return (cu->language == language_ada);
22764 }
22765
22766 /* Return the auxiliary type of the die in question using its
22767 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22768 attribute is not present. */
22769
22770 static struct type *
22771 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22772 {
22773 struct attribute *type_attr;
22774
22775 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22776 if (!type_attr)
22777 return NULL;
22778
22779 return lookup_die_type (die, type_attr, cu);
22780 }
22781
22782 /* If DIE has a descriptive_type attribute, then set the TYPE's
22783 descriptive type accordingly. */
22784
22785 static void
22786 set_descriptive_type (struct type *type, struct die_info *die,
22787 struct dwarf2_cu *cu)
22788 {
22789 struct type *descriptive_type = die_descriptive_type (die, cu);
22790
22791 if (descriptive_type)
22792 {
22793 ALLOCATE_GNAT_AUX_TYPE (type);
22794 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22795 }
22796 }
22797
22798 /* Return the containing type of the die in question using its
22799 DW_AT_containing_type attribute. */
22800
22801 static struct type *
22802 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22803 {
22804 struct attribute *type_attr;
22805 struct objfile *objfile = cu->per_objfile->objfile;
22806
22807 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22808 if (!type_attr)
22809 error (_("Dwarf Error: Problem turning containing type into gdb type "
22810 "[in module %s]"), objfile_name (objfile));
22811
22812 return lookup_die_type (die, type_attr, cu);
22813 }
22814
22815 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22816
22817 static struct type *
22818 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22819 {
22820 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22821 struct objfile *objfile = per_objfile->objfile;
22822 char *saved;
22823
22824 std::string message
22825 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22826 objfile_name (objfile),
22827 sect_offset_str (cu->header.sect_off),
22828 sect_offset_str (die->sect_off));
22829 saved = obstack_strdup (&objfile->objfile_obstack, message);
22830
22831 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22832 }
22833
22834 /* Look up the type of DIE in CU using its type attribute ATTR.
22835 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22836 DW_AT_containing_type.
22837 If there is no type substitute an error marker. */
22838
22839 static struct type *
22840 lookup_die_type (struct die_info *die, const struct attribute *attr,
22841 struct dwarf2_cu *cu)
22842 {
22843 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22844 struct objfile *objfile = per_objfile->objfile;
22845 struct type *this_type;
22846
22847 gdb_assert (attr->name == DW_AT_type
22848 || attr->name == DW_AT_GNAT_descriptive_type
22849 || attr->name == DW_AT_containing_type);
22850
22851 /* First see if we have it cached. */
22852
22853 if (attr->form == DW_FORM_GNU_ref_alt)
22854 {
22855 struct dwarf2_per_cu_data *per_cu;
22856 sect_offset sect_off = attr->get_ref_die_offset ();
22857
22858 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22859 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22860 }
22861 else if (attr->form_is_ref ())
22862 {
22863 sect_offset sect_off = attr->get_ref_die_offset ();
22864
22865 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22866 }
22867 else if (attr->form == DW_FORM_ref_sig8)
22868 {
22869 ULONGEST signature = attr->as_signature ();
22870
22871 return get_signatured_type (die, signature, cu);
22872 }
22873 else
22874 {
22875 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22876 " at %s [in module %s]"),
22877 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22878 objfile_name (objfile));
22879 return build_error_marker_type (cu, die);
22880 }
22881
22882 /* If not cached we need to read it in. */
22883
22884 if (this_type == NULL)
22885 {
22886 struct die_info *type_die = NULL;
22887 struct dwarf2_cu *type_cu = cu;
22888
22889 if (attr->form_is_ref ())
22890 type_die = follow_die_ref (die, attr, &type_cu);
22891 if (type_die == NULL)
22892 return build_error_marker_type (cu, die);
22893 /* If we find the type now, it's probably because the type came
22894 from an inter-CU reference and the type's CU got expanded before
22895 ours. */
22896 this_type = read_type_die (type_die, type_cu);
22897 }
22898
22899 /* If we still don't have a type use an error marker. */
22900
22901 if (this_type == NULL)
22902 return build_error_marker_type (cu, die);
22903
22904 return this_type;
22905 }
22906
22907 /* Return the type in DIE, CU.
22908 Returns NULL for invalid types.
22909
22910 This first does a lookup in die_type_hash,
22911 and only reads the die in if necessary.
22912
22913 NOTE: This can be called when reading in partial or full symbols. */
22914
22915 static struct type *
22916 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22917 {
22918 struct type *this_type;
22919
22920 this_type = get_die_type (die, cu);
22921 if (this_type)
22922 return this_type;
22923
22924 return read_type_die_1 (die, cu);
22925 }
22926
22927 /* Read the type in DIE, CU.
22928 Returns NULL for invalid types. */
22929
22930 static struct type *
22931 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22932 {
22933 struct type *this_type = NULL;
22934
22935 switch (die->tag)
22936 {
22937 case DW_TAG_class_type:
22938 case DW_TAG_interface_type:
22939 case DW_TAG_structure_type:
22940 case DW_TAG_union_type:
22941 this_type = read_structure_type (die, cu);
22942 break;
22943 case DW_TAG_enumeration_type:
22944 this_type = read_enumeration_type (die, cu);
22945 break;
22946 case DW_TAG_subprogram:
22947 case DW_TAG_subroutine_type:
22948 case DW_TAG_inlined_subroutine:
22949 this_type = read_subroutine_type (die, cu);
22950 break;
22951 case DW_TAG_array_type:
22952 this_type = read_array_type (die, cu);
22953 break;
22954 case DW_TAG_set_type:
22955 this_type = read_set_type (die, cu);
22956 break;
22957 case DW_TAG_pointer_type:
22958 this_type = read_tag_pointer_type (die, cu);
22959 break;
22960 case DW_TAG_ptr_to_member_type:
22961 this_type = read_tag_ptr_to_member_type (die, cu);
22962 break;
22963 case DW_TAG_reference_type:
22964 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22965 break;
22966 case DW_TAG_rvalue_reference_type:
22967 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22968 break;
22969 case DW_TAG_const_type:
22970 this_type = read_tag_const_type (die, cu);
22971 break;
22972 case DW_TAG_volatile_type:
22973 this_type = read_tag_volatile_type (die, cu);
22974 break;
22975 case DW_TAG_restrict_type:
22976 this_type = read_tag_restrict_type (die, cu);
22977 break;
22978 case DW_TAG_string_type:
22979 this_type = read_tag_string_type (die, cu);
22980 break;
22981 case DW_TAG_typedef:
22982 this_type = read_typedef (die, cu);
22983 break;
22984 case DW_TAG_subrange_type:
22985 this_type = read_subrange_type (die, cu);
22986 break;
22987 case DW_TAG_base_type:
22988 this_type = read_base_type (die, cu);
22989 break;
22990 case DW_TAG_unspecified_type:
22991 this_type = read_unspecified_type (die, cu);
22992 break;
22993 case DW_TAG_namespace:
22994 this_type = read_namespace_type (die, cu);
22995 break;
22996 case DW_TAG_module:
22997 this_type = read_module_type (die, cu);
22998 break;
22999 case DW_TAG_atomic_type:
23000 this_type = read_tag_atomic_type (die, cu);
23001 break;
23002 default:
23003 complaint (_("unexpected tag in read_type_die: '%s'"),
23004 dwarf_tag_name (die->tag));
23005 break;
23006 }
23007
23008 return this_type;
23009 }
23010
23011 /* See if we can figure out if the class lives in a namespace. We do
23012 this by looking for a member function; its demangled name will
23013 contain namespace info, if there is any.
23014 Return the computed name or NULL.
23015 Space for the result is allocated on the objfile's obstack.
23016 This is the full-die version of guess_partial_die_structure_name.
23017 In this case we know DIE has no useful parent. */
23018
23019 static const char *
23020 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23021 {
23022 struct die_info *spec_die;
23023 struct dwarf2_cu *spec_cu;
23024 struct die_info *child;
23025 struct objfile *objfile = cu->per_objfile->objfile;
23026
23027 spec_cu = cu;
23028 spec_die = die_specification (die, &spec_cu);
23029 if (spec_die != NULL)
23030 {
23031 die = spec_die;
23032 cu = spec_cu;
23033 }
23034
23035 for (child = die->child;
23036 child != NULL;
23037 child = child->sibling)
23038 {
23039 if (child->tag == DW_TAG_subprogram)
23040 {
23041 const char *linkage_name = dw2_linkage_name (child, cu);
23042
23043 if (linkage_name != NULL)
23044 {
23045 gdb::unique_xmalloc_ptr<char> actual_name
23046 (cu->language_defn->class_name_from_physname (linkage_name));
23047 const char *name = NULL;
23048
23049 if (actual_name != NULL)
23050 {
23051 const char *die_name = dwarf2_name (die, cu);
23052
23053 if (die_name != NULL
23054 && strcmp (die_name, actual_name.get ()) != 0)
23055 {
23056 /* Strip off the class name from the full name.
23057 We want the prefix. */
23058 int die_name_len = strlen (die_name);
23059 int actual_name_len = strlen (actual_name.get ());
23060 const char *ptr = actual_name.get ();
23061
23062 /* Test for '::' as a sanity check. */
23063 if (actual_name_len > die_name_len + 2
23064 && ptr[actual_name_len - die_name_len - 1] == ':')
23065 name = obstack_strndup (
23066 &objfile->per_bfd->storage_obstack,
23067 ptr, actual_name_len - die_name_len - 2);
23068 }
23069 }
23070 return name;
23071 }
23072 }
23073 }
23074
23075 return NULL;
23076 }
23077
23078 /* GCC might emit a nameless typedef that has a linkage name. Determine the
23079 prefix part in such case. See
23080 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23081
23082 static const char *
23083 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23084 {
23085 struct attribute *attr;
23086 const char *base;
23087
23088 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23089 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23090 return NULL;
23091
23092 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
23093 return NULL;
23094
23095 attr = dw2_linkage_name_attr (die, cu);
23096 const char *attr_name = attr->as_string ();
23097 if (attr == NULL || attr_name == NULL)
23098 return NULL;
23099
23100 /* dwarf2_name had to be already called. */
23101 gdb_assert (attr->canonical_string_p ());
23102
23103 /* Strip the base name, keep any leading namespaces/classes. */
23104 base = strrchr (attr_name, ':');
23105 if (base == NULL || base == attr_name || base[-1] != ':')
23106 return "";
23107
23108 struct objfile *objfile = cu->per_objfile->objfile;
23109 return obstack_strndup (&objfile->per_bfd->storage_obstack,
23110 attr_name,
23111 &base[-1] - attr_name);
23112 }
23113
23114 /* Return the name of the namespace/class that DIE is defined within,
23115 or "" if we can't tell. The caller should not xfree the result.
23116
23117 For example, if we're within the method foo() in the following
23118 code:
23119
23120 namespace N {
23121 class C {
23122 void foo () {
23123 }
23124 };
23125 }
23126
23127 then determine_prefix on foo's die will return "N::C". */
23128
23129 static const char *
23130 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23131 {
23132 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23133 struct die_info *parent, *spec_die;
23134 struct dwarf2_cu *spec_cu;
23135 struct type *parent_type;
23136 const char *retval;
23137
23138 if (cu->language != language_cplus
23139 && cu->language != language_fortran && cu->language != language_d
23140 && cu->language != language_rust)
23141 return "";
23142
23143 retval = anonymous_struct_prefix (die, cu);
23144 if (retval)
23145 return retval;
23146
23147 /* We have to be careful in the presence of DW_AT_specification.
23148 For example, with GCC 3.4, given the code
23149
23150 namespace N {
23151 void foo() {
23152 // Definition of N::foo.
23153 }
23154 }
23155
23156 then we'll have a tree of DIEs like this:
23157
23158 1: DW_TAG_compile_unit
23159 2: DW_TAG_namespace // N
23160 3: DW_TAG_subprogram // declaration of N::foo
23161 4: DW_TAG_subprogram // definition of N::foo
23162 DW_AT_specification // refers to die #3
23163
23164 Thus, when processing die #4, we have to pretend that we're in
23165 the context of its DW_AT_specification, namely the contex of die
23166 #3. */
23167 spec_cu = cu;
23168 spec_die = die_specification (die, &spec_cu);
23169 if (spec_die == NULL)
23170 parent = die->parent;
23171 else
23172 {
23173 parent = spec_die->parent;
23174 cu = spec_cu;
23175 }
23176
23177 if (parent == NULL)
23178 return "";
23179 else if (parent->building_fullname)
23180 {
23181 const char *name;
23182 const char *parent_name;
23183
23184 /* It has been seen on RealView 2.2 built binaries,
23185 DW_TAG_template_type_param types actually _defined_ as
23186 children of the parent class:
23187
23188 enum E {};
23189 template class <class Enum> Class{};
23190 Class<enum E> class_e;
23191
23192 1: DW_TAG_class_type (Class)
23193 2: DW_TAG_enumeration_type (E)
23194 3: DW_TAG_enumerator (enum1:0)
23195 3: DW_TAG_enumerator (enum2:1)
23196 ...
23197 2: DW_TAG_template_type_param
23198 DW_AT_type DW_FORM_ref_udata (E)
23199
23200 Besides being broken debug info, it can put GDB into an
23201 infinite loop. Consider:
23202
23203 When we're building the full name for Class<E>, we'll start
23204 at Class, and go look over its template type parameters,
23205 finding E. We'll then try to build the full name of E, and
23206 reach here. We're now trying to build the full name of E,
23207 and look over the parent DIE for containing scope. In the
23208 broken case, if we followed the parent DIE of E, we'd again
23209 find Class, and once again go look at its template type
23210 arguments, etc., etc. Simply don't consider such parent die
23211 as source-level parent of this die (it can't be, the language
23212 doesn't allow it), and break the loop here. */
23213 name = dwarf2_name (die, cu);
23214 parent_name = dwarf2_name (parent, cu);
23215 complaint (_("template param type '%s' defined within parent '%s'"),
23216 name ? name : "<unknown>",
23217 parent_name ? parent_name : "<unknown>");
23218 return "";
23219 }
23220 else
23221 switch (parent->tag)
23222 {
23223 case DW_TAG_namespace:
23224 parent_type = read_type_die (parent, cu);
23225 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23226 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23227 Work around this problem here. */
23228 if (cu->language == language_cplus
23229 && strcmp (parent_type->name (), "::") == 0)
23230 return "";
23231 /* We give a name to even anonymous namespaces. */
23232 return parent_type->name ();
23233 case DW_TAG_class_type:
23234 case DW_TAG_interface_type:
23235 case DW_TAG_structure_type:
23236 case DW_TAG_union_type:
23237 case DW_TAG_module:
23238 parent_type = read_type_die (parent, cu);
23239 if (parent_type->name () != NULL)
23240 return parent_type->name ();
23241 else
23242 /* An anonymous structure is only allowed non-static data
23243 members; no typedefs, no member functions, et cetera.
23244 So it does not need a prefix. */
23245 return "";
23246 case DW_TAG_compile_unit:
23247 case DW_TAG_partial_unit:
23248 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23249 if (cu->language == language_cplus
23250 && !per_objfile->per_bfd->types.empty ()
23251 && die->child != NULL
23252 && (die->tag == DW_TAG_class_type
23253 || die->tag == DW_TAG_structure_type
23254 || die->tag == DW_TAG_union_type))
23255 {
23256 const char *name = guess_full_die_structure_name (die, cu);
23257 if (name != NULL)
23258 return name;
23259 }
23260 return "";
23261 case DW_TAG_subprogram:
23262 /* Nested subroutines in Fortran get a prefix with the name
23263 of the parent's subroutine. */
23264 if (cu->language == language_fortran)
23265 {
23266 if ((die->tag == DW_TAG_subprogram)
23267 && (dwarf2_name (parent, cu) != NULL))
23268 return dwarf2_name (parent, cu);
23269 }
23270 return determine_prefix (parent, cu);
23271 case DW_TAG_enumeration_type:
23272 parent_type = read_type_die (parent, cu);
23273 if (TYPE_DECLARED_CLASS (parent_type))
23274 {
23275 if (parent_type->name () != NULL)
23276 return parent_type->name ();
23277 return "";
23278 }
23279 /* Fall through. */
23280 default:
23281 return determine_prefix (parent, cu);
23282 }
23283 }
23284
23285 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23286 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23287 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23288 an obconcat, otherwise allocate storage for the result. The CU argument is
23289 used to determine the language and hence, the appropriate separator. */
23290
23291 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23292
23293 static char *
23294 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23295 int physname, struct dwarf2_cu *cu)
23296 {
23297 const char *lead = "";
23298 const char *sep;
23299
23300 if (suffix == NULL || suffix[0] == '\0'
23301 || prefix == NULL || prefix[0] == '\0')
23302 sep = "";
23303 else if (cu->language == language_d)
23304 {
23305 /* For D, the 'main' function could be defined in any module, but it
23306 should never be prefixed. */
23307 if (strcmp (suffix, "D main") == 0)
23308 {
23309 prefix = "";
23310 sep = "";
23311 }
23312 else
23313 sep = ".";
23314 }
23315 else if (cu->language == language_fortran && physname)
23316 {
23317 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23318 DW_AT_MIPS_linkage_name is preferred and used instead. */
23319
23320 lead = "__";
23321 sep = "_MOD_";
23322 }
23323 else
23324 sep = "::";
23325
23326 if (prefix == NULL)
23327 prefix = "";
23328 if (suffix == NULL)
23329 suffix = "";
23330
23331 if (obs == NULL)
23332 {
23333 char *retval
23334 = ((char *)
23335 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23336
23337 strcpy (retval, lead);
23338 strcat (retval, prefix);
23339 strcat (retval, sep);
23340 strcat (retval, suffix);
23341 return retval;
23342 }
23343 else
23344 {
23345 /* We have an obstack. */
23346 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23347 }
23348 }
23349
23350 /* Get name of a die, return NULL if not found. */
23351
23352 static const char *
23353 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23354 struct objfile *objfile)
23355 {
23356 if (name && cu->language == language_cplus)
23357 {
23358 gdb::unique_xmalloc_ptr<char> canon_name
23359 = cp_canonicalize_string (name);
23360
23361 if (canon_name != nullptr)
23362 name = objfile->intern (canon_name.get ());
23363 }
23364
23365 return name;
23366 }
23367
23368 /* Get name of a die, return NULL if not found.
23369 Anonymous namespaces are converted to their magic string. */
23370
23371 static const char *
23372 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23373 {
23374 struct attribute *attr;
23375 struct objfile *objfile = cu->per_objfile->objfile;
23376
23377 attr = dwarf2_attr (die, DW_AT_name, cu);
23378 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23379 if (attr_name == nullptr
23380 && die->tag != DW_TAG_namespace
23381 && die->tag != DW_TAG_class_type
23382 && die->tag != DW_TAG_interface_type
23383 && die->tag != DW_TAG_structure_type
23384 && die->tag != DW_TAG_union_type)
23385 return NULL;
23386
23387 switch (die->tag)
23388 {
23389 case DW_TAG_compile_unit:
23390 case DW_TAG_partial_unit:
23391 /* Compilation units have a DW_AT_name that is a filename, not
23392 a source language identifier. */
23393 case DW_TAG_enumeration_type:
23394 case DW_TAG_enumerator:
23395 /* These tags always have simple identifiers already; no need
23396 to canonicalize them. */
23397 return attr_name;
23398
23399 case DW_TAG_namespace:
23400 if (attr_name != nullptr)
23401 return attr_name;
23402 return CP_ANONYMOUS_NAMESPACE_STR;
23403
23404 case DW_TAG_class_type:
23405 case DW_TAG_interface_type:
23406 case DW_TAG_structure_type:
23407 case DW_TAG_union_type:
23408 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23409 structures or unions. These were of the form "._%d" in GCC 4.1,
23410 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23411 and GCC 4.4. We work around this problem by ignoring these. */
23412 if (attr_name != nullptr
23413 && (startswith (attr_name, "._")
23414 || startswith (attr_name, "<anonymous")))
23415 return NULL;
23416
23417 /* GCC might emit a nameless typedef that has a linkage name. See
23418 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23419 if (!attr || attr_name == NULL)
23420 {
23421 attr = dw2_linkage_name_attr (die, cu);
23422 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23423 if (attr == NULL || attr_name == NULL)
23424 return NULL;
23425
23426 /* Avoid demangling attr_name the second time on a second
23427 call for the same DIE. */
23428 if (!attr->canonical_string_p ())
23429 {
23430 gdb::unique_xmalloc_ptr<char> demangled
23431 (gdb_demangle (attr_name, DMGL_TYPES));
23432 if (demangled == nullptr)
23433 return nullptr;
23434
23435 attr->set_string_canonical (objfile->intern (demangled.get ()));
23436 attr_name = attr->as_string ();
23437 }
23438
23439 /* Strip any leading namespaces/classes, keep only the
23440 base name. DW_AT_name for named DIEs does not
23441 contain the prefixes. */
23442 const char *base = strrchr (attr_name, ':');
23443 if (base && base > attr_name && base[-1] == ':')
23444 return &base[1];
23445 else
23446 return attr_name;
23447 }
23448 break;
23449
23450 default:
23451 break;
23452 }
23453
23454 if (!attr->canonical_string_p ())
23455 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23456 objfile));
23457 return attr->as_string ();
23458 }
23459
23460 /* Return the die that this die in an extension of, or NULL if there
23461 is none. *EXT_CU is the CU containing DIE on input, and the CU
23462 containing the return value on output. */
23463
23464 static struct die_info *
23465 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23466 {
23467 struct attribute *attr;
23468
23469 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23470 if (attr == NULL)
23471 return NULL;
23472
23473 return follow_die_ref (die, attr, ext_cu);
23474 }
23475
23476 static void
23477 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23478 {
23479 unsigned int i;
23480
23481 print_spaces (indent, f);
23482 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23483 dwarf_tag_name (die->tag), die->abbrev,
23484 sect_offset_str (die->sect_off));
23485
23486 if (die->parent != NULL)
23487 {
23488 print_spaces (indent, f);
23489 fprintf_unfiltered (f, " parent at offset: %s\n",
23490 sect_offset_str (die->parent->sect_off));
23491 }
23492
23493 print_spaces (indent, f);
23494 fprintf_unfiltered (f, " has children: %s\n",
23495 dwarf_bool_name (die->child != NULL));
23496
23497 print_spaces (indent, f);
23498 fprintf_unfiltered (f, " attributes:\n");
23499
23500 for (i = 0; i < die->num_attrs; ++i)
23501 {
23502 print_spaces (indent, f);
23503 fprintf_unfiltered (f, " %s (%s) ",
23504 dwarf_attr_name (die->attrs[i].name),
23505 dwarf_form_name (die->attrs[i].form));
23506
23507 switch (die->attrs[i].form)
23508 {
23509 case DW_FORM_addr:
23510 case DW_FORM_addrx:
23511 case DW_FORM_GNU_addr_index:
23512 fprintf_unfiltered (f, "address: ");
23513 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23514 break;
23515 case DW_FORM_block2:
23516 case DW_FORM_block4:
23517 case DW_FORM_block:
23518 case DW_FORM_block1:
23519 fprintf_unfiltered (f, "block: size %s",
23520 pulongest (die->attrs[i].as_block ()->size));
23521 break;
23522 case DW_FORM_exprloc:
23523 fprintf_unfiltered (f, "expression: size %s",
23524 pulongest (die->attrs[i].as_block ()->size));
23525 break;
23526 case DW_FORM_data16:
23527 fprintf_unfiltered (f, "constant of 16 bytes");
23528 break;
23529 case DW_FORM_ref_addr:
23530 fprintf_unfiltered (f, "ref address: ");
23531 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23532 break;
23533 case DW_FORM_GNU_ref_alt:
23534 fprintf_unfiltered (f, "alt ref address: ");
23535 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23536 break;
23537 case DW_FORM_ref1:
23538 case DW_FORM_ref2:
23539 case DW_FORM_ref4:
23540 case DW_FORM_ref8:
23541 case DW_FORM_ref_udata:
23542 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23543 (long) (die->attrs[i].as_unsigned ()));
23544 break;
23545 case DW_FORM_data1:
23546 case DW_FORM_data2:
23547 case DW_FORM_data4:
23548 case DW_FORM_data8:
23549 case DW_FORM_udata:
23550 fprintf_unfiltered (f, "constant: %s",
23551 pulongest (die->attrs[i].as_unsigned ()));
23552 break;
23553 case DW_FORM_sec_offset:
23554 fprintf_unfiltered (f, "section offset: %s",
23555 pulongest (die->attrs[i].as_unsigned ()));
23556 break;
23557 case DW_FORM_ref_sig8:
23558 fprintf_unfiltered (f, "signature: %s",
23559 hex_string (die->attrs[i].as_signature ()));
23560 break;
23561 case DW_FORM_string:
23562 case DW_FORM_strp:
23563 case DW_FORM_line_strp:
23564 case DW_FORM_strx:
23565 case DW_FORM_GNU_str_index:
23566 case DW_FORM_GNU_strp_alt:
23567 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23568 die->attrs[i].as_string ()
23569 ? die->attrs[i].as_string () : "",
23570 die->attrs[i].canonical_string_p () ? "is" : "not");
23571 break;
23572 case DW_FORM_flag:
23573 if (die->attrs[i].as_boolean ())
23574 fprintf_unfiltered (f, "flag: TRUE");
23575 else
23576 fprintf_unfiltered (f, "flag: FALSE");
23577 break;
23578 case DW_FORM_flag_present:
23579 fprintf_unfiltered (f, "flag: TRUE");
23580 break;
23581 case DW_FORM_indirect:
23582 /* The reader will have reduced the indirect form to
23583 the "base form" so this form should not occur. */
23584 fprintf_unfiltered (f,
23585 "unexpected attribute form: DW_FORM_indirect");
23586 break;
23587 case DW_FORM_sdata:
23588 case DW_FORM_implicit_const:
23589 fprintf_unfiltered (f, "constant: %s",
23590 plongest (die->attrs[i].as_signed ()));
23591 break;
23592 default:
23593 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23594 die->attrs[i].form);
23595 break;
23596 }
23597 fprintf_unfiltered (f, "\n");
23598 }
23599 }
23600
23601 static void
23602 dump_die_for_error (struct die_info *die)
23603 {
23604 dump_die_shallow (gdb_stderr, 0, die);
23605 }
23606
23607 static void
23608 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23609 {
23610 int indent = level * 4;
23611
23612 gdb_assert (die != NULL);
23613
23614 if (level >= max_level)
23615 return;
23616
23617 dump_die_shallow (f, indent, die);
23618
23619 if (die->child != NULL)
23620 {
23621 print_spaces (indent, f);
23622 fprintf_unfiltered (f, " Children:");
23623 if (level + 1 < max_level)
23624 {
23625 fprintf_unfiltered (f, "\n");
23626 dump_die_1 (f, level + 1, max_level, die->child);
23627 }
23628 else
23629 {
23630 fprintf_unfiltered (f,
23631 " [not printed, max nesting level reached]\n");
23632 }
23633 }
23634
23635 if (die->sibling != NULL && level > 0)
23636 {
23637 dump_die_1 (f, level, max_level, die->sibling);
23638 }
23639 }
23640
23641 /* This is called from the pdie macro in gdbinit.in.
23642 It's not static so gcc will keep a copy callable from gdb. */
23643
23644 void
23645 dump_die (struct die_info *die, int max_level)
23646 {
23647 dump_die_1 (gdb_stdlog, 0, max_level, die);
23648 }
23649
23650 static void
23651 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23652 {
23653 void **slot;
23654
23655 slot = htab_find_slot_with_hash (cu->die_hash, die,
23656 to_underlying (die->sect_off),
23657 INSERT);
23658
23659 *slot = die;
23660 }
23661
23662 /* Follow reference or signature attribute ATTR of SRC_DIE.
23663 On entry *REF_CU is the CU of SRC_DIE.
23664 On exit *REF_CU is the CU of the result. */
23665
23666 static struct die_info *
23667 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23668 struct dwarf2_cu **ref_cu)
23669 {
23670 struct die_info *die;
23671
23672 if (attr->form_is_ref ())
23673 die = follow_die_ref (src_die, attr, ref_cu);
23674 else if (attr->form == DW_FORM_ref_sig8)
23675 die = follow_die_sig (src_die, attr, ref_cu);
23676 else
23677 {
23678 dump_die_for_error (src_die);
23679 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23680 objfile_name ((*ref_cu)->per_objfile->objfile));
23681 }
23682
23683 return die;
23684 }
23685
23686 /* Follow reference OFFSET.
23687 On entry *REF_CU is the CU of the source die referencing OFFSET.
23688 On exit *REF_CU is the CU of the result.
23689 Returns NULL if OFFSET is invalid. */
23690
23691 static struct die_info *
23692 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23693 struct dwarf2_cu **ref_cu)
23694 {
23695 struct die_info temp_die;
23696 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23697 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23698
23699 gdb_assert (cu->per_cu != NULL);
23700
23701 target_cu = cu;
23702
23703 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23704 "source CU contains target offset: %d",
23705 sect_offset_str (cu->per_cu->sect_off),
23706 sect_offset_str (sect_off),
23707 cu->header.offset_in_cu_p (sect_off));
23708
23709 if (cu->per_cu->is_debug_types)
23710 {
23711 /* .debug_types CUs cannot reference anything outside their CU.
23712 If they need to, they have to reference a signatured type via
23713 DW_FORM_ref_sig8. */
23714 if (!cu->header.offset_in_cu_p (sect_off))
23715 return NULL;
23716 }
23717 else if (offset_in_dwz != cu->per_cu->is_dwz
23718 || !cu->header.offset_in_cu_p (sect_off))
23719 {
23720 struct dwarf2_per_cu_data *per_cu;
23721
23722 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23723 per_objfile);
23724
23725 dwarf_read_debug_printf_v ("target CU offset: %s, "
23726 "target CU DIEs loaded: %d",
23727 sect_offset_str (per_cu->sect_off),
23728 per_objfile->get_cu (per_cu) != nullptr);
23729
23730 /* If necessary, add it to the queue and load its DIEs.
23731
23732 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23733 it doesn't mean they are currently loaded. Since we require them
23734 to be loaded, we must check for ourselves. */
23735 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23736 || per_objfile->get_cu (per_cu) == nullptr)
23737 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23738 false, cu->language);
23739
23740 target_cu = per_objfile->get_cu (per_cu);
23741 gdb_assert (target_cu != nullptr);
23742 }
23743 else if (cu->dies == NULL)
23744 {
23745 /* We're loading full DIEs during partial symbol reading. */
23746 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23747 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23748 language_minimal);
23749 }
23750
23751 *ref_cu = target_cu;
23752 temp_die.sect_off = sect_off;
23753
23754 if (target_cu != cu)
23755 target_cu->ancestor = cu;
23756
23757 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23758 &temp_die,
23759 to_underlying (sect_off));
23760 }
23761
23762 /* Follow reference attribute ATTR of SRC_DIE.
23763 On entry *REF_CU is the CU of SRC_DIE.
23764 On exit *REF_CU is the CU of the result. */
23765
23766 static struct die_info *
23767 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23768 struct dwarf2_cu **ref_cu)
23769 {
23770 sect_offset sect_off = attr->get_ref_die_offset ();
23771 struct dwarf2_cu *cu = *ref_cu;
23772 struct die_info *die;
23773
23774 die = follow_die_offset (sect_off,
23775 (attr->form == DW_FORM_GNU_ref_alt
23776 || cu->per_cu->is_dwz),
23777 ref_cu);
23778 if (!die)
23779 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23780 "at %s [in module %s]"),
23781 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23782 objfile_name (cu->per_objfile->objfile));
23783
23784 return die;
23785 }
23786
23787 /* See read.h. */
23788
23789 struct dwarf2_locexpr_baton
23790 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23791 dwarf2_per_cu_data *per_cu,
23792 dwarf2_per_objfile *per_objfile,
23793 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23794 bool resolve_abstract_p)
23795 {
23796 struct die_info *die;
23797 struct attribute *attr;
23798 struct dwarf2_locexpr_baton retval;
23799 struct objfile *objfile = per_objfile->objfile;
23800
23801 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23802 if (cu == nullptr)
23803 cu = load_cu (per_cu, per_objfile, false);
23804
23805 if (cu == nullptr)
23806 {
23807 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23808 Instead just throw an error, not much else we can do. */
23809 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23810 sect_offset_str (sect_off), objfile_name (objfile));
23811 }
23812
23813 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23814 if (!die)
23815 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23816 sect_offset_str (sect_off), objfile_name (objfile));
23817
23818 attr = dwarf2_attr (die, DW_AT_location, cu);
23819 if (!attr && resolve_abstract_p
23820 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23821 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23822 {
23823 CORE_ADDR pc = get_frame_pc ();
23824 CORE_ADDR baseaddr = objfile->text_section_offset ();
23825 struct gdbarch *gdbarch = objfile->arch ();
23826
23827 for (const auto &cand_off
23828 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23829 {
23830 struct dwarf2_cu *cand_cu = cu;
23831 struct die_info *cand
23832 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23833 if (!cand
23834 || !cand->parent
23835 || cand->parent->tag != DW_TAG_subprogram)
23836 continue;
23837
23838 CORE_ADDR pc_low, pc_high;
23839 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23840 if (pc_low == ((CORE_ADDR) -1))
23841 continue;
23842 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23843 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23844 if (!(pc_low <= pc && pc < pc_high))
23845 continue;
23846
23847 die = cand;
23848 attr = dwarf2_attr (die, DW_AT_location, cu);
23849 break;
23850 }
23851 }
23852
23853 if (!attr)
23854 {
23855 /* DWARF: "If there is no such attribute, then there is no effect.".
23856 DATA is ignored if SIZE is 0. */
23857
23858 retval.data = NULL;
23859 retval.size = 0;
23860 }
23861 else if (attr->form_is_section_offset ())
23862 {
23863 struct dwarf2_loclist_baton loclist_baton;
23864 CORE_ADDR pc = get_frame_pc ();
23865 size_t size;
23866
23867 fill_in_loclist_baton (cu, &loclist_baton, attr);
23868
23869 retval.data = dwarf2_find_location_expression (&loclist_baton,
23870 &size, pc);
23871 retval.size = size;
23872 }
23873 else
23874 {
23875 if (!attr->form_is_block ())
23876 error (_("Dwarf Error: DIE at %s referenced in module %s "
23877 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23878 sect_offset_str (sect_off), objfile_name (objfile));
23879
23880 struct dwarf_block *block = attr->as_block ();
23881 retval.data = block->data;
23882 retval.size = block->size;
23883 }
23884 retval.per_objfile = per_objfile;
23885 retval.per_cu = cu->per_cu;
23886
23887 per_objfile->age_comp_units ();
23888
23889 return retval;
23890 }
23891
23892 /* See read.h. */
23893
23894 struct dwarf2_locexpr_baton
23895 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23896 dwarf2_per_cu_data *per_cu,
23897 dwarf2_per_objfile *per_objfile,
23898 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23899 {
23900 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23901
23902 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23903 get_frame_pc);
23904 }
23905
23906 /* Write a constant of a given type as target-ordered bytes into
23907 OBSTACK. */
23908
23909 static const gdb_byte *
23910 write_constant_as_bytes (struct obstack *obstack,
23911 enum bfd_endian byte_order,
23912 struct type *type,
23913 ULONGEST value,
23914 LONGEST *len)
23915 {
23916 gdb_byte *result;
23917
23918 *len = TYPE_LENGTH (type);
23919 result = (gdb_byte *) obstack_alloc (obstack, *len);
23920 store_unsigned_integer (result, *len, byte_order, value);
23921
23922 return result;
23923 }
23924
23925 /* See read.h. */
23926
23927 const gdb_byte *
23928 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23929 dwarf2_per_cu_data *per_cu,
23930 dwarf2_per_objfile *per_objfile,
23931 obstack *obstack,
23932 LONGEST *len)
23933 {
23934 struct die_info *die;
23935 struct attribute *attr;
23936 const gdb_byte *result = NULL;
23937 struct type *type;
23938 LONGEST value;
23939 enum bfd_endian byte_order;
23940 struct objfile *objfile = per_objfile->objfile;
23941
23942 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23943 if (cu == nullptr)
23944 cu = load_cu (per_cu, per_objfile, false);
23945
23946 if (cu == nullptr)
23947 {
23948 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23949 Instead just throw an error, not much else we can do. */
23950 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23951 sect_offset_str (sect_off), objfile_name (objfile));
23952 }
23953
23954 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23955 if (!die)
23956 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23957 sect_offset_str (sect_off), objfile_name (objfile));
23958
23959 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23960 if (attr == NULL)
23961 return NULL;
23962
23963 byte_order = (bfd_big_endian (objfile->obfd)
23964 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23965
23966 switch (attr->form)
23967 {
23968 case DW_FORM_addr:
23969 case DW_FORM_addrx:
23970 case DW_FORM_GNU_addr_index:
23971 {
23972 gdb_byte *tem;
23973
23974 *len = cu->header.addr_size;
23975 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23976 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23977 result = tem;
23978 }
23979 break;
23980 case DW_FORM_string:
23981 case DW_FORM_strp:
23982 case DW_FORM_strx:
23983 case DW_FORM_GNU_str_index:
23984 case DW_FORM_GNU_strp_alt:
23985 /* The string is already allocated on the objfile obstack, point
23986 directly to it. */
23987 {
23988 const char *attr_name = attr->as_string ();
23989 result = (const gdb_byte *) attr_name;
23990 *len = strlen (attr_name);
23991 }
23992 break;
23993 case DW_FORM_block1:
23994 case DW_FORM_block2:
23995 case DW_FORM_block4:
23996 case DW_FORM_block:
23997 case DW_FORM_exprloc:
23998 case DW_FORM_data16:
23999 {
24000 struct dwarf_block *block = attr->as_block ();
24001 result = block->data;
24002 *len = block->size;
24003 }
24004 break;
24005
24006 /* The DW_AT_const_value attributes are supposed to carry the
24007 symbol's value "represented as it would be on the target
24008 architecture." By the time we get here, it's already been
24009 converted to host endianness, so we just need to sign- or
24010 zero-extend it as appropriate. */
24011 case DW_FORM_data1:
24012 type = die_type (die, cu);
24013 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24014 if (result == NULL)
24015 result = write_constant_as_bytes (obstack, byte_order,
24016 type, value, len);
24017 break;
24018 case DW_FORM_data2:
24019 type = die_type (die, cu);
24020 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24021 if (result == NULL)
24022 result = write_constant_as_bytes (obstack, byte_order,
24023 type, value, len);
24024 break;
24025 case DW_FORM_data4:
24026 type = die_type (die, cu);
24027 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24028 if (result == NULL)
24029 result = write_constant_as_bytes (obstack, byte_order,
24030 type, value, len);
24031 break;
24032 case DW_FORM_data8:
24033 type = die_type (die, cu);
24034 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24035 if (result == NULL)
24036 result = write_constant_as_bytes (obstack, byte_order,
24037 type, value, len);
24038 break;
24039
24040 case DW_FORM_sdata:
24041 case DW_FORM_implicit_const:
24042 type = die_type (die, cu);
24043 result = write_constant_as_bytes (obstack, byte_order,
24044 type, attr->as_signed (), len);
24045 break;
24046
24047 case DW_FORM_udata:
24048 type = die_type (die, cu);
24049 result = write_constant_as_bytes (obstack, byte_order,
24050 type, attr->as_unsigned (), len);
24051 break;
24052
24053 default:
24054 complaint (_("unsupported const value attribute form: '%s'"),
24055 dwarf_form_name (attr->form));
24056 break;
24057 }
24058
24059 return result;
24060 }
24061
24062 /* See read.h. */
24063
24064 struct type *
24065 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
24066 dwarf2_per_cu_data *per_cu,
24067 dwarf2_per_objfile *per_objfile)
24068 {
24069 struct die_info *die;
24070
24071 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24072 if (cu == nullptr)
24073 cu = load_cu (per_cu, per_objfile, false);
24074
24075 if (cu == nullptr)
24076 return nullptr;
24077
24078 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24079 if (!die)
24080 return NULL;
24081
24082 return die_type (die, cu);
24083 }
24084
24085 /* See read.h. */
24086
24087 struct type *
24088 dwarf2_get_die_type (cu_offset die_offset,
24089 dwarf2_per_cu_data *per_cu,
24090 dwarf2_per_objfile *per_objfile)
24091 {
24092 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
24093 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
24094 }
24095
24096 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
24097 On entry *REF_CU is the CU of SRC_DIE.
24098 On exit *REF_CU is the CU of the result.
24099 Returns NULL if the referenced DIE isn't found. */
24100
24101 static struct die_info *
24102 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24103 struct dwarf2_cu **ref_cu)
24104 {
24105 struct die_info temp_die;
24106 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
24107 struct die_info *die;
24108 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
24109
24110
24111 /* While it might be nice to assert sig_type->type == NULL here,
24112 we can get here for DW_AT_imported_declaration where we need
24113 the DIE not the type. */
24114
24115 /* If necessary, add it to the queue and load its DIEs.
24116
24117 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24118 it doesn't mean they are currently loaded. Since we require them
24119 to be loaded, we must check for ourselves. */
24120 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
24121 language_minimal)
24122 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
24123 read_signatured_type (sig_type, per_objfile);
24124
24125 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
24126 gdb_assert (sig_cu != NULL);
24127 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24128 temp_die.sect_off = sig_type->type_offset_in_section;
24129 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24130 to_underlying (temp_die.sect_off));
24131 if (die)
24132 {
24133 /* For .gdb_index version 7 keep track of included TUs.
24134 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24135 if (per_objfile->per_bfd->index_table != NULL
24136 && per_objfile->per_bfd->index_table->version <= 7)
24137 {
24138 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24139 }
24140
24141 *ref_cu = sig_cu;
24142 if (sig_cu != cu)
24143 sig_cu->ancestor = cu;
24144
24145 return die;
24146 }
24147
24148 return NULL;
24149 }
24150
24151 /* Follow signatured type referenced by ATTR in SRC_DIE.
24152 On entry *REF_CU is the CU of SRC_DIE.
24153 On exit *REF_CU is the CU of the result.
24154 The result is the DIE of the type.
24155 If the referenced type cannot be found an error is thrown. */
24156
24157 static struct die_info *
24158 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24159 struct dwarf2_cu **ref_cu)
24160 {
24161 ULONGEST signature = attr->as_signature ();
24162 struct signatured_type *sig_type;
24163 struct die_info *die;
24164
24165 gdb_assert (attr->form == DW_FORM_ref_sig8);
24166
24167 sig_type = lookup_signatured_type (*ref_cu, signature);
24168 /* sig_type will be NULL if the signatured type is missing from
24169 the debug info. */
24170 if (sig_type == NULL)
24171 {
24172 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24173 " from DIE at %s [in module %s]"),
24174 hex_string (signature), sect_offset_str (src_die->sect_off),
24175 objfile_name ((*ref_cu)->per_objfile->objfile));
24176 }
24177
24178 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24179 if (die == NULL)
24180 {
24181 dump_die_for_error (src_die);
24182 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24183 " from DIE at %s [in module %s]"),
24184 hex_string (signature), sect_offset_str (src_die->sect_off),
24185 objfile_name ((*ref_cu)->per_objfile->objfile));
24186 }
24187
24188 return die;
24189 }
24190
24191 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24192 reading in and processing the type unit if necessary. */
24193
24194 static struct type *
24195 get_signatured_type (struct die_info *die, ULONGEST signature,
24196 struct dwarf2_cu *cu)
24197 {
24198 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24199 struct signatured_type *sig_type;
24200 struct dwarf2_cu *type_cu;
24201 struct die_info *type_die;
24202 struct type *type;
24203
24204 sig_type = lookup_signatured_type (cu, signature);
24205 /* sig_type will be NULL if the signatured type is missing from
24206 the debug info. */
24207 if (sig_type == NULL)
24208 {
24209 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24210 " from DIE at %s [in module %s]"),
24211 hex_string (signature), sect_offset_str (die->sect_off),
24212 objfile_name (per_objfile->objfile));
24213 return build_error_marker_type (cu, die);
24214 }
24215
24216 /* If we already know the type we're done. */
24217 type = per_objfile->get_type_for_signatured_type (sig_type);
24218 if (type != nullptr)
24219 return type;
24220
24221 type_cu = cu;
24222 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24223 if (type_die != NULL)
24224 {
24225 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24226 is created. This is important, for example, because for c++ classes
24227 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24228 type = read_type_die (type_die, type_cu);
24229 if (type == NULL)
24230 {
24231 complaint (_("Dwarf Error: Cannot build signatured type %s"
24232 " referenced from DIE at %s [in module %s]"),
24233 hex_string (signature), sect_offset_str (die->sect_off),
24234 objfile_name (per_objfile->objfile));
24235 type = build_error_marker_type (cu, die);
24236 }
24237 }
24238 else
24239 {
24240 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24241 " from DIE at %s [in module %s]"),
24242 hex_string (signature), sect_offset_str (die->sect_off),
24243 objfile_name (per_objfile->objfile));
24244 type = build_error_marker_type (cu, die);
24245 }
24246
24247 per_objfile->set_type_for_signatured_type (sig_type, type);
24248
24249 return type;
24250 }
24251
24252 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24253 reading in and processing the type unit if necessary. */
24254
24255 static struct type *
24256 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24257 struct dwarf2_cu *cu) /* ARI: editCase function */
24258 {
24259 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24260 if (attr->form_is_ref ())
24261 {
24262 struct dwarf2_cu *type_cu = cu;
24263 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24264
24265 return read_type_die (type_die, type_cu);
24266 }
24267 else if (attr->form == DW_FORM_ref_sig8)
24268 {
24269 return get_signatured_type (die, attr->as_signature (), cu);
24270 }
24271 else
24272 {
24273 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24274
24275 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24276 " at %s [in module %s]"),
24277 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24278 objfile_name (per_objfile->objfile));
24279 return build_error_marker_type (cu, die);
24280 }
24281 }
24282
24283 /* Load the DIEs associated with type unit PER_CU into memory. */
24284
24285 static void
24286 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24287 dwarf2_per_objfile *per_objfile)
24288 {
24289 struct signatured_type *sig_type;
24290
24291 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24292 gdb_assert (! per_cu->type_unit_group_p ());
24293
24294 /* We have the per_cu, but we need the signatured_type.
24295 Fortunately this is an easy translation. */
24296 gdb_assert (per_cu->is_debug_types);
24297 sig_type = (struct signatured_type *) per_cu;
24298
24299 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24300
24301 read_signatured_type (sig_type, per_objfile);
24302
24303 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24304 }
24305
24306 /* Read in a signatured type and build its CU and DIEs.
24307 If the type is a stub for the real type in a DWO file,
24308 read in the real type from the DWO file as well. */
24309
24310 static void
24311 read_signatured_type (signatured_type *sig_type,
24312 dwarf2_per_objfile *per_objfile)
24313 {
24314 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24315
24316 gdb_assert (per_cu->is_debug_types);
24317 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24318
24319 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24320
24321 if (!reader.dummy_p)
24322 {
24323 struct dwarf2_cu *cu = reader.cu;
24324 const gdb_byte *info_ptr = reader.info_ptr;
24325
24326 gdb_assert (cu->die_hash == NULL);
24327 cu->die_hash =
24328 htab_create_alloc_ex (cu->header.length / 12,
24329 die_hash,
24330 die_eq,
24331 NULL,
24332 &cu->comp_unit_obstack,
24333 hashtab_obstack_allocate,
24334 dummy_obstack_deallocate);
24335
24336 if (reader.comp_unit_die->has_children)
24337 reader.comp_unit_die->child
24338 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24339 reader.comp_unit_die);
24340 cu->dies = reader.comp_unit_die;
24341 /* comp_unit_die is not stored in die_hash, no need. */
24342
24343 /* We try not to read any attributes in this function, because
24344 not all CUs needed for references have been loaded yet, and
24345 symbol table processing isn't initialized. But we have to
24346 set the CU language, or we won't be able to build types
24347 correctly. Similarly, if we do not read the producer, we can
24348 not apply producer-specific interpretation. */
24349 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24350
24351 reader.keep ();
24352 }
24353
24354 sig_type->per_cu.tu_read = 1;
24355 }
24356
24357 /* Decode simple location descriptions.
24358 Given a pointer to a dwarf block that defines a location, compute
24359 the location and return the value. If COMPUTED is non-null, it is
24360 set to true to indicate that decoding was successful, and false
24361 otherwise. If COMPUTED is null, then this function may emit a
24362 complaint. */
24363
24364 static CORE_ADDR
24365 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24366 {
24367 struct objfile *objfile = cu->per_objfile->objfile;
24368 size_t i;
24369 size_t size = blk->size;
24370 const gdb_byte *data = blk->data;
24371 CORE_ADDR stack[64];
24372 int stacki;
24373 unsigned int bytes_read, unsnd;
24374 gdb_byte op;
24375
24376 if (computed != nullptr)
24377 *computed = false;
24378
24379 i = 0;
24380 stacki = 0;
24381 stack[stacki] = 0;
24382 stack[++stacki] = 0;
24383
24384 while (i < size)
24385 {
24386 op = data[i++];
24387 switch (op)
24388 {
24389 case DW_OP_lit0:
24390 case DW_OP_lit1:
24391 case DW_OP_lit2:
24392 case DW_OP_lit3:
24393 case DW_OP_lit4:
24394 case DW_OP_lit5:
24395 case DW_OP_lit6:
24396 case DW_OP_lit7:
24397 case DW_OP_lit8:
24398 case DW_OP_lit9:
24399 case DW_OP_lit10:
24400 case DW_OP_lit11:
24401 case DW_OP_lit12:
24402 case DW_OP_lit13:
24403 case DW_OP_lit14:
24404 case DW_OP_lit15:
24405 case DW_OP_lit16:
24406 case DW_OP_lit17:
24407 case DW_OP_lit18:
24408 case DW_OP_lit19:
24409 case DW_OP_lit20:
24410 case DW_OP_lit21:
24411 case DW_OP_lit22:
24412 case DW_OP_lit23:
24413 case DW_OP_lit24:
24414 case DW_OP_lit25:
24415 case DW_OP_lit26:
24416 case DW_OP_lit27:
24417 case DW_OP_lit28:
24418 case DW_OP_lit29:
24419 case DW_OP_lit30:
24420 case DW_OP_lit31:
24421 stack[++stacki] = op - DW_OP_lit0;
24422 break;
24423
24424 case DW_OP_reg0:
24425 case DW_OP_reg1:
24426 case DW_OP_reg2:
24427 case DW_OP_reg3:
24428 case DW_OP_reg4:
24429 case DW_OP_reg5:
24430 case DW_OP_reg6:
24431 case DW_OP_reg7:
24432 case DW_OP_reg8:
24433 case DW_OP_reg9:
24434 case DW_OP_reg10:
24435 case DW_OP_reg11:
24436 case DW_OP_reg12:
24437 case DW_OP_reg13:
24438 case DW_OP_reg14:
24439 case DW_OP_reg15:
24440 case DW_OP_reg16:
24441 case DW_OP_reg17:
24442 case DW_OP_reg18:
24443 case DW_OP_reg19:
24444 case DW_OP_reg20:
24445 case DW_OP_reg21:
24446 case DW_OP_reg22:
24447 case DW_OP_reg23:
24448 case DW_OP_reg24:
24449 case DW_OP_reg25:
24450 case DW_OP_reg26:
24451 case DW_OP_reg27:
24452 case DW_OP_reg28:
24453 case DW_OP_reg29:
24454 case DW_OP_reg30:
24455 case DW_OP_reg31:
24456 stack[++stacki] = op - DW_OP_reg0;
24457 if (i < size)
24458 {
24459 if (computed == nullptr)
24460 dwarf2_complex_location_expr_complaint ();
24461 else
24462 return 0;
24463 }
24464 break;
24465
24466 case DW_OP_regx:
24467 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24468 i += bytes_read;
24469 stack[++stacki] = unsnd;
24470 if (i < size)
24471 {
24472 if (computed == nullptr)
24473 dwarf2_complex_location_expr_complaint ();
24474 else
24475 return 0;
24476 }
24477 break;
24478
24479 case DW_OP_addr:
24480 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24481 &bytes_read);
24482 i += bytes_read;
24483 break;
24484
24485 case DW_OP_const1u:
24486 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24487 i += 1;
24488 break;
24489
24490 case DW_OP_const1s:
24491 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24492 i += 1;
24493 break;
24494
24495 case DW_OP_const2u:
24496 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24497 i += 2;
24498 break;
24499
24500 case DW_OP_const2s:
24501 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24502 i += 2;
24503 break;
24504
24505 case DW_OP_const4u:
24506 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24507 i += 4;
24508 break;
24509
24510 case DW_OP_const4s:
24511 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24512 i += 4;
24513 break;
24514
24515 case DW_OP_const8u:
24516 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24517 i += 8;
24518 break;
24519
24520 case DW_OP_constu:
24521 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24522 &bytes_read);
24523 i += bytes_read;
24524 break;
24525
24526 case DW_OP_consts:
24527 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24528 i += bytes_read;
24529 break;
24530
24531 case DW_OP_dup:
24532 stack[stacki + 1] = stack[stacki];
24533 stacki++;
24534 break;
24535
24536 case DW_OP_plus:
24537 stack[stacki - 1] += stack[stacki];
24538 stacki--;
24539 break;
24540
24541 case DW_OP_plus_uconst:
24542 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24543 &bytes_read);
24544 i += bytes_read;
24545 break;
24546
24547 case DW_OP_minus:
24548 stack[stacki - 1] -= stack[stacki];
24549 stacki--;
24550 break;
24551
24552 case DW_OP_deref:
24553 /* If we're not the last op, then we definitely can't encode
24554 this using GDB's address_class enum. This is valid for partial
24555 global symbols, although the variable's address will be bogus
24556 in the psymtab. */
24557 if (i < size)
24558 {
24559 if (computed == nullptr)
24560 dwarf2_complex_location_expr_complaint ();
24561 else
24562 return 0;
24563 }
24564 break;
24565
24566 case DW_OP_GNU_push_tls_address:
24567 case DW_OP_form_tls_address:
24568 /* The top of the stack has the offset from the beginning
24569 of the thread control block at which the variable is located. */
24570 /* Nothing should follow this operator, so the top of stack would
24571 be returned. */
24572 /* This is valid for partial global symbols, but the variable's
24573 address will be bogus in the psymtab. Make it always at least
24574 non-zero to not look as a variable garbage collected by linker
24575 which have DW_OP_addr 0. */
24576 if (i < size)
24577 {
24578 if (computed == nullptr)
24579 dwarf2_complex_location_expr_complaint ();
24580 else
24581 return 0;
24582 }
24583 stack[stacki]++;
24584 break;
24585
24586 case DW_OP_GNU_uninit:
24587 if (computed != nullptr)
24588 return 0;
24589 break;
24590
24591 case DW_OP_addrx:
24592 case DW_OP_GNU_addr_index:
24593 case DW_OP_GNU_const_index:
24594 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24595 &bytes_read);
24596 i += bytes_read;
24597 break;
24598
24599 default:
24600 if (computed == nullptr)
24601 {
24602 const char *name = get_DW_OP_name (op);
24603
24604 if (name)
24605 complaint (_("unsupported stack op: '%s'"),
24606 name);
24607 else
24608 complaint (_("unsupported stack op: '%02x'"),
24609 op);
24610 }
24611
24612 return (stack[stacki]);
24613 }
24614
24615 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24616 outside of the allocated space. Also enforce minimum>0. */
24617 if (stacki >= ARRAY_SIZE (stack) - 1)
24618 {
24619 if (computed == nullptr)
24620 complaint (_("location description stack overflow"));
24621 return 0;
24622 }
24623
24624 if (stacki <= 0)
24625 {
24626 if (computed == nullptr)
24627 complaint (_("location description stack underflow"));
24628 return 0;
24629 }
24630 }
24631
24632 if (computed != nullptr)
24633 *computed = true;
24634 return (stack[stacki]);
24635 }
24636
24637 /* memory allocation interface */
24638
24639 static struct dwarf_block *
24640 dwarf_alloc_block (struct dwarf2_cu *cu)
24641 {
24642 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24643 }
24644
24645 static struct die_info *
24646 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24647 {
24648 struct die_info *die;
24649 size_t size = sizeof (struct die_info);
24650
24651 if (num_attrs > 1)
24652 size += (num_attrs - 1) * sizeof (struct attribute);
24653
24654 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24655 memset (die, 0, sizeof (struct die_info));
24656 return (die);
24657 }
24658
24659 \f
24660
24661 /* Macro support. */
24662
24663 /* An overload of dwarf_decode_macros that finds the correct section
24664 and ensures it is read in before calling the other overload. */
24665
24666 static void
24667 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24668 int section_is_gnu)
24669 {
24670 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24671 struct objfile *objfile = per_objfile->objfile;
24672 const struct line_header *lh = cu->line_header;
24673 unsigned int offset_size = cu->header.offset_size;
24674 struct dwarf2_section_info *section;
24675 const char *section_name;
24676
24677 if (cu->dwo_unit != nullptr)
24678 {
24679 if (section_is_gnu)
24680 {
24681 section = &cu->dwo_unit->dwo_file->sections.macro;
24682 section_name = ".debug_macro.dwo";
24683 }
24684 else
24685 {
24686 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24687 section_name = ".debug_macinfo.dwo";
24688 }
24689 }
24690 else
24691 {
24692 if (section_is_gnu)
24693 {
24694 section = &per_objfile->per_bfd->macro;
24695 section_name = ".debug_macro";
24696 }
24697 else
24698 {
24699 section = &per_objfile->per_bfd->macinfo;
24700 section_name = ".debug_macinfo";
24701 }
24702 }
24703
24704 section->read (objfile);
24705 if (section->buffer == nullptr)
24706 {
24707 complaint (_("missing %s section"), section_name);
24708 return;
24709 }
24710
24711 buildsym_compunit *builder = cu->get_builder ();
24712
24713 struct dwarf2_section_info *str_offsets_section;
24714 struct dwarf2_section_info *str_section;
24715 ULONGEST str_offsets_base;
24716
24717 if (cu->dwo_unit != nullptr)
24718 {
24719 str_offsets_section = &cu->dwo_unit->dwo_file
24720 ->sections.str_offsets;
24721 str_section = &cu->dwo_unit->dwo_file->sections.str;
24722 str_offsets_base = cu->header.addr_size;
24723 }
24724 else
24725 {
24726 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24727 str_section = &per_objfile->per_bfd->str;
24728 str_offsets_base = *cu->str_offsets_base;
24729 }
24730
24731 dwarf_decode_macros (per_objfile, builder, section, lh,
24732 offset_size, offset, str_section, str_offsets_section,
24733 str_offsets_base, section_is_gnu);
24734 }
24735
24736 /* Return the .debug_loc section to use for CU.
24737 For DWO files use .debug_loc.dwo. */
24738
24739 static struct dwarf2_section_info *
24740 cu_debug_loc_section (struct dwarf2_cu *cu)
24741 {
24742 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24743
24744 if (cu->dwo_unit)
24745 {
24746 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24747
24748 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24749 }
24750 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24751 : &per_objfile->per_bfd->loc);
24752 }
24753
24754 /* Return the .debug_rnglists section to use for CU. */
24755 static struct dwarf2_section_info *
24756 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24757 {
24758 if (cu->header.version < 5)
24759 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24760 cu->header.version);
24761 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24762
24763 /* Make sure we read the .debug_rnglists section from the file that
24764 contains the DW_AT_ranges attribute we are reading. Normally that
24765 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24766 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24767 program. */
24768 if (cu->dwo_unit != nullptr
24769 && tag != DW_TAG_compile_unit
24770 && tag != DW_TAG_skeleton_unit)
24771 {
24772 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24773
24774 if (sections->rnglists.size > 0)
24775 return &sections->rnglists;
24776 else
24777 error (_(".debug_rnglists section is missing from .dwo file."));
24778 }
24779 return &dwarf2_per_objfile->per_bfd->rnglists;
24780 }
24781
24782 /* A helper function that fills in a dwarf2_loclist_baton. */
24783
24784 static void
24785 fill_in_loclist_baton (struct dwarf2_cu *cu,
24786 struct dwarf2_loclist_baton *baton,
24787 const struct attribute *attr)
24788 {
24789 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24790 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24791
24792 section->read (per_objfile->objfile);
24793
24794 baton->per_objfile = per_objfile;
24795 baton->per_cu = cu->per_cu;
24796 gdb_assert (baton->per_cu);
24797 /* We don't know how long the location list is, but make sure we
24798 don't run off the edge of the section. */
24799 baton->size = section->size - attr->as_unsigned ();
24800 baton->data = section->buffer + attr->as_unsigned ();
24801 if (cu->base_address.has_value ())
24802 baton->base_address = *cu->base_address;
24803 else
24804 baton->base_address = 0;
24805 baton->from_dwo = cu->dwo_unit != NULL;
24806 }
24807
24808 static void
24809 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24810 struct dwarf2_cu *cu, int is_block)
24811 {
24812 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24813 struct objfile *objfile = per_objfile->objfile;
24814 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24815
24816 if (attr->form_is_section_offset ()
24817 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24818 the section. If so, fall through to the complaint in the
24819 other branch. */
24820 && attr->as_unsigned () < section->get_size (objfile))
24821 {
24822 struct dwarf2_loclist_baton *baton;
24823
24824 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24825
24826 fill_in_loclist_baton (cu, baton, attr);
24827
24828 if (!cu->base_address.has_value ())
24829 complaint (_("Location list used without "
24830 "specifying the CU base address."));
24831
24832 SYMBOL_ACLASS_INDEX (sym) = (is_block
24833 ? dwarf2_loclist_block_index
24834 : dwarf2_loclist_index);
24835 SYMBOL_LOCATION_BATON (sym) = baton;
24836 }
24837 else
24838 {
24839 struct dwarf2_locexpr_baton *baton;
24840
24841 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24842 baton->per_objfile = per_objfile;
24843 baton->per_cu = cu->per_cu;
24844 gdb_assert (baton->per_cu);
24845
24846 if (attr->form_is_block ())
24847 {
24848 /* Note that we're just copying the block's data pointer
24849 here, not the actual data. We're still pointing into the
24850 info_buffer for SYM's objfile; right now we never release
24851 that buffer, but when we do clean up properly this may
24852 need to change. */
24853 struct dwarf_block *block = attr->as_block ();
24854 baton->size = block->size;
24855 baton->data = block->data;
24856 }
24857 else
24858 {
24859 dwarf2_invalid_attrib_class_complaint ("location description",
24860 sym->natural_name ());
24861 baton->size = 0;
24862 }
24863
24864 SYMBOL_ACLASS_INDEX (sym) = (is_block
24865 ? dwarf2_locexpr_block_index
24866 : dwarf2_locexpr_index);
24867 SYMBOL_LOCATION_BATON (sym) = baton;
24868 }
24869 }
24870
24871 /* See read.h. */
24872
24873 const comp_unit_head *
24874 dwarf2_per_cu_data::get_header () const
24875 {
24876 if (!m_header_read_in)
24877 {
24878 const gdb_byte *info_ptr
24879 = this->section->buffer + to_underlying (this->sect_off);
24880
24881 memset (&m_header, 0, sizeof (m_header));
24882
24883 read_comp_unit_head (&m_header, info_ptr, this->section,
24884 rcuh_kind::COMPILE);
24885
24886 m_header_read_in = true;
24887 }
24888
24889 return &m_header;
24890 }
24891
24892 /* See read.h. */
24893
24894 int
24895 dwarf2_per_cu_data::addr_size () const
24896 {
24897 return this->get_header ()->addr_size;
24898 }
24899
24900 /* See read.h. */
24901
24902 int
24903 dwarf2_per_cu_data::offset_size () const
24904 {
24905 return this->get_header ()->offset_size;
24906 }
24907
24908 /* See read.h. */
24909
24910 int
24911 dwarf2_per_cu_data::ref_addr_size () const
24912 {
24913 const comp_unit_head *header = this->get_header ();
24914
24915 if (header->version == 2)
24916 return header->addr_size;
24917 else
24918 return header->offset_size;
24919 }
24920
24921 /* See read.h. */
24922
24923 struct type *
24924 dwarf2_cu::addr_type () const
24925 {
24926 struct objfile *objfile = this->per_objfile->objfile;
24927 struct type *void_type = objfile_type (objfile)->builtin_void;
24928 struct type *addr_type = lookup_pointer_type (void_type);
24929 int addr_size = this->per_cu->addr_size ();
24930
24931 if (TYPE_LENGTH (addr_type) == addr_size)
24932 return addr_type;
24933
24934 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24935 return addr_type;
24936 }
24937
24938 /* A helper function for dwarf2_find_containing_comp_unit that returns
24939 the index of the result, and that searches a vector. It will
24940 return a result even if the offset in question does not actually
24941 occur in any CU. This is separate so that it can be unit
24942 tested. */
24943
24944 static int
24945 dwarf2_find_containing_comp_unit
24946 (sect_offset sect_off,
24947 unsigned int offset_in_dwz,
24948 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24949 {
24950 int low, high;
24951
24952 low = 0;
24953 high = all_comp_units.size () - 1;
24954 while (high > low)
24955 {
24956 struct dwarf2_per_cu_data *mid_cu;
24957 int mid = low + (high - low) / 2;
24958
24959 mid_cu = all_comp_units[mid];
24960 if (mid_cu->is_dwz > offset_in_dwz
24961 || (mid_cu->is_dwz == offset_in_dwz
24962 && mid_cu->sect_off + mid_cu->length > sect_off))
24963 high = mid;
24964 else
24965 low = mid + 1;
24966 }
24967 gdb_assert (low == high);
24968 return low;
24969 }
24970
24971 /* Locate the .debug_info compilation unit from CU's objfile which contains
24972 the DIE at OFFSET. Raises an error on failure. */
24973
24974 static struct dwarf2_per_cu_data *
24975 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24976 unsigned int offset_in_dwz,
24977 dwarf2_per_objfile *per_objfile)
24978 {
24979 int low = dwarf2_find_containing_comp_unit
24980 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24981 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24982
24983 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24984 {
24985 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24986 error (_("Dwarf Error: could not find partial DIE containing "
24987 "offset %s [in module %s]"),
24988 sect_offset_str (sect_off),
24989 bfd_get_filename (per_objfile->objfile->obfd));
24990
24991 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24992 <= sect_off);
24993 return per_objfile->per_bfd->all_comp_units[low-1];
24994 }
24995 else
24996 {
24997 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24998 && sect_off >= this_cu->sect_off + this_cu->length)
24999 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25000 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25001 return this_cu;
25002 }
25003 }
25004
25005 #if GDB_SELF_TEST
25006
25007 namespace selftests {
25008 namespace find_containing_comp_unit {
25009
25010 static void
25011 run_test ()
25012 {
25013 struct dwarf2_per_cu_data one {};
25014 struct dwarf2_per_cu_data two {};
25015 struct dwarf2_per_cu_data three {};
25016 struct dwarf2_per_cu_data four {};
25017
25018 one.length = 5;
25019 two.sect_off = sect_offset (one.length);
25020 two.length = 7;
25021
25022 three.length = 5;
25023 three.is_dwz = 1;
25024 four.sect_off = sect_offset (three.length);
25025 four.length = 7;
25026 four.is_dwz = 1;
25027
25028 std::vector<dwarf2_per_cu_data *> units;
25029 units.push_back (&one);
25030 units.push_back (&two);
25031 units.push_back (&three);
25032 units.push_back (&four);
25033
25034 int result;
25035
25036 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25037 SELF_CHECK (units[result] == &one);
25038 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25039 SELF_CHECK (units[result] == &one);
25040 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25041 SELF_CHECK (units[result] == &two);
25042
25043 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25044 SELF_CHECK (units[result] == &three);
25045 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25046 SELF_CHECK (units[result] == &three);
25047 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25048 SELF_CHECK (units[result] == &four);
25049 }
25050
25051 }
25052 }
25053
25054 #endif /* GDB_SELF_TEST */
25055
25056 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
25057
25058 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25059 dwarf2_per_objfile *per_objfile)
25060 : per_cu (per_cu),
25061 per_objfile (per_objfile),
25062 mark (false),
25063 has_loclist (false),
25064 checked_producer (false),
25065 producer_is_gxx_lt_4_6 (false),
25066 producer_is_gcc_lt_4_3 (false),
25067 producer_is_icc (false),
25068 producer_is_icc_lt_14 (false),
25069 producer_is_codewarrior (false),
25070 processing_has_namespace_info (false)
25071 {
25072 }
25073
25074 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25075
25076 static void
25077 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25078 enum language pretend_language)
25079 {
25080 struct attribute *attr;
25081
25082 /* Set the language we're debugging. */
25083 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25084 if (attr != nullptr)
25085 set_cu_language (attr->constant_value (0), cu);
25086 else
25087 {
25088 cu->language = pretend_language;
25089 cu->language_defn = language_def (cu->language);
25090 }
25091
25092 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25093 }
25094
25095 /* See read.h. */
25096
25097 dwarf2_cu *
25098 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
25099 {
25100 auto it = m_dwarf2_cus.find (per_cu);
25101 if (it == m_dwarf2_cus.end ())
25102 return nullptr;
25103
25104 return it->second;
25105 }
25106
25107 /* See read.h. */
25108
25109 void
25110 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25111 {
25112 gdb_assert (this->get_cu (per_cu) == nullptr);
25113
25114 m_dwarf2_cus[per_cu] = cu;
25115 }
25116
25117 /* See read.h. */
25118
25119 void
25120 dwarf2_per_objfile::age_comp_units ()
25121 {
25122 dwarf_read_debug_printf_v ("running");
25123
25124 /* This is not expected to be called in the middle of CU expansion. There is
25125 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25126 loaded in memory. Calling age_comp_units while the queue is in use could
25127 make us free the DIEs for a CU that is in the queue and therefore break
25128 that invariant. */
25129 gdb_assert (!this->per_bfd->queue.has_value ());
25130
25131 /* Start by clearing all marks. */
25132 for (auto pair : m_dwarf2_cus)
25133 pair.second->mark = false;
25134
25135 /* Traverse all CUs, mark them and their dependencies if used recently
25136 enough. */
25137 for (auto pair : m_dwarf2_cus)
25138 {
25139 dwarf2_cu *cu = pair.second;
25140
25141 cu->last_used++;
25142 if (cu->last_used <= dwarf_max_cache_age)
25143 dwarf2_mark (cu);
25144 }
25145
25146 /* Delete all CUs still not marked. */
25147 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
25148 {
25149 dwarf2_cu *cu = it->second;
25150
25151 if (!cu->mark)
25152 {
25153 dwarf_read_debug_printf_v ("deleting old CU %s",
25154 sect_offset_str (cu->per_cu->sect_off));
25155 delete cu;
25156 it = m_dwarf2_cus.erase (it);
25157 }
25158 else
25159 it++;
25160 }
25161 }
25162
25163 /* See read.h. */
25164
25165 void
25166 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25167 {
25168 auto it = m_dwarf2_cus.find (per_cu);
25169 if (it == m_dwarf2_cus.end ())
25170 return;
25171
25172 delete it->second;
25173
25174 m_dwarf2_cus.erase (it);
25175 }
25176
25177 dwarf2_per_objfile::~dwarf2_per_objfile ()
25178 {
25179 remove_all_cus ();
25180 }
25181
25182 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25183 We store these in a hash table separate from the DIEs, and preserve them
25184 when the DIEs are flushed out of cache.
25185
25186 The CU "per_cu" pointer is needed because offset alone is not enough to
25187 uniquely identify the type. A file may have multiple .debug_types sections,
25188 or the type may come from a DWO file. Furthermore, while it's more logical
25189 to use per_cu->section+offset, with Fission the section with the data is in
25190 the DWO file but we don't know that section at the point we need it.
25191 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25192 because we can enter the lookup routine, get_die_type_at_offset, from
25193 outside this file, and thus won't necessarily have PER_CU->cu.
25194 Fortunately, PER_CU is stable for the life of the objfile. */
25195
25196 struct dwarf2_per_cu_offset_and_type
25197 {
25198 const struct dwarf2_per_cu_data *per_cu;
25199 sect_offset sect_off;
25200 struct type *type;
25201 };
25202
25203 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25204
25205 static hashval_t
25206 per_cu_offset_and_type_hash (const void *item)
25207 {
25208 const struct dwarf2_per_cu_offset_and_type *ofs
25209 = (const struct dwarf2_per_cu_offset_and_type *) item;
25210
25211 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25212 }
25213
25214 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25215
25216 static int
25217 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25218 {
25219 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25220 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25221 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25222 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25223
25224 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25225 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25226 }
25227
25228 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25229 table if necessary. For convenience, return TYPE.
25230
25231 The DIEs reading must have careful ordering to:
25232 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25233 reading current DIE.
25234 * Not trying to dereference contents of still incompletely read in types
25235 while reading in other DIEs.
25236 * Enable referencing still incompletely read in types just by a pointer to
25237 the type without accessing its fields.
25238
25239 Therefore caller should follow these rules:
25240 * Try to fetch any prerequisite types we may need to build this DIE type
25241 before building the type and calling set_die_type.
25242 * After building type call set_die_type for current DIE as soon as
25243 possible before fetching more types to complete the current type.
25244 * Make the type as complete as possible before fetching more types. */
25245
25246 static struct type *
25247 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25248 bool skip_data_location)
25249 {
25250 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25251 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25252 struct objfile *objfile = per_objfile->objfile;
25253 struct attribute *attr;
25254 struct dynamic_prop prop;
25255
25256 /* For Ada types, make sure that the gnat-specific data is always
25257 initialized (if not already set). There are a few types where
25258 we should not be doing so, because the type-specific area is
25259 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25260 where the type-specific area is used to store the floatformat).
25261 But this is not a problem, because the gnat-specific information
25262 is actually not needed for these types. */
25263 if (need_gnat_info (cu)
25264 && type->code () != TYPE_CODE_FUNC
25265 && type->code () != TYPE_CODE_FLT
25266 && type->code () != TYPE_CODE_METHODPTR
25267 && type->code () != TYPE_CODE_MEMBERPTR
25268 && type->code () != TYPE_CODE_METHOD
25269 && type->code () != TYPE_CODE_FIXED_POINT
25270 && !HAVE_GNAT_AUX_INFO (type))
25271 INIT_GNAT_SPECIFIC (type);
25272
25273 /* Read DW_AT_allocated and set in type. */
25274 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25275 if (attr != NULL)
25276 {
25277 struct type *prop_type = cu->addr_sized_int_type (false);
25278 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25279 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25280 }
25281
25282 /* Read DW_AT_associated and set in type. */
25283 attr = dwarf2_attr (die, DW_AT_associated, cu);
25284 if (attr != NULL)
25285 {
25286 struct type *prop_type = cu->addr_sized_int_type (false);
25287 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25288 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25289 }
25290
25291 /* Read DW_AT_data_location and set in type. */
25292 if (!skip_data_location)
25293 {
25294 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25295 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25296 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25297 }
25298
25299 if (per_objfile->die_type_hash == NULL)
25300 per_objfile->die_type_hash
25301 = htab_up (htab_create_alloc (127,
25302 per_cu_offset_and_type_hash,
25303 per_cu_offset_and_type_eq,
25304 NULL, xcalloc, xfree));
25305
25306 ofs.per_cu = cu->per_cu;
25307 ofs.sect_off = die->sect_off;
25308 ofs.type = type;
25309 slot = (struct dwarf2_per_cu_offset_and_type **)
25310 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25311 if (*slot)
25312 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25313 sect_offset_str (die->sect_off));
25314 *slot = XOBNEW (&objfile->objfile_obstack,
25315 struct dwarf2_per_cu_offset_and_type);
25316 **slot = ofs;
25317 return type;
25318 }
25319
25320 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25321 or return NULL if the die does not have a saved type. */
25322
25323 static struct type *
25324 get_die_type_at_offset (sect_offset sect_off,
25325 dwarf2_per_cu_data *per_cu,
25326 dwarf2_per_objfile *per_objfile)
25327 {
25328 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25329
25330 if (per_objfile->die_type_hash == NULL)
25331 return NULL;
25332
25333 ofs.per_cu = per_cu;
25334 ofs.sect_off = sect_off;
25335 slot = ((struct dwarf2_per_cu_offset_and_type *)
25336 htab_find (per_objfile->die_type_hash.get (), &ofs));
25337 if (slot)
25338 return slot->type;
25339 else
25340 return NULL;
25341 }
25342
25343 /* Look up the type for DIE in CU in die_type_hash,
25344 or return NULL if DIE does not have a saved type. */
25345
25346 static struct type *
25347 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25348 {
25349 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25350 }
25351
25352 /* Add a dependence relationship from CU to REF_PER_CU. */
25353
25354 static void
25355 dwarf2_add_dependence (struct dwarf2_cu *cu,
25356 struct dwarf2_per_cu_data *ref_per_cu)
25357 {
25358 void **slot;
25359
25360 if (cu->dependencies == NULL)
25361 cu->dependencies
25362 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25363 NULL, &cu->comp_unit_obstack,
25364 hashtab_obstack_allocate,
25365 dummy_obstack_deallocate);
25366
25367 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25368 if (*slot == NULL)
25369 *slot = ref_per_cu;
25370 }
25371
25372 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25373 Set the mark field in every compilation unit in the
25374 cache that we must keep because we are keeping CU.
25375
25376 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25377
25378 static int
25379 dwarf2_mark_helper (void **slot, void *data)
25380 {
25381 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25382 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25383 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25384
25385 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25386 reading of the chain. As such dependencies remain valid it is not much
25387 useful to track and undo them during QUIT cleanups. */
25388 if (cu == nullptr)
25389 return 1;
25390
25391 if (cu->mark)
25392 return 1;
25393
25394 cu->mark = true;
25395
25396 if (cu->dependencies != nullptr)
25397 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25398
25399 return 1;
25400 }
25401
25402 /* Set the mark field in CU and in every other compilation unit in the
25403 cache that we must keep because we are keeping CU. */
25404
25405 static void
25406 dwarf2_mark (struct dwarf2_cu *cu)
25407 {
25408 if (cu->mark)
25409 return;
25410
25411 cu->mark = true;
25412
25413 if (cu->dependencies != nullptr)
25414 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25415 }
25416
25417 /* Trivial hash function for partial_die_info: the hash value of a DIE
25418 is its offset in .debug_info for this objfile. */
25419
25420 static hashval_t
25421 partial_die_hash (const void *item)
25422 {
25423 const struct partial_die_info *part_die
25424 = (const struct partial_die_info *) item;
25425
25426 return to_underlying (part_die->sect_off);
25427 }
25428
25429 /* Trivial comparison function for partial_die_info structures: two DIEs
25430 are equal if they have the same offset. */
25431
25432 static int
25433 partial_die_eq (const void *item_lhs, const void *item_rhs)
25434 {
25435 const struct partial_die_info *part_die_lhs
25436 = (const struct partial_die_info *) item_lhs;
25437 const struct partial_die_info *part_die_rhs
25438 = (const struct partial_die_info *) item_rhs;
25439
25440 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25441 }
25442
25443 struct cmd_list_element *set_dwarf_cmdlist;
25444 struct cmd_list_element *show_dwarf_cmdlist;
25445
25446 static void
25447 show_check_physname (struct ui_file *file, int from_tty,
25448 struct cmd_list_element *c, const char *value)
25449 {
25450 fprintf_filtered (file,
25451 _("Whether to check \"physname\" is %s.\n"),
25452 value);
25453 }
25454
25455 void _initialize_dwarf2_read ();
25456 void
25457 _initialize_dwarf2_read ()
25458 {
25459 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25460 Set DWARF specific variables.\n\
25461 Configure DWARF variables such as the cache size."),
25462 &set_dwarf_cmdlist, "maintenance set dwarf ",
25463 0/*allow-unknown*/, &maintenance_set_cmdlist);
25464
25465 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25466 Show DWARF specific variables.\n\
25467 Show DWARF variables such as the cache size."),
25468 &show_dwarf_cmdlist, "maintenance show dwarf ",
25469 0/*allow-unknown*/, &maintenance_show_cmdlist);
25470
25471 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25472 &dwarf_max_cache_age, _("\
25473 Set the upper bound on the age of cached DWARF compilation units."), _("\
25474 Show the upper bound on the age of cached DWARF compilation units."), _("\
25475 A higher limit means that cached compilation units will be stored\n\
25476 in memory longer, and more total memory will be used. Zero disables\n\
25477 caching, which can slow down startup."),
25478 NULL,
25479 show_dwarf_max_cache_age,
25480 &set_dwarf_cmdlist,
25481 &show_dwarf_cmdlist);
25482
25483 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25484 Set debugging of the DWARF reader."), _("\
25485 Show debugging of the DWARF reader."), _("\
25486 When enabled (non-zero), debugging messages are printed during DWARF\n\
25487 reading and symtab expansion. A value of 1 (one) provides basic\n\
25488 information. A value greater than 1 provides more verbose information."),
25489 NULL,
25490 NULL,
25491 &setdebuglist, &showdebuglist);
25492
25493 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25494 Set debugging of the DWARF DIE reader."), _("\
25495 Show debugging of the DWARF DIE reader."), _("\
25496 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25497 The value is the maximum depth to print."),
25498 NULL,
25499 NULL,
25500 &setdebuglist, &showdebuglist);
25501
25502 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25503 Set debugging of the dwarf line reader."), _("\
25504 Show debugging of the dwarf line reader."), _("\
25505 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25506 A value of 1 (one) provides basic information.\n\
25507 A value greater than 1 provides more verbose information."),
25508 NULL,
25509 NULL,
25510 &setdebuglist, &showdebuglist);
25511
25512 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25513 Set cross-checking of \"physname\" code against demangler."), _("\
25514 Show cross-checking of \"physname\" code against demangler."), _("\
25515 When enabled, GDB's internal \"physname\" code is checked against\n\
25516 the demangler."),
25517 NULL, show_check_physname,
25518 &setdebuglist, &showdebuglist);
25519
25520 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25521 no_class, &use_deprecated_index_sections, _("\
25522 Set whether to use deprecated gdb_index sections."), _("\
25523 Show whether to use deprecated gdb_index sections."), _("\
25524 When enabled, deprecated .gdb_index sections are used anyway.\n\
25525 Normally they are ignored either because of a missing feature or\n\
25526 performance issue.\n\
25527 Warning: This option must be enabled before gdb reads the file."),
25528 NULL,
25529 NULL,
25530 &setlist, &showlist);
25531
25532 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25533 &dwarf2_locexpr_funcs);
25534 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25535 &dwarf2_loclist_funcs);
25536
25537 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25538 &dwarf2_block_frame_base_locexpr_funcs);
25539 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25540 &dwarf2_block_frame_base_loclist_funcs);
25541
25542 #if GDB_SELF_TEST
25543 selftests::register_test ("dw2_expand_symtabs_matching",
25544 selftests::dw2_expand_symtabs_matching::run_test);
25545 selftests::register_test ("dwarf2_find_containing_comp_unit",
25546 selftests::find_containing_comp_unit::run_test);
25547 #endif
25548 }