Let expand_symtabs_matching short-circuit
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/sect-names.h"
44 #include "dwarf2/stringify.h"
45 #include "dwarf2/public.h"
46 #include "bfd.h"
47 #include "elf-bfd.h"
48 #include "symtab.h"
49 #include "gdbtypes.h"
50 #include "objfiles.h"
51 #include "dwarf2.h"
52 #include "buildsym.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91
92 /* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95 static unsigned int dwarf_read_debug = 0;
96
97 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
98
99 #define dwarf_read_debug_printf(fmt, ...) \
100 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
101 ##__VA_ARGS__)
102
103 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
104
105 #define dwarf_read_debug_printf_v(fmt, ...) \
106 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
107 ##__VA_ARGS__)
108
109 /* When non-zero, dump DIEs after they are read in. */
110 static unsigned int dwarf_die_debug = 0;
111
112 /* When non-zero, dump line number entries as they are read in. */
113 unsigned int dwarf_line_debug = 0;
114
115 /* When true, cross-check physname against demangler. */
116 static bool check_physname = false;
117
118 /* When true, do not reject deprecated .gdb_index sections. */
119 static bool use_deprecated_index_sections = false;
120
121 /* This is used to store the data that is always per objfile. */
122 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
123
124 /* These are used to store the dwarf2_per_bfd objects.
125
126 objfiles having the same BFD, which doesn't require relocations, are going to
127 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
128
129 Other objfiles are not going to share a dwarf2_per_bfd with any other
130 objfiles, so they'll have their own version kept in the _objfile_data_key
131 version. */
132 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
133 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
134
135 /* The "aclass" indices for various kinds of computed DWARF symbols. */
136
137 static int dwarf2_locexpr_index;
138 static int dwarf2_loclist_index;
139 static int dwarf2_locexpr_block_index;
140 static int dwarf2_loclist_block_index;
141
142 /* Size of .debug_loclists section header for 32-bit DWARF format. */
143 #define LOCLIST_HEADER_SIZE32 12
144
145 /* Size of .debug_loclists section header for 64-bit DWARF format. */
146 #define LOCLIST_HEADER_SIZE64 20
147
148 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
149 #define RNGLIST_HEADER_SIZE32 12
150
151 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
152 #define RNGLIST_HEADER_SIZE64 20
153
154 /* An index into a (C++) symbol name component in a symbol name as
155 recorded in the mapped_index's symbol table. For each C++ symbol
156 in the symbol table, we record one entry for the start of each
157 component in the symbol in a table of name components, and then
158 sort the table, in order to be able to binary search symbol names,
159 ignoring leading namespaces, both completion and regular look up.
160 For example, for symbol "A::B::C", we'll have an entry that points
161 to "A::B::C", another that points to "B::C", and another for "C".
162 Note that function symbols in GDB index have no parameter
163 information, just the function/method names. You can convert a
164 name_component to a "const char *" using the
165 'mapped_index::symbol_name_at(offset_type)' method. */
166
167 struct name_component
168 {
169 /* Offset in the symbol name where the component starts. Stored as
170 a (32-bit) offset instead of a pointer to save memory and improve
171 locality on 64-bit architectures. */
172 offset_type name_offset;
173
174 /* The symbol's index in the symbol and constant pool tables of a
175 mapped_index. */
176 offset_type idx;
177 };
178
179 /* Base class containing bits shared by both .gdb_index and
180 .debug_name indexes. */
181
182 struct mapped_index_base
183 {
184 mapped_index_base () = default;
185 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
186
187 /* The name_component table (a sorted vector). See name_component's
188 description above. */
189 std::vector<name_component> name_components;
190
191 /* How NAME_COMPONENTS is sorted. */
192 enum case_sensitivity name_components_casing;
193
194 /* Return the number of names in the symbol table. */
195 virtual size_t symbol_name_count () const = 0;
196
197 /* Get the name of the symbol at IDX in the symbol table. */
198 virtual const char *symbol_name_at
199 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
200
201 /* Return whether the name at IDX in the symbol table should be
202 ignored. */
203 virtual bool symbol_name_slot_invalid (offset_type idx) const
204 {
205 return false;
206 }
207
208 /* Build the symbol name component sorted vector, if we haven't
209 yet. */
210 void build_name_components (dwarf2_per_objfile *per_objfile);
211
212 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
213 possible matches for LN_NO_PARAMS in the name component
214 vector. */
215 std::pair<std::vector<name_component>::const_iterator,
216 std::vector<name_component>::const_iterator>
217 find_name_components_bounds (const lookup_name_info &ln_no_params,
218 enum language lang,
219 dwarf2_per_objfile *per_objfile) const;
220
221 /* Prevent deleting/destroying via a base class pointer. */
222 protected:
223 ~mapped_index_base() = default;
224 };
225
226 /* A description of the mapped index. The file format is described in
227 a comment by the code that writes the index. */
228 struct mapped_index final : public mapped_index_base
229 {
230 /* A slot/bucket in the symbol table hash. */
231 struct symbol_table_slot
232 {
233 const offset_type name;
234 const offset_type vec;
235 };
236
237 /* Index data format version. */
238 int version = 0;
239
240 /* The address table data. */
241 gdb::array_view<const gdb_byte> address_table;
242
243 /* The symbol table, implemented as a hash table. */
244 gdb::array_view<symbol_table_slot> symbol_table;
245
246 /* A pointer to the constant pool. */
247 const char *constant_pool = nullptr;
248
249 bool symbol_name_slot_invalid (offset_type idx) const override
250 {
251 const auto &bucket = this->symbol_table[idx];
252 return bucket.name == 0 && bucket.vec == 0;
253 }
254
255 /* Convenience method to get at the name of the symbol at IDX in the
256 symbol table. */
257 const char *symbol_name_at
258 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
259 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
260
261 size_t symbol_name_count () const override
262 { return this->symbol_table.size (); }
263 };
264
265 /* A description of the mapped .debug_names.
266 Uninitialized map has CU_COUNT 0. */
267 struct mapped_debug_names final : public mapped_index_base
268 {
269 bfd_endian dwarf5_byte_order;
270 bool dwarf5_is_dwarf64;
271 bool augmentation_is_gdb;
272 uint8_t offset_size;
273 uint32_t cu_count = 0;
274 uint32_t tu_count, bucket_count, name_count;
275 const gdb_byte *cu_table_reordered, *tu_table_reordered;
276 const uint32_t *bucket_table_reordered, *hash_table_reordered;
277 const gdb_byte *name_table_string_offs_reordered;
278 const gdb_byte *name_table_entry_offs_reordered;
279 const gdb_byte *entry_pool;
280
281 struct index_val
282 {
283 ULONGEST dwarf_tag;
284 struct attr
285 {
286 /* Attribute name DW_IDX_*. */
287 ULONGEST dw_idx;
288
289 /* Attribute form DW_FORM_*. */
290 ULONGEST form;
291
292 /* Value if FORM is DW_FORM_implicit_const. */
293 LONGEST implicit_const;
294 };
295 std::vector<attr> attr_vec;
296 };
297
298 std::unordered_map<ULONGEST, index_val> abbrev_map;
299
300 const char *namei_to_name
301 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
302
303 /* Implementation of the mapped_index_base virtual interface, for
304 the name_components cache. */
305
306 const char *symbol_name_at
307 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
308 { return namei_to_name (idx, per_objfile); }
309
310 size_t symbol_name_count () const override
311 { return this->name_count; }
312 };
313
314 /* See dwarf2read.h. */
315
316 dwarf2_per_objfile *
317 get_dwarf2_per_objfile (struct objfile *objfile)
318 {
319 return dwarf2_objfile_data_key.get (objfile);
320 }
321
322 /* Default names of the debugging sections. */
323
324 /* Note that if the debugging section has been compressed, it might
325 have a name like .zdebug_info. */
326
327 const struct dwarf2_debug_sections dwarf2_elf_names =
328 {
329 { ".debug_info", ".zdebug_info" },
330 { ".debug_abbrev", ".zdebug_abbrev" },
331 { ".debug_line", ".zdebug_line" },
332 { ".debug_loc", ".zdebug_loc" },
333 { ".debug_loclists", ".zdebug_loclists" },
334 { ".debug_macinfo", ".zdebug_macinfo" },
335 { ".debug_macro", ".zdebug_macro" },
336 { ".debug_str", ".zdebug_str" },
337 { ".debug_str_offsets", ".zdebug_str_offsets" },
338 { ".debug_line_str", ".zdebug_line_str" },
339 { ".debug_ranges", ".zdebug_ranges" },
340 { ".debug_rnglists", ".zdebug_rnglists" },
341 { ".debug_types", ".zdebug_types" },
342 { ".debug_addr", ".zdebug_addr" },
343 { ".debug_frame", ".zdebug_frame" },
344 { ".eh_frame", NULL },
345 { ".gdb_index", ".zgdb_index" },
346 { ".debug_names", ".zdebug_names" },
347 { ".debug_aranges", ".zdebug_aranges" },
348 23
349 };
350
351 /* List of DWO/DWP sections. */
352
353 static const struct dwop_section_names
354 {
355 struct dwarf2_section_names abbrev_dwo;
356 struct dwarf2_section_names info_dwo;
357 struct dwarf2_section_names line_dwo;
358 struct dwarf2_section_names loc_dwo;
359 struct dwarf2_section_names loclists_dwo;
360 struct dwarf2_section_names macinfo_dwo;
361 struct dwarf2_section_names macro_dwo;
362 struct dwarf2_section_names rnglists_dwo;
363 struct dwarf2_section_names str_dwo;
364 struct dwarf2_section_names str_offsets_dwo;
365 struct dwarf2_section_names types_dwo;
366 struct dwarf2_section_names cu_index;
367 struct dwarf2_section_names tu_index;
368 }
369 dwop_section_names =
370 {
371 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
372 { ".debug_info.dwo", ".zdebug_info.dwo" },
373 { ".debug_line.dwo", ".zdebug_line.dwo" },
374 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
375 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
376 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
377 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
378 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
379 { ".debug_str.dwo", ".zdebug_str.dwo" },
380 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
381 { ".debug_types.dwo", ".zdebug_types.dwo" },
382 { ".debug_cu_index", ".zdebug_cu_index" },
383 { ".debug_tu_index", ".zdebug_tu_index" },
384 };
385
386 /* local data types */
387
388 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
389 begin with a header, which contains the following information. */
390 struct loclists_rnglists_header
391 {
392 /* A 4-byte or 12-byte length containing the length of the
393 set of entries for this compilation unit, not including the
394 length field itself. */
395 unsigned int length;
396
397 /* A 2-byte version identifier. */
398 short version;
399
400 /* A 1-byte unsigned integer containing the size in bytes of an address on
401 the target system. */
402 unsigned char addr_size;
403
404 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
405 on the target system. */
406 unsigned char segment_collector_size;
407
408 /* A 4-byte count of the number of offsets that follow the header. */
409 unsigned int offset_entry_count;
410 };
411
412 /* Type used for delaying computation of method physnames.
413 See comments for compute_delayed_physnames. */
414 struct delayed_method_info
415 {
416 /* The type to which the method is attached, i.e., its parent class. */
417 struct type *type;
418
419 /* The index of the method in the type's function fieldlists. */
420 int fnfield_index;
421
422 /* The index of the method in the fieldlist. */
423 int index;
424
425 /* The name of the DIE. */
426 const char *name;
427
428 /* The DIE associated with this method. */
429 struct die_info *die;
430 };
431
432 /* Internal state when decoding a particular compilation unit. */
433 struct dwarf2_cu
434 {
435 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
436 dwarf2_per_objfile *per_objfile);
437
438 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
439
440 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
441 Create the set of symtabs used by this TU, or if this TU is sharing
442 symtabs with another TU and the symtabs have already been created
443 then restore those symtabs in the line header.
444 We don't need the pc/line-number mapping for type units. */
445 void setup_type_unit_groups (struct die_info *die);
446
447 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
448 buildsym_compunit constructor. */
449 struct compunit_symtab *start_symtab (const char *name,
450 const char *comp_dir,
451 CORE_ADDR low_pc);
452
453 /* Reset the builder. */
454 void reset_builder () { m_builder.reset (); }
455
456 /* Return a type that is a generic pointer type, the size of which
457 matches the address size given in the compilation unit header for
458 this CU. */
459 struct type *addr_type () const;
460
461 /* Find an integer type the same size as the address size given in
462 the compilation unit header for this CU. UNSIGNED_P controls if
463 the integer is unsigned or not. */
464 struct type *addr_sized_int_type (bool unsigned_p) const;
465
466 /* The header of the compilation unit. */
467 struct comp_unit_head header {};
468
469 /* Base address of this compilation unit. */
470 gdb::optional<CORE_ADDR> base_address;
471
472 /* The language we are debugging. */
473 enum language language = language_unknown;
474 const struct language_defn *language_defn = nullptr;
475
476 const char *producer = nullptr;
477
478 private:
479 /* The symtab builder for this CU. This is only non-NULL when full
480 symbols are being read. */
481 std::unique_ptr<buildsym_compunit> m_builder;
482
483 public:
484 /* The generic symbol table building routines have separate lists for
485 file scope symbols and all all other scopes (local scopes). So
486 we need to select the right one to pass to add_symbol_to_list().
487 We do it by keeping a pointer to the correct list in list_in_scope.
488
489 FIXME: The original dwarf code just treated the file scope as the
490 first local scope, and all other local scopes as nested local
491 scopes, and worked fine. Check to see if we really need to
492 distinguish these in buildsym.c. */
493 struct pending **list_in_scope = nullptr;
494
495 /* Hash table holding all the loaded partial DIEs
496 with partial_die->offset.SECT_OFF as hash. */
497 htab_t partial_dies = nullptr;
498
499 /* Storage for things with the same lifetime as this read-in compilation
500 unit, including partial DIEs. */
501 auto_obstack comp_unit_obstack;
502
503 /* Backlink to our per_cu entry. */
504 struct dwarf2_per_cu_data *per_cu;
505
506 /* The dwarf2_per_objfile that owns this. */
507 dwarf2_per_objfile *per_objfile;
508
509 /* How many compilation units ago was this CU last referenced? */
510 int last_used = 0;
511
512 /* A hash table of DIE cu_offset for following references with
513 die_info->offset.sect_off as hash. */
514 htab_t die_hash = nullptr;
515
516 /* Full DIEs if read in. */
517 struct die_info *dies = nullptr;
518
519 /* A set of pointers to dwarf2_per_cu_data objects for compilation
520 units referenced by this one. Only set during full symbol processing;
521 partial symbol tables do not have dependencies. */
522 htab_t dependencies = nullptr;
523
524 /* Header data from the line table, during full symbol processing. */
525 struct line_header *line_header = nullptr;
526 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
527 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
528 this is the DW_TAG_compile_unit die for this CU. We'll hold on
529 to the line header as long as this DIE is being processed. See
530 process_die_scope. */
531 die_info *line_header_die_owner = nullptr;
532
533 /* A list of methods which need to have physnames computed
534 after all type information has been read. */
535 std::vector<delayed_method_info> method_list;
536
537 /* To be copied to symtab->call_site_htab. */
538 htab_t call_site_htab = nullptr;
539
540 /* Non-NULL if this CU came from a DWO file.
541 There is an invariant here that is important to remember:
542 Except for attributes copied from the top level DIE in the "main"
543 (or "stub") file in preparation for reading the DWO file
544 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
545 Either there isn't a DWO file (in which case this is NULL and the point
546 is moot), or there is and either we're not going to read it (in which
547 case this is NULL) or there is and we are reading it (in which case this
548 is non-NULL). */
549 struct dwo_unit *dwo_unit = nullptr;
550
551 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
552 Note this value comes from the Fission stub CU/TU's DIE. */
553 gdb::optional<ULONGEST> addr_base;
554
555 /* The DW_AT_GNU_ranges_base attribute, if present.
556
557 This is only relevant in the context of pre-DWARF 5 split units. In this
558 context, there is a .debug_ranges section in the linked executable,
559 containing all the ranges data for all the compilation units. Each
560 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
561 indicates the base of its contribution to that section. The DW_AT_ranges
562 attributes in the split-unit are of the form DW_FORM_sec_offset and point
563 into the .debug_ranges section of the linked file. However, they are not
564 "true" DW_FORM_sec_offset, because they are relative to the base of their
565 compilation unit's contribution, rather than relative to the beginning of
566 the section. The DW_AT_GNU_ranges_base value must be added to it to make
567 it relative to the beginning of the section.
568
569 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
570 case, so this value can be added without needing to know whether we are in
571 this case or not.
572
573 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
574 skeleton/stub, it must not have the base added, as it already points to the
575 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
576 have a DW_AT_ranges attribute, we can use the
577
578 die->tag != DW_AT_compile_unit
579
580 to determine whether the base should be added or not. */
581 ULONGEST gnu_ranges_base = 0;
582
583 /* The DW_AT_rnglists_base attribute, if present.
584
585 This is used when processing attributes of form DW_FORM_rnglistx in
586 non-split units. Attributes of this form found in a split unit don't
587 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
588 section. */
589 ULONGEST rnglists_base = 0;
590
591 /* The DW_AT_loclists_base attribute if present. */
592 ULONGEST loclist_base = 0;
593
594 /* When reading debug info generated by older versions of rustc, we
595 have to rewrite some union types to be struct types with a
596 variant part. This rewriting must be done after the CU is fully
597 read in, because otherwise at the point of rewriting some struct
598 type might not have been fully processed. So, we keep a list of
599 all such types here and process them after expansion. */
600 std::vector<struct type *> rust_unions;
601
602 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
603 files, the value is implicitly zero. For DWARF 5 version DWO files, the
604 value is often implicit and is the size of the header of
605 .debug_str_offsets section (8 or 4, depending on the address size). */
606 gdb::optional<ULONGEST> str_offsets_base;
607
608 /* Mark used when releasing cached dies. */
609 bool mark : 1;
610
611 /* This CU references .debug_loc. See the symtab->locations_valid field.
612 This test is imperfect as there may exist optimized debug code not using
613 any location list and still facing inlining issues if handled as
614 unoptimized code. For a future better test see GCC PR other/32998. */
615 bool has_loclist : 1;
616
617 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
618 if all the producer_is_* fields are valid. This information is cached
619 because profiling CU expansion showed excessive time spent in
620 producer_is_gxx_lt_4_6. */
621 bool checked_producer : 1;
622 bool producer_is_gxx_lt_4_6 : 1;
623 bool producer_is_gcc_lt_4_3 : 1;
624 bool producer_is_icc : 1;
625 bool producer_is_icc_lt_14 : 1;
626 bool producer_is_codewarrior : 1;
627
628 /* When true, the file that we're processing is known to have
629 debugging info for C++ namespaces. GCC 3.3.x did not produce
630 this information, but later versions do. */
631
632 bool processing_has_namespace_info : 1;
633
634 struct partial_die_info *find_partial_die (sect_offset sect_off);
635
636 /* If this CU was inherited by another CU (via specification,
637 abstract_origin, etc), this is the ancestor CU. */
638 dwarf2_cu *ancestor;
639
640 /* Get the buildsym_compunit for this CU. */
641 buildsym_compunit *get_builder ()
642 {
643 /* If this CU has a builder associated with it, use that. */
644 if (m_builder != nullptr)
645 return m_builder.get ();
646
647 /* Otherwise, search ancestors for a valid builder. */
648 if (ancestor != nullptr)
649 return ancestor->get_builder ();
650
651 return nullptr;
652 }
653 };
654
655 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
656 This includes type_unit_group and quick_file_names. */
657
658 struct stmt_list_hash
659 {
660 /* The DWO unit this table is from or NULL if there is none. */
661 struct dwo_unit *dwo_unit;
662
663 /* Offset in .debug_line or .debug_line.dwo. */
664 sect_offset line_sect_off;
665 };
666
667 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
668 an object of this type. This contains elements of type unit groups
669 that can be shared across objfiles. The non-shareable parts are in
670 type_unit_group_unshareable. */
671
672 struct type_unit_group
673 {
674 /* dwarf2read.c's main "handle" on a TU symtab.
675 To simplify things we create an artificial CU that "includes" all the
676 type units using this stmt_list so that the rest of the code still has
677 a "per_cu" handle on the symtab. */
678 struct dwarf2_per_cu_data per_cu;
679
680 /* The TUs that share this DW_AT_stmt_list entry.
681 This is added to while parsing type units to build partial symtabs,
682 and is deleted afterwards and not used again. */
683 std::vector<signatured_type *> *tus;
684
685 /* The data used to construct the hash key. */
686 struct stmt_list_hash hash;
687 };
688
689 /* These sections are what may appear in a (real or virtual) DWO file. */
690
691 struct dwo_sections
692 {
693 struct dwarf2_section_info abbrev;
694 struct dwarf2_section_info line;
695 struct dwarf2_section_info loc;
696 struct dwarf2_section_info loclists;
697 struct dwarf2_section_info macinfo;
698 struct dwarf2_section_info macro;
699 struct dwarf2_section_info rnglists;
700 struct dwarf2_section_info str;
701 struct dwarf2_section_info str_offsets;
702 /* In the case of a virtual DWO file, these two are unused. */
703 struct dwarf2_section_info info;
704 std::vector<dwarf2_section_info> types;
705 };
706
707 /* CUs/TUs in DWP/DWO files. */
708
709 struct dwo_unit
710 {
711 /* Backlink to the containing struct dwo_file. */
712 struct dwo_file *dwo_file;
713
714 /* The "id" that distinguishes this CU/TU.
715 .debug_info calls this "dwo_id", .debug_types calls this "signature".
716 Since signatures came first, we stick with it for consistency. */
717 ULONGEST signature;
718
719 /* The section this CU/TU lives in, in the DWO file. */
720 struct dwarf2_section_info *section;
721
722 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
723 sect_offset sect_off;
724 unsigned int length;
725
726 /* For types, offset in the type's DIE of the type defined by this TU. */
727 cu_offset type_offset_in_tu;
728 };
729
730 /* include/dwarf2.h defines the DWP section codes.
731 It defines a max value but it doesn't define a min value, which we
732 use for error checking, so provide one. */
733
734 enum dwp_v2_section_ids
735 {
736 DW_SECT_MIN = 1
737 };
738
739 /* Data for one DWO file.
740
741 This includes virtual DWO files (a virtual DWO file is a DWO file as it
742 appears in a DWP file). DWP files don't really have DWO files per se -
743 comdat folding of types "loses" the DWO file they came from, and from
744 a high level view DWP files appear to contain a mass of random types.
745 However, to maintain consistency with the non-DWP case we pretend DWP
746 files contain virtual DWO files, and we assign each TU with one virtual
747 DWO file (generally based on the line and abbrev section offsets -
748 a heuristic that seems to work in practice). */
749
750 struct dwo_file
751 {
752 dwo_file () = default;
753 DISABLE_COPY_AND_ASSIGN (dwo_file);
754
755 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
756 For virtual DWO files the name is constructed from the section offsets
757 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
758 from related CU+TUs. */
759 const char *dwo_name = nullptr;
760
761 /* The DW_AT_comp_dir attribute. */
762 const char *comp_dir = nullptr;
763
764 /* The bfd, when the file is open. Otherwise this is NULL.
765 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
766 gdb_bfd_ref_ptr dbfd;
767
768 /* The sections that make up this DWO file.
769 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
770 sections (for lack of a better name). */
771 struct dwo_sections sections {};
772
773 /* The CUs in the file.
774 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
775 an extension to handle LLVM's Link Time Optimization output (where
776 multiple source files may be compiled into a single object/dwo pair). */
777 htab_up cus;
778
779 /* Table of TUs in the file.
780 Each element is a struct dwo_unit. */
781 htab_up tus;
782 };
783
784 /* These sections are what may appear in a DWP file. */
785
786 struct dwp_sections
787 {
788 /* These are used by all DWP versions (1, 2 and 5). */
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
792
793 /* These are only used by DWP version 2 and version 5 files.
794 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
795 sections are referenced by section number, and are not recorded here.
796 In DWP version 2 or 5 there is at most one copy of all these sections,
797 each section being (effectively) comprised of the concatenation of all of
798 the individual sections that exist in the version 1 format.
799 To keep the code simple we treat each of these concatenated pieces as a
800 section itself (a virtual section?). */
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info info;
803 struct dwarf2_section_info line;
804 struct dwarf2_section_info loc;
805 struct dwarf2_section_info loclists;
806 struct dwarf2_section_info macinfo;
807 struct dwarf2_section_info macro;
808 struct dwarf2_section_info rnglists;
809 struct dwarf2_section_info str_offsets;
810 struct dwarf2_section_info types;
811 };
812
813 /* These sections are what may appear in a virtual DWO file in DWP version 1.
814 A virtual DWO file is a DWO file as it appears in a DWP file. */
815
816 struct virtual_v1_dwo_sections
817 {
818 struct dwarf2_section_info abbrev;
819 struct dwarf2_section_info line;
820 struct dwarf2_section_info loc;
821 struct dwarf2_section_info macinfo;
822 struct dwarf2_section_info macro;
823 struct dwarf2_section_info str_offsets;
824 /* Each DWP hash table entry records one CU or one TU.
825 That is recorded here, and copied to dwo_unit.section. */
826 struct dwarf2_section_info info_or_types;
827 };
828
829 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
830 In version 2, the sections of the DWO files are concatenated together
831 and stored in one section of that name. Thus each ELF section contains
832 several "virtual" sections. */
833
834 struct virtual_v2_or_v5_dwo_sections
835 {
836 bfd_size_type abbrev_offset;
837 bfd_size_type abbrev_size;
838
839 bfd_size_type line_offset;
840 bfd_size_type line_size;
841
842 bfd_size_type loc_offset;
843 bfd_size_type loc_size;
844
845 bfd_size_type loclists_offset;
846 bfd_size_type loclists_size;
847
848 bfd_size_type macinfo_offset;
849 bfd_size_type macinfo_size;
850
851 bfd_size_type macro_offset;
852 bfd_size_type macro_size;
853
854 bfd_size_type rnglists_offset;
855 bfd_size_type rnglists_size;
856
857 bfd_size_type str_offsets_offset;
858 bfd_size_type str_offsets_size;
859
860 /* Each DWP hash table entry records one CU or one TU.
861 That is recorded here, and copied to dwo_unit.section. */
862 bfd_size_type info_or_types_offset;
863 bfd_size_type info_or_types_size;
864 };
865
866 /* Contents of DWP hash tables. */
867
868 struct dwp_hash_table
869 {
870 uint32_t version, nr_columns;
871 uint32_t nr_units, nr_slots;
872 const gdb_byte *hash_table, *unit_table;
873 union
874 {
875 struct
876 {
877 const gdb_byte *indices;
878 } v1;
879 struct
880 {
881 /* This is indexed by column number and gives the id of the section
882 in that column. */
883 #define MAX_NR_V2_DWO_SECTIONS \
884 (1 /* .debug_info or .debug_types */ \
885 + 1 /* .debug_abbrev */ \
886 + 1 /* .debug_line */ \
887 + 1 /* .debug_loc */ \
888 + 1 /* .debug_str_offsets */ \
889 + 1 /* .debug_macro or .debug_macinfo */)
890 int section_ids[MAX_NR_V2_DWO_SECTIONS];
891 const gdb_byte *offsets;
892 const gdb_byte *sizes;
893 } v2;
894 struct
895 {
896 /* This is indexed by column number and gives the id of the section
897 in that column. */
898 #define MAX_NR_V5_DWO_SECTIONS \
899 (1 /* .debug_info */ \
900 + 1 /* .debug_abbrev */ \
901 + 1 /* .debug_line */ \
902 + 1 /* .debug_loclists */ \
903 + 1 /* .debug_str_offsets */ \
904 + 1 /* .debug_macro */ \
905 + 1 /* .debug_rnglists */)
906 int section_ids[MAX_NR_V5_DWO_SECTIONS];
907 const gdb_byte *offsets;
908 const gdb_byte *sizes;
909 } v5;
910 } section_pool;
911 };
912
913 /* Data for one DWP file. */
914
915 struct dwp_file
916 {
917 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
918 : name (name_),
919 dbfd (std::move (abfd))
920 {
921 }
922
923 /* Name of the file. */
924 const char *name;
925
926 /* File format version. */
927 int version = 0;
928
929 /* The bfd. */
930 gdb_bfd_ref_ptr dbfd;
931
932 /* Section info for this file. */
933 struct dwp_sections sections {};
934
935 /* Table of CUs in the file. */
936 const struct dwp_hash_table *cus = nullptr;
937
938 /* Table of TUs in the file. */
939 const struct dwp_hash_table *tus = nullptr;
940
941 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
942 htab_up loaded_cus;
943 htab_up loaded_tus;
944
945 /* Table to map ELF section numbers to their sections.
946 This is only needed for the DWP V1 file format. */
947 unsigned int num_sections = 0;
948 asection **elf_sections = nullptr;
949 };
950
951 /* Struct used to pass misc. parameters to read_die_and_children, et
952 al. which are used for both .debug_info and .debug_types dies.
953 All parameters here are unchanging for the life of the call. This
954 struct exists to abstract away the constant parameters of die reading. */
955
956 struct die_reader_specs
957 {
958 /* The bfd of die_section. */
959 bfd *abfd;
960
961 /* The CU of the DIE we are parsing. */
962 struct dwarf2_cu *cu;
963
964 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
965 struct dwo_file *dwo_file;
966
967 /* The section the die comes from.
968 This is either .debug_info or .debug_types, or the .dwo variants. */
969 struct dwarf2_section_info *die_section;
970
971 /* die_section->buffer. */
972 const gdb_byte *buffer;
973
974 /* The end of the buffer. */
975 const gdb_byte *buffer_end;
976
977 /* The abbreviation table to use when reading the DIEs. */
978 struct abbrev_table *abbrev_table;
979 };
980
981 /* A subclass of die_reader_specs that holds storage and has complex
982 constructor and destructor behavior. */
983
984 class cutu_reader : public die_reader_specs
985 {
986 public:
987
988 cutu_reader (dwarf2_per_cu_data *this_cu,
989 dwarf2_per_objfile *per_objfile,
990 struct abbrev_table *abbrev_table,
991 dwarf2_cu *existing_cu,
992 bool skip_partial);
993
994 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
995 dwarf2_per_objfile *per_objfile,
996 struct dwarf2_cu *parent_cu = nullptr,
997 struct dwo_file *dwo_file = nullptr);
998
999 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1000
1001 const gdb_byte *info_ptr = nullptr;
1002 struct die_info *comp_unit_die = nullptr;
1003 bool dummy_p = false;
1004
1005 /* Release the new CU, putting it on the chain. This cannot be done
1006 for dummy CUs. */
1007 void keep ();
1008
1009 private:
1010 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1011 dwarf2_per_objfile *per_objfile,
1012 dwarf2_cu *existing_cu);
1013
1014 struct dwarf2_per_cu_data *m_this_cu;
1015 std::unique_ptr<dwarf2_cu> m_new_cu;
1016
1017 /* The ordinary abbreviation table. */
1018 abbrev_table_up m_abbrev_table_holder;
1019
1020 /* The DWO abbreviation table. */
1021 abbrev_table_up m_dwo_abbrev_table;
1022 };
1023
1024 /* When we construct a partial symbol table entry we only
1025 need this much information. */
1026 struct partial_die_info : public allocate_on_obstack
1027 {
1028 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
1029
1030 /* Disable assign but still keep copy ctor, which is needed
1031 load_partial_dies. */
1032 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1033
1034 /* Adjust the partial die before generating a symbol for it. This
1035 function may set the is_external flag or change the DIE's
1036 name. */
1037 void fixup (struct dwarf2_cu *cu);
1038
1039 /* Read a minimal amount of information into the minimal die
1040 structure. */
1041 const gdb_byte *read (const struct die_reader_specs *reader,
1042 const struct abbrev_info &abbrev,
1043 const gdb_byte *info_ptr);
1044
1045 /* Compute the name of this partial DIE. This memoizes the
1046 result, so it is safe to call multiple times. */
1047 const char *name (dwarf2_cu *cu);
1048
1049 /* Offset of this DIE. */
1050 const sect_offset sect_off;
1051
1052 /* DWARF-2 tag for this DIE. */
1053 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1054
1055 /* Assorted flags describing the data found in this DIE. */
1056 const unsigned int has_children : 1;
1057
1058 unsigned int is_external : 1;
1059 unsigned int is_declaration : 1;
1060 unsigned int has_type : 1;
1061 unsigned int has_specification : 1;
1062 unsigned int has_pc_info : 1;
1063 unsigned int may_be_inlined : 1;
1064
1065 /* This DIE has been marked DW_AT_main_subprogram. */
1066 unsigned int main_subprogram : 1;
1067
1068 /* Flag set if the SCOPE field of this structure has been
1069 computed. */
1070 unsigned int scope_set : 1;
1071
1072 /* Flag set if the DIE has a byte_size attribute. */
1073 unsigned int has_byte_size : 1;
1074
1075 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1076 unsigned int has_const_value : 1;
1077
1078 /* Flag set if any of the DIE's children are template arguments. */
1079 unsigned int has_template_arguments : 1;
1080
1081 /* Flag set if fixup has been called on this die. */
1082 unsigned int fixup_called : 1;
1083
1084 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1085 unsigned int is_dwz : 1;
1086
1087 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1088 unsigned int spec_is_dwz : 1;
1089
1090 unsigned int canonical_name : 1;
1091
1092 /* The name of this DIE. Normally the value of DW_AT_name, but
1093 sometimes a default name for unnamed DIEs. */
1094 const char *raw_name = nullptr;
1095
1096 /* The linkage name, if present. */
1097 const char *linkage_name = nullptr;
1098
1099 /* The scope to prepend to our children. This is generally
1100 allocated on the comp_unit_obstack, so will disappear
1101 when this compilation unit leaves the cache. */
1102 const char *scope = nullptr;
1103
1104 /* Some data associated with the partial DIE. The tag determines
1105 which field is live. */
1106 union
1107 {
1108 /* The location description associated with this DIE, if any. */
1109 struct dwarf_block *locdesc;
1110 /* The offset of an import, for DW_TAG_imported_unit. */
1111 sect_offset sect_off;
1112 } d {};
1113
1114 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1115 CORE_ADDR lowpc = 0;
1116 CORE_ADDR highpc = 0;
1117
1118 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1119 DW_AT_sibling, if any. */
1120 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1121 could return DW_AT_sibling values to its caller load_partial_dies. */
1122 const gdb_byte *sibling = nullptr;
1123
1124 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1125 DW_AT_specification (or DW_AT_abstract_origin or
1126 DW_AT_extension). */
1127 sect_offset spec_offset {};
1128
1129 /* Pointers to this DIE's parent, first child, and next sibling,
1130 if any. */
1131 struct partial_die_info *die_parent = nullptr;
1132 struct partial_die_info *die_child = nullptr;
1133 struct partial_die_info *die_sibling = nullptr;
1134
1135 friend struct partial_die_info *
1136 dwarf2_cu::find_partial_die (sect_offset sect_off);
1137
1138 private:
1139 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1140 partial_die_info (sect_offset sect_off)
1141 : partial_die_info (sect_off, DW_TAG_padding, 0)
1142 {
1143 }
1144
1145 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1146 int has_children_)
1147 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1148 {
1149 is_external = 0;
1150 is_declaration = 0;
1151 has_type = 0;
1152 has_specification = 0;
1153 has_pc_info = 0;
1154 may_be_inlined = 0;
1155 main_subprogram = 0;
1156 scope_set = 0;
1157 has_byte_size = 0;
1158 has_const_value = 0;
1159 has_template_arguments = 0;
1160 fixup_called = 0;
1161 is_dwz = 0;
1162 spec_is_dwz = 0;
1163 canonical_name = 0;
1164 }
1165 };
1166
1167 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1168 but this would require a corresponding change in unpack_field_as_long
1169 and friends. */
1170 static int bits_per_byte = 8;
1171
1172 struct variant_part_builder;
1173
1174 /* When reading a variant, we track a bit more information about the
1175 field, and store it in an object of this type. */
1176
1177 struct variant_field
1178 {
1179 int first_field = -1;
1180 int last_field = -1;
1181
1182 /* A variant can contain other variant parts. */
1183 std::vector<variant_part_builder> variant_parts;
1184
1185 /* If we see a DW_TAG_variant, then this will be set if this is the
1186 default branch. */
1187 bool default_branch = false;
1188 /* If we see a DW_AT_discr_value, then this will be the discriminant
1189 value. */
1190 ULONGEST discriminant_value = 0;
1191 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1192 data. */
1193 struct dwarf_block *discr_list_data = nullptr;
1194 };
1195
1196 /* This represents a DW_TAG_variant_part. */
1197
1198 struct variant_part_builder
1199 {
1200 /* The offset of the discriminant field. */
1201 sect_offset discriminant_offset {};
1202
1203 /* Variants that are direct children of this variant part. */
1204 std::vector<variant_field> variants;
1205
1206 /* True if we're currently reading a variant. */
1207 bool processing_variant = false;
1208 };
1209
1210 struct nextfield
1211 {
1212 int accessibility = 0;
1213 int virtuality = 0;
1214 /* Variant parts need to find the discriminant, which is a DIE
1215 reference. We track the section offset of each field to make
1216 this link. */
1217 sect_offset offset;
1218 struct field field {};
1219 };
1220
1221 struct fnfieldlist
1222 {
1223 const char *name = nullptr;
1224 std::vector<struct fn_field> fnfields;
1225 };
1226
1227 /* The routines that read and process dies for a C struct or C++ class
1228 pass lists of data member fields and lists of member function fields
1229 in an instance of a field_info structure, as defined below. */
1230 struct field_info
1231 {
1232 /* List of data member and baseclasses fields. */
1233 std::vector<struct nextfield> fields;
1234 std::vector<struct nextfield> baseclasses;
1235
1236 /* Set if the accessibility of one of the fields is not public. */
1237 bool non_public_fields = false;
1238
1239 /* Member function fieldlist array, contains name of possibly overloaded
1240 member function, number of overloaded member functions and a pointer
1241 to the head of the member function field chain. */
1242 std::vector<struct fnfieldlist> fnfieldlists;
1243
1244 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1245 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1246 std::vector<struct decl_field> typedef_field_list;
1247
1248 /* Nested types defined by this class and the number of elements in this
1249 list. */
1250 std::vector<struct decl_field> nested_types_list;
1251
1252 /* If non-null, this is the variant part we are currently
1253 reading. */
1254 variant_part_builder *current_variant_part = nullptr;
1255 /* This holds all the top-level variant parts attached to the type
1256 we're reading. */
1257 std::vector<variant_part_builder> variant_parts;
1258
1259 /* Return the total number of fields (including baseclasses). */
1260 int nfields () const
1261 {
1262 return fields.size () + baseclasses.size ();
1263 }
1264 };
1265
1266 /* Loaded secondary compilation units are kept in memory until they
1267 have not been referenced for the processing of this many
1268 compilation units. Set this to zero to disable caching. Cache
1269 sizes of up to at least twenty will improve startup time for
1270 typical inter-CU-reference binaries, at an obvious memory cost. */
1271 static int dwarf_max_cache_age = 5;
1272 static void
1273 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1274 struct cmd_list_element *c, const char *value)
1275 {
1276 fprintf_filtered (file, _("The upper bound on the age of cached "
1277 "DWARF compilation units is %s.\n"),
1278 value);
1279 }
1280 \f
1281 /* local function prototypes */
1282
1283 static void dwarf2_find_base_address (struct die_info *die,
1284 struct dwarf2_cu *cu);
1285
1286 static dwarf2_psymtab *create_partial_symtab
1287 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1288 const char *name);
1289
1290 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1291 const gdb_byte *info_ptr,
1292 struct die_info *type_unit_die);
1293
1294 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1295
1296 static void scan_partial_symbols (struct partial_die_info *,
1297 CORE_ADDR *, CORE_ADDR *,
1298 int, struct dwarf2_cu *);
1299
1300 static void add_partial_symbol (struct partial_die_info *,
1301 struct dwarf2_cu *);
1302
1303 static void add_partial_namespace (struct partial_die_info *pdi,
1304 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1305 int set_addrmap, struct dwarf2_cu *cu);
1306
1307 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1308 CORE_ADDR *highpc, int set_addrmap,
1309 struct dwarf2_cu *cu);
1310
1311 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1312 struct dwarf2_cu *cu);
1313
1314 static void add_partial_subprogram (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1316 int need_pc, struct dwarf2_cu *cu);
1317
1318 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1319
1320 static struct partial_die_info *load_partial_dies
1321 (const struct die_reader_specs *, const gdb_byte *, int);
1322
1323 /* A pair of partial_die_info and compilation unit. */
1324 struct cu_partial_die_info
1325 {
1326 /* The compilation unit of the partial_die_info. */
1327 struct dwarf2_cu *cu;
1328 /* A partial_die_info. */
1329 struct partial_die_info *pdi;
1330
1331 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1332 : cu (cu),
1333 pdi (pdi)
1334 { /* Nothing. */ }
1335
1336 private:
1337 cu_partial_die_info () = delete;
1338 };
1339
1340 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1341 struct dwarf2_cu *);
1342
1343 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1344 struct attribute *,
1345 const struct attr_abbrev *,
1346 const gdb_byte *);
1347
1348 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1349 struct attribute *attr, dwarf_tag tag);
1350
1351 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1352
1353 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1354 dwarf2_section_info *, sect_offset);
1355
1356 static const char *read_indirect_string
1357 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1358 const struct comp_unit_head *, unsigned int *);
1359
1360 static const char *read_indirect_string_at_offset
1361 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1362
1363 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1364 const gdb_byte *,
1365 unsigned int *);
1366
1367 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1368 ULONGEST str_index);
1369
1370 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1371 ULONGEST str_index);
1372
1373 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1374
1375 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1376 struct dwarf2_cu *);
1377
1378 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1379 struct dwarf2_cu *cu);
1380
1381 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1382
1383 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1384 struct dwarf2_cu *cu);
1385
1386 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1387
1388 static struct die_info *die_specification (struct die_info *die,
1389 struct dwarf2_cu **);
1390
1391 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1392 struct dwarf2_cu *cu);
1393
1394 static void dwarf_decode_lines (struct line_header *, const char *,
1395 struct dwarf2_cu *, dwarf2_psymtab *,
1396 CORE_ADDR, int decode_mapping);
1397
1398 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1399 const char *);
1400
1401 static struct symbol *new_symbol (struct die_info *, struct type *,
1402 struct dwarf2_cu *, struct symbol * = NULL);
1403
1404 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1405 struct dwarf2_cu *);
1406
1407 static void dwarf2_const_value_attr (const struct attribute *attr,
1408 struct type *type,
1409 const char *name,
1410 struct obstack *obstack,
1411 struct dwarf2_cu *cu, LONGEST *value,
1412 const gdb_byte **bytes,
1413 struct dwarf2_locexpr_baton **baton);
1414
1415 static struct type *read_subrange_index_type (struct die_info *die,
1416 struct dwarf2_cu *cu);
1417
1418 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1419
1420 static int need_gnat_info (struct dwarf2_cu *);
1421
1422 static struct type *die_descriptive_type (struct die_info *,
1423 struct dwarf2_cu *);
1424
1425 static void set_descriptive_type (struct type *, struct die_info *,
1426 struct dwarf2_cu *);
1427
1428 static struct type *die_containing_type (struct die_info *,
1429 struct dwarf2_cu *);
1430
1431 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1432 struct dwarf2_cu *);
1433
1434 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1435
1436 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1437
1438 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1439
1440 static char *typename_concat (struct obstack *obs, const char *prefix,
1441 const char *suffix, int physname,
1442 struct dwarf2_cu *cu);
1443
1444 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1445
1446 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1447
1448 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1449
1450 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1451
1452 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1453
1454 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1455
1456 /* Return the .debug_loclists section to use for cu. */
1457 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1458
1459 /* Return the .debug_rnglists section to use for cu. */
1460 static struct dwarf2_section_info *cu_debug_rnglists_section
1461 (struct dwarf2_cu *cu, dwarf_tag tag);
1462
1463 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1464 values. Keep the items ordered with increasing constraints compliance. */
1465 enum pc_bounds_kind
1466 {
1467 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1468 PC_BOUNDS_NOT_PRESENT,
1469
1470 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1471 were present but they do not form a valid range of PC addresses. */
1472 PC_BOUNDS_INVALID,
1473
1474 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1475 PC_BOUNDS_RANGES,
1476
1477 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1478 PC_BOUNDS_HIGH_LOW,
1479 };
1480
1481 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1482 CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *,
1484 dwarf2_psymtab *);
1485
1486 static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
1490 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1491 CORE_ADDR, struct dwarf2_cu *);
1492
1493 static void dwarf2_add_field (struct field_info *, struct die_info *,
1494 struct dwarf2_cu *);
1495
1496 static void dwarf2_attach_fields_to_type (struct field_info *,
1497 struct type *, struct dwarf2_cu *);
1498
1499 static void dwarf2_add_member_fn (struct field_info *,
1500 struct die_info *, struct type *,
1501 struct dwarf2_cu *);
1502
1503 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1504 struct type *,
1505 struct dwarf2_cu *);
1506
1507 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1508
1509 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1510
1511 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1512
1513 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
1515 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1516
1517 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1518
1519 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1520
1521 static struct type *read_module_type (struct die_info *die,
1522 struct dwarf2_cu *cu);
1523
1524 static const char *namespace_name (struct die_info *die,
1525 int *is_anonymous, struct dwarf2_cu *);
1526
1527 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1528
1529 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1530 bool * = nullptr);
1531
1532 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1533 struct dwarf2_cu *);
1534
1535 static struct die_info *read_die_and_siblings_1
1536 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1537 struct die_info *);
1538
1539 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1540 const gdb_byte *info_ptr,
1541 const gdb_byte **new_info_ptr,
1542 struct die_info *parent);
1543
1544 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1545 struct die_info **, const gdb_byte *,
1546 int);
1547
1548 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1549 struct die_info **, const gdb_byte *);
1550
1551 static void process_die (struct die_info *, struct dwarf2_cu *);
1552
1553 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1554 struct objfile *);
1555
1556 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1557
1558 static const char *dwarf2_full_name (const char *name,
1559 struct die_info *die,
1560 struct dwarf2_cu *cu);
1561
1562 static const char *dwarf2_physname (const char *name, struct die_info *die,
1563 struct dwarf2_cu *cu);
1564
1565 static struct die_info *dwarf2_extension (struct die_info *die,
1566 struct dwarf2_cu **);
1567
1568 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1569
1570 static void dump_die_for_error (struct die_info *);
1571
1572 static void dump_die_1 (struct ui_file *, int level, int max_level,
1573 struct die_info *);
1574
1575 /*static*/ void dump_die (struct die_info *, int max_level);
1576
1577 static void store_in_ref_table (struct die_info *,
1578 struct dwarf2_cu *);
1579
1580 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1581 const struct attribute *,
1582 struct dwarf2_cu **);
1583
1584 static struct die_info *follow_die_ref (struct die_info *,
1585 const struct attribute *,
1586 struct dwarf2_cu **);
1587
1588 static struct die_info *follow_die_sig (struct die_info *,
1589 const struct attribute *,
1590 struct dwarf2_cu **);
1591
1592 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1593 struct dwarf2_cu *);
1594
1595 static struct type *get_DW_AT_signature_type (struct die_info *,
1596 const struct attribute *,
1597 struct dwarf2_cu *);
1598
1599 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1600 dwarf2_per_objfile *per_objfile);
1601
1602 static void read_signatured_type (signatured_type *sig_type,
1603 dwarf2_per_objfile *per_objfile);
1604
1605 static int attr_to_dynamic_prop (const struct attribute *attr,
1606 struct die_info *die, struct dwarf2_cu *cu,
1607 struct dynamic_prop *prop, struct type *type);
1608
1609 /* memory allocation interface */
1610
1611 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1612
1613 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1614
1615 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1616
1617 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1618 struct dwarf2_loclist_baton *baton,
1619 const struct attribute *attr);
1620
1621 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1622 struct symbol *sym,
1623 struct dwarf2_cu *cu,
1624 int is_block);
1625
1626 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1627 const gdb_byte *info_ptr,
1628 const struct abbrev_info *abbrev);
1629
1630 static hashval_t partial_die_hash (const void *item);
1631
1632 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1633
1634 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1635 (sect_offset sect_off, unsigned int offset_in_dwz,
1636 dwarf2_per_objfile *per_objfile);
1637
1638 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1639 struct die_info *comp_unit_die,
1640 enum language pretend_language);
1641
1642 static struct type *set_die_type (struct die_info *, struct type *,
1643 struct dwarf2_cu *, bool = false);
1644
1645 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1646
1647 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1648
1649 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1650 dwarf2_per_objfile *per_objfile,
1651 dwarf2_cu *existing_cu,
1652 bool skip_partial,
1653 enum language pretend_language);
1654
1655 static void process_full_comp_unit (dwarf2_cu *cu,
1656 enum language pretend_language);
1657
1658 static void process_full_type_unit (dwarf2_cu *cu,
1659 enum language pretend_language);
1660
1661 static void dwarf2_add_dependence (struct dwarf2_cu *,
1662 struct dwarf2_per_cu_data *);
1663
1664 static void dwarf2_mark (struct dwarf2_cu *);
1665
1666 static struct type *get_die_type_at_offset (sect_offset,
1667 dwarf2_per_cu_data *per_cu,
1668 dwarf2_per_objfile *per_objfile);
1669
1670 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1671
1672 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1673 dwarf2_per_objfile *per_objfile,
1674 enum language pretend_language);
1675
1676 static void process_queue (dwarf2_per_objfile *per_objfile);
1677
1678 /* Class, the destructor of which frees all allocated queue entries. This
1679 will only have work to do if an error was thrown while processing the
1680 dwarf. If no error was thrown then the queue entries should have all
1681 been processed, and freed, as we went along. */
1682
1683 class dwarf2_queue_guard
1684 {
1685 public:
1686 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1687 : m_per_objfile (per_objfile)
1688 {
1689 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1690
1691 m_per_objfile->per_bfd->queue.emplace ();
1692 }
1693
1694 /* Free any entries remaining on the queue. There should only be
1695 entries left if we hit an error while processing the dwarf. */
1696 ~dwarf2_queue_guard ()
1697 {
1698 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1699
1700 m_per_objfile->per_bfd->queue.reset ();
1701 }
1702
1703 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1704
1705 private:
1706 dwarf2_per_objfile *m_per_objfile;
1707 };
1708
1709 dwarf2_queue_item::~dwarf2_queue_item ()
1710 {
1711 /* Anything still marked queued is likely to be in an
1712 inconsistent state, so discard it. */
1713 if (per_cu->queued)
1714 {
1715 per_objfile->remove_cu (per_cu);
1716 per_cu->queued = 0;
1717 }
1718 }
1719
1720 /* The return type of find_file_and_directory. Note, the enclosed
1721 string pointers are only valid while this object is valid. */
1722
1723 struct file_and_directory
1724 {
1725 /* The filename. This is never NULL. */
1726 const char *name;
1727
1728 /* The compilation directory. NULL if not known. If we needed to
1729 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1730 points directly to the DW_AT_comp_dir string attribute owned by
1731 the obstack that owns the DIE. */
1732 const char *comp_dir;
1733
1734 /* If we needed to build a new string for comp_dir, this is what
1735 owns the storage. */
1736 std::string comp_dir_storage;
1737 };
1738
1739 static file_and_directory find_file_and_directory (struct die_info *die,
1740 struct dwarf2_cu *cu);
1741
1742 static htab_up allocate_signatured_type_table ();
1743
1744 static htab_up allocate_dwo_unit_table ();
1745
1746 static struct dwo_unit *lookup_dwo_unit_in_dwp
1747 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1748 const char *comp_dir, ULONGEST signature, int is_debug_types);
1749
1750 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1751
1752 static struct dwo_unit *lookup_dwo_comp_unit
1753 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1754 ULONGEST signature);
1755
1756 static struct dwo_unit *lookup_dwo_type_unit
1757 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1758
1759 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1760
1761 /* A unique pointer to a dwo_file. */
1762
1763 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1764
1765 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1766
1767 static void check_producer (struct dwarf2_cu *cu);
1768
1769 static void free_line_header_voidp (void *arg);
1770 \f
1771 /* Various complaints about symbol reading that don't abort the process. */
1772
1773 static void
1774 dwarf2_debug_line_missing_file_complaint (void)
1775 {
1776 complaint (_(".debug_line section has line data without a file"));
1777 }
1778
1779 static void
1780 dwarf2_debug_line_missing_end_sequence_complaint (void)
1781 {
1782 complaint (_(".debug_line section has line "
1783 "program sequence without an end"));
1784 }
1785
1786 static void
1787 dwarf2_complex_location_expr_complaint (void)
1788 {
1789 complaint (_("location expression too complex"));
1790 }
1791
1792 static void
1793 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1794 int arg3)
1795 {
1796 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1797 arg1, arg2, arg3);
1798 }
1799
1800 static void
1801 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1802 {
1803 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1804 arg1, arg2);
1805 }
1806
1807 /* Hash function for line_header_hash. */
1808
1809 static hashval_t
1810 line_header_hash (const struct line_header *ofs)
1811 {
1812 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1813 }
1814
1815 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1816
1817 static hashval_t
1818 line_header_hash_voidp (const void *item)
1819 {
1820 const struct line_header *ofs = (const struct line_header *) item;
1821
1822 return line_header_hash (ofs);
1823 }
1824
1825 /* Equality function for line_header_hash. */
1826
1827 static int
1828 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1829 {
1830 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1831 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1832
1833 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1834 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1835 }
1836
1837 \f
1838
1839 /* See declaration. */
1840
1841 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1842 bool can_copy_)
1843 : obfd (obfd),
1844 can_copy (can_copy_)
1845 {
1846 if (names == NULL)
1847 names = &dwarf2_elf_names;
1848
1849 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1850 locate_sections (obfd, sec, *names);
1851 }
1852
1853 dwarf2_per_bfd::~dwarf2_per_bfd ()
1854 {
1855 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1856 per_cu->imported_symtabs_free ();
1857
1858 for (signatured_type *sig_type : all_type_units)
1859 sig_type->per_cu.imported_symtabs_free ();
1860
1861 /* Everything else should be on this->obstack. */
1862 }
1863
1864 /* See read.h. */
1865
1866 void
1867 dwarf2_per_objfile::remove_all_cus ()
1868 {
1869 gdb_assert (!this->per_bfd->queue.has_value ());
1870
1871 for (auto pair : m_dwarf2_cus)
1872 delete pair.second;
1873
1874 m_dwarf2_cus.clear ();
1875 }
1876
1877 /* A helper class that calls free_cached_comp_units on
1878 destruction. */
1879
1880 class free_cached_comp_units
1881 {
1882 public:
1883
1884 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1885 : m_per_objfile (per_objfile)
1886 {
1887 }
1888
1889 ~free_cached_comp_units ()
1890 {
1891 m_per_objfile->remove_all_cus ();
1892 }
1893
1894 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1895
1896 private:
1897
1898 dwarf2_per_objfile *m_per_objfile;
1899 };
1900
1901 /* See read.h. */
1902
1903 bool
1904 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1905 {
1906 gdb_assert (per_cu->index < this->m_symtabs.size ());
1907
1908 return this->m_symtabs[per_cu->index] != nullptr;
1909 }
1910
1911 /* See read.h. */
1912
1913 compunit_symtab *
1914 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1915 {
1916 gdb_assert (per_cu->index < this->m_symtabs.size ());
1917
1918 return this->m_symtabs[per_cu->index];
1919 }
1920
1921 /* See read.h. */
1922
1923 void
1924 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1925 compunit_symtab *symtab)
1926 {
1927 gdb_assert (per_cu->index < this->m_symtabs.size ());
1928 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1929
1930 this->m_symtabs[per_cu->index] = symtab;
1931 }
1932
1933 /* Try to locate the sections we need for DWARF 2 debugging
1934 information and return true if we have enough to do something.
1935 NAMES points to the dwarf2 section names, or is NULL if the standard
1936 ELF names are used. CAN_COPY is true for formats where symbol
1937 interposition is possible and so symbol values must follow copy
1938 relocation rules. */
1939
1940 int
1941 dwarf2_has_info (struct objfile *objfile,
1942 const struct dwarf2_debug_sections *names,
1943 bool can_copy)
1944 {
1945 if (objfile->flags & OBJF_READNEVER)
1946 return 0;
1947
1948 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1949
1950 if (per_objfile == NULL)
1951 {
1952 dwarf2_per_bfd *per_bfd;
1953
1954 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1955 BFD doesn't require relocations.
1956
1957 We don't share with objfiles for which -readnow was requested,
1958 because it would complicate things when loading the same BFD with
1959 -readnow and then without -readnow. */
1960 if (!gdb_bfd_requires_relocations (objfile->obfd)
1961 && (objfile->flags & OBJF_READNOW) == 0)
1962 {
1963 /* See if one has been created for this BFD yet. */
1964 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1965
1966 if (per_bfd == nullptr)
1967 {
1968 /* No, create it now. */
1969 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1970 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1971 }
1972 }
1973 else
1974 {
1975 /* No sharing possible, create one specifically for this objfile. */
1976 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1977 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1978 }
1979
1980 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1981 }
1982
1983 return (!per_objfile->per_bfd->info.is_virtual
1984 && per_objfile->per_bfd->info.s.section != NULL
1985 && !per_objfile->per_bfd->abbrev.is_virtual
1986 && per_objfile->per_bfd->abbrev.s.section != NULL);
1987 }
1988
1989 /* See declaration. */
1990
1991 void
1992 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1993 const dwarf2_debug_sections &names)
1994 {
1995 flagword aflag = bfd_section_flags (sectp);
1996
1997 if ((aflag & SEC_HAS_CONTENTS) == 0)
1998 {
1999 }
2000 else if (elf_section_data (sectp)->this_hdr.sh_size
2001 > bfd_get_file_size (abfd))
2002 {
2003 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2004 warning (_("Discarding section %s which has a section size (%s"
2005 ") larger than the file size [in module %s]"),
2006 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2007 bfd_get_filename (abfd));
2008 }
2009 else if (names.info.matches (sectp->name))
2010 {
2011 this->info.s.section = sectp;
2012 this->info.size = bfd_section_size (sectp);
2013 }
2014 else if (names.abbrev.matches (sectp->name))
2015 {
2016 this->abbrev.s.section = sectp;
2017 this->abbrev.size = bfd_section_size (sectp);
2018 }
2019 else if (names.line.matches (sectp->name))
2020 {
2021 this->line.s.section = sectp;
2022 this->line.size = bfd_section_size (sectp);
2023 }
2024 else if (names.loc.matches (sectp->name))
2025 {
2026 this->loc.s.section = sectp;
2027 this->loc.size = bfd_section_size (sectp);
2028 }
2029 else if (names.loclists.matches (sectp->name))
2030 {
2031 this->loclists.s.section = sectp;
2032 this->loclists.size = bfd_section_size (sectp);
2033 }
2034 else if (names.macinfo.matches (sectp->name))
2035 {
2036 this->macinfo.s.section = sectp;
2037 this->macinfo.size = bfd_section_size (sectp);
2038 }
2039 else if (names.macro.matches (sectp->name))
2040 {
2041 this->macro.s.section = sectp;
2042 this->macro.size = bfd_section_size (sectp);
2043 }
2044 else if (names.str.matches (sectp->name))
2045 {
2046 this->str.s.section = sectp;
2047 this->str.size = bfd_section_size (sectp);
2048 }
2049 else if (names.str_offsets.matches (sectp->name))
2050 {
2051 this->str_offsets.s.section = sectp;
2052 this->str_offsets.size = bfd_section_size (sectp);
2053 }
2054 else if (names.line_str.matches (sectp->name))
2055 {
2056 this->line_str.s.section = sectp;
2057 this->line_str.size = bfd_section_size (sectp);
2058 }
2059 else if (names.addr.matches (sectp->name))
2060 {
2061 this->addr.s.section = sectp;
2062 this->addr.size = bfd_section_size (sectp);
2063 }
2064 else if (names.frame.matches (sectp->name))
2065 {
2066 this->frame.s.section = sectp;
2067 this->frame.size = bfd_section_size (sectp);
2068 }
2069 else if (names.eh_frame.matches (sectp->name))
2070 {
2071 this->eh_frame.s.section = sectp;
2072 this->eh_frame.size = bfd_section_size (sectp);
2073 }
2074 else if (names.ranges.matches (sectp->name))
2075 {
2076 this->ranges.s.section = sectp;
2077 this->ranges.size = bfd_section_size (sectp);
2078 }
2079 else if (names.rnglists.matches (sectp->name))
2080 {
2081 this->rnglists.s.section = sectp;
2082 this->rnglists.size = bfd_section_size (sectp);
2083 }
2084 else if (names.types.matches (sectp->name))
2085 {
2086 struct dwarf2_section_info type_section;
2087
2088 memset (&type_section, 0, sizeof (type_section));
2089 type_section.s.section = sectp;
2090 type_section.size = bfd_section_size (sectp);
2091
2092 this->types.push_back (type_section);
2093 }
2094 else if (names.gdb_index.matches (sectp->name))
2095 {
2096 this->gdb_index.s.section = sectp;
2097 this->gdb_index.size = bfd_section_size (sectp);
2098 }
2099 else if (names.debug_names.matches (sectp->name))
2100 {
2101 this->debug_names.s.section = sectp;
2102 this->debug_names.size = bfd_section_size (sectp);
2103 }
2104 else if (names.debug_aranges.matches (sectp->name))
2105 {
2106 this->debug_aranges.s.section = sectp;
2107 this->debug_aranges.size = bfd_section_size (sectp);
2108 }
2109
2110 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2111 && bfd_section_vma (sectp) == 0)
2112 this->has_section_at_zero = true;
2113 }
2114
2115 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2116 SECTION_NAME. */
2117
2118 void
2119 dwarf2_get_section_info (struct objfile *objfile,
2120 enum dwarf2_section_enum sect,
2121 asection **sectp, const gdb_byte **bufp,
2122 bfd_size_type *sizep)
2123 {
2124 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2125 struct dwarf2_section_info *info;
2126
2127 /* We may see an objfile without any DWARF, in which case we just
2128 return nothing. */
2129 if (per_objfile == NULL)
2130 {
2131 *sectp = NULL;
2132 *bufp = NULL;
2133 *sizep = 0;
2134 return;
2135 }
2136 switch (sect)
2137 {
2138 case DWARF2_DEBUG_FRAME:
2139 info = &per_objfile->per_bfd->frame;
2140 break;
2141 case DWARF2_EH_FRAME:
2142 info = &per_objfile->per_bfd->eh_frame;
2143 break;
2144 default:
2145 gdb_assert_not_reached ("unexpected section");
2146 }
2147
2148 info->read (objfile);
2149
2150 *sectp = info->get_bfd_section ();
2151 *bufp = info->buffer;
2152 *sizep = info->size;
2153 }
2154
2155 \f
2156 /* DWARF quick_symbol_functions support. */
2157
2158 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2159 unique line tables, so we maintain a separate table of all .debug_line
2160 derived entries to support the sharing.
2161 All the quick functions need is the list of file names. We discard the
2162 line_header when we're done and don't need to record it here. */
2163 struct quick_file_names
2164 {
2165 /* The data used to construct the hash key. */
2166 struct stmt_list_hash hash;
2167
2168 /* The number of entries in file_names, real_names. */
2169 unsigned int num_file_names;
2170
2171 /* The file names from the line table, after being run through
2172 file_full_name. */
2173 const char **file_names;
2174
2175 /* The file names from the line table after being run through
2176 gdb_realpath. These are computed lazily. */
2177 const char **real_names;
2178 };
2179
2180 /* When using the index (and thus not using psymtabs), each CU has an
2181 object of this type. This is used to hold information needed by
2182 the various "quick" methods. */
2183 struct dwarf2_per_cu_quick_data
2184 {
2185 /* The file table. This can be NULL if there was no file table
2186 or it's currently not read in.
2187 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2188 struct quick_file_names *file_names;
2189
2190 /* A temporary mark bit used when iterating over all CUs in
2191 expand_symtabs_matching. */
2192 unsigned int mark : 1;
2193
2194 /* True if we've tried to read the file table and found there isn't one.
2195 There will be no point in trying to read it again next time. */
2196 unsigned int no_file_data : 1;
2197 };
2198
2199 /* A subclass of psymbol_functions that arranges to read the DWARF
2200 partial symbols when needed. */
2201 struct lazy_dwarf_reader : public psymbol_functions
2202 {
2203 using psymbol_functions::psymbol_functions;
2204
2205 bool can_lazily_read_symbols () override
2206 {
2207 return true;
2208 }
2209
2210 void read_partial_symbols (struct objfile *objfile) override
2211 {
2212 if (dwarf2_has_info (objfile, nullptr))
2213 dwarf2_build_psymtabs (objfile, this);
2214 }
2215 };
2216
2217 static quick_symbol_functions_up
2218 make_lazy_dwarf_reader ()
2219 {
2220 return quick_symbol_functions_up (new lazy_dwarf_reader);
2221 }
2222
2223 struct dwarf2_base_index_functions : public quick_symbol_functions
2224 {
2225 bool has_symbols (struct objfile *objfile) override;
2226
2227 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2228
2229 void forget_cached_source_info (struct objfile *objfile) override;
2230
2231 bool map_symtabs_matching_filename
2232 (struct objfile *objfile, const char *name, const char *real_path,
2233 gdb::function_view<bool (symtab *)> callback) override;
2234
2235 enum language lookup_global_symbol_language (struct objfile *objfile,
2236 const char *name,
2237 domain_enum domain,
2238 bool *symbol_found_p) override
2239 {
2240 *symbol_found_p = false;
2241 return language_unknown;
2242 }
2243
2244 void print_stats (struct objfile *objfile, bool print_bcache) override;
2245
2246 void expand_all_symtabs (struct objfile *objfile) override;
2247
2248 void expand_symtabs_with_fullname (struct objfile *objfile,
2249 const char *fullname) override;
2250
2251 struct compunit_symtab *find_pc_sect_compunit_symtab
2252 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2253 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2254
2255 struct compunit_symtab *find_compunit_symtab_by_address
2256 (struct objfile *objfile, CORE_ADDR address) override
2257 {
2258 return nullptr;
2259 }
2260
2261 void map_symbol_filenames (struct objfile *objfile,
2262 gdb::function_view<symbol_filename_ftype> fun,
2263 bool need_fullname) override;
2264 };
2265
2266 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2267 {
2268 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2269 block_enum block_index,
2270 const char *name,
2271 domain_enum domain) override;
2272
2273 void dump (struct objfile *objfile) override;
2274
2275 void expand_symtabs_for_function (struct objfile *objfile,
2276 const char *func_name) override;
2277
2278 void map_matching_symbols
2279 (struct objfile *,
2280 const lookup_name_info &lookup_name,
2281 domain_enum domain,
2282 int global,
2283 gdb::function_view<symbol_found_callback_ftype> callback,
2284 symbol_compare_ftype *ordered_compare) override;
2285
2286 bool expand_symtabs_matching
2287 (struct objfile *objfile,
2288 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2289 const lookup_name_info *lookup_name,
2290 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2291 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2292 enum search_domain kind) override;
2293 };
2294
2295 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2296 {
2297 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2298 block_enum block_index,
2299 const char *name,
2300 domain_enum domain) override;
2301
2302 void dump (struct objfile *objfile) override;
2303
2304 void expand_symtabs_for_function (struct objfile *objfile,
2305 const char *func_name) override;
2306
2307 void map_matching_symbols
2308 (struct objfile *,
2309 const lookup_name_info &lookup_name,
2310 domain_enum domain,
2311 int global,
2312 gdb::function_view<symbol_found_callback_ftype> callback,
2313 symbol_compare_ftype *ordered_compare) override;
2314
2315 bool expand_symtabs_matching
2316 (struct objfile *objfile,
2317 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2318 const lookup_name_info *lookup_name,
2319 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2320 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2321 enum search_domain kind) override;
2322 };
2323
2324 static quick_symbol_functions_up
2325 make_dwarf_gdb_index ()
2326 {
2327 return quick_symbol_functions_up (new dwarf2_gdb_index);
2328 }
2329
2330 static quick_symbol_functions_up
2331 make_dwarf_debug_names ()
2332 {
2333 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2334 }
2335
2336 /* Utility hash function for a stmt_list_hash. */
2337
2338 static hashval_t
2339 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2340 {
2341 hashval_t v = 0;
2342
2343 if (stmt_list_hash->dwo_unit != NULL)
2344 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2345 v += to_underlying (stmt_list_hash->line_sect_off);
2346 return v;
2347 }
2348
2349 /* Utility equality function for a stmt_list_hash. */
2350
2351 static int
2352 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2353 const struct stmt_list_hash *rhs)
2354 {
2355 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2356 return 0;
2357 if (lhs->dwo_unit != NULL
2358 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2359 return 0;
2360
2361 return lhs->line_sect_off == rhs->line_sect_off;
2362 }
2363
2364 /* Hash function for a quick_file_names. */
2365
2366 static hashval_t
2367 hash_file_name_entry (const void *e)
2368 {
2369 const struct quick_file_names *file_data
2370 = (const struct quick_file_names *) e;
2371
2372 return hash_stmt_list_entry (&file_data->hash);
2373 }
2374
2375 /* Equality function for a quick_file_names. */
2376
2377 static int
2378 eq_file_name_entry (const void *a, const void *b)
2379 {
2380 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2381 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2382
2383 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2384 }
2385
2386 /* Delete function for a quick_file_names. */
2387
2388 static void
2389 delete_file_name_entry (void *e)
2390 {
2391 struct quick_file_names *file_data = (struct quick_file_names *) e;
2392 int i;
2393
2394 for (i = 0; i < file_data->num_file_names; ++i)
2395 {
2396 xfree ((void*) file_data->file_names[i]);
2397 if (file_data->real_names)
2398 xfree ((void*) file_data->real_names[i]);
2399 }
2400
2401 /* The space for the struct itself lives on the obstack, so we don't
2402 free it here. */
2403 }
2404
2405 /* Create a quick_file_names hash table. */
2406
2407 static htab_up
2408 create_quick_file_names_table (unsigned int nr_initial_entries)
2409 {
2410 return htab_up (htab_create_alloc (nr_initial_entries,
2411 hash_file_name_entry, eq_file_name_entry,
2412 delete_file_name_entry, xcalloc, xfree));
2413 }
2414
2415 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2416 function is unrelated to symtabs, symtab would have to be created afterwards.
2417 You should call age_cached_comp_units after processing the CU. */
2418
2419 static dwarf2_cu *
2420 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2421 bool skip_partial)
2422 {
2423 if (per_cu->is_debug_types)
2424 load_full_type_unit (per_cu, per_objfile);
2425 else
2426 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2427 skip_partial, language_minimal);
2428
2429 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2430 if (cu == nullptr)
2431 return nullptr; /* Dummy CU. */
2432
2433 dwarf2_find_base_address (cu->dies, cu);
2434
2435 return cu;
2436 }
2437
2438 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2439
2440 static void
2441 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2442 dwarf2_per_objfile *per_objfile, bool skip_partial)
2443 {
2444 /* Skip type_unit_groups, reading the type units they contain
2445 is handled elsewhere. */
2446 if (per_cu->type_unit_group_p ())
2447 return;
2448
2449 {
2450 /* The destructor of dwarf2_queue_guard frees any entries left on
2451 the queue. After this point we're guaranteed to leave this function
2452 with the dwarf queue empty. */
2453 dwarf2_queue_guard q_guard (per_objfile);
2454
2455 if (!per_objfile->symtab_set_p (per_cu))
2456 {
2457 queue_comp_unit (per_cu, per_objfile, language_minimal);
2458 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2459
2460 /* If we just loaded a CU from a DWO, and we're working with an index
2461 that may badly handle TUs, load all the TUs in that DWO as well.
2462 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2463 if (!per_cu->is_debug_types
2464 && cu != NULL
2465 && cu->dwo_unit != NULL
2466 && per_objfile->per_bfd->index_table != NULL
2467 && per_objfile->per_bfd->index_table->version <= 7
2468 /* DWP files aren't supported yet. */
2469 && get_dwp_file (per_objfile) == NULL)
2470 queue_and_load_all_dwo_tus (cu);
2471 }
2472
2473 process_queue (per_objfile);
2474 }
2475
2476 /* Age the cache, releasing compilation units that have not
2477 been used recently. */
2478 per_objfile->age_comp_units ();
2479 }
2480
2481 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2482 the per-objfile for which this symtab is instantiated.
2483
2484 Returns the resulting symbol table. */
2485
2486 static struct compunit_symtab *
2487 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2488 dwarf2_per_objfile *per_objfile,
2489 bool skip_partial)
2490 {
2491 gdb_assert (per_objfile->per_bfd->using_index);
2492
2493 if (!per_objfile->symtab_set_p (per_cu))
2494 {
2495 free_cached_comp_units freer (per_objfile);
2496 scoped_restore decrementer = increment_reading_symtab ();
2497 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2498 process_cu_includes (per_objfile);
2499 }
2500
2501 return per_objfile->get_symtab (per_cu);
2502 }
2503
2504 /* See declaration. */
2505
2506 dwarf2_per_cu_data *
2507 dwarf2_per_bfd::get_cutu (int index)
2508 {
2509 if (index >= this->all_comp_units.size ())
2510 {
2511 index -= this->all_comp_units.size ();
2512 gdb_assert (index < this->all_type_units.size ());
2513 return &this->all_type_units[index]->per_cu;
2514 }
2515
2516 return this->all_comp_units[index];
2517 }
2518
2519 /* See declaration. */
2520
2521 dwarf2_per_cu_data *
2522 dwarf2_per_bfd::get_cu (int index)
2523 {
2524 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2525
2526 return this->all_comp_units[index];
2527 }
2528
2529 /* See declaration. */
2530
2531 signatured_type *
2532 dwarf2_per_bfd::get_tu (int index)
2533 {
2534 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2535
2536 return this->all_type_units[index];
2537 }
2538
2539 /* See read.h. */
2540
2541 dwarf2_per_cu_data *
2542 dwarf2_per_bfd::allocate_per_cu ()
2543 {
2544 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2545 result->per_bfd = this;
2546 result->index = m_num_psymtabs++;
2547 return result;
2548 }
2549
2550 /* See read.h. */
2551
2552 signatured_type *
2553 dwarf2_per_bfd::allocate_signatured_type ()
2554 {
2555 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2556 result->per_cu.per_bfd = this;
2557 result->per_cu.index = m_num_psymtabs++;
2558 return result;
2559 }
2560
2561 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2562 obstack, and constructed with the specified field values. */
2563
2564 static dwarf2_per_cu_data *
2565 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2566 struct dwarf2_section_info *section,
2567 int is_dwz,
2568 sect_offset sect_off, ULONGEST length)
2569 {
2570 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2571 the_cu->sect_off = sect_off;
2572 the_cu->length = length;
2573 the_cu->section = section;
2574 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2575 struct dwarf2_per_cu_quick_data);
2576 the_cu->is_dwz = is_dwz;
2577 return the_cu;
2578 }
2579
2580 /* A helper for create_cus_from_index that handles a given list of
2581 CUs. */
2582
2583 static void
2584 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2585 const gdb_byte *cu_list, offset_type n_elements,
2586 struct dwarf2_section_info *section,
2587 int is_dwz)
2588 {
2589 for (offset_type i = 0; i < n_elements; i += 2)
2590 {
2591 gdb_static_assert (sizeof (ULONGEST) >= 8);
2592
2593 sect_offset sect_off
2594 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2595 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2596 cu_list += 2 * 8;
2597
2598 dwarf2_per_cu_data *per_cu
2599 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2600 length);
2601 per_bfd->all_comp_units.push_back (per_cu);
2602 }
2603 }
2604
2605 /* Read the CU list from the mapped index, and use it to create all
2606 the CU objects for PER_BFD. */
2607
2608 static void
2609 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2610 const gdb_byte *cu_list, offset_type cu_list_elements,
2611 const gdb_byte *dwz_list, offset_type dwz_elements)
2612 {
2613 gdb_assert (per_bfd->all_comp_units.empty ());
2614 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2615
2616 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2617 &per_bfd->info, 0);
2618
2619 if (dwz_elements == 0)
2620 return;
2621
2622 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2623 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2624 &dwz->info, 1);
2625 }
2626
2627 /* Create the signatured type hash table from the index. */
2628
2629 static void
2630 create_signatured_type_table_from_index
2631 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2632 const gdb_byte *bytes, offset_type elements)
2633 {
2634 gdb_assert (per_bfd->all_type_units.empty ());
2635 per_bfd->all_type_units.reserve (elements / 3);
2636
2637 htab_up sig_types_hash = allocate_signatured_type_table ();
2638
2639 for (offset_type i = 0; i < elements; i += 3)
2640 {
2641 struct signatured_type *sig_type;
2642 ULONGEST signature;
2643 void **slot;
2644 cu_offset type_offset_in_tu;
2645
2646 gdb_static_assert (sizeof (ULONGEST) >= 8);
2647 sect_offset sect_off
2648 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2649 type_offset_in_tu
2650 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2651 BFD_ENDIAN_LITTLE);
2652 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2653 bytes += 3 * 8;
2654
2655 sig_type = per_bfd->allocate_signatured_type ();
2656 sig_type->signature = signature;
2657 sig_type->type_offset_in_tu = type_offset_in_tu;
2658 sig_type->per_cu.is_debug_types = 1;
2659 sig_type->per_cu.section = section;
2660 sig_type->per_cu.sect_off = sect_off;
2661 sig_type->per_cu.v.quick
2662 = OBSTACK_ZALLOC (&per_bfd->obstack,
2663 struct dwarf2_per_cu_quick_data);
2664
2665 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2666 *slot = sig_type;
2667
2668 per_bfd->all_type_units.push_back (sig_type);
2669 }
2670
2671 per_bfd->signatured_types = std::move (sig_types_hash);
2672 }
2673
2674 /* Create the signatured type hash table from .debug_names. */
2675
2676 static void
2677 create_signatured_type_table_from_debug_names
2678 (dwarf2_per_objfile *per_objfile,
2679 const mapped_debug_names &map,
2680 struct dwarf2_section_info *section,
2681 struct dwarf2_section_info *abbrev_section)
2682 {
2683 struct objfile *objfile = per_objfile->objfile;
2684
2685 section->read (objfile);
2686 abbrev_section->read (objfile);
2687
2688 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2689 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2690
2691 htab_up sig_types_hash = allocate_signatured_type_table ();
2692
2693 for (uint32_t i = 0; i < map.tu_count; ++i)
2694 {
2695 struct signatured_type *sig_type;
2696 void **slot;
2697
2698 sect_offset sect_off
2699 = (sect_offset) (extract_unsigned_integer
2700 (map.tu_table_reordered + i * map.offset_size,
2701 map.offset_size,
2702 map.dwarf5_byte_order));
2703
2704 comp_unit_head cu_header;
2705 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2706 abbrev_section,
2707 section->buffer + to_underlying (sect_off),
2708 rcuh_kind::TYPE);
2709
2710 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2711 sig_type->signature = cu_header.signature;
2712 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2713 sig_type->per_cu.is_debug_types = 1;
2714 sig_type->per_cu.section = section;
2715 sig_type->per_cu.sect_off = sect_off;
2716 sig_type->per_cu.v.quick
2717 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2718 struct dwarf2_per_cu_quick_data);
2719
2720 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2721 *slot = sig_type;
2722
2723 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2724 }
2725
2726 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2727 }
2728
2729 /* Read the address map data from the mapped index, and use it to
2730 populate the psymtabs_addrmap. */
2731
2732 static void
2733 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2734 struct mapped_index *index)
2735 {
2736 struct objfile *objfile = per_objfile->objfile;
2737 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2738 struct gdbarch *gdbarch = objfile->arch ();
2739 const gdb_byte *iter, *end;
2740 struct addrmap *mutable_map;
2741 CORE_ADDR baseaddr;
2742
2743 auto_obstack temp_obstack;
2744
2745 mutable_map = addrmap_create_mutable (&temp_obstack);
2746
2747 iter = index->address_table.data ();
2748 end = iter + index->address_table.size ();
2749
2750 baseaddr = objfile->text_section_offset ();
2751
2752 while (iter < end)
2753 {
2754 ULONGEST hi, lo, cu_index;
2755 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2756 iter += 8;
2757 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2758 iter += 8;
2759 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2760 iter += 4;
2761
2762 if (lo > hi)
2763 {
2764 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2765 hex_string (lo), hex_string (hi));
2766 continue;
2767 }
2768
2769 if (cu_index >= per_bfd->all_comp_units.size ())
2770 {
2771 complaint (_(".gdb_index address table has invalid CU number %u"),
2772 (unsigned) cu_index);
2773 continue;
2774 }
2775
2776 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2777 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2778 addrmap_set_empty (mutable_map, lo, hi - 1,
2779 per_bfd->get_cu (cu_index));
2780 }
2781
2782 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2783 &per_bfd->obstack);
2784 }
2785
2786 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2787 populate the psymtabs_addrmap. */
2788
2789 static void
2790 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2791 struct dwarf2_section_info *section)
2792 {
2793 struct objfile *objfile = per_objfile->objfile;
2794 bfd *abfd = objfile->obfd;
2795 struct gdbarch *gdbarch = objfile->arch ();
2796 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2797 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2798
2799 auto_obstack temp_obstack;
2800 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2801
2802 std::unordered_map<sect_offset,
2803 dwarf2_per_cu_data *,
2804 gdb::hash_enum<sect_offset>>
2805 debug_info_offset_to_per_cu;
2806 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
2807 {
2808 const auto insertpair
2809 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2810 if (!insertpair.second)
2811 {
2812 warning (_("Section .debug_aranges in %s has duplicate "
2813 "debug_info_offset %s, ignoring .debug_aranges."),
2814 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2815 return;
2816 }
2817 }
2818
2819 section->read (objfile);
2820
2821 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2822
2823 const gdb_byte *addr = section->buffer;
2824
2825 while (addr < section->buffer + section->size)
2826 {
2827 const gdb_byte *const entry_addr = addr;
2828 unsigned int bytes_read;
2829
2830 const LONGEST entry_length = read_initial_length (abfd, addr,
2831 &bytes_read);
2832 addr += bytes_read;
2833
2834 const gdb_byte *const entry_end = addr + entry_length;
2835 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2836 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2837 if (addr + entry_length > section->buffer + section->size)
2838 {
2839 warning (_("Section .debug_aranges in %s entry at offset %s "
2840 "length %s exceeds section length %s, "
2841 "ignoring .debug_aranges."),
2842 objfile_name (objfile),
2843 plongest (entry_addr - section->buffer),
2844 plongest (bytes_read + entry_length),
2845 pulongest (section->size));
2846 return;
2847 }
2848
2849 /* The version number. */
2850 const uint16_t version = read_2_bytes (abfd, addr);
2851 addr += 2;
2852 if (version != 2)
2853 {
2854 warning (_("Section .debug_aranges in %s entry at offset %s "
2855 "has unsupported version %d, ignoring .debug_aranges."),
2856 objfile_name (objfile),
2857 plongest (entry_addr - section->buffer), version);
2858 return;
2859 }
2860
2861 const uint64_t debug_info_offset
2862 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2863 addr += offset_size;
2864 const auto per_cu_it
2865 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2866 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2867 {
2868 warning (_("Section .debug_aranges in %s entry at offset %s "
2869 "debug_info_offset %s does not exists, "
2870 "ignoring .debug_aranges."),
2871 objfile_name (objfile),
2872 plongest (entry_addr - section->buffer),
2873 pulongest (debug_info_offset));
2874 return;
2875 }
2876 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2877
2878 const uint8_t address_size = *addr++;
2879 if (address_size < 1 || address_size > 8)
2880 {
2881 warning (_("Section .debug_aranges in %s entry at offset %s "
2882 "address_size %u is invalid, ignoring .debug_aranges."),
2883 objfile_name (objfile),
2884 plongest (entry_addr - section->buffer), address_size);
2885 return;
2886 }
2887
2888 const uint8_t segment_selector_size = *addr++;
2889 if (segment_selector_size != 0)
2890 {
2891 warning (_("Section .debug_aranges in %s entry at offset %s "
2892 "segment_selector_size %u is not supported, "
2893 "ignoring .debug_aranges."),
2894 objfile_name (objfile),
2895 plongest (entry_addr - section->buffer),
2896 segment_selector_size);
2897 return;
2898 }
2899
2900 /* Must pad to an alignment boundary that is twice the address
2901 size. It is undocumented by the DWARF standard but GCC does
2902 use it. */
2903 for (size_t padding = ((-(addr - section->buffer))
2904 & (2 * address_size - 1));
2905 padding > 0; padding--)
2906 if (*addr++ != 0)
2907 {
2908 warning (_("Section .debug_aranges in %s entry at offset %s "
2909 "padding is not zero, ignoring .debug_aranges."),
2910 objfile_name (objfile),
2911 plongest (entry_addr - section->buffer));
2912 return;
2913 }
2914
2915 for (;;)
2916 {
2917 if (addr + 2 * address_size > entry_end)
2918 {
2919 warning (_("Section .debug_aranges in %s entry at offset %s "
2920 "address list is not properly terminated, "
2921 "ignoring .debug_aranges."),
2922 objfile_name (objfile),
2923 plongest (entry_addr - section->buffer));
2924 return;
2925 }
2926 ULONGEST start = extract_unsigned_integer (addr, address_size,
2927 dwarf5_byte_order);
2928 addr += address_size;
2929 ULONGEST length = extract_unsigned_integer (addr, address_size,
2930 dwarf5_byte_order);
2931 addr += address_size;
2932 if (start == 0 && length == 0)
2933 break;
2934 if (start == 0 && !per_bfd->has_section_at_zero)
2935 {
2936 /* Symbol was eliminated due to a COMDAT group. */
2937 continue;
2938 }
2939 ULONGEST end = start + length;
2940 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2941 - baseaddr);
2942 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2943 - baseaddr);
2944 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2945 }
2946 }
2947
2948 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2949 &per_bfd->obstack);
2950 }
2951
2952 /* Find a slot in the mapped index INDEX for the object named NAME.
2953 If NAME is found, set *VEC_OUT to point to the CU vector in the
2954 constant pool and return true. If NAME cannot be found, return
2955 false. */
2956
2957 static bool
2958 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2959 offset_type **vec_out)
2960 {
2961 offset_type hash;
2962 offset_type slot, step;
2963 int (*cmp) (const char *, const char *);
2964
2965 gdb::unique_xmalloc_ptr<char> without_params;
2966 if (current_language->la_language == language_cplus
2967 || current_language->la_language == language_fortran
2968 || current_language->la_language == language_d)
2969 {
2970 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2971 not contain any. */
2972
2973 if (strchr (name, '(') != NULL)
2974 {
2975 without_params = cp_remove_params (name);
2976
2977 if (without_params != NULL)
2978 name = without_params.get ();
2979 }
2980 }
2981
2982 /* Index version 4 did not support case insensitive searches. But the
2983 indices for case insensitive languages are built in lowercase, therefore
2984 simulate our NAME being searched is also lowercased. */
2985 hash = mapped_index_string_hash ((index->version == 4
2986 && case_sensitivity == case_sensitive_off
2987 ? 5 : index->version),
2988 name);
2989
2990 slot = hash & (index->symbol_table.size () - 1);
2991 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2992 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2993
2994 for (;;)
2995 {
2996 const char *str;
2997
2998 const auto &bucket = index->symbol_table[slot];
2999 if (bucket.name == 0 && bucket.vec == 0)
3000 return false;
3001
3002 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3003 if (!cmp (name, str))
3004 {
3005 *vec_out = (offset_type *) (index->constant_pool
3006 + MAYBE_SWAP (bucket.vec));
3007 return true;
3008 }
3009
3010 slot = (slot + step) & (index->symbol_table.size () - 1);
3011 }
3012 }
3013
3014 /* A helper function that reads the .gdb_index from BUFFER and fills
3015 in MAP. FILENAME is the name of the file containing the data;
3016 it is used for error reporting. DEPRECATED_OK is true if it is
3017 ok to use deprecated sections.
3018
3019 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3020 out parameters that are filled in with information about the CU and
3021 TU lists in the section.
3022
3023 Returns true if all went well, false otherwise. */
3024
3025 static bool
3026 read_gdb_index_from_buffer (const char *filename,
3027 bool deprecated_ok,
3028 gdb::array_view<const gdb_byte> buffer,
3029 struct mapped_index *map,
3030 const gdb_byte **cu_list,
3031 offset_type *cu_list_elements,
3032 const gdb_byte **types_list,
3033 offset_type *types_list_elements)
3034 {
3035 const gdb_byte *addr = &buffer[0];
3036
3037 /* Version check. */
3038 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3039 /* Versions earlier than 3 emitted every copy of a psymbol. This
3040 causes the index to behave very poorly for certain requests. Version 3
3041 contained incomplete addrmap. So, it seems better to just ignore such
3042 indices. */
3043 if (version < 4)
3044 {
3045 static int warning_printed = 0;
3046 if (!warning_printed)
3047 {
3048 warning (_("Skipping obsolete .gdb_index section in %s."),
3049 filename);
3050 warning_printed = 1;
3051 }
3052 return 0;
3053 }
3054 /* Index version 4 uses a different hash function than index version
3055 5 and later.
3056
3057 Versions earlier than 6 did not emit psymbols for inlined
3058 functions. Using these files will cause GDB not to be able to
3059 set breakpoints on inlined functions by name, so we ignore these
3060 indices unless the user has done
3061 "set use-deprecated-index-sections on". */
3062 if (version < 6 && !deprecated_ok)
3063 {
3064 static int warning_printed = 0;
3065 if (!warning_printed)
3066 {
3067 warning (_("\
3068 Skipping deprecated .gdb_index section in %s.\n\
3069 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3070 to use the section anyway."),
3071 filename);
3072 warning_printed = 1;
3073 }
3074 return 0;
3075 }
3076 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3077 of the TU (for symbols coming from TUs),
3078 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3079 Plus gold-generated indices can have duplicate entries for global symbols,
3080 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3081 These are just performance bugs, and we can't distinguish gdb-generated
3082 indices from gold-generated ones, so issue no warning here. */
3083
3084 /* Indexes with higher version than the one supported by GDB may be no
3085 longer backward compatible. */
3086 if (version > 8)
3087 return 0;
3088
3089 map->version = version;
3090
3091 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3092
3093 int i = 0;
3094 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3095 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3096 / 8);
3097 ++i;
3098
3099 *types_list = addr + MAYBE_SWAP (metadata[i]);
3100 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3101 - MAYBE_SWAP (metadata[i]))
3102 / 8);
3103 ++i;
3104
3105 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3106 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3107 map->address_table
3108 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3109 ++i;
3110
3111 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3112 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3113 map->symbol_table
3114 = gdb::array_view<mapped_index::symbol_table_slot>
3115 ((mapped_index::symbol_table_slot *) symbol_table,
3116 (mapped_index::symbol_table_slot *) symbol_table_end);
3117
3118 ++i;
3119 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3120
3121 return 1;
3122 }
3123
3124 /* Callback types for dwarf2_read_gdb_index. */
3125
3126 typedef gdb::function_view
3127 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3128 get_gdb_index_contents_ftype;
3129 typedef gdb::function_view
3130 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3131 get_gdb_index_contents_dwz_ftype;
3132
3133 /* Read .gdb_index. If everything went ok, initialize the "quick"
3134 elements of all the CUs and return 1. Otherwise, return 0. */
3135
3136 static int
3137 dwarf2_read_gdb_index
3138 (dwarf2_per_objfile *per_objfile,
3139 get_gdb_index_contents_ftype get_gdb_index_contents,
3140 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3141 {
3142 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3143 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3144 struct dwz_file *dwz;
3145 struct objfile *objfile = per_objfile->objfile;
3146 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3147
3148 gdb::array_view<const gdb_byte> main_index_contents
3149 = get_gdb_index_contents (objfile, per_bfd);
3150
3151 if (main_index_contents.empty ())
3152 return 0;
3153
3154 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3155 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3156 use_deprecated_index_sections,
3157 main_index_contents, map.get (), &cu_list,
3158 &cu_list_elements, &types_list,
3159 &types_list_elements))
3160 return 0;
3161
3162 /* Don't use the index if it's empty. */
3163 if (map->symbol_table.empty ())
3164 return 0;
3165
3166 /* If there is a .dwz file, read it so we can get its CU list as
3167 well. */
3168 dwz = dwarf2_get_dwz_file (per_bfd);
3169 if (dwz != NULL)
3170 {
3171 struct mapped_index dwz_map;
3172 const gdb_byte *dwz_types_ignore;
3173 offset_type dwz_types_elements_ignore;
3174
3175 gdb::array_view<const gdb_byte> dwz_index_content
3176 = get_gdb_index_contents_dwz (objfile, dwz);
3177
3178 if (dwz_index_content.empty ())
3179 return 0;
3180
3181 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3182 1, dwz_index_content, &dwz_map,
3183 &dwz_list, &dwz_list_elements,
3184 &dwz_types_ignore,
3185 &dwz_types_elements_ignore))
3186 {
3187 warning (_("could not read '.gdb_index' section from %s; skipping"),
3188 bfd_get_filename (dwz->dwz_bfd.get ()));
3189 return 0;
3190 }
3191 }
3192
3193 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3194 dwz_list_elements);
3195
3196 if (types_list_elements)
3197 {
3198 /* We can only handle a single .debug_types when we have an
3199 index. */
3200 if (per_bfd->types.size () != 1)
3201 return 0;
3202
3203 dwarf2_section_info *section = &per_bfd->types[0];
3204
3205 create_signatured_type_table_from_index (per_bfd, section, types_list,
3206 types_list_elements);
3207 }
3208
3209 create_addrmap_from_index (per_objfile, map.get ());
3210
3211 per_bfd->index_table = std::move (map);
3212 per_bfd->using_index = 1;
3213 per_bfd->quick_file_names_table =
3214 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3215
3216 return 1;
3217 }
3218
3219 /* die_reader_func for dw2_get_file_names. */
3220
3221 static void
3222 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3223 struct die_info *comp_unit_die)
3224 {
3225 struct dwarf2_cu *cu = reader->cu;
3226 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3227 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3228 struct dwarf2_per_cu_data *lh_cu;
3229 struct attribute *attr;
3230 void **slot;
3231 struct quick_file_names *qfn;
3232
3233 gdb_assert (! this_cu->is_debug_types);
3234
3235 /* Our callers never want to match partial units -- instead they
3236 will match the enclosing full CU. */
3237 if (comp_unit_die->tag == DW_TAG_partial_unit)
3238 {
3239 this_cu->v.quick->no_file_data = 1;
3240 return;
3241 }
3242
3243 lh_cu = this_cu;
3244 slot = NULL;
3245
3246 line_header_up lh;
3247 sect_offset line_offset {};
3248
3249 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3250 if (attr != nullptr && attr->form_is_unsigned ())
3251 {
3252 struct quick_file_names find_entry;
3253
3254 line_offset = (sect_offset) attr->as_unsigned ();
3255
3256 /* We may have already read in this line header (TU line header sharing).
3257 If we have we're done. */
3258 find_entry.hash.dwo_unit = cu->dwo_unit;
3259 find_entry.hash.line_sect_off = line_offset;
3260 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3261 &find_entry, INSERT);
3262 if (*slot != NULL)
3263 {
3264 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3265 return;
3266 }
3267
3268 lh = dwarf_decode_line_header (line_offset, cu);
3269 }
3270 if (lh == NULL)
3271 {
3272 lh_cu->v.quick->no_file_data = 1;
3273 return;
3274 }
3275
3276 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3277 qfn->hash.dwo_unit = cu->dwo_unit;
3278 qfn->hash.line_sect_off = line_offset;
3279 gdb_assert (slot != NULL);
3280 *slot = qfn;
3281
3282 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3283
3284 int offset = 0;
3285 if (strcmp (fnd.name, "<unknown>") != 0)
3286 ++offset;
3287
3288 qfn->num_file_names = offset + lh->file_names_size ();
3289 qfn->file_names =
3290 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3291 qfn->num_file_names);
3292 if (offset != 0)
3293 qfn->file_names[0] = xstrdup (fnd.name);
3294 for (int i = 0; i < lh->file_names_size (); ++i)
3295 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3296 fnd.comp_dir).release ();
3297 qfn->real_names = NULL;
3298
3299 lh_cu->v.quick->file_names = qfn;
3300 }
3301
3302 /* A helper for the "quick" functions which attempts to read the line
3303 table for THIS_CU. */
3304
3305 static struct quick_file_names *
3306 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3307 dwarf2_per_objfile *per_objfile)
3308 {
3309 /* This should never be called for TUs. */
3310 gdb_assert (! this_cu->is_debug_types);
3311 /* Nor type unit groups. */
3312 gdb_assert (! this_cu->type_unit_group_p ());
3313
3314 if (this_cu->v.quick->file_names != NULL)
3315 return this_cu->v.quick->file_names;
3316 /* If we know there is no line data, no point in looking again. */
3317 if (this_cu->v.quick->no_file_data)
3318 return NULL;
3319
3320 cutu_reader reader (this_cu, per_objfile);
3321 if (!reader.dummy_p)
3322 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3323
3324 if (this_cu->v.quick->no_file_data)
3325 return NULL;
3326 return this_cu->v.quick->file_names;
3327 }
3328
3329 /* A helper for the "quick" functions which computes and caches the
3330 real path for a given file name from the line table. */
3331
3332 static const char *
3333 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3334 struct quick_file_names *qfn, int index)
3335 {
3336 if (qfn->real_names == NULL)
3337 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3338 qfn->num_file_names, const char *);
3339
3340 if (qfn->real_names[index] == NULL)
3341 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3342
3343 return qfn->real_names[index];
3344 }
3345
3346 struct symtab *
3347 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3348 {
3349 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3350 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3351 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3352
3353 if (cust == NULL)
3354 return NULL;
3355
3356 return compunit_primary_filetab (cust);
3357 }
3358
3359 /* Traversal function for dw2_forget_cached_source_info. */
3360
3361 static int
3362 dw2_free_cached_file_names (void **slot, void *info)
3363 {
3364 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3365
3366 if (file_data->real_names)
3367 {
3368 int i;
3369
3370 for (i = 0; i < file_data->num_file_names; ++i)
3371 {
3372 xfree ((void*) file_data->real_names[i]);
3373 file_data->real_names[i] = NULL;
3374 }
3375 }
3376
3377 return 1;
3378 }
3379
3380 void
3381 dwarf2_base_index_functions::forget_cached_source_info
3382 (struct objfile *objfile)
3383 {
3384 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3385
3386 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3387 dw2_free_cached_file_names, NULL);
3388 }
3389
3390 /* Helper function for dw2_map_symtabs_matching_filename that expands
3391 the symtabs and calls the iterator. */
3392
3393 static int
3394 dw2_map_expand_apply (struct objfile *objfile,
3395 struct dwarf2_per_cu_data *per_cu,
3396 const char *name, const char *real_path,
3397 gdb::function_view<bool (symtab *)> callback)
3398 {
3399 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3400
3401 /* Don't visit already-expanded CUs. */
3402 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3403 if (per_objfile->symtab_set_p (per_cu))
3404 return 0;
3405
3406 /* This may expand more than one symtab, and we want to iterate over
3407 all of them. */
3408 dw2_instantiate_symtab (per_cu, per_objfile, false);
3409
3410 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3411 last_made, callback);
3412 }
3413
3414 /* Implementation of the map_symtabs_matching_filename method. */
3415
3416 bool
3417 dwarf2_base_index_functions::map_symtabs_matching_filename
3418 (struct objfile *objfile, const char *name, const char *real_path,
3419 gdb::function_view<bool (symtab *)> callback)
3420 {
3421 const char *name_basename = lbasename (name);
3422 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3423
3424 /* The rule is CUs specify all the files, including those used by
3425 any TU, so there's no need to scan TUs here. */
3426
3427 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3428 {
3429 /* We only need to look at symtabs not already expanded. */
3430 if (per_objfile->symtab_set_p (per_cu))
3431 continue;
3432
3433 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3434 if (file_data == NULL)
3435 continue;
3436
3437 for (int j = 0; j < file_data->num_file_names; ++j)
3438 {
3439 const char *this_name = file_data->file_names[j];
3440 const char *this_real_name;
3441
3442 if (compare_filenames_for_search (this_name, name))
3443 {
3444 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3445 callback))
3446 return true;
3447 continue;
3448 }
3449
3450 /* Before we invoke realpath, which can get expensive when many
3451 files are involved, do a quick comparison of the basenames. */
3452 if (! basenames_may_differ
3453 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3454 continue;
3455
3456 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3457 if (compare_filenames_for_search (this_real_name, name))
3458 {
3459 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3460 callback))
3461 return true;
3462 continue;
3463 }
3464
3465 if (real_path != NULL)
3466 {
3467 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3468 gdb_assert (IS_ABSOLUTE_PATH (name));
3469 if (this_real_name != NULL
3470 && FILENAME_CMP (real_path, this_real_name) == 0)
3471 {
3472 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3473 callback))
3474 return true;
3475 continue;
3476 }
3477 }
3478 }
3479 }
3480
3481 return false;
3482 }
3483
3484 /* Struct used to manage iterating over all CUs looking for a symbol. */
3485
3486 struct dw2_symtab_iterator
3487 {
3488 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3489 dwarf2_per_objfile *per_objfile;
3490 /* If set, only look for symbols that match that block. Valid values are
3491 GLOBAL_BLOCK and STATIC_BLOCK. */
3492 gdb::optional<block_enum> block_index;
3493 /* The kind of symbol we're looking for. */
3494 domain_enum domain;
3495 /* The list of CUs from the index entry of the symbol,
3496 or NULL if not found. */
3497 offset_type *vec;
3498 /* The next element in VEC to look at. */
3499 int next;
3500 /* The number of elements in VEC, or zero if there is no match. */
3501 int length;
3502 /* Have we seen a global version of the symbol?
3503 If so we can ignore all further global instances.
3504 This is to work around gold/15646, inefficient gold-generated
3505 indices. */
3506 int global_seen;
3507 };
3508
3509 /* Initialize the index symtab iterator ITER, common part. */
3510
3511 static void
3512 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3513 dwarf2_per_objfile *per_objfile,
3514 gdb::optional<block_enum> block_index,
3515 domain_enum domain)
3516 {
3517 iter->per_objfile = per_objfile;
3518 iter->block_index = block_index;
3519 iter->domain = domain;
3520 iter->next = 0;
3521 iter->global_seen = 0;
3522 iter->vec = NULL;
3523 iter->length = 0;
3524 }
3525
3526 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3527
3528 static void
3529 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3530 dwarf2_per_objfile *per_objfile,
3531 gdb::optional<block_enum> block_index,
3532 domain_enum domain,
3533 const char *name)
3534 {
3535 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3536
3537 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3538 /* index is NULL if OBJF_READNOW. */
3539 if (index == NULL)
3540 return;
3541
3542 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3543 iter->length = MAYBE_SWAP (*iter->vec);
3544 }
3545
3546 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3547
3548 static void
3549 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3550 dwarf2_per_objfile *per_objfile,
3551 gdb::optional<block_enum> block_index,
3552 domain_enum domain, offset_type namei)
3553 {
3554 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3555
3556 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3557 /* index is NULL if OBJF_READNOW. */
3558 if (index == NULL)
3559 return;
3560
3561 gdb_assert (!index->symbol_name_slot_invalid (namei));
3562 const auto &bucket = index->symbol_table[namei];
3563
3564 iter->vec = (offset_type *) (index->constant_pool
3565 + MAYBE_SWAP (bucket.vec));
3566 iter->length = MAYBE_SWAP (*iter->vec);
3567 }
3568
3569 /* Return the next matching CU or NULL if there are no more. */
3570
3571 static struct dwarf2_per_cu_data *
3572 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3573 {
3574 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3575
3576 for ( ; iter->next < iter->length; ++iter->next)
3577 {
3578 offset_type cu_index_and_attrs =
3579 MAYBE_SWAP (iter->vec[iter->next + 1]);
3580 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3581 gdb_index_symbol_kind symbol_kind =
3582 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3583 /* Only check the symbol attributes if they're present.
3584 Indices prior to version 7 don't record them,
3585 and indices >= 7 may elide them for certain symbols
3586 (gold does this). */
3587 int attrs_valid =
3588 (per_objfile->per_bfd->index_table->version >= 7
3589 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3590
3591 /* Don't crash on bad data. */
3592 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3593 + per_objfile->per_bfd->all_type_units.size ()))
3594 {
3595 complaint (_(".gdb_index entry has bad CU index"
3596 " [in module %s]"), objfile_name (per_objfile->objfile));
3597 continue;
3598 }
3599
3600 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3601
3602 /* Skip if already read in. */
3603 if (per_objfile->symtab_set_p (per_cu))
3604 continue;
3605
3606 /* Check static vs global. */
3607 if (attrs_valid)
3608 {
3609 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3610
3611 if (iter->block_index.has_value ())
3612 {
3613 bool want_static = *iter->block_index == STATIC_BLOCK;
3614
3615 if (is_static != want_static)
3616 continue;
3617 }
3618
3619 /* Work around gold/15646. */
3620 if (!is_static
3621 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3622 {
3623 if (iter->global_seen)
3624 continue;
3625
3626 iter->global_seen = 1;
3627 }
3628 }
3629
3630 /* Only check the symbol's kind if it has one. */
3631 if (attrs_valid)
3632 {
3633 switch (iter->domain)
3634 {
3635 case VAR_DOMAIN:
3636 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3637 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3638 /* Some types are also in VAR_DOMAIN. */
3639 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3640 continue;
3641 break;
3642 case STRUCT_DOMAIN:
3643 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3644 continue;
3645 break;
3646 case LABEL_DOMAIN:
3647 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3648 continue;
3649 break;
3650 case MODULE_DOMAIN:
3651 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3652 continue;
3653 break;
3654 default:
3655 break;
3656 }
3657 }
3658
3659 ++iter->next;
3660 return per_cu;
3661 }
3662
3663 return NULL;
3664 }
3665
3666 struct compunit_symtab *
3667 dwarf2_gdb_index::lookup_symbol (struct objfile *objfile,
3668 block_enum block_index,
3669 const char *name, domain_enum domain)
3670 {
3671 struct compunit_symtab *stab_best = NULL;
3672 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3673
3674 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3675
3676 struct dw2_symtab_iterator iter;
3677 struct dwarf2_per_cu_data *per_cu;
3678
3679 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3680
3681 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3682 {
3683 struct symbol *sym, *with_opaque = NULL;
3684 struct compunit_symtab *stab
3685 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3686 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3687 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3688
3689 sym = block_find_symbol (block, name, domain,
3690 block_find_non_opaque_type_preferred,
3691 &with_opaque);
3692
3693 /* Some caution must be observed with overloaded functions
3694 and methods, since the index will not contain any overload
3695 information (but NAME might contain it). */
3696
3697 if (sym != NULL
3698 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3699 return stab;
3700 if (with_opaque != NULL
3701 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3702 stab_best = stab;
3703
3704 /* Keep looking through other CUs. */
3705 }
3706
3707 return stab_best;
3708 }
3709
3710 void
3711 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3712 bool print_bcache)
3713 {
3714 if (print_bcache)
3715 return;
3716
3717 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3718 int total = (per_objfile->per_bfd->all_comp_units.size ()
3719 + per_objfile->per_bfd->all_type_units.size ());
3720 int count = 0;
3721
3722 for (int i = 0; i < total; ++i)
3723 {
3724 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3725
3726 if (!per_objfile->symtab_set_p (per_cu))
3727 ++count;
3728 }
3729 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3730 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3731 }
3732
3733 /* This dumps minimal information about the index.
3734 It is called via "mt print objfiles".
3735 One use is to verify .gdb_index has been loaded by the
3736 gdb.dwarf2/gdb-index.exp testcase. */
3737
3738 void
3739 dwarf2_gdb_index::dump (struct objfile *objfile)
3740 {
3741 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3742
3743 gdb_assert (per_objfile->per_bfd->using_index);
3744 printf_filtered (".gdb_index:");
3745 if (per_objfile->per_bfd->index_table != NULL)
3746 {
3747 printf_filtered (" version %d\n",
3748 per_objfile->per_bfd->index_table->version);
3749 }
3750 else
3751 printf_filtered (" faked for \"readnow\"\n");
3752 printf_filtered ("\n");
3753 }
3754
3755 void
3756 dwarf2_gdb_index::expand_symtabs_for_function (struct objfile *objfile,
3757 const char *func_name)
3758 {
3759 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3760
3761 struct dw2_symtab_iterator iter;
3762 struct dwarf2_per_cu_data *per_cu;
3763
3764 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3765
3766 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3767 dw2_instantiate_symtab (per_cu, per_objfile, false);
3768
3769 }
3770
3771 void
3772 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3773 {
3774 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3775 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3776 + per_objfile->per_bfd->all_type_units.size ());
3777
3778 for (int i = 0; i < total_units; ++i)
3779 {
3780 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3781
3782 /* We don't want to directly expand a partial CU, because if we
3783 read it with the wrong language, then assertion failures can
3784 be triggered later on. See PR symtab/23010. So, tell
3785 dw2_instantiate_symtab to skip partial CUs -- any important
3786 partial CU will be read via DW_TAG_imported_unit anyway. */
3787 dw2_instantiate_symtab (per_cu, per_objfile, true);
3788 }
3789 }
3790
3791 void
3792 dwarf2_base_index_functions::expand_symtabs_with_fullname
3793 (struct objfile *objfile, const char *fullname)
3794 {
3795 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3796
3797 /* We don't need to consider type units here.
3798 This is only called for examining code, e.g. expand_line_sal.
3799 There can be an order of magnitude (or more) more type units
3800 than comp units, and we avoid them if we can. */
3801
3802 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3803 {
3804 /* We only need to look at symtabs not already expanded. */
3805 if (per_objfile->symtab_set_p (per_cu))
3806 continue;
3807
3808 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3809 if (file_data == NULL)
3810 continue;
3811
3812 for (int j = 0; j < file_data->num_file_names; ++j)
3813 {
3814 const char *this_fullname = file_data->file_names[j];
3815
3816 if (filename_cmp (this_fullname, fullname) == 0)
3817 {
3818 dw2_instantiate_symtab (per_cu, per_objfile, false);
3819 break;
3820 }
3821 }
3822 }
3823 }
3824
3825 static bool
3826 dw2_expand_symtabs_matching_symbol
3827 (mapped_index_base &index,
3828 const lookup_name_info &lookup_name_in,
3829 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3830 gdb::function_view<bool (offset_type)> match_callback,
3831 dwarf2_per_objfile *per_objfile);
3832
3833 static bool
3834 dw2_expand_symtabs_matching_one
3835 (dwarf2_per_cu_data *per_cu,
3836 dwarf2_per_objfile *per_objfile,
3837 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3838 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3839
3840 void
3841 dwarf2_gdb_index::map_matching_symbols
3842 (struct objfile *objfile,
3843 const lookup_name_info &name, domain_enum domain,
3844 int global,
3845 gdb::function_view<symbol_found_callback_ftype> callback,
3846 symbol_compare_ftype *ordered_compare)
3847 {
3848 /* Used for Ada. */
3849 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3850
3851 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3852
3853 if (per_objfile->per_bfd->index_table != nullptr)
3854 {
3855 mapped_index &index = *per_objfile->per_bfd->index_table;
3856
3857 const char *match_name = name.ada ().lookup_name ().c_str ();
3858 auto matcher = [&] (const char *symname)
3859 {
3860 if (ordered_compare == nullptr)
3861 return true;
3862 return ordered_compare (symname, match_name) == 0;
3863 };
3864
3865 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3866 [&] (offset_type namei)
3867 {
3868 struct dw2_symtab_iterator iter;
3869 struct dwarf2_per_cu_data *per_cu;
3870
3871 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3872 namei);
3873 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3874 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3875 nullptr);
3876 return true;
3877 }, per_objfile);
3878 }
3879 else
3880 {
3881 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3882 proceed assuming all symtabs have been read in. */
3883 }
3884
3885 for (compunit_symtab *cust : objfile->compunits ())
3886 {
3887 const struct block *block;
3888
3889 if (cust == NULL)
3890 continue;
3891 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3892 if (!iterate_over_symbols_terminated (block, name,
3893 domain, callback))
3894 return;
3895 }
3896 }
3897
3898 /* Starting from a search name, return the string that finds the upper
3899 bound of all strings that start with SEARCH_NAME in a sorted name
3900 list. Returns the empty string to indicate that the upper bound is
3901 the end of the list. */
3902
3903 static std::string
3904 make_sort_after_prefix_name (const char *search_name)
3905 {
3906 /* When looking to complete "func", we find the upper bound of all
3907 symbols that start with "func" by looking for where we'd insert
3908 the closest string that would follow "func" in lexicographical
3909 order. Usually, that's "func"-with-last-character-incremented,
3910 i.e. "fund". Mind non-ASCII characters, though. Usually those
3911 will be UTF-8 multi-byte sequences, but we can't be certain.
3912 Especially mind the 0xff character, which is a valid character in
3913 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3914 rule out compilers allowing it in identifiers. Note that
3915 conveniently, strcmp/strcasecmp are specified to compare
3916 characters interpreted as unsigned char. So what we do is treat
3917 the whole string as a base 256 number composed of a sequence of
3918 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3919 to 0, and carries 1 to the following more-significant position.
3920 If the very first character in SEARCH_NAME ends up incremented
3921 and carries/overflows, then the upper bound is the end of the
3922 list. The string after the empty string is also the empty
3923 string.
3924
3925 Some examples of this operation:
3926
3927 SEARCH_NAME => "+1" RESULT
3928
3929 "abc" => "abd"
3930 "ab\xff" => "ac"
3931 "\xff" "a" "\xff" => "\xff" "b"
3932 "\xff" => ""
3933 "\xff\xff" => ""
3934 "" => ""
3935
3936 Then, with these symbols for example:
3937
3938 func
3939 func1
3940 fund
3941
3942 completing "func" looks for symbols between "func" and
3943 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3944 which finds "func" and "func1", but not "fund".
3945
3946 And with:
3947
3948 funcÿ (Latin1 'ÿ' [0xff])
3949 funcÿ1
3950 fund
3951
3952 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3953 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3954
3955 And with:
3956
3957 ÿÿ (Latin1 'ÿ' [0xff])
3958 ÿÿ1
3959
3960 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3961 the end of the list.
3962 */
3963 std::string after = search_name;
3964 while (!after.empty () && (unsigned char) after.back () == 0xff)
3965 after.pop_back ();
3966 if (!after.empty ())
3967 after.back () = (unsigned char) after.back () + 1;
3968 return after;
3969 }
3970
3971 /* See declaration. */
3972
3973 std::pair<std::vector<name_component>::const_iterator,
3974 std::vector<name_component>::const_iterator>
3975 mapped_index_base::find_name_components_bounds
3976 (const lookup_name_info &lookup_name_without_params, language lang,
3977 dwarf2_per_objfile *per_objfile) const
3978 {
3979 auto *name_cmp
3980 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3981
3982 const char *lang_name
3983 = lookup_name_without_params.language_lookup_name (lang);
3984
3985 /* Comparison function object for lower_bound that matches against a
3986 given symbol name. */
3987 auto lookup_compare_lower = [&] (const name_component &elem,
3988 const char *name)
3989 {
3990 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3991 const char *elem_name = elem_qualified + elem.name_offset;
3992 return name_cmp (elem_name, name) < 0;
3993 };
3994
3995 /* Comparison function object for upper_bound that matches against a
3996 given symbol name. */
3997 auto lookup_compare_upper = [&] (const char *name,
3998 const name_component &elem)
3999 {
4000 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
4001 const char *elem_name = elem_qualified + elem.name_offset;
4002 return name_cmp (name, elem_name) < 0;
4003 };
4004
4005 auto begin = this->name_components.begin ();
4006 auto end = this->name_components.end ();
4007
4008 /* Find the lower bound. */
4009 auto lower = [&] ()
4010 {
4011 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4012 return begin;
4013 else
4014 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4015 } ();
4016
4017 /* Find the upper bound. */
4018 auto upper = [&] ()
4019 {
4020 if (lookup_name_without_params.completion_mode ())
4021 {
4022 /* In completion mode, we want UPPER to point past all
4023 symbols names that have the same prefix. I.e., with
4024 these symbols, and completing "func":
4025
4026 function << lower bound
4027 function1
4028 other_function << upper bound
4029
4030 We find the upper bound by looking for the insertion
4031 point of "func"-with-last-character-incremented,
4032 i.e. "fund". */
4033 std::string after = make_sort_after_prefix_name (lang_name);
4034 if (after.empty ())
4035 return end;
4036 return std::lower_bound (lower, end, after.c_str (),
4037 lookup_compare_lower);
4038 }
4039 else
4040 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4041 } ();
4042
4043 return {lower, upper};
4044 }
4045
4046 /* See declaration. */
4047
4048 void
4049 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4050 {
4051 if (!this->name_components.empty ())
4052 return;
4053
4054 this->name_components_casing = case_sensitivity;
4055 auto *name_cmp
4056 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4057
4058 /* The code below only knows how to break apart components of C++
4059 symbol names (and other languages that use '::' as
4060 namespace/module separator) and Ada symbol names. */
4061 auto count = this->symbol_name_count ();
4062 for (offset_type idx = 0; idx < count; idx++)
4063 {
4064 if (this->symbol_name_slot_invalid (idx))
4065 continue;
4066
4067 const char *name = this->symbol_name_at (idx, per_objfile);
4068
4069 /* Add each name component to the name component table. */
4070 unsigned int previous_len = 0;
4071
4072 if (strstr (name, "::") != nullptr)
4073 {
4074 for (unsigned int current_len = cp_find_first_component (name);
4075 name[current_len] != '\0';
4076 current_len += cp_find_first_component (name + current_len))
4077 {
4078 gdb_assert (name[current_len] == ':');
4079 this->name_components.push_back ({previous_len, idx});
4080 /* Skip the '::'. */
4081 current_len += 2;
4082 previous_len = current_len;
4083 }
4084 }
4085 else
4086 {
4087 /* Handle the Ada encoded (aka mangled) form here. */
4088 for (const char *iter = strstr (name, "__");
4089 iter != nullptr;
4090 iter = strstr (iter, "__"))
4091 {
4092 this->name_components.push_back ({previous_len, idx});
4093 iter += 2;
4094 previous_len = iter - name;
4095 }
4096 }
4097
4098 this->name_components.push_back ({previous_len, idx});
4099 }
4100
4101 /* Sort name_components elements by name. */
4102 auto name_comp_compare = [&] (const name_component &left,
4103 const name_component &right)
4104 {
4105 const char *left_qualified
4106 = this->symbol_name_at (left.idx, per_objfile);
4107 const char *right_qualified
4108 = this->symbol_name_at (right.idx, per_objfile);
4109
4110 const char *left_name = left_qualified + left.name_offset;
4111 const char *right_name = right_qualified + right.name_offset;
4112
4113 return name_cmp (left_name, right_name) < 0;
4114 };
4115
4116 std::sort (this->name_components.begin (),
4117 this->name_components.end (),
4118 name_comp_compare);
4119 }
4120
4121 /* Helper for dw2_expand_symtabs_matching that works with a
4122 mapped_index_base instead of the containing objfile. This is split
4123 to a separate function in order to be able to unit test the
4124 name_components matching using a mock mapped_index_base. For each
4125 symbol name that matches, calls MATCH_CALLBACK, passing it the
4126 symbol's index in the mapped_index_base symbol table. */
4127
4128 static bool
4129 dw2_expand_symtabs_matching_symbol
4130 (mapped_index_base &index,
4131 const lookup_name_info &lookup_name_in,
4132 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4133 gdb::function_view<bool (offset_type)> match_callback,
4134 dwarf2_per_objfile *per_objfile)
4135 {
4136 lookup_name_info lookup_name_without_params
4137 = lookup_name_in.make_ignore_params ();
4138
4139 /* Build the symbol name component sorted vector, if we haven't
4140 yet. */
4141 index.build_name_components (per_objfile);
4142
4143 /* The same symbol may appear more than once in the range though.
4144 E.g., if we're looking for symbols that complete "w", and we have
4145 a symbol named "w1::w2", we'll find the two name components for
4146 that same symbol in the range. To be sure we only call the
4147 callback once per symbol, we first collect the symbol name
4148 indexes that matched in a temporary vector and ignore
4149 duplicates. */
4150 std::vector<offset_type> matches;
4151
4152 struct name_and_matcher
4153 {
4154 symbol_name_matcher_ftype *matcher;
4155 const char *name;
4156
4157 bool operator== (const name_and_matcher &other) const
4158 {
4159 return matcher == other.matcher && strcmp (name, other.name) == 0;
4160 }
4161 };
4162
4163 /* A vector holding all the different symbol name matchers, for all
4164 languages. */
4165 std::vector<name_and_matcher> matchers;
4166
4167 for (int i = 0; i < nr_languages; i++)
4168 {
4169 enum language lang_e = (enum language) i;
4170
4171 const language_defn *lang = language_def (lang_e);
4172 symbol_name_matcher_ftype *name_matcher
4173 = lang->get_symbol_name_matcher (lookup_name_without_params);
4174
4175 name_and_matcher key {
4176 name_matcher,
4177 lookup_name_without_params.language_lookup_name (lang_e)
4178 };
4179
4180 /* Don't insert the same comparison routine more than once.
4181 Note that we do this linear walk. This is not a problem in
4182 practice because the number of supported languages is
4183 low. */
4184 if (std::find (matchers.begin (), matchers.end (), key)
4185 != matchers.end ())
4186 continue;
4187 matchers.push_back (std::move (key));
4188
4189 auto bounds
4190 = index.find_name_components_bounds (lookup_name_without_params,
4191 lang_e, per_objfile);
4192
4193 /* Now for each symbol name in range, check to see if we have a name
4194 match, and if so, call the MATCH_CALLBACK callback. */
4195
4196 for (; bounds.first != bounds.second; ++bounds.first)
4197 {
4198 const char *qualified
4199 = index.symbol_name_at (bounds.first->idx, per_objfile);
4200
4201 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4202 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4203 continue;
4204
4205 matches.push_back (bounds.first->idx);
4206 }
4207 }
4208
4209 std::sort (matches.begin (), matches.end ());
4210
4211 /* Finally call the callback, once per match. */
4212 ULONGEST prev = -1;
4213 bool result = true;
4214 for (offset_type idx : matches)
4215 {
4216 if (prev != idx)
4217 {
4218 if (!match_callback (idx))
4219 {
4220 result = false;
4221 break;
4222 }
4223 prev = idx;
4224 }
4225 }
4226
4227 /* Above we use a type wider than idx's for 'prev', since 0 and
4228 (offset_type)-1 are both possible values. */
4229 static_assert (sizeof (prev) > sizeof (offset_type), "");
4230
4231 return result;
4232 }
4233
4234 #if GDB_SELF_TEST
4235
4236 namespace selftests { namespace dw2_expand_symtabs_matching {
4237
4238 /* A mock .gdb_index/.debug_names-like name index table, enough to
4239 exercise dw2_expand_symtabs_matching_symbol, which works with the
4240 mapped_index_base interface. Builds an index from the symbol list
4241 passed as parameter to the constructor. */
4242 class mock_mapped_index : public mapped_index_base
4243 {
4244 public:
4245 mock_mapped_index (gdb::array_view<const char *> symbols)
4246 : m_symbol_table (symbols)
4247 {}
4248
4249 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4250
4251 /* Return the number of names in the symbol table. */
4252 size_t symbol_name_count () const override
4253 {
4254 return m_symbol_table.size ();
4255 }
4256
4257 /* Get the name of the symbol at IDX in the symbol table. */
4258 const char *symbol_name_at
4259 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4260 {
4261 return m_symbol_table[idx];
4262 }
4263
4264 private:
4265 gdb::array_view<const char *> m_symbol_table;
4266 };
4267
4268 /* Convenience function that converts a NULL pointer to a "<null>"
4269 string, to pass to print routines. */
4270
4271 static const char *
4272 string_or_null (const char *str)
4273 {
4274 return str != NULL ? str : "<null>";
4275 }
4276
4277 /* Check if a lookup_name_info built from
4278 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4279 index. EXPECTED_LIST is the list of expected matches, in expected
4280 matching order. If no match expected, then an empty list is
4281 specified. Returns true on success. On failure prints a warning
4282 indicating the file:line that failed, and returns false. */
4283
4284 static bool
4285 check_match (const char *file, int line,
4286 mock_mapped_index &mock_index,
4287 const char *name, symbol_name_match_type match_type,
4288 bool completion_mode,
4289 std::initializer_list<const char *> expected_list,
4290 dwarf2_per_objfile *per_objfile)
4291 {
4292 lookup_name_info lookup_name (name, match_type, completion_mode);
4293
4294 bool matched = true;
4295
4296 auto mismatch = [&] (const char *expected_str,
4297 const char *got)
4298 {
4299 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4300 "expected=\"%s\", got=\"%s\"\n"),
4301 file, line,
4302 (match_type == symbol_name_match_type::FULL
4303 ? "FULL" : "WILD"),
4304 name, string_or_null (expected_str), string_or_null (got));
4305 matched = false;
4306 };
4307
4308 auto expected_it = expected_list.begin ();
4309 auto expected_end = expected_list.end ();
4310
4311 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4312 nullptr,
4313 [&] (offset_type idx)
4314 {
4315 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4316 const char *expected_str
4317 = expected_it == expected_end ? NULL : *expected_it++;
4318
4319 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4320 mismatch (expected_str, matched_name);
4321 return true;
4322 }, per_objfile);
4323
4324 const char *expected_str
4325 = expected_it == expected_end ? NULL : *expected_it++;
4326 if (expected_str != NULL)
4327 mismatch (expected_str, NULL);
4328
4329 return matched;
4330 }
4331
4332 /* The symbols added to the mock mapped_index for testing (in
4333 canonical form). */
4334 static const char *test_symbols[] = {
4335 "function",
4336 "std::bar",
4337 "std::zfunction",
4338 "std::zfunction2",
4339 "w1::w2",
4340 "ns::foo<char*>",
4341 "ns::foo<int>",
4342 "ns::foo<long>",
4343 "ns2::tmpl<int>::foo2",
4344 "(anonymous namespace)::A::B::C",
4345
4346 /* These are used to check that the increment-last-char in the
4347 matching algorithm for completion doesn't match "t1_fund" when
4348 completing "t1_func". */
4349 "t1_func",
4350 "t1_func1",
4351 "t1_fund",
4352 "t1_fund1",
4353
4354 /* A UTF-8 name with multi-byte sequences to make sure that
4355 cp-name-parser understands this as a single identifier ("função"
4356 is "function" in PT). */
4357 u8"u8função",
4358
4359 /* \377 (0xff) is Latin1 'ÿ'. */
4360 "yfunc\377",
4361
4362 /* \377 (0xff) is Latin1 'ÿ'. */
4363 "\377",
4364 "\377\377123",
4365
4366 /* A name with all sorts of complications. Starts with "z" to make
4367 it easier for the completion tests below. */
4368 #define Z_SYM_NAME \
4369 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4370 "::tuple<(anonymous namespace)::ui*, " \
4371 "std::default_delete<(anonymous namespace)::ui>, void>"
4372
4373 Z_SYM_NAME
4374 };
4375
4376 /* Returns true if the mapped_index_base::find_name_component_bounds
4377 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4378 in completion mode. */
4379
4380 static bool
4381 check_find_bounds_finds (mapped_index_base &index,
4382 const char *search_name,
4383 gdb::array_view<const char *> expected_syms,
4384 dwarf2_per_objfile *per_objfile)
4385 {
4386 lookup_name_info lookup_name (search_name,
4387 symbol_name_match_type::FULL, true);
4388
4389 auto bounds = index.find_name_components_bounds (lookup_name,
4390 language_cplus,
4391 per_objfile);
4392
4393 size_t distance = std::distance (bounds.first, bounds.second);
4394 if (distance != expected_syms.size ())
4395 return false;
4396
4397 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4398 {
4399 auto nc_elem = bounds.first + exp_elem;
4400 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4401 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4402 return false;
4403 }
4404
4405 return true;
4406 }
4407
4408 /* Test the lower-level mapped_index::find_name_component_bounds
4409 method. */
4410
4411 static void
4412 test_mapped_index_find_name_component_bounds ()
4413 {
4414 mock_mapped_index mock_index (test_symbols);
4415
4416 mock_index.build_name_components (NULL /* per_objfile */);
4417
4418 /* Test the lower-level mapped_index::find_name_component_bounds
4419 method in completion mode. */
4420 {
4421 static const char *expected_syms[] = {
4422 "t1_func",
4423 "t1_func1",
4424 };
4425
4426 SELF_CHECK (check_find_bounds_finds
4427 (mock_index, "t1_func", expected_syms,
4428 NULL /* per_objfile */));
4429 }
4430
4431 /* Check that the increment-last-char in the name matching algorithm
4432 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4433 {
4434 static const char *expected_syms1[] = {
4435 "\377",
4436 "\377\377123",
4437 };
4438 SELF_CHECK (check_find_bounds_finds
4439 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4440
4441 static const char *expected_syms2[] = {
4442 "\377\377123",
4443 };
4444 SELF_CHECK (check_find_bounds_finds
4445 (mock_index, "\377\377", expected_syms2,
4446 NULL /* per_objfile */));
4447 }
4448 }
4449
4450 /* Test dw2_expand_symtabs_matching_symbol. */
4451
4452 static void
4453 test_dw2_expand_symtabs_matching_symbol ()
4454 {
4455 mock_mapped_index mock_index (test_symbols);
4456
4457 /* We let all tests run until the end even if some fails, for debug
4458 convenience. */
4459 bool any_mismatch = false;
4460
4461 /* Create the expected symbols list (an initializer_list). Needed
4462 because lists have commas, and we need to pass them to CHECK,
4463 which is a macro. */
4464 #define EXPECT(...) { __VA_ARGS__ }
4465
4466 /* Wrapper for check_match that passes down the current
4467 __FILE__/__LINE__. */
4468 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4469 any_mismatch |= !check_match (__FILE__, __LINE__, \
4470 mock_index, \
4471 NAME, MATCH_TYPE, COMPLETION_MODE, \
4472 EXPECTED_LIST, NULL)
4473
4474 /* Identity checks. */
4475 for (const char *sym : test_symbols)
4476 {
4477 /* Should be able to match all existing symbols. */
4478 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4479 EXPECT (sym));
4480
4481 /* Should be able to match all existing symbols with
4482 parameters. */
4483 std::string with_params = std::string (sym) + "(int)";
4484 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4485 EXPECT (sym));
4486
4487 /* Should be able to match all existing symbols with
4488 parameters and qualifiers. */
4489 with_params = std::string (sym) + " ( int ) const";
4490 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4491 EXPECT (sym));
4492
4493 /* This should really find sym, but cp-name-parser.y doesn't
4494 know about lvalue/rvalue qualifiers yet. */
4495 with_params = std::string (sym) + " ( int ) &&";
4496 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4497 {});
4498 }
4499
4500 /* Check that the name matching algorithm for completion doesn't get
4501 confused with Latin1 'ÿ' / 0xff. */
4502 {
4503 static const char str[] = "\377";
4504 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4505 EXPECT ("\377", "\377\377123"));
4506 }
4507
4508 /* Check that the increment-last-char in the matching algorithm for
4509 completion doesn't match "t1_fund" when completing "t1_func". */
4510 {
4511 static const char str[] = "t1_func";
4512 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4513 EXPECT ("t1_func", "t1_func1"));
4514 }
4515
4516 /* Check that completion mode works at each prefix of the expected
4517 symbol name. */
4518 {
4519 static const char str[] = "function(int)";
4520 size_t len = strlen (str);
4521 std::string lookup;
4522
4523 for (size_t i = 1; i < len; i++)
4524 {
4525 lookup.assign (str, i);
4526 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4527 EXPECT ("function"));
4528 }
4529 }
4530
4531 /* While "w" is a prefix of both components, the match function
4532 should still only be called once. */
4533 {
4534 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4535 EXPECT ("w1::w2"));
4536 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4537 EXPECT ("w1::w2"));
4538 }
4539
4540 /* Same, with a "complicated" symbol. */
4541 {
4542 static const char str[] = Z_SYM_NAME;
4543 size_t len = strlen (str);
4544 std::string lookup;
4545
4546 for (size_t i = 1; i < len; i++)
4547 {
4548 lookup.assign (str, i);
4549 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4550 EXPECT (Z_SYM_NAME));
4551 }
4552 }
4553
4554 /* In FULL mode, an incomplete symbol doesn't match. */
4555 {
4556 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4557 {});
4558 }
4559
4560 /* A complete symbol with parameters matches any overload, since the
4561 index has no overload info. */
4562 {
4563 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4564 EXPECT ("std::zfunction", "std::zfunction2"));
4565 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4566 EXPECT ("std::zfunction", "std::zfunction2"));
4567 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4568 EXPECT ("std::zfunction", "std::zfunction2"));
4569 }
4570
4571 /* Check that whitespace is ignored appropriately. A symbol with a
4572 template argument list. */
4573 {
4574 static const char expected[] = "ns::foo<int>";
4575 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4576 EXPECT (expected));
4577 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4578 EXPECT (expected));
4579 }
4580
4581 /* Check that whitespace is ignored appropriately. A symbol with a
4582 template argument list that includes a pointer. */
4583 {
4584 static const char expected[] = "ns::foo<char*>";
4585 /* Try both completion and non-completion modes. */
4586 static const bool completion_mode[2] = {false, true};
4587 for (size_t i = 0; i < 2; i++)
4588 {
4589 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4590 completion_mode[i], EXPECT (expected));
4591 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4592 completion_mode[i], EXPECT (expected));
4593
4594 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4595 completion_mode[i], EXPECT (expected));
4596 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4597 completion_mode[i], EXPECT (expected));
4598 }
4599 }
4600
4601 {
4602 /* Check method qualifiers are ignored. */
4603 static const char expected[] = "ns::foo<char*>";
4604 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4605 symbol_name_match_type::FULL, true, EXPECT (expected));
4606 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4607 symbol_name_match_type::FULL, true, EXPECT (expected));
4608 CHECK_MATCH ("foo < char * > ( int ) const",
4609 symbol_name_match_type::WILD, true, EXPECT (expected));
4610 CHECK_MATCH ("foo < char * > ( int ) &&",
4611 symbol_name_match_type::WILD, true, EXPECT (expected));
4612 }
4613
4614 /* Test lookup names that don't match anything. */
4615 {
4616 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4617 {});
4618
4619 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4620 {});
4621 }
4622
4623 /* Some wild matching tests, exercising "(anonymous namespace)",
4624 which should not be confused with a parameter list. */
4625 {
4626 static const char *syms[] = {
4627 "A::B::C",
4628 "B::C",
4629 "C",
4630 "A :: B :: C ( int )",
4631 "B :: C ( int )",
4632 "C ( int )",
4633 };
4634
4635 for (const char *s : syms)
4636 {
4637 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4638 EXPECT ("(anonymous namespace)::A::B::C"));
4639 }
4640 }
4641
4642 {
4643 static const char expected[] = "ns2::tmpl<int>::foo2";
4644 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4645 EXPECT (expected));
4646 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4647 EXPECT (expected));
4648 }
4649
4650 SELF_CHECK (!any_mismatch);
4651
4652 #undef EXPECT
4653 #undef CHECK_MATCH
4654 }
4655
4656 static void
4657 run_test ()
4658 {
4659 test_mapped_index_find_name_component_bounds ();
4660 test_dw2_expand_symtabs_matching_symbol ();
4661 }
4662
4663 }} // namespace selftests::dw2_expand_symtabs_matching
4664
4665 #endif /* GDB_SELF_TEST */
4666
4667 /* If FILE_MATCHER is NULL or if PER_CU has
4668 dwarf2_per_cu_quick_data::MARK set (see
4669 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4670 EXPANSION_NOTIFY on it. */
4671
4672 static bool
4673 dw2_expand_symtabs_matching_one
4674 (dwarf2_per_cu_data *per_cu,
4675 dwarf2_per_objfile *per_objfile,
4676 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4677 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4678 {
4679 if (file_matcher == NULL || per_cu->v.quick->mark)
4680 {
4681 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4682
4683 compunit_symtab *symtab
4684 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4685 gdb_assert (symtab != nullptr);
4686
4687 if (expansion_notify != NULL && symtab_was_null)
4688 return expansion_notify (symtab);
4689 }
4690 return true;
4691 }
4692
4693 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4694 matched, to expand corresponding CUs that were marked. IDX is the
4695 index of the symbol name that matched. */
4696
4697 static bool
4698 dw2_expand_marked_cus
4699 (dwarf2_per_objfile *per_objfile, offset_type idx,
4700 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4701 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4702 search_domain kind)
4703 {
4704 offset_type *vec, vec_len, vec_idx;
4705 bool global_seen = false;
4706 mapped_index &index = *per_objfile->per_bfd->index_table;
4707
4708 vec = (offset_type *) (index.constant_pool
4709 + MAYBE_SWAP (index.symbol_table[idx].vec));
4710 vec_len = MAYBE_SWAP (vec[0]);
4711 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4712 {
4713 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4714 /* This value is only valid for index versions >= 7. */
4715 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4716 gdb_index_symbol_kind symbol_kind =
4717 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4718 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4719 /* Only check the symbol attributes if they're present.
4720 Indices prior to version 7 don't record them,
4721 and indices >= 7 may elide them for certain symbols
4722 (gold does this). */
4723 int attrs_valid =
4724 (index.version >= 7
4725 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4726
4727 /* Work around gold/15646. */
4728 if (attrs_valid
4729 && !is_static
4730 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4731 {
4732 if (global_seen)
4733 continue;
4734
4735 global_seen = true;
4736 }
4737
4738 /* Only check the symbol's kind if it has one. */
4739 if (attrs_valid)
4740 {
4741 switch (kind)
4742 {
4743 case VARIABLES_DOMAIN:
4744 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4745 continue;
4746 break;
4747 case FUNCTIONS_DOMAIN:
4748 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4749 continue;
4750 break;
4751 case TYPES_DOMAIN:
4752 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4753 continue;
4754 break;
4755 case MODULES_DOMAIN:
4756 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4757 continue;
4758 break;
4759 default:
4760 break;
4761 }
4762 }
4763
4764 /* Don't crash on bad data. */
4765 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4766 + per_objfile->per_bfd->all_type_units.size ()))
4767 {
4768 complaint (_(".gdb_index entry has bad CU index"
4769 " [in module %s]"), objfile_name (per_objfile->objfile));
4770 continue;
4771 }
4772
4773 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4774 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4775 expansion_notify))
4776 return false;
4777 }
4778
4779 return true;
4780 }
4781
4782 /* If FILE_MATCHER is non-NULL, set all the
4783 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4784 that match FILE_MATCHER. */
4785
4786 static void
4787 dw_expand_symtabs_matching_file_matcher
4788 (dwarf2_per_objfile *per_objfile,
4789 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4790 {
4791 if (file_matcher == NULL)
4792 return;
4793
4794 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4795 htab_eq_pointer,
4796 NULL, xcalloc, xfree));
4797 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4798 htab_eq_pointer,
4799 NULL, xcalloc, xfree));
4800
4801 /* The rule is CUs specify all the files, including those used by
4802 any TU, so there's no need to scan TUs here. */
4803
4804 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4805 {
4806 QUIT;
4807
4808 per_cu->v.quick->mark = 0;
4809
4810 /* We only need to look at symtabs not already expanded. */
4811 if (per_objfile->symtab_set_p (per_cu))
4812 continue;
4813
4814 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4815 if (file_data == NULL)
4816 continue;
4817
4818 if (htab_find (visited_not_found.get (), file_data) != NULL)
4819 continue;
4820 else if (htab_find (visited_found.get (), file_data) != NULL)
4821 {
4822 per_cu->v.quick->mark = 1;
4823 continue;
4824 }
4825
4826 for (int j = 0; j < file_data->num_file_names; ++j)
4827 {
4828 const char *this_real_name;
4829
4830 if (file_matcher (file_data->file_names[j], false))
4831 {
4832 per_cu->v.quick->mark = 1;
4833 break;
4834 }
4835
4836 /* Before we invoke realpath, which can get expensive when many
4837 files are involved, do a quick comparison of the basenames. */
4838 if (!basenames_may_differ
4839 && !file_matcher (lbasename (file_data->file_names[j]),
4840 true))
4841 continue;
4842
4843 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4844 if (file_matcher (this_real_name, false))
4845 {
4846 per_cu->v.quick->mark = 1;
4847 break;
4848 }
4849 }
4850
4851 void **slot = htab_find_slot (per_cu->v.quick->mark
4852 ? visited_found.get ()
4853 : visited_not_found.get (),
4854 file_data, INSERT);
4855 *slot = file_data;
4856 }
4857 }
4858
4859 bool
4860 dwarf2_gdb_index::expand_symtabs_matching
4861 (struct objfile *objfile,
4862 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4863 const lookup_name_info *lookup_name,
4864 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4865 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4866 enum search_domain kind)
4867 {
4868 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4869
4870 /* index_table is NULL if OBJF_READNOW. */
4871 if (!per_objfile->per_bfd->index_table)
4872 return true;
4873
4874 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4875
4876 if (symbol_matcher == NULL && lookup_name == NULL)
4877 {
4878 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4879 {
4880 QUIT;
4881
4882 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4883 file_matcher,
4884 expansion_notify))
4885 return false;
4886 }
4887 return true;
4888 }
4889
4890 mapped_index &index = *per_objfile->per_bfd->index_table;
4891
4892 bool result
4893 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4894 symbol_matcher,
4895 [&] (offset_type idx)
4896 {
4897 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4898 expansion_notify, kind))
4899 return false;
4900 return true;
4901 }, per_objfile);
4902
4903 return result;
4904 }
4905
4906 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4907 symtab. */
4908
4909 static struct compunit_symtab *
4910 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4911 CORE_ADDR pc)
4912 {
4913 int i;
4914
4915 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4916 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4917 return cust;
4918
4919 if (cust->includes == NULL)
4920 return NULL;
4921
4922 for (i = 0; cust->includes[i]; ++i)
4923 {
4924 struct compunit_symtab *s = cust->includes[i];
4925
4926 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4927 if (s != NULL)
4928 return s;
4929 }
4930
4931 return NULL;
4932 }
4933
4934 struct compunit_symtab *
4935 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4936 (struct objfile *objfile,
4937 struct bound_minimal_symbol msymbol,
4938 CORE_ADDR pc,
4939 struct obj_section *section,
4940 int warn_if_readin)
4941 {
4942 struct dwarf2_per_cu_data *data;
4943 struct compunit_symtab *result;
4944
4945 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4946 if (per_objfile->per_bfd->index_addrmap == nullptr)
4947 return NULL;
4948
4949 CORE_ADDR baseaddr = objfile->text_section_offset ();
4950 data = ((struct dwarf2_per_cu_data *)
4951 addrmap_find (per_objfile->per_bfd->index_addrmap,
4952 pc - baseaddr));
4953 if (!data)
4954 return NULL;
4955
4956 if (warn_if_readin && per_objfile->symtab_set_p (data))
4957 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4958 paddress (objfile->arch (), pc));
4959
4960 result = recursively_find_pc_sect_compunit_symtab
4961 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4962
4963 gdb_assert (result != NULL);
4964 return result;
4965 }
4966
4967 void
4968 dwarf2_base_index_functions::map_symbol_filenames
4969 (struct objfile *objfile,
4970 gdb::function_view<symbol_filename_ftype> fun,
4971 bool need_fullname)
4972 {
4973 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4974
4975 if (!per_objfile->per_bfd->filenames_cache)
4976 {
4977 per_objfile->per_bfd->filenames_cache.emplace ();
4978
4979 htab_up visited (htab_create_alloc (10,
4980 htab_hash_pointer, htab_eq_pointer,
4981 NULL, xcalloc, xfree));
4982
4983 /* The rule is CUs specify all the files, including those used
4984 by any TU, so there's no need to scan TUs here. We can
4985 ignore file names coming from already-expanded CUs. */
4986
4987 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4988 {
4989 if (per_objfile->symtab_set_p (per_cu))
4990 {
4991 void **slot = htab_find_slot (visited.get (),
4992 per_cu->v.quick->file_names,
4993 INSERT);
4994
4995 *slot = per_cu->v.quick->file_names;
4996 }
4997 }
4998
4999 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5000 {
5001 /* We only need to look at symtabs not already expanded. */
5002 if (per_objfile->symtab_set_p (per_cu))
5003 continue;
5004
5005 quick_file_names *file_data
5006 = dw2_get_file_names (per_cu, per_objfile);
5007 if (file_data == NULL)
5008 continue;
5009
5010 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5011 if (*slot)
5012 {
5013 /* Already visited. */
5014 continue;
5015 }
5016 *slot = file_data;
5017
5018 for (int j = 0; j < file_data->num_file_names; ++j)
5019 {
5020 const char *filename = file_data->file_names[j];
5021 per_objfile->per_bfd->filenames_cache->seen (filename);
5022 }
5023 }
5024 }
5025
5026 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
5027 {
5028 gdb::unique_xmalloc_ptr<char> this_real_name;
5029
5030 if (need_fullname)
5031 this_real_name = gdb_realpath (filename);
5032 fun (filename, this_real_name.get ());
5033 });
5034 }
5035
5036 bool
5037 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
5038 {
5039 return true;
5040 }
5041
5042 /* DWARF-5 debug_names reader. */
5043
5044 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5045 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5046
5047 /* A helper function that reads the .debug_names section in SECTION
5048 and fills in MAP. FILENAME is the name of the file containing the
5049 section; it is used for error reporting.
5050
5051 Returns true if all went well, false otherwise. */
5052
5053 static bool
5054 read_debug_names_from_section (struct objfile *objfile,
5055 const char *filename,
5056 struct dwarf2_section_info *section,
5057 mapped_debug_names &map)
5058 {
5059 if (section->empty ())
5060 return false;
5061
5062 /* Older elfutils strip versions could keep the section in the main
5063 executable while splitting it for the separate debug info file. */
5064 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5065 return false;
5066
5067 section->read (objfile);
5068
5069 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5070
5071 const gdb_byte *addr = section->buffer;
5072
5073 bfd *const abfd = section->get_bfd_owner ();
5074
5075 unsigned int bytes_read;
5076 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5077 addr += bytes_read;
5078
5079 map.dwarf5_is_dwarf64 = bytes_read != 4;
5080 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5081 if (bytes_read + length != section->size)
5082 {
5083 /* There may be multiple per-CU indices. */
5084 warning (_("Section .debug_names in %s length %s does not match "
5085 "section length %s, ignoring .debug_names."),
5086 filename, plongest (bytes_read + length),
5087 pulongest (section->size));
5088 return false;
5089 }
5090
5091 /* The version number. */
5092 uint16_t version = read_2_bytes (abfd, addr);
5093 addr += 2;
5094 if (version != 5)
5095 {
5096 warning (_("Section .debug_names in %s has unsupported version %d, "
5097 "ignoring .debug_names."),
5098 filename, version);
5099 return false;
5100 }
5101
5102 /* Padding. */
5103 uint16_t padding = read_2_bytes (abfd, addr);
5104 addr += 2;
5105 if (padding != 0)
5106 {
5107 warning (_("Section .debug_names in %s has unsupported padding %d, "
5108 "ignoring .debug_names."),
5109 filename, padding);
5110 return false;
5111 }
5112
5113 /* comp_unit_count - The number of CUs in the CU list. */
5114 map.cu_count = read_4_bytes (abfd, addr);
5115 addr += 4;
5116
5117 /* local_type_unit_count - The number of TUs in the local TU
5118 list. */
5119 map.tu_count = read_4_bytes (abfd, addr);
5120 addr += 4;
5121
5122 /* foreign_type_unit_count - The number of TUs in the foreign TU
5123 list. */
5124 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5125 addr += 4;
5126 if (foreign_tu_count != 0)
5127 {
5128 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5129 "ignoring .debug_names."),
5130 filename, static_cast<unsigned long> (foreign_tu_count));
5131 return false;
5132 }
5133
5134 /* bucket_count - The number of hash buckets in the hash lookup
5135 table. */
5136 map.bucket_count = read_4_bytes (abfd, addr);
5137 addr += 4;
5138
5139 /* name_count - The number of unique names in the index. */
5140 map.name_count = read_4_bytes (abfd, addr);
5141 addr += 4;
5142
5143 /* abbrev_table_size - The size in bytes of the abbreviations
5144 table. */
5145 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5146 addr += 4;
5147
5148 /* augmentation_string_size - The size in bytes of the augmentation
5149 string. This value is rounded up to a multiple of 4. */
5150 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5151 addr += 4;
5152 map.augmentation_is_gdb = ((augmentation_string_size
5153 == sizeof (dwarf5_augmentation))
5154 && memcmp (addr, dwarf5_augmentation,
5155 sizeof (dwarf5_augmentation)) == 0);
5156 augmentation_string_size += (-augmentation_string_size) & 3;
5157 addr += augmentation_string_size;
5158
5159 /* List of CUs */
5160 map.cu_table_reordered = addr;
5161 addr += map.cu_count * map.offset_size;
5162
5163 /* List of Local TUs */
5164 map.tu_table_reordered = addr;
5165 addr += map.tu_count * map.offset_size;
5166
5167 /* Hash Lookup Table */
5168 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5169 addr += map.bucket_count * 4;
5170 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5171 addr += map.name_count * 4;
5172
5173 /* Name Table */
5174 map.name_table_string_offs_reordered = addr;
5175 addr += map.name_count * map.offset_size;
5176 map.name_table_entry_offs_reordered = addr;
5177 addr += map.name_count * map.offset_size;
5178
5179 const gdb_byte *abbrev_table_start = addr;
5180 for (;;)
5181 {
5182 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5183 addr += bytes_read;
5184 if (index_num == 0)
5185 break;
5186
5187 const auto insertpair
5188 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5189 if (!insertpair.second)
5190 {
5191 warning (_("Section .debug_names in %s has duplicate index %s, "
5192 "ignoring .debug_names."),
5193 filename, pulongest (index_num));
5194 return false;
5195 }
5196 mapped_debug_names::index_val &indexval = insertpair.first->second;
5197 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5198 addr += bytes_read;
5199
5200 for (;;)
5201 {
5202 mapped_debug_names::index_val::attr attr;
5203 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5204 addr += bytes_read;
5205 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5206 addr += bytes_read;
5207 if (attr.form == DW_FORM_implicit_const)
5208 {
5209 attr.implicit_const = read_signed_leb128 (abfd, addr,
5210 &bytes_read);
5211 addr += bytes_read;
5212 }
5213 if (attr.dw_idx == 0 && attr.form == 0)
5214 break;
5215 indexval.attr_vec.push_back (std::move (attr));
5216 }
5217 }
5218 if (addr != abbrev_table_start + abbrev_table_size)
5219 {
5220 warning (_("Section .debug_names in %s has abbreviation_table "
5221 "of size %s vs. written as %u, ignoring .debug_names."),
5222 filename, plongest (addr - abbrev_table_start),
5223 abbrev_table_size);
5224 return false;
5225 }
5226 map.entry_pool = addr;
5227
5228 return true;
5229 }
5230
5231 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5232 list. */
5233
5234 static void
5235 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5236 const mapped_debug_names &map,
5237 dwarf2_section_info &section,
5238 bool is_dwz)
5239 {
5240 if (!map.augmentation_is_gdb)
5241 {
5242 for (uint32_t i = 0; i < map.cu_count; ++i)
5243 {
5244 sect_offset sect_off
5245 = (sect_offset) (extract_unsigned_integer
5246 (map.cu_table_reordered + i * map.offset_size,
5247 map.offset_size,
5248 map.dwarf5_byte_order));
5249 /* We don't know the length of the CU, because the CU list in a
5250 .debug_names index can be incomplete, so we can't use the start
5251 of the next CU as end of this CU. We create the CUs here with
5252 length 0, and in cutu_reader::cutu_reader we'll fill in the
5253 actual length. */
5254 dwarf2_per_cu_data *per_cu
5255 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5256 sect_off, 0);
5257 per_bfd->all_comp_units.push_back (per_cu);
5258 }
5259 return;
5260 }
5261
5262 sect_offset sect_off_prev;
5263 for (uint32_t i = 0; i <= map.cu_count; ++i)
5264 {
5265 sect_offset sect_off_next;
5266 if (i < map.cu_count)
5267 {
5268 sect_off_next
5269 = (sect_offset) (extract_unsigned_integer
5270 (map.cu_table_reordered + i * map.offset_size,
5271 map.offset_size,
5272 map.dwarf5_byte_order));
5273 }
5274 else
5275 sect_off_next = (sect_offset) section.size;
5276 if (i >= 1)
5277 {
5278 const ULONGEST length = sect_off_next - sect_off_prev;
5279 dwarf2_per_cu_data *per_cu
5280 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5281 sect_off_prev, length);
5282 per_bfd->all_comp_units.push_back (per_cu);
5283 }
5284 sect_off_prev = sect_off_next;
5285 }
5286 }
5287
5288 /* Read the CU list from the mapped index, and use it to create all
5289 the CU objects for this dwarf2_per_objfile. */
5290
5291 static void
5292 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5293 const mapped_debug_names &map,
5294 const mapped_debug_names &dwz_map)
5295 {
5296 gdb_assert (per_bfd->all_comp_units.empty ());
5297 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5298
5299 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5300 false /* is_dwz */);
5301
5302 if (dwz_map.cu_count == 0)
5303 return;
5304
5305 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5306 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5307 true /* is_dwz */);
5308 }
5309
5310 /* Read .debug_names. If everything went ok, initialize the "quick"
5311 elements of all the CUs and return true. Otherwise, return false. */
5312
5313 static bool
5314 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5315 {
5316 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5317 mapped_debug_names dwz_map;
5318 struct objfile *objfile = per_objfile->objfile;
5319 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5320
5321 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5322 &per_bfd->debug_names, *map))
5323 return false;
5324
5325 /* Don't use the index if it's empty. */
5326 if (map->name_count == 0)
5327 return false;
5328
5329 /* If there is a .dwz file, read it so we can get its CU list as
5330 well. */
5331 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5332 if (dwz != NULL)
5333 {
5334 if (!read_debug_names_from_section (objfile,
5335 bfd_get_filename (dwz->dwz_bfd.get ()),
5336 &dwz->debug_names, dwz_map))
5337 {
5338 warning (_("could not read '.debug_names' section from %s; skipping"),
5339 bfd_get_filename (dwz->dwz_bfd.get ()));
5340 return false;
5341 }
5342 }
5343
5344 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5345
5346 if (map->tu_count != 0)
5347 {
5348 /* We can only handle a single .debug_types when we have an
5349 index. */
5350 if (per_bfd->types.size () != 1)
5351 return false;
5352
5353 dwarf2_section_info *section = &per_bfd->types[0];
5354
5355 create_signatured_type_table_from_debug_names
5356 (per_objfile, *map, section, &per_bfd->abbrev);
5357 }
5358
5359 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5360
5361 per_bfd->debug_names_table = std::move (map);
5362 per_bfd->using_index = 1;
5363 per_bfd->quick_file_names_table =
5364 create_quick_file_names_table (per_bfd->all_comp_units.size ());
5365
5366 return true;
5367 }
5368
5369 /* Type used to manage iterating over all CUs looking for a symbol for
5370 .debug_names. */
5371
5372 class dw2_debug_names_iterator
5373 {
5374 public:
5375 dw2_debug_names_iterator (const mapped_debug_names &map,
5376 gdb::optional<block_enum> block_index,
5377 domain_enum domain,
5378 const char *name, 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, name, per_objfile)),
5381 m_per_objfile (per_objfile)
5382 {}
5383
5384 dw2_debug_names_iterator (const mapped_debug_names &map,
5385 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5386 : m_map (map),
5387 m_search (search),
5388 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5389 m_per_objfile (per_objfile)
5390 {}
5391
5392 dw2_debug_names_iterator (const mapped_debug_names &map,
5393 block_enum block_index, domain_enum domain,
5394 uint32_t namei, dwarf2_per_objfile *per_objfile)
5395 : m_map (map), m_block_index (block_index), m_domain (domain),
5396 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5397 m_per_objfile (per_objfile)
5398 {}
5399
5400 /* Return the next matching CU or NULL if there are no more. */
5401 dwarf2_per_cu_data *next ();
5402
5403 private:
5404 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5405 const char *name,
5406 dwarf2_per_objfile *per_objfile);
5407 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5408 uint32_t namei,
5409 dwarf2_per_objfile *per_objfile);
5410
5411 /* The internalized form of .debug_names. */
5412 const mapped_debug_names &m_map;
5413
5414 /* If set, only look for symbols that match that block. Valid values are
5415 GLOBAL_BLOCK and STATIC_BLOCK. */
5416 const gdb::optional<block_enum> m_block_index;
5417
5418 /* The kind of symbol we're looking for. */
5419 const domain_enum m_domain = UNDEF_DOMAIN;
5420 const search_domain m_search = ALL_DOMAIN;
5421
5422 /* The list of CUs from the index entry of the symbol, or NULL if
5423 not found. */
5424 const gdb_byte *m_addr;
5425
5426 dwarf2_per_objfile *m_per_objfile;
5427 };
5428
5429 const char *
5430 mapped_debug_names::namei_to_name
5431 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5432 {
5433 const ULONGEST namei_string_offs
5434 = extract_unsigned_integer ((name_table_string_offs_reordered
5435 + namei * offset_size),
5436 offset_size,
5437 dwarf5_byte_order);
5438 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5439 }
5440
5441 /* Find a slot in .debug_names for the object named NAME. If NAME is
5442 found, return pointer to its pool data. If NAME cannot be found,
5443 return NULL. */
5444
5445 const gdb_byte *
5446 dw2_debug_names_iterator::find_vec_in_debug_names
5447 (const mapped_debug_names &map, const char *name,
5448 dwarf2_per_objfile *per_objfile)
5449 {
5450 int (*cmp) (const char *, const char *);
5451
5452 gdb::unique_xmalloc_ptr<char> without_params;
5453 if (current_language->la_language == language_cplus
5454 || current_language->la_language == language_fortran
5455 || current_language->la_language == language_d)
5456 {
5457 /* NAME is already canonical. Drop any qualifiers as
5458 .debug_names does not contain any. */
5459
5460 if (strchr (name, '(') != NULL)
5461 {
5462 without_params = cp_remove_params (name);
5463 if (without_params != NULL)
5464 name = without_params.get ();
5465 }
5466 }
5467
5468 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5469
5470 const uint32_t full_hash = dwarf5_djb_hash (name);
5471 uint32_t namei
5472 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5473 (map.bucket_table_reordered
5474 + (full_hash % map.bucket_count)), 4,
5475 map.dwarf5_byte_order);
5476 if (namei == 0)
5477 return NULL;
5478 --namei;
5479 if (namei >= map.name_count)
5480 {
5481 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5482 "[in module %s]"),
5483 namei, map.name_count,
5484 objfile_name (per_objfile->objfile));
5485 return NULL;
5486 }
5487
5488 for (;;)
5489 {
5490 const uint32_t namei_full_hash
5491 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5492 (map.hash_table_reordered + namei), 4,
5493 map.dwarf5_byte_order);
5494 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5495 return NULL;
5496
5497 if (full_hash == namei_full_hash)
5498 {
5499 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5500
5501 #if 0 /* An expensive sanity check. */
5502 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5503 {
5504 complaint (_("Wrong .debug_names hash for string at index %u "
5505 "[in module %s]"),
5506 namei, objfile_name (dwarf2_per_objfile->objfile));
5507 return NULL;
5508 }
5509 #endif
5510
5511 if (cmp (namei_string, name) == 0)
5512 {
5513 const ULONGEST namei_entry_offs
5514 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5515 + namei * map.offset_size),
5516 map.offset_size, map.dwarf5_byte_order);
5517 return map.entry_pool + namei_entry_offs;
5518 }
5519 }
5520
5521 ++namei;
5522 if (namei >= map.name_count)
5523 return NULL;
5524 }
5525 }
5526
5527 const gdb_byte *
5528 dw2_debug_names_iterator::find_vec_in_debug_names
5529 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5530 {
5531 if (namei >= map.name_count)
5532 {
5533 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5534 "[in module %s]"),
5535 namei, map.name_count,
5536 objfile_name (per_objfile->objfile));
5537 return NULL;
5538 }
5539
5540 const ULONGEST namei_entry_offs
5541 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5542 + namei * map.offset_size),
5543 map.offset_size, map.dwarf5_byte_order);
5544 return map.entry_pool + namei_entry_offs;
5545 }
5546
5547 /* See dw2_debug_names_iterator. */
5548
5549 dwarf2_per_cu_data *
5550 dw2_debug_names_iterator::next ()
5551 {
5552 if (m_addr == NULL)
5553 return NULL;
5554
5555 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5556 struct objfile *objfile = m_per_objfile->objfile;
5557 bfd *const abfd = objfile->obfd;
5558
5559 again:
5560
5561 unsigned int bytes_read;
5562 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5563 m_addr += bytes_read;
5564 if (abbrev == 0)
5565 return NULL;
5566
5567 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5568 if (indexval_it == m_map.abbrev_map.cend ())
5569 {
5570 complaint (_("Wrong .debug_names undefined abbrev code %s "
5571 "[in module %s]"),
5572 pulongest (abbrev), objfile_name (objfile));
5573 return NULL;
5574 }
5575 const mapped_debug_names::index_val &indexval = indexval_it->second;
5576 enum class symbol_linkage {
5577 unknown,
5578 static_,
5579 extern_,
5580 } symbol_linkage_ = symbol_linkage::unknown;
5581 dwarf2_per_cu_data *per_cu = NULL;
5582 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5583 {
5584 ULONGEST ull;
5585 switch (attr.form)
5586 {
5587 case DW_FORM_implicit_const:
5588 ull = attr.implicit_const;
5589 break;
5590 case DW_FORM_flag_present:
5591 ull = 1;
5592 break;
5593 case DW_FORM_udata:
5594 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5595 m_addr += bytes_read;
5596 break;
5597 case DW_FORM_ref4:
5598 ull = read_4_bytes (abfd, m_addr);
5599 m_addr += 4;
5600 break;
5601 case DW_FORM_ref8:
5602 ull = read_8_bytes (abfd, m_addr);
5603 m_addr += 8;
5604 break;
5605 case DW_FORM_ref_sig8:
5606 ull = read_8_bytes (abfd, m_addr);
5607 m_addr += 8;
5608 break;
5609 default:
5610 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5611 dwarf_form_name (attr.form),
5612 objfile_name (objfile));
5613 return NULL;
5614 }
5615 switch (attr.dw_idx)
5616 {
5617 case DW_IDX_compile_unit:
5618 /* Don't crash on bad data. */
5619 if (ull >= per_bfd->all_comp_units.size ())
5620 {
5621 complaint (_(".debug_names entry has bad CU index %s"
5622 " [in module %s]"),
5623 pulongest (ull),
5624 objfile_name (objfile));
5625 continue;
5626 }
5627 per_cu = per_bfd->get_cutu (ull);
5628 break;
5629 case DW_IDX_type_unit:
5630 /* Don't crash on bad data. */
5631 if (ull >= per_bfd->all_type_units.size ())
5632 {
5633 complaint (_(".debug_names entry has bad TU index %s"
5634 " [in module %s]"),
5635 pulongest (ull),
5636 objfile_name (objfile));
5637 continue;
5638 }
5639 per_cu = &per_bfd->get_tu (ull)->per_cu;
5640 break;
5641 case DW_IDX_die_offset:
5642 /* In a per-CU index (as opposed to a per-module index), index
5643 entries without CU attribute implicitly refer to the single CU. */
5644 if (per_cu == NULL)
5645 per_cu = per_bfd->get_cu (0);
5646 break;
5647 case DW_IDX_GNU_internal:
5648 if (!m_map.augmentation_is_gdb)
5649 break;
5650 symbol_linkage_ = symbol_linkage::static_;
5651 break;
5652 case DW_IDX_GNU_external:
5653 if (!m_map.augmentation_is_gdb)
5654 break;
5655 symbol_linkage_ = symbol_linkage::extern_;
5656 break;
5657 }
5658 }
5659
5660 /* Skip if already read in. */
5661 if (m_per_objfile->symtab_set_p (per_cu))
5662 goto again;
5663
5664 /* Check static vs global. */
5665 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5666 {
5667 const bool want_static = *m_block_index == STATIC_BLOCK;
5668 const bool symbol_is_static =
5669 symbol_linkage_ == symbol_linkage::static_;
5670 if (want_static != symbol_is_static)
5671 goto again;
5672 }
5673
5674 /* Match dw2_symtab_iter_next, symbol_kind
5675 and debug_names::psymbol_tag. */
5676 switch (m_domain)
5677 {
5678 case VAR_DOMAIN:
5679 switch (indexval.dwarf_tag)
5680 {
5681 case DW_TAG_variable:
5682 case DW_TAG_subprogram:
5683 /* Some types are also in VAR_DOMAIN. */
5684 case DW_TAG_typedef:
5685 case DW_TAG_structure_type:
5686 break;
5687 default:
5688 goto again;
5689 }
5690 break;
5691 case STRUCT_DOMAIN:
5692 switch (indexval.dwarf_tag)
5693 {
5694 case DW_TAG_typedef:
5695 case DW_TAG_structure_type:
5696 break;
5697 default:
5698 goto again;
5699 }
5700 break;
5701 case LABEL_DOMAIN:
5702 switch (indexval.dwarf_tag)
5703 {
5704 case 0:
5705 case DW_TAG_variable:
5706 break;
5707 default:
5708 goto again;
5709 }
5710 break;
5711 case MODULE_DOMAIN:
5712 switch (indexval.dwarf_tag)
5713 {
5714 case DW_TAG_module:
5715 break;
5716 default:
5717 goto again;
5718 }
5719 break;
5720 default:
5721 break;
5722 }
5723
5724 /* Match dw2_expand_symtabs_matching, symbol_kind and
5725 debug_names::psymbol_tag. */
5726 switch (m_search)
5727 {
5728 case VARIABLES_DOMAIN:
5729 switch (indexval.dwarf_tag)
5730 {
5731 case DW_TAG_variable:
5732 break;
5733 default:
5734 goto again;
5735 }
5736 break;
5737 case FUNCTIONS_DOMAIN:
5738 switch (indexval.dwarf_tag)
5739 {
5740 case DW_TAG_subprogram:
5741 break;
5742 default:
5743 goto again;
5744 }
5745 break;
5746 case TYPES_DOMAIN:
5747 switch (indexval.dwarf_tag)
5748 {
5749 case DW_TAG_typedef:
5750 case DW_TAG_structure_type:
5751 break;
5752 default:
5753 goto again;
5754 }
5755 break;
5756 case MODULES_DOMAIN:
5757 switch (indexval.dwarf_tag)
5758 {
5759 case DW_TAG_module:
5760 break;
5761 default:
5762 goto again;
5763 }
5764 default:
5765 break;
5766 }
5767
5768 return per_cu;
5769 }
5770
5771 struct compunit_symtab *
5772 dwarf2_debug_names_index::lookup_symbol
5773 (struct objfile *objfile, block_enum block_index,
5774 const char *name, domain_enum domain)
5775 {
5776 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5777
5778 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5779 if (!mapp)
5780 {
5781 /* index is NULL if OBJF_READNOW. */
5782 return NULL;
5783 }
5784 const auto &map = *mapp;
5785
5786 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5787
5788 struct compunit_symtab *stab_best = NULL;
5789 struct dwarf2_per_cu_data *per_cu;
5790 while ((per_cu = iter.next ()) != NULL)
5791 {
5792 struct symbol *sym, *with_opaque = NULL;
5793 compunit_symtab *stab
5794 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5795 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5796 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5797
5798 sym = block_find_symbol (block, name, domain,
5799 block_find_non_opaque_type_preferred,
5800 &with_opaque);
5801
5802 /* Some caution must be observed with overloaded functions and
5803 methods, since the index will not contain any overload
5804 information (but NAME might contain it). */
5805
5806 if (sym != NULL
5807 && strcmp_iw (sym->search_name (), name) == 0)
5808 return stab;
5809 if (with_opaque != NULL
5810 && strcmp_iw (with_opaque->search_name (), name) == 0)
5811 stab_best = stab;
5812
5813 /* Keep looking through other CUs. */
5814 }
5815
5816 return stab_best;
5817 }
5818
5819 /* This dumps minimal information about .debug_names. It is called
5820 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5821 uses this to verify that .debug_names has been loaded. */
5822
5823 void
5824 dwarf2_debug_names_index::dump (struct objfile *objfile)
5825 {
5826 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5827
5828 gdb_assert (per_objfile->per_bfd->using_index);
5829 printf_filtered (".debug_names:");
5830 if (per_objfile->per_bfd->debug_names_table)
5831 printf_filtered (" exists\n");
5832 else
5833 printf_filtered (" faked for \"readnow\"\n");
5834 printf_filtered ("\n");
5835 }
5836
5837 void
5838 dwarf2_debug_names_index::expand_symtabs_for_function
5839 (struct objfile *objfile, const char *func_name)
5840 {
5841 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5842
5843 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5844 if (per_objfile->per_bfd->debug_names_table)
5845 {
5846 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5847
5848 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5849 per_objfile);
5850
5851 struct dwarf2_per_cu_data *per_cu;
5852 while ((per_cu = iter.next ()) != NULL)
5853 dw2_instantiate_symtab (per_cu, per_objfile, false);
5854 }
5855 }
5856
5857 void
5858 dwarf2_debug_names_index::map_matching_symbols
5859 (struct objfile *objfile,
5860 const lookup_name_info &name, domain_enum domain,
5861 int global,
5862 gdb::function_view<symbol_found_callback_ftype> callback,
5863 symbol_compare_ftype *ordered_compare)
5864 {
5865 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5866
5867 /* debug_names_table is NULL if OBJF_READNOW. */
5868 if (!per_objfile->per_bfd->debug_names_table)
5869 return;
5870
5871 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5872 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5873
5874 const char *match_name = name.ada ().lookup_name ().c_str ();
5875 auto matcher = [&] (const char *symname)
5876 {
5877 if (ordered_compare == nullptr)
5878 return true;
5879 return ordered_compare (symname, match_name) == 0;
5880 };
5881
5882 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5883 [&] (offset_type namei)
5884 {
5885 /* The name was matched, now expand corresponding CUs that were
5886 marked. */
5887 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5888 per_objfile);
5889
5890 struct dwarf2_per_cu_data *per_cu;
5891 while ((per_cu = iter.next ()) != NULL)
5892 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5893 nullptr);
5894 return true;
5895 }, per_objfile);
5896
5897 /* It's a shame we couldn't do this inside the
5898 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5899 that have already been expanded. Instead, this loop matches what
5900 the psymtab code does. */
5901 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5902 {
5903 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5904 if (symtab != nullptr)
5905 {
5906 const struct block *block
5907 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5908 if (!iterate_over_symbols_terminated (block, name,
5909 domain, callback))
5910 break;
5911 }
5912 }
5913 }
5914
5915 bool
5916 dwarf2_debug_names_index::expand_symtabs_matching
5917 (struct objfile *objfile,
5918 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5919 const lookup_name_info *lookup_name,
5920 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5921 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5922 enum search_domain kind)
5923 {
5924 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5925
5926 /* debug_names_table is NULL if OBJF_READNOW. */
5927 if (!per_objfile->per_bfd->debug_names_table)
5928 return true;
5929
5930 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5931
5932 if (symbol_matcher == NULL && lookup_name == NULL)
5933 {
5934 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5935 {
5936 QUIT;
5937
5938 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5939 file_matcher,
5940 expansion_notify))
5941 return false;
5942 }
5943 return true;
5944 }
5945
5946 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5947
5948 bool result
5949 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5950 symbol_matcher,
5951 [&] (offset_type namei)
5952 {
5953 /* The name was matched, now expand corresponding CUs that were
5954 marked. */
5955 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5956
5957 struct dwarf2_per_cu_data *per_cu;
5958 while ((per_cu = iter.next ()) != NULL)
5959 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5960 file_matcher,
5961 expansion_notify))
5962 return false;
5963 return true;
5964 }, per_objfile);
5965
5966 return result;
5967 }
5968
5969 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5970 to either a dwarf2_per_bfd or dwz_file object. */
5971
5972 template <typename T>
5973 static gdb::array_view<const gdb_byte>
5974 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5975 {
5976 dwarf2_section_info *section = &section_owner->gdb_index;
5977
5978 if (section->empty ())
5979 return {};
5980
5981 /* Older elfutils strip versions could keep the section in the main
5982 executable while splitting it for the separate debug info file. */
5983 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5984 return {};
5985
5986 section->read (obj);
5987
5988 /* dwarf2_section_info::size is a bfd_size_type, while
5989 gdb::array_view works with size_t. On 32-bit hosts, with
5990 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5991 is 32-bit. So we need an explicit narrowing conversion here.
5992 This is fine, because it's impossible to allocate or mmap an
5993 array/buffer larger than what size_t can represent. */
5994 return gdb::make_array_view (section->buffer, section->size);
5995 }
5996
5997 /* Lookup the index cache for the contents of the index associated to
5998 DWARF2_OBJ. */
5999
6000 static gdb::array_view<const gdb_byte>
6001 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
6002 {
6003 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6004 if (build_id == nullptr)
6005 return {};
6006
6007 return global_index_cache.lookup_gdb_index (build_id,
6008 &dwarf2_per_bfd->index_cache_res);
6009 }
6010
6011 /* Same as the above, but for DWZ. */
6012
6013 static gdb::array_view<const gdb_byte>
6014 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6015 {
6016 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6017 if (build_id == nullptr)
6018 return {};
6019
6020 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6021 }
6022
6023 /* See dwarf2/public.h. */
6024
6025 void
6026 dwarf2_initialize_objfile (struct objfile *objfile)
6027 {
6028 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6029 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6030
6031 dwarf_read_debug_printf ("called");
6032
6033 /* If we're about to read full symbols, don't bother with the
6034 indices. In this case we also don't care if some other debug
6035 format is making psymtabs, because they are all about to be
6036 expanded anyway. */
6037 if ((objfile->flags & OBJF_READNOW))
6038 {
6039 dwarf_read_debug_printf ("readnow requested");
6040
6041 /* When using READNOW, the using_index flag (set below) indicates that
6042 PER_BFD was already initialized, when we loaded some other objfile. */
6043 if (per_bfd->using_index)
6044 {
6045 dwarf_read_debug_printf ("using_index already set");
6046 per_objfile->resize_symtabs ();
6047 objfile->qf.push_front (make_dwarf_gdb_index ());
6048 return;
6049 }
6050
6051 per_bfd->using_index = 1;
6052 create_all_comp_units (per_objfile);
6053 create_all_type_units (per_objfile);
6054 per_bfd->quick_file_names_table
6055 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6056 per_objfile->resize_symtabs ();
6057
6058 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6059 + per_bfd->all_type_units.size ()); ++i)
6060 {
6061 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6062
6063 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6064 struct dwarf2_per_cu_quick_data);
6065 }
6066
6067 /* Arrange for gdb to see the "quick" functions. However, these
6068 functions will be no-ops because we will have expanded all
6069 symtabs. */
6070 objfile->qf.push_front (make_dwarf_gdb_index ());
6071 return;
6072 }
6073
6074 /* Was a debug names index already read when we processed an objfile sharing
6075 PER_BFD? */
6076 if (per_bfd->debug_names_table != nullptr)
6077 {
6078 dwarf_read_debug_printf ("re-using shared debug names table");
6079 per_objfile->resize_symtabs ();
6080 objfile->qf.push_front (make_dwarf_debug_names ());
6081 return;
6082 }
6083
6084 /* Was a GDB index already read when we processed an objfile sharing
6085 PER_BFD? */
6086 if (per_bfd->index_table != nullptr)
6087 {
6088 dwarf_read_debug_printf ("re-using shared index table");
6089 per_objfile->resize_symtabs ();
6090 objfile->qf.push_front (make_dwarf_gdb_index ());
6091 return;
6092 }
6093
6094 /* There might already be partial symtabs built for this BFD. This happens
6095 when loading the same binary twice with the index-cache enabled. If so,
6096 don't try to read an index. The objfile / per_objfile initialization will
6097 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6098 code path. */
6099 if (per_bfd->partial_symtabs != nullptr)
6100 {
6101 dwarf_read_debug_printf ("re-using shared partial symtabs");
6102 objfile->qf.push_front (make_lazy_dwarf_reader ());
6103 return;
6104 }
6105
6106 if (dwarf2_read_debug_names (per_objfile))
6107 {
6108 dwarf_read_debug_printf ("found debug names");
6109 per_objfile->resize_symtabs ();
6110 objfile->qf.push_front (make_dwarf_debug_names ());
6111 return;
6112 }
6113
6114 if (dwarf2_read_gdb_index (per_objfile,
6115 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6116 get_gdb_index_contents_from_section<dwz_file>))
6117 {
6118 dwarf_read_debug_printf ("found gdb index from file");
6119 per_objfile->resize_symtabs ();
6120 objfile->qf.push_front (make_dwarf_gdb_index ());
6121 return;
6122 }
6123
6124 /* ... otherwise, try to find the index in the index cache. */
6125 if (dwarf2_read_gdb_index (per_objfile,
6126 get_gdb_index_contents_from_cache,
6127 get_gdb_index_contents_from_cache_dwz))
6128 {
6129 dwarf_read_debug_printf ("found gdb index from cache");
6130 global_index_cache.hit ();
6131 per_objfile->resize_symtabs ();
6132 objfile->qf.push_front (make_dwarf_gdb_index ());
6133 return;
6134 }
6135
6136 global_index_cache.miss ();
6137 objfile->qf.push_front (make_lazy_dwarf_reader ());
6138 }
6139
6140 \f
6141
6142 /* Build a partial symbol table. */
6143
6144 void
6145 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
6146 {
6147 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6148 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6149
6150 if (per_bfd->partial_symtabs != nullptr)
6151 {
6152 /* Partial symbols were already read, so now we can simply
6153 attach them. */
6154 if (psf == nullptr)
6155 {
6156 psf = new psymbol_functions (per_bfd->partial_symtabs);
6157 objfile->qf.emplace_front (psf);
6158 }
6159 else
6160 psf->set_partial_symtabs (per_bfd->partial_symtabs);
6161 per_objfile->resize_symtabs ();
6162 return;
6163 }
6164
6165 if (psf == nullptr)
6166 {
6167 psf = new psymbol_functions;
6168 objfile->qf.emplace_front (psf);
6169 }
6170 const std::shared_ptr<psymtab_storage> &partial_symtabs
6171 = psf->get_partial_symtabs ();
6172
6173 /* Set the local reference to partial symtabs, so that we don't try
6174 to read them again if reading another objfile with the same BFD.
6175 If we can't in fact share, this won't make a difference anyway as
6176 the dwarf2_per_bfd object won't be shared. */
6177 per_bfd->partial_symtabs = partial_symtabs;
6178
6179 try
6180 {
6181 /* This isn't really ideal: all the data we allocate on the
6182 objfile's obstack is still uselessly kept around. However,
6183 freeing it seems unsafe. */
6184 psymtab_discarder psymtabs (partial_symtabs.get ());
6185 dwarf2_build_psymtabs_hard (per_objfile);
6186 psymtabs.keep ();
6187
6188 per_objfile->resize_symtabs ();
6189
6190 /* (maybe) store an index in the cache. */
6191 global_index_cache.store (per_objfile);
6192 }
6193 catch (const gdb_exception_error &except)
6194 {
6195 exception_print (gdb_stderr, except);
6196 }
6197 }
6198
6199 /* Find the base address of the compilation unit for range lists and
6200 location lists. It will normally be specified by DW_AT_low_pc.
6201 In DWARF-3 draft 4, the base address could be overridden by
6202 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6203 compilation units with discontinuous ranges. */
6204
6205 static void
6206 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6207 {
6208 struct attribute *attr;
6209
6210 cu->base_address.reset ();
6211
6212 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6213 if (attr != nullptr)
6214 cu->base_address = attr->as_address ();
6215 else
6216 {
6217 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6218 if (attr != nullptr)
6219 cu->base_address = attr->as_address ();
6220 }
6221 }
6222
6223 /* Helper function that returns the proper abbrev section for
6224 THIS_CU. */
6225
6226 static struct dwarf2_section_info *
6227 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6228 {
6229 struct dwarf2_section_info *abbrev;
6230 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6231
6232 if (this_cu->is_dwz)
6233 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
6234 else
6235 abbrev = &per_bfd->abbrev;
6236
6237 return abbrev;
6238 }
6239
6240 /* Fetch the abbreviation table offset from a comp or type unit header. */
6241
6242 static sect_offset
6243 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6244 struct dwarf2_section_info *section,
6245 sect_offset sect_off)
6246 {
6247 bfd *abfd = section->get_bfd_owner ();
6248 const gdb_byte *info_ptr;
6249 unsigned int initial_length_size, offset_size;
6250 uint16_t version;
6251
6252 section->read (per_objfile->objfile);
6253 info_ptr = section->buffer + to_underlying (sect_off);
6254 read_initial_length (abfd, info_ptr, &initial_length_size);
6255 offset_size = initial_length_size == 4 ? 4 : 8;
6256 info_ptr += initial_length_size;
6257
6258 version = read_2_bytes (abfd, info_ptr);
6259 info_ptr += 2;
6260 if (version >= 5)
6261 {
6262 /* Skip unit type and address size. */
6263 info_ptr += 2;
6264 }
6265
6266 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6267 }
6268
6269 /* A partial symtab that is used only for include files. */
6270 struct dwarf2_include_psymtab : public partial_symtab
6271 {
6272 dwarf2_include_psymtab (const char *filename,
6273 psymtab_storage *partial_symtabs,
6274 objfile_per_bfd_storage *objfile_per_bfd)
6275 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
6276 {
6277 }
6278
6279 void read_symtab (struct objfile *objfile) override
6280 {
6281 /* It's an include file, no symbols to read for it.
6282 Everything is in the includer symtab. */
6283
6284 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6285 expansion of the includer psymtab. We use the dependencies[0] field to
6286 model the includer. But if we go the regular route of calling
6287 expand_psymtab here, and having expand_psymtab call expand_dependencies
6288 to expand the includer, we'll only use expand_psymtab on the includer
6289 (making it a non-toplevel psymtab), while if we expand the includer via
6290 another path, we'll use read_symtab (making it a toplevel psymtab).
6291 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6292 psymtab, and trigger read_symtab on the includer here directly. */
6293 includer ()->read_symtab (objfile);
6294 }
6295
6296 void expand_psymtab (struct objfile *objfile) override
6297 {
6298 /* This is not called by read_symtab, and should not be called by any
6299 expand_dependencies. */
6300 gdb_assert (false);
6301 }
6302
6303 bool readin_p (struct objfile *objfile) const override
6304 {
6305 return includer ()->readin_p (objfile);
6306 }
6307
6308 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6309 {
6310 return nullptr;
6311 }
6312
6313 private:
6314 partial_symtab *includer () const
6315 {
6316 /* An include psymtab has exactly one dependency: the psymtab that
6317 includes it. */
6318 gdb_assert (this->number_of_dependencies == 1);
6319 return this->dependencies[0];
6320 }
6321 };
6322
6323 /* Allocate a new partial symtab for file named NAME and mark this new
6324 partial symtab as being an include of PST. */
6325
6326 static void
6327 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
6328 const char *name,
6329 dwarf2_psymtab *pst,
6330 psymtab_storage *partial_symtabs,
6331 objfile_per_bfd_storage *objfile_per_bfd)
6332 {
6333 dwarf2_include_psymtab *subpst
6334 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
6335
6336 if (!IS_ABSOLUTE_PATH (subpst->filename))
6337 subpst->dirname = pst->dirname;
6338
6339 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
6340 subpst->dependencies[0] = pst;
6341 subpst->number_of_dependencies = 1;
6342 }
6343
6344 /* Read the Line Number Program data and extract the list of files
6345 included by the source file represented by PST. Build an include
6346 partial symtab for each of these included files. */
6347
6348 static void
6349 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6350 struct die_info *die,
6351 dwarf2_psymtab *pst)
6352 {
6353 line_header_up lh;
6354 struct attribute *attr;
6355
6356 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6357 if (attr != nullptr && attr->form_is_unsigned ())
6358 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6359 if (lh == NULL)
6360 return; /* No linetable, so no includes. */
6361
6362 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6363 that we pass in the raw text_low here; that is ok because we're
6364 only decoding the line table to make include partial symtabs, and
6365 so the addresses aren't really used. */
6366 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6367 pst->raw_text_low (), 1);
6368 }
6369
6370 static hashval_t
6371 hash_signatured_type (const void *item)
6372 {
6373 const struct signatured_type *sig_type
6374 = (const struct signatured_type *) item;
6375
6376 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6377 return sig_type->signature;
6378 }
6379
6380 static int
6381 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6382 {
6383 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6384 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6385
6386 return lhs->signature == rhs->signature;
6387 }
6388
6389 /* Allocate a hash table for signatured types. */
6390
6391 static htab_up
6392 allocate_signatured_type_table ()
6393 {
6394 return htab_up (htab_create_alloc (41,
6395 hash_signatured_type,
6396 eq_signatured_type,
6397 NULL, xcalloc, xfree));
6398 }
6399
6400 /* A helper function to add a signatured type CU to a table. */
6401
6402 static int
6403 add_signatured_type_cu_to_table (void **slot, void *datum)
6404 {
6405 struct signatured_type *sigt = (struct signatured_type *) *slot;
6406 std::vector<signatured_type *> *all_type_units
6407 = (std::vector<signatured_type *> *) datum;
6408
6409 all_type_units->push_back (sigt);
6410
6411 return 1;
6412 }
6413
6414 /* A helper for create_debug_types_hash_table. Read types from SECTION
6415 and fill them into TYPES_HTAB. It will process only type units,
6416 therefore DW_UT_type. */
6417
6418 static void
6419 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6420 struct dwo_file *dwo_file,
6421 dwarf2_section_info *section, htab_up &types_htab,
6422 rcuh_kind section_kind)
6423 {
6424 struct objfile *objfile = per_objfile->objfile;
6425 struct dwarf2_section_info *abbrev_section;
6426 bfd *abfd;
6427 const gdb_byte *info_ptr, *end_ptr;
6428
6429 abbrev_section = (dwo_file != NULL
6430 ? &dwo_file->sections.abbrev
6431 : &per_objfile->per_bfd->abbrev);
6432
6433 dwarf_read_debug_printf ("Reading %s for %s",
6434 section->get_name (),
6435 abbrev_section->get_file_name ());
6436
6437 section->read (objfile);
6438 info_ptr = section->buffer;
6439
6440 if (info_ptr == NULL)
6441 return;
6442
6443 /* We can't set abfd until now because the section may be empty or
6444 not present, in which case the bfd is unknown. */
6445 abfd = section->get_bfd_owner ();
6446
6447 /* We don't use cutu_reader here because we don't need to read
6448 any dies: the signature is in the header. */
6449
6450 end_ptr = info_ptr + section->size;
6451 while (info_ptr < end_ptr)
6452 {
6453 struct signatured_type *sig_type;
6454 struct dwo_unit *dwo_tu;
6455 void **slot;
6456 const gdb_byte *ptr = info_ptr;
6457 struct comp_unit_head header;
6458 unsigned int length;
6459
6460 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6461
6462 /* Initialize it due to a false compiler warning. */
6463 header.signature = -1;
6464 header.type_cu_offset_in_tu = (cu_offset) -1;
6465
6466 /* We need to read the type's signature in order to build the hash
6467 table, but we don't need anything else just yet. */
6468
6469 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6470 abbrev_section, ptr, section_kind);
6471
6472 length = header.get_length ();
6473
6474 /* Skip dummy type units. */
6475 if (ptr >= info_ptr + length
6476 || peek_abbrev_code (abfd, ptr) == 0
6477 || (header.unit_type != DW_UT_type
6478 && header.unit_type != DW_UT_split_type))
6479 {
6480 info_ptr += length;
6481 continue;
6482 }
6483
6484 if (types_htab == NULL)
6485 {
6486 if (dwo_file)
6487 types_htab = allocate_dwo_unit_table ();
6488 else
6489 types_htab = allocate_signatured_type_table ();
6490 }
6491
6492 if (dwo_file)
6493 {
6494 sig_type = NULL;
6495 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6496 dwo_tu->dwo_file = dwo_file;
6497 dwo_tu->signature = header.signature;
6498 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6499 dwo_tu->section = section;
6500 dwo_tu->sect_off = sect_off;
6501 dwo_tu->length = length;
6502 }
6503 else
6504 {
6505 /* N.B.: type_offset is not usable if this type uses a DWO file.
6506 The real type_offset is in the DWO file. */
6507 dwo_tu = NULL;
6508 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6509 sig_type->signature = header.signature;
6510 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6511 sig_type->per_cu.is_debug_types = 1;
6512 sig_type->per_cu.section = section;
6513 sig_type->per_cu.sect_off = sect_off;
6514 sig_type->per_cu.length = length;
6515 }
6516
6517 slot = htab_find_slot (types_htab.get (),
6518 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6519 INSERT);
6520 gdb_assert (slot != NULL);
6521 if (*slot != NULL)
6522 {
6523 sect_offset dup_sect_off;
6524
6525 if (dwo_file)
6526 {
6527 const struct dwo_unit *dup_tu
6528 = (const struct dwo_unit *) *slot;
6529
6530 dup_sect_off = dup_tu->sect_off;
6531 }
6532 else
6533 {
6534 const struct signatured_type *dup_tu
6535 = (const struct signatured_type *) *slot;
6536
6537 dup_sect_off = dup_tu->per_cu.sect_off;
6538 }
6539
6540 complaint (_("debug type entry at offset %s is duplicate to"
6541 " the entry at offset %s, signature %s"),
6542 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6543 hex_string (header.signature));
6544 }
6545 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6546
6547 dwarf_read_debug_printf_v (" offset %s, signature %s",
6548 sect_offset_str (sect_off),
6549 hex_string (header.signature));
6550
6551 info_ptr += length;
6552 }
6553 }
6554
6555 /* Create the hash table of all entries in the .debug_types
6556 (or .debug_types.dwo) section(s).
6557 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6558 otherwise it is NULL.
6559
6560 The result is a pointer to the hash table or NULL if there are no types.
6561
6562 Note: This function processes DWO files only, not DWP files. */
6563
6564 static void
6565 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6566 struct dwo_file *dwo_file,
6567 gdb::array_view<dwarf2_section_info> type_sections,
6568 htab_up &types_htab)
6569 {
6570 for (dwarf2_section_info &section : type_sections)
6571 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6572 rcuh_kind::TYPE);
6573 }
6574
6575 /* Create the hash table of all entries in the .debug_types section,
6576 and initialize all_type_units.
6577 The result is zero if there is an error (e.g. missing .debug_types section),
6578 otherwise non-zero. */
6579
6580 static int
6581 create_all_type_units (dwarf2_per_objfile *per_objfile)
6582 {
6583 htab_up types_htab;
6584
6585 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6586 types_htab, rcuh_kind::COMPILE);
6587 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6588 types_htab);
6589 if (types_htab == NULL)
6590 {
6591 per_objfile->per_bfd->signatured_types = NULL;
6592 return 0;
6593 }
6594
6595 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6596
6597 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6598 per_objfile->per_bfd->all_type_units.reserve
6599 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6600
6601 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6602 add_signatured_type_cu_to_table,
6603 &per_objfile->per_bfd->all_type_units);
6604
6605 return 1;
6606 }
6607
6608 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6609 If SLOT is non-NULL, it is the entry to use in the hash table.
6610 Otherwise we find one. */
6611
6612 static struct signatured_type *
6613 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6614 {
6615 if (per_objfile->per_bfd->all_type_units.size ()
6616 == per_objfile->per_bfd->all_type_units.capacity ())
6617 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6618
6619 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6620
6621 per_objfile->resize_symtabs ();
6622
6623 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6624 sig_type->signature = sig;
6625 sig_type->per_cu.is_debug_types = 1;
6626 if (per_objfile->per_bfd->using_index)
6627 {
6628 sig_type->per_cu.v.quick =
6629 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6630 struct dwarf2_per_cu_quick_data);
6631 }
6632
6633 if (slot == NULL)
6634 {
6635 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6636 sig_type, INSERT);
6637 }
6638 gdb_assert (*slot == NULL);
6639 *slot = sig_type;
6640 /* The rest of sig_type must be filled in by the caller. */
6641 return sig_type;
6642 }
6643
6644 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6645 Fill in SIG_ENTRY with DWO_ENTRY. */
6646
6647 static void
6648 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6649 struct signatured_type *sig_entry,
6650 struct dwo_unit *dwo_entry)
6651 {
6652 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6653
6654 /* Make sure we're not clobbering something we don't expect to. */
6655 gdb_assert (! sig_entry->per_cu.queued);
6656 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6657 if (per_bfd->using_index)
6658 {
6659 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6660 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6661 }
6662 else
6663 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6664 gdb_assert (sig_entry->signature == dwo_entry->signature);
6665 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6666 gdb_assert (sig_entry->type_unit_group == NULL);
6667 gdb_assert (sig_entry->dwo_unit == NULL);
6668
6669 sig_entry->per_cu.section = dwo_entry->section;
6670 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6671 sig_entry->per_cu.length = dwo_entry->length;
6672 sig_entry->per_cu.reading_dwo_directly = 1;
6673 sig_entry->per_cu.per_bfd = per_bfd;
6674 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6675 sig_entry->dwo_unit = dwo_entry;
6676 }
6677
6678 /* Subroutine of lookup_signatured_type.
6679 If we haven't read the TU yet, create the signatured_type data structure
6680 for a TU to be read in directly from a DWO file, bypassing the stub.
6681 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6682 using .gdb_index, then when reading a CU we want to stay in the DWO file
6683 containing that CU. Otherwise we could end up reading several other DWO
6684 files (due to comdat folding) to process the transitive closure of all the
6685 mentioned TUs, and that can be slow. The current DWO file will have every
6686 type signature that it needs.
6687 We only do this for .gdb_index because in the psymtab case we already have
6688 to read all the DWOs to build the type unit groups. */
6689
6690 static struct signatured_type *
6691 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6692 {
6693 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6694 struct dwo_file *dwo_file;
6695 struct dwo_unit find_dwo_entry, *dwo_entry;
6696 struct signatured_type find_sig_entry, *sig_entry;
6697 void **slot;
6698
6699 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6700
6701 /* If TU skeletons have been removed then we may not have read in any
6702 TUs yet. */
6703 if (per_objfile->per_bfd->signatured_types == NULL)
6704 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6705
6706 /* We only ever need to read in one copy of a signatured type.
6707 Use the global signatured_types array to do our own comdat-folding
6708 of types. If this is the first time we're reading this TU, and
6709 the TU has an entry in .gdb_index, replace the recorded data from
6710 .gdb_index with this TU. */
6711
6712 find_sig_entry.signature = sig;
6713 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6714 &find_sig_entry, INSERT);
6715 sig_entry = (struct signatured_type *) *slot;
6716
6717 /* We can get here with the TU already read, *or* in the process of being
6718 read. Don't reassign the global entry to point to this DWO if that's
6719 the case. Also note that if the TU is already being read, it may not
6720 have come from a DWO, the program may be a mix of Fission-compiled
6721 code and non-Fission-compiled code. */
6722
6723 /* Have we already tried to read this TU?
6724 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6725 needn't exist in the global table yet). */
6726 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6727 return sig_entry;
6728
6729 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6730 dwo_unit of the TU itself. */
6731 dwo_file = cu->dwo_unit->dwo_file;
6732
6733 /* Ok, this is the first time we're reading this TU. */
6734 if (dwo_file->tus == NULL)
6735 return NULL;
6736 find_dwo_entry.signature = sig;
6737 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6738 &find_dwo_entry);
6739 if (dwo_entry == NULL)
6740 return NULL;
6741
6742 /* If the global table doesn't have an entry for this TU, add one. */
6743 if (sig_entry == NULL)
6744 sig_entry = add_type_unit (per_objfile, sig, slot);
6745
6746 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6747 sig_entry->per_cu.tu_read = 1;
6748 return sig_entry;
6749 }
6750
6751 /* Subroutine of lookup_signatured_type.
6752 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6753 then try the DWP file. If the TU stub (skeleton) has been removed then
6754 it won't be in .gdb_index. */
6755
6756 static struct signatured_type *
6757 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6758 {
6759 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6760 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6761 struct dwo_unit *dwo_entry;
6762 struct signatured_type find_sig_entry, *sig_entry;
6763 void **slot;
6764
6765 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6766 gdb_assert (dwp_file != NULL);
6767
6768 /* If TU skeletons have been removed then we may not have read in any
6769 TUs yet. */
6770 if (per_objfile->per_bfd->signatured_types == NULL)
6771 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6772
6773 find_sig_entry.signature = sig;
6774 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6775 &find_sig_entry, INSERT);
6776 sig_entry = (struct signatured_type *) *slot;
6777
6778 /* Have we already tried to read this TU?
6779 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6780 needn't exist in the global table yet). */
6781 if (sig_entry != NULL)
6782 return sig_entry;
6783
6784 if (dwp_file->tus == NULL)
6785 return NULL;
6786 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6787 1 /* is_debug_types */);
6788 if (dwo_entry == NULL)
6789 return NULL;
6790
6791 sig_entry = add_type_unit (per_objfile, sig, slot);
6792 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6793
6794 return sig_entry;
6795 }
6796
6797 /* Lookup a signature based type for DW_FORM_ref_sig8.
6798 Returns NULL if signature SIG is not present in the table.
6799 It is up to the caller to complain about this. */
6800
6801 static struct signatured_type *
6802 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6803 {
6804 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6805
6806 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6807 {
6808 /* We're in a DWO/DWP file, and we're using .gdb_index.
6809 These cases require special processing. */
6810 if (get_dwp_file (per_objfile) == NULL)
6811 return lookup_dwo_signatured_type (cu, sig);
6812 else
6813 return lookup_dwp_signatured_type (cu, sig);
6814 }
6815 else
6816 {
6817 struct signatured_type find_entry, *entry;
6818
6819 if (per_objfile->per_bfd->signatured_types == NULL)
6820 return NULL;
6821 find_entry.signature = sig;
6822 entry = ((struct signatured_type *)
6823 htab_find (per_objfile->per_bfd->signatured_types.get (),
6824 &find_entry));
6825 return entry;
6826 }
6827 }
6828
6829 /* Low level DIE reading support. */
6830
6831 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6832
6833 static void
6834 init_cu_die_reader (struct die_reader_specs *reader,
6835 struct dwarf2_cu *cu,
6836 struct dwarf2_section_info *section,
6837 struct dwo_file *dwo_file,
6838 struct abbrev_table *abbrev_table)
6839 {
6840 gdb_assert (section->readin && section->buffer != NULL);
6841 reader->abfd = section->get_bfd_owner ();
6842 reader->cu = cu;
6843 reader->dwo_file = dwo_file;
6844 reader->die_section = section;
6845 reader->buffer = section->buffer;
6846 reader->buffer_end = section->buffer + section->size;
6847 reader->abbrev_table = abbrev_table;
6848 }
6849
6850 /* Subroutine of cutu_reader to simplify it.
6851 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6852 There's just a lot of work to do, and cutu_reader is big enough
6853 already.
6854
6855 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6856 from it to the DIE in the DWO. If NULL we are skipping the stub.
6857 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6858 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6859 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6860 STUB_COMP_DIR may be non-NULL.
6861 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6862 are filled in with the info of the DIE from the DWO file.
6863 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6864 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6865 kept around for at least as long as *RESULT_READER.
6866
6867 The result is non-zero if a valid (non-dummy) DIE was found. */
6868
6869 static int
6870 read_cutu_die_from_dwo (dwarf2_cu *cu,
6871 struct dwo_unit *dwo_unit,
6872 struct die_info *stub_comp_unit_die,
6873 const char *stub_comp_dir,
6874 struct die_reader_specs *result_reader,
6875 const gdb_byte **result_info_ptr,
6876 struct die_info **result_comp_unit_die,
6877 abbrev_table_up *result_dwo_abbrev_table)
6878 {
6879 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6880 dwarf2_per_cu_data *per_cu = cu->per_cu;
6881 struct objfile *objfile = per_objfile->objfile;
6882 bfd *abfd;
6883 const gdb_byte *begin_info_ptr, *info_ptr;
6884 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6885 int i,num_extra_attrs;
6886 struct dwarf2_section_info *dwo_abbrev_section;
6887 struct die_info *comp_unit_die;
6888
6889 /* At most one of these may be provided. */
6890 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6891
6892 /* These attributes aren't processed until later:
6893 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6894 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6895 referenced later. However, these attributes are found in the stub
6896 which we won't have later. In order to not impose this complication
6897 on the rest of the code, we read them here and copy them to the
6898 DWO CU/TU die. */
6899
6900 stmt_list = NULL;
6901 low_pc = NULL;
6902 high_pc = NULL;
6903 ranges = NULL;
6904 comp_dir = NULL;
6905
6906 if (stub_comp_unit_die != NULL)
6907 {
6908 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6909 DWO file. */
6910 if (!per_cu->is_debug_types)
6911 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6912 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6913 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6914 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6915 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6916
6917 cu->addr_base = stub_comp_unit_die->addr_base ();
6918
6919 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6920 We need the value before we can process DW_AT_ranges values from the
6921 DWO. */
6922 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6923
6924 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6925 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6926 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6927 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6928 section. */
6929 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6930 }
6931 else if (stub_comp_dir != NULL)
6932 {
6933 /* Reconstruct the comp_dir attribute to simplify the code below. */
6934 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6935 comp_dir->name = DW_AT_comp_dir;
6936 comp_dir->form = DW_FORM_string;
6937 comp_dir->set_string_noncanonical (stub_comp_dir);
6938 }
6939
6940 /* Set up for reading the DWO CU/TU. */
6941 cu->dwo_unit = dwo_unit;
6942 dwarf2_section_info *section = dwo_unit->section;
6943 section->read (objfile);
6944 abfd = section->get_bfd_owner ();
6945 begin_info_ptr = info_ptr = (section->buffer
6946 + to_underlying (dwo_unit->sect_off));
6947 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6948
6949 if (per_cu->is_debug_types)
6950 {
6951 signatured_type *sig_type = (struct signatured_type *) per_cu;
6952
6953 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6954 section, dwo_abbrev_section,
6955 info_ptr, rcuh_kind::TYPE);
6956 /* This is not an assert because it can be caused by bad debug info. */
6957 if (sig_type->signature != cu->header.signature)
6958 {
6959 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6960 " TU at offset %s [in module %s]"),
6961 hex_string (sig_type->signature),
6962 hex_string (cu->header.signature),
6963 sect_offset_str (dwo_unit->sect_off),
6964 bfd_get_filename (abfd));
6965 }
6966 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6967 /* For DWOs coming from DWP files, we don't know the CU length
6968 nor the type's offset in the TU until now. */
6969 dwo_unit->length = cu->header.get_length ();
6970 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6971
6972 /* Establish the type offset that can be used to lookup the type.
6973 For DWO files, we don't know it until now. */
6974 sig_type->type_offset_in_section
6975 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6976 }
6977 else
6978 {
6979 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6980 section, dwo_abbrev_section,
6981 info_ptr, rcuh_kind::COMPILE);
6982 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6983 /* For DWOs coming from DWP files, we don't know the CU length
6984 until now. */
6985 dwo_unit->length = cu->header.get_length ();
6986 }
6987
6988 dwo_abbrev_section->read (objfile);
6989 *result_dwo_abbrev_table
6990 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6991 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6992 result_dwo_abbrev_table->get ());
6993
6994 /* Read in the die, but leave space to copy over the attributes
6995 from the stub. This has the benefit of simplifying the rest of
6996 the code - all the work to maintain the illusion of a single
6997 DW_TAG_{compile,type}_unit DIE is done here. */
6998 num_extra_attrs = ((stmt_list != NULL)
6999 + (low_pc != NULL)
7000 + (high_pc != NULL)
7001 + (ranges != NULL)
7002 + (comp_dir != NULL));
7003 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7004 num_extra_attrs);
7005
7006 /* Copy over the attributes from the stub to the DIE we just read in. */
7007 comp_unit_die = *result_comp_unit_die;
7008 i = comp_unit_die->num_attrs;
7009 if (stmt_list != NULL)
7010 comp_unit_die->attrs[i++] = *stmt_list;
7011 if (low_pc != NULL)
7012 comp_unit_die->attrs[i++] = *low_pc;
7013 if (high_pc != NULL)
7014 comp_unit_die->attrs[i++] = *high_pc;
7015 if (ranges != NULL)
7016 comp_unit_die->attrs[i++] = *ranges;
7017 if (comp_dir != NULL)
7018 comp_unit_die->attrs[i++] = *comp_dir;
7019 comp_unit_die->num_attrs += num_extra_attrs;
7020
7021 if (dwarf_die_debug)
7022 {
7023 fprintf_unfiltered (gdb_stdlog,
7024 "Read die from %s@0x%x of %s:\n",
7025 section->get_name (),
7026 (unsigned) (begin_info_ptr - section->buffer),
7027 bfd_get_filename (abfd));
7028 dump_die (comp_unit_die, dwarf_die_debug);
7029 }
7030
7031 /* Skip dummy compilation units. */
7032 if (info_ptr >= begin_info_ptr + dwo_unit->length
7033 || peek_abbrev_code (abfd, info_ptr) == 0)
7034 return 0;
7035
7036 *result_info_ptr = info_ptr;
7037 return 1;
7038 }
7039
7040 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7041 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7042 signature is part of the header. */
7043 static gdb::optional<ULONGEST>
7044 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7045 {
7046 if (cu->header.version >= 5)
7047 return cu->header.signature;
7048 struct attribute *attr;
7049 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7050 if (attr == nullptr || !attr->form_is_unsigned ())
7051 return gdb::optional<ULONGEST> ();
7052 return attr->as_unsigned ();
7053 }
7054
7055 /* Subroutine of cutu_reader to simplify it.
7056 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7057 Returns NULL if the specified DWO unit cannot be found. */
7058
7059 static struct dwo_unit *
7060 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7061 {
7062 dwarf2_per_cu_data *per_cu = cu->per_cu;
7063 struct dwo_unit *dwo_unit;
7064 const char *comp_dir;
7065
7066 gdb_assert (cu != NULL);
7067
7068 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7069 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7070 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7071
7072 if (per_cu->is_debug_types)
7073 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7074 else
7075 {
7076 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7077
7078 if (!signature.has_value ())
7079 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7080 " [in module %s]"),
7081 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7082
7083 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7084 }
7085
7086 return dwo_unit;
7087 }
7088
7089 /* Subroutine of cutu_reader to simplify it.
7090 See it for a description of the parameters.
7091 Read a TU directly from a DWO file, bypassing the stub. */
7092
7093 void
7094 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7095 dwarf2_per_objfile *per_objfile,
7096 dwarf2_cu *existing_cu)
7097 {
7098 struct signatured_type *sig_type;
7099
7100 /* Verify we can do the following downcast, and that we have the
7101 data we need. */
7102 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7103 sig_type = (struct signatured_type *) this_cu;
7104 gdb_assert (sig_type->dwo_unit != NULL);
7105
7106 dwarf2_cu *cu;
7107
7108 if (existing_cu != nullptr)
7109 {
7110 cu = existing_cu;
7111 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7112 /* There's no need to do the rereading_dwo_cu handling that
7113 cutu_reader does since we don't read the stub. */
7114 }
7115 else
7116 {
7117 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7118 in per_objfile yet. */
7119 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7120 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7121 cu = m_new_cu.get ();
7122 }
7123
7124 /* A future optimization, if needed, would be to use an existing
7125 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7126 could share abbrev tables. */
7127
7128 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7129 NULL /* stub_comp_unit_die */,
7130 sig_type->dwo_unit->dwo_file->comp_dir,
7131 this, &info_ptr,
7132 &comp_unit_die,
7133 &m_dwo_abbrev_table) == 0)
7134 {
7135 /* Dummy die. */
7136 dummy_p = true;
7137 }
7138 }
7139
7140 /* Initialize a CU (or TU) and read its DIEs.
7141 If the CU defers to a DWO file, read the DWO file as well.
7142
7143 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7144 Otherwise the table specified in the comp unit header is read in and used.
7145 This is an optimization for when we already have the abbrev table.
7146
7147 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7148 allocated. */
7149
7150 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7151 dwarf2_per_objfile *per_objfile,
7152 struct abbrev_table *abbrev_table,
7153 dwarf2_cu *existing_cu,
7154 bool skip_partial)
7155 : die_reader_specs {},
7156 m_this_cu (this_cu)
7157 {
7158 struct objfile *objfile = per_objfile->objfile;
7159 struct dwarf2_section_info *section = this_cu->section;
7160 bfd *abfd = section->get_bfd_owner ();
7161 const gdb_byte *begin_info_ptr;
7162 struct signatured_type *sig_type = NULL;
7163 struct dwarf2_section_info *abbrev_section;
7164 /* Non-zero if CU currently points to a DWO file and we need to
7165 reread it. When this happens we need to reread the skeleton die
7166 before we can reread the DWO file (this only applies to CUs, not TUs). */
7167 int rereading_dwo_cu = 0;
7168
7169 if (dwarf_die_debug)
7170 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7171 this_cu->is_debug_types ? "type" : "comp",
7172 sect_offset_str (this_cu->sect_off));
7173
7174 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7175 file (instead of going through the stub), short-circuit all of this. */
7176 if (this_cu->reading_dwo_directly)
7177 {
7178 /* Narrow down the scope of possibilities to have to understand. */
7179 gdb_assert (this_cu->is_debug_types);
7180 gdb_assert (abbrev_table == NULL);
7181 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7182 return;
7183 }
7184
7185 /* This is cheap if the section is already read in. */
7186 section->read (objfile);
7187
7188 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7189
7190 abbrev_section = get_abbrev_section_for_cu (this_cu);
7191
7192 dwarf2_cu *cu;
7193
7194 if (existing_cu != nullptr)
7195 {
7196 cu = existing_cu;
7197 /* If this CU is from a DWO file we need to start over, we need to
7198 refetch the attributes from the skeleton CU.
7199 This could be optimized by retrieving those attributes from when we
7200 were here the first time: the previous comp_unit_die was stored in
7201 comp_unit_obstack. But there's no data yet that we need this
7202 optimization. */
7203 if (cu->dwo_unit != NULL)
7204 rereading_dwo_cu = 1;
7205 }
7206 else
7207 {
7208 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7209 in per_objfile yet. */
7210 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7211 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7212 cu = m_new_cu.get ();
7213 }
7214
7215 /* Get the header. */
7216 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7217 {
7218 /* We already have the header, there's no need to read it in again. */
7219 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7220 }
7221 else
7222 {
7223 if (this_cu->is_debug_types)
7224 {
7225 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7226 section, abbrev_section,
7227 info_ptr, rcuh_kind::TYPE);
7228
7229 /* Since per_cu is the first member of struct signatured_type,
7230 we can go from a pointer to one to a pointer to the other. */
7231 sig_type = (struct signatured_type *) this_cu;
7232 gdb_assert (sig_type->signature == cu->header.signature);
7233 gdb_assert (sig_type->type_offset_in_tu
7234 == cu->header.type_cu_offset_in_tu);
7235 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7236
7237 /* LENGTH has not been set yet for type units if we're
7238 using .gdb_index. */
7239 this_cu->length = cu->header.get_length ();
7240
7241 /* Establish the type offset that can be used to lookup the type. */
7242 sig_type->type_offset_in_section =
7243 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7244
7245 this_cu->dwarf_version = cu->header.version;
7246 }
7247 else
7248 {
7249 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7250 section, abbrev_section,
7251 info_ptr,
7252 rcuh_kind::COMPILE);
7253
7254 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7255 if (this_cu->length == 0)
7256 this_cu->length = cu->header.get_length ();
7257 else
7258 gdb_assert (this_cu->length == cu->header.get_length ());
7259 this_cu->dwarf_version = cu->header.version;
7260 }
7261 }
7262
7263 /* Skip dummy compilation units. */
7264 if (info_ptr >= begin_info_ptr + this_cu->length
7265 || peek_abbrev_code (abfd, info_ptr) == 0)
7266 {
7267 dummy_p = true;
7268 return;
7269 }
7270
7271 /* If we don't have them yet, read the abbrevs for this compilation unit.
7272 And if we need to read them now, make sure they're freed when we're
7273 done. */
7274 if (abbrev_table != NULL)
7275 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7276 else
7277 {
7278 abbrev_section->read (objfile);
7279 m_abbrev_table_holder
7280 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7281 abbrev_table = m_abbrev_table_holder.get ();
7282 }
7283
7284 /* Read the top level CU/TU die. */
7285 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7286 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7287
7288 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7289 {
7290 dummy_p = true;
7291 return;
7292 }
7293
7294 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7295 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7296 table from the DWO file and pass the ownership over to us. It will be
7297 referenced from READER, so we must make sure to free it after we're done
7298 with READER.
7299
7300 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7301 DWO CU, that this test will fail (the attribute will not be present). */
7302 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7303 if (dwo_name != nullptr)
7304 {
7305 struct dwo_unit *dwo_unit;
7306 struct die_info *dwo_comp_unit_die;
7307
7308 if (comp_unit_die->has_children)
7309 {
7310 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7311 " has children (offset %s) [in module %s]"),
7312 sect_offset_str (this_cu->sect_off),
7313 bfd_get_filename (abfd));
7314 }
7315 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7316 if (dwo_unit != NULL)
7317 {
7318 if (read_cutu_die_from_dwo (cu, dwo_unit,
7319 comp_unit_die, NULL,
7320 this, &info_ptr,
7321 &dwo_comp_unit_die,
7322 &m_dwo_abbrev_table) == 0)
7323 {
7324 /* Dummy die. */
7325 dummy_p = true;
7326 return;
7327 }
7328 comp_unit_die = dwo_comp_unit_die;
7329 }
7330 else
7331 {
7332 /* Yikes, we couldn't find the rest of the DIE, we only have
7333 the stub. A complaint has already been logged. There's
7334 not much more we can do except pass on the stub DIE to
7335 die_reader_func. We don't want to throw an error on bad
7336 debug info. */
7337 }
7338 }
7339 }
7340
7341 void
7342 cutu_reader::keep ()
7343 {
7344 /* Done, clean up. */
7345 gdb_assert (!dummy_p);
7346 if (m_new_cu != NULL)
7347 {
7348 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7349 now. */
7350 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7351 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7352 }
7353 }
7354
7355 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7356 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7357 assumed to have already done the lookup to find the DWO file).
7358
7359 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7360 THIS_CU->is_debug_types, but nothing else.
7361
7362 We fill in THIS_CU->length.
7363
7364 THIS_CU->cu is always freed when done.
7365 This is done in order to not leave THIS_CU->cu in a state where we have
7366 to care whether it refers to the "main" CU or the DWO CU.
7367
7368 When parent_cu is passed, it is used to provide a default value for
7369 str_offsets_base and addr_base from the parent. */
7370
7371 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7372 dwarf2_per_objfile *per_objfile,
7373 struct dwarf2_cu *parent_cu,
7374 struct dwo_file *dwo_file)
7375 : die_reader_specs {},
7376 m_this_cu (this_cu)
7377 {
7378 struct objfile *objfile = per_objfile->objfile;
7379 struct dwarf2_section_info *section = this_cu->section;
7380 bfd *abfd = section->get_bfd_owner ();
7381 struct dwarf2_section_info *abbrev_section;
7382 const gdb_byte *begin_info_ptr, *info_ptr;
7383
7384 if (dwarf_die_debug)
7385 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7386 this_cu->is_debug_types ? "type" : "comp",
7387 sect_offset_str (this_cu->sect_off));
7388
7389 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7390
7391 abbrev_section = (dwo_file != NULL
7392 ? &dwo_file->sections.abbrev
7393 : get_abbrev_section_for_cu (this_cu));
7394
7395 /* This is cheap if the section is already read in. */
7396 section->read (objfile);
7397
7398 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7399
7400 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7401 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7402 section, abbrev_section, info_ptr,
7403 (this_cu->is_debug_types
7404 ? rcuh_kind::TYPE
7405 : rcuh_kind::COMPILE));
7406
7407 if (parent_cu != nullptr)
7408 {
7409 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7410 m_new_cu->addr_base = parent_cu->addr_base;
7411 }
7412 this_cu->length = m_new_cu->header.get_length ();
7413
7414 /* Skip dummy compilation units. */
7415 if (info_ptr >= begin_info_ptr + this_cu->length
7416 || peek_abbrev_code (abfd, info_ptr) == 0)
7417 {
7418 dummy_p = true;
7419 return;
7420 }
7421
7422 abbrev_section->read (objfile);
7423 m_abbrev_table_holder
7424 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7425
7426 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7427 m_abbrev_table_holder.get ());
7428 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7429 }
7430
7431 \f
7432 /* Type Unit Groups.
7433
7434 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7435 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7436 so that all types coming from the same compilation (.o file) are grouped
7437 together. A future step could be to put the types in the same symtab as
7438 the CU the types ultimately came from. */
7439
7440 static hashval_t
7441 hash_type_unit_group (const void *item)
7442 {
7443 const struct type_unit_group *tu_group
7444 = (const struct type_unit_group *) item;
7445
7446 return hash_stmt_list_entry (&tu_group->hash);
7447 }
7448
7449 static int
7450 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7451 {
7452 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7453 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7454
7455 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7456 }
7457
7458 /* Allocate a hash table for type unit groups. */
7459
7460 static htab_up
7461 allocate_type_unit_groups_table ()
7462 {
7463 return htab_up (htab_create_alloc (3,
7464 hash_type_unit_group,
7465 eq_type_unit_group,
7466 NULL, xcalloc, xfree));
7467 }
7468
7469 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7470 partial symtabs. We combine several TUs per psymtab to not let the size
7471 of any one psymtab grow too big. */
7472 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7473 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7474
7475 /* Helper routine for get_type_unit_group.
7476 Create the type_unit_group object used to hold one or more TUs. */
7477
7478 static struct type_unit_group *
7479 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7480 {
7481 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7482 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7483 struct dwarf2_per_cu_data *per_cu;
7484 struct type_unit_group *tu_group;
7485
7486 tu_group = OBSTACK_ZALLOC (&per_bfd->obstack, type_unit_group);
7487 per_cu = &tu_group->per_cu;
7488 per_cu->per_bfd = per_bfd;
7489
7490 if (per_bfd->using_index)
7491 {
7492 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7493 struct dwarf2_per_cu_quick_data);
7494 }
7495 else
7496 {
7497 unsigned int line_offset = to_underlying (line_offset_struct);
7498 dwarf2_psymtab *pst;
7499 std::string name;
7500
7501 /* Give the symtab a useful name for debug purposes. */
7502 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7503 name = string_printf ("<type_units_%d>",
7504 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7505 else
7506 name = string_printf ("<type_units_at_0x%x>", line_offset);
7507
7508 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7509 pst->anonymous = true;
7510 }
7511
7512 tu_group->hash.dwo_unit = cu->dwo_unit;
7513 tu_group->hash.line_sect_off = line_offset_struct;
7514
7515 return tu_group;
7516 }
7517
7518 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7519 STMT_LIST is a DW_AT_stmt_list attribute. */
7520
7521 static struct type_unit_group *
7522 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7523 {
7524 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7525 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7526 struct type_unit_group *tu_group;
7527 void **slot;
7528 unsigned int line_offset;
7529 struct type_unit_group type_unit_group_for_lookup;
7530
7531 if (per_objfile->per_bfd->type_unit_groups == NULL)
7532 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7533
7534 /* Do we need to create a new group, or can we use an existing one? */
7535
7536 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7537 {
7538 line_offset = stmt_list->as_unsigned ();
7539 ++tu_stats->nr_symtab_sharers;
7540 }
7541 else
7542 {
7543 /* Ugh, no stmt_list. Rare, but we have to handle it.
7544 We can do various things here like create one group per TU or
7545 spread them over multiple groups to split up the expansion work.
7546 To avoid worst case scenarios (too many groups or too large groups)
7547 we, umm, group them in bunches. */
7548 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7549 | (tu_stats->nr_stmt_less_type_units
7550 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7551 ++tu_stats->nr_stmt_less_type_units;
7552 }
7553
7554 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7555 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7556 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7557 &type_unit_group_for_lookup, INSERT);
7558 if (*slot != NULL)
7559 {
7560 tu_group = (struct type_unit_group *) *slot;
7561 gdb_assert (tu_group != NULL);
7562 }
7563 else
7564 {
7565 sect_offset line_offset_struct = (sect_offset) line_offset;
7566 tu_group = create_type_unit_group (cu, line_offset_struct);
7567 *slot = tu_group;
7568 ++tu_stats->nr_symtabs;
7569 }
7570
7571 return tu_group;
7572 }
7573 \f
7574 /* Partial symbol tables. */
7575
7576 /* Create a psymtab named NAME and assign it to PER_CU.
7577
7578 The caller must fill in the following details:
7579 dirname, textlow, texthigh. */
7580
7581 static dwarf2_psymtab *
7582 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7583 dwarf2_per_objfile *per_objfile,
7584 const char *name)
7585 {
7586 dwarf2_psymtab *pst
7587 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7588 per_objfile->objfile->per_bfd, per_cu);
7589
7590 pst->psymtabs_addrmap_supported = true;
7591
7592 /* This is the glue that links PST into GDB's symbol API. */
7593 per_cu->v.psymtab = pst;
7594
7595 return pst;
7596 }
7597
7598 /* DIE reader function for process_psymtab_comp_unit. */
7599
7600 static void
7601 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7602 const gdb_byte *info_ptr,
7603 struct die_info *comp_unit_die,
7604 enum language pretend_language)
7605 {
7606 struct dwarf2_cu *cu = reader->cu;
7607 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7608 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7609 struct objfile *objfile = per_objfile->objfile;
7610 struct gdbarch *gdbarch = objfile->arch ();
7611 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7612 CORE_ADDR baseaddr;
7613 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7614 dwarf2_psymtab *pst;
7615 enum pc_bounds_kind cu_bounds_kind;
7616 const char *filename;
7617
7618 gdb_assert (! per_cu->is_debug_types);
7619
7620 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7621
7622 /* Allocate a new partial symbol table structure. */
7623 gdb::unique_xmalloc_ptr<char> debug_filename;
7624 static const char artificial[] = "<artificial>";
7625 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7626 if (filename == NULL)
7627 filename = "";
7628 else if (strcmp (filename, artificial) == 0)
7629 {
7630 debug_filename.reset (concat (artificial, "@",
7631 sect_offset_str (per_cu->sect_off),
7632 (char *) NULL));
7633 filename = debug_filename.get ();
7634 }
7635
7636 pst = create_partial_symtab (per_cu, per_objfile, filename);
7637
7638 /* This must be done before calling dwarf2_build_include_psymtabs. */
7639 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7640
7641 baseaddr = objfile->text_section_offset ();
7642
7643 dwarf2_find_base_address (comp_unit_die, cu);
7644
7645 /* Possibly set the default values of LOWPC and HIGHPC from
7646 `DW_AT_ranges'. */
7647 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7648 &best_highpc, cu, pst);
7649 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7650 {
7651 CORE_ADDR low
7652 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7653 - baseaddr);
7654 CORE_ADDR high
7655 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7656 - baseaddr - 1);
7657 /* Store the contiguous range if it is not empty; it can be
7658 empty for CUs with no code. */
7659 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7660 low, high, pst);
7661 }
7662
7663 /* Check if comp unit has_children.
7664 If so, read the rest of the partial symbols from this comp unit.
7665 If not, there's no more debug_info for this comp unit. */
7666 if (comp_unit_die->has_children)
7667 {
7668 struct partial_die_info *first_die;
7669 CORE_ADDR lowpc, highpc;
7670
7671 lowpc = ((CORE_ADDR) -1);
7672 highpc = ((CORE_ADDR) 0);
7673
7674 first_die = load_partial_dies (reader, info_ptr, 1);
7675
7676 scan_partial_symbols (first_die, &lowpc, &highpc,
7677 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7678
7679 /* If we didn't find a lowpc, set it to highpc to avoid
7680 complaints from `maint check'. */
7681 if (lowpc == ((CORE_ADDR) -1))
7682 lowpc = highpc;
7683
7684 /* If the compilation unit didn't have an explicit address range,
7685 then use the information extracted from its child dies. */
7686 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7687 {
7688 best_lowpc = lowpc;
7689 best_highpc = highpc;
7690 }
7691 }
7692 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7693 best_lowpc + baseaddr)
7694 - baseaddr);
7695 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7696 best_highpc + baseaddr)
7697 - baseaddr);
7698
7699 pst->end ();
7700
7701 if (!cu->per_cu->imported_symtabs_empty ())
7702 {
7703 int i;
7704 int len = cu->per_cu->imported_symtabs_size ();
7705
7706 /* Fill in 'dependencies' here; we fill in 'users' in a
7707 post-pass. */
7708 pst->number_of_dependencies = len;
7709 pst->dependencies
7710 = per_bfd->partial_symtabs->allocate_dependencies (len);
7711 for (i = 0; i < len; ++i)
7712 {
7713 pst->dependencies[i]
7714 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7715 }
7716
7717 cu->per_cu->imported_symtabs_free ();
7718 }
7719
7720 /* Get the list of files included in the current compilation unit,
7721 and build a psymtab for each of them. */
7722 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7723
7724 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7725 ", %d global, %d static syms",
7726 per_cu->is_debug_types ? "type" : "comp",
7727 sect_offset_str (per_cu->sect_off),
7728 paddress (gdbarch, pst->text_low (objfile)),
7729 paddress (gdbarch, pst->text_high (objfile)),
7730 (int) pst->global_psymbols.size (),
7731 (int) pst->static_psymbols.size ());
7732 }
7733
7734 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7735 Process compilation unit THIS_CU for a psymtab. */
7736
7737 static void
7738 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7739 dwarf2_per_objfile *per_objfile,
7740 bool want_partial_unit,
7741 enum language pretend_language)
7742 {
7743 /* If this compilation unit was already read in, free the
7744 cached copy in order to read it in again. This is
7745 necessary because we skipped some symbols when we first
7746 read in the compilation unit (see load_partial_dies).
7747 This problem could be avoided, but the benefit is unclear. */
7748 per_objfile->remove_cu (this_cu);
7749
7750 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7751
7752 switch (reader.comp_unit_die->tag)
7753 {
7754 case DW_TAG_compile_unit:
7755 this_cu->unit_type = DW_UT_compile;
7756 break;
7757 case DW_TAG_partial_unit:
7758 this_cu->unit_type = DW_UT_partial;
7759 break;
7760 case DW_TAG_type_unit:
7761 this_cu->unit_type = DW_UT_type;
7762 break;
7763 default:
7764 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7765 dwarf_tag_name (reader.comp_unit_die->tag),
7766 sect_offset_str (reader.cu->per_cu->sect_off),
7767 objfile_name (per_objfile->objfile));
7768 }
7769
7770 if (reader.dummy_p)
7771 {
7772 /* Nothing. */
7773 }
7774 else if (this_cu->is_debug_types)
7775 build_type_psymtabs_reader (&reader, reader.info_ptr,
7776 reader.comp_unit_die);
7777 else if (want_partial_unit
7778 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7779 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7780 reader.comp_unit_die,
7781 pretend_language);
7782
7783 this_cu->lang = reader.cu->language;
7784
7785 /* Age out any secondary CUs. */
7786 per_objfile->age_comp_units ();
7787 }
7788
7789 /* Reader function for build_type_psymtabs. */
7790
7791 static void
7792 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7793 const gdb_byte *info_ptr,
7794 struct die_info *type_unit_die)
7795 {
7796 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7797 struct dwarf2_cu *cu = reader->cu;
7798 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7799 struct signatured_type *sig_type;
7800 struct type_unit_group *tu_group;
7801 struct attribute *attr;
7802 struct partial_die_info *first_die;
7803 CORE_ADDR lowpc, highpc;
7804 dwarf2_psymtab *pst;
7805
7806 gdb_assert (per_cu->is_debug_types);
7807 sig_type = (struct signatured_type *) per_cu;
7808
7809 if (! type_unit_die->has_children)
7810 return;
7811
7812 attr = type_unit_die->attr (DW_AT_stmt_list);
7813 tu_group = get_type_unit_group (cu, attr);
7814
7815 if (tu_group->tus == nullptr)
7816 tu_group->tus = new std::vector<signatured_type *>;
7817 tu_group->tus->push_back (sig_type);
7818
7819 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7820 pst = create_partial_symtab (per_cu, per_objfile, "");
7821 pst->anonymous = true;
7822
7823 first_die = load_partial_dies (reader, info_ptr, 1);
7824
7825 lowpc = (CORE_ADDR) -1;
7826 highpc = (CORE_ADDR) 0;
7827 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7828
7829 pst->end ();
7830 }
7831
7832 /* Struct used to sort TUs by their abbreviation table offset. */
7833
7834 struct tu_abbrev_offset
7835 {
7836 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7837 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7838 {}
7839
7840 signatured_type *sig_type;
7841 sect_offset abbrev_offset;
7842 };
7843
7844 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7845
7846 static bool
7847 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7848 const struct tu_abbrev_offset &b)
7849 {
7850 return a.abbrev_offset < b.abbrev_offset;
7851 }
7852
7853 /* Efficiently read all the type units.
7854 This does the bulk of the work for build_type_psymtabs.
7855
7856 The efficiency is because we sort TUs by the abbrev table they use and
7857 only read each abbrev table once. In one program there are 200K TUs
7858 sharing 8K abbrev tables.
7859
7860 The main purpose of this function is to support building the
7861 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7862 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7863 can collapse the search space by grouping them by stmt_list.
7864 The savings can be significant, in the same program from above the 200K TUs
7865 share 8K stmt_list tables.
7866
7867 FUNC is expected to call get_type_unit_group, which will create the
7868 struct type_unit_group if necessary and add it to
7869 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7870
7871 static void
7872 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7873 {
7874 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7875 abbrev_table_up abbrev_table;
7876 sect_offset abbrev_offset;
7877
7878 /* It's up to the caller to not call us multiple times. */
7879 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7880
7881 if (per_objfile->per_bfd->all_type_units.empty ())
7882 return;
7883
7884 /* TUs typically share abbrev tables, and there can be way more TUs than
7885 abbrev tables. Sort by abbrev table to reduce the number of times we
7886 read each abbrev table in.
7887 Alternatives are to punt or to maintain a cache of abbrev tables.
7888 This is simpler and efficient enough for now.
7889
7890 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7891 symtab to use). Typically TUs with the same abbrev offset have the same
7892 stmt_list value too so in practice this should work well.
7893
7894 The basic algorithm here is:
7895
7896 sort TUs by abbrev table
7897 for each TU with same abbrev table:
7898 read abbrev table if first user
7899 read TU top level DIE
7900 [IWBN if DWO skeletons had DW_AT_stmt_list]
7901 call FUNC */
7902
7903 dwarf_read_debug_printf ("Building type unit groups ...");
7904
7905 /* Sort in a separate table to maintain the order of all_type_units
7906 for .gdb_index: TU indices directly index all_type_units. */
7907 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7908 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7909
7910 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7911 sorted_by_abbrev.emplace_back
7912 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7913 sig_type->per_cu.sect_off));
7914
7915 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7916 sort_tu_by_abbrev_offset);
7917
7918 abbrev_offset = (sect_offset) ~(unsigned) 0;
7919
7920 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7921 {
7922 /* Switch to the next abbrev table if necessary. */
7923 if (abbrev_table == NULL
7924 || tu.abbrev_offset != abbrev_offset)
7925 {
7926 abbrev_offset = tu.abbrev_offset;
7927 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7928 abbrev_table =
7929 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7930 ++tu_stats->nr_uniq_abbrev_tables;
7931 }
7932
7933 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7934 abbrev_table.get (), nullptr, false);
7935 if (!reader.dummy_p)
7936 build_type_psymtabs_reader (&reader, reader.info_ptr,
7937 reader.comp_unit_die);
7938 }
7939 }
7940
7941 /* Print collected type unit statistics. */
7942
7943 static void
7944 print_tu_stats (dwarf2_per_objfile *per_objfile)
7945 {
7946 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7947
7948 dwarf_read_debug_printf ("Type unit statistics:");
7949 dwarf_read_debug_printf (" %zu TUs",
7950 per_objfile->per_bfd->all_type_units.size ());
7951 dwarf_read_debug_printf (" %d uniq abbrev tables",
7952 tu_stats->nr_uniq_abbrev_tables);
7953 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7954 tu_stats->nr_symtabs);
7955 dwarf_read_debug_printf (" %d symtab sharers",
7956 tu_stats->nr_symtab_sharers);
7957 dwarf_read_debug_printf (" %d type units without a stmt_list",
7958 tu_stats->nr_stmt_less_type_units);
7959 dwarf_read_debug_printf (" %d all_type_units reallocs",
7960 tu_stats->nr_all_type_units_reallocs);
7961 }
7962
7963 /* Traversal function for build_type_psymtabs. */
7964
7965 static int
7966 build_type_psymtab_dependencies (void **slot, void *info)
7967 {
7968 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7969 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7970 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7971 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7972 dwarf2_psymtab *pst = per_cu->v.psymtab;
7973 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7974 int i;
7975
7976 gdb_assert (len > 0);
7977 gdb_assert (per_cu->type_unit_group_p ());
7978
7979 pst->number_of_dependencies = len;
7980 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7981 for (i = 0; i < len; ++i)
7982 {
7983 struct signatured_type *iter = tu_group->tus->at (i);
7984 gdb_assert (iter->per_cu.is_debug_types);
7985 pst->dependencies[i] = iter->per_cu.v.psymtab;
7986 iter->type_unit_group = tu_group;
7987 }
7988
7989 delete tu_group->tus;
7990 tu_group->tus = nullptr;
7991
7992 return 1;
7993 }
7994
7995 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7996 Build partial symbol tables for the .debug_types comp-units. */
7997
7998 static void
7999 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
8000 {
8001 if (! create_all_type_units (per_objfile))
8002 return;
8003
8004 build_type_psymtabs_1 (per_objfile);
8005 }
8006
8007 /* Traversal function for process_skeletonless_type_unit.
8008 Read a TU in a DWO file and build partial symbols for it. */
8009
8010 static int
8011 process_skeletonless_type_unit (void **slot, void *info)
8012 {
8013 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8014 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8015 struct signatured_type find_entry, *entry;
8016
8017 /* If this TU doesn't exist in the global table, add it and read it in. */
8018
8019 if (per_objfile->per_bfd->signatured_types == NULL)
8020 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
8021
8022 find_entry.signature = dwo_unit->signature;
8023 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
8024 &find_entry, INSERT);
8025 /* If we've already seen this type there's nothing to do. What's happening
8026 is we're doing our own version of comdat-folding here. */
8027 if (*slot != NULL)
8028 return 1;
8029
8030 /* This does the job that create_all_type_units would have done for
8031 this TU. */
8032 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8033 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
8034 *slot = entry;
8035
8036 /* This does the job that build_type_psymtabs_1 would have done. */
8037 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
8038 if (!reader.dummy_p)
8039 build_type_psymtabs_reader (&reader, reader.info_ptr,
8040 reader.comp_unit_die);
8041
8042 return 1;
8043 }
8044
8045 /* Traversal function for process_skeletonless_type_units. */
8046
8047 static int
8048 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8049 {
8050 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8051
8052 if (dwo_file->tus != NULL)
8053 htab_traverse_noresize (dwo_file->tus.get (),
8054 process_skeletonless_type_unit, info);
8055
8056 return 1;
8057 }
8058
8059 /* Scan all TUs of DWO files, verifying we've processed them.
8060 This is needed in case a TU was emitted without its skeleton.
8061 Note: This can't be done until we know what all the DWO files are. */
8062
8063 static void
8064 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8065 {
8066 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8067 if (get_dwp_file (per_objfile) == NULL
8068 && per_objfile->per_bfd->dwo_files != NULL)
8069 {
8070 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8071 process_dwo_file_for_skeletonless_type_units,
8072 per_objfile);
8073 }
8074 }
8075
8076 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8077
8078 static void
8079 set_partial_user (dwarf2_per_objfile *per_objfile)
8080 {
8081 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8082 {
8083 dwarf2_psymtab *pst = per_cu->v.psymtab;
8084
8085 if (pst == NULL)
8086 continue;
8087
8088 for (int j = 0; j < pst->number_of_dependencies; ++j)
8089 {
8090 /* Set the 'user' field only if it is not already set. */
8091 if (pst->dependencies[j]->user == NULL)
8092 pst->dependencies[j]->user = pst;
8093 }
8094 }
8095 }
8096
8097 /* Build the partial symbol table by doing a quick pass through the
8098 .debug_info and .debug_abbrev sections. */
8099
8100 static void
8101 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8102 {
8103 struct objfile *objfile = per_objfile->objfile;
8104 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
8105
8106 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8107 objfile_name (objfile));
8108
8109 scoped_restore restore_reading_psyms
8110 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
8111
8112 per_bfd->info.read (objfile);
8113
8114 /* Any cached compilation units will be linked by the per-objfile
8115 read_in_chain. Make sure to free them when we're done. */
8116 free_cached_comp_units freer (per_objfile);
8117
8118 build_type_psymtabs (per_objfile);
8119
8120 create_all_comp_units (per_objfile);
8121
8122 /* Create a temporary address map on a temporary obstack. We later
8123 copy this to the final obstack. */
8124 auto_obstack temp_obstack;
8125
8126 scoped_restore save_psymtabs_addrmap
8127 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
8128 addrmap_create_mutable (&temp_obstack));
8129
8130 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
8131 {
8132 if (per_cu->v.psymtab != NULL)
8133 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8134 continue;
8135 process_psymtab_comp_unit (per_cu, per_objfile, false,
8136 language_minimal);
8137 }
8138
8139 /* This has to wait until we read the CUs, we need the list of DWOs. */
8140 process_skeletonless_type_units (per_objfile);
8141
8142 /* Now that all TUs have been processed we can fill in the dependencies. */
8143 if (per_bfd->type_unit_groups != NULL)
8144 {
8145 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
8146 build_type_psymtab_dependencies, per_objfile);
8147 }
8148
8149 if (dwarf_read_debug > 0)
8150 print_tu_stats (per_objfile);
8151
8152 set_partial_user (per_objfile);
8153
8154 per_bfd->partial_symtabs->psymtabs_addrmap
8155 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
8156 per_bfd->partial_symtabs->obstack ());
8157 /* At this point we want to keep the address map. */
8158 save_psymtabs_addrmap.release ();
8159
8160 dwarf_read_debug_printf ("Done building psymtabs of %s",
8161 objfile_name (objfile));
8162 }
8163
8164 /* Load the partial DIEs for a secondary CU into memory.
8165 This is also used when rereading a primary CU with load_all_dies. */
8166
8167 static void
8168 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8169 dwarf2_per_objfile *per_objfile,
8170 dwarf2_cu *existing_cu)
8171 {
8172 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8173
8174 if (!reader.dummy_p)
8175 {
8176 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8177 language_minimal);
8178
8179 /* Check if comp unit has_children.
8180 If so, read the rest of the partial symbols from this comp unit.
8181 If not, there's no more debug_info for this comp unit. */
8182 if (reader.comp_unit_die->has_children)
8183 load_partial_dies (&reader, reader.info_ptr, 0);
8184
8185 reader.keep ();
8186 }
8187 }
8188
8189 static void
8190 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8191 struct dwarf2_section_info *section,
8192 struct dwarf2_section_info *abbrev_section,
8193 unsigned int is_dwz)
8194 {
8195 const gdb_byte *info_ptr;
8196 struct objfile *objfile = per_objfile->objfile;
8197
8198 dwarf_read_debug_printf ("Reading %s for %s",
8199 section->get_name (),
8200 section->get_file_name ());
8201
8202 section->read (objfile);
8203
8204 info_ptr = section->buffer;
8205
8206 while (info_ptr < section->buffer + section->size)
8207 {
8208 struct dwarf2_per_cu_data *this_cu;
8209
8210 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8211
8212 comp_unit_head cu_header;
8213 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8214 abbrev_section, info_ptr,
8215 rcuh_kind::COMPILE);
8216
8217 /* Save the compilation unit for later lookup. */
8218 if (cu_header.unit_type != DW_UT_type)
8219 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8220 else
8221 {
8222 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8223 sig_type->signature = cu_header.signature;
8224 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8225 this_cu = &sig_type->per_cu;
8226 }
8227 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8228 this_cu->sect_off = sect_off;
8229 this_cu->length = cu_header.length + cu_header.initial_length_size;
8230 this_cu->is_dwz = is_dwz;
8231 this_cu->section = section;
8232
8233 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8234
8235 info_ptr = info_ptr + this_cu->length;
8236 }
8237 }
8238
8239 /* Create a list of all compilation units in OBJFILE.
8240 This is only done for -readnow and building partial symtabs. */
8241
8242 static void
8243 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8244 {
8245 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8246 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8247 &per_objfile->per_bfd->abbrev, 0);
8248
8249 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8250 if (dwz != NULL)
8251 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8252 }
8253
8254 /* Process all loaded DIEs for compilation unit CU, starting at
8255 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8256 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8257 DW_AT_ranges). See the comments of add_partial_subprogram on how
8258 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8259
8260 static void
8261 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8262 CORE_ADDR *highpc, int set_addrmap,
8263 struct dwarf2_cu *cu)
8264 {
8265 struct partial_die_info *pdi;
8266
8267 /* Now, march along the PDI's, descending into ones which have
8268 interesting children but skipping the children of the other ones,
8269 until we reach the end of the compilation unit. */
8270
8271 pdi = first_die;
8272
8273 while (pdi != NULL)
8274 {
8275 pdi->fixup (cu);
8276
8277 /* Anonymous namespaces or modules have no name but have interesting
8278 children, so we need to look at them. Ditto for anonymous
8279 enums. */
8280
8281 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8282 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8283 || pdi->tag == DW_TAG_imported_unit
8284 || pdi->tag == DW_TAG_inlined_subroutine)
8285 {
8286 switch (pdi->tag)
8287 {
8288 case DW_TAG_subprogram:
8289 case DW_TAG_inlined_subroutine:
8290 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8291 if (cu->language == language_cplus)
8292 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8293 set_addrmap, cu);
8294 break;
8295 case DW_TAG_constant:
8296 case DW_TAG_variable:
8297 case DW_TAG_typedef:
8298 case DW_TAG_union_type:
8299 if (!pdi->is_declaration
8300 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8301 {
8302 add_partial_symbol (pdi, cu);
8303 }
8304 break;
8305 case DW_TAG_class_type:
8306 case DW_TAG_interface_type:
8307 case DW_TAG_structure_type:
8308 if (!pdi->is_declaration)
8309 {
8310 add_partial_symbol (pdi, cu);
8311 }
8312 if ((cu->language == language_rust
8313 || cu->language == language_cplus) && pdi->has_children)
8314 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8315 set_addrmap, cu);
8316 break;
8317 case DW_TAG_enumeration_type:
8318 if (!pdi->is_declaration)
8319 add_partial_enumeration (pdi, cu);
8320 break;
8321 case DW_TAG_base_type:
8322 case DW_TAG_subrange_type:
8323 /* File scope base type definitions are added to the partial
8324 symbol table. */
8325 add_partial_symbol (pdi, cu);
8326 break;
8327 case DW_TAG_namespace:
8328 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8329 break;
8330 case DW_TAG_module:
8331 if (!pdi->is_declaration)
8332 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8333 break;
8334 case DW_TAG_imported_unit:
8335 {
8336 struct dwarf2_per_cu_data *per_cu;
8337
8338 /* For now we don't handle imported units in type units. */
8339 if (cu->per_cu->is_debug_types)
8340 {
8341 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8342 " supported in type units [in module %s]"),
8343 objfile_name (cu->per_objfile->objfile));
8344 }
8345
8346 per_cu = dwarf2_find_containing_comp_unit
8347 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8348
8349 /* Go read the partial unit, if needed. */
8350 if (per_cu->v.psymtab == NULL)
8351 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8352 cu->language);
8353
8354 cu->per_cu->imported_symtabs_push (per_cu);
8355 }
8356 break;
8357 case DW_TAG_imported_declaration:
8358 add_partial_symbol (pdi, cu);
8359 break;
8360 default:
8361 break;
8362 }
8363 }
8364
8365 /* If the die has a sibling, skip to the sibling. */
8366
8367 pdi = pdi->die_sibling;
8368 }
8369 }
8370
8371 /* Functions used to compute the fully scoped name of a partial DIE.
8372
8373 Normally, this is simple. For C++, the parent DIE's fully scoped
8374 name is concatenated with "::" and the partial DIE's name.
8375 Enumerators are an exception; they use the scope of their parent
8376 enumeration type, i.e. the name of the enumeration type is not
8377 prepended to the enumerator.
8378
8379 There are two complexities. One is DW_AT_specification; in this
8380 case "parent" means the parent of the target of the specification,
8381 instead of the direct parent of the DIE. The other is compilers
8382 which do not emit DW_TAG_namespace; in this case we try to guess
8383 the fully qualified name of structure types from their members'
8384 linkage names. This must be done using the DIE's children rather
8385 than the children of any DW_AT_specification target. We only need
8386 to do this for structures at the top level, i.e. if the target of
8387 any DW_AT_specification (if any; otherwise the DIE itself) does not
8388 have a parent. */
8389
8390 /* Compute the scope prefix associated with PDI's parent, in
8391 compilation unit CU. The result will be allocated on CU's
8392 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8393 field. NULL is returned if no prefix is necessary. */
8394 static const char *
8395 partial_die_parent_scope (struct partial_die_info *pdi,
8396 struct dwarf2_cu *cu)
8397 {
8398 const char *grandparent_scope;
8399 struct partial_die_info *parent, *real_pdi;
8400
8401 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8402 then this means the parent of the specification DIE. */
8403
8404 real_pdi = pdi;
8405 while (real_pdi->has_specification)
8406 {
8407 auto res = find_partial_die (real_pdi->spec_offset,
8408 real_pdi->spec_is_dwz, cu);
8409 real_pdi = res.pdi;
8410 cu = res.cu;
8411 }
8412
8413 parent = real_pdi->die_parent;
8414 if (parent == NULL)
8415 return NULL;
8416
8417 if (parent->scope_set)
8418 return parent->scope;
8419
8420 parent->fixup (cu);
8421
8422 grandparent_scope = partial_die_parent_scope (parent, cu);
8423
8424 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8425 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8426 Work around this problem here. */
8427 if (cu->language == language_cplus
8428 && parent->tag == DW_TAG_namespace
8429 && strcmp (parent->name (cu), "::") == 0
8430 && grandparent_scope == NULL)
8431 {
8432 parent->scope = NULL;
8433 parent->scope_set = 1;
8434 return NULL;
8435 }
8436
8437 /* Nested subroutines in Fortran get a prefix. */
8438 if (pdi->tag == DW_TAG_enumerator)
8439 /* Enumerators should not get the name of the enumeration as a prefix. */
8440 parent->scope = grandparent_scope;
8441 else if (parent->tag == DW_TAG_namespace
8442 || parent->tag == DW_TAG_module
8443 || parent->tag == DW_TAG_structure_type
8444 || parent->tag == DW_TAG_class_type
8445 || parent->tag == DW_TAG_interface_type
8446 || parent->tag == DW_TAG_union_type
8447 || parent->tag == DW_TAG_enumeration_type
8448 || (cu->language == language_fortran
8449 && parent->tag == DW_TAG_subprogram
8450 && pdi->tag == DW_TAG_subprogram))
8451 {
8452 if (grandparent_scope == NULL)
8453 parent->scope = parent->name (cu);
8454 else
8455 parent->scope = typename_concat (&cu->comp_unit_obstack,
8456 grandparent_scope,
8457 parent->name (cu), 0, cu);
8458 }
8459 else
8460 {
8461 /* FIXME drow/2004-04-01: What should we be doing with
8462 function-local names? For partial symbols, we should probably be
8463 ignoring them. */
8464 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8465 dwarf_tag_name (parent->tag),
8466 sect_offset_str (pdi->sect_off));
8467 parent->scope = grandparent_scope;
8468 }
8469
8470 parent->scope_set = 1;
8471 return parent->scope;
8472 }
8473
8474 /* Return the fully scoped name associated with PDI, from compilation unit
8475 CU. The result will be allocated with malloc. */
8476
8477 static gdb::unique_xmalloc_ptr<char>
8478 partial_die_full_name (struct partial_die_info *pdi,
8479 struct dwarf2_cu *cu)
8480 {
8481 const char *parent_scope;
8482
8483 /* If this is a template instantiation, we can not work out the
8484 template arguments from partial DIEs. So, unfortunately, we have
8485 to go through the full DIEs. At least any work we do building
8486 types here will be reused if full symbols are loaded later. */
8487 if (pdi->has_template_arguments)
8488 {
8489 pdi->fixup (cu);
8490
8491 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8492 {
8493 struct die_info *die;
8494 struct attribute attr;
8495 struct dwarf2_cu *ref_cu = cu;
8496
8497 /* DW_FORM_ref_addr is using section offset. */
8498 attr.name = (enum dwarf_attribute) 0;
8499 attr.form = DW_FORM_ref_addr;
8500 attr.u.unsnd = to_underlying (pdi->sect_off);
8501 die = follow_die_ref (NULL, &attr, &ref_cu);
8502
8503 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8504 }
8505 }
8506
8507 parent_scope = partial_die_parent_scope (pdi, cu);
8508 if (parent_scope == NULL)
8509 return NULL;
8510 else
8511 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8512 pdi->name (cu),
8513 0, cu));
8514 }
8515
8516 static void
8517 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8518 {
8519 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8520 struct objfile *objfile = per_objfile->objfile;
8521 struct gdbarch *gdbarch = objfile->arch ();
8522 CORE_ADDR addr = 0;
8523 const char *actual_name = NULL;
8524 CORE_ADDR baseaddr;
8525
8526 baseaddr = objfile->text_section_offset ();
8527
8528 gdb::unique_xmalloc_ptr<char> built_actual_name
8529 = partial_die_full_name (pdi, cu);
8530 if (built_actual_name != NULL)
8531 actual_name = built_actual_name.get ();
8532
8533 if (actual_name == NULL)
8534 actual_name = pdi->name (cu);
8535
8536 partial_symbol psymbol;
8537 memset (&psymbol, 0, sizeof (psymbol));
8538 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8539 psymbol.ginfo.set_section_index (-1);
8540
8541 /* The code below indicates that the psymbol should be installed by
8542 setting this. */
8543 gdb::optional<psymbol_placement> where;
8544
8545 switch (pdi->tag)
8546 {
8547 case DW_TAG_inlined_subroutine:
8548 case DW_TAG_subprogram:
8549 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8550 - baseaddr);
8551 if (pdi->is_external
8552 || cu->language == language_ada
8553 || (cu->language == language_fortran
8554 && pdi->die_parent != NULL
8555 && pdi->die_parent->tag == DW_TAG_subprogram))
8556 {
8557 /* Normally, only "external" DIEs are part of the global scope.
8558 But in Ada and Fortran, we want to be able to access nested
8559 procedures globally. So all Ada and Fortran subprograms are
8560 stored in the global scope. */
8561 where = psymbol_placement::GLOBAL;
8562 }
8563 else
8564 where = psymbol_placement::STATIC;
8565
8566 psymbol.domain = VAR_DOMAIN;
8567 psymbol.aclass = LOC_BLOCK;
8568 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8569 psymbol.ginfo.value.address = addr;
8570
8571 if (pdi->main_subprogram && actual_name != NULL)
8572 set_objfile_main_name (objfile, actual_name, cu->language);
8573 break;
8574 case DW_TAG_constant:
8575 psymbol.domain = VAR_DOMAIN;
8576 psymbol.aclass = LOC_STATIC;
8577 where = (pdi->is_external
8578 ? psymbol_placement::GLOBAL
8579 : psymbol_placement::STATIC);
8580 break;
8581 case DW_TAG_variable:
8582 if (pdi->d.locdesc)
8583 addr = decode_locdesc (pdi->d.locdesc, cu);
8584
8585 if (pdi->d.locdesc
8586 && addr == 0
8587 && !per_objfile->per_bfd->has_section_at_zero)
8588 {
8589 /* A global or static variable may also have been stripped
8590 out by the linker if unused, in which case its address
8591 will be nullified; do not add such variables into partial
8592 symbol table then. */
8593 }
8594 else if (pdi->is_external)
8595 {
8596 /* Global Variable.
8597 Don't enter into the minimal symbol tables as there is
8598 a minimal symbol table entry from the ELF symbols already.
8599 Enter into partial symbol table if it has a location
8600 descriptor or a type.
8601 If the location descriptor is missing, new_symbol will create
8602 a LOC_UNRESOLVED symbol, the address of the variable will then
8603 be determined from the minimal symbol table whenever the variable
8604 is referenced.
8605 The address for the partial symbol table entry is not
8606 used by GDB, but it comes in handy for debugging partial symbol
8607 table building. */
8608
8609 if (pdi->d.locdesc || pdi->has_type)
8610 {
8611 psymbol.domain = VAR_DOMAIN;
8612 psymbol.aclass = LOC_STATIC;
8613 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8614 psymbol.ginfo.value.address = addr;
8615 where = psymbol_placement::GLOBAL;
8616 }
8617 }
8618 else
8619 {
8620 int has_loc = pdi->d.locdesc != NULL;
8621
8622 /* Static Variable. Skip symbols whose value we cannot know (those
8623 without location descriptors or constant values). */
8624 if (!has_loc && !pdi->has_const_value)
8625 return;
8626
8627 psymbol.domain = VAR_DOMAIN;
8628 psymbol.aclass = LOC_STATIC;
8629 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8630 if (has_loc)
8631 psymbol.ginfo.value.address = addr;
8632 where = psymbol_placement::STATIC;
8633 }
8634 break;
8635 case DW_TAG_array_type:
8636 case DW_TAG_typedef:
8637 case DW_TAG_base_type:
8638 case DW_TAG_subrange_type:
8639 psymbol.domain = VAR_DOMAIN;
8640 psymbol.aclass = LOC_TYPEDEF;
8641 where = psymbol_placement::STATIC;
8642 break;
8643 case DW_TAG_imported_declaration:
8644 case DW_TAG_namespace:
8645 psymbol.domain = VAR_DOMAIN;
8646 psymbol.aclass = LOC_TYPEDEF;
8647 where = psymbol_placement::GLOBAL;
8648 break;
8649 case DW_TAG_module:
8650 /* With Fortran 77 there might be a "BLOCK DATA" module
8651 available without any name. If so, we skip the module as it
8652 doesn't bring any value. */
8653 if (actual_name != nullptr)
8654 {
8655 psymbol.domain = MODULE_DOMAIN;
8656 psymbol.aclass = LOC_TYPEDEF;
8657 where = psymbol_placement::GLOBAL;
8658 }
8659 break;
8660 case DW_TAG_class_type:
8661 case DW_TAG_interface_type:
8662 case DW_TAG_structure_type:
8663 case DW_TAG_union_type:
8664 case DW_TAG_enumeration_type:
8665 /* Skip external references. The DWARF standard says in the section
8666 about "Structure, Union, and Class Type Entries": "An incomplete
8667 structure, union or class type is represented by a structure,
8668 union or class entry that does not have a byte size attribute
8669 and that has a DW_AT_declaration attribute." */
8670 if (!pdi->has_byte_size && pdi->is_declaration)
8671 return;
8672
8673 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8674 static vs. global. */
8675 psymbol.domain = STRUCT_DOMAIN;
8676 psymbol.aclass = LOC_TYPEDEF;
8677 where = (cu->language == language_cplus
8678 ? psymbol_placement::GLOBAL
8679 : psymbol_placement::STATIC);
8680 break;
8681 case DW_TAG_enumerator:
8682 psymbol.domain = VAR_DOMAIN;
8683 psymbol.aclass = LOC_CONST;
8684 where = (cu->language == language_cplus
8685 ? psymbol_placement::GLOBAL
8686 : psymbol_placement::STATIC);
8687 break;
8688 default:
8689 break;
8690 }
8691
8692 if (where.has_value ())
8693 {
8694 if (built_actual_name != nullptr)
8695 actual_name = objfile->intern (actual_name);
8696 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8697 psymbol.ginfo.set_linkage_name (actual_name);
8698 else
8699 {
8700 psymbol.ginfo.set_demangled_name (actual_name,
8701 &objfile->objfile_obstack);
8702 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8703 }
8704 cu->per_cu->v.psymtab->add_psymbol
8705 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8706 objfile);
8707 }
8708 }
8709
8710 /* Read a partial die corresponding to a namespace; also, add a symbol
8711 corresponding to that namespace to the symbol table. NAMESPACE is
8712 the name of the enclosing namespace. */
8713
8714 static void
8715 add_partial_namespace (struct partial_die_info *pdi,
8716 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8717 int set_addrmap, struct dwarf2_cu *cu)
8718 {
8719 /* Add a symbol for the namespace. */
8720
8721 add_partial_symbol (pdi, cu);
8722
8723 /* Now scan partial symbols in that namespace. */
8724
8725 if (pdi->has_children)
8726 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8727 }
8728
8729 /* Read a partial die corresponding to a Fortran module. */
8730
8731 static void
8732 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8733 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8734 {
8735 /* Add a symbol for the namespace. */
8736
8737 add_partial_symbol (pdi, cu);
8738
8739 /* Now scan partial symbols in that module. */
8740
8741 if (pdi->has_children)
8742 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8743 }
8744
8745 /* Read a partial die corresponding to a subprogram or an inlined
8746 subprogram and create a partial symbol for that subprogram.
8747 When the CU language allows it, this routine also defines a partial
8748 symbol for each nested subprogram that this subprogram contains.
8749 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8750 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8751
8752 PDI may also be a lexical block, in which case we simply search
8753 recursively for subprograms defined inside that lexical block.
8754 Again, this is only performed when the CU language allows this
8755 type of definitions. */
8756
8757 static void
8758 add_partial_subprogram (struct partial_die_info *pdi,
8759 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8760 int set_addrmap, struct dwarf2_cu *cu)
8761 {
8762 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8763 {
8764 if (pdi->has_pc_info)
8765 {
8766 if (pdi->lowpc < *lowpc)
8767 *lowpc = pdi->lowpc;
8768 if (pdi->highpc > *highpc)
8769 *highpc = pdi->highpc;
8770 if (set_addrmap)
8771 {
8772 struct objfile *objfile = cu->per_objfile->objfile;
8773 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8774 struct gdbarch *gdbarch = objfile->arch ();
8775 CORE_ADDR baseaddr;
8776 CORE_ADDR this_highpc;
8777 CORE_ADDR this_lowpc;
8778
8779 baseaddr = objfile->text_section_offset ();
8780 this_lowpc
8781 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8782 pdi->lowpc + baseaddr)
8783 - baseaddr);
8784 this_highpc
8785 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8786 pdi->highpc + baseaddr)
8787 - baseaddr);
8788 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8789 this_lowpc, this_highpc - 1,
8790 cu->per_cu->v.psymtab);
8791 }
8792 }
8793
8794 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8795 {
8796 if (!pdi->is_declaration)
8797 /* Ignore subprogram DIEs that do not have a name, they are
8798 illegal. Do not emit a complaint at this point, we will
8799 do so when we convert this psymtab into a symtab. */
8800 if (pdi->name (cu))
8801 add_partial_symbol (pdi, cu);
8802 }
8803 }
8804
8805 if (! pdi->has_children)
8806 return;
8807
8808 if (cu->language == language_ada || cu->language == language_fortran)
8809 {
8810 pdi = pdi->die_child;
8811 while (pdi != NULL)
8812 {
8813 pdi->fixup (cu);
8814 if (pdi->tag == DW_TAG_subprogram
8815 || pdi->tag == DW_TAG_inlined_subroutine
8816 || pdi->tag == DW_TAG_lexical_block)
8817 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8818 pdi = pdi->die_sibling;
8819 }
8820 }
8821 }
8822
8823 /* Read a partial die corresponding to an enumeration type. */
8824
8825 static void
8826 add_partial_enumeration (struct partial_die_info *enum_pdi,
8827 struct dwarf2_cu *cu)
8828 {
8829 struct partial_die_info *pdi;
8830
8831 if (enum_pdi->name (cu) != NULL)
8832 add_partial_symbol (enum_pdi, cu);
8833
8834 pdi = enum_pdi->die_child;
8835 while (pdi)
8836 {
8837 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8838 complaint (_("malformed enumerator DIE ignored"));
8839 else
8840 add_partial_symbol (pdi, cu);
8841 pdi = pdi->die_sibling;
8842 }
8843 }
8844
8845 /* Return the initial uleb128 in the die at INFO_PTR. */
8846
8847 static unsigned int
8848 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8849 {
8850 unsigned int bytes_read;
8851
8852 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8853 }
8854
8855 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8856 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8857
8858 Return the corresponding abbrev, or NULL if the number is zero (indicating
8859 an empty DIE). In either case *BYTES_READ will be set to the length of
8860 the initial number. */
8861
8862 static const struct abbrev_info *
8863 peek_die_abbrev (const die_reader_specs &reader,
8864 const gdb_byte *info_ptr, unsigned int *bytes_read)
8865 {
8866 dwarf2_cu *cu = reader.cu;
8867 bfd *abfd = reader.abfd;
8868 unsigned int abbrev_number
8869 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8870
8871 if (abbrev_number == 0)
8872 return NULL;
8873
8874 const abbrev_info *abbrev
8875 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8876 if (!abbrev)
8877 {
8878 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8879 " at offset %s [in module %s]"),
8880 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8881 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8882 }
8883
8884 return abbrev;
8885 }
8886
8887 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8888 Returns a pointer to the end of a series of DIEs, terminated by an empty
8889 DIE. Any children of the skipped DIEs will also be skipped. */
8890
8891 static const gdb_byte *
8892 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8893 {
8894 while (1)
8895 {
8896 unsigned int bytes_read;
8897 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8898 &bytes_read);
8899
8900 if (abbrev == NULL)
8901 return info_ptr + bytes_read;
8902 else
8903 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8904 }
8905 }
8906
8907 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8908 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8909 abbrev corresponding to that skipped uleb128 should be passed in
8910 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8911 children. */
8912
8913 static const gdb_byte *
8914 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8915 const struct abbrev_info *abbrev)
8916 {
8917 unsigned int bytes_read;
8918 struct attribute attr;
8919 bfd *abfd = reader->abfd;
8920 struct dwarf2_cu *cu = reader->cu;
8921 const gdb_byte *buffer = reader->buffer;
8922 const gdb_byte *buffer_end = reader->buffer_end;
8923 unsigned int form, i;
8924
8925 for (i = 0; i < abbrev->num_attrs; i++)
8926 {
8927 /* The only abbrev we care about is DW_AT_sibling. */
8928 if (abbrev->attrs[i].name == DW_AT_sibling)
8929 {
8930 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8931 if (attr.form == DW_FORM_ref_addr)
8932 complaint (_("ignoring absolute DW_AT_sibling"));
8933 else
8934 {
8935 sect_offset off = attr.get_ref_die_offset ();
8936 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8937
8938 if (sibling_ptr < info_ptr)
8939 complaint (_("DW_AT_sibling points backwards"));
8940 else if (sibling_ptr > reader->buffer_end)
8941 reader->die_section->overflow_complaint ();
8942 else
8943 return sibling_ptr;
8944 }
8945 }
8946
8947 /* If it isn't DW_AT_sibling, skip this attribute. */
8948 form = abbrev->attrs[i].form;
8949 skip_attribute:
8950 switch (form)
8951 {
8952 case DW_FORM_ref_addr:
8953 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8954 and later it is offset sized. */
8955 if (cu->header.version == 2)
8956 info_ptr += cu->header.addr_size;
8957 else
8958 info_ptr += cu->header.offset_size;
8959 break;
8960 case DW_FORM_GNU_ref_alt:
8961 info_ptr += cu->header.offset_size;
8962 break;
8963 case DW_FORM_addr:
8964 info_ptr += cu->header.addr_size;
8965 break;
8966 case DW_FORM_data1:
8967 case DW_FORM_ref1:
8968 case DW_FORM_flag:
8969 case DW_FORM_strx1:
8970 info_ptr += 1;
8971 break;
8972 case DW_FORM_flag_present:
8973 case DW_FORM_implicit_const:
8974 break;
8975 case DW_FORM_data2:
8976 case DW_FORM_ref2:
8977 case DW_FORM_strx2:
8978 info_ptr += 2;
8979 break;
8980 case DW_FORM_strx3:
8981 info_ptr += 3;
8982 break;
8983 case DW_FORM_data4:
8984 case DW_FORM_ref4:
8985 case DW_FORM_strx4:
8986 info_ptr += 4;
8987 break;
8988 case DW_FORM_data8:
8989 case DW_FORM_ref8:
8990 case DW_FORM_ref_sig8:
8991 info_ptr += 8;
8992 break;
8993 case DW_FORM_data16:
8994 info_ptr += 16;
8995 break;
8996 case DW_FORM_string:
8997 read_direct_string (abfd, info_ptr, &bytes_read);
8998 info_ptr += bytes_read;
8999 break;
9000 case DW_FORM_sec_offset:
9001 case DW_FORM_strp:
9002 case DW_FORM_GNU_strp_alt:
9003 info_ptr += cu->header.offset_size;
9004 break;
9005 case DW_FORM_exprloc:
9006 case DW_FORM_block:
9007 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9008 info_ptr += bytes_read;
9009 break;
9010 case DW_FORM_block1:
9011 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9012 break;
9013 case DW_FORM_block2:
9014 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9015 break;
9016 case DW_FORM_block4:
9017 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9018 break;
9019 case DW_FORM_addrx:
9020 case DW_FORM_strx:
9021 case DW_FORM_sdata:
9022 case DW_FORM_udata:
9023 case DW_FORM_ref_udata:
9024 case DW_FORM_GNU_addr_index:
9025 case DW_FORM_GNU_str_index:
9026 case DW_FORM_rnglistx:
9027 case DW_FORM_loclistx:
9028 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9029 break;
9030 case DW_FORM_indirect:
9031 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9032 info_ptr += bytes_read;
9033 /* We need to continue parsing from here, so just go back to
9034 the top. */
9035 goto skip_attribute;
9036
9037 default:
9038 error (_("Dwarf Error: Cannot handle %s "
9039 "in DWARF reader [in module %s]"),
9040 dwarf_form_name (form),
9041 bfd_get_filename (abfd));
9042 }
9043 }
9044
9045 if (abbrev->has_children)
9046 return skip_children (reader, info_ptr);
9047 else
9048 return info_ptr;
9049 }
9050
9051 /* Locate ORIG_PDI's sibling.
9052 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9053
9054 static const gdb_byte *
9055 locate_pdi_sibling (const struct die_reader_specs *reader,
9056 struct partial_die_info *orig_pdi,
9057 const gdb_byte *info_ptr)
9058 {
9059 /* Do we know the sibling already? */
9060
9061 if (orig_pdi->sibling)
9062 return orig_pdi->sibling;
9063
9064 /* Are there any children to deal with? */
9065
9066 if (!orig_pdi->has_children)
9067 return info_ptr;
9068
9069 /* Skip the children the long way. */
9070
9071 return skip_children (reader, info_ptr);
9072 }
9073
9074 /* Expand this partial symbol table into a full symbol table. SELF is
9075 not NULL. */
9076
9077 void
9078 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9079 {
9080 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9081
9082 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9083
9084 /* If this psymtab is constructed from a debug-only objfile, the
9085 has_section_at_zero flag will not necessarily be correct. We
9086 can get the correct value for this flag by looking at the data
9087 associated with the (presumably stripped) associated objfile. */
9088 if (objfile->separate_debug_objfile_backlink)
9089 {
9090 dwarf2_per_objfile *per_objfile_backlink
9091 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9092
9093 per_objfile->per_bfd->has_section_at_zero
9094 = per_objfile_backlink->per_bfd->has_section_at_zero;
9095 }
9096
9097 expand_psymtab (objfile);
9098
9099 process_cu_includes (per_objfile);
9100 }
9101 \f
9102 /* Reading in full CUs. */
9103
9104 /* Add PER_CU to the queue. */
9105
9106 static void
9107 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9108 dwarf2_per_objfile *per_objfile,
9109 enum language pretend_language)
9110 {
9111 per_cu->queued = 1;
9112
9113 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9114 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
9115 }
9116
9117 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9118
9119 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9120 dependency.
9121
9122 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9123 DIEs, false otherwise.
9124
9125 Explanation: there is an invariant that if a CU is queued for expansion
9126 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9127 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9128 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9129 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9130 invariant is respected.
9131
9132 The caller is therefore not required to load the CU's DIEs (we return false)
9133 if:
9134
9135 - the CU is already expanded, and therefore does not get enqueued
9136 - the CU gets enqueued for expansion, but its DIEs are already loaded
9137
9138 Note that the caller should not use this function's return value as an
9139 indicator of whether the CU's DIEs are loaded right now, it should check
9140 that by calling `dwarf2_per_objfile::get_cu` instead. */
9141
9142 static int
9143 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9144 dwarf2_per_cu_data *per_cu,
9145 dwarf2_per_objfile *per_objfile,
9146 enum language pretend_language)
9147 {
9148 /* We may arrive here during partial symbol reading, if we need full
9149 DIEs to process an unusual case (e.g. template arguments). Do
9150 not queue PER_CU, just tell our caller to load its DIEs. */
9151 if (per_cu->per_bfd->reading_partial_symbols)
9152 {
9153 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9154
9155 if (cu == NULL || cu->dies == NULL)
9156 return 1;
9157 return 0;
9158 }
9159
9160 /* Mark the dependence relation so that we don't flush PER_CU
9161 too early. */
9162 if (dependent_cu != NULL)
9163 dwarf2_add_dependence (dependent_cu, per_cu);
9164
9165 /* If it's already on the queue, we have nothing to do. */
9166 if (per_cu->queued)
9167 {
9168 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9169 loaded. */
9170 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9171
9172 /* If the CU is queued for expansion, it should not already be
9173 expanded. */
9174 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9175
9176 /* The DIEs are already loaded, the caller doesn't need to do it. */
9177 return 0;
9178 }
9179
9180 bool queued = false;
9181 if (!per_objfile->symtab_set_p (per_cu))
9182 {
9183 /* Add it to the queue. */
9184 queue_comp_unit (per_cu, per_objfile, pretend_language);
9185 queued = true;
9186 }
9187
9188 /* If the compilation unit is already loaded, just mark it as
9189 used. */
9190 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9191 if (cu != nullptr)
9192 cu->last_used = 0;
9193
9194 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9195 and the DIEs are not already loaded. */
9196 return queued && cu == nullptr;
9197 }
9198
9199 /* Process the queue. */
9200
9201 static void
9202 process_queue (dwarf2_per_objfile *per_objfile)
9203 {
9204 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9205 objfile_name (per_objfile->objfile));
9206
9207 /* The queue starts out with one item, but following a DIE reference
9208 may load a new CU, adding it to the end of the queue. */
9209 while (!per_objfile->per_bfd->queue->empty ())
9210 {
9211 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
9212 dwarf2_per_cu_data *per_cu = item.per_cu;
9213
9214 if (!per_objfile->symtab_set_p (per_cu))
9215 {
9216 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9217
9218 /* Skip dummy CUs. */
9219 if (cu != nullptr)
9220 {
9221 unsigned int debug_print_threshold;
9222 char buf[100];
9223
9224 if (per_cu->is_debug_types)
9225 {
9226 struct signatured_type *sig_type =
9227 (struct signatured_type *) per_cu;
9228
9229 sprintf (buf, "TU %s at offset %s",
9230 hex_string (sig_type->signature),
9231 sect_offset_str (per_cu->sect_off));
9232 /* There can be 100s of TUs.
9233 Only print them in verbose mode. */
9234 debug_print_threshold = 2;
9235 }
9236 else
9237 {
9238 sprintf (buf, "CU at offset %s",
9239 sect_offset_str (per_cu->sect_off));
9240 debug_print_threshold = 1;
9241 }
9242
9243 if (dwarf_read_debug >= debug_print_threshold)
9244 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9245
9246 if (per_cu->is_debug_types)
9247 process_full_type_unit (cu, item.pretend_language);
9248 else
9249 process_full_comp_unit (cu, item.pretend_language);
9250
9251 if (dwarf_read_debug >= debug_print_threshold)
9252 dwarf_read_debug_printf ("Done expanding %s", buf);
9253 }
9254 }
9255
9256 per_cu->queued = 0;
9257 per_objfile->per_bfd->queue->pop ();
9258 }
9259
9260 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9261 objfile_name (per_objfile->objfile));
9262 }
9263
9264 /* Read in full symbols for PST, and anything it depends on. */
9265
9266 void
9267 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9268 {
9269 gdb_assert (!readin_p (objfile));
9270
9271 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9272 free_cached_comp_units freer (per_objfile);
9273 expand_dependencies (objfile);
9274
9275 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9276 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9277 }
9278
9279 /* See psympriv.h. */
9280
9281 bool
9282 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9283 {
9284 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9285 return per_objfile->symtab_set_p (per_cu_data);
9286 }
9287
9288 /* See psympriv.h. */
9289
9290 compunit_symtab *
9291 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9292 {
9293 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9294 return per_objfile->get_symtab (per_cu_data);
9295 }
9296
9297 /* Trivial hash function for die_info: the hash value of a DIE
9298 is its offset in .debug_info for this objfile. */
9299
9300 static hashval_t
9301 die_hash (const void *item)
9302 {
9303 const struct die_info *die = (const struct die_info *) item;
9304
9305 return to_underlying (die->sect_off);
9306 }
9307
9308 /* Trivial comparison function for die_info structures: two DIEs
9309 are equal if they have the same offset. */
9310
9311 static int
9312 die_eq (const void *item_lhs, const void *item_rhs)
9313 {
9314 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9315 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9316
9317 return die_lhs->sect_off == die_rhs->sect_off;
9318 }
9319
9320 /* Load the DIEs associated with PER_CU into memory.
9321
9322 In some cases, the caller, while reading partial symbols, will need to load
9323 the full symbols for the CU for some reason. It will already have a
9324 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9325 rather than creating a new one. */
9326
9327 static void
9328 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9329 dwarf2_per_objfile *per_objfile,
9330 dwarf2_cu *existing_cu,
9331 bool skip_partial,
9332 enum language pretend_language)
9333 {
9334 gdb_assert (! this_cu->is_debug_types);
9335
9336 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9337 if (reader.dummy_p)
9338 return;
9339
9340 struct dwarf2_cu *cu = reader.cu;
9341 const gdb_byte *info_ptr = reader.info_ptr;
9342
9343 gdb_assert (cu->die_hash == NULL);
9344 cu->die_hash =
9345 htab_create_alloc_ex (cu->header.length / 12,
9346 die_hash,
9347 die_eq,
9348 NULL,
9349 &cu->comp_unit_obstack,
9350 hashtab_obstack_allocate,
9351 dummy_obstack_deallocate);
9352
9353 if (reader.comp_unit_die->has_children)
9354 reader.comp_unit_die->child
9355 = read_die_and_siblings (&reader, reader.info_ptr,
9356 &info_ptr, reader.comp_unit_die);
9357 cu->dies = reader.comp_unit_die;
9358 /* comp_unit_die is not stored in die_hash, no need. */
9359
9360 /* We try not to read any attributes in this function, because not
9361 all CUs needed for references have been loaded yet, and symbol
9362 table processing isn't initialized. But we have to set the CU language,
9363 or we won't be able to build types correctly.
9364 Similarly, if we do not read the producer, we can not apply
9365 producer-specific interpretation. */
9366 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9367
9368 reader.keep ();
9369 }
9370
9371 /* Add a DIE to the delayed physname list. */
9372
9373 static void
9374 add_to_method_list (struct type *type, int fnfield_index, int index,
9375 const char *name, struct die_info *die,
9376 struct dwarf2_cu *cu)
9377 {
9378 struct delayed_method_info mi;
9379 mi.type = type;
9380 mi.fnfield_index = fnfield_index;
9381 mi.index = index;
9382 mi.name = name;
9383 mi.die = die;
9384 cu->method_list.push_back (mi);
9385 }
9386
9387 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9388 "const" / "volatile". If so, decrements LEN by the length of the
9389 modifier and return true. Otherwise return false. */
9390
9391 template<size_t N>
9392 static bool
9393 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9394 {
9395 size_t mod_len = sizeof (mod) - 1;
9396 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9397 {
9398 len -= mod_len;
9399 return true;
9400 }
9401 return false;
9402 }
9403
9404 /* Compute the physnames of any methods on the CU's method list.
9405
9406 The computation of method physnames is delayed in order to avoid the
9407 (bad) condition that one of the method's formal parameters is of an as yet
9408 incomplete type. */
9409
9410 static void
9411 compute_delayed_physnames (struct dwarf2_cu *cu)
9412 {
9413 /* Only C++ delays computing physnames. */
9414 if (cu->method_list.empty ())
9415 return;
9416 gdb_assert (cu->language == language_cplus);
9417
9418 for (const delayed_method_info &mi : cu->method_list)
9419 {
9420 const char *physname;
9421 struct fn_fieldlist *fn_flp
9422 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9423 physname = dwarf2_physname (mi.name, mi.die, cu);
9424 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9425 = physname ? physname : "";
9426
9427 /* Since there's no tag to indicate whether a method is a
9428 const/volatile overload, extract that information out of the
9429 demangled name. */
9430 if (physname != NULL)
9431 {
9432 size_t len = strlen (physname);
9433
9434 while (1)
9435 {
9436 if (physname[len] == ')') /* shortcut */
9437 break;
9438 else if (check_modifier (physname, len, " const"))
9439 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9440 else if (check_modifier (physname, len, " volatile"))
9441 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9442 else
9443 break;
9444 }
9445 }
9446 }
9447
9448 /* The list is no longer needed. */
9449 cu->method_list.clear ();
9450 }
9451
9452 /* Go objects should be embedded in a DW_TAG_module DIE,
9453 and it's not clear if/how imported objects will appear.
9454 To keep Go support simple until that's worked out,
9455 go back through what we've read and create something usable.
9456 We could do this while processing each DIE, and feels kinda cleaner,
9457 but that way is more invasive.
9458 This is to, for example, allow the user to type "p var" or "b main"
9459 without having to specify the package name, and allow lookups
9460 of module.object to work in contexts that use the expression
9461 parser. */
9462
9463 static void
9464 fixup_go_packaging (struct dwarf2_cu *cu)
9465 {
9466 gdb::unique_xmalloc_ptr<char> package_name;
9467 struct pending *list;
9468 int i;
9469
9470 for (list = *cu->get_builder ()->get_global_symbols ();
9471 list != NULL;
9472 list = list->next)
9473 {
9474 for (i = 0; i < list->nsyms; ++i)
9475 {
9476 struct symbol *sym = list->symbol[i];
9477
9478 if (sym->language () == language_go
9479 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9480 {
9481 gdb::unique_xmalloc_ptr<char> this_package_name
9482 (go_symbol_package_name (sym));
9483
9484 if (this_package_name == NULL)
9485 continue;
9486 if (package_name == NULL)
9487 package_name = std::move (this_package_name);
9488 else
9489 {
9490 struct objfile *objfile = cu->per_objfile->objfile;
9491 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9492 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9493 (symbol_symtab (sym) != NULL
9494 ? symtab_to_filename_for_display
9495 (symbol_symtab (sym))
9496 : objfile_name (objfile)),
9497 this_package_name.get (), package_name.get ());
9498 }
9499 }
9500 }
9501 }
9502
9503 if (package_name != NULL)
9504 {
9505 struct objfile *objfile = cu->per_objfile->objfile;
9506 const char *saved_package_name = objfile->intern (package_name.get ());
9507 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9508 saved_package_name);
9509 struct symbol *sym;
9510
9511 sym = new (&objfile->objfile_obstack) symbol;
9512 sym->set_language (language_go, &objfile->objfile_obstack);
9513 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9514 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9515 e.g., "main" finds the "main" module and not C's main(). */
9516 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9517 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9518 SYMBOL_TYPE (sym) = type;
9519
9520 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9521 }
9522 }
9523
9524 /* Allocate a fully-qualified name consisting of the two parts on the
9525 obstack. */
9526
9527 static const char *
9528 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9529 {
9530 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9531 }
9532
9533 /* A helper that allocates a variant part to attach to a Rust enum
9534 type. OBSTACK is where the results should be allocated. TYPE is
9535 the type we're processing. DISCRIMINANT_INDEX is the index of the
9536 discriminant. It must be the index of one of the fields of TYPE,
9537 or -1 to mean there is no discriminant (univariant enum).
9538 DEFAULT_INDEX is the index of the default field; or -1 if there is
9539 no default. RANGES is indexed by "effective" field number (the
9540 field index, but omitting the discriminant and default fields) and
9541 must hold the discriminant values used by the variants. Note that
9542 RANGES must have a lifetime at least as long as OBSTACK -- either
9543 already allocated on it, or static. */
9544
9545 static void
9546 alloc_rust_variant (struct obstack *obstack, struct type *type,
9547 int discriminant_index, int default_index,
9548 gdb::array_view<discriminant_range> ranges)
9549 {
9550 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9551 gdb_assert (discriminant_index == -1
9552 || (discriminant_index >= 0
9553 && discriminant_index < type->num_fields ()));
9554 gdb_assert (default_index == -1
9555 || (default_index >= 0 && default_index < type->num_fields ()));
9556
9557 /* We have one variant for each non-discriminant field. */
9558 int n_variants = type->num_fields ();
9559 if (discriminant_index != -1)
9560 --n_variants;
9561
9562 variant *variants = new (obstack) variant[n_variants];
9563 int var_idx = 0;
9564 int range_idx = 0;
9565 for (int i = 0; i < type->num_fields (); ++i)
9566 {
9567 if (i == discriminant_index)
9568 continue;
9569
9570 variants[var_idx].first_field = i;
9571 variants[var_idx].last_field = i + 1;
9572
9573 /* The default field does not need a range, but other fields do.
9574 We skipped the discriminant above. */
9575 if (i != default_index)
9576 {
9577 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9578 ++range_idx;
9579 }
9580
9581 ++var_idx;
9582 }
9583
9584 gdb_assert (range_idx == ranges.size ());
9585 gdb_assert (var_idx == n_variants);
9586
9587 variant_part *part = new (obstack) variant_part;
9588 part->discriminant_index = discriminant_index;
9589 /* If there is no discriminant, then whether it is signed is of no
9590 consequence. */
9591 part->is_unsigned
9592 = (discriminant_index == -1
9593 ? false
9594 : type->field (discriminant_index).type ()->is_unsigned ());
9595 part->variants = gdb::array_view<variant> (variants, n_variants);
9596
9597 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9598 gdb::array_view<variant_part> *prop_value
9599 = new (storage) gdb::array_view<variant_part> (part, 1);
9600
9601 struct dynamic_prop prop;
9602 prop.set_variant_parts (prop_value);
9603
9604 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9605 }
9606
9607 /* Some versions of rustc emitted enums in an unusual way.
9608
9609 Ordinary enums were emitted as unions. The first element of each
9610 structure in the union was named "RUST$ENUM$DISR". This element
9611 held the discriminant.
9612
9613 These versions of Rust also implemented the "non-zero"
9614 optimization. When the enum had two values, and one is empty and
9615 the other holds a pointer that cannot be zero, the pointer is used
9616 as the discriminant, with a zero value meaning the empty variant.
9617 Here, the union's first member is of the form
9618 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9619 where the fieldnos are the indices of the fields that should be
9620 traversed in order to find the field (which may be several fields deep)
9621 and the variantname is the name of the variant of the case when the
9622 field is zero.
9623
9624 This function recognizes whether TYPE is of one of these forms,
9625 and, if so, smashes it to be a variant type. */
9626
9627 static void
9628 quirk_rust_enum (struct type *type, struct objfile *objfile)
9629 {
9630 gdb_assert (type->code () == TYPE_CODE_UNION);
9631
9632 /* We don't need to deal with empty enums. */
9633 if (type->num_fields () == 0)
9634 return;
9635
9636 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9637 if (type->num_fields () == 1
9638 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9639 {
9640 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9641
9642 /* Decode the field name to find the offset of the
9643 discriminant. */
9644 ULONGEST bit_offset = 0;
9645 struct type *field_type = type->field (0).type ();
9646 while (name[0] >= '0' && name[0] <= '9')
9647 {
9648 char *tail;
9649 unsigned long index = strtoul (name, &tail, 10);
9650 name = tail;
9651 if (*name != '$'
9652 || index >= field_type->num_fields ()
9653 || (TYPE_FIELD_LOC_KIND (field_type, index)
9654 != FIELD_LOC_KIND_BITPOS))
9655 {
9656 complaint (_("Could not parse Rust enum encoding string \"%s\""
9657 "[in module %s]"),
9658 TYPE_FIELD_NAME (type, 0),
9659 objfile_name (objfile));
9660 return;
9661 }
9662 ++name;
9663
9664 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9665 field_type = field_type->field (index).type ();
9666 }
9667
9668 /* Smash this type to be a structure type. We have to do this
9669 because the type has already been recorded. */
9670 type->set_code (TYPE_CODE_STRUCT);
9671 type->set_num_fields (3);
9672 /* Save the field we care about. */
9673 struct field saved_field = type->field (0);
9674 type->set_fields
9675 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9676
9677 /* Put the discriminant at index 0. */
9678 type->field (0).set_type (field_type);
9679 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9680 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9681 SET_FIELD_BITPOS (type->field (0), bit_offset);
9682
9683 /* The order of fields doesn't really matter, so put the real
9684 field at index 1 and the data-less field at index 2. */
9685 type->field (1) = saved_field;
9686 TYPE_FIELD_NAME (type, 1)
9687 = rust_last_path_segment (type->field (1).type ()->name ());
9688 type->field (1).type ()->set_name
9689 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9690 TYPE_FIELD_NAME (type, 1)));
9691
9692 const char *dataless_name
9693 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9694 name);
9695 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9696 dataless_name);
9697 type->field (2).set_type (dataless_type);
9698 /* NAME points into the original discriminant name, which
9699 already has the correct lifetime. */
9700 TYPE_FIELD_NAME (type, 2) = name;
9701 SET_FIELD_BITPOS (type->field (2), 0);
9702
9703 /* Indicate that this is a variant type. */
9704 static discriminant_range ranges[1] = { { 0, 0 } };
9705 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9706 }
9707 /* A union with a single anonymous field is probably an old-style
9708 univariant enum. */
9709 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9710 {
9711 /* Smash this type to be a structure type. We have to do this
9712 because the type has already been recorded. */
9713 type->set_code (TYPE_CODE_STRUCT);
9714
9715 struct type *field_type = type->field (0).type ();
9716 const char *variant_name
9717 = rust_last_path_segment (field_type->name ());
9718 TYPE_FIELD_NAME (type, 0) = variant_name;
9719 field_type->set_name
9720 (rust_fully_qualify (&objfile->objfile_obstack,
9721 type->name (), variant_name));
9722
9723 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9724 }
9725 else
9726 {
9727 struct type *disr_type = nullptr;
9728 for (int i = 0; i < type->num_fields (); ++i)
9729 {
9730 disr_type = type->field (i).type ();
9731
9732 if (disr_type->code () != TYPE_CODE_STRUCT)
9733 {
9734 /* All fields of a true enum will be structs. */
9735 return;
9736 }
9737 else if (disr_type->num_fields () == 0)
9738 {
9739 /* Could be data-less variant, so keep going. */
9740 disr_type = nullptr;
9741 }
9742 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9743 "RUST$ENUM$DISR") != 0)
9744 {
9745 /* Not a Rust enum. */
9746 return;
9747 }
9748 else
9749 {
9750 /* Found one. */
9751 break;
9752 }
9753 }
9754
9755 /* If we got here without a discriminant, then it's probably
9756 just a union. */
9757 if (disr_type == nullptr)
9758 return;
9759
9760 /* Smash this type to be a structure type. We have to do this
9761 because the type has already been recorded. */
9762 type->set_code (TYPE_CODE_STRUCT);
9763
9764 /* Make space for the discriminant field. */
9765 struct field *disr_field = &disr_type->field (0);
9766 field *new_fields
9767 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9768 * sizeof (struct field)));
9769 memcpy (new_fields + 1, type->fields (),
9770 type->num_fields () * sizeof (struct field));
9771 type->set_fields (new_fields);
9772 type->set_num_fields (type->num_fields () + 1);
9773
9774 /* Install the discriminant at index 0 in the union. */
9775 type->field (0) = *disr_field;
9776 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9777 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9778
9779 /* We need a way to find the correct discriminant given a
9780 variant name. For convenience we build a map here. */
9781 struct type *enum_type = disr_field->type ();
9782 std::unordered_map<std::string, ULONGEST> discriminant_map;
9783 for (int i = 0; i < enum_type->num_fields (); ++i)
9784 {
9785 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9786 {
9787 const char *name
9788 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9789 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9790 }
9791 }
9792
9793 int n_fields = type->num_fields ();
9794 /* We don't need a range entry for the discriminant, but we do
9795 need one for every other field, as there is no default
9796 variant. */
9797 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9798 discriminant_range,
9799 n_fields - 1);
9800 /* Skip the discriminant here. */
9801 for (int i = 1; i < n_fields; ++i)
9802 {
9803 /* Find the final word in the name of this variant's type.
9804 That name can be used to look up the correct
9805 discriminant. */
9806 const char *variant_name
9807 = rust_last_path_segment (type->field (i).type ()->name ());
9808
9809 auto iter = discriminant_map.find (variant_name);
9810 if (iter != discriminant_map.end ())
9811 {
9812 ranges[i - 1].low = iter->second;
9813 ranges[i - 1].high = iter->second;
9814 }
9815
9816 /* In Rust, each element should have the size of the
9817 enclosing enum. */
9818 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9819
9820 /* Remove the discriminant field, if it exists. */
9821 struct type *sub_type = type->field (i).type ();
9822 if (sub_type->num_fields () > 0)
9823 {
9824 sub_type->set_num_fields (sub_type->num_fields () - 1);
9825 sub_type->set_fields (sub_type->fields () + 1);
9826 }
9827 TYPE_FIELD_NAME (type, i) = variant_name;
9828 sub_type->set_name
9829 (rust_fully_qualify (&objfile->objfile_obstack,
9830 type->name (), variant_name));
9831 }
9832
9833 /* Indicate that this is a variant type. */
9834 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9835 gdb::array_view<discriminant_range> (ranges,
9836 n_fields - 1));
9837 }
9838 }
9839
9840 /* Rewrite some Rust unions to be structures with variants parts. */
9841
9842 static void
9843 rust_union_quirks (struct dwarf2_cu *cu)
9844 {
9845 gdb_assert (cu->language == language_rust);
9846 for (type *type_ : cu->rust_unions)
9847 quirk_rust_enum (type_, cu->per_objfile->objfile);
9848 /* We don't need this any more. */
9849 cu->rust_unions.clear ();
9850 }
9851
9852 /* See read.h. */
9853
9854 type_unit_group_unshareable *
9855 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9856 {
9857 auto iter = this->m_type_units.find (tu_group);
9858 if (iter != this->m_type_units.end ())
9859 return iter->second.get ();
9860
9861 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9862 type_unit_group_unshareable *result = uniq.get ();
9863 this->m_type_units[tu_group] = std::move (uniq);
9864 return result;
9865 }
9866
9867 struct type *
9868 dwarf2_per_objfile::get_type_for_signatured_type
9869 (signatured_type *sig_type) const
9870 {
9871 auto iter = this->m_type_map.find (sig_type);
9872 if (iter == this->m_type_map.end ())
9873 return nullptr;
9874
9875 return iter->second;
9876 }
9877
9878 void dwarf2_per_objfile::set_type_for_signatured_type
9879 (signatured_type *sig_type, struct type *type)
9880 {
9881 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9882
9883 this->m_type_map[sig_type] = type;
9884 }
9885
9886 /* A helper function for computing the list of all symbol tables
9887 included by PER_CU. */
9888
9889 static void
9890 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9891 htab_t all_children, htab_t all_type_symtabs,
9892 dwarf2_per_cu_data *per_cu,
9893 dwarf2_per_objfile *per_objfile,
9894 struct compunit_symtab *immediate_parent)
9895 {
9896 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9897 if (*slot != NULL)
9898 {
9899 /* This inclusion and its children have been processed. */
9900 return;
9901 }
9902
9903 *slot = per_cu;
9904
9905 /* Only add a CU if it has a symbol table. */
9906 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9907 if (cust != NULL)
9908 {
9909 /* If this is a type unit only add its symbol table if we haven't
9910 seen it yet (type unit per_cu's can share symtabs). */
9911 if (per_cu->is_debug_types)
9912 {
9913 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9914 if (*slot == NULL)
9915 {
9916 *slot = cust;
9917 result->push_back (cust);
9918 if (cust->user == NULL)
9919 cust->user = immediate_parent;
9920 }
9921 }
9922 else
9923 {
9924 result->push_back (cust);
9925 if (cust->user == NULL)
9926 cust->user = immediate_parent;
9927 }
9928 }
9929
9930 if (!per_cu->imported_symtabs_empty ())
9931 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9932 {
9933 recursively_compute_inclusions (result, all_children,
9934 all_type_symtabs, ptr, per_objfile,
9935 cust);
9936 }
9937 }
9938
9939 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9940 PER_CU. */
9941
9942 static void
9943 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9944 dwarf2_per_objfile *per_objfile)
9945 {
9946 gdb_assert (! per_cu->is_debug_types);
9947
9948 if (!per_cu->imported_symtabs_empty ())
9949 {
9950 int len;
9951 std::vector<compunit_symtab *> result_symtabs;
9952 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9953
9954 /* If we don't have a symtab, we can just skip this case. */
9955 if (cust == NULL)
9956 return;
9957
9958 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9959 htab_eq_pointer,
9960 NULL, xcalloc, xfree));
9961 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9962 htab_eq_pointer,
9963 NULL, xcalloc, xfree));
9964
9965 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9966 {
9967 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9968 all_type_symtabs.get (), ptr,
9969 per_objfile, cust);
9970 }
9971
9972 /* Now we have a transitive closure of all the included symtabs. */
9973 len = result_symtabs.size ();
9974 cust->includes
9975 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9976 struct compunit_symtab *, len + 1);
9977 memcpy (cust->includes, result_symtabs.data (),
9978 len * sizeof (compunit_symtab *));
9979 cust->includes[len] = NULL;
9980 }
9981 }
9982
9983 /* Compute the 'includes' field for the symtabs of all the CUs we just
9984 read. */
9985
9986 static void
9987 process_cu_includes (dwarf2_per_objfile *per_objfile)
9988 {
9989 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9990 {
9991 if (! iter->is_debug_types)
9992 compute_compunit_symtab_includes (iter, per_objfile);
9993 }
9994
9995 per_objfile->per_bfd->just_read_cus.clear ();
9996 }
9997
9998 /* Generate full symbol information for CU, whose DIEs have
9999 already been loaded into memory. */
10000
10001 static void
10002 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10003 {
10004 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10005 struct objfile *objfile = per_objfile->objfile;
10006 struct gdbarch *gdbarch = objfile->arch ();
10007 CORE_ADDR lowpc, highpc;
10008 struct compunit_symtab *cust;
10009 CORE_ADDR baseaddr;
10010 struct block *static_block;
10011 CORE_ADDR addr;
10012
10013 baseaddr = objfile->text_section_offset ();
10014
10015 /* Clear the list here in case something was left over. */
10016 cu->method_list.clear ();
10017
10018 cu->language = pretend_language;
10019 cu->language_defn = language_def (cu->language);
10020
10021 dwarf2_find_base_address (cu->dies, cu);
10022
10023 /* Before we start reading the top-level DIE, ensure it has a valid tag
10024 type. */
10025 switch (cu->dies->tag)
10026 {
10027 case DW_TAG_compile_unit:
10028 case DW_TAG_partial_unit:
10029 case DW_TAG_type_unit:
10030 break;
10031 default:
10032 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
10033 dwarf_tag_name (cu->dies->tag),
10034 sect_offset_str (cu->per_cu->sect_off),
10035 objfile_name (per_objfile->objfile));
10036 }
10037
10038 /* Do line number decoding in read_file_scope () */
10039 process_die (cu->dies, cu);
10040
10041 /* For now fudge the Go package. */
10042 if (cu->language == language_go)
10043 fixup_go_packaging (cu);
10044
10045 /* Now that we have processed all the DIEs in the CU, all the types
10046 should be complete, and it should now be safe to compute all of the
10047 physnames. */
10048 compute_delayed_physnames (cu);
10049
10050 if (cu->language == language_rust)
10051 rust_union_quirks (cu);
10052
10053 /* Some compilers don't define a DW_AT_high_pc attribute for the
10054 compilation unit. If the DW_AT_high_pc is missing, synthesize
10055 it, by scanning the DIE's below the compilation unit. */
10056 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10057
10058 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10059 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10060
10061 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10062 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10063 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10064 addrmap to help ensure it has an accurate map of pc values belonging to
10065 this comp unit. */
10066 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10067
10068 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10069 SECT_OFF_TEXT (objfile),
10070 0);
10071
10072 if (cust != NULL)
10073 {
10074 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10075
10076 /* Set symtab language to language from DW_AT_language. If the
10077 compilation is from a C file generated by language preprocessors, do
10078 not set the language if it was already deduced by start_subfile. */
10079 if (!(cu->language == language_c
10080 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10081 COMPUNIT_FILETABS (cust)->language = cu->language;
10082
10083 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10084 produce DW_AT_location with location lists but it can be possibly
10085 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10086 there were bugs in prologue debug info, fixed later in GCC-4.5
10087 by "unwind info for epilogues" patch (which is not directly related).
10088
10089 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10090 needed, it would be wrong due to missing DW_AT_producer there.
10091
10092 Still one can confuse GDB by using non-standard GCC compilation
10093 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10094 */
10095 if (cu->has_loclist && gcc_4_minor >= 5)
10096 cust->locations_valid = 1;
10097
10098 if (gcc_4_minor >= 5)
10099 cust->epilogue_unwind_valid = 1;
10100
10101 cust->call_site_htab = cu->call_site_htab;
10102 }
10103
10104 per_objfile->set_symtab (cu->per_cu, cust);
10105
10106 /* Push it for inclusion processing later. */
10107 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10108
10109 /* Not needed any more. */
10110 cu->reset_builder ();
10111 }
10112
10113 /* Generate full symbol information for type unit CU, whose DIEs have
10114 already been loaded into memory. */
10115
10116 static void
10117 process_full_type_unit (dwarf2_cu *cu,
10118 enum language pretend_language)
10119 {
10120 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10121 struct objfile *objfile = per_objfile->objfile;
10122 struct compunit_symtab *cust;
10123 struct signatured_type *sig_type;
10124
10125 gdb_assert (cu->per_cu->is_debug_types);
10126 sig_type = (struct signatured_type *) cu->per_cu;
10127
10128 /* Clear the list here in case something was left over. */
10129 cu->method_list.clear ();
10130
10131 cu->language = pretend_language;
10132 cu->language_defn = language_def (cu->language);
10133
10134 /* The symbol tables are set up in read_type_unit_scope. */
10135 process_die (cu->dies, cu);
10136
10137 /* For now fudge the Go package. */
10138 if (cu->language == language_go)
10139 fixup_go_packaging (cu);
10140
10141 /* Now that we have processed all the DIEs in the CU, all the types
10142 should be complete, and it should now be safe to compute all of the
10143 physnames. */
10144 compute_delayed_physnames (cu);
10145
10146 if (cu->language == language_rust)
10147 rust_union_quirks (cu);
10148
10149 /* TUs share symbol tables.
10150 If this is the first TU to use this symtab, complete the construction
10151 of it with end_expandable_symtab. Otherwise, complete the addition of
10152 this TU's symbols to the existing symtab. */
10153 type_unit_group_unshareable *tug_unshare =
10154 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10155 if (tug_unshare->compunit_symtab == NULL)
10156 {
10157 buildsym_compunit *builder = cu->get_builder ();
10158 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10159 tug_unshare->compunit_symtab = cust;
10160
10161 if (cust != NULL)
10162 {
10163 /* Set symtab language to language from DW_AT_language. If the
10164 compilation is from a C file generated by language preprocessors,
10165 do not set the language if it was already deduced by
10166 start_subfile. */
10167 if (!(cu->language == language_c
10168 && COMPUNIT_FILETABS (cust)->language != language_c))
10169 COMPUNIT_FILETABS (cust)->language = cu->language;
10170 }
10171 }
10172 else
10173 {
10174 cu->get_builder ()->augment_type_symtab ();
10175 cust = tug_unshare->compunit_symtab;
10176 }
10177
10178 per_objfile->set_symtab (cu->per_cu, cust);
10179
10180 /* Not needed any more. */
10181 cu->reset_builder ();
10182 }
10183
10184 /* Process an imported unit DIE. */
10185
10186 static void
10187 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10188 {
10189 struct attribute *attr;
10190
10191 /* For now we don't handle imported units in type units. */
10192 if (cu->per_cu->is_debug_types)
10193 {
10194 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10195 " supported in type units [in module %s]"),
10196 objfile_name (cu->per_objfile->objfile));
10197 }
10198
10199 attr = dwarf2_attr (die, DW_AT_import, cu);
10200 if (attr != NULL)
10201 {
10202 sect_offset sect_off = attr->get_ref_die_offset ();
10203 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10204 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10205 dwarf2_per_cu_data *per_cu
10206 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10207
10208 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10209 into another compilation unit, at root level. Regard this as a hint,
10210 and ignore it. */
10211 if (die->parent && die->parent->parent == NULL
10212 && per_cu->unit_type == DW_UT_compile
10213 && per_cu->lang == language_cplus)
10214 return;
10215
10216 /* If necessary, add it to the queue and load its DIEs. */
10217 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10218 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10219 false, cu->language);
10220
10221 cu->per_cu->imported_symtabs_push (per_cu);
10222 }
10223 }
10224
10225 /* RAII object that represents a process_die scope: i.e.,
10226 starts/finishes processing a DIE. */
10227 class process_die_scope
10228 {
10229 public:
10230 process_die_scope (die_info *die, dwarf2_cu *cu)
10231 : m_die (die), m_cu (cu)
10232 {
10233 /* We should only be processing DIEs not already in process. */
10234 gdb_assert (!m_die->in_process);
10235 m_die->in_process = true;
10236 }
10237
10238 ~process_die_scope ()
10239 {
10240 m_die->in_process = false;
10241
10242 /* If we're done processing the DIE for the CU that owns the line
10243 header, we don't need the line header anymore. */
10244 if (m_cu->line_header_die_owner == m_die)
10245 {
10246 delete m_cu->line_header;
10247 m_cu->line_header = NULL;
10248 m_cu->line_header_die_owner = NULL;
10249 }
10250 }
10251
10252 private:
10253 die_info *m_die;
10254 dwarf2_cu *m_cu;
10255 };
10256
10257 /* Process a die and its children. */
10258
10259 static void
10260 process_die (struct die_info *die, struct dwarf2_cu *cu)
10261 {
10262 process_die_scope scope (die, cu);
10263
10264 switch (die->tag)
10265 {
10266 case DW_TAG_padding:
10267 break;
10268 case DW_TAG_compile_unit:
10269 case DW_TAG_partial_unit:
10270 read_file_scope (die, cu);
10271 break;
10272 case DW_TAG_type_unit:
10273 read_type_unit_scope (die, cu);
10274 break;
10275 case DW_TAG_subprogram:
10276 /* Nested subprograms in Fortran get a prefix. */
10277 if (cu->language == language_fortran
10278 && die->parent != NULL
10279 && die->parent->tag == DW_TAG_subprogram)
10280 cu->processing_has_namespace_info = true;
10281 /* Fall through. */
10282 case DW_TAG_inlined_subroutine:
10283 read_func_scope (die, cu);
10284 break;
10285 case DW_TAG_lexical_block:
10286 case DW_TAG_try_block:
10287 case DW_TAG_catch_block:
10288 read_lexical_block_scope (die, cu);
10289 break;
10290 case DW_TAG_call_site:
10291 case DW_TAG_GNU_call_site:
10292 read_call_site_scope (die, cu);
10293 break;
10294 case DW_TAG_class_type:
10295 case DW_TAG_interface_type:
10296 case DW_TAG_structure_type:
10297 case DW_TAG_union_type:
10298 process_structure_scope (die, cu);
10299 break;
10300 case DW_TAG_enumeration_type:
10301 process_enumeration_scope (die, cu);
10302 break;
10303
10304 /* These dies have a type, but processing them does not create
10305 a symbol or recurse to process the children. Therefore we can
10306 read them on-demand through read_type_die. */
10307 case DW_TAG_subroutine_type:
10308 case DW_TAG_set_type:
10309 case DW_TAG_pointer_type:
10310 case DW_TAG_ptr_to_member_type:
10311 case DW_TAG_reference_type:
10312 case DW_TAG_rvalue_reference_type:
10313 case DW_TAG_string_type:
10314 break;
10315
10316 case DW_TAG_array_type:
10317 /* We only need to handle this case for Ada -- in other
10318 languages, it's normal for the compiler to emit a typedef
10319 instead. */
10320 if (cu->language != language_ada)
10321 break;
10322 /* FALLTHROUGH */
10323 case DW_TAG_base_type:
10324 case DW_TAG_subrange_type:
10325 case DW_TAG_typedef:
10326 /* Add a typedef symbol for the type definition, if it has a
10327 DW_AT_name. */
10328 new_symbol (die, read_type_die (die, cu), cu);
10329 break;
10330 case DW_TAG_common_block:
10331 read_common_block (die, cu);
10332 break;
10333 case DW_TAG_common_inclusion:
10334 break;
10335 case DW_TAG_namespace:
10336 cu->processing_has_namespace_info = true;
10337 read_namespace (die, cu);
10338 break;
10339 case DW_TAG_module:
10340 cu->processing_has_namespace_info = true;
10341 read_module (die, cu);
10342 break;
10343 case DW_TAG_imported_declaration:
10344 cu->processing_has_namespace_info = true;
10345 if (read_namespace_alias (die, cu))
10346 break;
10347 /* The declaration is not a global namespace alias. */
10348 /* Fall through. */
10349 case DW_TAG_imported_module:
10350 cu->processing_has_namespace_info = true;
10351 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10352 || cu->language != language_fortran))
10353 complaint (_("Tag '%s' has unexpected children"),
10354 dwarf_tag_name (die->tag));
10355 read_import_statement (die, cu);
10356 break;
10357
10358 case DW_TAG_imported_unit:
10359 process_imported_unit_die (die, cu);
10360 break;
10361
10362 case DW_TAG_variable:
10363 read_variable (die, cu);
10364 break;
10365
10366 default:
10367 new_symbol (die, NULL, cu);
10368 break;
10369 }
10370 }
10371 \f
10372 /* DWARF name computation. */
10373
10374 /* A helper function for dwarf2_compute_name which determines whether DIE
10375 needs to have the name of the scope prepended to the name listed in the
10376 die. */
10377
10378 static int
10379 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10380 {
10381 struct attribute *attr;
10382
10383 switch (die->tag)
10384 {
10385 case DW_TAG_namespace:
10386 case DW_TAG_typedef:
10387 case DW_TAG_class_type:
10388 case DW_TAG_interface_type:
10389 case DW_TAG_structure_type:
10390 case DW_TAG_union_type:
10391 case DW_TAG_enumeration_type:
10392 case DW_TAG_enumerator:
10393 case DW_TAG_subprogram:
10394 case DW_TAG_inlined_subroutine:
10395 case DW_TAG_member:
10396 case DW_TAG_imported_declaration:
10397 return 1;
10398
10399 case DW_TAG_variable:
10400 case DW_TAG_constant:
10401 /* We only need to prefix "globally" visible variables. These include
10402 any variable marked with DW_AT_external or any variable that
10403 lives in a namespace. [Variables in anonymous namespaces
10404 require prefixing, but they are not DW_AT_external.] */
10405
10406 if (dwarf2_attr (die, DW_AT_specification, cu))
10407 {
10408 struct dwarf2_cu *spec_cu = cu;
10409
10410 return die_needs_namespace (die_specification (die, &spec_cu),
10411 spec_cu);
10412 }
10413
10414 attr = dwarf2_attr (die, DW_AT_external, cu);
10415 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10416 && die->parent->tag != DW_TAG_module)
10417 return 0;
10418 /* A variable in a lexical block of some kind does not need a
10419 namespace, even though in C++ such variables may be external
10420 and have a mangled name. */
10421 if (die->parent->tag == DW_TAG_lexical_block
10422 || die->parent->tag == DW_TAG_try_block
10423 || die->parent->tag == DW_TAG_catch_block
10424 || die->parent->tag == DW_TAG_subprogram)
10425 return 0;
10426 return 1;
10427
10428 default:
10429 return 0;
10430 }
10431 }
10432
10433 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10434 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10435 defined for the given DIE. */
10436
10437 static struct attribute *
10438 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10439 {
10440 struct attribute *attr;
10441
10442 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10443 if (attr == NULL)
10444 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10445
10446 return attr;
10447 }
10448
10449 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10450 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10451 defined for the given DIE. */
10452
10453 static const char *
10454 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10455 {
10456 const char *linkage_name;
10457
10458 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10459 if (linkage_name == NULL)
10460 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10461
10462 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10463 See https://github.com/rust-lang/rust/issues/32925. */
10464 if (cu->language == language_rust && linkage_name != NULL
10465 && strchr (linkage_name, '{') != NULL)
10466 linkage_name = NULL;
10467
10468 return linkage_name;
10469 }
10470
10471 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10472 compute the physname for the object, which include a method's:
10473 - formal parameters (C++),
10474 - receiver type (Go),
10475
10476 The term "physname" is a bit confusing.
10477 For C++, for example, it is the demangled name.
10478 For Go, for example, it's the mangled name.
10479
10480 For Ada, return the DIE's linkage name rather than the fully qualified
10481 name. PHYSNAME is ignored..
10482
10483 The result is allocated on the objfile->per_bfd's obstack and
10484 canonicalized. */
10485
10486 static const char *
10487 dwarf2_compute_name (const char *name,
10488 struct die_info *die, struct dwarf2_cu *cu,
10489 int physname)
10490 {
10491 struct objfile *objfile = cu->per_objfile->objfile;
10492
10493 if (name == NULL)
10494 name = dwarf2_name (die, cu);
10495
10496 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10497 but otherwise compute it by typename_concat inside GDB.
10498 FIXME: Actually this is not really true, or at least not always true.
10499 It's all very confusing. compute_and_set_names doesn't try to demangle
10500 Fortran names because there is no mangling standard. So new_symbol
10501 will set the demangled name to the result of dwarf2_full_name, and it is
10502 the demangled name that GDB uses if it exists. */
10503 if (cu->language == language_ada
10504 || (cu->language == language_fortran && physname))
10505 {
10506 /* For Ada unit, we prefer the linkage name over the name, as
10507 the former contains the exported name, which the user expects
10508 to be able to reference. Ideally, we want the user to be able
10509 to reference this entity using either natural or linkage name,
10510 but we haven't started looking at this enhancement yet. */
10511 const char *linkage_name = dw2_linkage_name (die, cu);
10512
10513 if (linkage_name != NULL)
10514 return linkage_name;
10515 }
10516
10517 /* These are the only languages we know how to qualify names in. */
10518 if (name != NULL
10519 && (cu->language == language_cplus
10520 || cu->language == language_fortran || cu->language == language_d
10521 || cu->language == language_rust))
10522 {
10523 if (die_needs_namespace (die, cu))
10524 {
10525 const char *prefix;
10526 const char *canonical_name = NULL;
10527
10528 string_file buf;
10529
10530 prefix = determine_prefix (die, cu);
10531 if (*prefix != '\0')
10532 {
10533 gdb::unique_xmalloc_ptr<char> prefixed_name
10534 (typename_concat (NULL, prefix, name, physname, cu));
10535
10536 buf.puts (prefixed_name.get ());
10537 }
10538 else
10539 buf.puts (name);
10540
10541 /* Template parameters may be specified in the DIE's DW_AT_name, or
10542 as children with DW_TAG_template_type_param or
10543 DW_TAG_value_type_param. If the latter, add them to the name
10544 here. If the name already has template parameters, then
10545 skip this step; some versions of GCC emit both, and
10546 it is more efficient to use the pre-computed name.
10547
10548 Something to keep in mind about this process: it is very
10549 unlikely, or in some cases downright impossible, to produce
10550 something that will match the mangled name of a function.
10551 If the definition of the function has the same debug info,
10552 we should be able to match up with it anyway. But fallbacks
10553 using the minimal symbol, for instance to find a method
10554 implemented in a stripped copy of libstdc++, will not work.
10555 If we do not have debug info for the definition, we will have to
10556 match them up some other way.
10557
10558 When we do name matching there is a related problem with function
10559 templates; two instantiated function templates are allowed to
10560 differ only by their return types, which we do not add here. */
10561
10562 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10563 {
10564 struct attribute *attr;
10565 struct die_info *child;
10566 int first = 1;
10567 const language_defn *cplus_lang = language_def (cu->language);
10568
10569 die->building_fullname = 1;
10570
10571 for (child = die->child; child != NULL; child = child->sibling)
10572 {
10573 struct type *type;
10574 LONGEST value;
10575 const gdb_byte *bytes;
10576 struct dwarf2_locexpr_baton *baton;
10577 struct value *v;
10578
10579 if (child->tag != DW_TAG_template_type_param
10580 && child->tag != DW_TAG_template_value_param)
10581 continue;
10582
10583 if (first)
10584 {
10585 buf.puts ("<");
10586 first = 0;
10587 }
10588 else
10589 buf.puts (", ");
10590
10591 attr = dwarf2_attr (child, DW_AT_type, cu);
10592 if (attr == NULL)
10593 {
10594 complaint (_("template parameter missing DW_AT_type"));
10595 buf.puts ("UNKNOWN_TYPE");
10596 continue;
10597 }
10598 type = die_type (child, cu);
10599
10600 if (child->tag == DW_TAG_template_type_param)
10601 {
10602 cplus_lang->print_type (type, "", &buf, -1, 0,
10603 &type_print_raw_options);
10604 continue;
10605 }
10606
10607 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10608 if (attr == NULL)
10609 {
10610 complaint (_("template parameter missing "
10611 "DW_AT_const_value"));
10612 buf.puts ("UNKNOWN_VALUE");
10613 continue;
10614 }
10615
10616 dwarf2_const_value_attr (attr, type, name,
10617 &cu->comp_unit_obstack, cu,
10618 &value, &bytes, &baton);
10619
10620 if (type->has_no_signedness ())
10621 /* GDB prints characters as NUMBER 'CHAR'. If that's
10622 changed, this can use value_print instead. */
10623 cplus_lang->printchar (value, type, &buf);
10624 else
10625 {
10626 struct value_print_options opts;
10627
10628 if (baton != NULL)
10629 v = dwarf2_evaluate_loc_desc (type, NULL,
10630 baton->data,
10631 baton->size,
10632 baton->per_cu,
10633 baton->per_objfile);
10634 else if (bytes != NULL)
10635 {
10636 v = allocate_value (type);
10637 memcpy (value_contents_writeable (v), bytes,
10638 TYPE_LENGTH (type));
10639 }
10640 else
10641 v = value_from_longest (type, value);
10642
10643 /* Specify decimal so that we do not depend on
10644 the radix. */
10645 get_formatted_print_options (&opts, 'd');
10646 opts.raw = 1;
10647 value_print (v, &buf, &opts);
10648 release_value (v);
10649 }
10650 }
10651
10652 die->building_fullname = 0;
10653
10654 if (!first)
10655 {
10656 /* Close the argument list, with a space if necessary
10657 (nested templates). */
10658 if (!buf.empty () && buf.string ().back () == '>')
10659 buf.puts (" >");
10660 else
10661 buf.puts (">");
10662 }
10663 }
10664
10665 /* For C++ methods, append formal parameter type
10666 information, if PHYSNAME. */
10667
10668 if (physname && die->tag == DW_TAG_subprogram
10669 && cu->language == language_cplus)
10670 {
10671 struct type *type = read_type_die (die, cu);
10672
10673 c_type_print_args (type, &buf, 1, cu->language,
10674 &type_print_raw_options);
10675
10676 if (cu->language == language_cplus)
10677 {
10678 /* Assume that an artificial first parameter is
10679 "this", but do not crash if it is not. RealView
10680 marks unnamed (and thus unused) parameters as
10681 artificial; there is no way to differentiate
10682 the two cases. */
10683 if (type->num_fields () > 0
10684 && TYPE_FIELD_ARTIFICIAL (type, 0)
10685 && type->field (0).type ()->code () == TYPE_CODE_PTR
10686 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10687 buf.puts (" const");
10688 }
10689 }
10690
10691 const std::string &intermediate_name = buf.string ();
10692
10693 if (cu->language == language_cplus)
10694 canonical_name
10695 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10696 objfile);
10697
10698 /* If we only computed INTERMEDIATE_NAME, or if
10699 INTERMEDIATE_NAME is already canonical, then we need to
10700 intern it. */
10701 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10702 name = objfile->intern (intermediate_name);
10703 else
10704 name = canonical_name;
10705 }
10706 }
10707
10708 return name;
10709 }
10710
10711 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10712 If scope qualifiers are appropriate they will be added. The result
10713 will be allocated on the storage_obstack, or NULL if the DIE does
10714 not have a name. NAME may either be from a previous call to
10715 dwarf2_name or NULL.
10716
10717 The output string will be canonicalized (if C++). */
10718
10719 static const char *
10720 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10721 {
10722 return dwarf2_compute_name (name, die, cu, 0);
10723 }
10724
10725 /* Construct a physname for the given DIE in CU. NAME may either be
10726 from a previous call to dwarf2_name or NULL. The result will be
10727 allocated on the objfile_objstack or NULL if the DIE does not have a
10728 name.
10729
10730 The output string will be canonicalized (if C++). */
10731
10732 static const char *
10733 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10734 {
10735 struct objfile *objfile = cu->per_objfile->objfile;
10736 const char *retval, *mangled = NULL, *canon = NULL;
10737 int need_copy = 1;
10738
10739 /* In this case dwarf2_compute_name is just a shortcut not building anything
10740 on its own. */
10741 if (!die_needs_namespace (die, cu))
10742 return dwarf2_compute_name (name, die, cu, 1);
10743
10744 if (cu->language != language_rust)
10745 mangled = dw2_linkage_name (die, cu);
10746
10747 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10748 has computed. */
10749 gdb::unique_xmalloc_ptr<char> demangled;
10750 if (mangled != NULL)
10751 {
10752
10753 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10754 {
10755 /* Do nothing (do not demangle the symbol name). */
10756 }
10757 else
10758 {
10759 /* Use DMGL_RET_DROP for C++ template functions to suppress
10760 their return type. It is easier for GDB users to search
10761 for such functions as `name(params)' than `long name(params)'.
10762 In such case the minimal symbol names do not match the full
10763 symbol names but for template functions there is never a need
10764 to look up their definition from their declaration so
10765 the only disadvantage remains the minimal symbol variant
10766 `long name(params)' does not have the proper inferior type. */
10767 demangled.reset (gdb_demangle (mangled,
10768 (DMGL_PARAMS | DMGL_ANSI
10769 | DMGL_RET_DROP)));
10770 }
10771 if (demangled)
10772 canon = demangled.get ();
10773 else
10774 {
10775 canon = mangled;
10776 need_copy = 0;
10777 }
10778 }
10779
10780 if (canon == NULL || check_physname)
10781 {
10782 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10783
10784 if (canon != NULL && strcmp (physname, canon) != 0)
10785 {
10786 /* It may not mean a bug in GDB. The compiler could also
10787 compute DW_AT_linkage_name incorrectly. But in such case
10788 GDB would need to be bug-to-bug compatible. */
10789
10790 complaint (_("Computed physname <%s> does not match demangled <%s> "
10791 "(from linkage <%s>) - DIE at %s [in module %s]"),
10792 physname, canon, mangled, sect_offset_str (die->sect_off),
10793 objfile_name (objfile));
10794
10795 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10796 is available here - over computed PHYSNAME. It is safer
10797 against both buggy GDB and buggy compilers. */
10798
10799 retval = canon;
10800 }
10801 else
10802 {
10803 retval = physname;
10804 need_copy = 0;
10805 }
10806 }
10807 else
10808 retval = canon;
10809
10810 if (need_copy)
10811 retval = objfile->intern (retval);
10812
10813 return retval;
10814 }
10815
10816 /* Inspect DIE in CU for a namespace alias. If one exists, record
10817 a new symbol for it.
10818
10819 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10820
10821 static int
10822 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10823 {
10824 struct attribute *attr;
10825
10826 /* If the die does not have a name, this is not a namespace
10827 alias. */
10828 attr = dwarf2_attr (die, DW_AT_name, cu);
10829 if (attr != NULL)
10830 {
10831 int num;
10832 struct die_info *d = die;
10833 struct dwarf2_cu *imported_cu = cu;
10834
10835 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10836 keep inspecting DIEs until we hit the underlying import. */
10837 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10838 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10839 {
10840 attr = dwarf2_attr (d, DW_AT_import, cu);
10841 if (attr == NULL)
10842 break;
10843
10844 d = follow_die_ref (d, attr, &imported_cu);
10845 if (d->tag != DW_TAG_imported_declaration)
10846 break;
10847 }
10848
10849 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10850 {
10851 complaint (_("DIE at %s has too many recursively imported "
10852 "declarations"), sect_offset_str (d->sect_off));
10853 return 0;
10854 }
10855
10856 if (attr != NULL)
10857 {
10858 struct type *type;
10859 sect_offset sect_off = attr->get_ref_die_offset ();
10860
10861 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10862 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10863 {
10864 /* This declaration is a global namespace alias. Add
10865 a symbol for it whose type is the aliased namespace. */
10866 new_symbol (die, type, cu);
10867 return 1;
10868 }
10869 }
10870 }
10871
10872 return 0;
10873 }
10874
10875 /* Return the using directives repository (global or local?) to use in the
10876 current context for CU.
10877
10878 For Ada, imported declarations can materialize renamings, which *may* be
10879 global. However it is impossible (for now?) in DWARF to distinguish
10880 "external" imported declarations and "static" ones. As all imported
10881 declarations seem to be static in all other languages, make them all CU-wide
10882 global only in Ada. */
10883
10884 static struct using_direct **
10885 using_directives (struct dwarf2_cu *cu)
10886 {
10887 if (cu->language == language_ada
10888 && cu->get_builder ()->outermost_context_p ())
10889 return cu->get_builder ()->get_global_using_directives ();
10890 else
10891 return cu->get_builder ()->get_local_using_directives ();
10892 }
10893
10894 /* Read the import statement specified by the given die and record it. */
10895
10896 static void
10897 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10898 {
10899 struct objfile *objfile = cu->per_objfile->objfile;
10900 struct attribute *import_attr;
10901 struct die_info *imported_die, *child_die;
10902 struct dwarf2_cu *imported_cu;
10903 const char *imported_name;
10904 const char *imported_name_prefix;
10905 const char *canonical_name;
10906 const char *import_alias;
10907 const char *imported_declaration = NULL;
10908 const char *import_prefix;
10909 std::vector<const char *> excludes;
10910
10911 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10912 if (import_attr == NULL)
10913 {
10914 complaint (_("Tag '%s' has no DW_AT_import"),
10915 dwarf_tag_name (die->tag));
10916 return;
10917 }
10918
10919 imported_cu = cu;
10920 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10921 imported_name = dwarf2_name (imported_die, imported_cu);
10922 if (imported_name == NULL)
10923 {
10924 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10925
10926 The import in the following code:
10927 namespace A
10928 {
10929 typedef int B;
10930 }
10931
10932 int main ()
10933 {
10934 using A::B;
10935 B b;
10936 return b;
10937 }
10938
10939 ...
10940 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10941 <52> DW_AT_decl_file : 1
10942 <53> DW_AT_decl_line : 6
10943 <54> DW_AT_import : <0x75>
10944 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10945 <59> DW_AT_name : B
10946 <5b> DW_AT_decl_file : 1
10947 <5c> DW_AT_decl_line : 2
10948 <5d> DW_AT_type : <0x6e>
10949 ...
10950 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10951 <76> DW_AT_byte_size : 4
10952 <77> DW_AT_encoding : 5 (signed)
10953
10954 imports the wrong die ( 0x75 instead of 0x58 ).
10955 This case will be ignored until the gcc bug is fixed. */
10956 return;
10957 }
10958
10959 /* Figure out the local name after import. */
10960 import_alias = dwarf2_name (die, cu);
10961
10962 /* Figure out where the statement is being imported to. */
10963 import_prefix = determine_prefix (die, cu);
10964
10965 /* Figure out what the scope of the imported die is and prepend it
10966 to the name of the imported die. */
10967 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10968
10969 if (imported_die->tag != DW_TAG_namespace
10970 && imported_die->tag != DW_TAG_module)
10971 {
10972 imported_declaration = imported_name;
10973 canonical_name = imported_name_prefix;
10974 }
10975 else if (strlen (imported_name_prefix) > 0)
10976 canonical_name = obconcat (&objfile->objfile_obstack,
10977 imported_name_prefix,
10978 (cu->language == language_d ? "." : "::"),
10979 imported_name, (char *) NULL);
10980 else
10981 canonical_name = imported_name;
10982
10983 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10984 for (child_die = die->child; child_die && child_die->tag;
10985 child_die = child_die->sibling)
10986 {
10987 /* DWARF-4: A Fortran use statement with a “rename list” may be
10988 represented by an imported module entry with an import attribute
10989 referring to the module and owned entries corresponding to those
10990 entities that are renamed as part of being imported. */
10991
10992 if (child_die->tag != DW_TAG_imported_declaration)
10993 {
10994 complaint (_("child DW_TAG_imported_declaration expected "
10995 "- DIE at %s [in module %s]"),
10996 sect_offset_str (child_die->sect_off),
10997 objfile_name (objfile));
10998 continue;
10999 }
11000
11001 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11002 if (import_attr == NULL)
11003 {
11004 complaint (_("Tag '%s' has no DW_AT_import"),
11005 dwarf_tag_name (child_die->tag));
11006 continue;
11007 }
11008
11009 imported_cu = cu;
11010 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11011 &imported_cu);
11012 imported_name = dwarf2_name (imported_die, imported_cu);
11013 if (imported_name == NULL)
11014 {
11015 complaint (_("child DW_TAG_imported_declaration has unknown "
11016 "imported name - DIE at %s [in module %s]"),
11017 sect_offset_str (child_die->sect_off),
11018 objfile_name (objfile));
11019 continue;
11020 }
11021
11022 excludes.push_back (imported_name);
11023
11024 process_die (child_die, cu);
11025 }
11026
11027 add_using_directive (using_directives (cu),
11028 import_prefix,
11029 canonical_name,
11030 import_alias,
11031 imported_declaration,
11032 excludes,
11033 0,
11034 &objfile->objfile_obstack);
11035 }
11036
11037 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11038 types, but gives them a size of zero. Starting with version 14,
11039 ICC is compatible with GCC. */
11040
11041 static bool
11042 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11043 {
11044 if (!cu->checked_producer)
11045 check_producer (cu);
11046
11047 return cu->producer_is_icc_lt_14;
11048 }
11049
11050 /* ICC generates a DW_AT_type for C void functions. This was observed on
11051 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11052 which says that void functions should not have a DW_AT_type. */
11053
11054 static bool
11055 producer_is_icc (struct dwarf2_cu *cu)
11056 {
11057 if (!cu->checked_producer)
11058 check_producer (cu);
11059
11060 return cu->producer_is_icc;
11061 }
11062
11063 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11064 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11065 this, it was first present in GCC release 4.3.0. */
11066
11067 static bool
11068 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11069 {
11070 if (!cu->checked_producer)
11071 check_producer (cu);
11072
11073 return cu->producer_is_gcc_lt_4_3;
11074 }
11075
11076 static file_and_directory
11077 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11078 {
11079 file_and_directory res;
11080
11081 /* Find the filename. Do not use dwarf2_name here, since the filename
11082 is not a source language identifier. */
11083 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11084 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11085
11086 if (res.comp_dir == NULL
11087 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11088 && IS_ABSOLUTE_PATH (res.name))
11089 {
11090 res.comp_dir_storage = ldirname (res.name);
11091 if (!res.comp_dir_storage.empty ())
11092 res.comp_dir = res.comp_dir_storage.c_str ();
11093 }
11094 if (res.comp_dir != NULL)
11095 {
11096 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11097 directory, get rid of it. */
11098 const char *cp = strchr (res.comp_dir, ':');
11099
11100 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11101 res.comp_dir = cp + 1;
11102 }
11103
11104 if (res.name == NULL)
11105 res.name = "<unknown>";
11106
11107 return res;
11108 }
11109
11110 /* Handle DW_AT_stmt_list for a compilation unit.
11111 DIE is the DW_TAG_compile_unit die for CU.
11112 COMP_DIR is the compilation directory. LOWPC is passed to
11113 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11114
11115 static void
11116 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11117 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11118 {
11119 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11120 struct attribute *attr;
11121 struct line_header line_header_local;
11122 hashval_t line_header_local_hash;
11123 void **slot;
11124 int decode_mapping;
11125
11126 gdb_assert (! cu->per_cu->is_debug_types);
11127
11128 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11129 if (attr == NULL || !attr->form_is_unsigned ())
11130 return;
11131
11132 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11133
11134 /* The line header hash table is only created if needed (it exists to
11135 prevent redundant reading of the line table for partial_units).
11136 If we're given a partial_unit, we'll need it. If we're given a
11137 compile_unit, then use the line header hash table if it's already
11138 created, but don't create one just yet. */
11139
11140 if (per_objfile->line_header_hash == NULL
11141 && die->tag == DW_TAG_partial_unit)
11142 {
11143 per_objfile->line_header_hash
11144 .reset (htab_create_alloc (127, line_header_hash_voidp,
11145 line_header_eq_voidp,
11146 free_line_header_voidp,
11147 xcalloc, xfree));
11148 }
11149
11150 line_header_local.sect_off = line_offset;
11151 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11152 line_header_local_hash = line_header_hash (&line_header_local);
11153 if (per_objfile->line_header_hash != NULL)
11154 {
11155 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11156 &line_header_local,
11157 line_header_local_hash, NO_INSERT);
11158
11159 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11160 is not present in *SLOT (since if there is something in *SLOT then
11161 it will be for a partial_unit). */
11162 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11163 {
11164 gdb_assert (*slot != NULL);
11165 cu->line_header = (struct line_header *) *slot;
11166 return;
11167 }
11168 }
11169
11170 /* dwarf_decode_line_header does not yet provide sufficient information.
11171 We always have to call also dwarf_decode_lines for it. */
11172 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11173 if (lh == NULL)
11174 return;
11175
11176 cu->line_header = lh.release ();
11177 cu->line_header_die_owner = die;
11178
11179 if (per_objfile->line_header_hash == NULL)
11180 slot = NULL;
11181 else
11182 {
11183 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11184 &line_header_local,
11185 line_header_local_hash, INSERT);
11186 gdb_assert (slot != NULL);
11187 }
11188 if (slot != NULL && *slot == NULL)
11189 {
11190 /* This newly decoded line number information unit will be owned
11191 by line_header_hash hash table. */
11192 *slot = cu->line_header;
11193 cu->line_header_die_owner = NULL;
11194 }
11195 else
11196 {
11197 /* We cannot free any current entry in (*slot) as that struct line_header
11198 may be already used by multiple CUs. Create only temporary decoded
11199 line_header for this CU - it may happen at most once for each line
11200 number information unit. And if we're not using line_header_hash
11201 then this is what we want as well. */
11202 gdb_assert (die->tag != DW_TAG_partial_unit);
11203 }
11204 decode_mapping = (die->tag != DW_TAG_partial_unit);
11205 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11206 decode_mapping);
11207
11208 }
11209
11210 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11211
11212 static void
11213 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11214 {
11215 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11216 struct objfile *objfile = per_objfile->objfile;
11217 struct gdbarch *gdbarch = objfile->arch ();
11218 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11219 CORE_ADDR highpc = ((CORE_ADDR) 0);
11220 struct attribute *attr;
11221 struct die_info *child_die;
11222 CORE_ADDR baseaddr;
11223
11224 prepare_one_comp_unit (cu, die, cu->language);
11225 baseaddr = objfile->text_section_offset ();
11226
11227 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11228
11229 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11230 from finish_block. */
11231 if (lowpc == ((CORE_ADDR) -1))
11232 lowpc = highpc;
11233 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11234
11235 file_and_directory fnd = find_file_and_directory (die, cu);
11236
11237 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11238 standardised yet. As a workaround for the language detection we fall
11239 back to the DW_AT_producer string. */
11240 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11241 cu->language = language_opencl;
11242
11243 /* Similar hack for Go. */
11244 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11245 set_cu_language (DW_LANG_Go, cu);
11246
11247 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11248
11249 /* Decode line number information if present. We do this before
11250 processing child DIEs, so that the line header table is available
11251 for DW_AT_decl_file. */
11252 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11253
11254 /* Process all dies in compilation unit. */
11255 if (die->child != NULL)
11256 {
11257 child_die = die->child;
11258 while (child_die && child_die->tag)
11259 {
11260 process_die (child_die, cu);
11261 child_die = child_die->sibling;
11262 }
11263 }
11264
11265 /* Decode macro information, if present. Dwarf 2 macro information
11266 refers to information in the line number info statement program
11267 header, so we can only read it if we've read the header
11268 successfully. */
11269 attr = dwarf2_attr (die, DW_AT_macros, cu);
11270 if (attr == NULL)
11271 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11272 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11273 {
11274 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11275 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11276
11277 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11278 }
11279 else
11280 {
11281 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11282 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11283 {
11284 unsigned int macro_offset = attr->as_unsigned ();
11285
11286 dwarf_decode_macros (cu, macro_offset, 0);
11287 }
11288 }
11289 }
11290
11291 void
11292 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11293 {
11294 struct type_unit_group *tu_group;
11295 int first_time;
11296 struct attribute *attr;
11297 unsigned int i;
11298 struct signatured_type *sig_type;
11299
11300 gdb_assert (per_cu->is_debug_types);
11301 sig_type = (struct signatured_type *) per_cu;
11302
11303 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11304
11305 /* If we're using .gdb_index (includes -readnow) then
11306 per_cu->type_unit_group may not have been set up yet. */
11307 if (sig_type->type_unit_group == NULL)
11308 sig_type->type_unit_group = get_type_unit_group (this, attr);
11309 tu_group = sig_type->type_unit_group;
11310
11311 /* If we've already processed this stmt_list there's no real need to
11312 do it again, we could fake it and just recreate the part we need
11313 (file name,index -> symtab mapping). If data shows this optimization
11314 is useful we can do it then. */
11315 type_unit_group_unshareable *tug_unshare
11316 = per_objfile->get_type_unit_group_unshareable (tu_group);
11317 first_time = tug_unshare->compunit_symtab == NULL;
11318
11319 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11320 debug info. */
11321 line_header_up lh;
11322 if (attr != NULL && attr->form_is_unsigned ())
11323 {
11324 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11325 lh = dwarf_decode_line_header (line_offset, this);
11326 }
11327 if (lh == NULL)
11328 {
11329 if (first_time)
11330 start_symtab ("", NULL, 0);
11331 else
11332 {
11333 gdb_assert (tug_unshare->symtabs == NULL);
11334 gdb_assert (m_builder == nullptr);
11335 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11336 m_builder.reset (new struct buildsym_compunit
11337 (COMPUNIT_OBJFILE (cust), "",
11338 COMPUNIT_DIRNAME (cust),
11339 compunit_language (cust),
11340 0, cust));
11341 list_in_scope = get_builder ()->get_file_symbols ();
11342 }
11343 return;
11344 }
11345
11346 line_header = lh.release ();
11347 line_header_die_owner = die;
11348
11349 if (first_time)
11350 {
11351 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11352
11353 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11354 still initializing it, and our caller (a few levels up)
11355 process_full_type_unit still needs to know if this is the first
11356 time. */
11357
11358 tug_unshare->symtabs
11359 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11360 struct symtab *, line_header->file_names_size ());
11361
11362 auto &file_names = line_header->file_names ();
11363 for (i = 0; i < file_names.size (); ++i)
11364 {
11365 file_entry &fe = file_names[i];
11366 dwarf2_start_subfile (this, fe.name,
11367 fe.include_dir (line_header));
11368 buildsym_compunit *b = get_builder ();
11369 if (b->get_current_subfile ()->symtab == NULL)
11370 {
11371 /* NOTE: start_subfile will recognize when it's been
11372 passed a file it has already seen. So we can't
11373 assume there's a simple mapping from
11374 cu->line_header->file_names to subfiles, plus
11375 cu->line_header->file_names may contain dups. */
11376 b->get_current_subfile ()->symtab
11377 = allocate_symtab (cust, b->get_current_subfile ()->name);
11378 }
11379
11380 fe.symtab = b->get_current_subfile ()->symtab;
11381 tug_unshare->symtabs[i] = fe.symtab;
11382 }
11383 }
11384 else
11385 {
11386 gdb_assert (m_builder == nullptr);
11387 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11388 m_builder.reset (new struct buildsym_compunit
11389 (COMPUNIT_OBJFILE (cust), "",
11390 COMPUNIT_DIRNAME (cust),
11391 compunit_language (cust),
11392 0, cust));
11393 list_in_scope = get_builder ()->get_file_symbols ();
11394
11395 auto &file_names = line_header->file_names ();
11396 for (i = 0; i < file_names.size (); ++i)
11397 {
11398 file_entry &fe = file_names[i];
11399 fe.symtab = tug_unshare->symtabs[i];
11400 }
11401 }
11402
11403 /* The main symtab is allocated last. Type units don't have DW_AT_name
11404 so they don't have a "real" (so to speak) symtab anyway.
11405 There is later code that will assign the main symtab to all symbols
11406 that don't have one. We need to handle the case of a symbol with a
11407 missing symtab (DW_AT_decl_file) anyway. */
11408 }
11409
11410 /* Process DW_TAG_type_unit.
11411 For TUs we want to skip the first top level sibling if it's not the
11412 actual type being defined by this TU. In this case the first top
11413 level sibling is there to provide context only. */
11414
11415 static void
11416 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11417 {
11418 struct die_info *child_die;
11419
11420 prepare_one_comp_unit (cu, die, language_minimal);
11421
11422 /* Initialize (or reinitialize) the machinery for building symtabs.
11423 We do this before processing child DIEs, so that the line header table
11424 is available for DW_AT_decl_file. */
11425 cu->setup_type_unit_groups (die);
11426
11427 if (die->child != NULL)
11428 {
11429 child_die = die->child;
11430 while (child_die && child_die->tag)
11431 {
11432 process_die (child_die, cu);
11433 child_die = child_die->sibling;
11434 }
11435 }
11436 }
11437 \f
11438 /* DWO/DWP files.
11439
11440 http://gcc.gnu.org/wiki/DebugFission
11441 http://gcc.gnu.org/wiki/DebugFissionDWP
11442
11443 To simplify handling of both DWO files ("object" files with the DWARF info)
11444 and DWP files (a file with the DWOs packaged up into one file), we treat
11445 DWP files as having a collection of virtual DWO files. */
11446
11447 static hashval_t
11448 hash_dwo_file (const void *item)
11449 {
11450 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11451 hashval_t hash;
11452
11453 hash = htab_hash_string (dwo_file->dwo_name);
11454 if (dwo_file->comp_dir != NULL)
11455 hash += htab_hash_string (dwo_file->comp_dir);
11456 return hash;
11457 }
11458
11459 static int
11460 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11461 {
11462 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11463 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11464
11465 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11466 return 0;
11467 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11468 return lhs->comp_dir == rhs->comp_dir;
11469 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11470 }
11471
11472 /* Allocate a hash table for DWO files. */
11473
11474 static htab_up
11475 allocate_dwo_file_hash_table ()
11476 {
11477 auto delete_dwo_file = [] (void *item)
11478 {
11479 struct dwo_file *dwo_file = (struct dwo_file *) item;
11480
11481 delete dwo_file;
11482 };
11483
11484 return htab_up (htab_create_alloc (41,
11485 hash_dwo_file,
11486 eq_dwo_file,
11487 delete_dwo_file,
11488 xcalloc, xfree));
11489 }
11490
11491 /* Lookup DWO file DWO_NAME. */
11492
11493 static void **
11494 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11495 const char *dwo_name,
11496 const char *comp_dir)
11497 {
11498 struct dwo_file find_entry;
11499 void **slot;
11500
11501 if (per_objfile->per_bfd->dwo_files == NULL)
11502 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11503
11504 find_entry.dwo_name = dwo_name;
11505 find_entry.comp_dir = comp_dir;
11506 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11507 INSERT);
11508
11509 return slot;
11510 }
11511
11512 static hashval_t
11513 hash_dwo_unit (const void *item)
11514 {
11515 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11516
11517 /* This drops the top 32 bits of the id, but is ok for a hash. */
11518 return dwo_unit->signature;
11519 }
11520
11521 static int
11522 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11523 {
11524 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11525 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11526
11527 /* The signature is assumed to be unique within the DWO file.
11528 So while object file CU dwo_id's always have the value zero,
11529 that's OK, assuming each object file DWO file has only one CU,
11530 and that's the rule for now. */
11531 return lhs->signature == rhs->signature;
11532 }
11533
11534 /* Allocate a hash table for DWO CUs,TUs.
11535 There is one of these tables for each of CUs,TUs for each DWO file. */
11536
11537 static htab_up
11538 allocate_dwo_unit_table ()
11539 {
11540 /* Start out with a pretty small number.
11541 Generally DWO files contain only one CU and maybe some TUs. */
11542 return htab_up (htab_create_alloc (3,
11543 hash_dwo_unit,
11544 eq_dwo_unit,
11545 NULL, xcalloc, xfree));
11546 }
11547
11548 /* die_reader_func for create_dwo_cu. */
11549
11550 static void
11551 create_dwo_cu_reader (const struct die_reader_specs *reader,
11552 const gdb_byte *info_ptr,
11553 struct die_info *comp_unit_die,
11554 struct dwo_file *dwo_file,
11555 struct dwo_unit *dwo_unit)
11556 {
11557 struct dwarf2_cu *cu = reader->cu;
11558 sect_offset sect_off = cu->per_cu->sect_off;
11559 struct dwarf2_section_info *section = cu->per_cu->section;
11560
11561 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11562 if (!signature.has_value ())
11563 {
11564 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11565 " its dwo_id [in module %s]"),
11566 sect_offset_str (sect_off), dwo_file->dwo_name);
11567 return;
11568 }
11569
11570 dwo_unit->dwo_file = dwo_file;
11571 dwo_unit->signature = *signature;
11572 dwo_unit->section = section;
11573 dwo_unit->sect_off = sect_off;
11574 dwo_unit->length = cu->per_cu->length;
11575
11576 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11577 sect_offset_str (sect_off),
11578 hex_string (dwo_unit->signature));
11579 }
11580
11581 /* Create the dwo_units for the CUs in a DWO_FILE.
11582 Note: This function processes DWO files only, not DWP files. */
11583
11584 static void
11585 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11586 dwarf2_cu *cu, struct dwo_file &dwo_file,
11587 dwarf2_section_info &section, htab_up &cus_htab)
11588 {
11589 struct objfile *objfile = per_objfile->objfile;
11590 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11591 const gdb_byte *info_ptr, *end_ptr;
11592
11593 section.read (objfile);
11594 info_ptr = section.buffer;
11595
11596 if (info_ptr == NULL)
11597 return;
11598
11599 dwarf_read_debug_printf ("Reading %s for %s:",
11600 section.get_name (),
11601 section.get_file_name ());
11602
11603 end_ptr = info_ptr + section.size;
11604 while (info_ptr < end_ptr)
11605 {
11606 struct dwarf2_per_cu_data per_cu;
11607 struct dwo_unit read_unit {};
11608 struct dwo_unit *dwo_unit;
11609 void **slot;
11610 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11611
11612 memset (&per_cu, 0, sizeof (per_cu));
11613 per_cu.per_bfd = per_bfd;
11614 per_cu.is_debug_types = 0;
11615 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11616 per_cu.section = &section;
11617
11618 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11619 if (!reader.dummy_p)
11620 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11621 &dwo_file, &read_unit);
11622 info_ptr += per_cu.length;
11623
11624 // If the unit could not be parsed, skip it.
11625 if (read_unit.dwo_file == NULL)
11626 continue;
11627
11628 if (cus_htab == NULL)
11629 cus_htab = allocate_dwo_unit_table ();
11630
11631 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11632 struct dwo_unit);
11633 *dwo_unit = read_unit;
11634 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11635 gdb_assert (slot != NULL);
11636 if (*slot != NULL)
11637 {
11638 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11639 sect_offset dup_sect_off = dup_cu->sect_off;
11640
11641 complaint (_("debug cu entry at offset %s is duplicate to"
11642 " the entry at offset %s, signature %s"),
11643 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11644 hex_string (dwo_unit->signature));
11645 }
11646 *slot = (void *)dwo_unit;
11647 }
11648 }
11649
11650 /* DWP file .debug_{cu,tu}_index section format:
11651 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11652 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11653
11654 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11655 officially standard DWP format was published with DWARF v5 and is called
11656 Version 5. There are no versions 3 or 4.
11657
11658 DWP Version 1:
11659
11660 Both index sections have the same format, and serve to map a 64-bit
11661 signature to a set of section numbers. Each section begins with a header,
11662 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11663 indexes, and a pool of 32-bit section numbers. The index sections will be
11664 aligned at 8-byte boundaries in the file.
11665
11666 The index section header consists of:
11667
11668 V, 32 bit version number
11669 -, 32 bits unused
11670 N, 32 bit number of compilation units or type units in the index
11671 M, 32 bit number of slots in the hash table
11672
11673 Numbers are recorded using the byte order of the application binary.
11674
11675 The hash table begins at offset 16 in the section, and consists of an array
11676 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11677 order of the application binary). Unused slots in the hash table are 0.
11678 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11679
11680 The parallel table begins immediately after the hash table
11681 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11682 array of 32-bit indexes (using the byte order of the application binary),
11683 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11684 table contains a 32-bit index into the pool of section numbers. For unused
11685 hash table slots, the corresponding entry in the parallel table will be 0.
11686
11687 The pool of section numbers begins immediately following the hash table
11688 (at offset 16 + 12 * M from the beginning of the section). The pool of
11689 section numbers consists of an array of 32-bit words (using the byte order
11690 of the application binary). Each item in the array is indexed starting
11691 from 0. The hash table entry provides the index of the first section
11692 number in the set. Additional section numbers in the set follow, and the
11693 set is terminated by a 0 entry (section number 0 is not used in ELF).
11694
11695 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11696 section must be the first entry in the set, and the .debug_abbrev.dwo must
11697 be the second entry. Other members of the set may follow in any order.
11698
11699 ---
11700
11701 DWP Versions 2 and 5:
11702
11703 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11704 and the entries in the index tables are now offsets into these sections.
11705 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11706 section.
11707
11708 Index Section Contents:
11709 Header
11710 Hash Table of Signatures dwp_hash_table.hash_table
11711 Parallel Table of Indices dwp_hash_table.unit_table
11712 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11713 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11714
11715 The index section header consists of:
11716
11717 V, 32 bit version number
11718 L, 32 bit number of columns in the table of section offsets
11719 N, 32 bit number of compilation units or type units in the index
11720 M, 32 bit number of slots in the hash table
11721
11722 Numbers are recorded using the byte order of the application binary.
11723
11724 The hash table has the same format as version 1.
11725 The parallel table of indices has the same format as version 1,
11726 except that the entries are origin-1 indices into the table of sections
11727 offsets and the table of section sizes.
11728
11729 The table of offsets begins immediately following the parallel table
11730 (at offset 16 + 12 * M from the beginning of the section). The table is
11731 a two-dimensional array of 32-bit words (using the byte order of the
11732 application binary), with L columns and N+1 rows, in row-major order.
11733 Each row in the array is indexed starting from 0. The first row provides
11734 a key to the remaining rows: each column in this row provides an identifier
11735 for a debug section, and the offsets in the same column of subsequent rows
11736 refer to that section. The section identifiers for Version 2 are:
11737
11738 DW_SECT_INFO 1 .debug_info.dwo
11739 DW_SECT_TYPES 2 .debug_types.dwo
11740 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11741 DW_SECT_LINE 4 .debug_line.dwo
11742 DW_SECT_LOC 5 .debug_loc.dwo
11743 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11744 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11745 DW_SECT_MACRO 8 .debug_macro.dwo
11746
11747 The section identifiers for Version 5 are:
11748
11749 DW_SECT_INFO_V5 1 .debug_info.dwo
11750 DW_SECT_RESERVED_V5 2 --
11751 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11752 DW_SECT_LINE_V5 4 .debug_line.dwo
11753 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11754 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11755 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11756 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11757
11758 The offsets provided by the CU and TU index sections are the base offsets
11759 for the contributions made by each CU or TU to the corresponding section
11760 in the package file. Each CU and TU header contains an abbrev_offset
11761 field, used to find the abbreviations table for that CU or TU within the
11762 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11763 be interpreted as relative to the base offset given in the index section.
11764 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11765 should be interpreted as relative to the base offset for .debug_line.dwo,
11766 and offsets into other debug sections obtained from DWARF attributes should
11767 also be interpreted as relative to the corresponding base offset.
11768
11769 The table of sizes begins immediately following the table of offsets.
11770 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11771 with L columns and N rows, in row-major order. Each row in the array is
11772 indexed starting from 1 (row 0 is shared by the two tables).
11773
11774 ---
11775
11776 Hash table lookup is handled the same in version 1 and 2:
11777
11778 We assume that N and M will not exceed 2^32 - 1.
11779 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11780
11781 Given a 64-bit compilation unit signature or a type signature S, an entry
11782 in the hash table is located as follows:
11783
11784 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11785 the low-order k bits all set to 1.
11786
11787 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11788
11789 3) If the hash table entry at index H matches the signature, use that
11790 entry. If the hash table entry at index H is unused (all zeroes),
11791 terminate the search: the signature is not present in the table.
11792
11793 4) Let H = (H + H') modulo M. Repeat at Step 3.
11794
11795 Because M > N and H' and M are relatively prime, the search is guaranteed
11796 to stop at an unused slot or find the match. */
11797
11798 /* Create a hash table to map DWO IDs to their CU/TU entry in
11799 .debug_{info,types}.dwo in DWP_FILE.
11800 Returns NULL if there isn't one.
11801 Note: This function processes DWP files only, not DWO files. */
11802
11803 static struct dwp_hash_table *
11804 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11805 struct dwp_file *dwp_file, int is_debug_types)
11806 {
11807 struct objfile *objfile = per_objfile->objfile;
11808 bfd *dbfd = dwp_file->dbfd.get ();
11809 const gdb_byte *index_ptr, *index_end;
11810 struct dwarf2_section_info *index;
11811 uint32_t version, nr_columns, nr_units, nr_slots;
11812 struct dwp_hash_table *htab;
11813
11814 if (is_debug_types)
11815 index = &dwp_file->sections.tu_index;
11816 else
11817 index = &dwp_file->sections.cu_index;
11818
11819 if (index->empty ())
11820 return NULL;
11821 index->read (objfile);
11822
11823 index_ptr = index->buffer;
11824 index_end = index_ptr + index->size;
11825
11826 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11827 For now it's safe to just read 4 bytes (particularly as it's difficult to
11828 tell if you're dealing with Version 5 before you've read the version). */
11829 version = read_4_bytes (dbfd, index_ptr);
11830 index_ptr += 4;
11831 if (version == 2 || version == 5)
11832 nr_columns = read_4_bytes (dbfd, index_ptr);
11833 else
11834 nr_columns = 0;
11835 index_ptr += 4;
11836 nr_units = read_4_bytes (dbfd, index_ptr);
11837 index_ptr += 4;
11838 nr_slots = read_4_bytes (dbfd, index_ptr);
11839 index_ptr += 4;
11840
11841 if (version != 1 && version != 2 && version != 5)
11842 {
11843 error (_("Dwarf Error: unsupported DWP file version (%s)"
11844 " [in module %s]"),
11845 pulongest (version), dwp_file->name);
11846 }
11847 if (nr_slots != (nr_slots & -nr_slots))
11848 {
11849 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11850 " is not power of 2 [in module %s]"),
11851 pulongest (nr_slots), dwp_file->name);
11852 }
11853
11854 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11855 htab->version = version;
11856 htab->nr_columns = nr_columns;
11857 htab->nr_units = nr_units;
11858 htab->nr_slots = nr_slots;
11859 htab->hash_table = index_ptr;
11860 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11861
11862 /* Exit early if the table is empty. */
11863 if (nr_slots == 0 || nr_units == 0
11864 || (version == 2 && nr_columns == 0)
11865 || (version == 5 && nr_columns == 0))
11866 {
11867 /* All must be zero. */
11868 if (nr_slots != 0 || nr_units != 0
11869 || (version == 2 && nr_columns != 0)
11870 || (version == 5 && nr_columns != 0))
11871 {
11872 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11873 " all zero [in modules %s]"),
11874 dwp_file->name);
11875 }
11876 return htab;
11877 }
11878
11879 if (version == 1)
11880 {
11881 htab->section_pool.v1.indices =
11882 htab->unit_table + sizeof (uint32_t) * nr_slots;
11883 /* It's harder to decide whether the section is too small in v1.
11884 V1 is deprecated anyway so we punt. */
11885 }
11886 else if (version == 2)
11887 {
11888 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11889 int *ids = htab->section_pool.v2.section_ids;
11890 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11891 /* Reverse map for error checking. */
11892 int ids_seen[DW_SECT_MAX + 1];
11893 int i;
11894
11895 if (nr_columns < 2)
11896 {
11897 error (_("Dwarf Error: bad DWP hash table, too few columns"
11898 " in section table [in module %s]"),
11899 dwp_file->name);
11900 }
11901 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11902 {
11903 error (_("Dwarf Error: bad DWP hash table, too many columns"
11904 " in section table [in module %s]"),
11905 dwp_file->name);
11906 }
11907 memset (ids, 255, sizeof_ids);
11908 memset (ids_seen, 255, sizeof (ids_seen));
11909 for (i = 0; i < nr_columns; ++i)
11910 {
11911 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11912
11913 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11914 {
11915 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11916 " in section table [in module %s]"),
11917 id, dwp_file->name);
11918 }
11919 if (ids_seen[id] != -1)
11920 {
11921 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11922 " id %d in section table [in module %s]"),
11923 id, dwp_file->name);
11924 }
11925 ids_seen[id] = i;
11926 ids[i] = id;
11927 }
11928 /* Must have exactly one info or types section. */
11929 if (((ids_seen[DW_SECT_INFO] != -1)
11930 + (ids_seen[DW_SECT_TYPES] != -1))
11931 != 1)
11932 {
11933 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11934 " DWO info/types section [in module %s]"),
11935 dwp_file->name);
11936 }
11937 /* Must have an abbrev section. */
11938 if (ids_seen[DW_SECT_ABBREV] == -1)
11939 {
11940 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11941 " section [in module %s]"),
11942 dwp_file->name);
11943 }
11944 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11945 htab->section_pool.v2.sizes =
11946 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11947 * nr_units * nr_columns);
11948 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11949 * nr_units * nr_columns))
11950 > index_end)
11951 {
11952 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11953 " [in module %s]"),
11954 dwp_file->name);
11955 }
11956 }
11957 else /* version == 5 */
11958 {
11959 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11960 int *ids = htab->section_pool.v5.section_ids;
11961 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11962 /* Reverse map for error checking. */
11963 int ids_seen[DW_SECT_MAX_V5 + 1];
11964
11965 if (nr_columns < 2)
11966 {
11967 error (_("Dwarf Error: bad DWP hash table, too few columns"
11968 " in section table [in module %s]"),
11969 dwp_file->name);
11970 }
11971 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11972 {
11973 error (_("Dwarf Error: bad DWP hash table, too many columns"
11974 " in section table [in module %s]"),
11975 dwp_file->name);
11976 }
11977 memset (ids, 255, sizeof_ids);
11978 memset (ids_seen, 255, sizeof (ids_seen));
11979 for (int i = 0; i < nr_columns; ++i)
11980 {
11981 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11982
11983 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11984 {
11985 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11986 " in section table [in module %s]"),
11987 id, dwp_file->name);
11988 }
11989 if (ids_seen[id] != -1)
11990 {
11991 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11992 " id %d in section table [in module %s]"),
11993 id, dwp_file->name);
11994 }
11995 ids_seen[id] = i;
11996 ids[i] = id;
11997 }
11998 /* Must have seen an info section. */
11999 if (ids_seen[DW_SECT_INFO_V5] == -1)
12000 {
12001 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12002 " DWO info/types section [in module %s]"),
12003 dwp_file->name);
12004 }
12005 /* Must have an abbrev section. */
12006 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
12007 {
12008 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12009 " section [in module %s]"),
12010 dwp_file->name);
12011 }
12012 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12013 htab->section_pool.v5.sizes
12014 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
12015 * nr_units * nr_columns);
12016 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
12017 * nr_units * nr_columns))
12018 > index_end)
12019 {
12020 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12021 " [in module %s]"),
12022 dwp_file->name);
12023 }
12024 }
12025
12026 return htab;
12027 }
12028
12029 /* Update SECTIONS with the data from SECTP.
12030
12031 This function is like the other "locate" section routines, but in
12032 this context the sections to read comes from the DWP V1 hash table,
12033 not the full ELF section table.
12034
12035 The result is non-zero for success, or zero if an error was found. */
12036
12037 static int
12038 locate_v1_virtual_dwo_sections (asection *sectp,
12039 struct virtual_v1_dwo_sections *sections)
12040 {
12041 const struct dwop_section_names *names = &dwop_section_names;
12042
12043 if (names->abbrev_dwo.matches (sectp->name))
12044 {
12045 /* There can be only one. */
12046 if (sections->abbrev.s.section != NULL)
12047 return 0;
12048 sections->abbrev.s.section = sectp;
12049 sections->abbrev.size = bfd_section_size (sectp);
12050 }
12051 else if (names->info_dwo.matches (sectp->name)
12052 || names->types_dwo.matches (sectp->name))
12053 {
12054 /* There can be only one. */
12055 if (sections->info_or_types.s.section != NULL)
12056 return 0;
12057 sections->info_or_types.s.section = sectp;
12058 sections->info_or_types.size = bfd_section_size (sectp);
12059 }
12060 else if (names->line_dwo.matches (sectp->name))
12061 {
12062 /* There can be only one. */
12063 if (sections->line.s.section != NULL)
12064 return 0;
12065 sections->line.s.section = sectp;
12066 sections->line.size = bfd_section_size (sectp);
12067 }
12068 else if (names->loc_dwo.matches (sectp->name))
12069 {
12070 /* There can be only one. */
12071 if (sections->loc.s.section != NULL)
12072 return 0;
12073 sections->loc.s.section = sectp;
12074 sections->loc.size = bfd_section_size (sectp);
12075 }
12076 else if (names->macinfo_dwo.matches (sectp->name))
12077 {
12078 /* There can be only one. */
12079 if (sections->macinfo.s.section != NULL)
12080 return 0;
12081 sections->macinfo.s.section = sectp;
12082 sections->macinfo.size = bfd_section_size (sectp);
12083 }
12084 else if (names->macro_dwo.matches (sectp->name))
12085 {
12086 /* There can be only one. */
12087 if (sections->macro.s.section != NULL)
12088 return 0;
12089 sections->macro.s.section = sectp;
12090 sections->macro.size = bfd_section_size (sectp);
12091 }
12092 else if (names->str_offsets_dwo.matches (sectp->name))
12093 {
12094 /* There can be only one. */
12095 if (sections->str_offsets.s.section != NULL)
12096 return 0;
12097 sections->str_offsets.s.section = sectp;
12098 sections->str_offsets.size = bfd_section_size (sectp);
12099 }
12100 else
12101 {
12102 /* No other kind of section is valid. */
12103 return 0;
12104 }
12105
12106 return 1;
12107 }
12108
12109 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12110 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12111 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12112 This is for DWP version 1 files. */
12113
12114 static struct dwo_unit *
12115 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12116 struct dwp_file *dwp_file,
12117 uint32_t unit_index,
12118 const char *comp_dir,
12119 ULONGEST signature, int is_debug_types)
12120 {
12121 const struct dwp_hash_table *dwp_htab =
12122 is_debug_types ? dwp_file->tus : dwp_file->cus;
12123 bfd *dbfd = dwp_file->dbfd.get ();
12124 const char *kind = is_debug_types ? "TU" : "CU";
12125 struct dwo_file *dwo_file;
12126 struct dwo_unit *dwo_unit;
12127 struct virtual_v1_dwo_sections sections;
12128 void **dwo_file_slot;
12129 int i;
12130
12131 gdb_assert (dwp_file->version == 1);
12132
12133 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12134 kind, pulongest (unit_index), hex_string (signature),
12135 dwp_file->name);
12136
12137 /* Fetch the sections of this DWO unit.
12138 Put a limit on the number of sections we look for so that bad data
12139 doesn't cause us to loop forever. */
12140
12141 #define MAX_NR_V1_DWO_SECTIONS \
12142 (1 /* .debug_info or .debug_types */ \
12143 + 1 /* .debug_abbrev */ \
12144 + 1 /* .debug_line */ \
12145 + 1 /* .debug_loc */ \
12146 + 1 /* .debug_str_offsets */ \
12147 + 1 /* .debug_macro or .debug_macinfo */ \
12148 + 1 /* trailing zero */)
12149
12150 memset (&sections, 0, sizeof (sections));
12151
12152 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12153 {
12154 asection *sectp;
12155 uint32_t section_nr =
12156 read_4_bytes (dbfd,
12157 dwp_htab->section_pool.v1.indices
12158 + (unit_index + i) * sizeof (uint32_t));
12159
12160 if (section_nr == 0)
12161 break;
12162 if (section_nr >= dwp_file->num_sections)
12163 {
12164 error (_("Dwarf Error: bad DWP hash table, section number too large"
12165 " [in module %s]"),
12166 dwp_file->name);
12167 }
12168
12169 sectp = dwp_file->elf_sections[section_nr];
12170 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12171 {
12172 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12173 " [in module %s]"),
12174 dwp_file->name);
12175 }
12176 }
12177
12178 if (i < 2
12179 || sections.info_or_types.empty ()
12180 || sections.abbrev.empty ())
12181 {
12182 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12183 " [in module %s]"),
12184 dwp_file->name);
12185 }
12186 if (i == MAX_NR_V1_DWO_SECTIONS)
12187 {
12188 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12189 " [in module %s]"),
12190 dwp_file->name);
12191 }
12192
12193 /* It's easier for the rest of the code if we fake a struct dwo_file and
12194 have dwo_unit "live" in that. At least for now.
12195
12196 The DWP file can be made up of a random collection of CUs and TUs.
12197 However, for each CU + set of TUs that came from the same original DWO
12198 file, we can combine them back into a virtual DWO file to save space
12199 (fewer struct dwo_file objects to allocate). Remember that for really
12200 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12201
12202 std::string virtual_dwo_name =
12203 string_printf ("virtual-dwo/%d-%d-%d-%d",
12204 sections.abbrev.get_id (),
12205 sections.line.get_id (),
12206 sections.loc.get_id (),
12207 sections.str_offsets.get_id ());
12208 /* Can we use an existing virtual DWO file? */
12209 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12210 comp_dir);
12211 /* Create one if necessary. */
12212 if (*dwo_file_slot == NULL)
12213 {
12214 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12215 virtual_dwo_name.c_str ());
12216
12217 dwo_file = new struct dwo_file;
12218 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12219 dwo_file->comp_dir = comp_dir;
12220 dwo_file->sections.abbrev = sections.abbrev;
12221 dwo_file->sections.line = sections.line;
12222 dwo_file->sections.loc = sections.loc;
12223 dwo_file->sections.macinfo = sections.macinfo;
12224 dwo_file->sections.macro = sections.macro;
12225 dwo_file->sections.str_offsets = sections.str_offsets;
12226 /* The "str" section is global to the entire DWP file. */
12227 dwo_file->sections.str = dwp_file->sections.str;
12228 /* The info or types section is assigned below to dwo_unit,
12229 there's no need to record it in dwo_file.
12230 Also, we can't simply record type sections in dwo_file because
12231 we record a pointer into the vector in dwo_unit. As we collect more
12232 types we'll grow the vector and eventually have to reallocate space
12233 for it, invalidating all copies of pointers into the previous
12234 contents. */
12235 *dwo_file_slot = dwo_file;
12236 }
12237 else
12238 {
12239 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12240 virtual_dwo_name.c_str ());
12241
12242 dwo_file = (struct dwo_file *) *dwo_file_slot;
12243 }
12244
12245 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12246 dwo_unit->dwo_file = dwo_file;
12247 dwo_unit->signature = signature;
12248 dwo_unit->section =
12249 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12250 *dwo_unit->section = sections.info_or_types;
12251 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12252
12253 return dwo_unit;
12254 }
12255
12256 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12257 simplify them. Given a pointer to the containing section SECTION, and
12258 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12259 virtual section of just that piece. */
12260
12261 static struct dwarf2_section_info
12262 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12263 struct dwarf2_section_info *section,
12264 bfd_size_type offset, bfd_size_type size)
12265 {
12266 struct dwarf2_section_info result;
12267 asection *sectp;
12268
12269 gdb_assert (section != NULL);
12270 gdb_assert (!section->is_virtual);
12271
12272 memset (&result, 0, sizeof (result));
12273 result.s.containing_section = section;
12274 result.is_virtual = true;
12275
12276 if (size == 0)
12277 return result;
12278
12279 sectp = section->get_bfd_section ();
12280
12281 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12282 bounds of the real section. This is a pretty-rare event, so just
12283 flag an error (easier) instead of a warning and trying to cope. */
12284 if (sectp == NULL
12285 || offset + size > bfd_section_size (sectp))
12286 {
12287 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12288 " in section %s [in module %s]"),
12289 sectp ? bfd_section_name (sectp) : "<unknown>",
12290 objfile_name (per_objfile->objfile));
12291 }
12292
12293 result.virtual_offset = offset;
12294 result.size = size;
12295 return result;
12296 }
12297
12298 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12299 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12300 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12301 This is for DWP version 2 files. */
12302
12303 static struct dwo_unit *
12304 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12305 struct dwp_file *dwp_file,
12306 uint32_t unit_index,
12307 const char *comp_dir,
12308 ULONGEST signature, int is_debug_types)
12309 {
12310 const struct dwp_hash_table *dwp_htab =
12311 is_debug_types ? dwp_file->tus : dwp_file->cus;
12312 bfd *dbfd = dwp_file->dbfd.get ();
12313 const char *kind = is_debug_types ? "TU" : "CU";
12314 struct dwo_file *dwo_file;
12315 struct dwo_unit *dwo_unit;
12316 struct virtual_v2_or_v5_dwo_sections sections;
12317 void **dwo_file_slot;
12318 int i;
12319
12320 gdb_assert (dwp_file->version == 2);
12321
12322 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12323 kind, pulongest (unit_index), hex_string (signature),
12324 dwp_file->name);
12325
12326 /* Fetch the section offsets of this DWO unit. */
12327
12328 memset (&sections, 0, sizeof (sections));
12329
12330 for (i = 0; i < dwp_htab->nr_columns; ++i)
12331 {
12332 uint32_t offset = read_4_bytes (dbfd,
12333 dwp_htab->section_pool.v2.offsets
12334 + (((unit_index - 1) * dwp_htab->nr_columns
12335 + i)
12336 * sizeof (uint32_t)));
12337 uint32_t size = read_4_bytes (dbfd,
12338 dwp_htab->section_pool.v2.sizes
12339 + (((unit_index - 1) * dwp_htab->nr_columns
12340 + i)
12341 * sizeof (uint32_t)));
12342
12343 switch (dwp_htab->section_pool.v2.section_ids[i])
12344 {
12345 case DW_SECT_INFO:
12346 case DW_SECT_TYPES:
12347 sections.info_or_types_offset = offset;
12348 sections.info_or_types_size = size;
12349 break;
12350 case DW_SECT_ABBREV:
12351 sections.abbrev_offset = offset;
12352 sections.abbrev_size = size;
12353 break;
12354 case DW_SECT_LINE:
12355 sections.line_offset = offset;
12356 sections.line_size = size;
12357 break;
12358 case DW_SECT_LOC:
12359 sections.loc_offset = offset;
12360 sections.loc_size = size;
12361 break;
12362 case DW_SECT_STR_OFFSETS:
12363 sections.str_offsets_offset = offset;
12364 sections.str_offsets_size = size;
12365 break;
12366 case DW_SECT_MACINFO:
12367 sections.macinfo_offset = offset;
12368 sections.macinfo_size = size;
12369 break;
12370 case DW_SECT_MACRO:
12371 sections.macro_offset = offset;
12372 sections.macro_size = size;
12373 break;
12374 }
12375 }
12376
12377 /* It's easier for the rest of the code if we fake a struct dwo_file and
12378 have dwo_unit "live" in that. At least for now.
12379
12380 The DWP file can be made up of a random collection of CUs and TUs.
12381 However, for each CU + set of TUs that came from the same original DWO
12382 file, we can combine them back into a virtual DWO file to save space
12383 (fewer struct dwo_file objects to allocate). Remember that for really
12384 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12385
12386 std::string virtual_dwo_name =
12387 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12388 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12389 (long) (sections.line_size ? sections.line_offset : 0),
12390 (long) (sections.loc_size ? sections.loc_offset : 0),
12391 (long) (sections.str_offsets_size
12392 ? sections.str_offsets_offset : 0));
12393 /* Can we use an existing virtual DWO file? */
12394 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12395 comp_dir);
12396 /* Create one if necessary. */
12397 if (*dwo_file_slot == NULL)
12398 {
12399 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12400 virtual_dwo_name.c_str ());
12401
12402 dwo_file = new struct dwo_file;
12403 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12404 dwo_file->comp_dir = comp_dir;
12405 dwo_file->sections.abbrev =
12406 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12407 sections.abbrev_offset,
12408 sections.abbrev_size);
12409 dwo_file->sections.line =
12410 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12411 sections.line_offset,
12412 sections.line_size);
12413 dwo_file->sections.loc =
12414 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12415 sections.loc_offset, sections.loc_size);
12416 dwo_file->sections.macinfo =
12417 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12418 sections.macinfo_offset,
12419 sections.macinfo_size);
12420 dwo_file->sections.macro =
12421 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12422 sections.macro_offset,
12423 sections.macro_size);
12424 dwo_file->sections.str_offsets =
12425 create_dwp_v2_or_v5_section (per_objfile,
12426 &dwp_file->sections.str_offsets,
12427 sections.str_offsets_offset,
12428 sections.str_offsets_size);
12429 /* The "str" section is global to the entire DWP file. */
12430 dwo_file->sections.str = dwp_file->sections.str;
12431 /* The info or types section is assigned below to dwo_unit,
12432 there's no need to record it in dwo_file.
12433 Also, we can't simply record type sections in dwo_file because
12434 we record a pointer into the vector in dwo_unit. As we collect more
12435 types we'll grow the vector and eventually have to reallocate space
12436 for it, invalidating all copies of pointers into the previous
12437 contents. */
12438 *dwo_file_slot = dwo_file;
12439 }
12440 else
12441 {
12442 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12443 virtual_dwo_name.c_str ());
12444
12445 dwo_file = (struct dwo_file *) *dwo_file_slot;
12446 }
12447
12448 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12449 dwo_unit->dwo_file = dwo_file;
12450 dwo_unit->signature = signature;
12451 dwo_unit->section =
12452 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12453 *dwo_unit->section = create_dwp_v2_or_v5_section
12454 (per_objfile,
12455 is_debug_types
12456 ? &dwp_file->sections.types
12457 : &dwp_file->sections.info,
12458 sections.info_or_types_offset,
12459 sections.info_or_types_size);
12460 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12461
12462 return dwo_unit;
12463 }
12464
12465 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12466 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12467 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12468 This is for DWP version 5 files. */
12469
12470 static struct dwo_unit *
12471 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12472 struct dwp_file *dwp_file,
12473 uint32_t unit_index,
12474 const char *comp_dir,
12475 ULONGEST signature, int is_debug_types)
12476 {
12477 const struct dwp_hash_table *dwp_htab
12478 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12479 bfd *dbfd = dwp_file->dbfd.get ();
12480 const char *kind = is_debug_types ? "TU" : "CU";
12481 struct dwo_file *dwo_file;
12482 struct dwo_unit *dwo_unit;
12483 struct virtual_v2_or_v5_dwo_sections sections {};
12484 void **dwo_file_slot;
12485
12486 gdb_assert (dwp_file->version == 5);
12487
12488 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12489 kind, pulongest (unit_index), hex_string (signature),
12490 dwp_file->name);
12491
12492 /* Fetch the section offsets of this DWO unit. */
12493
12494 /* memset (&sections, 0, sizeof (sections)); */
12495
12496 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12497 {
12498 uint32_t offset = read_4_bytes (dbfd,
12499 dwp_htab->section_pool.v5.offsets
12500 + (((unit_index - 1)
12501 * dwp_htab->nr_columns
12502 + i)
12503 * sizeof (uint32_t)));
12504 uint32_t size = read_4_bytes (dbfd,
12505 dwp_htab->section_pool.v5.sizes
12506 + (((unit_index - 1) * dwp_htab->nr_columns
12507 + i)
12508 * sizeof (uint32_t)));
12509
12510 switch (dwp_htab->section_pool.v5.section_ids[i])
12511 {
12512 case DW_SECT_ABBREV_V5:
12513 sections.abbrev_offset = offset;
12514 sections.abbrev_size = size;
12515 break;
12516 case DW_SECT_INFO_V5:
12517 sections.info_or_types_offset = offset;
12518 sections.info_or_types_size = size;
12519 break;
12520 case DW_SECT_LINE_V5:
12521 sections.line_offset = offset;
12522 sections.line_size = size;
12523 break;
12524 case DW_SECT_LOCLISTS_V5:
12525 sections.loclists_offset = offset;
12526 sections.loclists_size = size;
12527 break;
12528 case DW_SECT_MACRO_V5:
12529 sections.macro_offset = offset;
12530 sections.macro_size = size;
12531 break;
12532 case DW_SECT_RNGLISTS_V5:
12533 sections.rnglists_offset = offset;
12534 sections.rnglists_size = size;
12535 break;
12536 case DW_SECT_STR_OFFSETS_V5:
12537 sections.str_offsets_offset = offset;
12538 sections.str_offsets_size = size;
12539 break;
12540 case DW_SECT_RESERVED_V5:
12541 default:
12542 break;
12543 }
12544 }
12545
12546 /* It's easier for the rest of the code if we fake a struct dwo_file and
12547 have dwo_unit "live" in that. At least for now.
12548
12549 The DWP file can be made up of a random collection of CUs and TUs.
12550 However, for each CU + set of TUs that came from the same original DWO
12551 file, we can combine them back into a virtual DWO file to save space
12552 (fewer struct dwo_file objects to allocate). Remember that for really
12553 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12554
12555 std::string virtual_dwo_name =
12556 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12557 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12558 (long) (sections.line_size ? sections.line_offset : 0),
12559 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12560 (long) (sections.str_offsets_size
12561 ? sections.str_offsets_offset : 0),
12562 (long) (sections.macro_size ? sections.macro_offset : 0),
12563 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12564 /* Can we use an existing virtual DWO file? */
12565 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12566 virtual_dwo_name.c_str (),
12567 comp_dir);
12568 /* Create one if necessary. */
12569 if (*dwo_file_slot == NULL)
12570 {
12571 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12572 virtual_dwo_name.c_str ());
12573
12574 dwo_file = new struct dwo_file;
12575 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12576 dwo_file->comp_dir = comp_dir;
12577 dwo_file->sections.abbrev =
12578 create_dwp_v2_or_v5_section (per_objfile,
12579 &dwp_file->sections.abbrev,
12580 sections.abbrev_offset,
12581 sections.abbrev_size);
12582 dwo_file->sections.line =
12583 create_dwp_v2_or_v5_section (per_objfile,
12584 &dwp_file->sections.line,
12585 sections.line_offset, sections.line_size);
12586 dwo_file->sections.macro =
12587 create_dwp_v2_or_v5_section (per_objfile,
12588 &dwp_file->sections.macro,
12589 sections.macro_offset,
12590 sections.macro_size);
12591 dwo_file->sections.loclists =
12592 create_dwp_v2_or_v5_section (per_objfile,
12593 &dwp_file->sections.loclists,
12594 sections.loclists_offset,
12595 sections.loclists_size);
12596 dwo_file->sections.rnglists =
12597 create_dwp_v2_or_v5_section (per_objfile,
12598 &dwp_file->sections.rnglists,
12599 sections.rnglists_offset,
12600 sections.rnglists_size);
12601 dwo_file->sections.str_offsets =
12602 create_dwp_v2_or_v5_section (per_objfile,
12603 &dwp_file->sections.str_offsets,
12604 sections.str_offsets_offset,
12605 sections.str_offsets_size);
12606 /* The "str" section is global to the entire DWP file. */
12607 dwo_file->sections.str = dwp_file->sections.str;
12608 /* The info or types section is assigned below to dwo_unit,
12609 there's no need to record it in dwo_file.
12610 Also, we can't simply record type sections in dwo_file because
12611 we record a pointer into the vector in dwo_unit. As we collect more
12612 types we'll grow the vector and eventually have to reallocate space
12613 for it, invalidating all copies of pointers into the previous
12614 contents. */
12615 *dwo_file_slot = dwo_file;
12616 }
12617 else
12618 {
12619 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12620 virtual_dwo_name.c_str ());
12621
12622 dwo_file = (struct dwo_file *) *dwo_file_slot;
12623 }
12624
12625 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12626 dwo_unit->dwo_file = dwo_file;
12627 dwo_unit->signature = signature;
12628 dwo_unit->section
12629 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12630 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12631 &dwp_file->sections.info,
12632 sections.info_or_types_offset,
12633 sections.info_or_types_size);
12634 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12635
12636 return dwo_unit;
12637 }
12638
12639 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12640 Returns NULL if the signature isn't found. */
12641
12642 static struct dwo_unit *
12643 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12644 struct dwp_file *dwp_file, const char *comp_dir,
12645 ULONGEST signature, int is_debug_types)
12646 {
12647 const struct dwp_hash_table *dwp_htab =
12648 is_debug_types ? dwp_file->tus : dwp_file->cus;
12649 bfd *dbfd = dwp_file->dbfd.get ();
12650 uint32_t mask = dwp_htab->nr_slots - 1;
12651 uint32_t hash = signature & mask;
12652 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12653 unsigned int i;
12654 void **slot;
12655 struct dwo_unit find_dwo_cu;
12656
12657 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12658 find_dwo_cu.signature = signature;
12659 slot = htab_find_slot (is_debug_types
12660 ? dwp_file->loaded_tus.get ()
12661 : dwp_file->loaded_cus.get (),
12662 &find_dwo_cu, INSERT);
12663
12664 if (*slot != NULL)
12665 return (struct dwo_unit *) *slot;
12666
12667 /* Use a for loop so that we don't loop forever on bad debug info. */
12668 for (i = 0; i < dwp_htab->nr_slots; ++i)
12669 {
12670 ULONGEST signature_in_table;
12671
12672 signature_in_table =
12673 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12674 if (signature_in_table == signature)
12675 {
12676 uint32_t unit_index =
12677 read_4_bytes (dbfd,
12678 dwp_htab->unit_table + hash * sizeof (uint32_t));
12679
12680 if (dwp_file->version == 1)
12681 {
12682 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12683 unit_index, comp_dir,
12684 signature, is_debug_types);
12685 }
12686 else if (dwp_file->version == 2)
12687 {
12688 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12689 unit_index, comp_dir,
12690 signature, is_debug_types);
12691 }
12692 else /* version == 5 */
12693 {
12694 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12695 unit_index, comp_dir,
12696 signature, is_debug_types);
12697 }
12698 return (struct dwo_unit *) *slot;
12699 }
12700 if (signature_in_table == 0)
12701 return NULL;
12702 hash = (hash + hash2) & mask;
12703 }
12704
12705 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12706 " [in module %s]"),
12707 dwp_file->name);
12708 }
12709
12710 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12711 Open the file specified by FILE_NAME and hand it off to BFD for
12712 preliminary analysis. Return a newly initialized bfd *, which
12713 includes a canonicalized copy of FILE_NAME.
12714 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12715 SEARCH_CWD is true if the current directory is to be searched.
12716 It will be searched before debug-file-directory.
12717 If successful, the file is added to the bfd include table of the
12718 objfile's bfd (see gdb_bfd_record_inclusion).
12719 If unable to find/open the file, return NULL.
12720 NOTE: This function is derived from symfile_bfd_open. */
12721
12722 static gdb_bfd_ref_ptr
12723 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12724 const char *file_name, int is_dwp, int search_cwd)
12725 {
12726 int desc;
12727 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12728 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12729 to debug_file_directory. */
12730 const char *search_path;
12731 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12732
12733 gdb::unique_xmalloc_ptr<char> search_path_holder;
12734 if (search_cwd)
12735 {
12736 if (*debug_file_directory != '\0')
12737 {
12738 search_path_holder.reset (concat (".", dirname_separator_string,
12739 debug_file_directory,
12740 (char *) NULL));
12741 search_path = search_path_holder.get ();
12742 }
12743 else
12744 search_path = ".";
12745 }
12746 else
12747 search_path = debug_file_directory;
12748
12749 /* Add the path for the executable binary to the list of search paths. */
12750 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12751 search_path_holder.reset (concat (objfile_dir.c_str (),
12752 dirname_separator_string,
12753 search_path, nullptr));
12754 search_path = search_path_holder.get ();
12755
12756 openp_flags flags = OPF_RETURN_REALPATH;
12757 if (is_dwp)
12758 flags |= OPF_SEARCH_IN_PATH;
12759
12760 gdb::unique_xmalloc_ptr<char> absolute_name;
12761 desc = openp (search_path, flags, file_name,
12762 O_RDONLY | O_BINARY, &absolute_name);
12763 if (desc < 0)
12764 return NULL;
12765
12766 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12767 gnutarget, desc));
12768 if (sym_bfd == NULL)
12769 return NULL;
12770 bfd_set_cacheable (sym_bfd.get (), 1);
12771
12772 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12773 return NULL;
12774
12775 /* Success. Record the bfd as having been included by the objfile's bfd.
12776 This is important because things like demangled_names_hash lives in the
12777 objfile's per_bfd space and may have references to things like symbol
12778 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12779 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12780
12781 return sym_bfd;
12782 }
12783
12784 /* Try to open DWO file FILE_NAME.
12785 COMP_DIR is the DW_AT_comp_dir attribute.
12786 The result is the bfd handle of the file.
12787 If there is a problem finding or opening the file, return NULL.
12788 Upon success, the canonicalized path of the file is stored in the bfd,
12789 same as symfile_bfd_open. */
12790
12791 static gdb_bfd_ref_ptr
12792 open_dwo_file (dwarf2_per_objfile *per_objfile,
12793 const char *file_name, const char *comp_dir)
12794 {
12795 if (IS_ABSOLUTE_PATH (file_name))
12796 return try_open_dwop_file (per_objfile, file_name,
12797 0 /*is_dwp*/, 0 /*search_cwd*/);
12798
12799 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12800
12801 if (comp_dir != NULL)
12802 {
12803 gdb::unique_xmalloc_ptr<char> path_to_try
12804 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12805
12806 /* NOTE: If comp_dir is a relative path, this will also try the
12807 search path, which seems useful. */
12808 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12809 0 /*is_dwp*/,
12810 1 /*search_cwd*/));
12811 if (abfd != NULL)
12812 return abfd;
12813 }
12814
12815 /* That didn't work, try debug-file-directory, which, despite its name,
12816 is a list of paths. */
12817
12818 if (*debug_file_directory == '\0')
12819 return NULL;
12820
12821 return try_open_dwop_file (per_objfile, file_name,
12822 0 /*is_dwp*/, 1 /*search_cwd*/);
12823 }
12824
12825 /* This function is mapped across the sections and remembers the offset and
12826 size of each of the DWO debugging sections we are interested in. */
12827
12828 static void
12829 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12830 dwo_sections *dwo_sections)
12831 {
12832 const struct dwop_section_names *names = &dwop_section_names;
12833
12834 if (names->abbrev_dwo.matches (sectp->name))
12835 {
12836 dwo_sections->abbrev.s.section = sectp;
12837 dwo_sections->abbrev.size = bfd_section_size (sectp);
12838 }
12839 else if (names->info_dwo.matches (sectp->name))
12840 {
12841 dwo_sections->info.s.section = sectp;
12842 dwo_sections->info.size = bfd_section_size (sectp);
12843 }
12844 else if (names->line_dwo.matches (sectp->name))
12845 {
12846 dwo_sections->line.s.section = sectp;
12847 dwo_sections->line.size = bfd_section_size (sectp);
12848 }
12849 else if (names->loc_dwo.matches (sectp->name))
12850 {
12851 dwo_sections->loc.s.section = sectp;
12852 dwo_sections->loc.size = bfd_section_size (sectp);
12853 }
12854 else if (names->loclists_dwo.matches (sectp->name))
12855 {
12856 dwo_sections->loclists.s.section = sectp;
12857 dwo_sections->loclists.size = bfd_section_size (sectp);
12858 }
12859 else if (names->macinfo_dwo.matches (sectp->name))
12860 {
12861 dwo_sections->macinfo.s.section = sectp;
12862 dwo_sections->macinfo.size = bfd_section_size (sectp);
12863 }
12864 else if (names->macro_dwo.matches (sectp->name))
12865 {
12866 dwo_sections->macro.s.section = sectp;
12867 dwo_sections->macro.size = bfd_section_size (sectp);
12868 }
12869 else if (names->rnglists_dwo.matches (sectp->name))
12870 {
12871 dwo_sections->rnglists.s.section = sectp;
12872 dwo_sections->rnglists.size = bfd_section_size (sectp);
12873 }
12874 else if (names->str_dwo.matches (sectp->name))
12875 {
12876 dwo_sections->str.s.section = sectp;
12877 dwo_sections->str.size = bfd_section_size (sectp);
12878 }
12879 else if (names->str_offsets_dwo.matches (sectp->name))
12880 {
12881 dwo_sections->str_offsets.s.section = sectp;
12882 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12883 }
12884 else if (names->types_dwo.matches (sectp->name))
12885 {
12886 struct dwarf2_section_info type_section;
12887
12888 memset (&type_section, 0, sizeof (type_section));
12889 type_section.s.section = sectp;
12890 type_section.size = bfd_section_size (sectp);
12891 dwo_sections->types.push_back (type_section);
12892 }
12893 }
12894
12895 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12896 by PER_CU. This is for the non-DWP case.
12897 The result is NULL if DWO_NAME can't be found. */
12898
12899 static struct dwo_file *
12900 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12901 const char *comp_dir)
12902 {
12903 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12904
12905 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12906 if (dbfd == NULL)
12907 {
12908 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12909
12910 return NULL;
12911 }
12912
12913 dwo_file_up dwo_file (new struct dwo_file);
12914 dwo_file->dwo_name = dwo_name;
12915 dwo_file->comp_dir = comp_dir;
12916 dwo_file->dbfd = std::move (dbfd);
12917
12918 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12919 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12920 &dwo_file->sections);
12921
12922 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12923 dwo_file->cus);
12924
12925 if (cu->per_cu->dwarf_version < 5)
12926 {
12927 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12928 dwo_file->sections.types, dwo_file->tus);
12929 }
12930 else
12931 {
12932 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12933 &dwo_file->sections.info, dwo_file->tus,
12934 rcuh_kind::COMPILE);
12935 }
12936
12937 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12938
12939 return dwo_file.release ();
12940 }
12941
12942 /* This function is mapped across the sections and remembers the offset and
12943 size of each of the DWP debugging sections common to version 1 and 2 that
12944 we are interested in. */
12945
12946 static void
12947 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12948 dwp_file *dwp_file)
12949 {
12950 const struct dwop_section_names *names = &dwop_section_names;
12951 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12952
12953 /* Record the ELF section number for later lookup: this is what the
12954 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12955 gdb_assert (elf_section_nr < dwp_file->num_sections);
12956 dwp_file->elf_sections[elf_section_nr] = sectp;
12957
12958 /* Look for specific sections that we need. */
12959 if (names->str_dwo.matches (sectp->name))
12960 {
12961 dwp_file->sections.str.s.section = sectp;
12962 dwp_file->sections.str.size = bfd_section_size (sectp);
12963 }
12964 else if (names->cu_index.matches (sectp->name))
12965 {
12966 dwp_file->sections.cu_index.s.section = sectp;
12967 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12968 }
12969 else if (names->tu_index.matches (sectp->name))
12970 {
12971 dwp_file->sections.tu_index.s.section = sectp;
12972 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12973 }
12974 }
12975
12976 /* This function is mapped across the sections and remembers the offset and
12977 size of each of the DWP version 2 debugging sections that we are interested
12978 in. This is split into a separate function because we don't know if we
12979 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12980
12981 static void
12982 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12983 {
12984 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12985 const struct dwop_section_names *names = &dwop_section_names;
12986 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12987
12988 /* Record the ELF section number for later lookup: this is what the
12989 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12990 gdb_assert (elf_section_nr < dwp_file->num_sections);
12991 dwp_file->elf_sections[elf_section_nr] = sectp;
12992
12993 /* Look for specific sections that we need. */
12994 if (names->abbrev_dwo.matches (sectp->name))
12995 {
12996 dwp_file->sections.abbrev.s.section = sectp;
12997 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12998 }
12999 else if (names->info_dwo.matches (sectp->name))
13000 {
13001 dwp_file->sections.info.s.section = sectp;
13002 dwp_file->sections.info.size = bfd_section_size (sectp);
13003 }
13004 else if (names->line_dwo.matches (sectp->name))
13005 {
13006 dwp_file->sections.line.s.section = sectp;
13007 dwp_file->sections.line.size = bfd_section_size (sectp);
13008 }
13009 else if (names->loc_dwo.matches (sectp->name))
13010 {
13011 dwp_file->sections.loc.s.section = sectp;
13012 dwp_file->sections.loc.size = bfd_section_size (sectp);
13013 }
13014 else if (names->macinfo_dwo.matches (sectp->name))
13015 {
13016 dwp_file->sections.macinfo.s.section = sectp;
13017 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13018 }
13019 else if (names->macro_dwo.matches (sectp->name))
13020 {
13021 dwp_file->sections.macro.s.section = sectp;
13022 dwp_file->sections.macro.size = bfd_section_size (sectp);
13023 }
13024 else if (names->str_offsets_dwo.matches (sectp->name))
13025 {
13026 dwp_file->sections.str_offsets.s.section = sectp;
13027 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13028 }
13029 else if (names->types_dwo.matches (sectp->name))
13030 {
13031 dwp_file->sections.types.s.section = sectp;
13032 dwp_file->sections.types.size = bfd_section_size (sectp);
13033 }
13034 }
13035
13036 /* This function is mapped across the sections and remembers the offset and
13037 size of each of the DWP version 5 debugging sections that we are interested
13038 in. This is split into a separate function because we don't know if we
13039 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13040
13041 static void
13042 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13043 {
13044 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13045 const struct dwop_section_names *names = &dwop_section_names;
13046 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13047
13048 /* Record the ELF section number for later lookup: this is what the
13049 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13050 gdb_assert (elf_section_nr < dwp_file->num_sections);
13051 dwp_file->elf_sections[elf_section_nr] = sectp;
13052
13053 /* Look for specific sections that we need. */
13054 if (names->abbrev_dwo.matches (sectp->name))
13055 {
13056 dwp_file->sections.abbrev.s.section = sectp;
13057 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13058 }
13059 else if (names->info_dwo.matches (sectp->name))
13060 {
13061 dwp_file->sections.info.s.section = sectp;
13062 dwp_file->sections.info.size = bfd_section_size (sectp);
13063 }
13064 else if (names->line_dwo.matches (sectp->name))
13065 {
13066 dwp_file->sections.line.s.section = sectp;
13067 dwp_file->sections.line.size = bfd_section_size (sectp);
13068 }
13069 else if (names->loclists_dwo.matches (sectp->name))
13070 {
13071 dwp_file->sections.loclists.s.section = sectp;
13072 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13073 }
13074 else if (names->macro_dwo.matches (sectp->name))
13075 {
13076 dwp_file->sections.macro.s.section = sectp;
13077 dwp_file->sections.macro.size = bfd_section_size (sectp);
13078 }
13079 else if (names->rnglists_dwo.matches (sectp->name))
13080 {
13081 dwp_file->sections.rnglists.s.section = sectp;
13082 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13083 }
13084 else if (names->str_offsets_dwo.matches (sectp->name))
13085 {
13086 dwp_file->sections.str_offsets.s.section = sectp;
13087 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13088 }
13089 }
13090
13091 /* Hash function for dwp_file loaded CUs/TUs. */
13092
13093 static hashval_t
13094 hash_dwp_loaded_cutus (const void *item)
13095 {
13096 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13097
13098 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13099 return dwo_unit->signature;
13100 }
13101
13102 /* Equality function for dwp_file loaded CUs/TUs. */
13103
13104 static int
13105 eq_dwp_loaded_cutus (const void *a, const void *b)
13106 {
13107 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13108 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13109
13110 return dua->signature == dub->signature;
13111 }
13112
13113 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13114
13115 static htab_up
13116 allocate_dwp_loaded_cutus_table ()
13117 {
13118 return htab_up (htab_create_alloc (3,
13119 hash_dwp_loaded_cutus,
13120 eq_dwp_loaded_cutus,
13121 NULL, xcalloc, xfree));
13122 }
13123
13124 /* Try to open DWP file FILE_NAME.
13125 The result is the bfd handle of the file.
13126 If there is a problem finding or opening the file, return NULL.
13127 Upon success, the canonicalized path of the file is stored in the bfd,
13128 same as symfile_bfd_open. */
13129
13130 static gdb_bfd_ref_ptr
13131 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13132 {
13133 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13134 1 /*is_dwp*/,
13135 1 /*search_cwd*/));
13136 if (abfd != NULL)
13137 return abfd;
13138
13139 /* Work around upstream bug 15652.
13140 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13141 [Whether that's a "bug" is debatable, but it is getting in our way.]
13142 We have no real idea where the dwp file is, because gdb's realpath-ing
13143 of the executable's path may have discarded the needed info.
13144 [IWBN if the dwp file name was recorded in the executable, akin to
13145 .gnu_debuglink, but that doesn't exist yet.]
13146 Strip the directory from FILE_NAME and search again. */
13147 if (*debug_file_directory != '\0')
13148 {
13149 /* Don't implicitly search the current directory here.
13150 If the user wants to search "." to handle this case,
13151 it must be added to debug-file-directory. */
13152 return try_open_dwop_file (per_objfile, lbasename (file_name),
13153 1 /*is_dwp*/,
13154 0 /*search_cwd*/);
13155 }
13156
13157 return NULL;
13158 }
13159
13160 /* Initialize the use of the DWP file for the current objfile.
13161 By convention the name of the DWP file is ${objfile}.dwp.
13162 The result is NULL if it can't be found. */
13163
13164 static std::unique_ptr<struct dwp_file>
13165 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13166 {
13167 struct objfile *objfile = per_objfile->objfile;
13168
13169 /* Try to find first .dwp for the binary file before any symbolic links
13170 resolving. */
13171
13172 /* If the objfile is a debug file, find the name of the real binary
13173 file and get the name of dwp file from there. */
13174 std::string dwp_name;
13175 if (objfile->separate_debug_objfile_backlink != NULL)
13176 {
13177 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13178 const char *backlink_basename = lbasename (backlink->original_name);
13179
13180 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13181 }
13182 else
13183 dwp_name = objfile->original_name;
13184
13185 dwp_name += ".dwp";
13186
13187 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13188 if (dbfd == NULL
13189 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13190 {
13191 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13192 dwp_name = objfile_name (objfile);
13193 dwp_name += ".dwp";
13194 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13195 }
13196
13197 if (dbfd == NULL)
13198 {
13199 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13200
13201 return std::unique_ptr<dwp_file> ();
13202 }
13203
13204 const char *name = bfd_get_filename (dbfd.get ());
13205 std::unique_ptr<struct dwp_file> dwp_file
13206 (new struct dwp_file (name, std::move (dbfd)));
13207
13208 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13209 dwp_file->elf_sections =
13210 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13211 dwp_file->num_sections, asection *);
13212
13213 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13214 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13215 dwp_file.get ());
13216
13217 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13218
13219 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13220
13221 /* The DWP file version is stored in the hash table. Oh well. */
13222 if (dwp_file->cus && dwp_file->tus
13223 && dwp_file->cus->version != dwp_file->tus->version)
13224 {
13225 /* Technically speaking, we should try to limp along, but this is
13226 pretty bizarre. We use pulongest here because that's the established
13227 portability solution (e.g, we cannot use %u for uint32_t). */
13228 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13229 " TU version %s [in DWP file %s]"),
13230 pulongest (dwp_file->cus->version),
13231 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13232 }
13233
13234 if (dwp_file->cus)
13235 dwp_file->version = dwp_file->cus->version;
13236 else if (dwp_file->tus)
13237 dwp_file->version = dwp_file->tus->version;
13238 else
13239 dwp_file->version = 2;
13240
13241 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13242 {
13243 if (dwp_file->version == 2)
13244 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13245 dwp_file.get ());
13246 else
13247 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13248 dwp_file.get ());
13249 }
13250
13251 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13252 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13253
13254 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13255 dwarf_read_debug_printf (" %s CUs, %s TUs",
13256 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13257 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13258
13259 return dwp_file;
13260 }
13261
13262 /* Wrapper around open_and_init_dwp_file, only open it once. */
13263
13264 static struct dwp_file *
13265 get_dwp_file (dwarf2_per_objfile *per_objfile)
13266 {
13267 if (!per_objfile->per_bfd->dwp_checked)
13268 {
13269 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13270 per_objfile->per_bfd->dwp_checked = 1;
13271 }
13272 return per_objfile->per_bfd->dwp_file.get ();
13273 }
13274
13275 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13276 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13277 or in the DWP file for the objfile, referenced by THIS_UNIT.
13278 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13279 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13280
13281 This is called, for example, when wanting to read a variable with a
13282 complex location. Therefore we don't want to do file i/o for every call.
13283 Therefore we don't want to look for a DWO file on every call.
13284 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13285 then we check if we've already seen DWO_NAME, and only THEN do we check
13286 for a DWO file.
13287
13288 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13289 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13290
13291 static struct dwo_unit *
13292 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13293 ULONGEST signature, int is_debug_types)
13294 {
13295 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13296 struct objfile *objfile = per_objfile->objfile;
13297 const char *kind = is_debug_types ? "TU" : "CU";
13298 void **dwo_file_slot;
13299 struct dwo_file *dwo_file;
13300 struct dwp_file *dwp_file;
13301
13302 /* First see if there's a DWP file.
13303 If we have a DWP file but didn't find the DWO inside it, don't
13304 look for the original DWO file. It makes gdb behave differently
13305 depending on whether one is debugging in the build tree. */
13306
13307 dwp_file = get_dwp_file (per_objfile);
13308 if (dwp_file != NULL)
13309 {
13310 const struct dwp_hash_table *dwp_htab =
13311 is_debug_types ? dwp_file->tus : dwp_file->cus;
13312
13313 if (dwp_htab != NULL)
13314 {
13315 struct dwo_unit *dwo_cutu =
13316 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13317 is_debug_types);
13318
13319 if (dwo_cutu != NULL)
13320 {
13321 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13322 kind, hex_string (signature),
13323 host_address_to_string (dwo_cutu));
13324
13325 return dwo_cutu;
13326 }
13327 }
13328 }
13329 else
13330 {
13331 /* No DWP file, look for the DWO file. */
13332
13333 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13334 if (*dwo_file_slot == NULL)
13335 {
13336 /* Read in the file and build a table of the CUs/TUs it contains. */
13337 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13338 }
13339 /* NOTE: This will be NULL if unable to open the file. */
13340 dwo_file = (struct dwo_file *) *dwo_file_slot;
13341
13342 if (dwo_file != NULL)
13343 {
13344 struct dwo_unit *dwo_cutu = NULL;
13345
13346 if (is_debug_types && dwo_file->tus)
13347 {
13348 struct dwo_unit find_dwo_cutu;
13349
13350 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13351 find_dwo_cutu.signature = signature;
13352 dwo_cutu
13353 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13354 &find_dwo_cutu);
13355 }
13356 else if (!is_debug_types && dwo_file->cus)
13357 {
13358 struct dwo_unit find_dwo_cutu;
13359
13360 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13361 find_dwo_cutu.signature = signature;
13362 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13363 &find_dwo_cutu);
13364 }
13365
13366 if (dwo_cutu != NULL)
13367 {
13368 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13369 kind, dwo_name, hex_string (signature),
13370 host_address_to_string (dwo_cutu));
13371
13372 return dwo_cutu;
13373 }
13374 }
13375 }
13376
13377 /* We didn't find it. This could mean a dwo_id mismatch, or
13378 someone deleted the DWO/DWP file, or the search path isn't set up
13379 correctly to find the file. */
13380
13381 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13382 kind, dwo_name, hex_string (signature));
13383
13384 /* This is a warning and not a complaint because it can be caused by
13385 pilot error (e.g., user accidentally deleting the DWO). */
13386 {
13387 /* Print the name of the DWP file if we looked there, helps the user
13388 better diagnose the problem. */
13389 std::string dwp_text;
13390
13391 if (dwp_file != NULL)
13392 dwp_text = string_printf (" [in DWP file %s]",
13393 lbasename (dwp_file->name));
13394
13395 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13396 " [in module %s]"),
13397 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13398 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13399 }
13400 return NULL;
13401 }
13402
13403 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13404 See lookup_dwo_cutu_unit for details. */
13405
13406 static struct dwo_unit *
13407 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13408 ULONGEST signature)
13409 {
13410 gdb_assert (!cu->per_cu->is_debug_types);
13411
13412 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13413 }
13414
13415 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13416 See lookup_dwo_cutu_unit for details. */
13417
13418 static struct dwo_unit *
13419 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13420 {
13421 gdb_assert (cu->per_cu->is_debug_types);
13422
13423 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13424
13425 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13426 }
13427
13428 /* Traversal function for queue_and_load_all_dwo_tus. */
13429
13430 static int
13431 queue_and_load_dwo_tu (void **slot, void *info)
13432 {
13433 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13434 dwarf2_cu *cu = (dwarf2_cu *) info;
13435 ULONGEST signature = dwo_unit->signature;
13436 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13437
13438 if (sig_type != NULL)
13439 {
13440 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13441
13442 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13443 a real dependency of PER_CU on SIG_TYPE. That is detected later
13444 while processing PER_CU. */
13445 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13446 load_full_type_unit (sig_cu, cu->per_objfile);
13447 cu->per_cu->imported_symtabs_push (sig_cu);
13448 }
13449
13450 return 1;
13451 }
13452
13453 /* Queue all TUs contained in the DWO of CU to be read in.
13454 The DWO may have the only definition of the type, though it may not be
13455 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13456 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13457
13458 static void
13459 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13460 {
13461 struct dwo_unit *dwo_unit;
13462 struct dwo_file *dwo_file;
13463
13464 gdb_assert (cu != nullptr);
13465 gdb_assert (!cu->per_cu->is_debug_types);
13466 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13467
13468 dwo_unit = cu->dwo_unit;
13469 gdb_assert (dwo_unit != NULL);
13470
13471 dwo_file = dwo_unit->dwo_file;
13472 if (dwo_file->tus != NULL)
13473 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13474 }
13475
13476 /* Read in various DIEs. */
13477
13478 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13479 Inherit only the children of the DW_AT_abstract_origin DIE not being
13480 already referenced by DW_AT_abstract_origin from the children of the
13481 current DIE. */
13482
13483 static void
13484 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13485 {
13486 struct die_info *child_die;
13487 sect_offset *offsetp;
13488 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13489 struct die_info *origin_die;
13490 /* Iterator of the ORIGIN_DIE children. */
13491 struct die_info *origin_child_die;
13492 struct attribute *attr;
13493 struct dwarf2_cu *origin_cu;
13494 struct pending **origin_previous_list_in_scope;
13495
13496 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13497 if (!attr)
13498 return;
13499
13500 /* Note that following die references may follow to a die in a
13501 different cu. */
13502
13503 origin_cu = cu;
13504 origin_die = follow_die_ref (die, attr, &origin_cu);
13505
13506 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13507 symbols in. */
13508 origin_previous_list_in_scope = origin_cu->list_in_scope;
13509 origin_cu->list_in_scope = cu->list_in_scope;
13510
13511 if (die->tag != origin_die->tag
13512 && !(die->tag == DW_TAG_inlined_subroutine
13513 && origin_die->tag == DW_TAG_subprogram))
13514 complaint (_("DIE %s and its abstract origin %s have different tags"),
13515 sect_offset_str (die->sect_off),
13516 sect_offset_str (origin_die->sect_off));
13517
13518 /* Find if the concrete and abstract trees are structurally the
13519 same. This is a shallow traversal and it is not bullet-proof;
13520 the compiler can trick the debugger into believing that the trees
13521 are isomorphic, whereas they actually are not. However, the
13522 likelyhood of this happening is pretty low, and a full-fledged
13523 check would be an overkill. */
13524 bool are_isomorphic = true;
13525 die_info *concrete_child = die->child;
13526 die_info *abstract_child = origin_die->child;
13527 while (concrete_child != nullptr || abstract_child != nullptr)
13528 {
13529 if (concrete_child == nullptr
13530 || abstract_child == nullptr
13531 || concrete_child->tag != abstract_child->tag)
13532 {
13533 are_isomorphic = false;
13534 break;
13535 }
13536
13537 concrete_child = concrete_child->sibling;
13538 abstract_child = abstract_child->sibling;
13539 }
13540
13541 /* Walk the origin's children in parallel to the concrete children.
13542 This helps match an origin child in case the debug info misses
13543 DW_AT_abstract_origin attributes. Keep in mind that the abstract
13544 origin tree may not have the same tree structure as the concrete
13545 DIE, though. */
13546 die_info *corresponding_abstract_child
13547 = are_isomorphic ? origin_die->child : nullptr;
13548
13549 std::vector<sect_offset> offsets;
13550
13551 for (child_die = die->child;
13552 child_die && child_die->tag;
13553 child_die = child_die->sibling)
13554 {
13555 struct die_info *child_origin_die;
13556 struct dwarf2_cu *child_origin_cu;
13557
13558 /* We are trying to process concrete instance entries:
13559 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13560 it's not relevant to our analysis here. i.e. detecting DIEs that are
13561 present in the abstract instance but not referenced in the concrete
13562 one. */
13563 if (child_die->tag == DW_TAG_call_site
13564 || child_die->tag == DW_TAG_GNU_call_site)
13565 {
13566 if (are_isomorphic)
13567 corresponding_abstract_child
13568 = corresponding_abstract_child->sibling;
13569 continue;
13570 }
13571
13572 /* For each CHILD_DIE, find the corresponding child of
13573 ORIGIN_DIE. If there is more than one layer of
13574 DW_AT_abstract_origin, follow them all; there shouldn't be,
13575 but GCC versions at least through 4.4 generate this (GCC PR
13576 40573). */
13577 child_origin_die = child_die;
13578 child_origin_cu = cu;
13579 while (1)
13580 {
13581 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13582 child_origin_cu);
13583 if (attr == NULL)
13584 break;
13585 child_origin_die = follow_die_ref (child_origin_die, attr,
13586 &child_origin_cu);
13587 }
13588
13589 /* If missing DW_AT_abstract_origin, try the corresponding child
13590 of the origin. Clang emits such lexical scopes. */
13591 if (child_origin_die == child_die
13592 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13593 && are_isomorphic
13594 && child_die->tag == DW_TAG_lexical_block)
13595 child_origin_die = corresponding_abstract_child;
13596
13597 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13598 counterpart may exist. */
13599 if (child_origin_die != child_die)
13600 {
13601 if (child_die->tag != child_origin_die->tag
13602 && !(child_die->tag == DW_TAG_inlined_subroutine
13603 && child_origin_die->tag == DW_TAG_subprogram))
13604 complaint (_("Child DIE %s and its abstract origin %s have "
13605 "different tags"),
13606 sect_offset_str (child_die->sect_off),
13607 sect_offset_str (child_origin_die->sect_off));
13608 if (child_origin_die->parent != origin_die)
13609 complaint (_("Child DIE %s and its abstract origin %s have "
13610 "different parents"),
13611 sect_offset_str (child_die->sect_off),
13612 sect_offset_str (child_origin_die->sect_off));
13613 else
13614 offsets.push_back (child_origin_die->sect_off);
13615 }
13616
13617 if (are_isomorphic)
13618 corresponding_abstract_child = corresponding_abstract_child->sibling;
13619 }
13620 std::sort (offsets.begin (), offsets.end ());
13621 sect_offset *offsets_end = offsets.data () + offsets.size ();
13622 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13623 if (offsetp[-1] == *offsetp)
13624 complaint (_("Multiple children of DIE %s refer "
13625 "to DIE %s as their abstract origin"),
13626 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13627
13628 offsetp = offsets.data ();
13629 origin_child_die = origin_die->child;
13630 while (origin_child_die && origin_child_die->tag)
13631 {
13632 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13633 while (offsetp < offsets_end
13634 && *offsetp < origin_child_die->sect_off)
13635 offsetp++;
13636 if (offsetp >= offsets_end
13637 || *offsetp > origin_child_die->sect_off)
13638 {
13639 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13640 Check whether we're already processing ORIGIN_CHILD_DIE.
13641 This can happen with mutually referenced abstract_origins.
13642 PR 16581. */
13643 if (!origin_child_die->in_process)
13644 process_die (origin_child_die, origin_cu);
13645 }
13646 origin_child_die = origin_child_die->sibling;
13647 }
13648 origin_cu->list_in_scope = origin_previous_list_in_scope;
13649
13650 if (cu != origin_cu)
13651 compute_delayed_physnames (origin_cu);
13652 }
13653
13654 static void
13655 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13656 {
13657 struct objfile *objfile = cu->per_objfile->objfile;
13658 struct gdbarch *gdbarch = objfile->arch ();
13659 struct context_stack *newobj;
13660 CORE_ADDR lowpc;
13661 CORE_ADDR highpc;
13662 struct die_info *child_die;
13663 struct attribute *attr, *call_line, *call_file;
13664 const char *name;
13665 CORE_ADDR baseaddr;
13666 struct block *block;
13667 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13668 std::vector<struct symbol *> template_args;
13669 struct template_symbol *templ_func = NULL;
13670
13671 if (inlined_func)
13672 {
13673 /* If we do not have call site information, we can't show the
13674 caller of this inlined function. That's too confusing, so
13675 only use the scope for local variables. */
13676 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13677 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13678 if (call_line == NULL || call_file == NULL)
13679 {
13680 read_lexical_block_scope (die, cu);
13681 return;
13682 }
13683 }
13684
13685 baseaddr = objfile->text_section_offset ();
13686
13687 name = dwarf2_name (die, cu);
13688
13689 /* Ignore functions with missing or empty names. These are actually
13690 illegal according to the DWARF standard. */
13691 if (name == NULL)
13692 {
13693 complaint (_("missing name for subprogram DIE at %s"),
13694 sect_offset_str (die->sect_off));
13695 return;
13696 }
13697
13698 /* Ignore functions with missing or invalid low and high pc attributes. */
13699 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13700 <= PC_BOUNDS_INVALID)
13701 {
13702 attr = dwarf2_attr (die, DW_AT_external, cu);
13703 if (attr == nullptr || !attr->as_boolean ())
13704 complaint (_("cannot get low and high bounds "
13705 "for subprogram DIE at %s"),
13706 sect_offset_str (die->sect_off));
13707 return;
13708 }
13709
13710 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13711 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13712
13713 /* If we have any template arguments, then we must allocate a
13714 different sort of symbol. */
13715 for (child_die = die->child; child_die; child_die = child_die->sibling)
13716 {
13717 if (child_die->tag == DW_TAG_template_type_param
13718 || child_die->tag == DW_TAG_template_value_param)
13719 {
13720 templ_func = new (&objfile->objfile_obstack) template_symbol;
13721 templ_func->subclass = SYMBOL_TEMPLATE;
13722 break;
13723 }
13724 }
13725
13726 gdb_assert (cu->get_builder () != nullptr);
13727 newobj = cu->get_builder ()->push_context (0, lowpc);
13728 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13729 (struct symbol *) templ_func);
13730
13731 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13732 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13733 cu->language);
13734
13735 /* If there is a location expression for DW_AT_frame_base, record
13736 it. */
13737 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13738 if (attr != nullptr)
13739 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13740
13741 /* If there is a location for the static link, record it. */
13742 newobj->static_link = NULL;
13743 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13744 if (attr != nullptr)
13745 {
13746 newobj->static_link
13747 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13748 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13749 cu->addr_type ());
13750 }
13751
13752 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13753
13754 if (die->child != NULL)
13755 {
13756 child_die = die->child;
13757 while (child_die && child_die->tag)
13758 {
13759 if (child_die->tag == DW_TAG_template_type_param
13760 || child_die->tag == DW_TAG_template_value_param)
13761 {
13762 struct symbol *arg = new_symbol (child_die, NULL, cu);
13763
13764 if (arg != NULL)
13765 template_args.push_back (arg);
13766 }
13767 else
13768 process_die (child_die, cu);
13769 child_die = child_die->sibling;
13770 }
13771 }
13772
13773 inherit_abstract_dies (die, cu);
13774
13775 /* If we have a DW_AT_specification, we might need to import using
13776 directives from the context of the specification DIE. See the
13777 comment in determine_prefix. */
13778 if (cu->language == language_cplus
13779 && dwarf2_attr (die, DW_AT_specification, cu))
13780 {
13781 struct dwarf2_cu *spec_cu = cu;
13782 struct die_info *spec_die = die_specification (die, &spec_cu);
13783
13784 while (spec_die)
13785 {
13786 child_die = spec_die->child;
13787 while (child_die && child_die->tag)
13788 {
13789 if (child_die->tag == DW_TAG_imported_module)
13790 process_die (child_die, spec_cu);
13791 child_die = child_die->sibling;
13792 }
13793
13794 /* In some cases, GCC generates specification DIEs that
13795 themselves contain DW_AT_specification attributes. */
13796 spec_die = die_specification (spec_die, &spec_cu);
13797 }
13798 }
13799
13800 struct context_stack cstk = cu->get_builder ()->pop_context ();
13801 /* Make a block for the local symbols within. */
13802 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13803 cstk.static_link, lowpc, highpc);
13804
13805 /* For C++, set the block's scope. */
13806 if ((cu->language == language_cplus
13807 || cu->language == language_fortran
13808 || cu->language == language_d
13809 || cu->language == language_rust)
13810 && cu->processing_has_namespace_info)
13811 block_set_scope (block, determine_prefix (die, cu),
13812 &objfile->objfile_obstack);
13813
13814 /* If we have address ranges, record them. */
13815 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13816
13817 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13818
13819 /* Attach template arguments to function. */
13820 if (!template_args.empty ())
13821 {
13822 gdb_assert (templ_func != NULL);
13823
13824 templ_func->n_template_arguments = template_args.size ();
13825 templ_func->template_arguments
13826 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13827 templ_func->n_template_arguments);
13828 memcpy (templ_func->template_arguments,
13829 template_args.data (),
13830 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13831
13832 /* Make sure that the symtab is set on the new symbols. Even
13833 though they don't appear in this symtab directly, other parts
13834 of gdb assume that symbols do, and this is reasonably
13835 true. */
13836 for (symbol *sym : template_args)
13837 symbol_set_symtab (sym, symbol_symtab (templ_func));
13838 }
13839
13840 /* In C++, we can have functions nested inside functions (e.g., when
13841 a function declares a class that has methods). This means that
13842 when we finish processing a function scope, we may need to go
13843 back to building a containing block's symbol lists. */
13844 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13845 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13846
13847 /* If we've finished processing a top-level function, subsequent
13848 symbols go in the file symbol list. */
13849 if (cu->get_builder ()->outermost_context_p ())
13850 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13851 }
13852
13853 /* Process all the DIES contained within a lexical block scope. Start
13854 a new scope, process the dies, and then close the scope. */
13855
13856 static void
13857 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13858 {
13859 struct objfile *objfile = cu->per_objfile->objfile;
13860 struct gdbarch *gdbarch = objfile->arch ();
13861 CORE_ADDR lowpc, highpc;
13862 struct die_info *child_die;
13863 CORE_ADDR baseaddr;
13864
13865 baseaddr = objfile->text_section_offset ();
13866
13867 /* Ignore blocks with missing or invalid low and high pc attributes. */
13868 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13869 as multiple lexical blocks? Handling children in a sane way would
13870 be nasty. Might be easier to properly extend generic blocks to
13871 describe ranges. */
13872 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13873 {
13874 case PC_BOUNDS_NOT_PRESENT:
13875 /* DW_TAG_lexical_block has no attributes, process its children as if
13876 there was no wrapping by that DW_TAG_lexical_block.
13877 GCC does no longer produces such DWARF since GCC r224161. */
13878 for (child_die = die->child;
13879 child_die != NULL && child_die->tag;
13880 child_die = child_die->sibling)
13881 {
13882 /* We might already be processing this DIE. This can happen
13883 in an unusual circumstance -- where a subroutine A
13884 appears lexically in another subroutine B, but A actually
13885 inlines B. The recursion is broken here, rather than in
13886 inherit_abstract_dies, because it seems better to simply
13887 drop concrete children here. */
13888 if (!child_die->in_process)
13889 process_die (child_die, cu);
13890 }
13891 return;
13892 case PC_BOUNDS_INVALID:
13893 return;
13894 }
13895 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13896 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13897
13898 cu->get_builder ()->push_context (0, lowpc);
13899 if (die->child != NULL)
13900 {
13901 child_die = die->child;
13902 while (child_die && child_die->tag)
13903 {
13904 process_die (child_die, cu);
13905 child_die = child_die->sibling;
13906 }
13907 }
13908 inherit_abstract_dies (die, cu);
13909 struct context_stack cstk = cu->get_builder ()->pop_context ();
13910
13911 if (*cu->get_builder ()->get_local_symbols () != NULL
13912 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13913 {
13914 struct block *block
13915 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13916 cstk.start_addr, highpc);
13917
13918 /* Note that recording ranges after traversing children, as we
13919 do here, means that recording a parent's ranges entails
13920 walking across all its children's ranges as they appear in
13921 the address map, which is quadratic behavior.
13922
13923 It would be nicer to record the parent's ranges before
13924 traversing its children, simply overriding whatever you find
13925 there. But since we don't even decide whether to create a
13926 block until after we've traversed its children, that's hard
13927 to do. */
13928 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13929 }
13930 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13931 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13932 }
13933
13934 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13935
13936 static void
13937 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13938 {
13939 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13940 struct objfile *objfile = per_objfile->objfile;
13941 struct gdbarch *gdbarch = objfile->arch ();
13942 CORE_ADDR pc, baseaddr;
13943 struct attribute *attr;
13944 struct call_site *call_site, call_site_local;
13945 void **slot;
13946 int nparams;
13947 struct die_info *child_die;
13948
13949 baseaddr = objfile->text_section_offset ();
13950
13951 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13952 if (attr == NULL)
13953 {
13954 /* This was a pre-DWARF-5 GNU extension alias
13955 for DW_AT_call_return_pc. */
13956 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13957 }
13958 if (!attr)
13959 {
13960 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13961 "DIE %s [in module %s]"),
13962 sect_offset_str (die->sect_off), objfile_name (objfile));
13963 return;
13964 }
13965 pc = attr->as_address () + baseaddr;
13966 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13967
13968 if (cu->call_site_htab == NULL)
13969 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13970 NULL, &objfile->objfile_obstack,
13971 hashtab_obstack_allocate, NULL);
13972 call_site_local.pc = pc;
13973 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13974 if (*slot != NULL)
13975 {
13976 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13977 "DIE %s [in module %s]"),
13978 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13979 objfile_name (objfile));
13980 return;
13981 }
13982
13983 /* Count parameters at the caller. */
13984
13985 nparams = 0;
13986 for (child_die = die->child; child_die && child_die->tag;
13987 child_die = child_die->sibling)
13988 {
13989 if (child_die->tag != DW_TAG_call_site_parameter
13990 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13991 {
13992 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13993 "DW_TAG_call_site child DIE %s [in module %s]"),
13994 child_die->tag, sect_offset_str (child_die->sect_off),
13995 objfile_name (objfile));
13996 continue;
13997 }
13998
13999 nparams++;
14000 }
14001
14002 call_site
14003 = ((struct call_site *)
14004 obstack_alloc (&objfile->objfile_obstack,
14005 sizeof (*call_site)
14006 + (sizeof (*call_site->parameter) * (nparams - 1))));
14007 *slot = call_site;
14008 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14009 call_site->pc = pc;
14010
14011 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14012 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14013 {
14014 struct die_info *func_die;
14015
14016 /* Skip also over DW_TAG_inlined_subroutine. */
14017 for (func_die = die->parent;
14018 func_die && func_die->tag != DW_TAG_subprogram
14019 && func_die->tag != DW_TAG_subroutine_type;
14020 func_die = func_die->parent);
14021
14022 /* DW_AT_call_all_calls is a superset
14023 of DW_AT_call_all_tail_calls. */
14024 if (func_die
14025 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14026 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14027 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14028 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14029 {
14030 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14031 not complete. But keep CALL_SITE for look ups via call_site_htab,
14032 both the initial caller containing the real return address PC and
14033 the final callee containing the current PC of a chain of tail
14034 calls do not need to have the tail call list complete. But any
14035 function candidate for a virtual tail call frame searched via
14036 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14037 determined unambiguously. */
14038 }
14039 else
14040 {
14041 struct type *func_type = NULL;
14042
14043 if (func_die)
14044 func_type = get_die_type (func_die, cu);
14045 if (func_type != NULL)
14046 {
14047 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
14048
14049 /* Enlist this call site to the function. */
14050 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14051 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14052 }
14053 else
14054 complaint (_("Cannot find function owning DW_TAG_call_site "
14055 "DIE %s [in module %s]"),
14056 sect_offset_str (die->sect_off), objfile_name (objfile));
14057 }
14058 }
14059
14060 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14061 if (attr == NULL)
14062 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14063 if (attr == NULL)
14064 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14065 if (attr == NULL)
14066 {
14067 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14068 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14069 }
14070 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14071 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
14072 /* Keep NULL DWARF_BLOCK. */;
14073 else if (attr->form_is_block ())
14074 {
14075 struct dwarf2_locexpr_baton *dlbaton;
14076 struct dwarf_block *block = attr->as_block ();
14077
14078 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14079 dlbaton->data = block->data;
14080 dlbaton->size = block->size;
14081 dlbaton->per_objfile = per_objfile;
14082 dlbaton->per_cu = cu->per_cu;
14083
14084 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14085 }
14086 else if (attr->form_is_ref ())
14087 {
14088 struct dwarf2_cu *target_cu = cu;
14089 struct die_info *target_die;
14090
14091 target_die = follow_die_ref (die, attr, &target_cu);
14092 gdb_assert (target_cu->per_objfile->objfile == objfile);
14093 if (die_is_declaration (target_die, target_cu))
14094 {
14095 const char *target_physname;
14096
14097 /* Prefer the mangled name; otherwise compute the demangled one. */
14098 target_physname = dw2_linkage_name (target_die, target_cu);
14099 if (target_physname == NULL)
14100 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14101 if (target_physname == NULL)
14102 complaint (_("DW_AT_call_target target DIE has invalid "
14103 "physname, for referencing DIE %s [in module %s]"),
14104 sect_offset_str (die->sect_off), objfile_name (objfile));
14105 else
14106 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14107 }
14108 else
14109 {
14110 CORE_ADDR lowpc;
14111
14112 /* DW_AT_entry_pc should be preferred. */
14113 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14114 <= PC_BOUNDS_INVALID)
14115 complaint (_("DW_AT_call_target target DIE has invalid "
14116 "low pc, for referencing DIE %s [in module %s]"),
14117 sect_offset_str (die->sect_off), objfile_name (objfile));
14118 else
14119 {
14120 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14121 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14122 }
14123 }
14124 }
14125 else
14126 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14127 "block nor reference, for DIE %s [in module %s]"),
14128 sect_offset_str (die->sect_off), objfile_name (objfile));
14129
14130 call_site->per_cu = cu->per_cu;
14131 call_site->per_objfile = per_objfile;
14132
14133 for (child_die = die->child;
14134 child_die && child_die->tag;
14135 child_die = child_die->sibling)
14136 {
14137 struct call_site_parameter *parameter;
14138 struct attribute *loc, *origin;
14139
14140 if (child_die->tag != DW_TAG_call_site_parameter
14141 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14142 {
14143 /* Already printed the complaint above. */
14144 continue;
14145 }
14146
14147 gdb_assert (call_site->parameter_count < nparams);
14148 parameter = &call_site->parameter[call_site->parameter_count];
14149
14150 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14151 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14152 register is contained in DW_AT_call_value. */
14153
14154 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14155 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14156 if (origin == NULL)
14157 {
14158 /* This was a pre-DWARF-5 GNU extension alias
14159 for DW_AT_call_parameter. */
14160 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14161 }
14162 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14163 {
14164 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14165
14166 sect_offset sect_off = origin->get_ref_die_offset ();
14167 if (!cu->header.offset_in_cu_p (sect_off))
14168 {
14169 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14170 binding can be done only inside one CU. Such referenced DIE
14171 therefore cannot be even moved to DW_TAG_partial_unit. */
14172 complaint (_("DW_AT_call_parameter offset is not in CU for "
14173 "DW_TAG_call_site child DIE %s [in module %s]"),
14174 sect_offset_str (child_die->sect_off),
14175 objfile_name (objfile));
14176 continue;
14177 }
14178 parameter->u.param_cu_off
14179 = (cu_offset) (sect_off - cu->header.sect_off);
14180 }
14181 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14182 {
14183 complaint (_("No DW_FORM_block* DW_AT_location for "
14184 "DW_TAG_call_site child DIE %s [in module %s]"),
14185 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14186 continue;
14187 }
14188 else
14189 {
14190 struct dwarf_block *block = loc->as_block ();
14191
14192 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14193 (block->data, &block->data[block->size]);
14194 if (parameter->u.dwarf_reg != -1)
14195 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14196 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14197 &block->data[block->size],
14198 &parameter->u.fb_offset))
14199 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14200 else
14201 {
14202 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14203 "for DW_FORM_block* DW_AT_location is supported for "
14204 "DW_TAG_call_site child DIE %s "
14205 "[in module %s]"),
14206 sect_offset_str (child_die->sect_off),
14207 objfile_name (objfile));
14208 continue;
14209 }
14210 }
14211
14212 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14213 if (attr == NULL)
14214 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14215 if (attr == NULL || !attr->form_is_block ())
14216 {
14217 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14218 "DW_TAG_call_site child DIE %s [in module %s]"),
14219 sect_offset_str (child_die->sect_off),
14220 objfile_name (objfile));
14221 continue;
14222 }
14223
14224 struct dwarf_block *block = attr->as_block ();
14225 parameter->value = block->data;
14226 parameter->value_size = block->size;
14227
14228 /* Parameters are not pre-cleared by memset above. */
14229 parameter->data_value = NULL;
14230 parameter->data_value_size = 0;
14231 call_site->parameter_count++;
14232
14233 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14234 if (attr == NULL)
14235 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14236 if (attr != nullptr)
14237 {
14238 if (!attr->form_is_block ())
14239 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14240 "DW_TAG_call_site child DIE %s [in module %s]"),
14241 sect_offset_str (child_die->sect_off),
14242 objfile_name (objfile));
14243 else
14244 {
14245 block = attr->as_block ();
14246 parameter->data_value = block->data;
14247 parameter->data_value_size = block->size;
14248 }
14249 }
14250 }
14251 }
14252
14253 /* Helper function for read_variable. If DIE represents a virtual
14254 table, then return the type of the concrete object that is
14255 associated with the virtual table. Otherwise, return NULL. */
14256
14257 static struct type *
14258 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14259 {
14260 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14261 if (attr == NULL)
14262 return NULL;
14263
14264 /* Find the type DIE. */
14265 struct die_info *type_die = NULL;
14266 struct dwarf2_cu *type_cu = cu;
14267
14268 if (attr->form_is_ref ())
14269 type_die = follow_die_ref (die, attr, &type_cu);
14270 if (type_die == NULL)
14271 return NULL;
14272
14273 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14274 return NULL;
14275 return die_containing_type (type_die, type_cu);
14276 }
14277
14278 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14279
14280 static void
14281 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14282 {
14283 struct rust_vtable_symbol *storage = NULL;
14284
14285 if (cu->language == language_rust)
14286 {
14287 struct type *containing_type = rust_containing_type (die, cu);
14288
14289 if (containing_type != NULL)
14290 {
14291 struct objfile *objfile = cu->per_objfile->objfile;
14292
14293 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14294 storage->concrete_type = containing_type;
14295 storage->subclass = SYMBOL_RUST_VTABLE;
14296 }
14297 }
14298
14299 struct symbol *res = new_symbol (die, NULL, cu, storage);
14300 struct attribute *abstract_origin
14301 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14302 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14303 if (res == NULL && loc && abstract_origin)
14304 {
14305 /* We have a variable without a name, but with a location and an abstract
14306 origin. This may be a concrete instance of an abstract variable
14307 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14308 later. */
14309 struct dwarf2_cu *origin_cu = cu;
14310 struct die_info *origin_die
14311 = follow_die_ref (die, abstract_origin, &origin_cu);
14312 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14313 per_objfile->per_bfd->abstract_to_concrete
14314 [origin_die->sect_off].push_back (die->sect_off);
14315 }
14316 }
14317
14318 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14319 reading .debug_rnglists.
14320 Callback's type should be:
14321 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14322 Return true if the attributes are present and valid, otherwise,
14323 return false. */
14324
14325 template <typename Callback>
14326 static bool
14327 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14328 dwarf_tag tag, Callback &&callback)
14329 {
14330 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14331 struct objfile *objfile = per_objfile->objfile;
14332 bfd *obfd = objfile->obfd;
14333 /* Base address selection entry. */
14334 gdb::optional<CORE_ADDR> base;
14335 const gdb_byte *buffer;
14336 CORE_ADDR baseaddr;
14337 bool overflow = false;
14338 ULONGEST addr_index;
14339 struct dwarf2_section_info *rnglists_section;
14340
14341 base = cu->base_address;
14342 rnglists_section = cu_debug_rnglists_section (cu, tag);
14343 rnglists_section->read (objfile);
14344
14345 if (offset >= rnglists_section->size)
14346 {
14347 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14348 offset);
14349 return false;
14350 }
14351 buffer = rnglists_section->buffer + offset;
14352
14353 baseaddr = objfile->text_section_offset ();
14354
14355 while (1)
14356 {
14357 /* Initialize it due to a false compiler warning. */
14358 CORE_ADDR range_beginning = 0, range_end = 0;
14359 const gdb_byte *buf_end = (rnglists_section->buffer
14360 + rnglists_section->size);
14361 unsigned int bytes_read;
14362
14363 if (buffer == buf_end)
14364 {
14365 overflow = true;
14366 break;
14367 }
14368 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14369 switch (rlet)
14370 {
14371 case DW_RLE_end_of_list:
14372 break;
14373 case DW_RLE_base_address:
14374 if (buffer + cu->header.addr_size > buf_end)
14375 {
14376 overflow = true;
14377 break;
14378 }
14379 base = cu->header.read_address (obfd, buffer, &bytes_read);
14380 buffer += bytes_read;
14381 break;
14382 case DW_RLE_base_addressx:
14383 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14384 buffer += bytes_read;
14385 base = read_addr_index (cu, addr_index);
14386 break;
14387 case DW_RLE_start_length:
14388 if (buffer + cu->header.addr_size > buf_end)
14389 {
14390 overflow = true;
14391 break;
14392 }
14393 range_beginning = cu->header.read_address (obfd, buffer,
14394 &bytes_read);
14395 buffer += bytes_read;
14396 range_end = (range_beginning
14397 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14398 buffer += bytes_read;
14399 if (buffer > buf_end)
14400 {
14401 overflow = true;
14402 break;
14403 }
14404 break;
14405 case DW_RLE_startx_length:
14406 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14407 buffer += bytes_read;
14408 range_beginning = read_addr_index (cu, addr_index);
14409 if (buffer > buf_end)
14410 {
14411 overflow = true;
14412 break;
14413 }
14414 range_end = (range_beginning
14415 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14416 buffer += bytes_read;
14417 break;
14418 case DW_RLE_offset_pair:
14419 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14420 buffer += bytes_read;
14421 if (buffer > buf_end)
14422 {
14423 overflow = true;
14424 break;
14425 }
14426 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14427 buffer += bytes_read;
14428 if (buffer > buf_end)
14429 {
14430 overflow = true;
14431 break;
14432 }
14433 break;
14434 case DW_RLE_start_end:
14435 if (buffer + 2 * cu->header.addr_size > buf_end)
14436 {
14437 overflow = true;
14438 break;
14439 }
14440 range_beginning = cu->header.read_address (obfd, buffer,
14441 &bytes_read);
14442 buffer += bytes_read;
14443 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14444 buffer += bytes_read;
14445 break;
14446 case DW_RLE_startx_endx:
14447 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14448 buffer += bytes_read;
14449 range_beginning = read_addr_index (cu, addr_index);
14450 if (buffer > buf_end)
14451 {
14452 overflow = true;
14453 break;
14454 }
14455 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14456 buffer += bytes_read;
14457 range_end = read_addr_index (cu, addr_index);
14458 break;
14459 default:
14460 complaint (_("Invalid .debug_rnglists data (no base address)"));
14461 return false;
14462 }
14463 if (rlet == DW_RLE_end_of_list || overflow)
14464 break;
14465 if (rlet == DW_RLE_base_address)
14466 continue;
14467
14468 if (range_beginning > range_end)
14469 {
14470 /* Inverted range entries are invalid. */
14471 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14472 return false;
14473 }
14474
14475 /* Empty range entries have no effect. */
14476 if (range_beginning == range_end)
14477 continue;
14478
14479 /* Only DW_RLE_offset_pair needs the base address added. */
14480 if (rlet == DW_RLE_offset_pair)
14481 {
14482 if (!base.has_value ())
14483 {
14484 /* We have no valid base address for the DW_RLE_offset_pair. */
14485 complaint (_("Invalid .debug_rnglists data (no base address for "
14486 "DW_RLE_offset_pair)"));
14487 return false;
14488 }
14489
14490 range_beginning += *base;
14491 range_end += *base;
14492 }
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_rnglists 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 if (overflow)
14508 {
14509 complaint (_("Offset %d is not terminated "
14510 "for DW_AT_ranges attribute"),
14511 offset);
14512 return false;
14513 }
14514
14515 return true;
14516 }
14517
14518 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14519 Callback's type should be:
14520 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14521 Return 1 if the attributes are present and valid, otherwise, return 0. */
14522
14523 template <typename Callback>
14524 static int
14525 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14526 Callback &&callback)
14527 {
14528 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14529 struct objfile *objfile = per_objfile->objfile;
14530 struct comp_unit_head *cu_header = &cu->header;
14531 bfd *obfd = objfile->obfd;
14532 unsigned int addr_size = cu_header->addr_size;
14533 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14534 /* Base address selection entry. */
14535 gdb::optional<CORE_ADDR> base;
14536 unsigned int dummy;
14537 const gdb_byte *buffer;
14538 CORE_ADDR baseaddr;
14539
14540 if (cu_header->version >= 5)
14541 return dwarf2_rnglists_process (offset, cu, tag, callback);
14542
14543 base = cu->base_address;
14544
14545 per_objfile->per_bfd->ranges.read (objfile);
14546 if (offset >= per_objfile->per_bfd->ranges.size)
14547 {
14548 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14549 offset);
14550 return 0;
14551 }
14552 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14553
14554 baseaddr = objfile->text_section_offset ();
14555
14556 while (1)
14557 {
14558 CORE_ADDR range_beginning, range_end;
14559
14560 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14561 buffer += addr_size;
14562 range_end = cu->header.read_address (obfd, buffer, &dummy);
14563 buffer += addr_size;
14564 offset += 2 * addr_size;
14565
14566 /* An end of list marker is a pair of zero addresses. */
14567 if (range_beginning == 0 && range_end == 0)
14568 /* Found the end of list entry. */
14569 break;
14570
14571 /* Each base address selection entry is a pair of 2 values.
14572 The first is the largest possible address, the second is
14573 the base address. Check for a base address here. */
14574 if ((range_beginning & mask) == mask)
14575 {
14576 /* If we found the largest possible address, then we already
14577 have the base address in range_end. */
14578 base = range_end;
14579 continue;
14580 }
14581
14582 if (!base.has_value ())
14583 {
14584 /* We have no valid base address for the ranges
14585 data. */
14586 complaint (_("Invalid .debug_ranges data (no base address)"));
14587 return 0;
14588 }
14589
14590 if (range_beginning > range_end)
14591 {
14592 /* Inverted range entries are invalid. */
14593 complaint (_("Invalid .debug_ranges data (inverted range)"));
14594 return 0;
14595 }
14596
14597 /* Empty range entries have no effect. */
14598 if (range_beginning == range_end)
14599 continue;
14600
14601 range_beginning += *base;
14602 range_end += *base;
14603
14604 /* A not-uncommon case of bad debug info.
14605 Don't pollute the addrmap with bad data. */
14606 if (range_beginning + baseaddr == 0
14607 && !per_objfile->per_bfd->has_section_at_zero)
14608 {
14609 complaint (_(".debug_ranges entry has start address of zero"
14610 " [in module %s]"), objfile_name (objfile));
14611 continue;
14612 }
14613
14614 callback (range_beginning, range_end);
14615 }
14616
14617 return 1;
14618 }
14619
14620 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14621 Return 1 if the attributes are present and valid, otherwise, return 0.
14622 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14623
14624 static int
14625 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14626 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14627 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14628 {
14629 struct objfile *objfile = cu->per_objfile->objfile;
14630 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14631 struct gdbarch *gdbarch = objfile->arch ();
14632 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14633 int low_set = 0;
14634 CORE_ADDR low = 0;
14635 CORE_ADDR high = 0;
14636 int retval;
14637
14638 retval = dwarf2_ranges_process (offset, cu, tag,
14639 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14640 {
14641 if (ranges_pst != NULL)
14642 {
14643 CORE_ADDR lowpc;
14644 CORE_ADDR highpc;
14645
14646 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14647 range_beginning + baseaddr)
14648 - baseaddr);
14649 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14650 range_end + baseaddr)
14651 - baseaddr);
14652 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14653 lowpc, highpc - 1, ranges_pst);
14654 }
14655
14656 /* FIXME: This is recording everything as a low-high
14657 segment of consecutive addresses. We should have a
14658 data structure for discontiguous block ranges
14659 instead. */
14660 if (! low_set)
14661 {
14662 low = range_beginning;
14663 high = range_end;
14664 low_set = 1;
14665 }
14666 else
14667 {
14668 if (range_beginning < low)
14669 low = range_beginning;
14670 if (range_end > high)
14671 high = range_end;
14672 }
14673 });
14674 if (!retval)
14675 return 0;
14676
14677 if (! low_set)
14678 /* If the first entry is an end-of-list marker, the range
14679 describes an empty scope, i.e. no instructions. */
14680 return 0;
14681
14682 if (low_return)
14683 *low_return = low;
14684 if (high_return)
14685 *high_return = high;
14686 return 1;
14687 }
14688
14689 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14690 definition for the return value. *LOWPC and *HIGHPC are set iff
14691 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14692
14693 static enum pc_bounds_kind
14694 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14695 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14696 dwarf2_psymtab *pst)
14697 {
14698 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14699 struct attribute *attr;
14700 struct attribute *attr_high;
14701 CORE_ADDR low = 0;
14702 CORE_ADDR high = 0;
14703 enum pc_bounds_kind ret;
14704
14705 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14706 if (attr_high)
14707 {
14708 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14709 if (attr != nullptr)
14710 {
14711 low = attr->as_address ();
14712 high = attr_high->as_address ();
14713 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14714 high += low;
14715 }
14716 else
14717 /* Found high w/o low attribute. */
14718 return PC_BOUNDS_INVALID;
14719
14720 /* Found consecutive range of addresses. */
14721 ret = PC_BOUNDS_HIGH_LOW;
14722 }
14723 else
14724 {
14725 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14726 if (attr != nullptr && attr->form_is_unsigned ())
14727 {
14728 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14729 on DWARF version). */
14730 ULONGEST ranges_offset = attr->as_unsigned ();
14731
14732 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14733 this value. */
14734 if (die->tag != DW_TAG_compile_unit)
14735 ranges_offset += cu->gnu_ranges_base;
14736
14737 /* Value of the DW_AT_ranges attribute is the offset in the
14738 .debug_ranges section. */
14739 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14740 die->tag))
14741 return PC_BOUNDS_INVALID;
14742 /* Found discontinuous range of addresses. */
14743 ret = PC_BOUNDS_RANGES;
14744 }
14745 else
14746 return PC_BOUNDS_NOT_PRESENT;
14747 }
14748
14749 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14750 if (high <= low)
14751 return PC_BOUNDS_INVALID;
14752
14753 /* When using the GNU linker, .gnu.linkonce. sections are used to
14754 eliminate duplicate copies of functions and vtables and such.
14755 The linker will arbitrarily choose one and discard the others.
14756 The AT_*_pc values for such functions refer to local labels in
14757 these sections. If the section from that file was discarded, the
14758 labels are not in the output, so the relocs get a value of 0.
14759 If this is a discarded function, mark the pc bounds as invalid,
14760 so that GDB will ignore it. */
14761 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14762 return PC_BOUNDS_INVALID;
14763
14764 *lowpc = low;
14765 if (highpc)
14766 *highpc = high;
14767 return ret;
14768 }
14769
14770 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14771 its low and high PC addresses. Do nothing if these addresses could not
14772 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14773 and HIGHPC to the high address if greater than HIGHPC. */
14774
14775 static void
14776 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14777 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14778 struct dwarf2_cu *cu)
14779 {
14780 CORE_ADDR low, high;
14781 struct die_info *child = die->child;
14782
14783 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14784 {
14785 *lowpc = std::min (*lowpc, low);
14786 *highpc = std::max (*highpc, high);
14787 }
14788
14789 /* If the language does not allow nested subprograms (either inside
14790 subprograms or lexical blocks), we're done. */
14791 if (cu->language != language_ada)
14792 return;
14793
14794 /* Check all the children of the given DIE. If it contains nested
14795 subprograms, then check their pc bounds. Likewise, we need to
14796 check lexical blocks as well, as they may also contain subprogram
14797 definitions. */
14798 while (child && child->tag)
14799 {
14800 if (child->tag == DW_TAG_subprogram
14801 || child->tag == DW_TAG_lexical_block)
14802 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14803 child = child->sibling;
14804 }
14805 }
14806
14807 /* Get the low and high pc's represented by the scope DIE, and store
14808 them in *LOWPC and *HIGHPC. If the correct values can't be
14809 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14810
14811 static void
14812 get_scope_pc_bounds (struct die_info *die,
14813 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14814 struct dwarf2_cu *cu)
14815 {
14816 CORE_ADDR best_low = (CORE_ADDR) -1;
14817 CORE_ADDR best_high = (CORE_ADDR) 0;
14818 CORE_ADDR current_low, current_high;
14819
14820 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14821 >= PC_BOUNDS_RANGES)
14822 {
14823 best_low = current_low;
14824 best_high = current_high;
14825 }
14826 else
14827 {
14828 struct die_info *child = die->child;
14829
14830 while (child && child->tag)
14831 {
14832 switch (child->tag) {
14833 case DW_TAG_subprogram:
14834 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14835 break;
14836 case DW_TAG_namespace:
14837 case DW_TAG_module:
14838 /* FIXME: carlton/2004-01-16: Should we do this for
14839 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14840 that current GCC's always emit the DIEs corresponding
14841 to definitions of methods of classes as children of a
14842 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14843 the DIEs giving the declarations, which could be
14844 anywhere). But I don't see any reason why the
14845 standards says that they have to be there. */
14846 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14847
14848 if (current_low != ((CORE_ADDR) -1))
14849 {
14850 best_low = std::min (best_low, current_low);
14851 best_high = std::max (best_high, current_high);
14852 }
14853 break;
14854 default:
14855 /* Ignore. */
14856 break;
14857 }
14858
14859 child = child->sibling;
14860 }
14861 }
14862
14863 *lowpc = best_low;
14864 *highpc = best_high;
14865 }
14866
14867 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14868 in DIE. */
14869
14870 static void
14871 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14872 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14873 {
14874 struct objfile *objfile = cu->per_objfile->objfile;
14875 struct gdbarch *gdbarch = objfile->arch ();
14876 struct attribute *attr;
14877 struct attribute *attr_high;
14878
14879 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14880 if (attr_high)
14881 {
14882 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14883 if (attr != nullptr)
14884 {
14885 CORE_ADDR low = attr->as_address ();
14886 CORE_ADDR high = attr_high->as_address ();
14887
14888 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14889 high += low;
14890
14891 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14892 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14893 cu->get_builder ()->record_block_range (block, low, high - 1);
14894 }
14895 }
14896
14897 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14898 if (attr != nullptr && attr->form_is_unsigned ())
14899 {
14900 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14901 on DWARF version). */
14902 ULONGEST ranges_offset = attr->as_unsigned ();
14903
14904 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14905 this value. */
14906 if (die->tag != DW_TAG_compile_unit)
14907 ranges_offset += cu->gnu_ranges_base;
14908
14909 std::vector<blockrange> blockvec;
14910 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14911 [&] (CORE_ADDR start, CORE_ADDR end)
14912 {
14913 start += baseaddr;
14914 end += baseaddr;
14915 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14916 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14917 cu->get_builder ()->record_block_range (block, start, end - 1);
14918 blockvec.emplace_back (start, end);
14919 });
14920
14921 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14922 }
14923 }
14924
14925 /* Check whether the producer field indicates either of GCC < 4.6, or the
14926 Intel C/C++ compiler, and cache the result in CU. */
14927
14928 static void
14929 check_producer (struct dwarf2_cu *cu)
14930 {
14931 int major, minor;
14932
14933 if (cu->producer == NULL)
14934 {
14935 /* For unknown compilers expect their behavior is DWARF version
14936 compliant.
14937
14938 GCC started to support .debug_types sections by -gdwarf-4 since
14939 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14940 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14941 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14942 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14943 }
14944 else if (producer_is_gcc (cu->producer, &major, &minor))
14945 {
14946 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14947 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14948 }
14949 else if (producer_is_icc (cu->producer, &major, &minor))
14950 {
14951 cu->producer_is_icc = true;
14952 cu->producer_is_icc_lt_14 = major < 14;
14953 }
14954 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14955 cu->producer_is_codewarrior = true;
14956 else
14957 {
14958 /* For other non-GCC compilers, expect their behavior is DWARF version
14959 compliant. */
14960 }
14961
14962 cu->checked_producer = true;
14963 }
14964
14965 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14966 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14967 during 4.6.0 experimental. */
14968
14969 static bool
14970 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14971 {
14972 if (!cu->checked_producer)
14973 check_producer (cu);
14974
14975 return cu->producer_is_gxx_lt_4_6;
14976 }
14977
14978
14979 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14980 with incorrect is_stmt attributes. */
14981
14982 static bool
14983 producer_is_codewarrior (struct dwarf2_cu *cu)
14984 {
14985 if (!cu->checked_producer)
14986 check_producer (cu);
14987
14988 return cu->producer_is_codewarrior;
14989 }
14990
14991 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14992 If that attribute is not available, return the appropriate
14993 default. */
14994
14995 static enum dwarf_access_attribute
14996 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14997 {
14998 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14999 if (attr != nullptr)
15000 {
15001 LONGEST value = attr->constant_value (-1);
15002 if (value == DW_ACCESS_public
15003 || value == DW_ACCESS_protected
15004 || value == DW_ACCESS_private)
15005 return (dwarf_access_attribute) value;
15006 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
15007 plongest (value));
15008 }
15009
15010 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15011 {
15012 /* The default DWARF 2 accessibility for members is public, the default
15013 accessibility for inheritance is private. */
15014
15015 if (die->tag != DW_TAG_inheritance)
15016 return DW_ACCESS_public;
15017 else
15018 return DW_ACCESS_private;
15019 }
15020 else
15021 {
15022 /* DWARF 3+ defines the default accessibility a different way. The same
15023 rules apply now for DW_TAG_inheritance as for the members and it only
15024 depends on the container kind. */
15025
15026 if (die->parent->tag == DW_TAG_class_type)
15027 return DW_ACCESS_private;
15028 else
15029 return DW_ACCESS_public;
15030 }
15031 }
15032
15033 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15034 offset. If the attribute was not found return 0, otherwise return
15035 1. If it was found but could not properly be handled, set *OFFSET
15036 to 0. */
15037
15038 static int
15039 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15040 LONGEST *offset)
15041 {
15042 struct attribute *attr;
15043
15044 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15045 if (attr != NULL)
15046 {
15047 *offset = 0;
15048
15049 /* Note that we do not check for a section offset first here.
15050 This is because DW_AT_data_member_location is new in DWARF 4,
15051 so if we see it, we can assume that a constant form is really
15052 a constant and not a section offset. */
15053 if (attr->form_is_constant ())
15054 *offset = attr->constant_value (0);
15055 else if (attr->form_is_section_offset ())
15056 dwarf2_complex_location_expr_complaint ();
15057 else if (attr->form_is_block ())
15058 *offset = decode_locdesc (attr->as_block (), cu);
15059 else
15060 dwarf2_complex_location_expr_complaint ();
15061
15062 return 1;
15063 }
15064
15065 return 0;
15066 }
15067
15068 /* Look for DW_AT_data_member_location and store the results in FIELD. */
15069
15070 static void
15071 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15072 struct field *field)
15073 {
15074 struct attribute *attr;
15075
15076 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15077 if (attr != NULL)
15078 {
15079 if (attr->form_is_constant ())
15080 {
15081 LONGEST offset = attr->constant_value (0);
15082 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15083 }
15084 else if (attr->form_is_section_offset ())
15085 dwarf2_complex_location_expr_complaint ();
15086 else if (attr->form_is_block ())
15087 {
15088 bool handled;
15089 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
15090 if (handled)
15091 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15092 else
15093 {
15094 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15095 struct objfile *objfile = per_objfile->objfile;
15096 struct dwarf2_locexpr_baton *dlbaton
15097 = XOBNEW (&objfile->objfile_obstack,
15098 struct dwarf2_locexpr_baton);
15099 dlbaton->data = attr->as_block ()->data;
15100 dlbaton->size = attr->as_block ()->size;
15101 /* When using this baton, we want to compute the address
15102 of the field, not the value. This is why
15103 is_reference is set to false here. */
15104 dlbaton->is_reference = false;
15105 dlbaton->per_objfile = per_objfile;
15106 dlbaton->per_cu = cu->per_cu;
15107
15108 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15109 }
15110 }
15111 else
15112 dwarf2_complex_location_expr_complaint ();
15113 }
15114 }
15115
15116 /* Add an aggregate field to the field list. */
15117
15118 static void
15119 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15120 struct dwarf2_cu *cu)
15121 {
15122 struct objfile *objfile = cu->per_objfile->objfile;
15123 struct gdbarch *gdbarch = objfile->arch ();
15124 struct nextfield *new_field;
15125 struct attribute *attr;
15126 struct field *fp;
15127 const char *fieldname = "";
15128
15129 if (die->tag == DW_TAG_inheritance)
15130 {
15131 fip->baseclasses.emplace_back ();
15132 new_field = &fip->baseclasses.back ();
15133 }
15134 else
15135 {
15136 fip->fields.emplace_back ();
15137 new_field = &fip->fields.back ();
15138 }
15139
15140 new_field->offset = die->sect_off;
15141
15142 new_field->accessibility = dwarf2_access_attribute (die, cu);
15143 if (new_field->accessibility != DW_ACCESS_public)
15144 fip->non_public_fields = true;
15145
15146 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15147 if (attr != nullptr)
15148 new_field->virtuality = attr->as_virtuality ();
15149 else
15150 new_field->virtuality = DW_VIRTUALITY_none;
15151
15152 fp = &new_field->field;
15153
15154 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15155 {
15156 /* Data member other than a C++ static data member. */
15157
15158 /* Get type of field. */
15159 fp->set_type (die_type (die, cu));
15160
15161 SET_FIELD_BITPOS (*fp, 0);
15162
15163 /* Get bit size of field (zero if none). */
15164 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15165 if (attr != nullptr)
15166 {
15167 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15168 }
15169 else
15170 {
15171 FIELD_BITSIZE (*fp) = 0;
15172 }
15173
15174 /* Get bit offset of field. */
15175 handle_data_member_location (die, cu, fp);
15176 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15177 if (attr != nullptr && attr->form_is_constant ())
15178 {
15179 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15180 {
15181 /* For big endian bits, the DW_AT_bit_offset gives the
15182 additional bit offset from the MSB of the containing
15183 anonymous object to the MSB of the field. We don't
15184 have to do anything special since we don't need to
15185 know the size of the anonymous object. */
15186 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15187 + attr->constant_value (0)));
15188 }
15189 else
15190 {
15191 /* For little endian bits, compute the bit offset to the
15192 MSB of the anonymous object, subtract off the number of
15193 bits from the MSB of the field to the MSB of the
15194 object, and then subtract off the number of bits of
15195 the field itself. The result is the bit offset of
15196 the LSB of the field. */
15197 int anonymous_size;
15198 int bit_offset = attr->constant_value (0);
15199
15200 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15201 if (attr != nullptr && attr->form_is_constant ())
15202 {
15203 /* The size of the anonymous object containing
15204 the bit field is explicit, so use the
15205 indicated size (in bytes). */
15206 anonymous_size = attr->constant_value (0);
15207 }
15208 else
15209 {
15210 /* The size of the anonymous object containing
15211 the bit field must be inferred from the type
15212 attribute of the data member containing the
15213 bit field. */
15214 anonymous_size = TYPE_LENGTH (fp->type ());
15215 }
15216 SET_FIELD_BITPOS (*fp,
15217 (FIELD_BITPOS (*fp)
15218 + anonymous_size * bits_per_byte
15219 - bit_offset - FIELD_BITSIZE (*fp)));
15220 }
15221 }
15222 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15223 if (attr != NULL)
15224 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15225 + attr->constant_value (0)));
15226
15227 /* Get name of field. */
15228 fieldname = dwarf2_name (die, cu);
15229 if (fieldname == NULL)
15230 fieldname = "";
15231
15232 /* The name is already allocated along with this objfile, so we don't
15233 need to duplicate it for the type. */
15234 fp->name = fieldname;
15235
15236 /* Change accessibility for artificial fields (e.g. virtual table
15237 pointer or virtual base class pointer) to private. */
15238 if (dwarf2_attr (die, DW_AT_artificial, cu))
15239 {
15240 FIELD_ARTIFICIAL (*fp) = 1;
15241 new_field->accessibility = DW_ACCESS_private;
15242 fip->non_public_fields = true;
15243 }
15244 }
15245 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15246 {
15247 /* C++ static member. */
15248
15249 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15250 is a declaration, but all versions of G++ as of this writing
15251 (so through at least 3.2.1) incorrectly generate
15252 DW_TAG_variable tags. */
15253
15254 const char *physname;
15255
15256 /* Get name of field. */
15257 fieldname = dwarf2_name (die, cu);
15258 if (fieldname == NULL)
15259 return;
15260
15261 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15262 if (attr
15263 /* Only create a symbol if this is an external value.
15264 new_symbol checks this and puts the value in the global symbol
15265 table, which we want. If it is not external, new_symbol
15266 will try to put the value in cu->list_in_scope which is wrong. */
15267 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15268 {
15269 /* A static const member, not much different than an enum as far as
15270 we're concerned, except that we can support more types. */
15271 new_symbol (die, NULL, cu);
15272 }
15273
15274 /* Get physical name. */
15275 physname = dwarf2_physname (fieldname, die, cu);
15276
15277 /* The name is already allocated along with this objfile, so we don't
15278 need to duplicate it for the type. */
15279 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15280 fp->set_type (die_type (die, cu));
15281 FIELD_NAME (*fp) = fieldname;
15282 }
15283 else if (die->tag == DW_TAG_inheritance)
15284 {
15285 /* C++ base class field. */
15286 handle_data_member_location (die, cu, fp);
15287 FIELD_BITSIZE (*fp) = 0;
15288 fp->set_type (die_type (die, cu));
15289 FIELD_NAME (*fp) = fp->type ()->name ();
15290 }
15291 else
15292 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15293 }
15294
15295 /* Can the type given by DIE define another type? */
15296
15297 static bool
15298 type_can_define_types (const struct die_info *die)
15299 {
15300 switch (die->tag)
15301 {
15302 case DW_TAG_typedef:
15303 case DW_TAG_class_type:
15304 case DW_TAG_structure_type:
15305 case DW_TAG_union_type:
15306 case DW_TAG_enumeration_type:
15307 return true;
15308
15309 default:
15310 return false;
15311 }
15312 }
15313
15314 /* Add a type definition defined in the scope of the FIP's class. */
15315
15316 static void
15317 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15318 struct dwarf2_cu *cu)
15319 {
15320 struct decl_field fp;
15321 memset (&fp, 0, sizeof (fp));
15322
15323 gdb_assert (type_can_define_types (die));
15324
15325 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15326 fp.name = dwarf2_name (die, cu);
15327 fp.type = read_type_die (die, cu);
15328
15329 /* Save accessibility. */
15330 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15331 switch (accessibility)
15332 {
15333 case DW_ACCESS_public:
15334 /* The assumed value if neither private nor protected. */
15335 break;
15336 case DW_ACCESS_private:
15337 fp.is_private = 1;
15338 break;
15339 case DW_ACCESS_protected:
15340 fp.is_protected = 1;
15341 break;
15342 }
15343
15344 if (die->tag == DW_TAG_typedef)
15345 fip->typedef_field_list.push_back (fp);
15346 else
15347 fip->nested_types_list.push_back (fp);
15348 }
15349
15350 /* A convenience typedef that's used when finding the discriminant
15351 field for a variant part. */
15352 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15353 offset_map_type;
15354
15355 /* Compute the discriminant range for a given variant. OBSTACK is
15356 where the results will be stored. VARIANT is the variant to
15357 process. IS_UNSIGNED indicates whether the discriminant is signed
15358 or unsigned. */
15359
15360 static const gdb::array_view<discriminant_range>
15361 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15362 bool is_unsigned)
15363 {
15364 std::vector<discriminant_range> ranges;
15365
15366 if (variant.default_branch)
15367 return {};
15368
15369 if (variant.discr_list_data == nullptr)
15370 {
15371 discriminant_range r
15372 = {variant.discriminant_value, variant.discriminant_value};
15373 ranges.push_back (r);
15374 }
15375 else
15376 {
15377 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15378 variant.discr_list_data->size);
15379 while (!data.empty ())
15380 {
15381 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15382 {
15383 complaint (_("invalid discriminant marker: %d"), data[0]);
15384 break;
15385 }
15386 bool is_range = data[0] == DW_DSC_range;
15387 data = data.slice (1);
15388
15389 ULONGEST low, high;
15390 unsigned int bytes_read;
15391
15392 if (data.empty ())
15393 {
15394 complaint (_("DW_AT_discr_list missing low value"));
15395 break;
15396 }
15397 if (is_unsigned)
15398 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15399 else
15400 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15401 &bytes_read);
15402 data = data.slice (bytes_read);
15403
15404 if (is_range)
15405 {
15406 if (data.empty ())
15407 {
15408 complaint (_("DW_AT_discr_list missing high value"));
15409 break;
15410 }
15411 if (is_unsigned)
15412 high = read_unsigned_leb128 (nullptr, data.data (),
15413 &bytes_read);
15414 else
15415 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15416 &bytes_read);
15417 data = data.slice (bytes_read);
15418 }
15419 else
15420 high = low;
15421
15422 ranges.push_back ({ low, high });
15423 }
15424 }
15425
15426 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15427 ranges.size ());
15428 std::copy (ranges.begin (), ranges.end (), result);
15429 return gdb::array_view<discriminant_range> (result, ranges.size ());
15430 }
15431
15432 static const gdb::array_view<variant_part> create_variant_parts
15433 (struct obstack *obstack,
15434 const offset_map_type &offset_map,
15435 struct field_info *fi,
15436 const std::vector<variant_part_builder> &variant_parts);
15437
15438 /* Fill in a "struct variant" for a given variant field. RESULT is
15439 the variant to fill in. OBSTACK is where any needed allocations
15440 will be done. OFFSET_MAP holds the mapping from section offsets to
15441 fields for the type. FI describes the fields of the type we're
15442 processing. FIELD is the variant field we're converting. */
15443
15444 static void
15445 create_one_variant (variant &result, struct obstack *obstack,
15446 const offset_map_type &offset_map,
15447 struct field_info *fi, const variant_field &field)
15448 {
15449 result.discriminants = convert_variant_range (obstack, field, false);
15450 result.first_field = field.first_field + fi->baseclasses.size ();
15451 result.last_field = field.last_field + fi->baseclasses.size ();
15452 result.parts = create_variant_parts (obstack, offset_map, fi,
15453 field.variant_parts);
15454 }
15455
15456 /* Fill in a "struct variant_part" for a given variant part. RESULT
15457 is the variant part to fill in. OBSTACK is where any needed
15458 allocations will be done. OFFSET_MAP holds the mapping from
15459 section offsets to fields for the type. FI describes the fields of
15460 the type we're processing. BUILDER is the variant part to be
15461 converted. */
15462
15463 static void
15464 create_one_variant_part (variant_part &result,
15465 struct obstack *obstack,
15466 const offset_map_type &offset_map,
15467 struct field_info *fi,
15468 const variant_part_builder &builder)
15469 {
15470 auto iter = offset_map.find (builder.discriminant_offset);
15471 if (iter == offset_map.end ())
15472 {
15473 result.discriminant_index = -1;
15474 /* Doesn't matter. */
15475 result.is_unsigned = false;
15476 }
15477 else
15478 {
15479 result.discriminant_index = iter->second;
15480 result.is_unsigned
15481 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15482 }
15483
15484 size_t n = builder.variants.size ();
15485 variant *output = new (obstack) variant[n];
15486 for (size_t i = 0; i < n; ++i)
15487 create_one_variant (output[i], obstack, offset_map, fi,
15488 builder.variants[i]);
15489
15490 result.variants = gdb::array_view<variant> (output, n);
15491 }
15492
15493 /* Create a vector of variant parts that can be attached to a type.
15494 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15495 holds the mapping from section offsets to fields for the type. FI
15496 describes the fields of the type we're processing. VARIANT_PARTS
15497 is the vector to convert. */
15498
15499 static const gdb::array_view<variant_part>
15500 create_variant_parts (struct obstack *obstack,
15501 const offset_map_type &offset_map,
15502 struct field_info *fi,
15503 const std::vector<variant_part_builder> &variant_parts)
15504 {
15505 if (variant_parts.empty ())
15506 return {};
15507
15508 size_t n = variant_parts.size ();
15509 variant_part *result = new (obstack) variant_part[n];
15510 for (size_t i = 0; i < n; ++i)
15511 create_one_variant_part (result[i], obstack, offset_map, fi,
15512 variant_parts[i]);
15513
15514 return gdb::array_view<variant_part> (result, n);
15515 }
15516
15517 /* Compute the variant part vector for FIP, attaching it to TYPE when
15518 done. */
15519
15520 static void
15521 add_variant_property (struct field_info *fip, struct type *type,
15522 struct dwarf2_cu *cu)
15523 {
15524 /* Map section offsets of fields to their field index. Note the
15525 field index here does not take the number of baseclasses into
15526 account. */
15527 offset_map_type offset_map;
15528 for (int i = 0; i < fip->fields.size (); ++i)
15529 offset_map[fip->fields[i].offset] = i;
15530
15531 struct objfile *objfile = cu->per_objfile->objfile;
15532 gdb::array_view<variant_part> parts
15533 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15534 fip->variant_parts);
15535
15536 struct dynamic_prop prop;
15537 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15538 obstack_copy (&objfile->objfile_obstack, &parts,
15539 sizeof (parts)));
15540
15541 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15542 }
15543
15544 /* Create the vector of fields, and attach it to the type. */
15545
15546 static void
15547 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15548 struct dwarf2_cu *cu)
15549 {
15550 int nfields = fip->nfields ();
15551
15552 /* Record the field count, allocate space for the array of fields,
15553 and create blank accessibility bitfields if necessary. */
15554 type->set_num_fields (nfields);
15555 type->set_fields
15556 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15557
15558 if (fip->non_public_fields && cu->language != language_ada)
15559 {
15560 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15561
15562 TYPE_FIELD_PRIVATE_BITS (type) =
15563 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15564 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15565
15566 TYPE_FIELD_PROTECTED_BITS (type) =
15567 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15568 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15569
15570 TYPE_FIELD_IGNORE_BITS (type) =
15571 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15572 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15573 }
15574
15575 /* If the type has baseclasses, allocate and clear a bit vector for
15576 TYPE_FIELD_VIRTUAL_BITS. */
15577 if (!fip->baseclasses.empty () && cu->language != language_ada)
15578 {
15579 int num_bytes = B_BYTES (fip->baseclasses.size ());
15580 unsigned char *pointer;
15581
15582 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15583 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15584 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15585 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15586 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15587 }
15588
15589 if (!fip->variant_parts.empty ())
15590 add_variant_property (fip, type, cu);
15591
15592 /* Copy the saved-up fields into the field vector. */
15593 for (int i = 0; i < nfields; ++i)
15594 {
15595 struct nextfield &field
15596 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15597 : fip->fields[i - fip->baseclasses.size ()]);
15598
15599 type->field (i) = field.field;
15600 switch (field.accessibility)
15601 {
15602 case DW_ACCESS_private:
15603 if (cu->language != language_ada)
15604 SET_TYPE_FIELD_PRIVATE (type, i);
15605 break;
15606
15607 case DW_ACCESS_protected:
15608 if (cu->language != language_ada)
15609 SET_TYPE_FIELD_PROTECTED (type, i);
15610 break;
15611
15612 case DW_ACCESS_public:
15613 break;
15614
15615 default:
15616 /* Unknown accessibility. Complain and treat it as public. */
15617 {
15618 complaint (_("unsupported accessibility %d"),
15619 field.accessibility);
15620 }
15621 break;
15622 }
15623 if (i < fip->baseclasses.size ())
15624 {
15625 switch (field.virtuality)
15626 {
15627 case DW_VIRTUALITY_virtual:
15628 case DW_VIRTUALITY_pure_virtual:
15629 if (cu->language == language_ada)
15630 error (_("unexpected virtuality in component of Ada type"));
15631 SET_TYPE_FIELD_VIRTUAL (type, i);
15632 break;
15633 }
15634 }
15635 }
15636 }
15637
15638 /* Return true if this member function is a constructor, false
15639 otherwise. */
15640
15641 static int
15642 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15643 {
15644 const char *fieldname;
15645 const char *type_name;
15646 int len;
15647
15648 if (die->parent == NULL)
15649 return 0;
15650
15651 if (die->parent->tag != DW_TAG_structure_type
15652 && die->parent->tag != DW_TAG_union_type
15653 && die->parent->tag != DW_TAG_class_type)
15654 return 0;
15655
15656 fieldname = dwarf2_name (die, cu);
15657 type_name = dwarf2_name (die->parent, cu);
15658 if (fieldname == NULL || type_name == NULL)
15659 return 0;
15660
15661 len = strlen (fieldname);
15662 return (strncmp (fieldname, type_name, len) == 0
15663 && (type_name[len] == '\0' || type_name[len] == '<'));
15664 }
15665
15666 /* Add a member function to the proper fieldlist. */
15667
15668 static void
15669 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15670 struct type *type, struct dwarf2_cu *cu)
15671 {
15672 struct objfile *objfile = cu->per_objfile->objfile;
15673 struct attribute *attr;
15674 int i;
15675 struct fnfieldlist *flp = nullptr;
15676 struct fn_field *fnp;
15677 const char *fieldname;
15678 struct type *this_type;
15679
15680 if (cu->language == language_ada)
15681 error (_("unexpected member function in Ada type"));
15682
15683 /* Get name of member function. */
15684 fieldname = dwarf2_name (die, cu);
15685 if (fieldname == NULL)
15686 return;
15687
15688 /* Look up member function name in fieldlist. */
15689 for (i = 0; i < fip->fnfieldlists.size (); i++)
15690 {
15691 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15692 {
15693 flp = &fip->fnfieldlists[i];
15694 break;
15695 }
15696 }
15697
15698 /* Create a new fnfieldlist if necessary. */
15699 if (flp == nullptr)
15700 {
15701 fip->fnfieldlists.emplace_back ();
15702 flp = &fip->fnfieldlists.back ();
15703 flp->name = fieldname;
15704 i = fip->fnfieldlists.size () - 1;
15705 }
15706
15707 /* Create a new member function field and add it to the vector of
15708 fnfieldlists. */
15709 flp->fnfields.emplace_back ();
15710 fnp = &flp->fnfields.back ();
15711
15712 /* Delay processing of the physname until later. */
15713 if (cu->language == language_cplus)
15714 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15715 die, cu);
15716 else
15717 {
15718 const char *physname = dwarf2_physname (fieldname, die, cu);
15719 fnp->physname = physname ? physname : "";
15720 }
15721
15722 fnp->type = alloc_type (objfile);
15723 this_type = read_type_die (die, cu);
15724 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15725 {
15726 int nparams = this_type->num_fields ();
15727
15728 /* TYPE is the domain of this method, and THIS_TYPE is the type
15729 of the method itself (TYPE_CODE_METHOD). */
15730 smash_to_method_type (fnp->type, type,
15731 TYPE_TARGET_TYPE (this_type),
15732 this_type->fields (),
15733 this_type->num_fields (),
15734 this_type->has_varargs ());
15735
15736 /* Handle static member functions.
15737 Dwarf2 has no clean way to discern C++ static and non-static
15738 member functions. G++ helps GDB by marking the first
15739 parameter for non-static member functions (which is the this
15740 pointer) as artificial. We obtain this information from
15741 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15742 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15743 fnp->voffset = VOFFSET_STATIC;
15744 }
15745 else
15746 complaint (_("member function type missing for '%s'"),
15747 dwarf2_full_name (fieldname, die, cu));
15748
15749 /* Get fcontext from DW_AT_containing_type if present. */
15750 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15751 fnp->fcontext = die_containing_type (die, cu);
15752
15753 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15754 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15755
15756 /* Get accessibility. */
15757 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15758 switch (accessibility)
15759 {
15760 case DW_ACCESS_private:
15761 fnp->is_private = 1;
15762 break;
15763 case DW_ACCESS_protected:
15764 fnp->is_protected = 1;
15765 break;
15766 }
15767
15768 /* Check for artificial methods. */
15769 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15770 if (attr && attr->as_boolean ())
15771 fnp->is_artificial = 1;
15772
15773 /* Check for defaulted methods. */
15774 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15775 if (attr != nullptr)
15776 fnp->defaulted = attr->defaulted ();
15777
15778 /* Check for deleted methods. */
15779 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15780 if (attr != nullptr && attr->as_boolean ())
15781 fnp->is_deleted = 1;
15782
15783 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15784
15785 /* Get index in virtual function table if it is a virtual member
15786 function. For older versions of GCC, this is an offset in the
15787 appropriate virtual table, as specified by DW_AT_containing_type.
15788 For everyone else, it is an expression to be evaluated relative
15789 to the object address. */
15790
15791 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15792 if (attr != nullptr)
15793 {
15794 if (attr->form_is_block () && attr->as_block ()->size > 0)
15795 {
15796 struct dwarf_block *block = attr->as_block ();
15797
15798 if (block->data[0] == DW_OP_constu)
15799 {
15800 /* Old-style GCC. */
15801 fnp->voffset = decode_locdesc (block, cu) + 2;
15802 }
15803 else if (block->data[0] == DW_OP_deref
15804 || (block->size > 1
15805 && block->data[0] == DW_OP_deref_size
15806 && block->data[1] == cu->header.addr_size))
15807 {
15808 fnp->voffset = decode_locdesc (block, cu);
15809 if ((fnp->voffset % cu->header.addr_size) != 0)
15810 dwarf2_complex_location_expr_complaint ();
15811 else
15812 fnp->voffset /= cu->header.addr_size;
15813 fnp->voffset += 2;
15814 }
15815 else
15816 dwarf2_complex_location_expr_complaint ();
15817
15818 if (!fnp->fcontext)
15819 {
15820 /* If there is no `this' field and no DW_AT_containing_type,
15821 we cannot actually find a base class context for the
15822 vtable! */
15823 if (this_type->num_fields () == 0
15824 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15825 {
15826 complaint (_("cannot determine context for virtual member "
15827 "function \"%s\" (offset %s)"),
15828 fieldname, sect_offset_str (die->sect_off));
15829 }
15830 else
15831 {
15832 fnp->fcontext
15833 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15834 }
15835 }
15836 }
15837 else if (attr->form_is_section_offset ())
15838 {
15839 dwarf2_complex_location_expr_complaint ();
15840 }
15841 else
15842 {
15843 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15844 fieldname);
15845 }
15846 }
15847 else
15848 {
15849 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15850 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15851 {
15852 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15853 complaint (_("Member function \"%s\" (offset %s) is virtual "
15854 "but the vtable offset is not specified"),
15855 fieldname, sect_offset_str (die->sect_off));
15856 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15857 TYPE_CPLUS_DYNAMIC (type) = 1;
15858 }
15859 }
15860 }
15861
15862 /* Create the vector of member function fields, and attach it to the type. */
15863
15864 static void
15865 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15866 struct dwarf2_cu *cu)
15867 {
15868 if (cu->language == language_ada)
15869 error (_("unexpected member functions in Ada type"));
15870
15871 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15872 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15873 TYPE_ALLOC (type,
15874 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15875
15876 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15877 {
15878 struct fnfieldlist &nf = fip->fnfieldlists[i];
15879 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15880
15881 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15882 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15883 fn_flp->fn_fields = (struct fn_field *)
15884 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15885
15886 for (int k = 0; k < nf.fnfields.size (); ++k)
15887 fn_flp->fn_fields[k] = nf.fnfields[k];
15888 }
15889
15890 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15891 }
15892
15893 /* Returns non-zero if NAME is the name of a vtable member in CU's
15894 language, zero otherwise. */
15895 static int
15896 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15897 {
15898 static const char vptr[] = "_vptr";
15899
15900 /* Look for the C++ form of the vtable. */
15901 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15902 return 1;
15903
15904 return 0;
15905 }
15906
15907 /* GCC outputs unnamed structures that are really pointers to member
15908 functions, with the ABI-specified layout. If TYPE describes
15909 such a structure, smash it into a member function type.
15910
15911 GCC shouldn't do this; it should just output pointer to member DIEs.
15912 This is GCC PR debug/28767. */
15913
15914 static void
15915 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15916 {
15917 struct type *pfn_type, *self_type, *new_type;
15918
15919 /* Check for a structure with no name and two children. */
15920 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15921 return;
15922
15923 /* Check for __pfn and __delta members. */
15924 if (TYPE_FIELD_NAME (type, 0) == NULL
15925 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15926 || TYPE_FIELD_NAME (type, 1) == NULL
15927 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15928 return;
15929
15930 /* Find the type of the method. */
15931 pfn_type = type->field (0).type ();
15932 if (pfn_type == NULL
15933 || pfn_type->code () != TYPE_CODE_PTR
15934 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15935 return;
15936
15937 /* Look for the "this" argument. */
15938 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15939 if (pfn_type->num_fields () == 0
15940 /* || pfn_type->field (0).type () == NULL */
15941 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15942 return;
15943
15944 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15945 new_type = alloc_type (objfile);
15946 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15947 pfn_type->fields (), pfn_type->num_fields (),
15948 pfn_type->has_varargs ());
15949 smash_to_methodptr_type (type, new_type);
15950 }
15951
15952 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15953 requires rewriting, then copy it and return the updated copy.
15954 Otherwise return nullptr. */
15955
15956 static struct type *
15957 rewrite_array_type (struct type *type)
15958 {
15959 if (type->code () != TYPE_CODE_ARRAY)
15960 return nullptr;
15961
15962 struct type *index_type = type->index_type ();
15963 range_bounds *current_bounds = index_type->bounds ();
15964
15965 /* Handle multi-dimensional arrays. */
15966 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15967 if (new_target == nullptr)
15968 {
15969 /* Maybe we don't need to rewrite this array. */
15970 if (current_bounds->low.kind () == PROP_CONST
15971 && current_bounds->high.kind () == PROP_CONST)
15972 return nullptr;
15973 }
15974
15975 /* Either the target type was rewritten, or the bounds have to be
15976 updated. Either way we want to copy the type and update
15977 everything. */
15978 struct type *copy = copy_type (type);
15979 int nfields = copy->num_fields ();
15980 field *new_fields
15981 = ((struct field *) TYPE_ZALLOC (copy,
15982 nfields * sizeof (struct field)));
15983 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15984 copy->set_fields (new_fields);
15985 if (new_target != nullptr)
15986 TYPE_TARGET_TYPE (copy) = new_target;
15987
15988 struct type *index_copy = copy_type (index_type);
15989 range_bounds *bounds
15990 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15991 sizeof (range_bounds));
15992 *bounds = *current_bounds;
15993 bounds->low.set_const_val (1);
15994 bounds->high.set_const_val (0);
15995 index_copy->set_bounds (bounds);
15996 copy->set_index_type (index_copy);
15997
15998 return copy;
15999 }
16000
16001 /* While some versions of GCC will generate complicated DWARF for an
16002 array (see quirk_ada_thick_pointer), more recent versions were
16003 modified to emit an explicit thick pointer structure. However, in
16004 this case, the array still has DWARF expressions for its ranges,
16005 and these must be ignored. */
16006
16007 static void
16008 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
16009 struct type *type)
16010 {
16011 gdb_assert (cu->language == language_ada);
16012
16013 /* Check for a structure with two children. */
16014 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
16015 return;
16016
16017 /* Check for P_ARRAY and P_BOUNDS members. */
16018 if (TYPE_FIELD_NAME (type, 0) == NULL
16019 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
16020 || TYPE_FIELD_NAME (type, 1) == NULL
16021 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
16022 return;
16023
16024 /* Make sure we're looking at a pointer to an array. */
16025 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
16026 return;
16027
16028 /* The Ada code already knows how to handle these types, so all that
16029 we need to do is turn the bounds into static bounds. However, we
16030 don't want to rewrite existing array or index types in-place,
16031 because those may be referenced in other contexts where this
16032 rewriting is undesirable. */
16033 struct type *new_ary_type
16034 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
16035 if (new_ary_type != nullptr)
16036 type->field (0).set_type (lookup_pointer_type (new_ary_type));
16037 }
16038
16039 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
16040 appropriate error checking and issuing complaints if there is a
16041 problem. */
16042
16043 static ULONGEST
16044 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
16045 {
16046 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
16047
16048 if (attr == nullptr)
16049 return 0;
16050
16051 if (!attr->form_is_constant ())
16052 {
16053 complaint (_("DW_AT_alignment must have constant form"
16054 " - DIE at %s [in module %s]"),
16055 sect_offset_str (die->sect_off),
16056 objfile_name (cu->per_objfile->objfile));
16057 return 0;
16058 }
16059
16060 LONGEST val = attr->constant_value (0);
16061 if (val < 0)
16062 {
16063 complaint (_("DW_AT_alignment value must not be negative"
16064 " - DIE at %s [in module %s]"),
16065 sect_offset_str (die->sect_off),
16066 objfile_name (cu->per_objfile->objfile));
16067 return 0;
16068 }
16069 ULONGEST align = val;
16070
16071 if (align == 0)
16072 {
16073 complaint (_("DW_AT_alignment value must not be zero"
16074 " - DIE at %s [in module %s]"),
16075 sect_offset_str (die->sect_off),
16076 objfile_name (cu->per_objfile->objfile));
16077 return 0;
16078 }
16079 if ((align & (align - 1)) != 0)
16080 {
16081 complaint (_("DW_AT_alignment value must be a power of 2"
16082 " - DIE at %s [in module %s]"),
16083 sect_offset_str (die->sect_off),
16084 objfile_name (cu->per_objfile->objfile));
16085 return 0;
16086 }
16087
16088 return align;
16089 }
16090
16091 /* If the DIE has a DW_AT_alignment attribute, use its value to set
16092 the alignment for TYPE. */
16093
16094 static void
16095 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16096 struct type *type)
16097 {
16098 if (!set_type_align (type, get_alignment (cu, die)))
16099 complaint (_("DW_AT_alignment value too large"
16100 " - DIE at %s [in module %s]"),
16101 sect_offset_str (die->sect_off),
16102 objfile_name (cu->per_objfile->objfile));
16103 }
16104
16105 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16106 constant for a type, according to DWARF5 spec, Table 5.5. */
16107
16108 static bool
16109 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16110 {
16111 switch (value)
16112 {
16113 case DW_CC_normal:
16114 case DW_CC_pass_by_reference:
16115 case DW_CC_pass_by_value:
16116 return true;
16117
16118 default:
16119 complaint (_("unrecognized DW_AT_calling_convention value "
16120 "(%s) for a type"), pulongest (value));
16121 return false;
16122 }
16123 }
16124
16125 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16126 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16127 also according to GNU-specific values (see include/dwarf2.h). */
16128
16129 static bool
16130 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16131 {
16132 switch (value)
16133 {
16134 case DW_CC_normal:
16135 case DW_CC_program:
16136 case DW_CC_nocall:
16137 return true;
16138
16139 case DW_CC_GNU_renesas_sh:
16140 case DW_CC_GNU_borland_fastcall_i386:
16141 case DW_CC_GDB_IBM_OpenCL:
16142 return true;
16143
16144 default:
16145 complaint (_("unrecognized DW_AT_calling_convention value "
16146 "(%s) for a subroutine"), pulongest (value));
16147 return false;
16148 }
16149 }
16150
16151 /* Called when we find the DIE that starts a structure or union scope
16152 (definition) to create a type for the structure or union. Fill in
16153 the type's name and general properties; the members will not be
16154 processed until process_structure_scope. A symbol table entry for
16155 the type will also not be done until process_structure_scope (assuming
16156 the type has a name).
16157
16158 NOTE: we need to call these functions regardless of whether or not the
16159 DIE has a DW_AT_name attribute, since it might be an anonymous
16160 structure or union. This gets the type entered into our set of
16161 user defined types. */
16162
16163 static struct type *
16164 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16165 {
16166 struct objfile *objfile = cu->per_objfile->objfile;
16167 struct type *type;
16168 struct attribute *attr;
16169 const char *name;
16170
16171 /* If the definition of this type lives in .debug_types, read that type.
16172 Don't follow DW_AT_specification though, that will take us back up
16173 the chain and we want to go down. */
16174 attr = die->attr (DW_AT_signature);
16175 if (attr != nullptr)
16176 {
16177 type = get_DW_AT_signature_type (die, attr, cu);
16178
16179 /* The type's CU may not be the same as CU.
16180 Ensure TYPE is recorded with CU in die_type_hash. */
16181 return set_die_type (die, type, cu);
16182 }
16183
16184 type = alloc_type (objfile);
16185 INIT_CPLUS_SPECIFIC (type);
16186
16187 name = dwarf2_name (die, cu);
16188 if (name != NULL)
16189 {
16190 if (cu->language == language_cplus
16191 || cu->language == language_d
16192 || cu->language == language_rust)
16193 {
16194 const char *full_name = dwarf2_full_name (name, die, cu);
16195
16196 /* dwarf2_full_name might have already finished building the DIE's
16197 type. If so, there is no need to continue. */
16198 if (get_die_type (die, cu) != NULL)
16199 return get_die_type (die, cu);
16200
16201 type->set_name (full_name);
16202 }
16203 else
16204 {
16205 /* The name is already allocated along with this objfile, so
16206 we don't need to duplicate it for the type. */
16207 type->set_name (name);
16208 }
16209 }
16210
16211 if (die->tag == DW_TAG_structure_type)
16212 {
16213 type->set_code (TYPE_CODE_STRUCT);
16214 }
16215 else if (die->tag == DW_TAG_union_type)
16216 {
16217 type->set_code (TYPE_CODE_UNION);
16218 }
16219 else
16220 {
16221 type->set_code (TYPE_CODE_STRUCT);
16222 }
16223
16224 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16225 type->set_is_declared_class (true);
16226
16227 /* Store the calling convention in the type if it's available in
16228 the die. Otherwise the calling convention remains set to
16229 the default value DW_CC_normal. */
16230 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16231 if (attr != nullptr
16232 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16233 {
16234 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16235 TYPE_CPLUS_CALLING_CONVENTION (type)
16236 = (enum dwarf_calling_convention) (attr->constant_value (0));
16237 }
16238
16239 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16240 if (attr != nullptr)
16241 {
16242 if (attr->form_is_constant ())
16243 TYPE_LENGTH (type) = attr->constant_value (0);
16244 else
16245 {
16246 struct dynamic_prop prop;
16247 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16248 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16249 TYPE_LENGTH (type) = 0;
16250 }
16251 }
16252 else
16253 {
16254 TYPE_LENGTH (type) = 0;
16255 }
16256
16257 maybe_set_alignment (cu, die, type);
16258
16259 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16260 {
16261 /* ICC<14 does not output the required DW_AT_declaration on
16262 incomplete types, but gives them a size of zero. */
16263 type->set_is_stub (true);
16264 }
16265 else
16266 type->set_stub_is_supported (true);
16267
16268 if (die_is_declaration (die, cu))
16269 type->set_is_stub (true);
16270 else if (attr == NULL && die->child == NULL
16271 && producer_is_realview (cu->producer))
16272 /* RealView does not output the required DW_AT_declaration
16273 on incomplete types. */
16274 type->set_is_stub (true);
16275
16276 /* We need to add the type field to the die immediately so we don't
16277 infinitely recurse when dealing with pointers to the structure
16278 type within the structure itself. */
16279 set_die_type (die, type, cu);
16280
16281 /* set_die_type should be already done. */
16282 set_descriptive_type (type, die, cu);
16283
16284 return type;
16285 }
16286
16287 static void handle_struct_member_die
16288 (struct die_info *child_die,
16289 struct type *type,
16290 struct field_info *fi,
16291 std::vector<struct symbol *> *template_args,
16292 struct dwarf2_cu *cu);
16293
16294 /* A helper for handle_struct_member_die that handles
16295 DW_TAG_variant_part. */
16296
16297 static void
16298 handle_variant_part (struct die_info *die, struct type *type,
16299 struct field_info *fi,
16300 std::vector<struct symbol *> *template_args,
16301 struct dwarf2_cu *cu)
16302 {
16303 variant_part_builder *new_part;
16304 if (fi->current_variant_part == nullptr)
16305 {
16306 fi->variant_parts.emplace_back ();
16307 new_part = &fi->variant_parts.back ();
16308 }
16309 else if (!fi->current_variant_part->processing_variant)
16310 {
16311 complaint (_("nested DW_TAG_variant_part seen "
16312 "- DIE at %s [in module %s]"),
16313 sect_offset_str (die->sect_off),
16314 objfile_name (cu->per_objfile->objfile));
16315 return;
16316 }
16317 else
16318 {
16319 variant_field &current = fi->current_variant_part->variants.back ();
16320 current.variant_parts.emplace_back ();
16321 new_part = &current.variant_parts.back ();
16322 }
16323
16324 /* When we recurse, we want callees to add to this new variant
16325 part. */
16326 scoped_restore save_current_variant_part
16327 = make_scoped_restore (&fi->current_variant_part, new_part);
16328
16329 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16330 if (discr == NULL)
16331 {
16332 /* It's a univariant form, an extension we support. */
16333 }
16334 else if (discr->form_is_ref ())
16335 {
16336 struct dwarf2_cu *target_cu = cu;
16337 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16338
16339 new_part->discriminant_offset = target_die->sect_off;
16340 }
16341 else
16342 {
16343 complaint (_("DW_AT_discr does not have DIE reference form"
16344 " - DIE at %s [in module %s]"),
16345 sect_offset_str (die->sect_off),
16346 objfile_name (cu->per_objfile->objfile));
16347 }
16348
16349 for (die_info *child_die = die->child;
16350 child_die != NULL;
16351 child_die = child_die->sibling)
16352 handle_struct_member_die (child_die, type, fi, template_args, cu);
16353 }
16354
16355 /* A helper for handle_struct_member_die that handles
16356 DW_TAG_variant. */
16357
16358 static void
16359 handle_variant (struct die_info *die, struct type *type,
16360 struct field_info *fi,
16361 std::vector<struct symbol *> *template_args,
16362 struct dwarf2_cu *cu)
16363 {
16364 if (fi->current_variant_part == nullptr)
16365 {
16366 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16367 "- DIE at %s [in module %s]"),
16368 sect_offset_str (die->sect_off),
16369 objfile_name (cu->per_objfile->objfile));
16370 return;
16371 }
16372 if (fi->current_variant_part->processing_variant)
16373 {
16374 complaint (_("nested DW_TAG_variant seen "
16375 "- DIE at %s [in module %s]"),
16376 sect_offset_str (die->sect_off),
16377 objfile_name (cu->per_objfile->objfile));
16378 return;
16379 }
16380
16381 scoped_restore save_processing_variant
16382 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16383 true);
16384
16385 fi->current_variant_part->variants.emplace_back ();
16386 variant_field &variant = fi->current_variant_part->variants.back ();
16387 variant.first_field = fi->fields.size ();
16388
16389 /* In a variant we want to get the discriminant and also add a
16390 field for our sole member child. */
16391 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16392 if (discr == nullptr || !discr->form_is_constant ())
16393 {
16394 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16395 if (discr == nullptr || discr->as_block ()->size == 0)
16396 variant.default_branch = true;
16397 else
16398 variant.discr_list_data = discr->as_block ();
16399 }
16400 else
16401 variant.discriminant_value = discr->constant_value (0);
16402
16403 for (die_info *variant_child = die->child;
16404 variant_child != NULL;
16405 variant_child = variant_child->sibling)
16406 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16407
16408 variant.last_field = fi->fields.size ();
16409 }
16410
16411 /* A helper for process_structure_scope that handles a single member
16412 DIE. */
16413
16414 static void
16415 handle_struct_member_die (struct die_info *child_die, struct type *type,
16416 struct field_info *fi,
16417 std::vector<struct symbol *> *template_args,
16418 struct dwarf2_cu *cu)
16419 {
16420 if (child_die->tag == DW_TAG_member
16421 || child_die->tag == DW_TAG_variable)
16422 {
16423 /* NOTE: carlton/2002-11-05: A C++ static data member
16424 should be a DW_TAG_member that is a declaration, but
16425 all versions of G++ as of this writing (so through at
16426 least 3.2.1) incorrectly generate DW_TAG_variable
16427 tags for them instead. */
16428 dwarf2_add_field (fi, child_die, cu);
16429 }
16430 else if (child_die->tag == DW_TAG_subprogram)
16431 {
16432 /* Rust doesn't have member functions in the C++ sense.
16433 However, it does emit ordinary functions as children
16434 of a struct DIE. */
16435 if (cu->language == language_rust)
16436 read_func_scope (child_die, cu);
16437 else
16438 {
16439 /* C++ member function. */
16440 dwarf2_add_member_fn (fi, child_die, type, cu);
16441 }
16442 }
16443 else if (child_die->tag == DW_TAG_inheritance)
16444 {
16445 /* C++ base class field. */
16446 dwarf2_add_field (fi, child_die, cu);
16447 }
16448 else if (type_can_define_types (child_die))
16449 dwarf2_add_type_defn (fi, child_die, cu);
16450 else if (child_die->tag == DW_TAG_template_type_param
16451 || child_die->tag == DW_TAG_template_value_param)
16452 {
16453 struct symbol *arg = new_symbol (child_die, NULL, cu);
16454
16455 if (arg != NULL)
16456 template_args->push_back (arg);
16457 }
16458 else if (child_die->tag == DW_TAG_variant_part)
16459 handle_variant_part (child_die, type, fi, template_args, cu);
16460 else if (child_die->tag == DW_TAG_variant)
16461 handle_variant (child_die, type, fi, template_args, cu);
16462 }
16463
16464 /* Finish creating a structure or union type, including filling in
16465 its members and creating a symbol for it. */
16466
16467 static void
16468 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16469 {
16470 struct objfile *objfile = cu->per_objfile->objfile;
16471 struct die_info *child_die;
16472 struct type *type;
16473
16474 type = get_die_type (die, cu);
16475 if (type == NULL)
16476 type = read_structure_type (die, cu);
16477
16478 bool has_template_parameters = false;
16479 if (die->child != NULL && ! die_is_declaration (die, cu))
16480 {
16481 struct field_info fi;
16482 std::vector<struct symbol *> template_args;
16483
16484 child_die = die->child;
16485
16486 while (child_die && child_die->tag)
16487 {
16488 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16489 child_die = child_die->sibling;
16490 }
16491
16492 /* Attach template arguments to type. */
16493 if (!template_args.empty ())
16494 {
16495 has_template_parameters = true;
16496 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16497 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16498 TYPE_TEMPLATE_ARGUMENTS (type)
16499 = XOBNEWVEC (&objfile->objfile_obstack,
16500 struct symbol *,
16501 TYPE_N_TEMPLATE_ARGUMENTS (type));
16502 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16503 template_args.data (),
16504 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16505 * sizeof (struct symbol *)));
16506 }
16507
16508 /* Attach fields and member functions to the type. */
16509 if (fi.nfields () > 0)
16510 dwarf2_attach_fields_to_type (&fi, type, cu);
16511 if (!fi.fnfieldlists.empty ())
16512 {
16513 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16514
16515 /* Get the type which refers to the base class (possibly this
16516 class itself) which contains the vtable pointer for the current
16517 class from the DW_AT_containing_type attribute. This use of
16518 DW_AT_containing_type is a GNU extension. */
16519
16520 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16521 {
16522 struct type *t = die_containing_type (die, cu);
16523
16524 set_type_vptr_basetype (type, t);
16525 if (type == t)
16526 {
16527 int i;
16528
16529 /* Our own class provides vtbl ptr. */
16530 for (i = t->num_fields () - 1;
16531 i >= TYPE_N_BASECLASSES (t);
16532 --i)
16533 {
16534 const char *fieldname = TYPE_FIELD_NAME (t, i);
16535
16536 if (is_vtable_name (fieldname, cu))
16537 {
16538 set_type_vptr_fieldno (type, i);
16539 break;
16540 }
16541 }
16542
16543 /* Complain if virtual function table field not found. */
16544 if (i < TYPE_N_BASECLASSES (t))
16545 complaint (_("virtual function table pointer "
16546 "not found when defining class '%s'"),
16547 type->name () ? type->name () : "");
16548 }
16549 else
16550 {
16551 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16552 }
16553 }
16554 else if (cu->producer
16555 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16556 {
16557 /* The IBM XLC compiler does not provide direct indication
16558 of the containing type, but the vtable pointer is
16559 always named __vfp. */
16560
16561 int i;
16562
16563 for (i = type->num_fields () - 1;
16564 i >= TYPE_N_BASECLASSES (type);
16565 --i)
16566 {
16567 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16568 {
16569 set_type_vptr_fieldno (type, i);
16570 set_type_vptr_basetype (type, type);
16571 break;
16572 }
16573 }
16574 }
16575 }
16576
16577 /* Copy fi.typedef_field_list linked list elements content into the
16578 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16579 if (!fi.typedef_field_list.empty ())
16580 {
16581 int count = fi.typedef_field_list.size ();
16582
16583 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16584 TYPE_TYPEDEF_FIELD_ARRAY (type)
16585 = ((struct decl_field *)
16586 TYPE_ALLOC (type,
16587 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16588 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16589
16590 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16591 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16592 }
16593
16594 /* Copy fi.nested_types_list linked list elements content into the
16595 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16596 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16597 {
16598 int count = fi.nested_types_list.size ();
16599
16600 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16601 TYPE_NESTED_TYPES_ARRAY (type)
16602 = ((struct decl_field *)
16603 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16604 TYPE_NESTED_TYPES_COUNT (type) = count;
16605
16606 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16607 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16608 }
16609 }
16610
16611 quirk_gcc_member_function_pointer (type, objfile);
16612 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16613 cu->rust_unions.push_back (type);
16614 else if (cu->language == language_ada)
16615 quirk_ada_thick_pointer_struct (die, cu, type);
16616
16617 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16618 snapshots) has been known to create a die giving a declaration
16619 for a class that has, as a child, a die giving a definition for a
16620 nested class. So we have to process our children even if the
16621 current die is a declaration. Normally, of course, a declaration
16622 won't have any children at all. */
16623
16624 child_die = die->child;
16625
16626 while (child_die != NULL && child_die->tag)
16627 {
16628 if (child_die->tag == DW_TAG_member
16629 || child_die->tag == DW_TAG_variable
16630 || child_die->tag == DW_TAG_inheritance
16631 || child_die->tag == DW_TAG_template_value_param
16632 || child_die->tag == DW_TAG_template_type_param)
16633 {
16634 /* Do nothing. */
16635 }
16636 else
16637 process_die (child_die, cu);
16638
16639 child_die = child_die->sibling;
16640 }
16641
16642 /* Do not consider external references. According to the DWARF standard,
16643 these DIEs are identified by the fact that they have no byte_size
16644 attribute, and a declaration attribute. */
16645 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16646 || !die_is_declaration (die, cu)
16647 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16648 {
16649 struct symbol *sym = new_symbol (die, type, cu);
16650
16651 if (has_template_parameters)
16652 {
16653 struct symtab *symtab;
16654 if (sym != nullptr)
16655 symtab = symbol_symtab (sym);
16656 else if (cu->line_header != nullptr)
16657 {
16658 /* Any related symtab will do. */
16659 symtab
16660 = cu->line_header->file_names ()[0].symtab;
16661 }
16662 else
16663 {
16664 symtab = nullptr;
16665 complaint (_("could not find suitable "
16666 "symtab for template parameter"
16667 " - DIE at %s [in module %s]"),
16668 sect_offset_str (die->sect_off),
16669 objfile_name (objfile));
16670 }
16671
16672 if (symtab != nullptr)
16673 {
16674 /* Make sure that the symtab is set on the new symbols.
16675 Even though they don't appear in this symtab directly,
16676 other parts of gdb assume that symbols do, and this is
16677 reasonably true. */
16678 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16679 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16680 }
16681 }
16682 }
16683 }
16684
16685 /* Assuming DIE is an enumeration type, and TYPE is its associated
16686 type, update TYPE using some information only available in DIE's
16687 children. In particular, the fields are computed. */
16688
16689 static void
16690 update_enumeration_type_from_children (struct die_info *die,
16691 struct type *type,
16692 struct dwarf2_cu *cu)
16693 {
16694 struct die_info *child_die;
16695 int unsigned_enum = 1;
16696 int flag_enum = 1;
16697
16698 auto_obstack obstack;
16699 std::vector<struct field> fields;
16700
16701 for (child_die = die->child;
16702 child_die != NULL && child_die->tag;
16703 child_die = child_die->sibling)
16704 {
16705 struct attribute *attr;
16706 LONGEST value;
16707 const gdb_byte *bytes;
16708 struct dwarf2_locexpr_baton *baton;
16709 const char *name;
16710
16711 if (child_die->tag != DW_TAG_enumerator)
16712 continue;
16713
16714 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16715 if (attr == NULL)
16716 continue;
16717
16718 name = dwarf2_name (child_die, cu);
16719 if (name == NULL)
16720 name = "<anonymous enumerator>";
16721
16722 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16723 &value, &bytes, &baton);
16724 if (value < 0)
16725 {
16726 unsigned_enum = 0;
16727 flag_enum = 0;
16728 }
16729 else
16730 {
16731 if (count_one_bits_ll (value) >= 2)
16732 flag_enum = 0;
16733 }
16734
16735 fields.emplace_back ();
16736 struct field &field = fields.back ();
16737 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16738 SET_FIELD_ENUMVAL (field, value);
16739 }
16740
16741 if (!fields.empty ())
16742 {
16743 type->set_num_fields (fields.size ());
16744 type->set_fields
16745 ((struct field *)
16746 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16747 memcpy (type->fields (), fields.data (),
16748 sizeof (struct field) * fields.size ());
16749 }
16750
16751 if (unsigned_enum)
16752 type->set_is_unsigned (true);
16753
16754 if (flag_enum)
16755 type->set_is_flag_enum (true);
16756 }
16757
16758 /* Given a DW_AT_enumeration_type die, set its type. We do not
16759 complete the type's fields yet, or create any symbols. */
16760
16761 static struct type *
16762 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16763 {
16764 struct objfile *objfile = cu->per_objfile->objfile;
16765 struct type *type;
16766 struct attribute *attr;
16767 const char *name;
16768
16769 /* If the definition of this type lives in .debug_types, read that type.
16770 Don't follow DW_AT_specification though, that will take us back up
16771 the chain and we want to go down. */
16772 attr = die->attr (DW_AT_signature);
16773 if (attr != nullptr)
16774 {
16775 type = get_DW_AT_signature_type (die, attr, cu);
16776
16777 /* The type's CU may not be the same as CU.
16778 Ensure TYPE is recorded with CU in die_type_hash. */
16779 return set_die_type (die, type, cu);
16780 }
16781
16782 type = alloc_type (objfile);
16783
16784 type->set_code (TYPE_CODE_ENUM);
16785 name = dwarf2_full_name (NULL, die, cu);
16786 if (name != NULL)
16787 type->set_name (name);
16788
16789 attr = dwarf2_attr (die, DW_AT_type, cu);
16790 if (attr != NULL)
16791 {
16792 struct type *underlying_type = die_type (die, cu);
16793
16794 TYPE_TARGET_TYPE (type) = underlying_type;
16795 }
16796
16797 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16798 if (attr != nullptr)
16799 {
16800 TYPE_LENGTH (type) = attr->constant_value (0);
16801 }
16802 else
16803 {
16804 TYPE_LENGTH (type) = 0;
16805 }
16806
16807 maybe_set_alignment (cu, die, type);
16808
16809 /* The enumeration DIE can be incomplete. In Ada, any type can be
16810 declared as private in the package spec, and then defined only
16811 inside the package body. Such types are known as Taft Amendment
16812 Types. When another package uses such a type, an incomplete DIE
16813 may be generated by the compiler. */
16814 if (die_is_declaration (die, cu))
16815 type->set_is_stub (true);
16816
16817 /* If this type has an underlying type that is not a stub, then we
16818 may use its attributes. We always use the "unsigned" attribute
16819 in this situation, because ordinarily we guess whether the type
16820 is unsigned -- but the guess can be wrong and the underlying type
16821 can tell us the reality. However, we defer to a local size
16822 attribute if one exists, because this lets the compiler override
16823 the underlying type if needed. */
16824 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16825 {
16826 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16827 underlying_type = check_typedef (underlying_type);
16828
16829 type->set_is_unsigned (underlying_type->is_unsigned ());
16830
16831 if (TYPE_LENGTH (type) == 0)
16832 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16833
16834 if (TYPE_RAW_ALIGN (type) == 0
16835 && TYPE_RAW_ALIGN (underlying_type) != 0)
16836 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16837 }
16838
16839 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16840
16841 set_die_type (die, type, cu);
16842
16843 /* Finish the creation of this type by using the enum's children.
16844 Note that, as usual, this must come after set_die_type to avoid
16845 infinite recursion when trying to compute the names of the
16846 enumerators. */
16847 update_enumeration_type_from_children (die, type, cu);
16848
16849 return type;
16850 }
16851
16852 /* Given a pointer to a die which begins an enumeration, process all
16853 the dies that define the members of the enumeration, and create the
16854 symbol for the enumeration type.
16855
16856 NOTE: We reverse the order of the element list. */
16857
16858 static void
16859 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16860 {
16861 struct type *this_type;
16862
16863 this_type = get_die_type (die, cu);
16864 if (this_type == NULL)
16865 this_type = read_enumeration_type (die, cu);
16866
16867 if (die->child != NULL)
16868 {
16869 struct die_info *child_die;
16870 const char *name;
16871
16872 child_die = die->child;
16873 while (child_die && child_die->tag)
16874 {
16875 if (child_die->tag != DW_TAG_enumerator)
16876 {
16877 process_die (child_die, cu);
16878 }
16879 else
16880 {
16881 name = dwarf2_name (child_die, cu);
16882 if (name)
16883 new_symbol (child_die, this_type, cu);
16884 }
16885
16886 child_die = child_die->sibling;
16887 }
16888 }
16889
16890 /* If we are reading an enum from a .debug_types unit, and the enum
16891 is a declaration, and the enum is not the signatured type in the
16892 unit, then we do not want to add a symbol for it. Adding a
16893 symbol would in some cases obscure the true definition of the
16894 enum, giving users an incomplete type when the definition is
16895 actually available. Note that we do not want to do this for all
16896 enums which are just declarations, because C++0x allows forward
16897 enum declarations. */
16898 if (cu->per_cu->is_debug_types
16899 && die_is_declaration (die, cu))
16900 {
16901 struct signatured_type *sig_type;
16902
16903 sig_type = (struct signatured_type *) cu->per_cu;
16904 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16905 if (sig_type->type_offset_in_section != die->sect_off)
16906 return;
16907 }
16908
16909 new_symbol (die, this_type, cu);
16910 }
16911
16912 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16913 expression for an index type and finds the corresponding field
16914 offset in the hidden "P_BOUNDS" structure. Returns true on success
16915 and updates *FIELD, false if it fails to recognize an
16916 expression. */
16917
16918 static bool
16919 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16920 int *bounds_offset, struct field *field,
16921 struct dwarf2_cu *cu)
16922 {
16923 struct attribute *attr = dwarf2_attr (die, name, cu);
16924 if (attr == nullptr || !attr->form_is_block ())
16925 return false;
16926
16927 const struct dwarf_block *block = attr->as_block ();
16928 const gdb_byte *start = block->data;
16929 const gdb_byte *end = block->data + block->size;
16930
16931 /* The expression to recognize generally looks like:
16932
16933 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16934 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16935
16936 However, the second "plus_uconst" may be missing:
16937
16938 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16939 DW_OP_deref_size: 4)
16940
16941 This happens when the field is at the start of the structure.
16942
16943 Also, the final deref may not be sized:
16944
16945 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16946 DW_OP_deref)
16947
16948 This happens when the size of the index type happens to be the
16949 same as the architecture's word size. This can occur with or
16950 without the second plus_uconst. */
16951
16952 if (end - start < 2)
16953 return false;
16954 if (*start++ != DW_OP_push_object_address)
16955 return false;
16956 if (*start++ != DW_OP_plus_uconst)
16957 return false;
16958
16959 uint64_t this_bound_off;
16960 start = gdb_read_uleb128 (start, end, &this_bound_off);
16961 if (start == nullptr || (int) this_bound_off != this_bound_off)
16962 return false;
16963 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16964 is consistent among all bounds. */
16965 if (*bounds_offset == -1)
16966 *bounds_offset = this_bound_off;
16967 else if (*bounds_offset != this_bound_off)
16968 return false;
16969
16970 if (start == end || *start++ != DW_OP_deref)
16971 return false;
16972
16973 int offset = 0;
16974 if (start ==end)
16975 return false;
16976 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16977 {
16978 /* This means an offset of 0. */
16979 }
16980 else if (*start++ != DW_OP_plus_uconst)
16981 return false;
16982 else
16983 {
16984 /* The size is the parameter to DW_OP_plus_uconst. */
16985 uint64_t val;
16986 start = gdb_read_uleb128 (start, end, &val);
16987 if (start == nullptr)
16988 return false;
16989 if ((int) val != val)
16990 return false;
16991 offset = val;
16992 }
16993
16994 if (start == end)
16995 return false;
16996
16997 uint64_t size;
16998 if (*start == DW_OP_deref_size)
16999 {
17000 start = gdb_read_uleb128 (start + 1, end, &size);
17001 if (start == nullptr)
17002 return false;
17003 }
17004 else if (*start == DW_OP_deref)
17005 {
17006 size = cu->header.addr_size;
17007 ++start;
17008 }
17009 else
17010 return false;
17011
17012 SET_FIELD_BITPOS (*field, 8 * offset);
17013 if (size != TYPE_LENGTH (field->type ()))
17014 FIELD_BITSIZE (*field) = 8 * size;
17015
17016 return true;
17017 }
17018
17019 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
17020 some kinds of Ada arrays:
17021
17022 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
17023 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
17024 <11e0> DW_AT_data_location: 2 byte block: 97 6
17025 (DW_OP_push_object_address; DW_OP_deref)
17026 <11e3> DW_AT_type : <0x1173>
17027 <11e7> DW_AT_sibling : <0x1201>
17028 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
17029 <11ec> DW_AT_type : <0x1206>
17030 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
17031 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17032 DW_OP_deref_size: 4)
17033 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
17034 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17035 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
17036
17037 This actually represents a "thick pointer", which is a structure
17038 with two elements: one that is a pointer to the array data, and one
17039 that is a pointer to another structure; this second structure holds
17040 the array bounds.
17041
17042 This returns a new type on success, or nullptr if this didn't
17043 recognize the type. */
17044
17045 static struct type *
17046 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
17047 struct type *type)
17048 {
17049 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
17050 /* So far we've only seen this with block form. */
17051 if (attr == nullptr || !attr->form_is_block ())
17052 return nullptr;
17053
17054 /* Note that this will fail if the structure layout is changed by
17055 the compiler. However, we have no good way to recognize some
17056 other layout, because we don't know what expression the compiler
17057 might choose to emit should this happen. */
17058 struct dwarf_block *blk = attr->as_block ();
17059 if (blk->size != 2
17060 || blk->data[0] != DW_OP_push_object_address
17061 || blk->data[1] != DW_OP_deref)
17062 return nullptr;
17063
17064 int bounds_offset = -1;
17065 int max_align = -1;
17066 std::vector<struct field> range_fields;
17067 for (struct die_info *child_die = die->child;
17068 child_die;
17069 child_die = child_die->sibling)
17070 {
17071 if (child_die->tag == DW_TAG_subrange_type)
17072 {
17073 struct type *underlying = read_subrange_index_type (child_die, cu);
17074
17075 int this_align = type_align (underlying);
17076 if (this_align > max_align)
17077 max_align = this_align;
17078
17079 range_fields.emplace_back ();
17080 range_fields.emplace_back ();
17081
17082 struct field &lower = range_fields[range_fields.size () - 2];
17083 struct field &upper = range_fields[range_fields.size () - 1];
17084
17085 lower.set_type (underlying);
17086 FIELD_ARTIFICIAL (lower) = 1;
17087
17088 upper.set_type (underlying);
17089 FIELD_ARTIFICIAL (upper) = 1;
17090
17091 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17092 &bounds_offset, &lower, cu)
17093 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17094 &bounds_offset, &upper, cu))
17095 return nullptr;
17096 }
17097 }
17098
17099 /* This shouldn't really happen, but double-check that we found
17100 where the bounds are stored. */
17101 if (bounds_offset == -1)
17102 return nullptr;
17103
17104 struct objfile *objfile = cu->per_objfile->objfile;
17105 for (int i = 0; i < range_fields.size (); i += 2)
17106 {
17107 char name[20];
17108
17109 /* Set the name of each field in the bounds. */
17110 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17111 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17112 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17113 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17114 }
17115
17116 struct type *bounds = alloc_type (objfile);
17117 bounds->set_code (TYPE_CODE_STRUCT);
17118
17119 bounds->set_num_fields (range_fields.size ());
17120 bounds->set_fields
17121 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17122 * sizeof (struct field))));
17123 memcpy (bounds->fields (), range_fields.data (),
17124 bounds->num_fields () * sizeof (struct field));
17125
17126 int last_fieldno = range_fields.size () - 1;
17127 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17128 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17129 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17130
17131 /* Rewrite the existing array type in place. Specifically, we
17132 remove any dynamic properties we might have read, and we replace
17133 the index types. */
17134 struct type *iter = type;
17135 for (int i = 0; i < range_fields.size (); i += 2)
17136 {
17137 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17138 iter->main_type->dyn_prop_list = nullptr;
17139 iter->set_index_type
17140 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17141 iter = TYPE_TARGET_TYPE (iter);
17142 }
17143
17144 struct type *result = alloc_type (objfile);
17145 result->set_code (TYPE_CODE_STRUCT);
17146
17147 result->set_num_fields (2);
17148 result->set_fields
17149 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17150 * sizeof (struct field))));
17151
17152 /* The names are chosen to coincide with what the compiler does with
17153 -fgnat-encodings=all, which the Ada code in gdb already
17154 understands. */
17155 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17156 result->field (0).set_type (lookup_pointer_type (type));
17157
17158 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17159 result->field (1).set_type (lookup_pointer_type (bounds));
17160 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17161
17162 result->set_name (type->name ());
17163 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17164 + TYPE_LENGTH (result->field (1).type ()));
17165
17166 return result;
17167 }
17168
17169 /* Extract all information from a DW_TAG_array_type DIE and put it in
17170 the DIE's type field. For now, this only handles one dimensional
17171 arrays. */
17172
17173 static struct type *
17174 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17175 {
17176 struct objfile *objfile = cu->per_objfile->objfile;
17177 struct die_info *child_die;
17178 struct type *type;
17179 struct type *element_type, *range_type, *index_type;
17180 struct attribute *attr;
17181 const char *name;
17182 struct dynamic_prop *byte_stride_prop = NULL;
17183 unsigned int bit_stride = 0;
17184
17185 element_type = die_type (die, cu);
17186
17187 /* The die_type call above may have already set the type for this DIE. */
17188 type = get_die_type (die, cu);
17189 if (type)
17190 return type;
17191
17192 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17193 if (attr != NULL)
17194 {
17195 int stride_ok;
17196 struct type *prop_type = cu->addr_sized_int_type (false);
17197
17198 byte_stride_prop
17199 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17200 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17201 prop_type);
17202 if (!stride_ok)
17203 {
17204 complaint (_("unable to read array DW_AT_byte_stride "
17205 " - DIE at %s [in module %s]"),
17206 sect_offset_str (die->sect_off),
17207 objfile_name (cu->per_objfile->objfile));
17208 /* Ignore this attribute. We will likely not be able to print
17209 arrays of this type correctly, but there is little we can do
17210 to help if we cannot read the attribute's value. */
17211 byte_stride_prop = NULL;
17212 }
17213 }
17214
17215 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17216 if (attr != NULL)
17217 bit_stride = attr->constant_value (0);
17218
17219 /* Irix 6.2 native cc creates array types without children for
17220 arrays with unspecified length. */
17221 if (die->child == NULL)
17222 {
17223 index_type = objfile_type (objfile)->builtin_int;
17224 range_type = create_static_range_type (NULL, index_type, 0, -1);
17225 type = create_array_type_with_stride (NULL, element_type, range_type,
17226 byte_stride_prop, bit_stride);
17227 return set_die_type (die, type, cu);
17228 }
17229
17230 std::vector<struct type *> range_types;
17231 child_die = die->child;
17232 while (child_die && child_die->tag)
17233 {
17234 if (child_die->tag == DW_TAG_subrange_type)
17235 {
17236 struct type *child_type = read_type_die (child_die, cu);
17237
17238 if (child_type != NULL)
17239 {
17240 /* The range type was succesfully read. Save it for the
17241 array type creation. */
17242 range_types.push_back (child_type);
17243 }
17244 }
17245 child_die = child_die->sibling;
17246 }
17247
17248 if (range_types.empty ())
17249 {
17250 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17251 sect_offset_str (die->sect_off),
17252 objfile_name (cu->per_objfile->objfile));
17253 return NULL;
17254 }
17255
17256 /* Dwarf2 dimensions are output from left to right, create the
17257 necessary array types in backwards order. */
17258
17259 type = element_type;
17260
17261 if (read_array_order (die, cu) == DW_ORD_col_major)
17262 {
17263 int i = 0;
17264
17265 while (i < range_types.size ())
17266 {
17267 type = create_array_type_with_stride (NULL, type, range_types[i++],
17268 byte_stride_prop, bit_stride);
17269 bit_stride = 0;
17270 byte_stride_prop = nullptr;
17271 }
17272 }
17273 else
17274 {
17275 size_t ndim = range_types.size ();
17276 while (ndim-- > 0)
17277 {
17278 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17279 byte_stride_prop, bit_stride);
17280 bit_stride = 0;
17281 byte_stride_prop = nullptr;
17282 }
17283 }
17284
17285 gdb_assert (type != element_type);
17286
17287 /* Understand Dwarf2 support for vector types (like they occur on
17288 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17289 array type. This is not part of the Dwarf2/3 standard yet, but a
17290 custom vendor extension. The main difference between a regular
17291 array and the vector variant is that vectors are passed by value
17292 to functions. */
17293 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17294 if (attr != nullptr)
17295 make_vector_type (type);
17296
17297 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17298 implementation may choose to implement triple vectors using this
17299 attribute. */
17300 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17301 if (attr != nullptr && attr->form_is_unsigned ())
17302 {
17303 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17304 TYPE_LENGTH (type) = attr->as_unsigned ();
17305 else
17306 complaint (_("DW_AT_byte_size for array type smaller "
17307 "than the total size of elements"));
17308 }
17309
17310 name = dwarf2_name (die, cu);
17311 if (name)
17312 type->set_name (name);
17313
17314 maybe_set_alignment (cu, die, type);
17315
17316 struct type *replacement_type = nullptr;
17317 if (cu->language == language_ada)
17318 {
17319 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17320 if (replacement_type != nullptr)
17321 type = replacement_type;
17322 }
17323
17324 /* Install the type in the die. */
17325 set_die_type (die, type, cu, replacement_type != nullptr);
17326
17327 /* set_die_type should be already done. */
17328 set_descriptive_type (type, die, cu);
17329
17330 return type;
17331 }
17332
17333 static enum dwarf_array_dim_ordering
17334 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17335 {
17336 struct attribute *attr;
17337
17338 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17339
17340 if (attr != nullptr)
17341 {
17342 LONGEST val = attr->constant_value (-1);
17343 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17344 return (enum dwarf_array_dim_ordering) val;
17345 }
17346
17347 /* GNU F77 is a special case, as at 08/2004 array type info is the
17348 opposite order to the dwarf2 specification, but data is still
17349 laid out as per normal fortran.
17350
17351 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17352 version checking. */
17353
17354 if (cu->language == language_fortran
17355 && cu->producer && strstr (cu->producer, "GNU F77"))
17356 {
17357 return DW_ORD_row_major;
17358 }
17359
17360 switch (cu->language_defn->array_ordering ())
17361 {
17362 case array_column_major:
17363 return DW_ORD_col_major;
17364 case array_row_major:
17365 default:
17366 return DW_ORD_row_major;
17367 };
17368 }
17369
17370 /* Extract all information from a DW_TAG_set_type DIE and put it in
17371 the DIE's type field. */
17372
17373 static struct type *
17374 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17375 {
17376 struct type *domain_type, *set_type;
17377 struct attribute *attr;
17378
17379 domain_type = die_type (die, cu);
17380
17381 /* The die_type call above may have already set the type for this DIE. */
17382 set_type = get_die_type (die, cu);
17383 if (set_type)
17384 return set_type;
17385
17386 set_type = create_set_type (NULL, domain_type);
17387
17388 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17389 if (attr != nullptr && attr->form_is_unsigned ())
17390 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17391
17392 maybe_set_alignment (cu, die, set_type);
17393
17394 return set_die_type (die, set_type, cu);
17395 }
17396
17397 /* A helper for read_common_block that creates a locexpr baton.
17398 SYM is the symbol which we are marking as computed.
17399 COMMON_DIE is the DIE for the common block.
17400 COMMON_LOC is the location expression attribute for the common
17401 block itself.
17402 MEMBER_LOC is the location expression attribute for the particular
17403 member of the common block that we are processing.
17404 CU is the CU from which the above come. */
17405
17406 static void
17407 mark_common_block_symbol_computed (struct symbol *sym,
17408 struct die_info *common_die,
17409 struct attribute *common_loc,
17410 struct attribute *member_loc,
17411 struct dwarf2_cu *cu)
17412 {
17413 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17414 struct objfile *objfile = per_objfile->objfile;
17415 struct dwarf2_locexpr_baton *baton;
17416 gdb_byte *ptr;
17417 unsigned int cu_off;
17418 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17419 LONGEST offset = 0;
17420
17421 gdb_assert (common_loc && member_loc);
17422 gdb_assert (common_loc->form_is_block ());
17423 gdb_assert (member_loc->form_is_block ()
17424 || member_loc->form_is_constant ());
17425
17426 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17427 baton->per_objfile = per_objfile;
17428 baton->per_cu = cu->per_cu;
17429 gdb_assert (baton->per_cu);
17430
17431 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17432
17433 if (member_loc->form_is_constant ())
17434 {
17435 offset = member_loc->constant_value (0);
17436 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17437 }
17438 else
17439 baton->size += member_loc->as_block ()->size;
17440
17441 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17442 baton->data = ptr;
17443
17444 *ptr++ = DW_OP_call4;
17445 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17446 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17447 ptr += 4;
17448
17449 if (member_loc->form_is_constant ())
17450 {
17451 *ptr++ = DW_OP_addr;
17452 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17453 ptr += cu->header.addr_size;
17454 }
17455 else
17456 {
17457 /* We have to copy the data here, because DW_OP_call4 will only
17458 use a DW_AT_location attribute. */
17459 struct dwarf_block *block = member_loc->as_block ();
17460 memcpy (ptr, block->data, block->size);
17461 ptr += block->size;
17462 }
17463
17464 *ptr++ = DW_OP_plus;
17465 gdb_assert (ptr - baton->data == baton->size);
17466
17467 SYMBOL_LOCATION_BATON (sym) = baton;
17468 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17469 }
17470
17471 /* Create appropriate locally-scoped variables for all the
17472 DW_TAG_common_block entries. Also create a struct common_block
17473 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17474 is used to separate the common blocks name namespace from regular
17475 variable names. */
17476
17477 static void
17478 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17479 {
17480 struct attribute *attr;
17481
17482 attr = dwarf2_attr (die, DW_AT_location, cu);
17483 if (attr != nullptr)
17484 {
17485 /* Support the .debug_loc offsets. */
17486 if (attr->form_is_block ())
17487 {
17488 /* Ok. */
17489 }
17490 else if (attr->form_is_section_offset ())
17491 {
17492 dwarf2_complex_location_expr_complaint ();
17493 attr = NULL;
17494 }
17495 else
17496 {
17497 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17498 "common block member");
17499 attr = NULL;
17500 }
17501 }
17502
17503 if (die->child != NULL)
17504 {
17505 struct objfile *objfile = cu->per_objfile->objfile;
17506 struct die_info *child_die;
17507 size_t n_entries = 0, size;
17508 struct common_block *common_block;
17509 struct symbol *sym;
17510
17511 for (child_die = die->child;
17512 child_die && child_die->tag;
17513 child_die = child_die->sibling)
17514 ++n_entries;
17515
17516 size = (sizeof (struct common_block)
17517 + (n_entries - 1) * sizeof (struct symbol *));
17518 common_block
17519 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17520 size);
17521 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17522 common_block->n_entries = 0;
17523
17524 for (child_die = die->child;
17525 child_die && child_die->tag;
17526 child_die = child_die->sibling)
17527 {
17528 /* Create the symbol in the DW_TAG_common_block block in the current
17529 symbol scope. */
17530 sym = new_symbol (child_die, NULL, cu);
17531 if (sym != NULL)
17532 {
17533 struct attribute *member_loc;
17534
17535 common_block->contents[common_block->n_entries++] = sym;
17536
17537 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17538 cu);
17539 if (member_loc)
17540 {
17541 /* GDB has handled this for a long time, but it is
17542 not specified by DWARF. It seems to have been
17543 emitted by gfortran at least as recently as:
17544 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17545 complaint (_("Variable in common block has "
17546 "DW_AT_data_member_location "
17547 "- DIE at %s [in module %s]"),
17548 sect_offset_str (child_die->sect_off),
17549 objfile_name (objfile));
17550
17551 if (member_loc->form_is_section_offset ())
17552 dwarf2_complex_location_expr_complaint ();
17553 else if (member_loc->form_is_constant ()
17554 || member_loc->form_is_block ())
17555 {
17556 if (attr != nullptr)
17557 mark_common_block_symbol_computed (sym, die, attr,
17558 member_loc, cu);
17559 }
17560 else
17561 dwarf2_complex_location_expr_complaint ();
17562 }
17563 }
17564 }
17565
17566 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17567 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17568 }
17569 }
17570
17571 /* Create a type for a C++ namespace. */
17572
17573 static struct type *
17574 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17575 {
17576 struct objfile *objfile = cu->per_objfile->objfile;
17577 const char *previous_prefix, *name;
17578 int is_anonymous;
17579 struct type *type;
17580
17581 /* For extensions, reuse the type of the original namespace. */
17582 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17583 {
17584 struct die_info *ext_die;
17585 struct dwarf2_cu *ext_cu = cu;
17586
17587 ext_die = dwarf2_extension (die, &ext_cu);
17588 type = read_type_die (ext_die, ext_cu);
17589
17590 /* EXT_CU may not be the same as CU.
17591 Ensure TYPE is recorded with CU in die_type_hash. */
17592 return set_die_type (die, type, cu);
17593 }
17594
17595 name = namespace_name (die, &is_anonymous, cu);
17596
17597 /* Now build the name of the current namespace. */
17598
17599 previous_prefix = determine_prefix (die, cu);
17600 if (previous_prefix[0] != '\0')
17601 name = typename_concat (&objfile->objfile_obstack,
17602 previous_prefix, name, 0, cu);
17603
17604 /* Create the type. */
17605 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17606
17607 return set_die_type (die, type, cu);
17608 }
17609
17610 /* Read a namespace scope. */
17611
17612 static void
17613 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17614 {
17615 struct objfile *objfile = cu->per_objfile->objfile;
17616 int is_anonymous;
17617
17618 /* Add a symbol associated to this if we haven't seen the namespace
17619 before. Also, add a using directive if it's an anonymous
17620 namespace. */
17621
17622 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17623 {
17624 struct type *type;
17625
17626 type = read_type_die (die, cu);
17627 new_symbol (die, type, cu);
17628
17629 namespace_name (die, &is_anonymous, cu);
17630 if (is_anonymous)
17631 {
17632 const char *previous_prefix = determine_prefix (die, cu);
17633
17634 std::vector<const char *> excludes;
17635 add_using_directive (using_directives (cu),
17636 previous_prefix, type->name (), NULL,
17637 NULL, excludes, 0, &objfile->objfile_obstack);
17638 }
17639 }
17640
17641 if (die->child != NULL)
17642 {
17643 struct die_info *child_die = die->child;
17644
17645 while (child_die && child_die->tag)
17646 {
17647 process_die (child_die, cu);
17648 child_die = child_die->sibling;
17649 }
17650 }
17651 }
17652
17653 /* Read a Fortran module as type. This DIE can be only a declaration used for
17654 imported module. Still we need that type as local Fortran "use ... only"
17655 declaration imports depend on the created type in determine_prefix. */
17656
17657 static struct type *
17658 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17659 {
17660 struct objfile *objfile = cu->per_objfile->objfile;
17661 const char *module_name;
17662 struct type *type;
17663
17664 module_name = dwarf2_name (die, cu);
17665 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17666
17667 return set_die_type (die, type, cu);
17668 }
17669
17670 /* Read a Fortran module. */
17671
17672 static void
17673 read_module (struct die_info *die, struct dwarf2_cu *cu)
17674 {
17675 struct die_info *child_die = die->child;
17676 struct type *type;
17677
17678 type = read_type_die (die, cu);
17679 new_symbol (die, type, cu);
17680
17681 while (child_die && child_die->tag)
17682 {
17683 process_die (child_die, cu);
17684 child_die = child_die->sibling;
17685 }
17686 }
17687
17688 /* Return the name of the namespace represented by DIE. Set
17689 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17690 namespace. */
17691
17692 static const char *
17693 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17694 {
17695 struct die_info *current_die;
17696 const char *name = NULL;
17697
17698 /* Loop through the extensions until we find a name. */
17699
17700 for (current_die = die;
17701 current_die != NULL;
17702 current_die = dwarf2_extension (die, &cu))
17703 {
17704 /* We don't use dwarf2_name here so that we can detect the absence
17705 of a name -> anonymous namespace. */
17706 name = dwarf2_string_attr (die, DW_AT_name, cu);
17707
17708 if (name != NULL)
17709 break;
17710 }
17711
17712 /* Is it an anonymous namespace? */
17713
17714 *is_anonymous = (name == NULL);
17715 if (*is_anonymous)
17716 name = CP_ANONYMOUS_NAMESPACE_STR;
17717
17718 return name;
17719 }
17720
17721 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17722 the user defined type vector. */
17723
17724 static struct type *
17725 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17726 {
17727 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17728 struct comp_unit_head *cu_header = &cu->header;
17729 struct type *type;
17730 struct attribute *attr_byte_size;
17731 struct attribute *attr_address_class;
17732 int byte_size, addr_class;
17733 struct type *target_type;
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_pointer_type (target_type);
17743
17744 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17745 if (attr_byte_size)
17746 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17747 else
17748 byte_size = cu_header->addr_size;
17749
17750 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17751 if (attr_address_class)
17752 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17753 else
17754 addr_class = DW_ADDR_none;
17755
17756 ULONGEST alignment = get_alignment (cu, die);
17757
17758 /* If the pointer size, alignment, or address class is different
17759 than the default, create a type variant marked as such and set
17760 the length accordingly. */
17761 if (TYPE_LENGTH (type) != byte_size
17762 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17763 && alignment != TYPE_RAW_ALIGN (type))
17764 || addr_class != DW_ADDR_none)
17765 {
17766 if (gdbarch_address_class_type_flags_p (gdbarch))
17767 {
17768 type_instance_flags type_flags
17769 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17770 addr_class);
17771 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17772 == 0);
17773 type = make_type_with_address_space (type, type_flags);
17774 }
17775 else if (TYPE_LENGTH (type) != byte_size)
17776 {
17777 complaint (_("invalid pointer size %d"), byte_size);
17778 }
17779 else if (TYPE_RAW_ALIGN (type) != alignment)
17780 {
17781 complaint (_("Invalid DW_AT_alignment"
17782 " - DIE at %s [in module %s]"),
17783 sect_offset_str (die->sect_off),
17784 objfile_name (cu->per_objfile->objfile));
17785 }
17786 else
17787 {
17788 /* Should we also complain about unhandled address classes? */
17789 }
17790 }
17791
17792 TYPE_LENGTH (type) = byte_size;
17793 set_type_align (type, alignment);
17794 return set_die_type (die, type, cu);
17795 }
17796
17797 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17798 the user defined type vector. */
17799
17800 static struct type *
17801 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17802 {
17803 struct type *type;
17804 struct type *to_type;
17805 struct type *domain;
17806
17807 to_type = die_type (die, cu);
17808 domain = die_containing_type (die, cu);
17809
17810 /* The calls above may have already set the type for this DIE. */
17811 type = get_die_type (die, cu);
17812 if (type)
17813 return type;
17814
17815 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17816 type = lookup_methodptr_type (to_type);
17817 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17818 {
17819 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17820
17821 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17822 to_type->fields (), to_type->num_fields (),
17823 to_type->has_varargs ());
17824 type = lookup_methodptr_type (new_type);
17825 }
17826 else
17827 type = lookup_memberptr_type (to_type, domain);
17828
17829 return set_die_type (die, type, cu);
17830 }
17831
17832 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17833 the user defined type vector. */
17834
17835 static struct type *
17836 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17837 enum type_code refcode)
17838 {
17839 struct comp_unit_head *cu_header = &cu->header;
17840 struct type *type, *target_type;
17841 struct attribute *attr;
17842
17843 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17844
17845 target_type = die_type (die, cu);
17846
17847 /* The die_type call above may have already set the type for this DIE. */
17848 type = get_die_type (die, cu);
17849 if (type)
17850 return type;
17851
17852 type = lookup_reference_type (target_type, refcode);
17853 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17854 if (attr != nullptr)
17855 {
17856 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17857 }
17858 else
17859 {
17860 TYPE_LENGTH (type) = cu_header->addr_size;
17861 }
17862 maybe_set_alignment (cu, die, type);
17863 return set_die_type (die, type, cu);
17864 }
17865
17866 /* Add the given cv-qualifiers to the element type of the array. GCC
17867 outputs DWARF type qualifiers that apply to an array, not the
17868 element type. But GDB relies on the array element type to carry
17869 the cv-qualifiers. This mimics section 6.7.3 of the C99
17870 specification. */
17871
17872 static struct type *
17873 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17874 struct type *base_type, int cnst, int voltl)
17875 {
17876 struct type *el_type, *inner_array;
17877
17878 base_type = copy_type (base_type);
17879 inner_array = base_type;
17880
17881 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17882 {
17883 TYPE_TARGET_TYPE (inner_array) =
17884 copy_type (TYPE_TARGET_TYPE (inner_array));
17885 inner_array = TYPE_TARGET_TYPE (inner_array);
17886 }
17887
17888 el_type = TYPE_TARGET_TYPE (inner_array);
17889 cnst |= TYPE_CONST (el_type);
17890 voltl |= TYPE_VOLATILE (el_type);
17891 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17892
17893 return set_die_type (die, base_type, cu);
17894 }
17895
17896 static struct type *
17897 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17898 {
17899 struct type *base_type, *cv_type;
17900
17901 base_type = die_type (die, cu);
17902
17903 /* The die_type call above may have already set the type for this DIE. */
17904 cv_type = get_die_type (die, cu);
17905 if (cv_type)
17906 return cv_type;
17907
17908 /* In case the const qualifier is applied to an array type, the element type
17909 is so qualified, not the array type (section 6.7.3 of C99). */
17910 if (base_type->code () == TYPE_CODE_ARRAY)
17911 return add_array_cv_type (die, cu, base_type, 1, 0);
17912
17913 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17914 return set_die_type (die, cv_type, cu);
17915 }
17916
17917 static struct type *
17918 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17919 {
17920 struct type *base_type, *cv_type;
17921
17922 base_type = die_type (die, cu);
17923
17924 /* The die_type call above may have already set the type for this DIE. */
17925 cv_type = get_die_type (die, cu);
17926 if (cv_type)
17927 return cv_type;
17928
17929 /* In case the volatile qualifier is applied to an array type, the
17930 element type is so qualified, not the array type (section 6.7.3
17931 of C99). */
17932 if (base_type->code () == TYPE_CODE_ARRAY)
17933 return add_array_cv_type (die, cu, base_type, 0, 1);
17934
17935 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17936 return set_die_type (die, cv_type, cu);
17937 }
17938
17939 /* Handle DW_TAG_restrict_type. */
17940
17941 static struct type *
17942 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17943 {
17944 struct type *base_type, *cv_type;
17945
17946 base_type = die_type (die, cu);
17947
17948 /* The die_type call above may have already set the type for this DIE. */
17949 cv_type = get_die_type (die, cu);
17950 if (cv_type)
17951 return cv_type;
17952
17953 cv_type = make_restrict_type (base_type);
17954 return set_die_type (die, cv_type, cu);
17955 }
17956
17957 /* Handle DW_TAG_atomic_type. */
17958
17959 static struct type *
17960 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17961 {
17962 struct type *base_type, *cv_type;
17963
17964 base_type = die_type (die, cu);
17965
17966 /* The die_type call above may have already set the type for this DIE. */
17967 cv_type = get_die_type (die, cu);
17968 if (cv_type)
17969 return cv_type;
17970
17971 cv_type = make_atomic_type (base_type);
17972 return set_die_type (die, cv_type, cu);
17973 }
17974
17975 /* Extract all information from a DW_TAG_string_type DIE and add to
17976 the user defined type vector. It isn't really a user defined type,
17977 but it behaves like one, with other DIE's using an AT_user_def_type
17978 attribute to reference it. */
17979
17980 static struct type *
17981 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17982 {
17983 struct objfile *objfile = cu->per_objfile->objfile;
17984 struct gdbarch *gdbarch = objfile->arch ();
17985 struct type *type, *range_type, *index_type, *char_type;
17986 struct attribute *attr;
17987 struct dynamic_prop prop;
17988 bool length_is_constant = true;
17989 LONGEST length;
17990
17991 /* There are a couple of places where bit sizes might be made use of
17992 when parsing a DW_TAG_string_type, however, no producer that we know
17993 of make use of these. Handling bit sizes that are a multiple of the
17994 byte size is easy enough, but what about other bit sizes? Lets deal
17995 with that problem when we have to. Warn about these attributes being
17996 unsupported, then parse the type and ignore them like we always
17997 have. */
17998 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17999 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
18000 {
18001 static bool warning_printed = false;
18002 if (!warning_printed)
18003 {
18004 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
18005 "currently supported on DW_TAG_string_type."));
18006 warning_printed = true;
18007 }
18008 }
18009
18010 attr = dwarf2_attr (die, DW_AT_string_length, cu);
18011 if (attr != nullptr && !attr->form_is_constant ())
18012 {
18013 /* The string length describes the location at which the length of
18014 the string can be found. The size of the length field can be
18015 specified with one of the attributes below. */
18016 struct type *prop_type;
18017 struct attribute *len
18018 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
18019 if (len == nullptr)
18020 len = dwarf2_attr (die, DW_AT_byte_size, cu);
18021 if (len != nullptr && len->form_is_constant ())
18022 {
18023 /* Pass 0 as the default as we know this attribute is constant
18024 and the default value will not be returned. */
18025 LONGEST sz = len->constant_value (0);
18026 prop_type = cu->per_objfile->int_type (sz, true);
18027 }
18028 else
18029 {
18030 /* If the size is not specified then we assume it is the size of
18031 an address on this target. */
18032 prop_type = cu->addr_sized_int_type (true);
18033 }
18034
18035 /* Convert the attribute into a dynamic property. */
18036 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
18037 length = 1;
18038 else
18039 length_is_constant = false;
18040 }
18041 else if (attr != nullptr)
18042 {
18043 /* This DW_AT_string_length just contains the length with no
18044 indirection. There's no need to create a dynamic property in this
18045 case. Pass 0 for the default value as we know it will not be
18046 returned in this case. */
18047 length = attr->constant_value (0);
18048 }
18049 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
18050 {
18051 /* We don't currently support non-constant byte sizes for strings. */
18052 length = attr->constant_value (1);
18053 }
18054 else
18055 {
18056 /* Use 1 as a fallback length if we have nothing else. */
18057 length = 1;
18058 }
18059
18060 index_type = objfile_type (objfile)->builtin_int;
18061 if (length_is_constant)
18062 range_type = create_static_range_type (NULL, index_type, 1, length);
18063 else
18064 {
18065 struct dynamic_prop low_bound;
18066
18067 low_bound.set_const_val (1);
18068 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
18069 }
18070 char_type = language_string_char_type (cu->language_defn, gdbarch);
18071 type = create_string_type (NULL, char_type, range_type);
18072
18073 return set_die_type (die, type, cu);
18074 }
18075
18076 /* Assuming that DIE corresponds to a function, returns nonzero
18077 if the function is prototyped. */
18078
18079 static int
18080 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18081 {
18082 struct attribute *attr;
18083
18084 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
18085 if (attr && attr->as_boolean ())
18086 return 1;
18087
18088 /* The DWARF standard implies that the DW_AT_prototyped attribute
18089 is only meaningful for C, but the concept also extends to other
18090 languages that allow unprototyped functions (Eg: Objective C).
18091 For all other languages, assume that functions are always
18092 prototyped. */
18093 if (cu->language != language_c
18094 && cu->language != language_objc
18095 && cu->language != language_opencl)
18096 return 1;
18097
18098 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18099 prototyped and unprototyped functions; default to prototyped,
18100 since that is more common in modern code (and RealView warns
18101 about unprototyped functions). */
18102 if (producer_is_realview (cu->producer))
18103 return 1;
18104
18105 return 0;
18106 }
18107
18108 /* Handle DIES due to C code like:
18109
18110 struct foo
18111 {
18112 int (*funcp)(int a, long l);
18113 int b;
18114 };
18115
18116 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18117
18118 static struct type *
18119 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18120 {
18121 struct objfile *objfile = cu->per_objfile->objfile;
18122 struct type *type; /* Type that this function returns. */
18123 struct type *ftype; /* Function that returns above type. */
18124 struct attribute *attr;
18125
18126 type = die_type (die, cu);
18127
18128 /* The die_type call above may have already set the type for this DIE. */
18129 ftype = get_die_type (die, cu);
18130 if (ftype)
18131 return ftype;
18132
18133 ftype = lookup_function_type (type);
18134
18135 if (prototyped_function_p (die, cu))
18136 ftype->set_is_prototyped (true);
18137
18138 /* Store the calling convention in the type if it's available in
18139 the subroutine die. Otherwise set the calling convention to
18140 the default value DW_CC_normal. */
18141 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18142 if (attr != nullptr
18143 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18144 TYPE_CALLING_CONVENTION (ftype)
18145 = (enum dwarf_calling_convention) attr->constant_value (0);
18146 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18147 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18148 else
18149 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18150
18151 /* Record whether the function returns normally to its caller or not
18152 if the DWARF producer set that information. */
18153 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18154 if (attr && attr->as_boolean ())
18155 TYPE_NO_RETURN (ftype) = 1;
18156
18157 /* We need to add the subroutine type to the die immediately so
18158 we don't infinitely recurse when dealing with parameters
18159 declared as the same subroutine type. */
18160 set_die_type (die, ftype, cu);
18161
18162 if (die->child != NULL)
18163 {
18164 struct type *void_type = objfile_type (objfile)->builtin_void;
18165 struct die_info *child_die;
18166 int nparams, iparams;
18167
18168 /* Count the number of parameters.
18169 FIXME: GDB currently ignores vararg functions, but knows about
18170 vararg member functions. */
18171 nparams = 0;
18172 child_die = die->child;
18173 while (child_die && child_die->tag)
18174 {
18175 if (child_die->tag == DW_TAG_formal_parameter)
18176 nparams++;
18177 else if (child_die->tag == DW_TAG_unspecified_parameters)
18178 ftype->set_has_varargs (true);
18179
18180 child_die = child_die->sibling;
18181 }
18182
18183 /* Allocate storage for parameters and fill them in. */
18184 ftype->set_num_fields (nparams);
18185 ftype->set_fields
18186 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18187
18188 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18189 even if we error out during the parameters reading below. */
18190 for (iparams = 0; iparams < nparams; iparams++)
18191 ftype->field (iparams).set_type (void_type);
18192
18193 iparams = 0;
18194 child_die = die->child;
18195 while (child_die && child_die->tag)
18196 {
18197 if (child_die->tag == DW_TAG_formal_parameter)
18198 {
18199 struct type *arg_type;
18200
18201 /* DWARF version 2 has no clean way to discern C++
18202 static and non-static member functions. G++ helps
18203 GDB by marking the first parameter for non-static
18204 member functions (which is the this pointer) as
18205 artificial. We pass this information to
18206 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18207
18208 DWARF version 3 added DW_AT_object_pointer, which GCC
18209 4.5 does not yet generate. */
18210 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18211 if (attr != nullptr)
18212 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18213 else
18214 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18215 arg_type = die_type (child_die, cu);
18216
18217 /* RealView does not mark THIS as const, which the testsuite
18218 expects. GCC marks THIS as const in method definitions,
18219 but not in the class specifications (GCC PR 43053). */
18220 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18221 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18222 {
18223 int is_this = 0;
18224 struct dwarf2_cu *arg_cu = cu;
18225 const char *name = dwarf2_name (child_die, cu);
18226
18227 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18228 if (attr != nullptr)
18229 {
18230 /* If the compiler emits this, use it. */
18231 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18232 is_this = 1;
18233 }
18234 else if (name && strcmp (name, "this") == 0)
18235 /* Function definitions will have the argument names. */
18236 is_this = 1;
18237 else if (name == NULL && iparams == 0)
18238 /* Declarations may not have the names, so like
18239 elsewhere in GDB, assume an artificial first
18240 argument is "this". */
18241 is_this = 1;
18242
18243 if (is_this)
18244 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18245 arg_type, 0);
18246 }
18247
18248 ftype->field (iparams).set_type (arg_type);
18249 iparams++;
18250 }
18251 child_die = child_die->sibling;
18252 }
18253 }
18254
18255 return ftype;
18256 }
18257
18258 static struct type *
18259 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18260 {
18261 struct objfile *objfile = cu->per_objfile->objfile;
18262 const char *name = NULL;
18263 struct type *this_type, *target_type;
18264
18265 name = dwarf2_full_name (NULL, die, cu);
18266 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18267 this_type->set_target_is_stub (true);
18268 set_die_type (die, this_type, cu);
18269 target_type = die_type (die, cu);
18270 if (target_type != this_type)
18271 TYPE_TARGET_TYPE (this_type) = target_type;
18272 else
18273 {
18274 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18275 spec and cause infinite loops in GDB. */
18276 complaint (_("Self-referential DW_TAG_typedef "
18277 "- DIE at %s [in module %s]"),
18278 sect_offset_str (die->sect_off), objfile_name (objfile));
18279 TYPE_TARGET_TYPE (this_type) = NULL;
18280 }
18281 if (name == NULL)
18282 {
18283 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18284 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18285 Handle these by just returning the target type, rather than
18286 constructing an anonymous typedef type and trying to handle this
18287 elsewhere. */
18288 set_die_type (die, target_type, cu);
18289 return target_type;
18290 }
18291 return this_type;
18292 }
18293
18294 /* Helper for get_dwarf2_rational_constant that computes the value of
18295 a given gmp_mpz given an attribute. */
18296
18297 static void
18298 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18299 {
18300 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18301 location expression that pushes an implicit value. */
18302 if (attr->form == DW_FORM_exprloc)
18303 {
18304 dwarf_block *blk = attr->as_block ();
18305 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18306 {
18307 uint64_t len;
18308 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18309 blk->data + blk->size,
18310 &len);
18311 if (ptr - blk->data + len <= blk->size)
18312 {
18313 mpz_import (value->val, len,
18314 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18315 1, 0, 0, ptr);
18316 return;
18317 }
18318 }
18319
18320 /* On failure set it to 1. */
18321 *value = gdb_mpz (1);
18322 }
18323 else if (attr->form_is_block ())
18324 {
18325 dwarf_block *blk = attr->as_block ();
18326 mpz_import (value->val, blk->size,
18327 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18328 1, 0, 0, blk->data);
18329 }
18330 else
18331 *value = gdb_mpz (attr->constant_value (1));
18332 }
18333
18334 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18335 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18336
18337 If the numerator and/or numerator attribute is missing,
18338 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18339 untouched. */
18340
18341 static void
18342 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18343 gdb_mpz *numerator, gdb_mpz *denominator)
18344 {
18345 struct attribute *num_attr, *denom_attr;
18346
18347 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18348 if (num_attr == nullptr)
18349 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18350 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18351
18352 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18353 if (denom_attr == nullptr)
18354 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18355 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18356
18357 if (num_attr == nullptr || denom_attr == nullptr)
18358 return;
18359
18360 get_mpz (cu, numerator, num_attr);
18361 get_mpz (cu, denominator, denom_attr);
18362 }
18363
18364 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18365 rational constant, rather than a signed one.
18366
18367 If the rational constant has a negative value, a complaint
18368 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18369
18370 static void
18371 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18372 struct dwarf2_cu *cu,
18373 gdb_mpz *numerator,
18374 gdb_mpz *denominator)
18375 {
18376 gdb_mpz num (1);
18377 gdb_mpz denom (1);
18378
18379 get_dwarf2_rational_constant (die, cu, &num, &denom);
18380 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18381 {
18382 mpz_neg (num.val, num.val);
18383 mpz_neg (denom.val, denom.val);
18384 }
18385 else if (mpz_sgn (num.val) == -1)
18386 {
18387 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18388 " in DIE at %s"),
18389 sect_offset_str (die->sect_off));
18390 return;
18391 }
18392 else if (mpz_sgn (denom.val) == -1)
18393 {
18394 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18395 " in DIE at %s"),
18396 sect_offset_str (die->sect_off));
18397 return;
18398 }
18399
18400 *numerator = std::move (num);
18401 *denominator = std::move (denom);
18402 }
18403
18404 /* Assuming that ENCODING is a string whose contents starting at the
18405 K'th character is "_nn" where "nn" is a decimal number, scan that
18406 number and set RESULT to the value. K is updated to point to the
18407 character immediately following the number.
18408
18409 If the string does not conform to the format described above, false
18410 is returned, and K may or may not be changed. */
18411
18412 static bool
18413 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18414 {
18415 /* The next character should be an underscore ('_') followed
18416 by a digit. */
18417 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18418 return false;
18419
18420 /* Skip the underscore. */
18421 k++;
18422 int start = k;
18423
18424 /* Determine the number of digits for our number. */
18425 while (isdigit (encoding[k]))
18426 k++;
18427 if (k == start)
18428 return false;
18429
18430 std::string copy (&encoding[start], k - start);
18431 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18432 return false;
18433
18434 return true;
18435 }
18436
18437 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18438 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18439 DENOM, update OFFSET, and return true on success. Return false on
18440 failure. */
18441
18442 static bool
18443 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18444 gdb_mpz *num, gdb_mpz *denom)
18445 {
18446 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18447 return false;
18448 return ada_get_gnat_encoded_number (encoding, offset, denom);
18449 }
18450
18451 /* Assuming DIE corresponds to a fixed point type, finish the creation
18452 of the corresponding TYPE by setting its type-specific data. CU is
18453 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18454 encodings. It is nullptr if the GNAT encoding should be
18455 ignored. */
18456
18457 static void
18458 finish_fixed_point_type (struct type *type, const char *suffix,
18459 struct die_info *die, struct dwarf2_cu *cu)
18460 {
18461 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18462 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18463
18464 /* If GNAT encodings are preferred, don't examine the
18465 attributes. */
18466 struct attribute *attr = nullptr;
18467 if (suffix == nullptr)
18468 {
18469 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18470 if (attr == nullptr)
18471 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18472 if (attr == nullptr)
18473 attr = dwarf2_attr (die, DW_AT_small, cu);
18474 }
18475
18476 /* Numerator and denominator of our fixed-point type's scaling factor.
18477 The default is a scaling factor of 1, which we use as a fallback
18478 when we are not able to decode it (problem with the debugging info,
18479 unsupported forms, bug in GDB, etc...). Using that as the default
18480 allows us to at least print the unscaled value, which might still
18481 be useful to a user. */
18482 gdb_mpz scale_num (1);
18483 gdb_mpz scale_denom (1);
18484
18485 if (attr == nullptr)
18486 {
18487 int offset = 0;
18488 if (suffix != nullptr
18489 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18490 &scale_denom)
18491 /* The number might be encoded as _nn_dd_nn_dd, where the
18492 second ratio is the 'small value. In this situation, we
18493 want the second value. */
18494 && (suffix[offset] != '_'
18495 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18496 &scale_denom)))
18497 {
18498 /* Found it. */
18499 }
18500 else
18501 {
18502 /* Scaling factor not found. Assume a scaling factor of 1,
18503 and hope for the best. At least the user will be able to
18504 see the encoded value. */
18505 scale_num = 1;
18506 scale_denom = 1;
18507 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18508 sect_offset_str (die->sect_off));
18509 }
18510 }
18511 else if (attr->name == DW_AT_binary_scale)
18512 {
18513 LONGEST scale_exp = attr->constant_value (0);
18514 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18515
18516 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18517 }
18518 else if (attr->name == DW_AT_decimal_scale)
18519 {
18520 LONGEST scale_exp = attr->constant_value (0);
18521 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18522
18523 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18524 }
18525 else if (attr->name == DW_AT_small)
18526 {
18527 struct die_info *scale_die;
18528 struct dwarf2_cu *scale_cu = cu;
18529
18530 scale_die = follow_die_ref (die, attr, &scale_cu);
18531 if (scale_die->tag == DW_TAG_constant)
18532 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18533 &scale_num, &scale_denom);
18534 else
18535 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18536 " (DIE at %s)"),
18537 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18538 }
18539 else
18540 {
18541 complaint (_("unsupported scale attribute %s for fixed-point type"
18542 " (DIE at %s)"),
18543 dwarf_attr_name (attr->name),
18544 sect_offset_str (die->sect_off));
18545 }
18546
18547 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18548 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18549 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18550 mpq_canonicalize (scaling_factor.val);
18551 }
18552
18553 /* The gnat-encoding suffix for fixed point. */
18554
18555 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18556
18557 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18558 "XF" suffix of the name. The text after this is what encodes the
18559 'small and 'delta information. Otherwise, return nullptr. */
18560
18561 static const char *
18562 gnat_encoded_fixed_point_type_info (const char *name)
18563 {
18564 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18565 }
18566
18567 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18568 (which may be different from NAME) to the architecture back-end to allow
18569 it to guess the correct format if necessary. */
18570
18571 static struct type *
18572 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18573 const char *name_hint, enum bfd_endian byte_order)
18574 {
18575 struct gdbarch *gdbarch = objfile->arch ();
18576 const struct floatformat **format;
18577 struct type *type;
18578
18579 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18580 if (format)
18581 type = init_float_type (objfile, bits, name, format, byte_order);
18582 else
18583 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18584
18585 return type;
18586 }
18587
18588 /* Allocate an integer type of size BITS and name NAME. */
18589
18590 static struct type *
18591 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18592 int bits, int unsigned_p, const char *name)
18593 {
18594 struct type *type;
18595
18596 /* Versions of Intel's C Compiler generate an integer type called "void"
18597 instead of using DW_TAG_unspecified_type. This has been seen on
18598 at least versions 14, 17, and 18. */
18599 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18600 && strcmp (name, "void") == 0)
18601 type = objfile_type (objfile)->builtin_void;
18602 else
18603 type = init_integer_type (objfile, bits, unsigned_p, name);
18604
18605 return type;
18606 }
18607
18608 /* Return true if DIE has a DW_AT_small attribute whose value is
18609 a constant rational, where both the numerator and denominator
18610 are equal to zero.
18611
18612 CU is the DIE's Compilation Unit. */
18613
18614 static bool
18615 has_zero_over_zero_small_attribute (struct die_info *die,
18616 struct dwarf2_cu *cu)
18617 {
18618 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18619 if (attr == nullptr)
18620 return false;
18621
18622 struct dwarf2_cu *scale_cu = cu;
18623 struct die_info *scale_die
18624 = follow_die_ref (die, attr, &scale_cu);
18625
18626 if (scale_die->tag != DW_TAG_constant)
18627 return false;
18628
18629 gdb_mpz num (1), denom (1);
18630 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18631 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18632 }
18633
18634 /* Initialise and return a floating point type of size BITS suitable for
18635 use as a component of a complex number. The NAME_HINT is passed through
18636 when initialising the floating point type and is the name of the complex
18637 type.
18638
18639 As DWARF doesn't currently provide an explicit name for the components
18640 of a complex number, but it can be helpful to have these components
18641 named, we try to select a suitable name based on the size of the
18642 component. */
18643 static struct type *
18644 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18645 struct objfile *objfile,
18646 int bits, const char *name_hint,
18647 enum bfd_endian byte_order)
18648 {
18649 gdbarch *gdbarch = objfile->arch ();
18650 struct type *tt = nullptr;
18651
18652 /* Try to find a suitable floating point builtin type of size BITS.
18653 We're going to use the name of this type as the name for the complex
18654 target type that we are about to create. */
18655 switch (cu->language)
18656 {
18657 case language_fortran:
18658 switch (bits)
18659 {
18660 case 32:
18661 tt = builtin_f_type (gdbarch)->builtin_real;
18662 break;
18663 case 64:
18664 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18665 break;
18666 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18667 case 128:
18668 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18669 break;
18670 }
18671 break;
18672 default:
18673 switch (bits)
18674 {
18675 case 32:
18676 tt = builtin_type (gdbarch)->builtin_float;
18677 break;
18678 case 64:
18679 tt = builtin_type (gdbarch)->builtin_double;
18680 break;
18681 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18682 case 128:
18683 tt = builtin_type (gdbarch)->builtin_long_double;
18684 break;
18685 }
18686 break;
18687 }
18688
18689 /* If the type we found doesn't match the size we were looking for, then
18690 pretend we didn't find a type at all, the complex target type we
18691 create will then be nameless. */
18692 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18693 tt = nullptr;
18694
18695 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18696 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18697 }
18698
18699 /* Find a representation of a given base type and install
18700 it in the TYPE field of the die. */
18701
18702 static struct type *
18703 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18704 {
18705 struct objfile *objfile = cu->per_objfile->objfile;
18706 struct type *type;
18707 struct attribute *attr;
18708 int encoding = 0, bits = 0;
18709 const char *name;
18710 gdbarch *arch;
18711
18712 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18713 if (attr != nullptr && attr->form_is_constant ())
18714 encoding = attr->constant_value (0);
18715 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18716 if (attr != nullptr)
18717 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18718 name = dwarf2_name (die, cu);
18719 if (!name)
18720 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18721
18722 arch = objfile->arch ();
18723 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18724
18725 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18726 if (attr != nullptr && attr->form_is_constant ())
18727 {
18728 int endianity = attr->constant_value (0);
18729
18730 switch (endianity)
18731 {
18732 case DW_END_big:
18733 byte_order = BFD_ENDIAN_BIG;
18734 break;
18735 case DW_END_little:
18736 byte_order = BFD_ENDIAN_LITTLE;
18737 break;
18738 default:
18739 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18740 break;
18741 }
18742 }
18743
18744 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18745 && cu->language == language_ada
18746 && has_zero_over_zero_small_attribute (die, cu))
18747 {
18748 /* brobecker/2018-02-24: This is a fixed point type for which
18749 the scaling factor is represented as fraction whose value
18750 does not make sense (zero divided by zero), so we should
18751 normally never see these. However, there is a small category
18752 of fixed point types for which GNAT is unable to provide
18753 the scaling factor via the standard DWARF mechanisms, and
18754 for which the info is provided via the GNAT encodings instead.
18755 This is likely what this DIE is about. */
18756 encoding = (encoding == DW_ATE_signed_fixed
18757 ? DW_ATE_signed
18758 : DW_ATE_unsigned);
18759 }
18760
18761 /* With GNAT encodings, fixed-point information will be encoded in
18762 the type name. Note that this can also occur with the above
18763 zero-over-zero case, which is why this is a separate "if" rather
18764 than an "else if". */
18765 const char *gnat_encoding_suffix = nullptr;
18766 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18767 && cu->language == language_ada
18768 && name != nullptr)
18769 {
18770 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18771 if (gnat_encoding_suffix != nullptr)
18772 {
18773 gdb_assert (startswith (gnat_encoding_suffix,
18774 GNAT_FIXED_POINT_SUFFIX));
18775 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18776 name, gnat_encoding_suffix - name);
18777 /* Use -1 here so that SUFFIX points at the "_" after the
18778 "XF". */
18779 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18780
18781 encoding = (encoding == DW_ATE_signed
18782 ? DW_ATE_signed_fixed
18783 : DW_ATE_unsigned_fixed);
18784 }
18785 }
18786
18787 switch (encoding)
18788 {
18789 case DW_ATE_address:
18790 /* Turn DW_ATE_address into a void * pointer. */
18791 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18792 type = init_pointer_type (objfile, bits, name, type);
18793 break;
18794 case DW_ATE_boolean:
18795 type = init_boolean_type (objfile, bits, 1, name);
18796 break;
18797 case DW_ATE_complex_float:
18798 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18799 byte_order);
18800 if (type->code () == TYPE_CODE_ERROR)
18801 {
18802 if (name == nullptr)
18803 {
18804 struct obstack *obstack
18805 = &cu->per_objfile->objfile->objfile_obstack;
18806 name = obconcat (obstack, "_Complex ", type->name (),
18807 nullptr);
18808 }
18809 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18810 }
18811 else
18812 type = init_complex_type (name, type);
18813 break;
18814 case DW_ATE_decimal_float:
18815 type = init_decfloat_type (objfile, bits, name);
18816 break;
18817 case DW_ATE_float:
18818 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18819 break;
18820 case DW_ATE_signed:
18821 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18822 break;
18823 case DW_ATE_unsigned:
18824 if (cu->language == language_fortran
18825 && name
18826 && startswith (name, "character("))
18827 type = init_character_type (objfile, bits, 1, name);
18828 else
18829 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18830 break;
18831 case DW_ATE_signed_char:
18832 if (cu->language == language_ada || cu->language == language_m2
18833 || cu->language == language_pascal
18834 || cu->language == language_fortran)
18835 type = init_character_type (objfile, bits, 0, name);
18836 else
18837 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18838 break;
18839 case DW_ATE_unsigned_char:
18840 if (cu->language == language_ada || cu->language == language_m2
18841 || cu->language == language_pascal
18842 || cu->language == language_fortran
18843 || cu->language == language_rust)
18844 type = init_character_type (objfile, bits, 1, name);
18845 else
18846 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18847 break;
18848 case DW_ATE_UTF:
18849 {
18850 if (bits == 16)
18851 type = builtin_type (arch)->builtin_char16;
18852 else if (bits == 32)
18853 type = builtin_type (arch)->builtin_char32;
18854 else
18855 {
18856 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18857 bits);
18858 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18859 }
18860 return set_die_type (die, type, cu);
18861 }
18862 break;
18863 case DW_ATE_signed_fixed:
18864 type = init_fixed_point_type (objfile, bits, 0, name);
18865 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18866 break;
18867 case DW_ATE_unsigned_fixed:
18868 type = init_fixed_point_type (objfile, bits, 1, name);
18869 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18870 break;
18871
18872 default:
18873 complaint (_("unsupported DW_AT_encoding: '%s'"),
18874 dwarf_type_encoding_name (encoding));
18875 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18876 break;
18877 }
18878
18879 if (name && strcmp (name, "char") == 0)
18880 type->set_has_no_signedness (true);
18881
18882 maybe_set_alignment (cu, die, type);
18883
18884 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18885
18886 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18887 {
18888 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18889 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18890 {
18891 unsigned real_bit_size = attr->as_unsigned ();
18892 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18893 /* Only use the attributes if they make sense together. */
18894 if (attr == nullptr
18895 || (attr->as_unsigned () + real_bit_size
18896 <= 8 * TYPE_LENGTH (type)))
18897 {
18898 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18899 = real_bit_size;
18900 if (attr != nullptr)
18901 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18902 = attr->as_unsigned ();
18903 }
18904 }
18905 }
18906
18907 return set_die_type (die, type, cu);
18908 }
18909
18910 /* Parse dwarf attribute if it's a block, reference or constant and put the
18911 resulting value of the attribute into struct bound_prop.
18912 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18913
18914 static int
18915 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18916 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18917 struct type *default_type)
18918 {
18919 struct dwarf2_property_baton *baton;
18920 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18921 struct objfile *objfile = per_objfile->objfile;
18922 struct obstack *obstack = &objfile->objfile_obstack;
18923
18924 gdb_assert (default_type != NULL);
18925
18926 if (attr == NULL || prop == NULL)
18927 return 0;
18928
18929 if (attr->form_is_block ())
18930 {
18931 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18932 baton->property_type = default_type;
18933 baton->locexpr.per_cu = cu->per_cu;
18934 baton->locexpr.per_objfile = per_objfile;
18935
18936 struct dwarf_block *block = attr->as_block ();
18937 baton->locexpr.size = block->size;
18938 baton->locexpr.data = block->data;
18939 switch (attr->name)
18940 {
18941 case DW_AT_string_length:
18942 baton->locexpr.is_reference = true;
18943 break;
18944 default:
18945 baton->locexpr.is_reference = false;
18946 break;
18947 }
18948
18949 prop->set_locexpr (baton);
18950 gdb_assert (prop->baton () != NULL);
18951 }
18952 else if (attr->form_is_ref ())
18953 {
18954 struct dwarf2_cu *target_cu = cu;
18955 struct die_info *target_die;
18956 struct attribute *target_attr;
18957
18958 target_die = follow_die_ref (die, attr, &target_cu);
18959 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18960 if (target_attr == NULL)
18961 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18962 target_cu);
18963 if (target_attr == NULL)
18964 return 0;
18965
18966 switch (target_attr->name)
18967 {
18968 case DW_AT_location:
18969 if (target_attr->form_is_section_offset ())
18970 {
18971 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18972 baton->property_type = die_type (target_die, target_cu);
18973 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18974 prop->set_loclist (baton);
18975 gdb_assert (prop->baton () != NULL);
18976 }
18977 else if (target_attr->form_is_block ())
18978 {
18979 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18980 baton->property_type = die_type (target_die, target_cu);
18981 baton->locexpr.per_cu = cu->per_cu;
18982 baton->locexpr.per_objfile = per_objfile;
18983 struct dwarf_block *block = target_attr->as_block ();
18984 baton->locexpr.size = block->size;
18985 baton->locexpr.data = block->data;
18986 baton->locexpr.is_reference = true;
18987 prop->set_locexpr (baton);
18988 gdb_assert (prop->baton () != NULL);
18989 }
18990 else
18991 {
18992 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18993 "dynamic property");
18994 return 0;
18995 }
18996 break;
18997 case DW_AT_data_member_location:
18998 {
18999 LONGEST offset;
19000
19001 if (!handle_data_member_location (target_die, target_cu,
19002 &offset))
19003 return 0;
19004
19005 baton = XOBNEW (obstack, struct dwarf2_property_baton);
19006 baton->property_type = read_type_die (target_die->parent,
19007 target_cu);
19008 baton->offset_info.offset = offset;
19009 baton->offset_info.type = die_type (target_die, target_cu);
19010 prop->set_addr_offset (baton);
19011 break;
19012 }
19013 }
19014 }
19015 else if (attr->form_is_constant ())
19016 prop->set_const_val (attr->constant_value (0));
19017 else
19018 {
19019 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
19020 dwarf2_name (die, cu));
19021 return 0;
19022 }
19023
19024 return 1;
19025 }
19026
19027 /* See read.h. */
19028
19029 struct type *
19030 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
19031 {
19032 struct type *int_type;
19033
19034 /* Helper macro to examine the various builtin types. */
19035 #define TRY_TYPE(F) \
19036 int_type = (unsigned_p \
19037 ? objfile_type (objfile)->builtin_unsigned_ ## F \
19038 : objfile_type (objfile)->builtin_ ## F); \
19039 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
19040 return int_type
19041
19042 TRY_TYPE (char);
19043 TRY_TYPE (short);
19044 TRY_TYPE (int);
19045 TRY_TYPE (long);
19046 TRY_TYPE (long_long);
19047
19048 #undef TRY_TYPE
19049
19050 gdb_assert_not_reached ("unable to find suitable integer type");
19051 }
19052
19053 /* See read.h. */
19054
19055 struct type *
19056 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
19057 {
19058 int addr_size = this->per_cu->addr_size ();
19059 return this->per_objfile->int_type (addr_size, unsigned_p);
19060 }
19061
19062 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
19063 present (which is valid) then compute the default type based on the
19064 compilation units address size. */
19065
19066 static struct type *
19067 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
19068 {
19069 struct type *index_type = die_type (die, cu);
19070
19071 /* Dwarf-2 specifications explicitly allows to create subrange types
19072 without specifying a base type.
19073 In that case, the base type must be set to the type of
19074 the lower bound, upper bound or count, in that order, if any of these
19075 three attributes references an object that has a type.
19076 If no base type is found, the Dwarf-2 specifications say that
19077 a signed integer type of size equal to the size of an address should
19078 be used.
19079 For the following C code: `extern char gdb_int [];'
19080 GCC produces an empty range DIE.
19081 FIXME: muller/2010-05-28: Possible references to object for low bound,
19082 high bound or count are not yet handled by this code. */
19083 if (index_type->code () == TYPE_CODE_VOID)
19084 index_type = cu->addr_sized_int_type (false);
19085
19086 return index_type;
19087 }
19088
19089 /* Read the given DW_AT_subrange DIE. */
19090
19091 static struct type *
19092 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
19093 {
19094 struct type *base_type, *orig_base_type;
19095 struct type *range_type;
19096 struct attribute *attr;
19097 struct dynamic_prop low, high;
19098 int low_default_is_valid;
19099 int high_bound_is_count = 0;
19100 const char *name;
19101 ULONGEST negative_mask;
19102
19103 orig_base_type = read_subrange_index_type (die, cu);
19104
19105 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
19106 whereas the real type might be. So, we use ORIG_BASE_TYPE when
19107 creating the range type, but we use the result of check_typedef
19108 when examining properties of the type. */
19109 base_type = check_typedef (orig_base_type);
19110
19111 /* The die_type call above may have already set the type for this DIE. */
19112 range_type = get_die_type (die, cu);
19113 if (range_type)
19114 return range_type;
19115
19116 high.set_const_val (0);
19117
19118 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19119 omitting DW_AT_lower_bound. */
19120 switch (cu->language)
19121 {
19122 case language_c:
19123 case language_cplus:
19124 low.set_const_val (0);
19125 low_default_is_valid = 1;
19126 break;
19127 case language_fortran:
19128 low.set_const_val (1);
19129 low_default_is_valid = 1;
19130 break;
19131 case language_d:
19132 case language_objc:
19133 case language_rust:
19134 low.set_const_val (0);
19135 low_default_is_valid = (cu->header.version >= 4);
19136 break;
19137 case language_ada:
19138 case language_m2:
19139 case language_pascal:
19140 low.set_const_val (1);
19141 low_default_is_valid = (cu->header.version >= 4);
19142 break;
19143 default:
19144 low.set_const_val (0);
19145 low_default_is_valid = 0;
19146 break;
19147 }
19148
19149 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
19150 if (attr != nullptr)
19151 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
19152 else if (!low_default_is_valid)
19153 complaint (_("Missing DW_AT_lower_bound "
19154 "- DIE at %s [in module %s]"),
19155 sect_offset_str (die->sect_off),
19156 objfile_name (cu->per_objfile->objfile));
19157
19158 struct attribute *attr_ub, *attr_count;
19159 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
19160 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19161 {
19162 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
19163 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19164 {
19165 /* If bounds are constant do the final calculation here. */
19166 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19167 high.set_const_val (low.const_val () + high.const_val () - 1);
19168 else
19169 high_bound_is_count = 1;
19170 }
19171 else
19172 {
19173 if (attr_ub != NULL)
19174 complaint (_("Unresolved DW_AT_upper_bound "
19175 "- DIE at %s [in module %s]"),
19176 sect_offset_str (die->sect_off),
19177 objfile_name (cu->per_objfile->objfile));
19178 if (attr_count != NULL)
19179 complaint (_("Unresolved DW_AT_count "
19180 "- DIE at %s [in module %s]"),
19181 sect_offset_str (die->sect_off),
19182 objfile_name (cu->per_objfile->objfile));
19183 }
19184 }
19185
19186 LONGEST bias = 0;
19187 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
19188 if (bias_attr != nullptr && bias_attr->form_is_constant ())
19189 bias = bias_attr->constant_value (0);
19190
19191 /* Normally, the DWARF producers are expected to use a signed
19192 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19193 But this is unfortunately not always the case, as witnessed
19194 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19195 is used instead. To work around that ambiguity, we treat
19196 the bounds as signed, and thus sign-extend their values, when
19197 the base type is signed. */
19198 negative_mask =
19199 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19200 if (low.kind () == PROP_CONST
19201 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19202 low.set_const_val (low.const_val () | negative_mask);
19203 if (high.kind () == PROP_CONST
19204 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19205 high.set_const_val (high.const_val () | negative_mask);
19206
19207 /* Check for bit and byte strides. */
19208 struct dynamic_prop byte_stride_prop;
19209 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19210 if (attr_byte_stride != nullptr)
19211 {
19212 struct type *prop_type = cu->addr_sized_int_type (false);
19213 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19214 prop_type);
19215 }
19216
19217 struct dynamic_prop bit_stride_prop;
19218 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19219 if (attr_bit_stride != nullptr)
19220 {
19221 /* It only makes sense to have either a bit or byte stride. */
19222 if (attr_byte_stride != nullptr)
19223 {
19224 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19225 "- DIE at %s [in module %s]"),
19226 sect_offset_str (die->sect_off),
19227 objfile_name (cu->per_objfile->objfile));
19228 attr_bit_stride = nullptr;
19229 }
19230 else
19231 {
19232 struct type *prop_type = cu->addr_sized_int_type (false);
19233 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19234 prop_type);
19235 }
19236 }
19237
19238 if (attr_byte_stride != nullptr
19239 || attr_bit_stride != nullptr)
19240 {
19241 bool byte_stride_p = (attr_byte_stride != nullptr);
19242 struct dynamic_prop *stride
19243 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19244
19245 range_type
19246 = create_range_type_with_stride (NULL, orig_base_type, &low,
19247 &high, bias, stride, byte_stride_p);
19248 }
19249 else
19250 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19251
19252 if (high_bound_is_count)
19253 range_type->bounds ()->flag_upper_bound_is_count = 1;
19254
19255 /* Ada expects an empty array on no boundary attributes. */
19256 if (attr == NULL && cu->language != language_ada)
19257 range_type->bounds ()->high.set_undefined ();
19258
19259 name = dwarf2_name (die, cu);
19260 if (name)
19261 range_type->set_name (name);
19262
19263 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19264 if (attr != nullptr)
19265 TYPE_LENGTH (range_type) = attr->constant_value (0);
19266
19267 maybe_set_alignment (cu, die, range_type);
19268
19269 set_die_type (die, range_type, cu);
19270
19271 /* set_die_type should be already done. */
19272 set_descriptive_type (range_type, die, cu);
19273
19274 return range_type;
19275 }
19276
19277 static struct type *
19278 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19279 {
19280 struct type *type;
19281
19282 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19283 type->set_name (dwarf2_name (die, cu));
19284
19285 /* In Ada, an unspecified type is typically used when the description
19286 of the type is deferred to a different unit. When encountering
19287 such a type, we treat it as a stub, and try to resolve it later on,
19288 when needed. */
19289 if (cu->language == language_ada)
19290 type->set_is_stub (true);
19291
19292 return set_die_type (die, type, cu);
19293 }
19294
19295 /* Read a single die and all its descendents. Set the die's sibling
19296 field to NULL; set other fields in the die correctly, and set all
19297 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19298 location of the info_ptr after reading all of those dies. PARENT
19299 is the parent of the die in question. */
19300
19301 static struct die_info *
19302 read_die_and_children (const struct die_reader_specs *reader,
19303 const gdb_byte *info_ptr,
19304 const gdb_byte **new_info_ptr,
19305 struct die_info *parent)
19306 {
19307 struct die_info *die;
19308 const gdb_byte *cur_ptr;
19309
19310 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19311 if (die == NULL)
19312 {
19313 *new_info_ptr = cur_ptr;
19314 return NULL;
19315 }
19316 store_in_ref_table (die, reader->cu);
19317
19318 if (die->has_children)
19319 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19320 else
19321 {
19322 die->child = NULL;
19323 *new_info_ptr = cur_ptr;
19324 }
19325
19326 die->sibling = NULL;
19327 die->parent = parent;
19328 return die;
19329 }
19330
19331 /* Read a die, all of its descendents, and all of its siblings; set
19332 all of the fields of all of the dies correctly. Arguments are as
19333 in read_die_and_children. */
19334
19335 static struct die_info *
19336 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19337 const gdb_byte *info_ptr,
19338 const gdb_byte **new_info_ptr,
19339 struct die_info *parent)
19340 {
19341 struct die_info *first_die, *last_sibling;
19342 const gdb_byte *cur_ptr;
19343
19344 cur_ptr = info_ptr;
19345 first_die = last_sibling = NULL;
19346
19347 while (1)
19348 {
19349 struct die_info *die
19350 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19351
19352 if (die == NULL)
19353 {
19354 *new_info_ptr = cur_ptr;
19355 return first_die;
19356 }
19357
19358 if (!first_die)
19359 first_die = die;
19360 else
19361 last_sibling->sibling = die;
19362
19363 last_sibling = die;
19364 }
19365 }
19366
19367 /* Read a die, all of its descendents, and all of its siblings; set
19368 all of the fields of all of the dies correctly. Arguments are as
19369 in read_die_and_children.
19370 This the main entry point for reading a DIE and all its children. */
19371
19372 static struct die_info *
19373 read_die_and_siblings (const struct die_reader_specs *reader,
19374 const gdb_byte *info_ptr,
19375 const gdb_byte **new_info_ptr,
19376 struct die_info *parent)
19377 {
19378 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19379 new_info_ptr, parent);
19380
19381 if (dwarf_die_debug)
19382 {
19383 fprintf_unfiltered (gdb_stdlog,
19384 "Read die from %s@0x%x of %s:\n",
19385 reader->die_section->get_name (),
19386 (unsigned) (info_ptr - reader->die_section->buffer),
19387 bfd_get_filename (reader->abfd));
19388 dump_die (die, dwarf_die_debug);
19389 }
19390
19391 return die;
19392 }
19393
19394 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19395 attributes.
19396 The caller is responsible for filling in the extra attributes
19397 and updating (*DIEP)->num_attrs.
19398 Set DIEP to point to a newly allocated die with its information,
19399 except for its child, sibling, and parent fields. */
19400
19401 static const gdb_byte *
19402 read_full_die_1 (const struct die_reader_specs *reader,
19403 struct die_info **diep, const gdb_byte *info_ptr,
19404 int num_extra_attrs)
19405 {
19406 unsigned int abbrev_number, bytes_read, i;
19407 const struct abbrev_info *abbrev;
19408 struct die_info *die;
19409 struct dwarf2_cu *cu = reader->cu;
19410 bfd *abfd = reader->abfd;
19411
19412 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19413 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19414 info_ptr += bytes_read;
19415 if (!abbrev_number)
19416 {
19417 *diep = NULL;
19418 return info_ptr;
19419 }
19420
19421 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19422 if (!abbrev)
19423 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19424 abbrev_number,
19425 bfd_get_filename (abfd));
19426
19427 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19428 die->sect_off = sect_off;
19429 die->tag = abbrev->tag;
19430 die->abbrev = abbrev_number;
19431 die->has_children = abbrev->has_children;
19432
19433 /* Make the result usable.
19434 The caller needs to update num_attrs after adding the extra
19435 attributes. */
19436 die->num_attrs = abbrev->num_attrs;
19437
19438 bool any_need_reprocess = false;
19439 for (i = 0; i < abbrev->num_attrs; ++i)
19440 {
19441 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19442 info_ptr);
19443 if (die->attrs[i].requires_reprocessing_p ())
19444 any_need_reprocess = true;
19445 }
19446
19447 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19448 if (attr != nullptr && attr->form_is_unsigned ())
19449 cu->str_offsets_base = attr->as_unsigned ();
19450
19451 attr = die->attr (DW_AT_loclists_base);
19452 if (attr != nullptr)
19453 cu->loclist_base = attr->as_unsigned ();
19454
19455 auto maybe_addr_base = die->addr_base ();
19456 if (maybe_addr_base.has_value ())
19457 cu->addr_base = *maybe_addr_base;
19458
19459 attr = die->attr (DW_AT_rnglists_base);
19460 if (attr != nullptr)
19461 cu->rnglists_base = attr->as_unsigned ();
19462
19463 if (any_need_reprocess)
19464 {
19465 for (i = 0; i < abbrev->num_attrs; ++i)
19466 {
19467 if (die->attrs[i].requires_reprocessing_p ())
19468 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19469 }
19470 }
19471 *diep = die;
19472 return info_ptr;
19473 }
19474
19475 /* Read a die and all its attributes.
19476 Set DIEP to point to a newly allocated die with its information,
19477 except for its child, sibling, and parent fields. */
19478
19479 static const gdb_byte *
19480 read_full_die (const struct die_reader_specs *reader,
19481 struct die_info **diep, const gdb_byte *info_ptr)
19482 {
19483 const gdb_byte *result;
19484
19485 result = read_full_die_1 (reader, diep, info_ptr, 0);
19486
19487 if (dwarf_die_debug)
19488 {
19489 fprintf_unfiltered (gdb_stdlog,
19490 "Read die from %s@0x%x of %s:\n",
19491 reader->die_section->get_name (),
19492 (unsigned) (info_ptr - reader->die_section->buffer),
19493 bfd_get_filename (reader->abfd));
19494 dump_die (*diep, dwarf_die_debug);
19495 }
19496
19497 return result;
19498 }
19499 \f
19500
19501 /* Returns nonzero if TAG represents a type that we might generate a partial
19502 symbol for. */
19503
19504 static int
19505 is_type_tag_for_partial (int tag, enum language lang)
19506 {
19507 switch (tag)
19508 {
19509 #if 0
19510 /* Some types that would be reasonable to generate partial symbols for,
19511 that we don't at present. Note that normally this does not
19512 matter, mainly because C compilers don't give names to these
19513 types, but instead emit DW_TAG_typedef. */
19514 case DW_TAG_file_type:
19515 case DW_TAG_ptr_to_member_type:
19516 case DW_TAG_set_type:
19517 case DW_TAG_string_type:
19518 case DW_TAG_subroutine_type:
19519 #endif
19520
19521 /* GNAT may emit an array with a name, but no typedef, so we
19522 need to make a symbol in this case. */
19523 case DW_TAG_array_type:
19524 return lang == language_ada;
19525
19526 case DW_TAG_base_type:
19527 case DW_TAG_class_type:
19528 case DW_TAG_interface_type:
19529 case DW_TAG_enumeration_type:
19530 case DW_TAG_structure_type:
19531 case DW_TAG_subrange_type:
19532 case DW_TAG_typedef:
19533 case DW_TAG_union_type:
19534 return 1;
19535 default:
19536 return 0;
19537 }
19538 }
19539
19540 /* Load all DIEs that are interesting for partial symbols into memory. */
19541
19542 static struct partial_die_info *
19543 load_partial_dies (const struct die_reader_specs *reader,
19544 const gdb_byte *info_ptr, int building_psymtab)
19545 {
19546 struct dwarf2_cu *cu = reader->cu;
19547 struct objfile *objfile = cu->per_objfile->objfile;
19548 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19549 unsigned int bytes_read;
19550 unsigned int load_all = 0;
19551 int nesting_level = 1;
19552
19553 parent_die = NULL;
19554 last_die = NULL;
19555
19556 gdb_assert (cu->per_cu != NULL);
19557 if (cu->per_cu->load_all_dies)
19558 load_all = 1;
19559
19560 cu->partial_dies
19561 = htab_create_alloc_ex (cu->header.length / 12,
19562 partial_die_hash,
19563 partial_die_eq,
19564 NULL,
19565 &cu->comp_unit_obstack,
19566 hashtab_obstack_allocate,
19567 dummy_obstack_deallocate);
19568
19569 while (1)
19570 {
19571 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19572 &bytes_read);
19573
19574 /* A NULL abbrev means the end of a series of children. */
19575 if (abbrev == NULL)
19576 {
19577 if (--nesting_level == 0)
19578 return first_die;
19579
19580 info_ptr += bytes_read;
19581 last_die = parent_die;
19582 parent_die = parent_die->die_parent;
19583 continue;
19584 }
19585
19586 /* Check for template arguments. We never save these; if
19587 they're seen, we just mark the parent, and go on our way. */
19588 if (parent_die != NULL
19589 && cu->language == language_cplus
19590 && (abbrev->tag == DW_TAG_template_type_param
19591 || abbrev->tag == DW_TAG_template_value_param))
19592 {
19593 parent_die->has_template_arguments = 1;
19594
19595 if (!load_all)
19596 {
19597 /* We don't need a partial DIE for the template argument. */
19598 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19599 continue;
19600 }
19601 }
19602
19603 /* We only recurse into c++ subprograms looking for template arguments.
19604 Skip their other children. */
19605 if (!load_all
19606 && cu->language == language_cplus
19607 && parent_die != NULL
19608 && parent_die->tag == DW_TAG_subprogram
19609 && abbrev->tag != DW_TAG_inlined_subroutine)
19610 {
19611 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19612 continue;
19613 }
19614
19615 /* Check whether this DIE is interesting enough to save. Normally
19616 we would not be interested in members here, but there may be
19617 later variables referencing them via DW_AT_specification (for
19618 static members). */
19619 if (!load_all
19620 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19621 && abbrev->tag != DW_TAG_constant
19622 && abbrev->tag != DW_TAG_enumerator
19623 && abbrev->tag != DW_TAG_subprogram
19624 && abbrev->tag != DW_TAG_inlined_subroutine
19625 && abbrev->tag != DW_TAG_lexical_block
19626 && abbrev->tag != DW_TAG_variable
19627 && abbrev->tag != DW_TAG_namespace
19628 && abbrev->tag != DW_TAG_module
19629 && abbrev->tag != DW_TAG_member
19630 && abbrev->tag != DW_TAG_imported_unit
19631 && abbrev->tag != DW_TAG_imported_declaration)
19632 {
19633 /* Otherwise we skip to the next sibling, if any. */
19634 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19635 continue;
19636 }
19637
19638 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19639 abbrev);
19640
19641 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19642
19643 /* This two-pass algorithm for processing partial symbols has a
19644 high cost in cache pressure. Thus, handle some simple cases
19645 here which cover the majority of C partial symbols. DIEs
19646 which neither have specification tags in them, nor could have
19647 specification tags elsewhere pointing at them, can simply be
19648 processed and discarded.
19649
19650 This segment is also optional; scan_partial_symbols and
19651 add_partial_symbol will handle these DIEs if we chain
19652 them in normally. When compilers which do not emit large
19653 quantities of duplicate debug information are more common,
19654 this code can probably be removed. */
19655
19656 /* Any complete simple types at the top level (pretty much all
19657 of them, for a language without namespaces), can be processed
19658 directly. */
19659 if (parent_die == NULL
19660 && pdi.has_specification == 0
19661 && pdi.is_declaration == 0
19662 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19663 || pdi.tag == DW_TAG_base_type
19664 || pdi.tag == DW_TAG_array_type
19665 || pdi.tag == DW_TAG_subrange_type))
19666 {
19667 if (building_psymtab && pdi.raw_name != NULL)
19668 add_partial_symbol (&pdi, cu);
19669
19670 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19671 continue;
19672 }
19673
19674 /* The exception for DW_TAG_typedef with has_children above is
19675 a workaround of GCC PR debug/47510. In the case of this complaint
19676 type_name_or_error will error on such types later.
19677
19678 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19679 it could not find the child DIEs referenced later, this is checked
19680 above. In correct DWARF DW_TAG_typedef should have no children. */
19681
19682 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19683 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19684 "- DIE at %s [in module %s]"),
19685 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19686
19687 /* If we're at the second level, and we're an enumerator, and
19688 our parent has no specification (meaning possibly lives in a
19689 namespace elsewhere), then we can add the partial symbol now
19690 instead of queueing it. */
19691 if (pdi.tag == DW_TAG_enumerator
19692 && parent_die != NULL
19693 && parent_die->die_parent == NULL
19694 && parent_die->tag == DW_TAG_enumeration_type
19695 && parent_die->has_specification == 0)
19696 {
19697 if (pdi.raw_name == NULL)
19698 complaint (_("malformed enumerator DIE ignored"));
19699 else if (building_psymtab)
19700 add_partial_symbol (&pdi, cu);
19701
19702 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19703 continue;
19704 }
19705
19706 struct partial_die_info *part_die
19707 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19708
19709 /* We'll save this DIE so link it in. */
19710 part_die->die_parent = parent_die;
19711 part_die->die_sibling = NULL;
19712 part_die->die_child = NULL;
19713
19714 if (last_die && last_die == parent_die)
19715 last_die->die_child = part_die;
19716 else if (last_die)
19717 last_die->die_sibling = part_die;
19718
19719 last_die = part_die;
19720
19721 if (first_die == NULL)
19722 first_die = part_die;
19723
19724 /* Maybe add the DIE to the hash table. Not all DIEs that we
19725 find interesting need to be in the hash table, because we
19726 also have the parent/sibling/child chains; only those that we
19727 might refer to by offset later during partial symbol reading.
19728
19729 For now this means things that might have be the target of a
19730 DW_AT_specification, DW_AT_abstract_origin, or
19731 DW_AT_extension. DW_AT_extension will refer only to
19732 namespaces; DW_AT_abstract_origin refers to functions (and
19733 many things under the function DIE, but we do not recurse
19734 into function DIEs during partial symbol reading) and
19735 possibly variables as well; DW_AT_specification refers to
19736 declarations. Declarations ought to have the DW_AT_declaration
19737 flag. It happens that GCC forgets to put it in sometimes, but
19738 only for functions, not for types.
19739
19740 Adding more things than necessary to the hash table is harmless
19741 except for the performance cost. Adding too few will result in
19742 wasted time in find_partial_die, when we reread the compilation
19743 unit with load_all_dies set. */
19744
19745 if (load_all
19746 || abbrev->tag == DW_TAG_constant
19747 || abbrev->tag == DW_TAG_subprogram
19748 || abbrev->tag == DW_TAG_variable
19749 || abbrev->tag == DW_TAG_namespace
19750 || part_die->is_declaration)
19751 {
19752 void **slot;
19753
19754 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19755 to_underlying (part_die->sect_off),
19756 INSERT);
19757 *slot = part_die;
19758 }
19759
19760 /* For some DIEs we want to follow their children (if any). For C
19761 we have no reason to follow the children of structures; for other
19762 languages we have to, so that we can get at method physnames
19763 to infer fully qualified class names, for DW_AT_specification,
19764 and for C++ template arguments. For C++, we also look one level
19765 inside functions to find template arguments (if the name of the
19766 function does not already contain the template arguments).
19767
19768 For Ada and Fortran, we need to scan the children of subprograms
19769 and lexical blocks as well because these languages allow the
19770 definition of nested entities that could be interesting for the
19771 debugger, such as nested subprograms for instance. */
19772 if (last_die->has_children
19773 && (load_all
19774 || last_die->tag == DW_TAG_namespace
19775 || last_die->tag == DW_TAG_module
19776 || last_die->tag == DW_TAG_enumeration_type
19777 || (cu->language == language_cplus
19778 && last_die->tag == DW_TAG_subprogram
19779 && (last_die->raw_name == NULL
19780 || strchr (last_die->raw_name, '<') == NULL))
19781 || (cu->language != language_c
19782 && (last_die->tag == DW_TAG_class_type
19783 || last_die->tag == DW_TAG_interface_type
19784 || last_die->tag == DW_TAG_structure_type
19785 || last_die->tag == DW_TAG_union_type))
19786 || ((cu->language == language_ada
19787 || cu->language == language_fortran)
19788 && (last_die->tag == DW_TAG_subprogram
19789 || last_die->tag == DW_TAG_lexical_block))))
19790 {
19791 nesting_level++;
19792 parent_die = last_die;
19793 continue;
19794 }
19795
19796 /* Otherwise we skip to the next sibling, if any. */
19797 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19798
19799 /* Back to the top, do it again. */
19800 }
19801 }
19802
19803 partial_die_info::partial_die_info (sect_offset sect_off_,
19804 const struct abbrev_info *abbrev)
19805 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19806 {
19807 }
19808
19809 /* See class definition. */
19810
19811 const char *
19812 partial_die_info::name (dwarf2_cu *cu)
19813 {
19814 if (!canonical_name && raw_name != nullptr)
19815 {
19816 struct objfile *objfile = cu->per_objfile->objfile;
19817 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19818 canonical_name = 1;
19819 }
19820
19821 return raw_name;
19822 }
19823
19824 /* Read a minimal amount of information into the minimal die structure.
19825 INFO_PTR should point just after the initial uleb128 of a DIE. */
19826
19827 const gdb_byte *
19828 partial_die_info::read (const struct die_reader_specs *reader,
19829 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19830 {
19831 struct dwarf2_cu *cu = reader->cu;
19832 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19833 unsigned int i;
19834 int has_low_pc_attr = 0;
19835 int has_high_pc_attr = 0;
19836 int high_pc_relative = 0;
19837
19838 for (i = 0; i < abbrev.num_attrs; ++i)
19839 {
19840 attribute attr;
19841 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19842 /* String and address offsets that need to do the reprocessing have
19843 already been read at this point, so there is no need to wait until
19844 the loop terminates to do the reprocessing. */
19845 if (attr.requires_reprocessing_p ())
19846 read_attribute_reprocess (reader, &attr, tag);
19847 /* Store the data if it is of an attribute we want to keep in a
19848 partial symbol table. */
19849 switch (attr.name)
19850 {
19851 case DW_AT_name:
19852 switch (tag)
19853 {
19854 case DW_TAG_compile_unit:
19855 case DW_TAG_partial_unit:
19856 case DW_TAG_type_unit:
19857 /* Compilation units have a DW_AT_name that is a filename, not
19858 a source language identifier. */
19859 case DW_TAG_enumeration_type:
19860 case DW_TAG_enumerator:
19861 /* These tags always have simple identifiers already; no need
19862 to canonicalize them. */
19863 canonical_name = 1;
19864 raw_name = attr.as_string ();
19865 break;
19866 default:
19867 canonical_name = 0;
19868 raw_name = attr.as_string ();
19869 break;
19870 }
19871 break;
19872 case DW_AT_linkage_name:
19873 case DW_AT_MIPS_linkage_name:
19874 /* Note that both forms of linkage name might appear. We
19875 assume they will be the same, and we only store the last
19876 one we see. */
19877 linkage_name = attr.as_string ();
19878 break;
19879 case DW_AT_low_pc:
19880 has_low_pc_attr = 1;
19881 lowpc = attr.as_address ();
19882 break;
19883 case DW_AT_high_pc:
19884 has_high_pc_attr = 1;
19885 highpc = attr.as_address ();
19886 if (cu->header.version >= 4 && attr.form_is_constant ())
19887 high_pc_relative = 1;
19888 break;
19889 case DW_AT_location:
19890 /* Support the .debug_loc offsets. */
19891 if (attr.form_is_block ())
19892 {
19893 d.locdesc = attr.as_block ();
19894 }
19895 else if (attr.form_is_section_offset ())
19896 {
19897 dwarf2_complex_location_expr_complaint ();
19898 }
19899 else
19900 {
19901 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19902 "partial symbol information");
19903 }
19904 break;
19905 case DW_AT_external:
19906 is_external = attr.as_boolean ();
19907 break;
19908 case DW_AT_declaration:
19909 is_declaration = attr.as_boolean ();
19910 break;
19911 case DW_AT_type:
19912 has_type = 1;
19913 break;
19914 case DW_AT_abstract_origin:
19915 case DW_AT_specification:
19916 case DW_AT_extension:
19917 has_specification = 1;
19918 spec_offset = attr.get_ref_die_offset ();
19919 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19920 || cu->per_cu->is_dwz);
19921 break;
19922 case DW_AT_sibling:
19923 /* Ignore absolute siblings, they might point outside of
19924 the current compile unit. */
19925 if (attr.form == DW_FORM_ref_addr)
19926 complaint (_("ignoring absolute DW_AT_sibling"));
19927 else
19928 {
19929 const gdb_byte *buffer = reader->buffer;
19930 sect_offset off = attr.get_ref_die_offset ();
19931 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19932
19933 if (sibling_ptr < info_ptr)
19934 complaint (_("DW_AT_sibling points backwards"));
19935 else if (sibling_ptr > reader->buffer_end)
19936 reader->die_section->overflow_complaint ();
19937 else
19938 sibling = sibling_ptr;
19939 }
19940 break;
19941 case DW_AT_byte_size:
19942 has_byte_size = 1;
19943 break;
19944 case DW_AT_const_value:
19945 has_const_value = 1;
19946 break;
19947 case DW_AT_calling_convention:
19948 /* DWARF doesn't provide a way to identify a program's source-level
19949 entry point. DW_AT_calling_convention attributes are only meant
19950 to describe functions' calling conventions.
19951
19952 However, because it's a necessary piece of information in
19953 Fortran, and before DWARF 4 DW_CC_program was the only
19954 piece of debugging information whose definition refers to
19955 a 'main program' at all, several compilers marked Fortran
19956 main programs with DW_CC_program --- even when those
19957 functions use the standard calling conventions.
19958
19959 Although DWARF now specifies a way to provide this
19960 information, we support this practice for backward
19961 compatibility. */
19962 if (attr.constant_value (0) == DW_CC_program
19963 && cu->language == language_fortran)
19964 main_subprogram = 1;
19965 break;
19966 case DW_AT_inline:
19967 {
19968 LONGEST value = attr.constant_value (-1);
19969 if (value == DW_INL_inlined
19970 || value == DW_INL_declared_inlined)
19971 may_be_inlined = 1;
19972 }
19973 break;
19974
19975 case DW_AT_import:
19976 if (tag == DW_TAG_imported_unit)
19977 {
19978 d.sect_off = attr.get_ref_die_offset ();
19979 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19980 || cu->per_cu->is_dwz);
19981 }
19982 break;
19983
19984 case DW_AT_main_subprogram:
19985 main_subprogram = attr.as_boolean ();
19986 break;
19987
19988 case DW_AT_ranges:
19989 {
19990 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19991 on DWARF version). */
19992 ULONGEST ranges_offset = attr.as_unsigned ();
19993
19994 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19995 this value. */
19996 if (tag != DW_TAG_compile_unit)
19997 ranges_offset += cu->gnu_ranges_base;
19998
19999 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
20000 nullptr, tag))
20001 has_pc_info = 1;
20002 }
20003 break;
20004
20005 default:
20006 break;
20007 }
20008 }
20009
20010 /* For Ada, if both the name and the linkage name appear, we prefer
20011 the latter. This lets "catch exception" work better, regardless
20012 of the order in which the name and linkage name were emitted.
20013 Really, though, this is just a workaround for the fact that gdb
20014 doesn't store both the name and the linkage name. */
20015 if (cu->language == language_ada && linkage_name != nullptr)
20016 raw_name = linkage_name;
20017
20018 if (high_pc_relative)
20019 highpc += lowpc;
20020
20021 if (has_low_pc_attr && has_high_pc_attr)
20022 {
20023 /* When using the GNU linker, .gnu.linkonce. sections are used to
20024 eliminate duplicate copies of functions and vtables and such.
20025 The linker will arbitrarily choose one and discard the others.
20026 The AT_*_pc values for such functions refer to local labels in
20027 these sections. If the section from that file was discarded, the
20028 labels are not in the output, so the relocs get a value of 0.
20029 If this is a discarded function, mark the pc bounds as invalid,
20030 so that GDB will ignore it. */
20031 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
20032 {
20033 struct objfile *objfile = per_objfile->objfile;
20034 struct gdbarch *gdbarch = objfile->arch ();
20035
20036 complaint (_("DW_AT_low_pc %s is zero "
20037 "for DIE at %s [in module %s]"),
20038 paddress (gdbarch, lowpc),
20039 sect_offset_str (sect_off),
20040 objfile_name (objfile));
20041 }
20042 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
20043 else if (lowpc >= highpc)
20044 {
20045 struct objfile *objfile = per_objfile->objfile;
20046 struct gdbarch *gdbarch = objfile->arch ();
20047
20048 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
20049 "for DIE at %s [in module %s]"),
20050 paddress (gdbarch, lowpc),
20051 paddress (gdbarch, highpc),
20052 sect_offset_str (sect_off),
20053 objfile_name (objfile));
20054 }
20055 else
20056 has_pc_info = 1;
20057 }
20058
20059 return info_ptr;
20060 }
20061
20062 /* Find a cached partial DIE at OFFSET in CU. */
20063
20064 struct partial_die_info *
20065 dwarf2_cu::find_partial_die (sect_offset sect_off)
20066 {
20067 struct partial_die_info *lookup_die = NULL;
20068 struct partial_die_info part_die (sect_off);
20069
20070 lookup_die = ((struct partial_die_info *)
20071 htab_find_with_hash (partial_dies, &part_die,
20072 to_underlying (sect_off)));
20073
20074 return lookup_die;
20075 }
20076
20077 /* Find a partial DIE at OFFSET, which may or may not be in CU,
20078 except in the case of .debug_types DIEs which do not reference
20079 outside their CU (they do however referencing other types via
20080 DW_FORM_ref_sig8). */
20081
20082 static const struct cu_partial_die_info
20083 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
20084 {
20085 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20086 struct objfile *objfile = per_objfile->objfile;
20087 struct partial_die_info *pd = NULL;
20088
20089 if (offset_in_dwz == cu->per_cu->is_dwz
20090 && cu->header.offset_in_cu_p (sect_off))
20091 {
20092 pd = cu->find_partial_die (sect_off);
20093 if (pd != NULL)
20094 return { cu, pd };
20095 /* We missed recording what we needed.
20096 Load all dies and try again. */
20097 }
20098 else
20099 {
20100 /* TUs don't reference other CUs/TUs (except via type signatures). */
20101 if (cu->per_cu->is_debug_types)
20102 {
20103 error (_("Dwarf Error: Type Unit at offset %s contains"
20104 " external reference to offset %s [in module %s].\n"),
20105 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
20106 bfd_get_filename (objfile->obfd));
20107 }
20108 dwarf2_per_cu_data *per_cu
20109 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20110 per_objfile);
20111
20112 cu = per_objfile->get_cu (per_cu);
20113 if (cu == NULL || cu->partial_dies == NULL)
20114 load_partial_comp_unit (per_cu, per_objfile, nullptr);
20115
20116 cu = per_objfile->get_cu (per_cu);
20117
20118 cu->last_used = 0;
20119 pd = cu->find_partial_die (sect_off);
20120 }
20121
20122 /* If we didn't find it, and not all dies have been loaded,
20123 load them all and try again. */
20124
20125 if (pd == NULL && cu->per_cu->load_all_dies == 0)
20126 {
20127 cu->per_cu->load_all_dies = 1;
20128
20129 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20130 THIS_CU->cu may already be in use. So we can't just free it and
20131 replace its DIEs with the ones we read in. Instead, we leave those
20132 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20133 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20134 set. */
20135 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
20136
20137 pd = cu->find_partial_die (sect_off);
20138 }
20139
20140 if (pd == NULL)
20141 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
20142 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
20143 return { cu, pd };
20144 }
20145
20146 /* See if we can figure out if the class lives in a namespace. We do
20147 this by looking for a member function; its demangled name will
20148 contain namespace info, if there is any. */
20149
20150 static void
20151 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20152 struct dwarf2_cu *cu)
20153 {
20154 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20155 what template types look like, because the demangler
20156 frequently doesn't give the same name as the debug info. We
20157 could fix this by only using the demangled name to get the
20158 prefix (but see comment in read_structure_type). */
20159
20160 struct partial_die_info *real_pdi;
20161 struct partial_die_info *child_pdi;
20162
20163 /* If this DIE (this DIE's specification, if any) has a parent, then
20164 we should not do this. We'll prepend the parent's fully qualified
20165 name when we create the partial symbol. */
20166
20167 real_pdi = struct_pdi;
20168 while (real_pdi->has_specification)
20169 {
20170 auto res = find_partial_die (real_pdi->spec_offset,
20171 real_pdi->spec_is_dwz, cu);
20172 real_pdi = res.pdi;
20173 cu = res.cu;
20174 }
20175
20176 if (real_pdi->die_parent != NULL)
20177 return;
20178
20179 for (child_pdi = struct_pdi->die_child;
20180 child_pdi != NULL;
20181 child_pdi = child_pdi->die_sibling)
20182 {
20183 if (child_pdi->tag == DW_TAG_subprogram
20184 && child_pdi->linkage_name != NULL)
20185 {
20186 gdb::unique_xmalloc_ptr<char> actual_class_name
20187 (cu->language_defn->class_name_from_physname
20188 (child_pdi->linkage_name));
20189 if (actual_class_name != NULL)
20190 {
20191 struct objfile *objfile = cu->per_objfile->objfile;
20192 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20193 struct_pdi->canonical_name = 1;
20194 }
20195 break;
20196 }
20197 }
20198 }
20199
20200 /* Return true if a DIE with TAG may have the DW_AT_const_value
20201 attribute. */
20202
20203 static bool
20204 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20205 {
20206 switch (tag)
20207 {
20208 case DW_TAG_constant:
20209 case DW_TAG_enumerator:
20210 case DW_TAG_formal_parameter:
20211 case DW_TAG_template_value_param:
20212 case DW_TAG_variable:
20213 return true;
20214 }
20215
20216 return false;
20217 }
20218
20219 void
20220 partial_die_info::fixup (struct dwarf2_cu *cu)
20221 {
20222 /* Once we've fixed up a die, there's no point in doing so again.
20223 This also avoids a memory leak if we were to call
20224 guess_partial_die_structure_name multiple times. */
20225 if (fixup_called)
20226 return;
20227
20228 /* If we found a reference attribute and the DIE has no name, try
20229 to find a name in the referred to DIE. */
20230
20231 if (raw_name == NULL && has_specification)
20232 {
20233 struct partial_die_info *spec_die;
20234
20235 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20236 spec_die = res.pdi;
20237 cu = res.cu;
20238
20239 spec_die->fixup (cu);
20240
20241 if (spec_die->raw_name)
20242 {
20243 raw_name = spec_die->raw_name;
20244 canonical_name = spec_die->canonical_name;
20245
20246 /* Copy DW_AT_external attribute if it is set. */
20247 if (spec_die->is_external)
20248 is_external = spec_die->is_external;
20249 }
20250 }
20251
20252 if (!has_const_value && has_specification
20253 && can_have_DW_AT_const_value_p (tag))
20254 {
20255 struct partial_die_info *spec_die;
20256
20257 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20258 spec_die = res.pdi;
20259 cu = res.cu;
20260
20261 spec_die->fixup (cu);
20262
20263 if (spec_die->has_const_value)
20264 {
20265 /* Copy DW_AT_const_value attribute if it is set. */
20266 has_const_value = spec_die->has_const_value;
20267 }
20268 }
20269
20270 /* Set default names for some unnamed DIEs. */
20271
20272 if (raw_name == NULL && tag == DW_TAG_namespace)
20273 {
20274 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20275 canonical_name = 1;
20276 }
20277
20278 /* If there is no parent die to provide a namespace, and there are
20279 children, see if we can determine the namespace from their linkage
20280 name. */
20281 if (cu->language == language_cplus
20282 && !cu->per_objfile->per_bfd->types.empty ()
20283 && die_parent == NULL
20284 && has_children
20285 && (tag == DW_TAG_class_type
20286 || tag == DW_TAG_structure_type
20287 || tag == DW_TAG_union_type))
20288 guess_partial_die_structure_name (this, cu);
20289
20290 /* GCC might emit a nameless struct or union that has a linkage
20291 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20292 if (raw_name == NULL
20293 && (tag == DW_TAG_class_type
20294 || tag == DW_TAG_interface_type
20295 || tag == DW_TAG_structure_type
20296 || tag == DW_TAG_union_type)
20297 && linkage_name != NULL)
20298 {
20299 gdb::unique_xmalloc_ptr<char> demangled
20300 (gdb_demangle (linkage_name, DMGL_TYPES));
20301 if (demangled != nullptr)
20302 {
20303 const char *base;
20304
20305 /* Strip any leading namespaces/classes, keep only the base name.
20306 DW_AT_name for named DIEs does not contain the prefixes. */
20307 base = strrchr (demangled.get (), ':');
20308 if (base && base > demangled.get () && base[-1] == ':')
20309 base++;
20310 else
20311 base = demangled.get ();
20312
20313 struct objfile *objfile = cu->per_objfile->objfile;
20314 raw_name = objfile->intern (base);
20315 canonical_name = 1;
20316 }
20317 }
20318
20319 fixup_called = 1;
20320 }
20321
20322 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20323 contents from the given SECTION in the HEADER.
20324
20325 HEADER_OFFSET is the offset of the header in the section. */
20326 static void
20327 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20328 struct dwarf2_section_info *section,
20329 sect_offset header_offset)
20330 {
20331 unsigned int bytes_read;
20332 bfd *abfd = section->get_bfd_owner ();
20333 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20334
20335 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20336 info_ptr += bytes_read;
20337
20338 header->version = read_2_bytes (abfd, info_ptr);
20339 info_ptr += 2;
20340
20341 header->addr_size = read_1_byte (abfd, info_ptr);
20342 info_ptr += 1;
20343
20344 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20345 info_ptr += 1;
20346
20347 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20348 }
20349
20350 /* Return the DW_AT_loclists_base value for the CU. */
20351 static ULONGEST
20352 lookup_loclist_base (struct dwarf2_cu *cu)
20353 {
20354 /* For the .dwo unit, the loclist_base points to the first offset following
20355 the header. The header consists of the following entities-
20356 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20357 bit format)
20358 2. version (2 bytes)
20359 3. address size (1 byte)
20360 4. segment selector size (1 byte)
20361 5. offset entry count (4 bytes)
20362 These sizes are derived as per the DWARFv5 standard. */
20363 if (cu->dwo_unit != nullptr)
20364 {
20365 if (cu->header.initial_length_size == 4)
20366 return LOCLIST_HEADER_SIZE32;
20367 return LOCLIST_HEADER_SIZE64;
20368 }
20369 return cu->loclist_base;
20370 }
20371
20372 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20373 array of offsets in the .debug_loclists section. */
20374
20375 static sect_offset
20376 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20377 {
20378 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20379 struct objfile *objfile = per_objfile->objfile;
20380 bfd *abfd = objfile->obfd;
20381 ULONGEST loclist_header_size =
20382 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20383 : LOCLIST_HEADER_SIZE64);
20384 ULONGEST loclist_base = lookup_loclist_base (cu);
20385
20386 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20387 ULONGEST start_offset =
20388 loclist_base + loclist_index * cu->header.offset_size;
20389
20390 /* Get loclists section. */
20391 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20392
20393 /* Read the loclists section content. */
20394 section->read (objfile);
20395 if (section->buffer == NULL)
20396 error (_("DW_FORM_loclistx used without .debug_loclists "
20397 "section [in module %s]"), objfile_name (objfile));
20398
20399 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20400 so if loclist_base is smaller than the header size, we have a problem. */
20401 if (loclist_base < loclist_header_size)
20402 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20403 objfile_name (objfile));
20404
20405 /* Read the header of the loclists contribution. */
20406 struct loclists_rnglists_header header;
20407 read_loclists_rnglists_header (&header, section,
20408 (sect_offset) (loclist_base - loclist_header_size));
20409
20410 /* Verify the loclist index is valid. */
20411 if (loclist_index >= header.offset_entry_count)
20412 error (_("DW_FORM_loclistx pointing outside of "
20413 ".debug_loclists offset array [in module %s]"),
20414 objfile_name (objfile));
20415
20416 /* Validate that reading won't go beyond the end of the section. */
20417 if (start_offset + cu->header.offset_size > section->size)
20418 error (_("Reading DW_FORM_loclistx index beyond end of"
20419 ".debug_loclists section [in module %s]"),
20420 objfile_name (objfile));
20421
20422 const gdb_byte *info_ptr = section->buffer + start_offset;
20423
20424 if (cu->header.offset_size == 4)
20425 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20426 else
20427 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20428 }
20429
20430 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20431 array of offsets in the .debug_rnglists section. */
20432
20433 static sect_offset
20434 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20435 dwarf_tag tag)
20436 {
20437 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20438 struct objfile *objfile = dwarf2_per_objfile->objfile;
20439 bfd *abfd = objfile->obfd;
20440 ULONGEST rnglist_header_size =
20441 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20442 : RNGLIST_HEADER_SIZE64);
20443
20444 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20445 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20446 doesn't apply. */
20447 ULONGEST rnglist_base =
20448 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20449
20450 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20451 ULONGEST start_offset =
20452 rnglist_base + rnglist_index * cu->header.offset_size;
20453
20454 /* Get rnglists section. */
20455 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20456
20457 /* Read the rnglists section content. */
20458 section->read (objfile);
20459 if (section->buffer == nullptr)
20460 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20461 "[in module %s]"),
20462 objfile_name (objfile));
20463
20464 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20465 so if rnglist_base is smaller than the header size, we have a problem. */
20466 if (rnglist_base < rnglist_header_size)
20467 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20468 objfile_name (objfile));
20469
20470 /* Read the header of the rnglists contribution. */
20471 struct loclists_rnglists_header header;
20472 read_loclists_rnglists_header (&header, section,
20473 (sect_offset) (rnglist_base - rnglist_header_size));
20474
20475 /* Verify the rnglist index is valid. */
20476 if (rnglist_index >= header.offset_entry_count)
20477 error (_("DW_FORM_rnglistx index pointing outside of "
20478 ".debug_rnglists offset array [in module %s]"),
20479 objfile_name (objfile));
20480
20481 /* Validate that reading won't go beyond the end of the section. */
20482 if (start_offset + cu->header.offset_size > section->size)
20483 error (_("Reading DW_FORM_rnglistx index beyond end of"
20484 ".debug_rnglists section [in module %s]"),
20485 objfile_name (objfile));
20486
20487 const gdb_byte *info_ptr = section->buffer + start_offset;
20488
20489 if (cu->header.offset_size == 4)
20490 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20491 else
20492 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20493 }
20494
20495 /* Process the attributes that had to be skipped in the first round. These
20496 attributes are the ones that need str_offsets_base or addr_base attributes.
20497 They could not have been processed in the first round, because at the time
20498 the values of str_offsets_base or addr_base may not have been known. */
20499 static void
20500 read_attribute_reprocess (const struct die_reader_specs *reader,
20501 struct attribute *attr, dwarf_tag tag)
20502 {
20503 struct dwarf2_cu *cu = reader->cu;
20504 switch (attr->form)
20505 {
20506 case DW_FORM_addrx:
20507 case DW_FORM_GNU_addr_index:
20508 attr->set_address (read_addr_index (cu,
20509 attr->as_unsigned_reprocess ()));
20510 break;
20511 case DW_FORM_loclistx:
20512 {
20513 sect_offset loclists_sect_off
20514 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20515
20516 attr->set_unsigned (to_underlying (loclists_sect_off));
20517 }
20518 break;
20519 case DW_FORM_rnglistx:
20520 {
20521 sect_offset rnglists_sect_off
20522 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20523
20524 attr->set_unsigned (to_underlying (rnglists_sect_off));
20525 }
20526 break;
20527 case DW_FORM_strx:
20528 case DW_FORM_strx1:
20529 case DW_FORM_strx2:
20530 case DW_FORM_strx3:
20531 case DW_FORM_strx4:
20532 case DW_FORM_GNU_str_index:
20533 {
20534 unsigned int str_index = attr->as_unsigned_reprocess ();
20535 gdb_assert (!attr->canonical_string_p ());
20536 if (reader->dwo_file != NULL)
20537 attr->set_string_noncanonical (read_dwo_str_index (reader,
20538 str_index));
20539 else
20540 attr->set_string_noncanonical (read_stub_str_index (cu,
20541 str_index));
20542 break;
20543 }
20544 default:
20545 gdb_assert_not_reached (_("Unexpected DWARF form."));
20546 }
20547 }
20548
20549 /* Read an attribute value described by an attribute form. */
20550
20551 static const gdb_byte *
20552 read_attribute_value (const struct die_reader_specs *reader,
20553 struct attribute *attr, unsigned form,
20554 LONGEST implicit_const, const gdb_byte *info_ptr)
20555 {
20556 struct dwarf2_cu *cu = reader->cu;
20557 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20558 struct objfile *objfile = per_objfile->objfile;
20559 bfd *abfd = reader->abfd;
20560 struct comp_unit_head *cu_header = &cu->header;
20561 unsigned int bytes_read;
20562 struct dwarf_block *blk;
20563
20564 attr->form = (enum dwarf_form) form;
20565 switch (form)
20566 {
20567 case DW_FORM_ref_addr:
20568 if (cu_header->version == 2)
20569 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20570 &bytes_read));
20571 else
20572 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20573 &bytes_read));
20574 info_ptr += bytes_read;
20575 break;
20576 case DW_FORM_GNU_ref_alt:
20577 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20578 &bytes_read));
20579 info_ptr += bytes_read;
20580 break;
20581 case DW_FORM_addr:
20582 {
20583 struct gdbarch *gdbarch = objfile->arch ();
20584 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20585 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20586 attr->set_address (addr);
20587 info_ptr += bytes_read;
20588 }
20589 break;
20590 case DW_FORM_block2:
20591 blk = dwarf_alloc_block (cu);
20592 blk->size = read_2_bytes (abfd, info_ptr);
20593 info_ptr += 2;
20594 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20595 info_ptr += blk->size;
20596 attr->set_block (blk);
20597 break;
20598 case DW_FORM_block4:
20599 blk = dwarf_alloc_block (cu);
20600 blk->size = read_4_bytes (abfd, info_ptr);
20601 info_ptr += 4;
20602 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20603 info_ptr += blk->size;
20604 attr->set_block (blk);
20605 break;
20606 case DW_FORM_data2:
20607 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20608 info_ptr += 2;
20609 break;
20610 case DW_FORM_data4:
20611 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20612 info_ptr += 4;
20613 break;
20614 case DW_FORM_data8:
20615 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20616 info_ptr += 8;
20617 break;
20618 case DW_FORM_data16:
20619 blk = dwarf_alloc_block (cu);
20620 blk->size = 16;
20621 blk->data = read_n_bytes (abfd, info_ptr, 16);
20622 info_ptr += 16;
20623 attr->set_block (blk);
20624 break;
20625 case DW_FORM_sec_offset:
20626 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20627 &bytes_read));
20628 info_ptr += bytes_read;
20629 break;
20630 case DW_FORM_loclistx:
20631 {
20632 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20633 &bytes_read));
20634 info_ptr += bytes_read;
20635 }
20636 break;
20637 case DW_FORM_string:
20638 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20639 &bytes_read));
20640 info_ptr += bytes_read;
20641 break;
20642 case DW_FORM_strp:
20643 if (!cu->per_cu->is_dwz)
20644 {
20645 attr->set_string_noncanonical
20646 (read_indirect_string (per_objfile,
20647 abfd, info_ptr, cu_header,
20648 &bytes_read));
20649 info_ptr += bytes_read;
20650 break;
20651 }
20652 /* FALLTHROUGH */
20653 case DW_FORM_line_strp:
20654 if (!cu->per_cu->is_dwz)
20655 {
20656 attr->set_string_noncanonical
20657 (per_objfile->read_line_string (info_ptr, cu_header,
20658 &bytes_read));
20659 info_ptr += bytes_read;
20660 break;
20661 }
20662 /* FALLTHROUGH */
20663 case DW_FORM_GNU_strp_alt:
20664 {
20665 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20666 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20667 &bytes_read);
20668
20669 attr->set_string_noncanonical
20670 (dwz->read_string (objfile, str_offset));
20671 info_ptr += bytes_read;
20672 }
20673 break;
20674 case DW_FORM_exprloc:
20675 case DW_FORM_block:
20676 blk = dwarf_alloc_block (cu);
20677 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20678 info_ptr += bytes_read;
20679 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20680 info_ptr += blk->size;
20681 attr->set_block (blk);
20682 break;
20683 case DW_FORM_block1:
20684 blk = dwarf_alloc_block (cu);
20685 blk->size = read_1_byte (abfd, info_ptr);
20686 info_ptr += 1;
20687 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20688 info_ptr += blk->size;
20689 attr->set_block (blk);
20690 break;
20691 case DW_FORM_data1:
20692 case DW_FORM_flag:
20693 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20694 info_ptr += 1;
20695 break;
20696 case DW_FORM_flag_present:
20697 attr->set_unsigned (1);
20698 break;
20699 case DW_FORM_sdata:
20700 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20701 info_ptr += bytes_read;
20702 break;
20703 case DW_FORM_rnglistx:
20704 {
20705 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20706 &bytes_read));
20707 info_ptr += bytes_read;
20708 }
20709 break;
20710 case DW_FORM_udata:
20711 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20712 info_ptr += bytes_read;
20713 break;
20714 case DW_FORM_ref1:
20715 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20716 + read_1_byte (abfd, info_ptr)));
20717 info_ptr += 1;
20718 break;
20719 case DW_FORM_ref2:
20720 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20721 + read_2_bytes (abfd, info_ptr)));
20722 info_ptr += 2;
20723 break;
20724 case DW_FORM_ref4:
20725 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20726 + read_4_bytes (abfd, info_ptr)));
20727 info_ptr += 4;
20728 break;
20729 case DW_FORM_ref8:
20730 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20731 + read_8_bytes (abfd, info_ptr)));
20732 info_ptr += 8;
20733 break;
20734 case DW_FORM_ref_sig8:
20735 attr->set_signature (read_8_bytes (abfd, info_ptr));
20736 info_ptr += 8;
20737 break;
20738 case DW_FORM_ref_udata:
20739 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20740 + read_unsigned_leb128 (abfd, info_ptr,
20741 &bytes_read)));
20742 info_ptr += bytes_read;
20743 break;
20744 case DW_FORM_indirect:
20745 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20746 info_ptr += bytes_read;
20747 if (form == DW_FORM_implicit_const)
20748 {
20749 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20750 info_ptr += bytes_read;
20751 }
20752 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20753 info_ptr);
20754 break;
20755 case DW_FORM_implicit_const:
20756 attr->set_signed (implicit_const);
20757 break;
20758 case DW_FORM_addrx:
20759 case DW_FORM_GNU_addr_index:
20760 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20761 &bytes_read));
20762 info_ptr += bytes_read;
20763 break;
20764 case DW_FORM_strx:
20765 case DW_FORM_strx1:
20766 case DW_FORM_strx2:
20767 case DW_FORM_strx3:
20768 case DW_FORM_strx4:
20769 case DW_FORM_GNU_str_index:
20770 {
20771 ULONGEST str_index;
20772 if (form == DW_FORM_strx1)
20773 {
20774 str_index = read_1_byte (abfd, info_ptr);
20775 info_ptr += 1;
20776 }
20777 else if (form == DW_FORM_strx2)
20778 {
20779 str_index = read_2_bytes (abfd, info_ptr);
20780 info_ptr += 2;
20781 }
20782 else if (form == DW_FORM_strx3)
20783 {
20784 str_index = read_3_bytes (abfd, info_ptr);
20785 info_ptr += 3;
20786 }
20787 else if (form == DW_FORM_strx4)
20788 {
20789 str_index = read_4_bytes (abfd, info_ptr);
20790 info_ptr += 4;
20791 }
20792 else
20793 {
20794 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20795 info_ptr += bytes_read;
20796 }
20797 attr->set_unsigned_reprocess (str_index);
20798 }
20799 break;
20800 default:
20801 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20802 dwarf_form_name (form),
20803 bfd_get_filename (abfd));
20804 }
20805
20806 /* Super hack. */
20807 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20808 attr->form = DW_FORM_GNU_ref_alt;
20809
20810 /* We have seen instances where the compiler tried to emit a byte
20811 size attribute of -1 which ended up being encoded as an unsigned
20812 0xffffffff. Although 0xffffffff is technically a valid size value,
20813 an object of this size seems pretty unlikely so we can relatively
20814 safely treat these cases as if the size attribute was invalid and
20815 treat them as zero by default. */
20816 if (attr->name == DW_AT_byte_size
20817 && form == DW_FORM_data4
20818 && attr->as_unsigned () >= 0xffffffff)
20819 {
20820 complaint
20821 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20822 hex_string (attr->as_unsigned ()));
20823 attr->set_unsigned (0);
20824 }
20825
20826 return info_ptr;
20827 }
20828
20829 /* Read an attribute described by an abbreviated attribute. */
20830
20831 static const gdb_byte *
20832 read_attribute (const struct die_reader_specs *reader,
20833 struct attribute *attr, const struct attr_abbrev *abbrev,
20834 const gdb_byte *info_ptr)
20835 {
20836 attr->name = abbrev->name;
20837 attr->string_is_canonical = 0;
20838 attr->requires_reprocessing = 0;
20839 return read_attribute_value (reader, attr, abbrev->form,
20840 abbrev->implicit_const, info_ptr);
20841 }
20842
20843 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20844
20845 static const char *
20846 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20847 LONGEST str_offset)
20848 {
20849 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20850 str_offset, "DW_FORM_strp");
20851 }
20852
20853 /* Return pointer to string at .debug_str offset as read from BUF.
20854 BUF is assumed to be in a compilation unit described by CU_HEADER.
20855 Return *BYTES_READ_PTR count of bytes read from BUF. */
20856
20857 static const char *
20858 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20859 const gdb_byte *buf,
20860 const struct comp_unit_head *cu_header,
20861 unsigned int *bytes_read_ptr)
20862 {
20863 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20864
20865 return read_indirect_string_at_offset (per_objfile, str_offset);
20866 }
20867
20868 /* See read.h. */
20869
20870 const char *
20871 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20872 const struct comp_unit_head *cu_header,
20873 unsigned int *bytes_read_ptr)
20874 {
20875 bfd *abfd = objfile->obfd;
20876 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20877
20878 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20879 }
20880
20881 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20882 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20883 ADDR_SIZE is the size of addresses from the CU header. */
20884
20885 static CORE_ADDR
20886 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20887 gdb::optional<ULONGEST> addr_base, int addr_size)
20888 {
20889 struct objfile *objfile = per_objfile->objfile;
20890 bfd *abfd = objfile->obfd;
20891 const gdb_byte *info_ptr;
20892 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20893
20894 per_objfile->per_bfd->addr.read (objfile);
20895 if (per_objfile->per_bfd->addr.buffer == NULL)
20896 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20897 objfile_name (objfile));
20898 if (addr_base_or_zero + addr_index * addr_size
20899 >= per_objfile->per_bfd->addr.size)
20900 error (_("DW_FORM_addr_index pointing outside of "
20901 ".debug_addr section [in module %s]"),
20902 objfile_name (objfile));
20903 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20904 + addr_index * addr_size);
20905 if (addr_size == 4)
20906 return bfd_get_32 (abfd, info_ptr);
20907 else
20908 return bfd_get_64 (abfd, info_ptr);
20909 }
20910
20911 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20912
20913 static CORE_ADDR
20914 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20915 {
20916 return read_addr_index_1 (cu->per_objfile, addr_index,
20917 cu->addr_base, cu->header.addr_size);
20918 }
20919
20920 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20921
20922 static CORE_ADDR
20923 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20924 unsigned int *bytes_read)
20925 {
20926 bfd *abfd = cu->per_objfile->objfile->obfd;
20927 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20928
20929 return read_addr_index (cu, addr_index);
20930 }
20931
20932 /* See read.h. */
20933
20934 CORE_ADDR
20935 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20936 dwarf2_per_objfile *per_objfile,
20937 unsigned int addr_index)
20938 {
20939 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20940 gdb::optional<ULONGEST> addr_base;
20941 int addr_size;
20942
20943 /* We need addr_base and addr_size.
20944 If we don't have PER_CU->cu, we have to get it.
20945 Nasty, but the alternative is storing the needed info in PER_CU,
20946 which at this point doesn't seem justified: it's not clear how frequently
20947 it would get used and it would increase the size of every PER_CU.
20948 Entry points like dwarf2_per_cu_addr_size do a similar thing
20949 so we're not in uncharted territory here.
20950 Alas we need to be a bit more complicated as addr_base is contained
20951 in the DIE.
20952
20953 We don't need to read the entire CU(/TU).
20954 We just need the header and top level die.
20955
20956 IWBN to use the aging mechanism to let us lazily later discard the CU.
20957 For now we skip this optimization. */
20958
20959 if (cu != NULL)
20960 {
20961 addr_base = cu->addr_base;
20962 addr_size = cu->header.addr_size;
20963 }
20964 else
20965 {
20966 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20967 addr_base = reader.cu->addr_base;
20968 addr_size = reader.cu->header.addr_size;
20969 }
20970
20971 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20972 }
20973
20974 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20975 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20976 DWO file. */
20977
20978 static const char *
20979 read_str_index (struct dwarf2_cu *cu,
20980 struct dwarf2_section_info *str_section,
20981 struct dwarf2_section_info *str_offsets_section,
20982 ULONGEST str_offsets_base, ULONGEST str_index)
20983 {
20984 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20985 struct objfile *objfile = per_objfile->objfile;
20986 const char *objf_name = objfile_name (objfile);
20987 bfd *abfd = objfile->obfd;
20988 const gdb_byte *info_ptr;
20989 ULONGEST str_offset;
20990 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20991
20992 str_section->read (objfile);
20993 str_offsets_section->read (objfile);
20994 if (str_section->buffer == NULL)
20995 error (_("%s used without %s section"
20996 " in CU at offset %s [in module %s]"),
20997 form_name, str_section->get_name (),
20998 sect_offset_str (cu->header.sect_off), objf_name);
20999 if (str_offsets_section->buffer == NULL)
21000 error (_("%s used without %s section"
21001 " in CU at offset %s [in module %s]"),
21002 form_name, str_section->get_name (),
21003 sect_offset_str (cu->header.sect_off), objf_name);
21004 info_ptr = (str_offsets_section->buffer
21005 + str_offsets_base
21006 + str_index * cu->header.offset_size);
21007 if (cu->header.offset_size == 4)
21008 str_offset = bfd_get_32 (abfd, info_ptr);
21009 else
21010 str_offset = bfd_get_64 (abfd, info_ptr);
21011 if (str_offset >= str_section->size)
21012 error (_("Offset from %s pointing outside of"
21013 " .debug_str.dwo section in CU at offset %s [in module %s]"),
21014 form_name, sect_offset_str (cu->header.sect_off), objf_name);
21015 return (const char *) (str_section->buffer + str_offset);
21016 }
21017
21018 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
21019
21020 static const char *
21021 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
21022 {
21023 ULONGEST str_offsets_base = reader->cu->header.version >= 5
21024 ? reader->cu->header.addr_size : 0;
21025 return read_str_index (reader->cu,
21026 &reader->dwo_file->sections.str,
21027 &reader->dwo_file->sections.str_offsets,
21028 str_offsets_base, str_index);
21029 }
21030
21031 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
21032
21033 static const char *
21034 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
21035 {
21036 struct objfile *objfile = cu->per_objfile->objfile;
21037 const char *objf_name = objfile_name (objfile);
21038 static const char form_name[] = "DW_FORM_GNU_str_index";
21039 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
21040
21041 if (!cu->str_offsets_base.has_value ())
21042 error (_("%s used in Fission stub without %s"
21043 " in CU at offset 0x%lx [in module %s]"),
21044 form_name, str_offsets_attr_name,
21045 (long) cu->header.offset_size, objf_name);
21046
21047 return read_str_index (cu,
21048 &cu->per_objfile->per_bfd->str,
21049 &cu->per_objfile->per_bfd->str_offsets,
21050 *cu->str_offsets_base, str_index);
21051 }
21052
21053 /* Return the length of an LEB128 number in BUF. */
21054
21055 static int
21056 leb128_size (const gdb_byte *buf)
21057 {
21058 const gdb_byte *begin = buf;
21059 gdb_byte byte;
21060
21061 while (1)
21062 {
21063 byte = *buf++;
21064 if ((byte & 128) == 0)
21065 return buf - begin;
21066 }
21067 }
21068
21069 static void
21070 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
21071 {
21072 switch (lang)
21073 {
21074 case DW_LANG_C89:
21075 case DW_LANG_C99:
21076 case DW_LANG_C11:
21077 case DW_LANG_C:
21078 case DW_LANG_UPC:
21079 cu->language = language_c;
21080 break;
21081 case DW_LANG_Java:
21082 case DW_LANG_C_plus_plus:
21083 case DW_LANG_C_plus_plus_11:
21084 case DW_LANG_C_plus_plus_14:
21085 cu->language = language_cplus;
21086 break;
21087 case DW_LANG_D:
21088 cu->language = language_d;
21089 break;
21090 case DW_LANG_Fortran77:
21091 case DW_LANG_Fortran90:
21092 case DW_LANG_Fortran95:
21093 case DW_LANG_Fortran03:
21094 case DW_LANG_Fortran08:
21095 cu->language = language_fortran;
21096 break;
21097 case DW_LANG_Go:
21098 cu->language = language_go;
21099 break;
21100 case DW_LANG_Mips_Assembler:
21101 cu->language = language_asm;
21102 break;
21103 case DW_LANG_Ada83:
21104 case DW_LANG_Ada95:
21105 cu->language = language_ada;
21106 break;
21107 case DW_LANG_Modula2:
21108 cu->language = language_m2;
21109 break;
21110 case DW_LANG_Pascal83:
21111 cu->language = language_pascal;
21112 break;
21113 case DW_LANG_ObjC:
21114 cu->language = language_objc;
21115 break;
21116 case DW_LANG_Rust:
21117 case DW_LANG_Rust_old:
21118 cu->language = language_rust;
21119 break;
21120 case DW_LANG_Cobol74:
21121 case DW_LANG_Cobol85:
21122 default:
21123 cu->language = language_minimal;
21124 break;
21125 }
21126 cu->language_defn = language_def (cu->language);
21127 }
21128
21129 /* Return the named attribute or NULL if not there. */
21130
21131 static struct attribute *
21132 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21133 {
21134 for (;;)
21135 {
21136 unsigned int i;
21137 struct attribute *spec = NULL;
21138
21139 for (i = 0; i < die->num_attrs; ++i)
21140 {
21141 if (die->attrs[i].name == name)
21142 return &die->attrs[i];
21143 if (die->attrs[i].name == DW_AT_specification
21144 || die->attrs[i].name == DW_AT_abstract_origin)
21145 spec = &die->attrs[i];
21146 }
21147
21148 if (!spec)
21149 break;
21150
21151 die = follow_die_ref (die, spec, &cu);
21152 }
21153
21154 return NULL;
21155 }
21156
21157 /* Return the string associated with a string-typed attribute, or NULL if it
21158 is either not found or is of an incorrect type. */
21159
21160 static const char *
21161 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21162 {
21163 struct attribute *attr;
21164 const char *str = NULL;
21165
21166 attr = dwarf2_attr (die, name, cu);
21167
21168 if (attr != NULL)
21169 {
21170 str = attr->as_string ();
21171 if (str == nullptr)
21172 complaint (_("string type expected for attribute %s for "
21173 "DIE at %s in module %s"),
21174 dwarf_attr_name (name), sect_offset_str (die->sect_off),
21175 objfile_name (cu->per_objfile->objfile));
21176 }
21177
21178 return str;
21179 }
21180
21181 /* Return the dwo name or NULL if not present. If present, it is in either
21182 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
21183 static const char *
21184 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21185 {
21186 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21187 if (dwo_name == nullptr)
21188 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21189 return dwo_name;
21190 }
21191
21192 /* Return non-zero iff the attribute NAME is defined for the given DIE,
21193 and holds a non-zero value. This function should only be used for
21194 DW_FORM_flag or DW_FORM_flag_present attributes. */
21195
21196 static int
21197 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21198 {
21199 struct attribute *attr = dwarf2_attr (die, name, cu);
21200
21201 return attr != nullptr && attr->as_boolean ();
21202 }
21203
21204 static int
21205 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
21206 {
21207 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21208 which value is non-zero. However, we have to be careful with
21209 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21210 (via dwarf2_flag_true_p) follows this attribute. So we may
21211 end up accidently finding a declaration attribute that belongs
21212 to a different DIE referenced by the specification attribute,
21213 even though the given DIE does not have a declaration attribute. */
21214 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21215 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
21216 }
21217
21218 /* Return the die giving the specification for DIE, if there is
21219 one. *SPEC_CU is the CU containing DIE on input, and the CU
21220 containing the return value on output. If there is no
21221 specification, but there is an abstract origin, that is
21222 returned. */
21223
21224 static struct die_info *
21225 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
21226 {
21227 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21228 *spec_cu);
21229
21230 if (spec_attr == NULL)
21231 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21232
21233 if (spec_attr == NULL)
21234 return NULL;
21235 else
21236 return follow_die_ref (die, spec_attr, spec_cu);
21237 }
21238
21239 /* Stub for free_line_header to match void * callback types. */
21240
21241 static void
21242 free_line_header_voidp (void *arg)
21243 {
21244 struct line_header *lh = (struct line_header *) arg;
21245
21246 delete lh;
21247 }
21248
21249 /* A convenience function to find the proper .debug_line section for a CU. */
21250
21251 static struct dwarf2_section_info *
21252 get_debug_line_section (struct dwarf2_cu *cu)
21253 {
21254 struct dwarf2_section_info *section;
21255 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21256
21257 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21258 DWO file. */
21259 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21260 section = &cu->dwo_unit->dwo_file->sections.line;
21261 else if (cu->per_cu->is_dwz)
21262 {
21263 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
21264
21265 section = &dwz->line;
21266 }
21267 else
21268 section = &per_objfile->per_bfd->line;
21269
21270 return section;
21271 }
21272
21273 /* Read the statement program header starting at OFFSET in
21274 .debug_line, or .debug_line.dwo. Return a pointer
21275 to a struct line_header, allocated using xmalloc.
21276 Returns NULL if there is a problem reading the header, e.g., if it
21277 has a version we don't understand.
21278
21279 NOTE: the strings in the include directory and file name tables of
21280 the returned object point into the dwarf line section buffer,
21281 and must not be freed. */
21282
21283 static line_header_up
21284 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21285 {
21286 struct dwarf2_section_info *section;
21287 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21288
21289 section = get_debug_line_section (cu);
21290 section->read (per_objfile->objfile);
21291 if (section->buffer == NULL)
21292 {
21293 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21294 complaint (_("missing .debug_line.dwo section"));
21295 else
21296 complaint (_("missing .debug_line section"));
21297 return 0;
21298 }
21299
21300 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21301 per_objfile, section, &cu->header);
21302 }
21303
21304 /* Subroutine of dwarf_decode_lines to simplify it.
21305 Return the file name of the psymtab for the given file_entry.
21306 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21307 If space for the result is malloc'd, *NAME_HOLDER will be set.
21308 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21309
21310 static const char *
21311 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21312 const dwarf2_psymtab *pst,
21313 const char *comp_dir,
21314 gdb::unique_xmalloc_ptr<char> *name_holder)
21315 {
21316 const char *include_name = fe.name;
21317 const char *include_name_to_compare = include_name;
21318 const char *pst_filename;
21319 int file_is_pst;
21320
21321 const char *dir_name = fe.include_dir (lh);
21322
21323 gdb::unique_xmalloc_ptr<char> hold_compare;
21324 if (!IS_ABSOLUTE_PATH (include_name)
21325 && (dir_name != NULL || comp_dir != NULL))
21326 {
21327 /* Avoid creating a duplicate psymtab for PST.
21328 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21329 Before we do the comparison, however, we need to account
21330 for DIR_NAME and COMP_DIR.
21331 First prepend dir_name (if non-NULL). If we still don't
21332 have an absolute path prepend comp_dir (if non-NULL).
21333 However, the directory we record in the include-file's
21334 psymtab does not contain COMP_DIR (to match the
21335 corresponding symtab(s)).
21336
21337 Example:
21338
21339 bash$ cd /tmp
21340 bash$ gcc -g ./hello.c
21341 include_name = "hello.c"
21342 dir_name = "."
21343 DW_AT_comp_dir = comp_dir = "/tmp"
21344 DW_AT_name = "./hello.c"
21345
21346 */
21347
21348 if (dir_name != NULL)
21349 {
21350 name_holder->reset (concat (dir_name, SLASH_STRING,
21351 include_name, (char *) NULL));
21352 include_name = name_holder->get ();
21353 include_name_to_compare = include_name;
21354 }
21355 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21356 {
21357 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21358 include_name, (char *) NULL));
21359 include_name_to_compare = hold_compare.get ();
21360 }
21361 }
21362
21363 pst_filename = pst->filename;
21364 gdb::unique_xmalloc_ptr<char> copied_name;
21365 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21366 {
21367 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21368 pst_filename, (char *) NULL));
21369 pst_filename = copied_name.get ();
21370 }
21371
21372 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21373
21374 if (file_is_pst)
21375 return NULL;
21376 return include_name;
21377 }
21378
21379 /* State machine to track the state of the line number program. */
21380
21381 class lnp_state_machine
21382 {
21383 public:
21384 /* Initialize a machine state for the start of a line number
21385 program. */
21386 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21387 bool record_lines_p);
21388
21389 file_entry *current_file ()
21390 {
21391 /* lh->file_names is 0-based, but the file name numbers in the
21392 statement program are 1-based. */
21393 return m_line_header->file_name_at (m_file);
21394 }
21395
21396 /* Record the line in the state machine. END_SEQUENCE is true if
21397 we're processing the end of a sequence. */
21398 void record_line (bool end_sequence);
21399
21400 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21401 nop-out rest of the lines in this sequence. */
21402 void check_line_address (struct dwarf2_cu *cu,
21403 const gdb_byte *line_ptr,
21404 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21405
21406 void handle_set_discriminator (unsigned int discriminator)
21407 {
21408 m_discriminator = discriminator;
21409 m_line_has_non_zero_discriminator |= discriminator != 0;
21410 }
21411
21412 /* Handle DW_LNE_set_address. */
21413 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21414 {
21415 m_op_index = 0;
21416 address += baseaddr;
21417 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21418 }
21419
21420 /* Handle DW_LNS_advance_pc. */
21421 void handle_advance_pc (CORE_ADDR adjust);
21422
21423 /* Handle a special opcode. */
21424 void handle_special_opcode (unsigned char op_code);
21425
21426 /* Handle DW_LNS_advance_line. */
21427 void handle_advance_line (int line_delta)
21428 {
21429 advance_line (line_delta);
21430 }
21431
21432 /* Handle DW_LNS_set_file. */
21433 void handle_set_file (file_name_index file);
21434
21435 /* Handle DW_LNS_negate_stmt. */
21436 void handle_negate_stmt ()
21437 {
21438 m_is_stmt = !m_is_stmt;
21439 }
21440
21441 /* Handle DW_LNS_const_add_pc. */
21442 void handle_const_add_pc ();
21443
21444 /* Handle DW_LNS_fixed_advance_pc. */
21445 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21446 {
21447 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21448 m_op_index = 0;
21449 }
21450
21451 /* Handle DW_LNS_copy. */
21452 void handle_copy ()
21453 {
21454 record_line (false);
21455 m_discriminator = 0;
21456 }
21457
21458 /* Handle DW_LNE_end_sequence. */
21459 void handle_end_sequence ()
21460 {
21461 m_currently_recording_lines = true;
21462 }
21463
21464 private:
21465 /* Advance the line by LINE_DELTA. */
21466 void advance_line (int line_delta)
21467 {
21468 m_line += line_delta;
21469
21470 if (line_delta != 0)
21471 m_line_has_non_zero_discriminator = m_discriminator != 0;
21472 }
21473
21474 struct dwarf2_cu *m_cu;
21475
21476 gdbarch *m_gdbarch;
21477
21478 /* True if we're recording lines.
21479 Otherwise we're building partial symtabs and are just interested in
21480 finding include files mentioned by the line number program. */
21481 bool m_record_lines_p;
21482
21483 /* The line number header. */
21484 line_header *m_line_header;
21485
21486 /* These are part of the standard DWARF line number state machine,
21487 and initialized according to the DWARF spec. */
21488
21489 unsigned char m_op_index = 0;
21490 /* The line table index of the current file. */
21491 file_name_index m_file = 1;
21492 unsigned int m_line = 1;
21493
21494 /* These are initialized in the constructor. */
21495
21496 CORE_ADDR m_address;
21497 bool m_is_stmt;
21498 unsigned int m_discriminator;
21499
21500 /* Additional bits of state we need to track. */
21501
21502 /* The last file that we called dwarf2_start_subfile for.
21503 This is only used for TLLs. */
21504 unsigned int m_last_file = 0;
21505 /* The last file a line number was recorded for. */
21506 struct subfile *m_last_subfile = NULL;
21507
21508 /* The address of the last line entry. */
21509 CORE_ADDR m_last_address;
21510
21511 /* Set to true when a previous line at the same address (using
21512 m_last_address) had m_is_stmt true. This is reset to false when a
21513 line entry at a new address (m_address different to m_last_address) is
21514 processed. */
21515 bool m_stmt_at_address = false;
21516
21517 /* When true, record the lines we decode. */
21518 bool m_currently_recording_lines = false;
21519
21520 /* The last line number that was recorded, used to coalesce
21521 consecutive entries for the same line. This can happen, for
21522 example, when discriminators are present. PR 17276. */
21523 unsigned int m_last_line = 0;
21524 bool m_line_has_non_zero_discriminator = false;
21525 };
21526
21527 void
21528 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21529 {
21530 CORE_ADDR addr_adj = (((m_op_index + adjust)
21531 / m_line_header->maximum_ops_per_instruction)
21532 * m_line_header->minimum_instruction_length);
21533 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21534 m_op_index = ((m_op_index + adjust)
21535 % m_line_header->maximum_ops_per_instruction);
21536 }
21537
21538 void
21539 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21540 {
21541 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21542 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21543 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21544 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21545 / m_line_header->maximum_ops_per_instruction)
21546 * m_line_header->minimum_instruction_length);
21547 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21548 m_op_index = ((m_op_index + adj_opcode_d)
21549 % m_line_header->maximum_ops_per_instruction);
21550
21551 int line_delta = m_line_header->line_base + adj_opcode_r;
21552 advance_line (line_delta);
21553 record_line (false);
21554 m_discriminator = 0;
21555 }
21556
21557 void
21558 lnp_state_machine::handle_set_file (file_name_index file)
21559 {
21560 m_file = file;
21561
21562 const file_entry *fe = current_file ();
21563 if (fe == NULL)
21564 dwarf2_debug_line_missing_file_complaint ();
21565 else if (m_record_lines_p)
21566 {
21567 const char *dir = fe->include_dir (m_line_header);
21568
21569 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21570 m_line_has_non_zero_discriminator = m_discriminator != 0;
21571 dwarf2_start_subfile (m_cu, fe->name, dir);
21572 }
21573 }
21574
21575 void
21576 lnp_state_machine::handle_const_add_pc ()
21577 {
21578 CORE_ADDR adjust
21579 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21580
21581 CORE_ADDR addr_adj
21582 = (((m_op_index + adjust)
21583 / m_line_header->maximum_ops_per_instruction)
21584 * m_line_header->minimum_instruction_length);
21585
21586 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21587 m_op_index = ((m_op_index + adjust)
21588 % m_line_header->maximum_ops_per_instruction);
21589 }
21590
21591 /* Return non-zero if we should add LINE to the line number table.
21592 LINE is the line to add, LAST_LINE is the last line that was added,
21593 LAST_SUBFILE is the subfile for LAST_LINE.
21594 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21595 had a non-zero discriminator.
21596
21597 We have to be careful in the presence of discriminators.
21598 E.g., for this line:
21599
21600 for (i = 0; i < 100000; i++);
21601
21602 clang can emit four line number entries for that one line,
21603 each with a different discriminator.
21604 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21605
21606 However, we want gdb to coalesce all four entries into one.
21607 Otherwise the user could stepi into the middle of the line and
21608 gdb would get confused about whether the pc really was in the
21609 middle of the line.
21610
21611 Things are further complicated by the fact that two consecutive
21612 line number entries for the same line is a heuristic used by gcc
21613 to denote the end of the prologue. So we can't just discard duplicate
21614 entries, we have to be selective about it. The heuristic we use is
21615 that we only collapse consecutive entries for the same line if at least
21616 one of those entries has a non-zero discriminator. PR 17276.
21617
21618 Note: Addresses in the line number state machine can never go backwards
21619 within one sequence, thus this coalescing is ok. */
21620
21621 static int
21622 dwarf_record_line_p (struct dwarf2_cu *cu,
21623 unsigned int line, unsigned int last_line,
21624 int line_has_non_zero_discriminator,
21625 struct subfile *last_subfile)
21626 {
21627 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21628 return 1;
21629 if (line != last_line)
21630 return 1;
21631 /* Same line for the same file that we've seen already.
21632 As a last check, for pr 17276, only record the line if the line
21633 has never had a non-zero discriminator. */
21634 if (!line_has_non_zero_discriminator)
21635 return 1;
21636 return 0;
21637 }
21638
21639 /* Use the CU's builder to record line number LINE beginning at
21640 address ADDRESS in the line table of subfile SUBFILE. */
21641
21642 static void
21643 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21644 unsigned int line, CORE_ADDR address, bool is_stmt,
21645 struct dwarf2_cu *cu)
21646 {
21647 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21648
21649 if (dwarf_line_debug)
21650 {
21651 fprintf_unfiltered (gdb_stdlog,
21652 "Recording line %u, file %s, address %s\n",
21653 line, lbasename (subfile->name),
21654 paddress (gdbarch, address));
21655 }
21656
21657 if (cu != nullptr)
21658 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21659 }
21660
21661 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21662 Mark the end of a set of line number records.
21663 The arguments are the same as for dwarf_record_line_1.
21664 If SUBFILE is NULL the request is ignored. */
21665
21666 static void
21667 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21668 CORE_ADDR address, struct dwarf2_cu *cu)
21669 {
21670 if (subfile == NULL)
21671 return;
21672
21673 if (dwarf_line_debug)
21674 {
21675 fprintf_unfiltered (gdb_stdlog,
21676 "Finishing current line, file %s, address %s\n",
21677 lbasename (subfile->name),
21678 paddress (gdbarch, address));
21679 }
21680
21681 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21682 }
21683
21684 void
21685 lnp_state_machine::record_line (bool end_sequence)
21686 {
21687 if (dwarf_line_debug)
21688 {
21689 fprintf_unfiltered (gdb_stdlog,
21690 "Processing actual line %u: file %u,"
21691 " address %s, is_stmt %u, discrim %u%s\n",
21692 m_line, m_file,
21693 paddress (m_gdbarch, m_address),
21694 m_is_stmt, m_discriminator,
21695 (end_sequence ? "\t(end sequence)" : ""));
21696 }
21697
21698 file_entry *fe = current_file ();
21699
21700 if (fe == NULL)
21701 dwarf2_debug_line_missing_file_complaint ();
21702 /* For now we ignore lines not starting on an instruction boundary.
21703 But not when processing end_sequence for compatibility with the
21704 previous version of the code. */
21705 else if (m_op_index == 0 || end_sequence)
21706 {
21707 fe->included_p = 1;
21708 if (m_record_lines_p)
21709 {
21710 /* When we switch files we insert an end maker in the first file,
21711 switch to the second file and add a new line entry. The
21712 problem is that the end marker inserted in the first file will
21713 discard any previous line entries at the same address. If the
21714 line entries in the first file are marked as is-stmt, while
21715 the new line in the second file is non-stmt, then this means
21716 the end marker will discard is-stmt lines so we can have a
21717 non-stmt line. This means that there are less addresses at
21718 which the user can insert a breakpoint.
21719
21720 To improve this we track the last address in m_last_address,
21721 and whether we have seen an is-stmt at this address. Then
21722 when switching files, if we have seen a stmt at the current
21723 address, and we are switching to create a non-stmt line, then
21724 discard the new line. */
21725 bool file_changed
21726 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21727 bool ignore_this_line
21728 = ((file_changed && !end_sequence && m_last_address == m_address
21729 && !m_is_stmt && m_stmt_at_address)
21730 || (!end_sequence && m_line == 0));
21731
21732 if ((file_changed && !ignore_this_line) || end_sequence)
21733 {
21734 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21735 m_currently_recording_lines ? m_cu : nullptr);
21736 }
21737
21738 if (!end_sequence && !ignore_this_line)
21739 {
21740 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21741
21742 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21743 m_line_has_non_zero_discriminator,
21744 m_last_subfile))
21745 {
21746 buildsym_compunit *builder = m_cu->get_builder ();
21747 dwarf_record_line_1 (m_gdbarch,
21748 builder->get_current_subfile (),
21749 m_line, m_address, is_stmt,
21750 m_currently_recording_lines ? m_cu : nullptr);
21751 }
21752 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21753 m_last_line = m_line;
21754 }
21755 }
21756 }
21757
21758 /* Track whether we have seen any m_is_stmt true at m_address in case we
21759 have multiple line table entries all at m_address. */
21760 if (m_last_address != m_address)
21761 {
21762 m_stmt_at_address = false;
21763 m_last_address = m_address;
21764 }
21765 m_stmt_at_address |= m_is_stmt;
21766 }
21767
21768 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21769 line_header *lh, bool record_lines_p)
21770 {
21771 m_cu = cu;
21772 m_gdbarch = arch;
21773 m_record_lines_p = record_lines_p;
21774 m_line_header = lh;
21775
21776 m_currently_recording_lines = true;
21777
21778 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21779 was a line entry for it so that the backend has a chance to adjust it
21780 and also record it in case it needs it. This is currently used by MIPS
21781 code, cf. `mips_adjust_dwarf2_line'. */
21782 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21783 m_is_stmt = lh->default_is_stmt;
21784 m_discriminator = 0;
21785
21786 m_last_address = m_address;
21787 m_stmt_at_address = false;
21788 }
21789
21790 void
21791 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21792 const gdb_byte *line_ptr,
21793 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21794 {
21795 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21796 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21797 located at 0x0. In this case, additionally check that if
21798 ADDRESS < UNRELOCATED_LOWPC. */
21799
21800 if ((address == 0 && address < unrelocated_lowpc)
21801 || address == (CORE_ADDR) -1)
21802 {
21803 /* This line table is for a function which has been
21804 GCd by the linker. Ignore it. PR gdb/12528 */
21805
21806 struct objfile *objfile = cu->per_objfile->objfile;
21807 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21808
21809 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21810 line_offset, objfile_name (objfile));
21811 m_currently_recording_lines = false;
21812 /* Note: m_currently_recording_lines is left as false until we see
21813 DW_LNE_end_sequence. */
21814 }
21815 }
21816
21817 /* Subroutine of dwarf_decode_lines to simplify it.
21818 Process the line number information in LH.
21819 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21820 program in order to set included_p for every referenced header. */
21821
21822 static void
21823 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21824 const int decode_for_pst_p, CORE_ADDR lowpc)
21825 {
21826 const gdb_byte *line_ptr, *extended_end;
21827 const gdb_byte *line_end;
21828 unsigned int bytes_read, extended_len;
21829 unsigned char op_code, extended_op;
21830 CORE_ADDR baseaddr;
21831 struct objfile *objfile = cu->per_objfile->objfile;
21832 bfd *abfd = objfile->obfd;
21833 struct gdbarch *gdbarch = objfile->arch ();
21834 /* True if we're recording line info (as opposed to building partial
21835 symtabs and just interested in finding include files mentioned by
21836 the line number program). */
21837 bool record_lines_p = !decode_for_pst_p;
21838
21839 baseaddr = objfile->text_section_offset ();
21840
21841 line_ptr = lh->statement_program_start;
21842 line_end = lh->statement_program_end;
21843
21844 /* Read the statement sequences until there's nothing left. */
21845 while (line_ptr < line_end)
21846 {
21847 /* The DWARF line number program state machine. Reset the state
21848 machine at the start of each sequence. */
21849 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21850 bool end_sequence = false;
21851
21852 if (record_lines_p)
21853 {
21854 /* Start a subfile for the current file of the state
21855 machine. */
21856 const file_entry *fe = state_machine.current_file ();
21857
21858 if (fe != NULL)
21859 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21860 }
21861
21862 /* Decode the table. */
21863 while (line_ptr < line_end && !end_sequence)
21864 {
21865 op_code = read_1_byte (abfd, line_ptr);
21866 line_ptr += 1;
21867
21868 if (op_code >= lh->opcode_base)
21869 {
21870 /* Special opcode. */
21871 state_machine.handle_special_opcode (op_code);
21872 }
21873 else switch (op_code)
21874 {
21875 case DW_LNS_extended_op:
21876 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21877 &bytes_read);
21878 line_ptr += bytes_read;
21879 extended_end = line_ptr + extended_len;
21880 extended_op = read_1_byte (abfd, line_ptr);
21881 line_ptr += 1;
21882 if (DW_LNE_lo_user <= extended_op
21883 && extended_op <= DW_LNE_hi_user)
21884 {
21885 /* Vendor extension, ignore. */
21886 line_ptr = extended_end;
21887 break;
21888 }
21889 switch (extended_op)
21890 {
21891 case DW_LNE_end_sequence:
21892 state_machine.handle_end_sequence ();
21893 end_sequence = true;
21894 break;
21895 case DW_LNE_set_address:
21896 {
21897 CORE_ADDR address
21898 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21899 line_ptr += bytes_read;
21900
21901 state_machine.check_line_address (cu, line_ptr,
21902 lowpc - baseaddr, address);
21903 state_machine.handle_set_address (baseaddr, address);
21904 }
21905 break;
21906 case DW_LNE_define_file:
21907 {
21908 const char *cur_file;
21909 unsigned int mod_time, length;
21910 dir_index dindex;
21911
21912 cur_file = read_direct_string (abfd, line_ptr,
21913 &bytes_read);
21914 line_ptr += bytes_read;
21915 dindex = (dir_index)
21916 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21917 line_ptr += bytes_read;
21918 mod_time =
21919 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21920 line_ptr += bytes_read;
21921 length =
21922 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21923 line_ptr += bytes_read;
21924 lh->add_file_name (cur_file, dindex, mod_time, length);
21925 }
21926 break;
21927 case DW_LNE_set_discriminator:
21928 {
21929 /* The discriminator is not interesting to the
21930 debugger; just ignore it. We still need to
21931 check its value though:
21932 if there are consecutive entries for the same
21933 (non-prologue) line we want to coalesce them.
21934 PR 17276. */
21935 unsigned int discr
21936 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21937 line_ptr += bytes_read;
21938
21939 state_machine.handle_set_discriminator (discr);
21940 }
21941 break;
21942 default:
21943 complaint (_("mangled .debug_line section"));
21944 return;
21945 }
21946 /* Make sure that we parsed the extended op correctly. If e.g.
21947 we expected a different address size than the producer used,
21948 we may have read the wrong number of bytes. */
21949 if (line_ptr != extended_end)
21950 {
21951 complaint (_("mangled .debug_line section"));
21952 return;
21953 }
21954 break;
21955 case DW_LNS_copy:
21956 state_machine.handle_copy ();
21957 break;
21958 case DW_LNS_advance_pc:
21959 {
21960 CORE_ADDR adjust
21961 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21962 line_ptr += bytes_read;
21963
21964 state_machine.handle_advance_pc (adjust);
21965 }
21966 break;
21967 case DW_LNS_advance_line:
21968 {
21969 int line_delta
21970 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21971 line_ptr += bytes_read;
21972
21973 state_machine.handle_advance_line (line_delta);
21974 }
21975 break;
21976 case DW_LNS_set_file:
21977 {
21978 file_name_index file
21979 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21980 &bytes_read);
21981 line_ptr += bytes_read;
21982
21983 state_machine.handle_set_file (file);
21984 }
21985 break;
21986 case DW_LNS_set_column:
21987 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21988 line_ptr += bytes_read;
21989 break;
21990 case DW_LNS_negate_stmt:
21991 state_machine.handle_negate_stmt ();
21992 break;
21993 case DW_LNS_set_basic_block:
21994 break;
21995 /* Add to the address register of the state machine the
21996 address increment value corresponding to special opcode
21997 255. I.e., this value is scaled by the minimum
21998 instruction length since special opcode 255 would have
21999 scaled the increment. */
22000 case DW_LNS_const_add_pc:
22001 state_machine.handle_const_add_pc ();
22002 break;
22003 case DW_LNS_fixed_advance_pc:
22004 {
22005 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
22006 line_ptr += 2;
22007
22008 state_machine.handle_fixed_advance_pc (addr_adj);
22009 }
22010 break;
22011 default:
22012 {
22013 /* Unknown standard opcode, ignore it. */
22014 int i;
22015
22016 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
22017 {
22018 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
22019 line_ptr += bytes_read;
22020 }
22021 }
22022 }
22023 }
22024
22025 if (!end_sequence)
22026 dwarf2_debug_line_missing_end_sequence_complaint ();
22027
22028 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
22029 in which case we still finish recording the last line). */
22030 state_machine.record_line (true);
22031 }
22032 }
22033
22034 /* Decode the Line Number Program (LNP) for the given line_header
22035 structure and CU. The actual information extracted and the type
22036 of structures created from the LNP depends on the value of PST.
22037
22038 1. If PST is NULL, then this procedure uses the data from the program
22039 to create all necessary symbol tables, and their linetables.
22040
22041 2. If PST is not NULL, this procedure reads the program to determine
22042 the list of files included by the unit represented by PST, and
22043 builds all the associated partial symbol tables.
22044
22045 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
22046 It is used for relative paths in the line table.
22047 NOTE: When processing partial symtabs (pst != NULL),
22048 comp_dir == pst->dirname.
22049
22050 NOTE: It is important that psymtabs have the same file name (via strcmp)
22051 as the corresponding symtab. Since COMP_DIR is not used in the name of the
22052 symtab we don't use it in the name of the psymtabs we create.
22053 E.g. expand_line_sal requires this when finding psymtabs to expand.
22054 A good testcase for this is mb-inline.exp.
22055
22056 LOWPC is the lowest address in CU (or 0 if not known).
22057
22058 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
22059 for its PC<->lines mapping information. Otherwise only the filename
22060 table is read in. */
22061
22062 static void
22063 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
22064 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
22065 CORE_ADDR lowpc, int decode_mapping)
22066 {
22067 struct objfile *objfile = cu->per_objfile->objfile;
22068 const int decode_for_pst_p = (pst != NULL);
22069
22070 if (decode_mapping)
22071 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
22072
22073 if (decode_for_pst_p)
22074 {
22075 /* Now that we're done scanning the Line Header Program, we can
22076 create the psymtab of each included file. */
22077 for (auto &file_entry : lh->file_names ())
22078 if (file_entry.included_p == 1)
22079 {
22080 gdb::unique_xmalloc_ptr<char> name_holder;
22081 const char *include_name =
22082 psymtab_include_file_name (lh, file_entry, pst,
22083 comp_dir, &name_holder);
22084 if (include_name != NULL)
22085 dwarf2_create_include_psymtab
22086 (cu->per_objfile->per_bfd, include_name, pst,
22087 cu->per_objfile->per_bfd->partial_symtabs.get (),
22088 objfile->per_bfd);
22089 }
22090 }
22091 else
22092 {
22093 /* Make sure a symtab is created for every file, even files
22094 which contain only variables (i.e. no code with associated
22095 line numbers). */
22096 buildsym_compunit *builder = cu->get_builder ();
22097 struct compunit_symtab *cust = builder->get_compunit_symtab ();
22098
22099 for (auto &fe : lh->file_names ())
22100 {
22101 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
22102 if (builder->get_current_subfile ()->symtab == NULL)
22103 {
22104 builder->get_current_subfile ()->symtab
22105 = allocate_symtab (cust,
22106 builder->get_current_subfile ()->name);
22107 }
22108 fe.symtab = builder->get_current_subfile ()->symtab;
22109 }
22110 }
22111 }
22112
22113 /* Start a subfile for DWARF. FILENAME is the name of the file and
22114 DIRNAME the name of the source directory which contains FILENAME
22115 or NULL if not known.
22116 This routine tries to keep line numbers from identical absolute and
22117 relative file names in a common subfile.
22118
22119 Using the `list' example from the GDB testsuite, which resides in
22120 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22121 of /srcdir/list0.c yields the following debugging information for list0.c:
22122
22123 DW_AT_name: /srcdir/list0.c
22124 DW_AT_comp_dir: /compdir
22125 files.files[0].name: list0.h
22126 files.files[0].dir: /srcdir
22127 files.files[1].name: list0.c
22128 files.files[1].dir: /srcdir
22129
22130 The line number information for list0.c has to end up in a single
22131 subfile, so that `break /srcdir/list0.c:1' works as expected.
22132 start_subfile will ensure that this happens provided that we pass the
22133 concatenation of files.files[1].dir and files.files[1].name as the
22134 subfile's name. */
22135
22136 static void
22137 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22138 const char *dirname)
22139 {
22140 gdb::unique_xmalloc_ptr<char> copy;
22141
22142 /* In order not to lose the line information directory,
22143 we concatenate it to the filename when it makes sense.
22144 Note that the Dwarf3 standard says (speaking of filenames in line
22145 information): ``The directory index is ignored for file names
22146 that represent full path names''. Thus ignoring dirname in the
22147 `else' branch below isn't an issue. */
22148
22149 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
22150 {
22151 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22152 filename = copy.get ();
22153 }
22154
22155 cu->get_builder ()->start_subfile (filename);
22156 }
22157
22158 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22159 buildsym_compunit constructor. */
22160
22161 struct compunit_symtab *
22162 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22163 CORE_ADDR low_pc)
22164 {
22165 gdb_assert (m_builder == nullptr);
22166
22167 m_builder.reset (new struct buildsym_compunit
22168 (this->per_objfile->objfile,
22169 name, comp_dir, language, low_pc));
22170
22171 list_in_scope = get_builder ()->get_file_symbols ();
22172
22173 get_builder ()->record_debugformat ("DWARF 2");
22174 get_builder ()->record_producer (producer);
22175
22176 processing_has_namespace_info = false;
22177
22178 return get_builder ()->get_compunit_symtab ();
22179 }
22180
22181 static void
22182 var_decode_location (struct attribute *attr, struct symbol *sym,
22183 struct dwarf2_cu *cu)
22184 {
22185 struct objfile *objfile = cu->per_objfile->objfile;
22186 struct comp_unit_head *cu_header = &cu->header;
22187
22188 /* NOTE drow/2003-01-30: There used to be a comment and some special
22189 code here to turn a symbol with DW_AT_external and a
22190 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22191 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22192 with some versions of binutils) where shared libraries could have
22193 relocations against symbols in their debug information - the
22194 minimal symbol would have the right address, but the debug info
22195 would not. It's no longer necessary, because we will explicitly
22196 apply relocations when we read in the debug information now. */
22197
22198 /* A DW_AT_location attribute with no contents indicates that a
22199 variable has been optimized away. */
22200 if (attr->form_is_block () && attr->as_block ()->size == 0)
22201 {
22202 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22203 return;
22204 }
22205
22206 /* Handle one degenerate form of location expression specially, to
22207 preserve GDB's previous behavior when section offsets are
22208 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22209 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
22210
22211 if (attr->form_is_block ())
22212 {
22213 struct dwarf_block *block = attr->as_block ();
22214
22215 if ((block->data[0] == DW_OP_addr
22216 && block->size == 1 + cu_header->addr_size)
22217 || ((block->data[0] == DW_OP_GNU_addr_index
22218 || block->data[0] == DW_OP_addrx)
22219 && (block->size
22220 == 1 + leb128_size (&block->data[1]))))
22221 {
22222 unsigned int dummy;
22223
22224 if (block->data[0] == DW_OP_addr)
22225 SET_SYMBOL_VALUE_ADDRESS
22226 (sym, cu->header.read_address (objfile->obfd,
22227 block->data + 1,
22228 &dummy));
22229 else
22230 SET_SYMBOL_VALUE_ADDRESS
22231 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22232 &dummy));
22233 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22234 fixup_symbol_section (sym, objfile);
22235 SET_SYMBOL_VALUE_ADDRESS
22236 (sym,
22237 SYMBOL_VALUE_ADDRESS (sym)
22238 + objfile->section_offsets[sym->section_index ()]);
22239 return;
22240 }
22241 }
22242
22243 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22244 expression evaluator, and use LOC_COMPUTED only when necessary
22245 (i.e. when the value of a register or memory location is
22246 referenced, or a thread-local block, etc.). Then again, it might
22247 not be worthwhile. I'm assuming that it isn't unless performance
22248 or memory numbers show me otherwise. */
22249
22250 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22251
22252 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22253 cu->has_loclist = true;
22254 }
22255
22256 /* Given a pointer to a DWARF information entry, figure out if we need
22257 to make a symbol table entry for it, and if so, create a new entry
22258 and return a pointer to it.
22259 If TYPE is NULL, determine symbol type from the die, otherwise
22260 used the passed type.
22261 If SPACE is not NULL, use it to hold the new symbol. If it is
22262 NULL, allocate a new symbol on the objfile's obstack. */
22263
22264 static struct symbol *
22265 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22266 struct symbol *space)
22267 {
22268 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22269 struct objfile *objfile = per_objfile->objfile;
22270 struct gdbarch *gdbarch = objfile->arch ();
22271 struct symbol *sym = NULL;
22272 const char *name;
22273 struct attribute *attr = NULL;
22274 struct attribute *attr2 = NULL;
22275 CORE_ADDR baseaddr;
22276 struct pending **list_to_add = NULL;
22277
22278 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22279
22280 baseaddr = objfile->text_section_offset ();
22281
22282 name = dwarf2_name (die, cu);
22283 if (name)
22284 {
22285 int suppress_add = 0;
22286
22287 if (space)
22288 sym = space;
22289 else
22290 sym = new (&objfile->objfile_obstack) symbol;
22291 OBJSTAT (objfile, n_syms++);
22292
22293 /* Cache this symbol's name and the name's demangled form (if any). */
22294 sym->set_language (cu->language, &objfile->objfile_obstack);
22295 /* Fortran does not have mangling standard and the mangling does differ
22296 between gfortran, iFort etc. */
22297 const char *physname
22298 = (cu->language == language_fortran
22299 ? dwarf2_full_name (name, die, cu)
22300 : dwarf2_physname (name, die, cu));
22301 const char *linkagename = dw2_linkage_name (die, cu);
22302
22303 if (linkagename == nullptr || cu->language == language_ada)
22304 sym->set_linkage_name (physname);
22305 else
22306 {
22307 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22308 sym->set_linkage_name (linkagename);
22309 }
22310
22311 /* Default assumptions.
22312 Use the passed type or decode it from the die. */
22313 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22314 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22315 if (type != NULL)
22316 SYMBOL_TYPE (sym) = type;
22317 else
22318 SYMBOL_TYPE (sym) = die_type (die, cu);
22319 attr = dwarf2_attr (die,
22320 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22321 cu);
22322 if (attr != nullptr)
22323 SYMBOL_LINE (sym) = attr->constant_value (0);
22324
22325 attr = dwarf2_attr (die,
22326 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22327 cu);
22328 if (attr != nullptr && attr->is_nonnegative ())
22329 {
22330 file_name_index file_index
22331 = (file_name_index) attr->as_nonnegative ();
22332 struct file_entry *fe;
22333
22334 if (cu->line_header != NULL)
22335 fe = cu->line_header->file_name_at (file_index);
22336 else
22337 fe = NULL;
22338
22339 if (fe == NULL)
22340 complaint (_("file index out of range"));
22341 else
22342 symbol_set_symtab (sym, fe->symtab);
22343 }
22344
22345 switch (die->tag)
22346 {
22347 case DW_TAG_label:
22348 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22349 if (attr != nullptr)
22350 {
22351 CORE_ADDR addr;
22352
22353 addr = attr->as_address ();
22354 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22355 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22356 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22357 }
22358 else
22359 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22360 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22361 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22362 add_symbol_to_list (sym, cu->list_in_scope);
22363 break;
22364 case DW_TAG_subprogram:
22365 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22366 finish_block. */
22367 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22368 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22369 if ((attr2 != nullptr && attr2->as_boolean ())
22370 || cu->language == language_ada
22371 || cu->language == language_fortran)
22372 {
22373 /* Subprograms marked external are stored as a global symbol.
22374 Ada and Fortran subprograms, whether marked external or
22375 not, are always stored as a global symbol, because we want
22376 to be able to access them globally. For instance, we want
22377 to be able to break on a nested subprogram without having
22378 to specify the context. */
22379 list_to_add = cu->get_builder ()->get_global_symbols ();
22380 }
22381 else
22382 {
22383 list_to_add = cu->list_in_scope;
22384 }
22385 break;
22386 case DW_TAG_inlined_subroutine:
22387 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22388 finish_block. */
22389 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22390 SYMBOL_INLINED (sym) = 1;
22391 list_to_add = cu->list_in_scope;
22392 break;
22393 case DW_TAG_template_value_param:
22394 suppress_add = 1;
22395 /* Fall through. */
22396 case DW_TAG_constant:
22397 case DW_TAG_variable:
22398 case DW_TAG_member:
22399 /* Compilation with minimal debug info may result in
22400 variables with missing type entries. Change the
22401 misleading `void' type to something sensible. */
22402 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22403 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22404
22405 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22406 /* In the case of DW_TAG_member, we should only be called for
22407 static const members. */
22408 if (die->tag == DW_TAG_member)
22409 {
22410 /* dwarf2_add_field uses die_is_declaration,
22411 so we do the same. */
22412 gdb_assert (die_is_declaration (die, cu));
22413 gdb_assert (attr);
22414 }
22415 if (attr != nullptr)
22416 {
22417 dwarf2_const_value (attr, sym, cu);
22418 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22419 if (!suppress_add)
22420 {
22421 if (attr2 != nullptr && attr2->as_boolean ())
22422 list_to_add = cu->get_builder ()->get_global_symbols ();
22423 else
22424 list_to_add = cu->list_in_scope;
22425 }
22426 break;
22427 }
22428 attr = dwarf2_attr (die, DW_AT_location, cu);
22429 if (attr != nullptr)
22430 {
22431 var_decode_location (attr, sym, cu);
22432 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22433
22434 /* Fortran explicitly imports any global symbols to the local
22435 scope by DW_TAG_common_block. */
22436 if (cu->language == language_fortran && die->parent
22437 && die->parent->tag == DW_TAG_common_block)
22438 attr2 = NULL;
22439
22440 if (SYMBOL_CLASS (sym) == LOC_STATIC
22441 && SYMBOL_VALUE_ADDRESS (sym) == 0
22442 && !per_objfile->per_bfd->has_section_at_zero)
22443 {
22444 /* When a static variable is eliminated by the linker,
22445 the corresponding debug information is not stripped
22446 out, but the variable address is set to null;
22447 do not add such variables into symbol table. */
22448 }
22449 else if (attr2 != nullptr && attr2->as_boolean ())
22450 {
22451 if (SYMBOL_CLASS (sym) == LOC_STATIC
22452 && (objfile->flags & OBJF_MAINLINE) == 0
22453 && per_objfile->per_bfd->can_copy)
22454 {
22455 /* A global static variable might be subject to
22456 copy relocation. We first check for a local
22457 minsym, though, because maybe the symbol was
22458 marked hidden, in which case this would not
22459 apply. */
22460 bound_minimal_symbol found
22461 = (lookup_minimal_symbol_linkage
22462 (sym->linkage_name (), objfile));
22463 if (found.minsym != nullptr)
22464 sym->maybe_copied = 1;
22465 }
22466
22467 /* A variable with DW_AT_external is never static,
22468 but it may be block-scoped. */
22469 list_to_add
22470 = ((cu->list_in_scope
22471 == cu->get_builder ()->get_file_symbols ())
22472 ? cu->get_builder ()->get_global_symbols ()
22473 : cu->list_in_scope);
22474 }
22475 else
22476 list_to_add = cu->list_in_scope;
22477 }
22478 else
22479 {
22480 /* We do not know the address of this symbol.
22481 If it is an external symbol and we have type information
22482 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22483 The address of the variable will then be determined from
22484 the minimal symbol table whenever the variable is
22485 referenced. */
22486 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22487
22488 /* Fortran explicitly imports any global symbols to the local
22489 scope by DW_TAG_common_block. */
22490 if (cu->language == language_fortran && die->parent
22491 && die->parent->tag == DW_TAG_common_block)
22492 {
22493 /* SYMBOL_CLASS doesn't matter here because
22494 read_common_block is going to reset it. */
22495 if (!suppress_add)
22496 list_to_add = cu->list_in_scope;
22497 }
22498 else if (attr2 != nullptr && attr2->as_boolean ()
22499 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22500 {
22501 /* A variable with DW_AT_external is never static, but it
22502 may be block-scoped. */
22503 list_to_add
22504 = ((cu->list_in_scope
22505 == cu->get_builder ()->get_file_symbols ())
22506 ? cu->get_builder ()->get_global_symbols ()
22507 : cu->list_in_scope);
22508
22509 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22510 }
22511 else if (!die_is_declaration (die, cu))
22512 {
22513 /* Use the default LOC_OPTIMIZED_OUT class. */
22514 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22515 if (!suppress_add)
22516 list_to_add = cu->list_in_scope;
22517 }
22518 }
22519 break;
22520 case DW_TAG_formal_parameter:
22521 {
22522 /* If we are inside a function, mark this as an argument. If
22523 not, we might be looking at an argument to an inlined function
22524 when we do not have enough information to show inlined frames;
22525 pretend it's a local variable in that case so that the user can
22526 still see it. */
22527 struct context_stack *curr
22528 = cu->get_builder ()->get_current_context_stack ();
22529 if (curr != nullptr && curr->name != nullptr)
22530 SYMBOL_IS_ARGUMENT (sym) = 1;
22531 attr = dwarf2_attr (die, DW_AT_location, cu);
22532 if (attr != nullptr)
22533 {
22534 var_decode_location (attr, sym, cu);
22535 }
22536 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22537 if (attr != nullptr)
22538 {
22539 dwarf2_const_value (attr, sym, cu);
22540 }
22541
22542 list_to_add = cu->list_in_scope;
22543 }
22544 break;
22545 case DW_TAG_unspecified_parameters:
22546 /* From varargs functions; gdb doesn't seem to have any
22547 interest in this information, so just ignore it for now.
22548 (FIXME?) */
22549 break;
22550 case DW_TAG_template_type_param:
22551 suppress_add = 1;
22552 /* Fall through. */
22553 case DW_TAG_class_type:
22554 case DW_TAG_interface_type:
22555 case DW_TAG_structure_type:
22556 case DW_TAG_union_type:
22557 case DW_TAG_set_type:
22558 case DW_TAG_enumeration_type:
22559 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22560 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22561
22562 {
22563 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22564 really ever be static objects: otherwise, if you try
22565 to, say, break of a class's method and you're in a file
22566 which doesn't mention that class, it won't work unless
22567 the check for all static symbols in lookup_symbol_aux
22568 saves you. See the OtherFileClass tests in
22569 gdb.c++/namespace.exp. */
22570
22571 if (!suppress_add)
22572 {
22573 buildsym_compunit *builder = cu->get_builder ();
22574 list_to_add
22575 = (cu->list_in_scope == builder->get_file_symbols ()
22576 && cu->language == language_cplus
22577 ? builder->get_global_symbols ()
22578 : cu->list_in_scope);
22579
22580 /* The semantics of C++ state that "struct foo {
22581 ... }" also defines a typedef for "foo". */
22582 if (cu->language == language_cplus
22583 || cu->language == language_ada
22584 || cu->language == language_d
22585 || cu->language == language_rust)
22586 {
22587 /* The symbol's name is already allocated along
22588 with this objfile, so we don't need to
22589 duplicate it for the type. */
22590 if (SYMBOL_TYPE (sym)->name () == 0)
22591 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22592 }
22593 }
22594 }
22595 break;
22596 case DW_TAG_typedef:
22597 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22598 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22599 list_to_add = cu->list_in_scope;
22600 break;
22601 case DW_TAG_array_type:
22602 case DW_TAG_base_type:
22603 case DW_TAG_subrange_type:
22604 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22605 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22606 list_to_add = cu->list_in_scope;
22607 break;
22608 case DW_TAG_enumerator:
22609 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22610 if (attr != nullptr)
22611 {
22612 dwarf2_const_value (attr, sym, cu);
22613 }
22614 {
22615 /* NOTE: carlton/2003-11-10: See comment above in the
22616 DW_TAG_class_type, etc. block. */
22617
22618 list_to_add
22619 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22620 && cu->language == language_cplus
22621 ? cu->get_builder ()->get_global_symbols ()
22622 : cu->list_in_scope);
22623 }
22624 break;
22625 case DW_TAG_imported_declaration:
22626 case DW_TAG_namespace:
22627 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22628 list_to_add = cu->get_builder ()->get_global_symbols ();
22629 break;
22630 case DW_TAG_module:
22631 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22632 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22633 list_to_add = cu->get_builder ()->get_global_symbols ();
22634 break;
22635 case DW_TAG_common_block:
22636 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22637 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22638 add_symbol_to_list (sym, cu->list_in_scope);
22639 break;
22640 default:
22641 /* Not a tag we recognize. Hopefully we aren't processing
22642 trash data, but since we must specifically ignore things
22643 we don't recognize, there is nothing else we should do at
22644 this point. */
22645 complaint (_("unsupported tag: '%s'"),
22646 dwarf_tag_name (die->tag));
22647 break;
22648 }
22649
22650 if (suppress_add)
22651 {
22652 sym->hash_next = objfile->template_symbols;
22653 objfile->template_symbols = sym;
22654 list_to_add = NULL;
22655 }
22656
22657 if (list_to_add != NULL)
22658 add_symbol_to_list (sym, list_to_add);
22659
22660 /* For the benefit of old versions of GCC, check for anonymous
22661 namespaces based on the demangled name. */
22662 if (!cu->processing_has_namespace_info
22663 && cu->language == language_cplus)
22664 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22665 }
22666 return (sym);
22667 }
22668
22669 /* Given an attr with a DW_FORM_dataN value in host byte order,
22670 zero-extend it as appropriate for the symbol's type. The DWARF
22671 standard (v4) is not entirely clear about the meaning of using
22672 DW_FORM_dataN for a constant with a signed type, where the type is
22673 wider than the data. The conclusion of a discussion on the DWARF
22674 list was that this is unspecified. We choose to always zero-extend
22675 because that is the interpretation long in use by GCC. */
22676
22677 static gdb_byte *
22678 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22679 struct dwarf2_cu *cu, LONGEST *value, int bits)
22680 {
22681 struct objfile *objfile = cu->per_objfile->objfile;
22682 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22683 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22684 LONGEST l = attr->constant_value (0);
22685
22686 if (bits < sizeof (*value) * 8)
22687 {
22688 l &= ((LONGEST) 1 << bits) - 1;
22689 *value = l;
22690 }
22691 else if (bits == sizeof (*value) * 8)
22692 *value = l;
22693 else
22694 {
22695 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22696 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22697 return bytes;
22698 }
22699
22700 return NULL;
22701 }
22702
22703 /* Read a constant value from an attribute. Either set *VALUE, or if
22704 the value does not fit in *VALUE, set *BYTES - either already
22705 allocated on the objfile obstack, or newly allocated on OBSTACK,
22706 or, set *BATON, if we translated the constant to a location
22707 expression. */
22708
22709 static void
22710 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22711 const char *name, struct obstack *obstack,
22712 struct dwarf2_cu *cu,
22713 LONGEST *value, const gdb_byte **bytes,
22714 struct dwarf2_locexpr_baton **baton)
22715 {
22716 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22717 struct objfile *objfile = per_objfile->objfile;
22718 struct comp_unit_head *cu_header = &cu->header;
22719 struct dwarf_block *blk;
22720 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22721 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22722
22723 *value = 0;
22724 *bytes = NULL;
22725 *baton = NULL;
22726
22727 switch (attr->form)
22728 {
22729 case DW_FORM_addr:
22730 case DW_FORM_addrx:
22731 case DW_FORM_GNU_addr_index:
22732 {
22733 gdb_byte *data;
22734
22735 if (TYPE_LENGTH (type) != cu_header->addr_size)
22736 dwarf2_const_value_length_mismatch_complaint (name,
22737 cu_header->addr_size,
22738 TYPE_LENGTH (type));
22739 /* Symbols of this form are reasonably rare, so we just
22740 piggyback on the existing location code rather than writing
22741 a new implementation of symbol_computed_ops. */
22742 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22743 (*baton)->per_objfile = per_objfile;
22744 (*baton)->per_cu = cu->per_cu;
22745 gdb_assert ((*baton)->per_cu);
22746
22747 (*baton)->size = 2 + cu_header->addr_size;
22748 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22749 (*baton)->data = data;
22750
22751 data[0] = DW_OP_addr;
22752 store_unsigned_integer (&data[1], cu_header->addr_size,
22753 byte_order, attr->as_address ());
22754 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22755 }
22756 break;
22757 case DW_FORM_string:
22758 case DW_FORM_strp:
22759 case DW_FORM_strx:
22760 case DW_FORM_GNU_str_index:
22761 case DW_FORM_GNU_strp_alt:
22762 /* The string is already allocated on the objfile obstack, point
22763 directly to it. */
22764 *bytes = (const gdb_byte *) attr->as_string ();
22765 break;
22766 case DW_FORM_block1:
22767 case DW_FORM_block2:
22768 case DW_FORM_block4:
22769 case DW_FORM_block:
22770 case DW_FORM_exprloc:
22771 case DW_FORM_data16:
22772 blk = attr->as_block ();
22773 if (TYPE_LENGTH (type) != blk->size)
22774 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22775 TYPE_LENGTH (type));
22776 *bytes = blk->data;
22777 break;
22778
22779 /* The DW_AT_const_value attributes are supposed to carry the
22780 symbol's value "represented as it would be on the target
22781 architecture." By the time we get here, it's already been
22782 converted to host endianness, so we just need to sign- or
22783 zero-extend it as appropriate. */
22784 case DW_FORM_data1:
22785 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22786 break;
22787 case DW_FORM_data2:
22788 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22789 break;
22790 case DW_FORM_data4:
22791 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22792 break;
22793 case DW_FORM_data8:
22794 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22795 break;
22796
22797 case DW_FORM_sdata:
22798 case DW_FORM_implicit_const:
22799 *value = attr->as_signed ();
22800 break;
22801
22802 case DW_FORM_udata:
22803 *value = attr->as_unsigned ();
22804 break;
22805
22806 default:
22807 complaint (_("unsupported const value attribute form: '%s'"),
22808 dwarf_form_name (attr->form));
22809 *value = 0;
22810 break;
22811 }
22812 }
22813
22814
22815 /* Copy constant value from an attribute to a symbol. */
22816
22817 static void
22818 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22819 struct dwarf2_cu *cu)
22820 {
22821 struct objfile *objfile = cu->per_objfile->objfile;
22822 LONGEST value;
22823 const gdb_byte *bytes;
22824 struct dwarf2_locexpr_baton *baton;
22825
22826 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22827 sym->print_name (),
22828 &objfile->objfile_obstack, cu,
22829 &value, &bytes, &baton);
22830
22831 if (baton != NULL)
22832 {
22833 SYMBOL_LOCATION_BATON (sym) = baton;
22834 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22835 }
22836 else if (bytes != NULL)
22837 {
22838 SYMBOL_VALUE_BYTES (sym) = bytes;
22839 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22840 }
22841 else
22842 {
22843 SYMBOL_VALUE (sym) = value;
22844 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22845 }
22846 }
22847
22848 /* Return the type of the die in question using its DW_AT_type attribute. */
22849
22850 static struct type *
22851 die_type (struct die_info *die, struct dwarf2_cu *cu)
22852 {
22853 struct attribute *type_attr;
22854
22855 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22856 if (!type_attr)
22857 {
22858 struct objfile *objfile = cu->per_objfile->objfile;
22859 /* A missing DW_AT_type represents a void type. */
22860 return objfile_type (objfile)->builtin_void;
22861 }
22862
22863 return lookup_die_type (die, type_attr, cu);
22864 }
22865
22866 /* True iff CU's producer generates GNAT Ada auxiliary information
22867 that allows to find parallel types through that information instead
22868 of having to do expensive parallel lookups by type name. */
22869
22870 static int
22871 need_gnat_info (struct dwarf2_cu *cu)
22872 {
22873 /* Assume that the Ada compiler was GNAT, which always produces
22874 the auxiliary information. */
22875 return (cu->language == language_ada);
22876 }
22877
22878 /* Return the auxiliary type of the die in question using its
22879 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22880 attribute is not present. */
22881
22882 static struct type *
22883 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22884 {
22885 struct attribute *type_attr;
22886
22887 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22888 if (!type_attr)
22889 return NULL;
22890
22891 return lookup_die_type (die, type_attr, cu);
22892 }
22893
22894 /* If DIE has a descriptive_type attribute, then set the TYPE's
22895 descriptive type accordingly. */
22896
22897 static void
22898 set_descriptive_type (struct type *type, struct die_info *die,
22899 struct dwarf2_cu *cu)
22900 {
22901 struct type *descriptive_type = die_descriptive_type (die, cu);
22902
22903 if (descriptive_type)
22904 {
22905 ALLOCATE_GNAT_AUX_TYPE (type);
22906 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22907 }
22908 }
22909
22910 /* Return the containing type of the die in question using its
22911 DW_AT_containing_type attribute. */
22912
22913 static struct type *
22914 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22915 {
22916 struct attribute *type_attr;
22917 struct objfile *objfile = cu->per_objfile->objfile;
22918
22919 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22920 if (!type_attr)
22921 error (_("Dwarf Error: Problem turning containing type into gdb type "
22922 "[in module %s]"), objfile_name (objfile));
22923
22924 return lookup_die_type (die, type_attr, cu);
22925 }
22926
22927 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22928
22929 static struct type *
22930 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22931 {
22932 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22933 struct objfile *objfile = per_objfile->objfile;
22934 char *saved;
22935
22936 std::string message
22937 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22938 objfile_name (objfile),
22939 sect_offset_str (cu->header.sect_off),
22940 sect_offset_str (die->sect_off));
22941 saved = obstack_strdup (&objfile->objfile_obstack, message);
22942
22943 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22944 }
22945
22946 /* Look up the type of DIE in CU using its type attribute ATTR.
22947 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22948 DW_AT_containing_type.
22949 If there is no type substitute an error marker. */
22950
22951 static struct type *
22952 lookup_die_type (struct die_info *die, const struct attribute *attr,
22953 struct dwarf2_cu *cu)
22954 {
22955 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22956 struct objfile *objfile = per_objfile->objfile;
22957 struct type *this_type;
22958
22959 gdb_assert (attr->name == DW_AT_type
22960 || attr->name == DW_AT_GNAT_descriptive_type
22961 || attr->name == DW_AT_containing_type);
22962
22963 /* First see if we have it cached. */
22964
22965 if (attr->form == DW_FORM_GNU_ref_alt)
22966 {
22967 struct dwarf2_per_cu_data *per_cu;
22968 sect_offset sect_off = attr->get_ref_die_offset ();
22969
22970 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22971 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22972 }
22973 else if (attr->form_is_ref ())
22974 {
22975 sect_offset sect_off = attr->get_ref_die_offset ();
22976
22977 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22978 }
22979 else if (attr->form == DW_FORM_ref_sig8)
22980 {
22981 ULONGEST signature = attr->as_signature ();
22982
22983 return get_signatured_type (die, signature, cu);
22984 }
22985 else
22986 {
22987 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22988 " at %s [in module %s]"),
22989 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22990 objfile_name (objfile));
22991 return build_error_marker_type (cu, die);
22992 }
22993
22994 /* If not cached we need to read it in. */
22995
22996 if (this_type == NULL)
22997 {
22998 struct die_info *type_die = NULL;
22999 struct dwarf2_cu *type_cu = cu;
23000
23001 if (attr->form_is_ref ())
23002 type_die = follow_die_ref (die, attr, &type_cu);
23003 if (type_die == NULL)
23004 return build_error_marker_type (cu, die);
23005 /* If we find the type now, it's probably because the type came
23006 from an inter-CU reference and the type's CU got expanded before
23007 ours. */
23008 this_type = read_type_die (type_die, type_cu);
23009 }
23010
23011 /* If we still don't have a type use an error marker. */
23012
23013 if (this_type == NULL)
23014 return build_error_marker_type (cu, die);
23015
23016 return this_type;
23017 }
23018
23019 /* Return the type in DIE, CU.
23020 Returns NULL for invalid types.
23021
23022 This first does a lookup in die_type_hash,
23023 and only reads the die in if necessary.
23024
23025 NOTE: This can be called when reading in partial or full symbols. */
23026
23027 static struct type *
23028 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
23029 {
23030 struct type *this_type;
23031
23032 this_type = get_die_type (die, cu);
23033 if (this_type)
23034 return this_type;
23035
23036 return read_type_die_1 (die, cu);
23037 }
23038
23039 /* Read the type in DIE, CU.
23040 Returns NULL for invalid types. */
23041
23042 static struct type *
23043 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
23044 {
23045 struct type *this_type = NULL;
23046
23047 switch (die->tag)
23048 {
23049 case DW_TAG_class_type:
23050 case DW_TAG_interface_type:
23051 case DW_TAG_structure_type:
23052 case DW_TAG_union_type:
23053 this_type = read_structure_type (die, cu);
23054 break;
23055 case DW_TAG_enumeration_type:
23056 this_type = read_enumeration_type (die, cu);
23057 break;
23058 case DW_TAG_subprogram:
23059 case DW_TAG_subroutine_type:
23060 case DW_TAG_inlined_subroutine:
23061 this_type = read_subroutine_type (die, cu);
23062 break;
23063 case DW_TAG_array_type:
23064 this_type = read_array_type (die, cu);
23065 break;
23066 case DW_TAG_set_type:
23067 this_type = read_set_type (die, cu);
23068 break;
23069 case DW_TAG_pointer_type:
23070 this_type = read_tag_pointer_type (die, cu);
23071 break;
23072 case DW_TAG_ptr_to_member_type:
23073 this_type = read_tag_ptr_to_member_type (die, cu);
23074 break;
23075 case DW_TAG_reference_type:
23076 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
23077 break;
23078 case DW_TAG_rvalue_reference_type:
23079 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
23080 break;
23081 case DW_TAG_const_type:
23082 this_type = read_tag_const_type (die, cu);
23083 break;
23084 case DW_TAG_volatile_type:
23085 this_type = read_tag_volatile_type (die, cu);
23086 break;
23087 case DW_TAG_restrict_type:
23088 this_type = read_tag_restrict_type (die, cu);
23089 break;
23090 case DW_TAG_string_type:
23091 this_type = read_tag_string_type (die, cu);
23092 break;
23093 case DW_TAG_typedef:
23094 this_type = read_typedef (die, cu);
23095 break;
23096 case DW_TAG_subrange_type:
23097 this_type = read_subrange_type (die, cu);
23098 break;
23099 case DW_TAG_base_type:
23100 this_type = read_base_type (die, cu);
23101 break;
23102 case DW_TAG_unspecified_type:
23103 this_type = read_unspecified_type (die, cu);
23104 break;
23105 case DW_TAG_namespace:
23106 this_type = read_namespace_type (die, cu);
23107 break;
23108 case DW_TAG_module:
23109 this_type = read_module_type (die, cu);
23110 break;
23111 case DW_TAG_atomic_type:
23112 this_type = read_tag_atomic_type (die, cu);
23113 break;
23114 default:
23115 complaint (_("unexpected tag in read_type_die: '%s'"),
23116 dwarf_tag_name (die->tag));
23117 break;
23118 }
23119
23120 return this_type;
23121 }
23122
23123 /* See if we can figure out if the class lives in a namespace. We do
23124 this by looking for a member function; its demangled name will
23125 contain namespace info, if there is any.
23126 Return the computed name or NULL.
23127 Space for the result is allocated on the objfile's obstack.
23128 This is the full-die version of guess_partial_die_structure_name.
23129 In this case we know DIE has no useful parent. */
23130
23131 static const char *
23132 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23133 {
23134 struct die_info *spec_die;
23135 struct dwarf2_cu *spec_cu;
23136 struct die_info *child;
23137 struct objfile *objfile = cu->per_objfile->objfile;
23138
23139 spec_cu = cu;
23140 spec_die = die_specification (die, &spec_cu);
23141 if (spec_die != NULL)
23142 {
23143 die = spec_die;
23144 cu = spec_cu;
23145 }
23146
23147 for (child = die->child;
23148 child != NULL;
23149 child = child->sibling)
23150 {
23151 if (child->tag == DW_TAG_subprogram)
23152 {
23153 const char *linkage_name = dw2_linkage_name (child, cu);
23154
23155 if (linkage_name != NULL)
23156 {
23157 gdb::unique_xmalloc_ptr<char> actual_name
23158 (cu->language_defn->class_name_from_physname (linkage_name));
23159 const char *name = NULL;
23160
23161 if (actual_name != NULL)
23162 {
23163 const char *die_name = dwarf2_name (die, cu);
23164
23165 if (die_name != NULL
23166 && strcmp (die_name, actual_name.get ()) != 0)
23167 {
23168 /* Strip off the class name from the full name.
23169 We want the prefix. */
23170 int die_name_len = strlen (die_name);
23171 int actual_name_len = strlen (actual_name.get ());
23172 const char *ptr = actual_name.get ();
23173
23174 /* Test for '::' as a sanity check. */
23175 if (actual_name_len > die_name_len + 2
23176 && ptr[actual_name_len - die_name_len - 1] == ':')
23177 name = obstack_strndup (
23178 &objfile->per_bfd->storage_obstack,
23179 ptr, actual_name_len - die_name_len - 2);
23180 }
23181 }
23182 return name;
23183 }
23184 }
23185 }
23186
23187 return NULL;
23188 }
23189
23190 /* GCC might emit a nameless typedef that has a linkage name. Determine the
23191 prefix part in such case. See
23192 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23193
23194 static const char *
23195 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23196 {
23197 struct attribute *attr;
23198 const char *base;
23199
23200 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23201 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23202 return NULL;
23203
23204 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
23205 return NULL;
23206
23207 attr = dw2_linkage_name_attr (die, cu);
23208 const char *attr_name = attr->as_string ();
23209 if (attr == NULL || attr_name == NULL)
23210 return NULL;
23211
23212 /* dwarf2_name had to be already called. */
23213 gdb_assert (attr->canonical_string_p ());
23214
23215 /* Strip the base name, keep any leading namespaces/classes. */
23216 base = strrchr (attr_name, ':');
23217 if (base == NULL || base == attr_name || base[-1] != ':')
23218 return "";
23219
23220 struct objfile *objfile = cu->per_objfile->objfile;
23221 return obstack_strndup (&objfile->per_bfd->storage_obstack,
23222 attr_name,
23223 &base[-1] - attr_name);
23224 }
23225
23226 /* Return the name of the namespace/class that DIE is defined within,
23227 or "" if we can't tell. The caller should not xfree the result.
23228
23229 For example, if we're within the method foo() in the following
23230 code:
23231
23232 namespace N {
23233 class C {
23234 void foo () {
23235 }
23236 };
23237 }
23238
23239 then determine_prefix on foo's die will return "N::C". */
23240
23241 static const char *
23242 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23243 {
23244 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23245 struct die_info *parent, *spec_die;
23246 struct dwarf2_cu *spec_cu;
23247 struct type *parent_type;
23248 const char *retval;
23249
23250 if (cu->language != language_cplus
23251 && cu->language != language_fortran && cu->language != language_d
23252 && cu->language != language_rust)
23253 return "";
23254
23255 retval = anonymous_struct_prefix (die, cu);
23256 if (retval)
23257 return retval;
23258
23259 /* We have to be careful in the presence of DW_AT_specification.
23260 For example, with GCC 3.4, given the code
23261
23262 namespace N {
23263 void foo() {
23264 // Definition of N::foo.
23265 }
23266 }
23267
23268 then we'll have a tree of DIEs like this:
23269
23270 1: DW_TAG_compile_unit
23271 2: DW_TAG_namespace // N
23272 3: DW_TAG_subprogram // declaration of N::foo
23273 4: DW_TAG_subprogram // definition of N::foo
23274 DW_AT_specification // refers to die #3
23275
23276 Thus, when processing die #4, we have to pretend that we're in
23277 the context of its DW_AT_specification, namely the contex of die
23278 #3. */
23279 spec_cu = cu;
23280 spec_die = die_specification (die, &spec_cu);
23281 if (spec_die == NULL)
23282 parent = die->parent;
23283 else
23284 {
23285 parent = spec_die->parent;
23286 cu = spec_cu;
23287 }
23288
23289 if (parent == NULL)
23290 return "";
23291 else if (parent->building_fullname)
23292 {
23293 const char *name;
23294 const char *parent_name;
23295
23296 /* It has been seen on RealView 2.2 built binaries,
23297 DW_TAG_template_type_param types actually _defined_ as
23298 children of the parent class:
23299
23300 enum E {};
23301 template class <class Enum> Class{};
23302 Class<enum E> class_e;
23303
23304 1: DW_TAG_class_type (Class)
23305 2: DW_TAG_enumeration_type (E)
23306 3: DW_TAG_enumerator (enum1:0)
23307 3: DW_TAG_enumerator (enum2:1)
23308 ...
23309 2: DW_TAG_template_type_param
23310 DW_AT_type DW_FORM_ref_udata (E)
23311
23312 Besides being broken debug info, it can put GDB into an
23313 infinite loop. Consider:
23314
23315 When we're building the full name for Class<E>, we'll start
23316 at Class, and go look over its template type parameters,
23317 finding E. We'll then try to build the full name of E, and
23318 reach here. We're now trying to build the full name of E,
23319 and look over the parent DIE for containing scope. In the
23320 broken case, if we followed the parent DIE of E, we'd again
23321 find Class, and once again go look at its template type
23322 arguments, etc., etc. Simply don't consider such parent die
23323 as source-level parent of this die (it can't be, the language
23324 doesn't allow it), and break the loop here. */
23325 name = dwarf2_name (die, cu);
23326 parent_name = dwarf2_name (parent, cu);
23327 complaint (_("template param type '%s' defined within parent '%s'"),
23328 name ? name : "<unknown>",
23329 parent_name ? parent_name : "<unknown>");
23330 return "";
23331 }
23332 else
23333 switch (parent->tag)
23334 {
23335 case DW_TAG_namespace:
23336 parent_type = read_type_die (parent, cu);
23337 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23338 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23339 Work around this problem here. */
23340 if (cu->language == language_cplus
23341 && strcmp (parent_type->name (), "::") == 0)
23342 return "";
23343 /* We give a name to even anonymous namespaces. */
23344 return parent_type->name ();
23345 case DW_TAG_class_type:
23346 case DW_TAG_interface_type:
23347 case DW_TAG_structure_type:
23348 case DW_TAG_union_type:
23349 case DW_TAG_module:
23350 parent_type = read_type_die (parent, cu);
23351 if (parent_type->name () != NULL)
23352 return parent_type->name ();
23353 else
23354 /* An anonymous structure is only allowed non-static data
23355 members; no typedefs, no member functions, et cetera.
23356 So it does not need a prefix. */
23357 return "";
23358 case DW_TAG_compile_unit:
23359 case DW_TAG_partial_unit:
23360 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23361 if (cu->language == language_cplus
23362 && !per_objfile->per_bfd->types.empty ()
23363 && die->child != NULL
23364 && (die->tag == DW_TAG_class_type
23365 || die->tag == DW_TAG_structure_type
23366 || die->tag == DW_TAG_union_type))
23367 {
23368 const char *name = guess_full_die_structure_name (die, cu);
23369 if (name != NULL)
23370 return name;
23371 }
23372 return "";
23373 case DW_TAG_subprogram:
23374 /* Nested subroutines in Fortran get a prefix with the name
23375 of the parent's subroutine. */
23376 if (cu->language == language_fortran)
23377 {
23378 if ((die->tag == DW_TAG_subprogram)
23379 && (dwarf2_name (parent, cu) != NULL))
23380 return dwarf2_name (parent, cu);
23381 }
23382 return determine_prefix (parent, cu);
23383 case DW_TAG_enumeration_type:
23384 parent_type = read_type_die (parent, cu);
23385 if (parent_type->is_declared_class ())
23386 {
23387 if (parent_type->name () != NULL)
23388 return parent_type->name ();
23389 return "";
23390 }
23391 /* Fall through. */
23392 default:
23393 return determine_prefix (parent, cu);
23394 }
23395 }
23396
23397 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23398 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23399 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23400 an obconcat, otherwise allocate storage for the result. The CU argument is
23401 used to determine the language and hence, the appropriate separator. */
23402
23403 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23404
23405 static char *
23406 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23407 int physname, struct dwarf2_cu *cu)
23408 {
23409 const char *lead = "";
23410 const char *sep;
23411
23412 if (suffix == NULL || suffix[0] == '\0'
23413 || prefix == NULL || prefix[0] == '\0')
23414 sep = "";
23415 else if (cu->language == language_d)
23416 {
23417 /* For D, the 'main' function could be defined in any module, but it
23418 should never be prefixed. */
23419 if (strcmp (suffix, "D main") == 0)
23420 {
23421 prefix = "";
23422 sep = "";
23423 }
23424 else
23425 sep = ".";
23426 }
23427 else if (cu->language == language_fortran && physname)
23428 {
23429 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23430 DW_AT_MIPS_linkage_name is preferred and used instead. */
23431
23432 lead = "__";
23433 sep = "_MOD_";
23434 }
23435 else
23436 sep = "::";
23437
23438 if (prefix == NULL)
23439 prefix = "";
23440 if (suffix == NULL)
23441 suffix = "";
23442
23443 if (obs == NULL)
23444 {
23445 char *retval
23446 = ((char *)
23447 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23448
23449 strcpy (retval, lead);
23450 strcat (retval, prefix);
23451 strcat (retval, sep);
23452 strcat (retval, suffix);
23453 return retval;
23454 }
23455 else
23456 {
23457 /* We have an obstack. */
23458 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23459 }
23460 }
23461
23462 /* Get name of a die, return NULL if not found. */
23463
23464 static const char *
23465 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23466 struct objfile *objfile)
23467 {
23468 if (name && cu->language == language_cplus)
23469 {
23470 gdb::unique_xmalloc_ptr<char> canon_name
23471 = cp_canonicalize_string (name);
23472
23473 if (canon_name != nullptr)
23474 name = objfile->intern (canon_name.get ());
23475 }
23476
23477 return name;
23478 }
23479
23480 /* Get name of a die, return NULL if not found.
23481 Anonymous namespaces are converted to their magic string. */
23482
23483 static const char *
23484 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23485 {
23486 struct attribute *attr;
23487 struct objfile *objfile = cu->per_objfile->objfile;
23488
23489 attr = dwarf2_attr (die, DW_AT_name, cu);
23490 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23491 if (attr_name == nullptr
23492 && die->tag != DW_TAG_namespace
23493 && die->tag != DW_TAG_class_type
23494 && die->tag != DW_TAG_interface_type
23495 && die->tag != DW_TAG_structure_type
23496 && die->tag != DW_TAG_union_type)
23497 return NULL;
23498
23499 switch (die->tag)
23500 {
23501 case DW_TAG_compile_unit:
23502 case DW_TAG_partial_unit:
23503 /* Compilation units have a DW_AT_name that is a filename, not
23504 a source language identifier. */
23505 case DW_TAG_enumeration_type:
23506 case DW_TAG_enumerator:
23507 /* These tags always have simple identifiers already; no need
23508 to canonicalize them. */
23509 return attr_name;
23510
23511 case DW_TAG_namespace:
23512 if (attr_name != nullptr)
23513 return attr_name;
23514 return CP_ANONYMOUS_NAMESPACE_STR;
23515
23516 case DW_TAG_class_type:
23517 case DW_TAG_interface_type:
23518 case DW_TAG_structure_type:
23519 case DW_TAG_union_type:
23520 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23521 structures or unions. These were of the form "._%d" in GCC 4.1,
23522 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23523 and GCC 4.4. We work around this problem by ignoring these. */
23524 if (attr_name != nullptr
23525 && (startswith (attr_name, "._")
23526 || startswith (attr_name, "<anonymous")))
23527 return NULL;
23528
23529 /* GCC might emit a nameless typedef that has a linkage name. See
23530 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23531 if (!attr || attr_name == NULL)
23532 {
23533 attr = dw2_linkage_name_attr (die, cu);
23534 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23535 if (attr == NULL || attr_name == NULL)
23536 return NULL;
23537
23538 /* Avoid demangling attr_name the second time on a second
23539 call for the same DIE. */
23540 if (!attr->canonical_string_p ())
23541 {
23542 gdb::unique_xmalloc_ptr<char> demangled
23543 (gdb_demangle (attr_name, DMGL_TYPES));
23544 if (demangled == nullptr)
23545 return nullptr;
23546
23547 attr->set_string_canonical (objfile->intern (demangled.get ()));
23548 attr_name = attr->as_string ();
23549 }
23550
23551 /* Strip any leading namespaces/classes, keep only the
23552 base name. DW_AT_name for named DIEs does not
23553 contain the prefixes. */
23554 const char *base = strrchr (attr_name, ':');
23555 if (base && base > attr_name && base[-1] == ':')
23556 return &base[1];
23557 else
23558 return attr_name;
23559 }
23560 break;
23561
23562 default:
23563 break;
23564 }
23565
23566 if (!attr->canonical_string_p ())
23567 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23568 objfile));
23569 return attr->as_string ();
23570 }
23571
23572 /* Return the die that this die in an extension of, or NULL if there
23573 is none. *EXT_CU is the CU containing DIE on input, and the CU
23574 containing the return value on output. */
23575
23576 static struct die_info *
23577 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23578 {
23579 struct attribute *attr;
23580
23581 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23582 if (attr == NULL)
23583 return NULL;
23584
23585 return follow_die_ref (die, attr, ext_cu);
23586 }
23587
23588 static void
23589 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23590 {
23591 unsigned int i;
23592
23593 print_spaces (indent, f);
23594 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23595 dwarf_tag_name (die->tag), die->abbrev,
23596 sect_offset_str (die->sect_off));
23597
23598 if (die->parent != NULL)
23599 {
23600 print_spaces (indent, f);
23601 fprintf_unfiltered (f, " parent at offset: %s\n",
23602 sect_offset_str (die->parent->sect_off));
23603 }
23604
23605 print_spaces (indent, f);
23606 fprintf_unfiltered (f, " has children: %s\n",
23607 dwarf_bool_name (die->child != NULL));
23608
23609 print_spaces (indent, f);
23610 fprintf_unfiltered (f, " attributes:\n");
23611
23612 for (i = 0; i < die->num_attrs; ++i)
23613 {
23614 print_spaces (indent, f);
23615 fprintf_unfiltered (f, " %s (%s) ",
23616 dwarf_attr_name (die->attrs[i].name),
23617 dwarf_form_name (die->attrs[i].form));
23618
23619 switch (die->attrs[i].form)
23620 {
23621 case DW_FORM_addr:
23622 case DW_FORM_addrx:
23623 case DW_FORM_GNU_addr_index:
23624 fprintf_unfiltered (f, "address: ");
23625 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23626 break;
23627 case DW_FORM_block2:
23628 case DW_FORM_block4:
23629 case DW_FORM_block:
23630 case DW_FORM_block1:
23631 fprintf_unfiltered (f, "block: size %s",
23632 pulongest (die->attrs[i].as_block ()->size));
23633 break;
23634 case DW_FORM_exprloc:
23635 fprintf_unfiltered (f, "expression: size %s",
23636 pulongest (die->attrs[i].as_block ()->size));
23637 break;
23638 case DW_FORM_data16:
23639 fprintf_unfiltered (f, "constant of 16 bytes");
23640 break;
23641 case DW_FORM_ref_addr:
23642 fprintf_unfiltered (f, "ref address: ");
23643 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23644 break;
23645 case DW_FORM_GNU_ref_alt:
23646 fprintf_unfiltered (f, "alt ref address: ");
23647 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23648 break;
23649 case DW_FORM_ref1:
23650 case DW_FORM_ref2:
23651 case DW_FORM_ref4:
23652 case DW_FORM_ref8:
23653 case DW_FORM_ref_udata:
23654 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23655 (long) (die->attrs[i].as_unsigned ()));
23656 break;
23657 case DW_FORM_data1:
23658 case DW_FORM_data2:
23659 case DW_FORM_data4:
23660 case DW_FORM_data8:
23661 case DW_FORM_udata:
23662 fprintf_unfiltered (f, "constant: %s",
23663 pulongest (die->attrs[i].as_unsigned ()));
23664 break;
23665 case DW_FORM_sec_offset:
23666 fprintf_unfiltered (f, "section offset: %s",
23667 pulongest (die->attrs[i].as_unsigned ()));
23668 break;
23669 case DW_FORM_ref_sig8:
23670 fprintf_unfiltered (f, "signature: %s",
23671 hex_string (die->attrs[i].as_signature ()));
23672 break;
23673 case DW_FORM_string:
23674 case DW_FORM_strp:
23675 case DW_FORM_line_strp:
23676 case DW_FORM_strx:
23677 case DW_FORM_GNU_str_index:
23678 case DW_FORM_GNU_strp_alt:
23679 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23680 die->attrs[i].as_string ()
23681 ? die->attrs[i].as_string () : "",
23682 die->attrs[i].canonical_string_p () ? "is" : "not");
23683 break;
23684 case DW_FORM_flag:
23685 if (die->attrs[i].as_boolean ())
23686 fprintf_unfiltered (f, "flag: TRUE");
23687 else
23688 fprintf_unfiltered (f, "flag: FALSE");
23689 break;
23690 case DW_FORM_flag_present:
23691 fprintf_unfiltered (f, "flag: TRUE");
23692 break;
23693 case DW_FORM_indirect:
23694 /* The reader will have reduced the indirect form to
23695 the "base form" so this form should not occur. */
23696 fprintf_unfiltered (f,
23697 "unexpected attribute form: DW_FORM_indirect");
23698 break;
23699 case DW_FORM_sdata:
23700 case DW_FORM_implicit_const:
23701 fprintf_unfiltered (f, "constant: %s",
23702 plongest (die->attrs[i].as_signed ()));
23703 break;
23704 default:
23705 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23706 die->attrs[i].form);
23707 break;
23708 }
23709 fprintf_unfiltered (f, "\n");
23710 }
23711 }
23712
23713 static void
23714 dump_die_for_error (struct die_info *die)
23715 {
23716 dump_die_shallow (gdb_stderr, 0, die);
23717 }
23718
23719 static void
23720 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23721 {
23722 int indent = level * 4;
23723
23724 gdb_assert (die != NULL);
23725
23726 if (level >= max_level)
23727 return;
23728
23729 dump_die_shallow (f, indent, die);
23730
23731 if (die->child != NULL)
23732 {
23733 print_spaces (indent, f);
23734 fprintf_unfiltered (f, " Children:");
23735 if (level + 1 < max_level)
23736 {
23737 fprintf_unfiltered (f, "\n");
23738 dump_die_1 (f, level + 1, max_level, die->child);
23739 }
23740 else
23741 {
23742 fprintf_unfiltered (f,
23743 " [not printed, max nesting level reached]\n");
23744 }
23745 }
23746
23747 if (die->sibling != NULL && level > 0)
23748 {
23749 dump_die_1 (f, level, max_level, die->sibling);
23750 }
23751 }
23752
23753 /* This is called from the pdie macro in gdbinit.in.
23754 It's not static so gcc will keep a copy callable from gdb. */
23755
23756 void
23757 dump_die (struct die_info *die, int max_level)
23758 {
23759 dump_die_1 (gdb_stdlog, 0, max_level, die);
23760 }
23761
23762 static void
23763 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23764 {
23765 void **slot;
23766
23767 slot = htab_find_slot_with_hash (cu->die_hash, die,
23768 to_underlying (die->sect_off),
23769 INSERT);
23770
23771 *slot = die;
23772 }
23773
23774 /* Follow reference or signature attribute ATTR of SRC_DIE.
23775 On entry *REF_CU is the CU of SRC_DIE.
23776 On exit *REF_CU is the CU of the result. */
23777
23778 static struct die_info *
23779 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23780 struct dwarf2_cu **ref_cu)
23781 {
23782 struct die_info *die;
23783
23784 if (attr->form_is_ref ())
23785 die = follow_die_ref (src_die, attr, ref_cu);
23786 else if (attr->form == DW_FORM_ref_sig8)
23787 die = follow_die_sig (src_die, attr, ref_cu);
23788 else
23789 {
23790 dump_die_for_error (src_die);
23791 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23792 objfile_name ((*ref_cu)->per_objfile->objfile));
23793 }
23794
23795 return die;
23796 }
23797
23798 /* Follow reference OFFSET.
23799 On entry *REF_CU is the CU of the source die referencing OFFSET.
23800 On exit *REF_CU is the CU of the result.
23801 Returns NULL if OFFSET is invalid. */
23802
23803 static struct die_info *
23804 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23805 struct dwarf2_cu **ref_cu)
23806 {
23807 struct die_info temp_die;
23808 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23809 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23810
23811 gdb_assert (cu->per_cu != NULL);
23812
23813 target_cu = cu;
23814
23815 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23816 "source CU contains target offset: %d",
23817 sect_offset_str (cu->per_cu->sect_off),
23818 sect_offset_str (sect_off),
23819 cu->header.offset_in_cu_p (sect_off));
23820
23821 if (cu->per_cu->is_debug_types)
23822 {
23823 /* .debug_types CUs cannot reference anything outside their CU.
23824 If they need to, they have to reference a signatured type via
23825 DW_FORM_ref_sig8. */
23826 if (!cu->header.offset_in_cu_p (sect_off))
23827 return NULL;
23828 }
23829 else if (offset_in_dwz != cu->per_cu->is_dwz
23830 || !cu->header.offset_in_cu_p (sect_off))
23831 {
23832 struct dwarf2_per_cu_data *per_cu;
23833
23834 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23835 per_objfile);
23836
23837 dwarf_read_debug_printf_v ("target CU offset: %s, "
23838 "target CU DIEs loaded: %d",
23839 sect_offset_str (per_cu->sect_off),
23840 per_objfile->get_cu (per_cu) != nullptr);
23841
23842 /* If necessary, add it to the queue and load its DIEs.
23843
23844 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23845 it doesn't mean they are currently loaded. Since we require them
23846 to be loaded, we must check for ourselves. */
23847 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23848 || per_objfile->get_cu (per_cu) == nullptr)
23849 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23850 false, cu->language);
23851
23852 target_cu = per_objfile->get_cu (per_cu);
23853 gdb_assert (target_cu != nullptr);
23854 }
23855 else if (cu->dies == NULL)
23856 {
23857 /* We're loading full DIEs during partial symbol reading. */
23858 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23859 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23860 language_minimal);
23861 }
23862
23863 *ref_cu = target_cu;
23864 temp_die.sect_off = sect_off;
23865
23866 if (target_cu != cu)
23867 target_cu->ancestor = cu;
23868
23869 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23870 &temp_die,
23871 to_underlying (sect_off));
23872 }
23873
23874 /* Follow reference attribute ATTR of SRC_DIE.
23875 On entry *REF_CU is the CU of SRC_DIE.
23876 On exit *REF_CU is the CU of the result. */
23877
23878 static struct die_info *
23879 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23880 struct dwarf2_cu **ref_cu)
23881 {
23882 sect_offset sect_off = attr->get_ref_die_offset ();
23883 struct dwarf2_cu *cu = *ref_cu;
23884 struct die_info *die;
23885
23886 die = follow_die_offset (sect_off,
23887 (attr->form == DW_FORM_GNU_ref_alt
23888 || cu->per_cu->is_dwz),
23889 ref_cu);
23890 if (!die)
23891 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23892 "at %s [in module %s]"),
23893 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23894 objfile_name (cu->per_objfile->objfile));
23895
23896 return die;
23897 }
23898
23899 /* See read.h. */
23900
23901 struct dwarf2_locexpr_baton
23902 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23903 dwarf2_per_cu_data *per_cu,
23904 dwarf2_per_objfile *per_objfile,
23905 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23906 bool resolve_abstract_p)
23907 {
23908 struct die_info *die;
23909 struct attribute *attr;
23910 struct dwarf2_locexpr_baton retval;
23911 struct objfile *objfile = per_objfile->objfile;
23912
23913 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23914 if (cu == nullptr)
23915 cu = load_cu (per_cu, per_objfile, false);
23916
23917 if (cu == nullptr)
23918 {
23919 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23920 Instead just throw an error, not much else we can do. */
23921 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23922 sect_offset_str (sect_off), objfile_name (objfile));
23923 }
23924
23925 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23926 if (!die)
23927 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23928 sect_offset_str (sect_off), objfile_name (objfile));
23929
23930 attr = dwarf2_attr (die, DW_AT_location, cu);
23931 if (!attr && resolve_abstract_p
23932 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23933 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23934 {
23935 CORE_ADDR pc = get_frame_pc ();
23936 CORE_ADDR baseaddr = objfile->text_section_offset ();
23937 struct gdbarch *gdbarch = objfile->arch ();
23938
23939 for (const auto &cand_off
23940 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23941 {
23942 struct dwarf2_cu *cand_cu = cu;
23943 struct die_info *cand
23944 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23945 if (!cand
23946 || !cand->parent
23947 || cand->parent->tag != DW_TAG_subprogram)
23948 continue;
23949
23950 CORE_ADDR pc_low, pc_high;
23951 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23952 if (pc_low == ((CORE_ADDR) -1))
23953 continue;
23954 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23955 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23956 if (!(pc_low <= pc && pc < pc_high))
23957 continue;
23958
23959 die = cand;
23960 attr = dwarf2_attr (die, DW_AT_location, cu);
23961 break;
23962 }
23963 }
23964
23965 if (!attr)
23966 {
23967 /* DWARF: "If there is no such attribute, then there is no effect.".
23968 DATA is ignored if SIZE is 0. */
23969
23970 retval.data = NULL;
23971 retval.size = 0;
23972 }
23973 else if (attr->form_is_section_offset ())
23974 {
23975 struct dwarf2_loclist_baton loclist_baton;
23976 CORE_ADDR pc = get_frame_pc ();
23977 size_t size;
23978
23979 fill_in_loclist_baton (cu, &loclist_baton, attr);
23980
23981 retval.data = dwarf2_find_location_expression (&loclist_baton,
23982 &size, pc);
23983 retval.size = size;
23984 }
23985 else
23986 {
23987 if (!attr->form_is_block ())
23988 error (_("Dwarf Error: DIE at %s referenced in module %s "
23989 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23990 sect_offset_str (sect_off), objfile_name (objfile));
23991
23992 struct dwarf_block *block = attr->as_block ();
23993 retval.data = block->data;
23994 retval.size = block->size;
23995 }
23996 retval.per_objfile = per_objfile;
23997 retval.per_cu = cu->per_cu;
23998
23999 per_objfile->age_comp_units ();
24000
24001 return retval;
24002 }
24003
24004 /* See read.h. */
24005
24006 struct dwarf2_locexpr_baton
24007 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
24008 dwarf2_per_cu_data *per_cu,
24009 dwarf2_per_objfile *per_objfile,
24010 gdb::function_view<CORE_ADDR ()> get_frame_pc)
24011 {
24012 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
24013
24014 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
24015 get_frame_pc);
24016 }
24017
24018 /* Write a constant of a given type as target-ordered bytes into
24019 OBSTACK. */
24020
24021 static const gdb_byte *
24022 write_constant_as_bytes (struct obstack *obstack,
24023 enum bfd_endian byte_order,
24024 struct type *type,
24025 ULONGEST value,
24026 LONGEST *len)
24027 {
24028 gdb_byte *result;
24029
24030 *len = TYPE_LENGTH (type);
24031 result = (gdb_byte *) obstack_alloc (obstack, *len);
24032 store_unsigned_integer (result, *len, byte_order, value);
24033
24034 return result;
24035 }
24036
24037 /* See read.h. */
24038
24039 const gdb_byte *
24040 dwarf2_fetch_constant_bytes (sect_offset sect_off,
24041 dwarf2_per_cu_data *per_cu,
24042 dwarf2_per_objfile *per_objfile,
24043 obstack *obstack,
24044 LONGEST *len)
24045 {
24046 struct die_info *die;
24047 struct attribute *attr;
24048 const gdb_byte *result = NULL;
24049 struct type *type;
24050 LONGEST value;
24051 enum bfd_endian byte_order;
24052 struct objfile *objfile = per_objfile->objfile;
24053
24054 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24055 if (cu == nullptr)
24056 cu = load_cu (per_cu, per_objfile, false);
24057
24058 if (cu == nullptr)
24059 {
24060 /* We shouldn't get here for a dummy CU, but don't crash on the user.
24061 Instead just throw an error, not much else we can do. */
24062 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
24063 sect_offset_str (sect_off), objfile_name (objfile));
24064 }
24065
24066 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24067 if (!die)
24068 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
24069 sect_offset_str (sect_off), objfile_name (objfile));
24070
24071 attr = dwarf2_attr (die, DW_AT_const_value, cu);
24072 if (attr == NULL)
24073 return NULL;
24074
24075 byte_order = (bfd_big_endian (objfile->obfd)
24076 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
24077
24078 switch (attr->form)
24079 {
24080 case DW_FORM_addr:
24081 case DW_FORM_addrx:
24082 case DW_FORM_GNU_addr_index:
24083 {
24084 gdb_byte *tem;
24085
24086 *len = cu->header.addr_size;
24087 tem = (gdb_byte *) obstack_alloc (obstack, *len);
24088 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
24089 result = tem;
24090 }
24091 break;
24092 case DW_FORM_string:
24093 case DW_FORM_strp:
24094 case DW_FORM_strx:
24095 case DW_FORM_GNU_str_index:
24096 case DW_FORM_GNU_strp_alt:
24097 /* The string is already allocated on the objfile obstack, point
24098 directly to it. */
24099 {
24100 const char *attr_name = attr->as_string ();
24101 result = (const gdb_byte *) attr_name;
24102 *len = strlen (attr_name);
24103 }
24104 break;
24105 case DW_FORM_block1:
24106 case DW_FORM_block2:
24107 case DW_FORM_block4:
24108 case DW_FORM_block:
24109 case DW_FORM_exprloc:
24110 case DW_FORM_data16:
24111 {
24112 struct dwarf_block *block = attr->as_block ();
24113 result = block->data;
24114 *len = block->size;
24115 }
24116 break;
24117
24118 /* The DW_AT_const_value attributes are supposed to carry the
24119 symbol's value "represented as it would be on the target
24120 architecture." By the time we get here, it's already been
24121 converted to host endianness, so we just need to sign- or
24122 zero-extend it as appropriate. */
24123 case DW_FORM_data1:
24124 type = die_type (die, cu);
24125 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24126 if (result == NULL)
24127 result = write_constant_as_bytes (obstack, byte_order,
24128 type, value, len);
24129 break;
24130 case DW_FORM_data2:
24131 type = die_type (die, cu);
24132 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24133 if (result == NULL)
24134 result = write_constant_as_bytes (obstack, byte_order,
24135 type, value, len);
24136 break;
24137 case DW_FORM_data4:
24138 type = die_type (die, cu);
24139 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24140 if (result == NULL)
24141 result = write_constant_as_bytes (obstack, byte_order,
24142 type, value, len);
24143 break;
24144 case DW_FORM_data8:
24145 type = die_type (die, cu);
24146 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24147 if (result == NULL)
24148 result = write_constant_as_bytes (obstack, byte_order,
24149 type, value, len);
24150 break;
24151
24152 case DW_FORM_sdata:
24153 case DW_FORM_implicit_const:
24154 type = die_type (die, cu);
24155 result = write_constant_as_bytes (obstack, byte_order,
24156 type, attr->as_signed (), len);
24157 break;
24158
24159 case DW_FORM_udata:
24160 type = die_type (die, cu);
24161 result = write_constant_as_bytes (obstack, byte_order,
24162 type, attr->as_unsigned (), len);
24163 break;
24164
24165 default:
24166 complaint (_("unsupported const value attribute form: '%s'"),
24167 dwarf_form_name (attr->form));
24168 break;
24169 }
24170
24171 return result;
24172 }
24173
24174 /* See read.h. */
24175
24176 struct type *
24177 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
24178 dwarf2_per_cu_data *per_cu,
24179 dwarf2_per_objfile *per_objfile)
24180 {
24181 struct die_info *die;
24182
24183 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24184 if (cu == nullptr)
24185 cu = load_cu (per_cu, per_objfile, false);
24186
24187 if (cu == nullptr)
24188 return nullptr;
24189
24190 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24191 if (!die)
24192 return NULL;
24193
24194 return die_type (die, cu);
24195 }
24196
24197 /* See read.h. */
24198
24199 struct type *
24200 dwarf2_get_die_type (cu_offset die_offset,
24201 dwarf2_per_cu_data *per_cu,
24202 dwarf2_per_objfile *per_objfile)
24203 {
24204 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
24205 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
24206 }
24207
24208 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
24209 On entry *REF_CU is the CU of SRC_DIE.
24210 On exit *REF_CU is the CU of the result.
24211 Returns NULL if the referenced DIE isn't found. */
24212
24213 static struct die_info *
24214 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24215 struct dwarf2_cu **ref_cu)
24216 {
24217 struct die_info temp_die;
24218 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
24219 struct die_info *die;
24220 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
24221
24222
24223 /* While it might be nice to assert sig_type->type == NULL here,
24224 we can get here for DW_AT_imported_declaration where we need
24225 the DIE not the type. */
24226
24227 /* If necessary, add it to the queue and load its DIEs.
24228
24229 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24230 it doesn't mean they are currently loaded. Since we require them
24231 to be loaded, we must check for ourselves. */
24232 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
24233 language_minimal)
24234 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
24235 read_signatured_type (sig_type, per_objfile);
24236
24237 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
24238 gdb_assert (sig_cu != NULL);
24239 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24240 temp_die.sect_off = sig_type->type_offset_in_section;
24241 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24242 to_underlying (temp_die.sect_off));
24243 if (die)
24244 {
24245 /* For .gdb_index version 7 keep track of included TUs.
24246 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24247 if (per_objfile->per_bfd->index_table != NULL
24248 && per_objfile->per_bfd->index_table->version <= 7)
24249 {
24250 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24251 }
24252
24253 *ref_cu = sig_cu;
24254 if (sig_cu != cu)
24255 sig_cu->ancestor = cu;
24256
24257 return die;
24258 }
24259
24260 return NULL;
24261 }
24262
24263 /* Follow signatured type referenced by ATTR in SRC_DIE.
24264 On entry *REF_CU is the CU of SRC_DIE.
24265 On exit *REF_CU is the CU of the result.
24266 The result is the DIE of the type.
24267 If the referenced type cannot be found an error is thrown. */
24268
24269 static struct die_info *
24270 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24271 struct dwarf2_cu **ref_cu)
24272 {
24273 ULONGEST signature = attr->as_signature ();
24274 struct signatured_type *sig_type;
24275 struct die_info *die;
24276
24277 gdb_assert (attr->form == DW_FORM_ref_sig8);
24278
24279 sig_type = lookup_signatured_type (*ref_cu, signature);
24280 /* sig_type will be NULL if the signatured type is missing from
24281 the debug info. */
24282 if (sig_type == NULL)
24283 {
24284 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24285 " from DIE at %s [in module %s]"),
24286 hex_string (signature), sect_offset_str (src_die->sect_off),
24287 objfile_name ((*ref_cu)->per_objfile->objfile));
24288 }
24289
24290 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24291 if (die == NULL)
24292 {
24293 dump_die_for_error (src_die);
24294 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24295 " from DIE at %s [in module %s]"),
24296 hex_string (signature), sect_offset_str (src_die->sect_off),
24297 objfile_name ((*ref_cu)->per_objfile->objfile));
24298 }
24299
24300 return die;
24301 }
24302
24303 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24304 reading in and processing the type unit if necessary. */
24305
24306 static struct type *
24307 get_signatured_type (struct die_info *die, ULONGEST signature,
24308 struct dwarf2_cu *cu)
24309 {
24310 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24311 struct signatured_type *sig_type;
24312 struct dwarf2_cu *type_cu;
24313 struct die_info *type_die;
24314 struct type *type;
24315
24316 sig_type = lookup_signatured_type (cu, signature);
24317 /* sig_type will be NULL if the signatured type is missing from
24318 the debug info. */
24319 if (sig_type == NULL)
24320 {
24321 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24322 " from DIE at %s [in module %s]"),
24323 hex_string (signature), sect_offset_str (die->sect_off),
24324 objfile_name (per_objfile->objfile));
24325 return build_error_marker_type (cu, die);
24326 }
24327
24328 /* If we already know the type we're done. */
24329 type = per_objfile->get_type_for_signatured_type (sig_type);
24330 if (type != nullptr)
24331 return type;
24332
24333 type_cu = cu;
24334 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24335 if (type_die != NULL)
24336 {
24337 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24338 is created. This is important, for example, because for c++ classes
24339 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24340 type = read_type_die (type_die, type_cu);
24341 if (type == NULL)
24342 {
24343 complaint (_("Dwarf Error: Cannot build signatured type %s"
24344 " referenced from DIE at %s [in module %s]"),
24345 hex_string (signature), sect_offset_str (die->sect_off),
24346 objfile_name (per_objfile->objfile));
24347 type = build_error_marker_type (cu, die);
24348 }
24349 }
24350 else
24351 {
24352 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24353 " from DIE at %s [in module %s]"),
24354 hex_string (signature), sect_offset_str (die->sect_off),
24355 objfile_name (per_objfile->objfile));
24356 type = build_error_marker_type (cu, die);
24357 }
24358
24359 per_objfile->set_type_for_signatured_type (sig_type, type);
24360
24361 return type;
24362 }
24363
24364 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24365 reading in and processing the type unit if necessary. */
24366
24367 static struct type *
24368 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24369 struct dwarf2_cu *cu) /* ARI: editCase function */
24370 {
24371 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24372 if (attr->form_is_ref ())
24373 {
24374 struct dwarf2_cu *type_cu = cu;
24375 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24376
24377 return read_type_die (type_die, type_cu);
24378 }
24379 else if (attr->form == DW_FORM_ref_sig8)
24380 {
24381 return get_signatured_type (die, attr->as_signature (), cu);
24382 }
24383 else
24384 {
24385 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24386
24387 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24388 " at %s [in module %s]"),
24389 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24390 objfile_name (per_objfile->objfile));
24391 return build_error_marker_type (cu, die);
24392 }
24393 }
24394
24395 /* Load the DIEs associated with type unit PER_CU into memory. */
24396
24397 static void
24398 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24399 dwarf2_per_objfile *per_objfile)
24400 {
24401 struct signatured_type *sig_type;
24402
24403 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24404 gdb_assert (! per_cu->type_unit_group_p ());
24405
24406 /* We have the per_cu, but we need the signatured_type.
24407 Fortunately this is an easy translation. */
24408 gdb_assert (per_cu->is_debug_types);
24409 sig_type = (struct signatured_type *) per_cu;
24410
24411 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24412
24413 read_signatured_type (sig_type, per_objfile);
24414
24415 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24416 }
24417
24418 /* Read in a signatured type and build its CU and DIEs.
24419 If the type is a stub for the real type in a DWO file,
24420 read in the real type from the DWO file as well. */
24421
24422 static void
24423 read_signatured_type (signatured_type *sig_type,
24424 dwarf2_per_objfile *per_objfile)
24425 {
24426 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24427
24428 gdb_assert (per_cu->is_debug_types);
24429 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24430
24431 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24432
24433 if (!reader.dummy_p)
24434 {
24435 struct dwarf2_cu *cu = reader.cu;
24436 const gdb_byte *info_ptr = reader.info_ptr;
24437
24438 gdb_assert (cu->die_hash == NULL);
24439 cu->die_hash =
24440 htab_create_alloc_ex (cu->header.length / 12,
24441 die_hash,
24442 die_eq,
24443 NULL,
24444 &cu->comp_unit_obstack,
24445 hashtab_obstack_allocate,
24446 dummy_obstack_deallocate);
24447
24448 if (reader.comp_unit_die->has_children)
24449 reader.comp_unit_die->child
24450 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24451 reader.comp_unit_die);
24452 cu->dies = reader.comp_unit_die;
24453 /* comp_unit_die is not stored in die_hash, no need. */
24454
24455 /* We try not to read any attributes in this function, because
24456 not all CUs needed for references have been loaded yet, and
24457 symbol table processing isn't initialized. But we have to
24458 set the CU language, or we won't be able to build types
24459 correctly. Similarly, if we do not read the producer, we can
24460 not apply producer-specific interpretation. */
24461 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24462
24463 reader.keep ();
24464 }
24465
24466 sig_type->per_cu.tu_read = 1;
24467 }
24468
24469 /* Decode simple location descriptions.
24470 Given a pointer to a dwarf block that defines a location, compute
24471 the location and return the value. If COMPUTED is non-null, it is
24472 set to true to indicate that decoding was successful, and false
24473 otherwise. If COMPUTED is null, then this function may emit a
24474 complaint. */
24475
24476 static CORE_ADDR
24477 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24478 {
24479 struct objfile *objfile = cu->per_objfile->objfile;
24480 size_t i;
24481 size_t size = blk->size;
24482 const gdb_byte *data = blk->data;
24483 CORE_ADDR stack[64];
24484 int stacki;
24485 unsigned int bytes_read, unsnd;
24486 gdb_byte op;
24487
24488 if (computed != nullptr)
24489 *computed = false;
24490
24491 i = 0;
24492 stacki = 0;
24493 stack[stacki] = 0;
24494 stack[++stacki] = 0;
24495
24496 while (i < size)
24497 {
24498 op = data[i++];
24499 switch (op)
24500 {
24501 case DW_OP_lit0:
24502 case DW_OP_lit1:
24503 case DW_OP_lit2:
24504 case DW_OP_lit3:
24505 case DW_OP_lit4:
24506 case DW_OP_lit5:
24507 case DW_OP_lit6:
24508 case DW_OP_lit7:
24509 case DW_OP_lit8:
24510 case DW_OP_lit9:
24511 case DW_OP_lit10:
24512 case DW_OP_lit11:
24513 case DW_OP_lit12:
24514 case DW_OP_lit13:
24515 case DW_OP_lit14:
24516 case DW_OP_lit15:
24517 case DW_OP_lit16:
24518 case DW_OP_lit17:
24519 case DW_OP_lit18:
24520 case DW_OP_lit19:
24521 case DW_OP_lit20:
24522 case DW_OP_lit21:
24523 case DW_OP_lit22:
24524 case DW_OP_lit23:
24525 case DW_OP_lit24:
24526 case DW_OP_lit25:
24527 case DW_OP_lit26:
24528 case DW_OP_lit27:
24529 case DW_OP_lit28:
24530 case DW_OP_lit29:
24531 case DW_OP_lit30:
24532 case DW_OP_lit31:
24533 stack[++stacki] = op - DW_OP_lit0;
24534 break;
24535
24536 case DW_OP_reg0:
24537 case DW_OP_reg1:
24538 case DW_OP_reg2:
24539 case DW_OP_reg3:
24540 case DW_OP_reg4:
24541 case DW_OP_reg5:
24542 case DW_OP_reg6:
24543 case DW_OP_reg7:
24544 case DW_OP_reg8:
24545 case DW_OP_reg9:
24546 case DW_OP_reg10:
24547 case DW_OP_reg11:
24548 case DW_OP_reg12:
24549 case DW_OP_reg13:
24550 case DW_OP_reg14:
24551 case DW_OP_reg15:
24552 case DW_OP_reg16:
24553 case DW_OP_reg17:
24554 case DW_OP_reg18:
24555 case DW_OP_reg19:
24556 case DW_OP_reg20:
24557 case DW_OP_reg21:
24558 case DW_OP_reg22:
24559 case DW_OP_reg23:
24560 case DW_OP_reg24:
24561 case DW_OP_reg25:
24562 case DW_OP_reg26:
24563 case DW_OP_reg27:
24564 case DW_OP_reg28:
24565 case DW_OP_reg29:
24566 case DW_OP_reg30:
24567 case DW_OP_reg31:
24568 stack[++stacki] = op - DW_OP_reg0;
24569 if (i < size)
24570 {
24571 if (computed == nullptr)
24572 dwarf2_complex_location_expr_complaint ();
24573 else
24574 return 0;
24575 }
24576 break;
24577
24578 case DW_OP_regx:
24579 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24580 i += bytes_read;
24581 stack[++stacki] = unsnd;
24582 if (i < size)
24583 {
24584 if (computed == nullptr)
24585 dwarf2_complex_location_expr_complaint ();
24586 else
24587 return 0;
24588 }
24589 break;
24590
24591 case DW_OP_addr:
24592 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24593 &bytes_read);
24594 i += bytes_read;
24595 break;
24596
24597 case DW_OP_const1u:
24598 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24599 i += 1;
24600 break;
24601
24602 case DW_OP_const1s:
24603 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24604 i += 1;
24605 break;
24606
24607 case DW_OP_const2u:
24608 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24609 i += 2;
24610 break;
24611
24612 case DW_OP_const2s:
24613 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24614 i += 2;
24615 break;
24616
24617 case DW_OP_const4u:
24618 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24619 i += 4;
24620 break;
24621
24622 case DW_OP_const4s:
24623 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24624 i += 4;
24625 break;
24626
24627 case DW_OP_const8u:
24628 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24629 i += 8;
24630 break;
24631
24632 case DW_OP_constu:
24633 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24634 &bytes_read);
24635 i += bytes_read;
24636 break;
24637
24638 case DW_OP_consts:
24639 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24640 i += bytes_read;
24641 break;
24642
24643 case DW_OP_dup:
24644 stack[stacki + 1] = stack[stacki];
24645 stacki++;
24646 break;
24647
24648 case DW_OP_plus:
24649 stack[stacki - 1] += stack[stacki];
24650 stacki--;
24651 break;
24652
24653 case DW_OP_plus_uconst:
24654 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24655 &bytes_read);
24656 i += bytes_read;
24657 break;
24658
24659 case DW_OP_minus:
24660 stack[stacki - 1] -= stack[stacki];
24661 stacki--;
24662 break;
24663
24664 case DW_OP_deref:
24665 /* If we're not the last op, then we definitely can't encode
24666 this using GDB's address_class enum. This is valid for partial
24667 global symbols, although the variable's address will be bogus
24668 in the psymtab. */
24669 if (i < size)
24670 {
24671 if (computed == nullptr)
24672 dwarf2_complex_location_expr_complaint ();
24673 else
24674 return 0;
24675 }
24676 break;
24677
24678 case DW_OP_GNU_push_tls_address:
24679 case DW_OP_form_tls_address:
24680 /* The top of the stack has the offset from the beginning
24681 of the thread control block at which the variable is located. */
24682 /* Nothing should follow this operator, so the top of stack would
24683 be returned. */
24684 /* This is valid for partial global symbols, but the variable's
24685 address will be bogus in the psymtab. Make it always at least
24686 non-zero to not look as a variable garbage collected by linker
24687 which have DW_OP_addr 0. */
24688 if (i < size)
24689 {
24690 if (computed == nullptr)
24691 dwarf2_complex_location_expr_complaint ();
24692 else
24693 return 0;
24694 }
24695 stack[stacki]++;
24696 break;
24697
24698 case DW_OP_GNU_uninit:
24699 if (computed != nullptr)
24700 return 0;
24701 break;
24702
24703 case DW_OP_addrx:
24704 case DW_OP_GNU_addr_index:
24705 case DW_OP_GNU_const_index:
24706 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24707 &bytes_read);
24708 i += bytes_read;
24709 break;
24710
24711 default:
24712 if (computed == nullptr)
24713 {
24714 const char *name = get_DW_OP_name (op);
24715
24716 if (name)
24717 complaint (_("unsupported stack op: '%s'"),
24718 name);
24719 else
24720 complaint (_("unsupported stack op: '%02x'"),
24721 op);
24722 }
24723
24724 return (stack[stacki]);
24725 }
24726
24727 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24728 outside of the allocated space. Also enforce minimum>0. */
24729 if (stacki >= ARRAY_SIZE (stack) - 1)
24730 {
24731 if (computed == nullptr)
24732 complaint (_("location description stack overflow"));
24733 return 0;
24734 }
24735
24736 if (stacki <= 0)
24737 {
24738 if (computed == nullptr)
24739 complaint (_("location description stack underflow"));
24740 return 0;
24741 }
24742 }
24743
24744 if (computed != nullptr)
24745 *computed = true;
24746 return (stack[stacki]);
24747 }
24748
24749 /* memory allocation interface */
24750
24751 static struct dwarf_block *
24752 dwarf_alloc_block (struct dwarf2_cu *cu)
24753 {
24754 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24755 }
24756
24757 static struct die_info *
24758 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24759 {
24760 struct die_info *die;
24761 size_t size = sizeof (struct die_info);
24762
24763 if (num_attrs > 1)
24764 size += (num_attrs - 1) * sizeof (struct attribute);
24765
24766 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24767 memset (die, 0, sizeof (struct die_info));
24768 return (die);
24769 }
24770
24771 \f
24772
24773 /* Macro support. */
24774
24775 /* An overload of dwarf_decode_macros that finds the correct section
24776 and ensures it is read in before calling the other overload. */
24777
24778 static void
24779 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24780 int section_is_gnu)
24781 {
24782 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24783 struct objfile *objfile = per_objfile->objfile;
24784 const struct line_header *lh = cu->line_header;
24785 unsigned int offset_size = cu->header.offset_size;
24786 struct dwarf2_section_info *section;
24787 const char *section_name;
24788
24789 if (cu->dwo_unit != nullptr)
24790 {
24791 if (section_is_gnu)
24792 {
24793 section = &cu->dwo_unit->dwo_file->sections.macro;
24794 section_name = ".debug_macro.dwo";
24795 }
24796 else
24797 {
24798 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24799 section_name = ".debug_macinfo.dwo";
24800 }
24801 }
24802 else
24803 {
24804 if (section_is_gnu)
24805 {
24806 section = &per_objfile->per_bfd->macro;
24807 section_name = ".debug_macro";
24808 }
24809 else
24810 {
24811 section = &per_objfile->per_bfd->macinfo;
24812 section_name = ".debug_macinfo";
24813 }
24814 }
24815
24816 section->read (objfile);
24817 if (section->buffer == nullptr)
24818 {
24819 complaint (_("missing %s section"), section_name);
24820 return;
24821 }
24822
24823 buildsym_compunit *builder = cu->get_builder ();
24824
24825 struct dwarf2_section_info *str_offsets_section;
24826 struct dwarf2_section_info *str_section;
24827 ULONGEST str_offsets_base;
24828
24829 if (cu->dwo_unit != nullptr)
24830 {
24831 str_offsets_section = &cu->dwo_unit->dwo_file
24832 ->sections.str_offsets;
24833 str_section = &cu->dwo_unit->dwo_file->sections.str;
24834 str_offsets_base = cu->header.addr_size;
24835 }
24836 else
24837 {
24838 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24839 str_section = &per_objfile->per_bfd->str;
24840 str_offsets_base = *cu->str_offsets_base;
24841 }
24842
24843 dwarf_decode_macros (per_objfile, builder, section, lh,
24844 offset_size, offset, str_section, str_offsets_section,
24845 str_offsets_base, section_is_gnu);
24846 }
24847
24848 /* Return the .debug_loc section to use for CU.
24849 For DWO files use .debug_loc.dwo. */
24850
24851 static struct dwarf2_section_info *
24852 cu_debug_loc_section (struct dwarf2_cu *cu)
24853 {
24854 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24855
24856 if (cu->dwo_unit)
24857 {
24858 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24859
24860 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24861 }
24862 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24863 : &per_objfile->per_bfd->loc);
24864 }
24865
24866 /* Return the .debug_rnglists section to use for CU. */
24867 static struct dwarf2_section_info *
24868 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24869 {
24870 if (cu->header.version < 5)
24871 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24872 cu->header.version);
24873 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24874
24875 /* Make sure we read the .debug_rnglists section from the file that
24876 contains the DW_AT_ranges attribute we are reading. Normally that
24877 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24878 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24879 program. */
24880 if (cu->dwo_unit != nullptr
24881 && tag != DW_TAG_compile_unit
24882 && tag != DW_TAG_skeleton_unit)
24883 {
24884 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24885
24886 if (sections->rnglists.size > 0)
24887 return &sections->rnglists;
24888 else
24889 error (_(".debug_rnglists section is missing from .dwo file."));
24890 }
24891 return &dwarf2_per_objfile->per_bfd->rnglists;
24892 }
24893
24894 /* A helper function that fills in a dwarf2_loclist_baton. */
24895
24896 static void
24897 fill_in_loclist_baton (struct dwarf2_cu *cu,
24898 struct dwarf2_loclist_baton *baton,
24899 const struct attribute *attr)
24900 {
24901 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24902 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24903
24904 section->read (per_objfile->objfile);
24905
24906 baton->per_objfile = per_objfile;
24907 baton->per_cu = cu->per_cu;
24908 gdb_assert (baton->per_cu);
24909 /* We don't know how long the location list is, but make sure we
24910 don't run off the edge of the section. */
24911 baton->size = section->size - attr->as_unsigned ();
24912 baton->data = section->buffer + attr->as_unsigned ();
24913 if (cu->base_address.has_value ())
24914 baton->base_address = *cu->base_address;
24915 else
24916 baton->base_address = 0;
24917 baton->from_dwo = cu->dwo_unit != NULL;
24918 }
24919
24920 static void
24921 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24922 struct dwarf2_cu *cu, int is_block)
24923 {
24924 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24925 struct objfile *objfile = per_objfile->objfile;
24926 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24927
24928 if (attr->form_is_section_offset ()
24929 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24930 the section. If so, fall through to the complaint in the
24931 other branch. */
24932 && attr->as_unsigned () < section->get_size (objfile))
24933 {
24934 struct dwarf2_loclist_baton *baton;
24935
24936 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24937
24938 fill_in_loclist_baton (cu, baton, attr);
24939
24940 if (!cu->base_address.has_value ())
24941 complaint (_("Location list used without "
24942 "specifying the CU base address."));
24943
24944 SYMBOL_ACLASS_INDEX (sym) = (is_block
24945 ? dwarf2_loclist_block_index
24946 : dwarf2_loclist_index);
24947 SYMBOL_LOCATION_BATON (sym) = baton;
24948 }
24949 else
24950 {
24951 struct dwarf2_locexpr_baton *baton;
24952
24953 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24954 baton->per_objfile = per_objfile;
24955 baton->per_cu = cu->per_cu;
24956 gdb_assert (baton->per_cu);
24957
24958 if (attr->form_is_block ())
24959 {
24960 /* Note that we're just copying the block's data pointer
24961 here, not the actual data. We're still pointing into the
24962 info_buffer for SYM's objfile; right now we never release
24963 that buffer, but when we do clean up properly this may
24964 need to change. */
24965 struct dwarf_block *block = attr->as_block ();
24966 baton->size = block->size;
24967 baton->data = block->data;
24968 }
24969 else
24970 {
24971 dwarf2_invalid_attrib_class_complaint ("location description",
24972 sym->natural_name ());
24973 baton->size = 0;
24974 }
24975
24976 SYMBOL_ACLASS_INDEX (sym) = (is_block
24977 ? dwarf2_locexpr_block_index
24978 : dwarf2_locexpr_index);
24979 SYMBOL_LOCATION_BATON (sym) = baton;
24980 }
24981 }
24982
24983 /* See read.h. */
24984
24985 const comp_unit_head *
24986 dwarf2_per_cu_data::get_header () const
24987 {
24988 if (!m_header_read_in)
24989 {
24990 const gdb_byte *info_ptr
24991 = this->section->buffer + to_underlying (this->sect_off);
24992
24993 memset (&m_header, 0, sizeof (m_header));
24994
24995 read_comp_unit_head (&m_header, info_ptr, this->section,
24996 rcuh_kind::COMPILE);
24997
24998 m_header_read_in = true;
24999 }
25000
25001 return &m_header;
25002 }
25003
25004 /* See read.h. */
25005
25006 int
25007 dwarf2_per_cu_data::addr_size () const
25008 {
25009 return this->get_header ()->addr_size;
25010 }
25011
25012 /* See read.h. */
25013
25014 int
25015 dwarf2_per_cu_data::offset_size () const
25016 {
25017 return this->get_header ()->offset_size;
25018 }
25019
25020 /* See read.h. */
25021
25022 int
25023 dwarf2_per_cu_data::ref_addr_size () const
25024 {
25025 const comp_unit_head *header = this->get_header ();
25026
25027 if (header->version == 2)
25028 return header->addr_size;
25029 else
25030 return header->offset_size;
25031 }
25032
25033 /* See read.h. */
25034
25035 struct type *
25036 dwarf2_cu::addr_type () const
25037 {
25038 struct objfile *objfile = this->per_objfile->objfile;
25039 struct type *void_type = objfile_type (objfile)->builtin_void;
25040 struct type *addr_type = lookup_pointer_type (void_type);
25041 int addr_size = this->per_cu->addr_size ();
25042
25043 if (TYPE_LENGTH (addr_type) == addr_size)
25044 return addr_type;
25045
25046 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
25047 return addr_type;
25048 }
25049
25050 /* A helper function for dwarf2_find_containing_comp_unit that returns
25051 the index of the result, and that searches a vector. It will
25052 return a result even if the offset in question does not actually
25053 occur in any CU. This is separate so that it can be unit
25054 tested. */
25055
25056 static int
25057 dwarf2_find_containing_comp_unit
25058 (sect_offset sect_off,
25059 unsigned int offset_in_dwz,
25060 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
25061 {
25062 int low, high;
25063
25064 low = 0;
25065 high = all_comp_units.size () - 1;
25066 while (high > low)
25067 {
25068 struct dwarf2_per_cu_data *mid_cu;
25069 int mid = low + (high - low) / 2;
25070
25071 mid_cu = all_comp_units[mid];
25072 if (mid_cu->is_dwz > offset_in_dwz
25073 || (mid_cu->is_dwz == offset_in_dwz
25074 && mid_cu->sect_off + mid_cu->length > sect_off))
25075 high = mid;
25076 else
25077 low = mid + 1;
25078 }
25079 gdb_assert (low == high);
25080 return low;
25081 }
25082
25083 /* Locate the .debug_info compilation unit from CU's objfile which contains
25084 the DIE at OFFSET. Raises an error on failure. */
25085
25086 static struct dwarf2_per_cu_data *
25087 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25088 unsigned int offset_in_dwz,
25089 dwarf2_per_objfile *per_objfile)
25090 {
25091 int low = dwarf2_find_containing_comp_unit
25092 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
25093 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
25094
25095 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25096 {
25097 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25098 error (_("Dwarf Error: could not find partial DIE containing "
25099 "offset %s [in module %s]"),
25100 sect_offset_str (sect_off),
25101 bfd_get_filename (per_objfile->objfile->obfd));
25102
25103 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
25104 <= sect_off);
25105 return per_objfile->per_bfd->all_comp_units[low-1];
25106 }
25107 else
25108 {
25109 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
25110 && sect_off >= this_cu->sect_off + this_cu->length)
25111 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25112 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25113 return this_cu;
25114 }
25115 }
25116
25117 #if GDB_SELF_TEST
25118
25119 namespace selftests {
25120 namespace find_containing_comp_unit {
25121
25122 static void
25123 run_test ()
25124 {
25125 struct dwarf2_per_cu_data one {};
25126 struct dwarf2_per_cu_data two {};
25127 struct dwarf2_per_cu_data three {};
25128 struct dwarf2_per_cu_data four {};
25129
25130 one.length = 5;
25131 two.sect_off = sect_offset (one.length);
25132 two.length = 7;
25133
25134 three.length = 5;
25135 three.is_dwz = 1;
25136 four.sect_off = sect_offset (three.length);
25137 four.length = 7;
25138 four.is_dwz = 1;
25139
25140 std::vector<dwarf2_per_cu_data *> units;
25141 units.push_back (&one);
25142 units.push_back (&two);
25143 units.push_back (&three);
25144 units.push_back (&four);
25145
25146 int result;
25147
25148 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25149 SELF_CHECK (units[result] == &one);
25150 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25151 SELF_CHECK (units[result] == &one);
25152 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25153 SELF_CHECK (units[result] == &two);
25154
25155 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25156 SELF_CHECK (units[result] == &three);
25157 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25158 SELF_CHECK (units[result] == &three);
25159 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25160 SELF_CHECK (units[result] == &four);
25161 }
25162
25163 }
25164 }
25165
25166 #endif /* GDB_SELF_TEST */
25167
25168 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
25169
25170 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25171 dwarf2_per_objfile *per_objfile)
25172 : per_cu (per_cu),
25173 per_objfile (per_objfile),
25174 mark (false),
25175 has_loclist (false),
25176 checked_producer (false),
25177 producer_is_gxx_lt_4_6 (false),
25178 producer_is_gcc_lt_4_3 (false),
25179 producer_is_icc (false),
25180 producer_is_icc_lt_14 (false),
25181 producer_is_codewarrior (false),
25182 processing_has_namespace_info (false)
25183 {
25184 }
25185
25186 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25187
25188 static void
25189 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25190 enum language pretend_language)
25191 {
25192 struct attribute *attr;
25193
25194 /* Set the language we're debugging. */
25195 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25196 if (attr != nullptr)
25197 set_cu_language (attr->constant_value (0), cu);
25198 else
25199 {
25200 cu->language = pretend_language;
25201 cu->language_defn = language_def (cu->language);
25202 }
25203
25204 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25205 }
25206
25207 /* See read.h. */
25208
25209 dwarf2_cu *
25210 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
25211 {
25212 auto it = m_dwarf2_cus.find (per_cu);
25213 if (it == m_dwarf2_cus.end ())
25214 return nullptr;
25215
25216 return it->second;
25217 }
25218
25219 /* See read.h. */
25220
25221 void
25222 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25223 {
25224 gdb_assert (this->get_cu (per_cu) == nullptr);
25225
25226 m_dwarf2_cus[per_cu] = cu;
25227 }
25228
25229 /* See read.h. */
25230
25231 void
25232 dwarf2_per_objfile::age_comp_units ()
25233 {
25234 dwarf_read_debug_printf_v ("running");
25235
25236 /* This is not expected to be called in the middle of CU expansion. There is
25237 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25238 loaded in memory. Calling age_comp_units while the queue is in use could
25239 make us free the DIEs for a CU that is in the queue and therefore break
25240 that invariant. */
25241 gdb_assert (!this->per_bfd->queue.has_value ());
25242
25243 /* Start by clearing all marks. */
25244 for (auto pair : m_dwarf2_cus)
25245 pair.second->mark = false;
25246
25247 /* Traverse all CUs, mark them and their dependencies if used recently
25248 enough. */
25249 for (auto pair : m_dwarf2_cus)
25250 {
25251 dwarf2_cu *cu = pair.second;
25252
25253 cu->last_used++;
25254 if (cu->last_used <= dwarf_max_cache_age)
25255 dwarf2_mark (cu);
25256 }
25257
25258 /* Delete all CUs still not marked. */
25259 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
25260 {
25261 dwarf2_cu *cu = it->second;
25262
25263 if (!cu->mark)
25264 {
25265 dwarf_read_debug_printf_v ("deleting old CU %s",
25266 sect_offset_str (cu->per_cu->sect_off));
25267 delete cu;
25268 it = m_dwarf2_cus.erase (it);
25269 }
25270 else
25271 it++;
25272 }
25273 }
25274
25275 /* See read.h. */
25276
25277 void
25278 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25279 {
25280 auto it = m_dwarf2_cus.find (per_cu);
25281 if (it == m_dwarf2_cus.end ())
25282 return;
25283
25284 delete it->second;
25285
25286 m_dwarf2_cus.erase (it);
25287 }
25288
25289 dwarf2_per_objfile::~dwarf2_per_objfile ()
25290 {
25291 remove_all_cus ();
25292 }
25293
25294 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25295 We store these in a hash table separate from the DIEs, and preserve them
25296 when the DIEs are flushed out of cache.
25297
25298 The CU "per_cu" pointer is needed because offset alone is not enough to
25299 uniquely identify the type. A file may have multiple .debug_types sections,
25300 or the type may come from a DWO file. Furthermore, while it's more logical
25301 to use per_cu->section+offset, with Fission the section with the data is in
25302 the DWO file but we don't know that section at the point we need it.
25303 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25304 because we can enter the lookup routine, get_die_type_at_offset, from
25305 outside this file, and thus won't necessarily have PER_CU->cu.
25306 Fortunately, PER_CU is stable for the life of the objfile. */
25307
25308 struct dwarf2_per_cu_offset_and_type
25309 {
25310 const struct dwarf2_per_cu_data *per_cu;
25311 sect_offset sect_off;
25312 struct type *type;
25313 };
25314
25315 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25316
25317 static hashval_t
25318 per_cu_offset_and_type_hash (const void *item)
25319 {
25320 const struct dwarf2_per_cu_offset_and_type *ofs
25321 = (const struct dwarf2_per_cu_offset_and_type *) item;
25322
25323 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25324 }
25325
25326 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25327
25328 static int
25329 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25330 {
25331 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25332 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25333 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25334 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25335
25336 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25337 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25338 }
25339
25340 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25341 table if necessary. For convenience, return TYPE.
25342
25343 The DIEs reading must have careful ordering to:
25344 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25345 reading current DIE.
25346 * Not trying to dereference contents of still incompletely read in types
25347 while reading in other DIEs.
25348 * Enable referencing still incompletely read in types just by a pointer to
25349 the type without accessing its fields.
25350
25351 Therefore caller should follow these rules:
25352 * Try to fetch any prerequisite types we may need to build this DIE type
25353 before building the type and calling set_die_type.
25354 * After building type call set_die_type for current DIE as soon as
25355 possible before fetching more types to complete the current type.
25356 * Make the type as complete as possible before fetching more types. */
25357
25358 static struct type *
25359 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25360 bool skip_data_location)
25361 {
25362 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25363 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25364 struct objfile *objfile = per_objfile->objfile;
25365 struct attribute *attr;
25366 struct dynamic_prop prop;
25367
25368 /* For Ada types, make sure that the gnat-specific data is always
25369 initialized (if not already set). There are a few types where
25370 we should not be doing so, because the type-specific area is
25371 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25372 where the type-specific area is used to store the floatformat).
25373 But this is not a problem, because the gnat-specific information
25374 is actually not needed for these types. */
25375 if (need_gnat_info (cu)
25376 && type->code () != TYPE_CODE_FUNC
25377 && type->code () != TYPE_CODE_FLT
25378 && type->code () != TYPE_CODE_METHODPTR
25379 && type->code () != TYPE_CODE_MEMBERPTR
25380 && type->code () != TYPE_CODE_METHOD
25381 && type->code () != TYPE_CODE_FIXED_POINT
25382 && !HAVE_GNAT_AUX_INFO (type))
25383 INIT_GNAT_SPECIFIC (type);
25384
25385 /* Read DW_AT_allocated and set in type. */
25386 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25387 if (attr != NULL)
25388 {
25389 struct type *prop_type = cu->addr_sized_int_type (false);
25390 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25391 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25392 }
25393
25394 /* Read DW_AT_associated and set in type. */
25395 attr = dwarf2_attr (die, DW_AT_associated, cu);
25396 if (attr != NULL)
25397 {
25398 struct type *prop_type = cu->addr_sized_int_type (false);
25399 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25400 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25401 }
25402
25403 /* Read DW_AT_data_location and set in type. */
25404 if (!skip_data_location)
25405 {
25406 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25407 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25408 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25409 }
25410
25411 if (per_objfile->die_type_hash == NULL)
25412 per_objfile->die_type_hash
25413 = htab_up (htab_create_alloc (127,
25414 per_cu_offset_and_type_hash,
25415 per_cu_offset_and_type_eq,
25416 NULL, xcalloc, xfree));
25417
25418 ofs.per_cu = cu->per_cu;
25419 ofs.sect_off = die->sect_off;
25420 ofs.type = type;
25421 slot = (struct dwarf2_per_cu_offset_and_type **)
25422 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25423 if (*slot)
25424 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25425 sect_offset_str (die->sect_off));
25426 *slot = XOBNEW (&objfile->objfile_obstack,
25427 struct dwarf2_per_cu_offset_and_type);
25428 **slot = ofs;
25429 return type;
25430 }
25431
25432 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25433 or return NULL if the die does not have a saved type. */
25434
25435 static struct type *
25436 get_die_type_at_offset (sect_offset sect_off,
25437 dwarf2_per_cu_data *per_cu,
25438 dwarf2_per_objfile *per_objfile)
25439 {
25440 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25441
25442 if (per_objfile->die_type_hash == NULL)
25443 return NULL;
25444
25445 ofs.per_cu = per_cu;
25446 ofs.sect_off = sect_off;
25447 slot = ((struct dwarf2_per_cu_offset_and_type *)
25448 htab_find (per_objfile->die_type_hash.get (), &ofs));
25449 if (slot)
25450 return slot->type;
25451 else
25452 return NULL;
25453 }
25454
25455 /* Look up the type for DIE in CU in die_type_hash,
25456 or return NULL if DIE does not have a saved type. */
25457
25458 static struct type *
25459 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25460 {
25461 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25462 }
25463
25464 /* Add a dependence relationship from CU to REF_PER_CU. */
25465
25466 static void
25467 dwarf2_add_dependence (struct dwarf2_cu *cu,
25468 struct dwarf2_per_cu_data *ref_per_cu)
25469 {
25470 void **slot;
25471
25472 if (cu->dependencies == NULL)
25473 cu->dependencies
25474 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25475 NULL, &cu->comp_unit_obstack,
25476 hashtab_obstack_allocate,
25477 dummy_obstack_deallocate);
25478
25479 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25480 if (*slot == NULL)
25481 *slot = ref_per_cu;
25482 }
25483
25484 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25485 Set the mark field in every compilation unit in the
25486 cache that we must keep because we are keeping CU.
25487
25488 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25489
25490 static int
25491 dwarf2_mark_helper (void **slot, void *data)
25492 {
25493 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25494 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25495 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25496
25497 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25498 reading of the chain. As such dependencies remain valid it is not much
25499 useful to track and undo them during QUIT cleanups. */
25500 if (cu == nullptr)
25501 return 1;
25502
25503 if (cu->mark)
25504 return 1;
25505
25506 cu->mark = true;
25507
25508 if (cu->dependencies != nullptr)
25509 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25510
25511 return 1;
25512 }
25513
25514 /* Set the mark field in CU and in every other compilation unit in the
25515 cache that we must keep because we are keeping CU. */
25516
25517 static void
25518 dwarf2_mark (struct dwarf2_cu *cu)
25519 {
25520 if (cu->mark)
25521 return;
25522
25523 cu->mark = true;
25524
25525 if (cu->dependencies != nullptr)
25526 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25527 }
25528
25529 /* Trivial hash function for partial_die_info: the hash value of a DIE
25530 is its offset in .debug_info for this objfile. */
25531
25532 static hashval_t
25533 partial_die_hash (const void *item)
25534 {
25535 const struct partial_die_info *part_die
25536 = (const struct partial_die_info *) item;
25537
25538 return to_underlying (part_die->sect_off);
25539 }
25540
25541 /* Trivial comparison function for partial_die_info structures: two DIEs
25542 are equal if they have the same offset. */
25543
25544 static int
25545 partial_die_eq (const void *item_lhs, const void *item_rhs)
25546 {
25547 const struct partial_die_info *part_die_lhs
25548 = (const struct partial_die_info *) item_lhs;
25549 const struct partial_die_info *part_die_rhs
25550 = (const struct partial_die_info *) item_rhs;
25551
25552 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25553 }
25554
25555 struct cmd_list_element *set_dwarf_cmdlist;
25556 struct cmd_list_element *show_dwarf_cmdlist;
25557
25558 static void
25559 show_check_physname (struct ui_file *file, int from_tty,
25560 struct cmd_list_element *c, const char *value)
25561 {
25562 fprintf_filtered (file,
25563 _("Whether to check \"physname\" is %s.\n"),
25564 value);
25565 }
25566
25567 void _initialize_dwarf2_read ();
25568 void
25569 _initialize_dwarf2_read ()
25570 {
25571 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25572 Set DWARF specific variables.\n\
25573 Configure DWARF variables such as the cache size."),
25574 &set_dwarf_cmdlist, "maintenance set dwarf ",
25575 0/*allow-unknown*/, &maintenance_set_cmdlist);
25576
25577 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25578 Show DWARF specific variables.\n\
25579 Show DWARF variables such as the cache size."),
25580 &show_dwarf_cmdlist, "maintenance show dwarf ",
25581 0/*allow-unknown*/, &maintenance_show_cmdlist);
25582
25583 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25584 &dwarf_max_cache_age, _("\
25585 Set the upper bound on the age of cached DWARF compilation units."), _("\
25586 Show the upper bound on the age of cached DWARF compilation units."), _("\
25587 A higher limit means that cached compilation units will be stored\n\
25588 in memory longer, and more total memory will be used. Zero disables\n\
25589 caching, which can slow down startup."),
25590 NULL,
25591 show_dwarf_max_cache_age,
25592 &set_dwarf_cmdlist,
25593 &show_dwarf_cmdlist);
25594
25595 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25596 Set debugging of the DWARF reader."), _("\
25597 Show debugging of the DWARF reader."), _("\
25598 When enabled (non-zero), debugging messages are printed during DWARF\n\
25599 reading and symtab expansion. A value of 1 (one) provides basic\n\
25600 information. A value greater than 1 provides more verbose information."),
25601 NULL,
25602 NULL,
25603 &setdebuglist, &showdebuglist);
25604
25605 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25606 Set debugging of the DWARF DIE reader."), _("\
25607 Show debugging of the DWARF DIE reader."), _("\
25608 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25609 The value is the maximum depth to print."),
25610 NULL,
25611 NULL,
25612 &setdebuglist, &showdebuglist);
25613
25614 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25615 Set debugging of the dwarf line reader."), _("\
25616 Show debugging of the dwarf line reader."), _("\
25617 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25618 A value of 1 (one) provides basic information.\n\
25619 A value greater than 1 provides more verbose information."),
25620 NULL,
25621 NULL,
25622 &setdebuglist, &showdebuglist);
25623
25624 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25625 Set cross-checking of \"physname\" code against demangler."), _("\
25626 Show cross-checking of \"physname\" code against demangler."), _("\
25627 When enabled, GDB's internal \"physname\" code is checked against\n\
25628 the demangler."),
25629 NULL, show_check_physname,
25630 &setdebuglist, &showdebuglist);
25631
25632 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25633 no_class, &use_deprecated_index_sections, _("\
25634 Set whether to use deprecated gdb_index sections."), _("\
25635 Show whether to use deprecated gdb_index sections."), _("\
25636 When enabled, deprecated .gdb_index sections are used anyway.\n\
25637 Normally they are ignored either because of a missing feature or\n\
25638 performance issue.\n\
25639 Warning: This option must be enabled before gdb reads the file."),
25640 NULL,
25641 NULL,
25642 &setlist, &showlist);
25643
25644 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25645 &dwarf2_locexpr_funcs);
25646 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25647 &dwarf2_loclist_funcs);
25648
25649 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25650 &dwarf2_block_frame_base_locexpr_funcs);
25651 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25652 &dwarf2_block_frame_base_loclist_funcs);
25653
25654 #if GDB_SELF_TEST
25655 selftests::register_test ("dw2_expand_symtabs_matching",
25656 selftests::dw2_expand_symtabs_matching::run_test);
25657 selftests::register_test ("dwarf2_find_containing_comp_unit",
25658 selftests::find_containing_comp_unit::run_test);
25659 #endif
25660 }
This page took 0.713878 seconds and 4 git commands to generate.