824945a95bcd79a80765b15a1639bcece68df21a
[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 block_search_flags search_flags,
2293 enum search_domain kind) override;
2294 };
2295
2296 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2297 {
2298 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2299 block_enum block_index,
2300 const char *name,
2301 domain_enum domain) override;
2302
2303 void dump (struct objfile *objfile) override;
2304
2305 void expand_symtabs_for_function (struct objfile *objfile,
2306 const char *func_name) override;
2307
2308 void map_matching_symbols
2309 (struct objfile *,
2310 const lookup_name_info &lookup_name,
2311 domain_enum domain,
2312 int global,
2313 gdb::function_view<symbol_found_callback_ftype> callback,
2314 symbol_compare_ftype *ordered_compare) override;
2315
2316 bool expand_symtabs_matching
2317 (struct objfile *objfile,
2318 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2319 const lookup_name_info *lookup_name,
2320 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2321 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2322 block_search_flags search_flags,
2323 enum search_domain kind) override;
2324 };
2325
2326 static quick_symbol_functions_up
2327 make_dwarf_gdb_index ()
2328 {
2329 return quick_symbol_functions_up (new dwarf2_gdb_index);
2330 }
2331
2332 static quick_symbol_functions_up
2333 make_dwarf_debug_names ()
2334 {
2335 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2336 }
2337
2338 /* Utility hash function for a stmt_list_hash. */
2339
2340 static hashval_t
2341 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2342 {
2343 hashval_t v = 0;
2344
2345 if (stmt_list_hash->dwo_unit != NULL)
2346 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2347 v += to_underlying (stmt_list_hash->line_sect_off);
2348 return v;
2349 }
2350
2351 /* Utility equality function for a stmt_list_hash. */
2352
2353 static int
2354 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2355 const struct stmt_list_hash *rhs)
2356 {
2357 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2358 return 0;
2359 if (lhs->dwo_unit != NULL
2360 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2361 return 0;
2362
2363 return lhs->line_sect_off == rhs->line_sect_off;
2364 }
2365
2366 /* Hash function for a quick_file_names. */
2367
2368 static hashval_t
2369 hash_file_name_entry (const void *e)
2370 {
2371 const struct quick_file_names *file_data
2372 = (const struct quick_file_names *) e;
2373
2374 return hash_stmt_list_entry (&file_data->hash);
2375 }
2376
2377 /* Equality function for a quick_file_names. */
2378
2379 static int
2380 eq_file_name_entry (const void *a, const void *b)
2381 {
2382 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2383 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2384
2385 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2386 }
2387
2388 /* Delete function for a quick_file_names. */
2389
2390 static void
2391 delete_file_name_entry (void *e)
2392 {
2393 struct quick_file_names *file_data = (struct quick_file_names *) e;
2394 int i;
2395
2396 for (i = 0; i < file_data->num_file_names; ++i)
2397 {
2398 xfree ((void*) file_data->file_names[i]);
2399 if (file_data->real_names)
2400 xfree ((void*) file_data->real_names[i]);
2401 }
2402
2403 /* The space for the struct itself lives on the obstack, so we don't
2404 free it here. */
2405 }
2406
2407 /* Create a quick_file_names hash table. */
2408
2409 static htab_up
2410 create_quick_file_names_table (unsigned int nr_initial_entries)
2411 {
2412 return htab_up (htab_create_alloc (nr_initial_entries,
2413 hash_file_name_entry, eq_file_name_entry,
2414 delete_file_name_entry, xcalloc, xfree));
2415 }
2416
2417 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2418 function is unrelated to symtabs, symtab would have to be created afterwards.
2419 You should call age_cached_comp_units after processing the CU. */
2420
2421 static dwarf2_cu *
2422 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2423 bool skip_partial)
2424 {
2425 if (per_cu->is_debug_types)
2426 load_full_type_unit (per_cu, per_objfile);
2427 else
2428 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2429 skip_partial, language_minimal);
2430
2431 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2432 if (cu == nullptr)
2433 return nullptr; /* Dummy CU. */
2434
2435 dwarf2_find_base_address (cu->dies, cu);
2436
2437 return cu;
2438 }
2439
2440 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2441
2442 static void
2443 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2444 dwarf2_per_objfile *per_objfile, bool skip_partial)
2445 {
2446 /* Skip type_unit_groups, reading the type units they contain
2447 is handled elsewhere. */
2448 if (per_cu->type_unit_group_p ())
2449 return;
2450
2451 {
2452 /* The destructor of dwarf2_queue_guard frees any entries left on
2453 the queue. After this point we're guaranteed to leave this function
2454 with the dwarf queue empty. */
2455 dwarf2_queue_guard q_guard (per_objfile);
2456
2457 if (!per_objfile->symtab_set_p (per_cu))
2458 {
2459 queue_comp_unit (per_cu, per_objfile, language_minimal);
2460 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2461
2462 /* If we just loaded a CU from a DWO, and we're working with an index
2463 that may badly handle TUs, load all the TUs in that DWO as well.
2464 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2465 if (!per_cu->is_debug_types
2466 && cu != NULL
2467 && cu->dwo_unit != NULL
2468 && per_objfile->per_bfd->index_table != NULL
2469 && per_objfile->per_bfd->index_table->version <= 7
2470 /* DWP files aren't supported yet. */
2471 && get_dwp_file (per_objfile) == NULL)
2472 queue_and_load_all_dwo_tus (cu);
2473 }
2474
2475 process_queue (per_objfile);
2476 }
2477
2478 /* Age the cache, releasing compilation units that have not
2479 been used recently. */
2480 per_objfile->age_comp_units ();
2481 }
2482
2483 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2484 the per-objfile for which this symtab is instantiated.
2485
2486 Returns the resulting symbol table. */
2487
2488 static struct compunit_symtab *
2489 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2490 dwarf2_per_objfile *per_objfile,
2491 bool skip_partial)
2492 {
2493 gdb_assert (per_objfile->per_bfd->using_index);
2494
2495 if (!per_objfile->symtab_set_p (per_cu))
2496 {
2497 free_cached_comp_units freer (per_objfile);
2498 scoped_restore decrementer = increment_reading_symtab ();
2499 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2500 process_cu_includes (per_objfile);
2501 }
2502
2503 return per_objfile->get_symtab (per_cu);
2504 }
2505
2506 /* See declaration. */
2507
2508 dwarf2_per_cu_data *
2509 dwarf2_per_bfd::get_cutu (int index)
2510 {
2511 if (index >= this->all_comp_units.size ())
2512 {
2513 index -= this->all_comp_units.size ();
2514 gdb_assert (index < this->all_type_units.size ());
2515 return &this->all_type_units[index]->per_cu;
2516 }
2517
2518 return this->all_comp_units[index];
2519 }
2520
2521 /* See declaration. */
2522
2523 dwarf2_per_cu_data *
2524 dwarf2_per_bfd::get_cu (int index)
2525 {
2526 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2527
2528 return this->all_comp_units[index];
2529 }
2530
2531 /* See declaration. */
2532
2533 signatured_type *
2534 dwarf2_per_bfd::get_tu (int index)
2535 {
2536 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2537
2538 return this->all_type_units[index];
2539 }
2540
2541 /* See read.h. */
2542
2543 dwarf2_per_cu_data *
2544 dwarf2_per_bfd::allocate_per_cu ()
2545 {
2546 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2547 result->per_bfd = this;
2548 result->index = m_num_psymtabs++;
2549 return result;
2550 }
2551
2552 /* See read.h. */
2553
2554 signatured_type *
2555 dwarf2_per_bfd::allocate_signatured_type ()
2556 {
2557 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2558 result->per_cu.per_bfd = this;
2559 result->per_cu.index = m_num_psymtabs++;
2560 return result;
2561 }
2562
2563 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2564 obstack, and constructed with the specified field values. */
2565
2566 static dwarf2_per_cu_data *
2567 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2568 struct dwarf2_section_info *section,
2569 int is_dwz,
2570 sect_offset sect_off, ULONGEST length)
2571 {
2572 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2573 the_cu->sect_off = sect_off;
2574 the_cu->length = length;
2575 the_cu->section = section;
2576 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2577 struct dwarf2_per_cu_quick_data);
2578 the_cu->is_dwz = is_dwz;
2579 return the_cu;
2580 }
2581
2582 /* A helper for create_cus_from_index that handles a given list of
2583 CUs. */
2584
2585 static void
2586 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2587 const gdb_byte *cu_list, offset_type n_elements,
2588 struct dwarf2_section_info *section,
2589 int is_dwz)
2590 {
2591 for (offset_type i = 0; i < n_elements; i += 2)
2592 {
2593 gdb_static_assert (sizeof (ULONGEST) >= 8);
2594
2595 sect_offset sect_off
2596 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2597 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2598 cu_list += 2 * 8;
2599
2600 dwarf2_per_cu_data *per_cu
2601 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2602 length);
2603 per_bfd->all_comp_units.push_back (per_cu);
2604 }
2605 }
2606
2607 /* Read the CU list from the mapped index, and use it to create all
2608 the CU objects for PER_BFD. */
2609
2610 static void
2611 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2612 const gdb_byte *cu_list, offset_type cu_list_elements,
2613 const gdb_byte *dwz_list, offset_type dwz_elements)
2614 {
2615 gdb_assert (per_bfd->all_comp_units.empty ());
2616 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2617
2618 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2619 &per_bfd->info, 0);
2620
2621 if (dwz_elements == 0)
2622 return;
2623
2624 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2625 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2626 &dwz->info, 1);
2627 }
2628
2629 /* Create the signatured type hash table from the index. */
2630
2631 static void
2632 create_signatured_type_table_from_index
2633 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2634 const gdb_byte *bytes, offset_type elements)
2635 {
2636 gdb_assert (per_bfd->all_type_units.empty ());
2637 per_bfd->all_type_units.reserve (elements / 3);
2638
2639 htab_up sig_types_hash = allocate_signatured_type_table ();
2640
2641 for (offset_type i = 0; i < elements; i += 3)
2642 {
2643 struct signatured_type *sig_type;
2644 ULONGEST signature;
2645 void **slot;
2646 cu_offset type_offset_in_tu;
2647
2648 gdb_static_assert (sizeof (ULONGEST) >= 8);
2649 sect_offset sect_off
2650 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2651 type_offset_in_tu
2652 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2653 BFD_ENDIAN_LITTLE);
2654 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2655 bytes += 3 * 8;
2656
2657 sig_type = per_bfd->allocate_signatured_type ();
2658 sig_type->signature = signature;
2659 sig_type->type_offset_in_tu = type_offset_in_tu;
2660 sig_type->per_cu.is_debug_types = 1;
2661 sig_type->per_cu.section = section;
2662 sig_type->per_cu.sect_off = sect_off;
2663 sig_type->per_cu.v.quick
2664 = OBSTACK_ZALLOC (&per_bfd->obstack,
2665 struct dwarf2_per_cu_quick_data);
2666
2667 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2668 *slot = sig_type;
2669
2670 per_bfd->all_type_units.push_back (sig_type);
2671 }
2672
2673 per_bfd->signatured_types = std::move (sig_types_hash);
2674 }
2675
2676 /* Create the signatured type hash table from .debug_names. */
2677
2678 static void
2679 create_signatured_type_table_from_debug_names
2680 (dwarf2_per_objfile *per_objfile,
2681 const mapped_debug_names &map,
2682 struct dwarf2_section_info *section,
2683 struct dwarf2_section_info *abbrev_section)
2684 {
2685 struct objfile *objfile = per_objfile->objfile;
2686
2687 section->read (objfile);
2688 abbrev_section->read (objfile);
2689
2690 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2691 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2692
2693 htab_up sig_types_hash = allocate_signatured_type_table ();
2694
2695 for (uint32_t i = 0; i < map.tu_count; ++i)
2696 {
2697 struct signatured_type *sig_type;
2698 void **slot;
2699
2700 sect_offset sect_off
2701 = (sect_offset) (extract_unsigned_integer
2702 (map.tu_table_reordered + i * map.offset_size,
2703 map.offset_size,
2704 map.dwarf5_byte_order));
2705
2706 comp_unit_head cu_header;
2707 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2708 abbrev_section,
2709 section->buffer + to_underlying (sect_off),
2710 rcuh_kind::TYPE);
2711
2712 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2713 sig_type->signature = cu_header.signature;
2714 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2715 sig_type->per_cu.is_debug_types = 1;
2716 sig_type->per_cu.section = section;
2717 sig_type->per_cu.sect_off = sect_off;
2718 sig_type->per_cu.v.quick
2719 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2720 struct dwarf2_per_cu_quick_data);
2721
2722 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2723 *slot = sig_type;
2724
2725 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2726 }
2727
2728 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2729 }
2730
2731 /* Read the address map data from the mapped index, and use it to
2732 populate the psymtabs_addrmap. */
2733
2734 static void
2735 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2736 struct mapped_index *index)
2737 {
2738 struct objfile *objfile = per_objfile->objfile;
2739 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2740 struct gdbarch *gdbarch = objfile->arch ();
2741 const gdb_byte *iter, *end;
2742 struct addrmap *mutable_map;
2743 CORE_ADDR baseaddr;
2744
2745 auto_obstack temp_obstack;
2746
2747 mutable_map = addrmap_create_mutable (&temp_obstack);
2748
2749 iter = index->address_table.data ();
2750 end = iter + index->address_table.size ();
2751
2752 baseaddr = objfile->text_section_offset ();
2753
2754 while (iter < end)
2755 {
2756 ULONGEST hi, lo, cu_index;
2757 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2758 iter += 8;
2759 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2760 iter += 8;
2761 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2762 iter += 4;
2763
2764 if (lo > hi)
2765 {
2766 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2767 hex_string (lo), hex_string (hi));
2768 continue;
2769 }
2770
2771 if (cu_index >= per_bfd->all_comp_units.size ())
2772 {
2773 complaint (_(".gdb_index address table has invalid CU number %u"),
2774 (unsigned) cu_index);
2775 continue;
2776 }
2777
2778 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2779 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2780 addrmap_set_empty (mutable_map, lo, hi - 1,
2781 per_bfd->get_cu (cu_index));
2782 }
2783
2784 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2785 &per_bfd->obstack);
2786 }
2787
2788 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2789 populate the psymtabs_addrmap. */
2790
2791 static void
2792 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2793 struct dwarf2_section_info *section)
2794 {
2795 struct objfile *objfile = per_objfile->objfile;
2796 bfd *abfd = objfile->obfd;
2797 struct gdbarch *gdbarch = objfile->arch ();
2798 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2799 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2800
2801 auto_obstack temp_obstack;
2802 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2803
2804 std::unordered_map<sect_offset,
2805 dwarf2_per_cu_data *,
2806 gdb::hash_enum<sect_offset>>
2807 debug_info_offset_to_per_cu;
2808 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
2809 {
2810 const auto insertpair
2811 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2812 if (!insertpair.second)
2813 {
2814 warning (_("Section .debug_aranges in %s has duplicate "
2815 "debug_info_offset %s, ignoring .debug_aranges."),
2816 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2817 return;
2818 }
2819 }
2820
2821 section->read (objfile);
2822
2823 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2824
2825 const gdb_byte *addr = section->buffer;
2826
2827 while (addr < section->buffer + section->size)
2828 {
2829 const gdb_byte *const entry_addr = addr;
2830 unsigned int bytes_read;
2831
2832 const LONGEST entry_length = read_initial_length (abfd, addr,
2833 &bytes_read);
2834 addr += bytes_read;
2835
2836 const gdb_byte *const entry_end = addr + entry_length;
2837 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2838 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2839 if (addr + entry_length > section->buffer + section->size)
2840 {
2841 warning (_("Section .debug_aranges in %s entry at offset %s "
2842 "length %s exceeds section length %s, "
2843 "ignoring .debug_aranges."),
2844 objfile_name (objfile),
2845 plongest (entry_addr - section->buffer),
2846 plongest (bytes_read + entry_length),
2847 pulongest (section->size));
2848 return;
2849 }
2850
2851 /* The version number. */
2852 const uint16_t version = read_2_bytes (abfd, addr);
2853 addr += 2;
2854 if (version != 2)
2855 {
2856 warning (_("Section .debug_aranges in %s entry at offset %s "
2857 "has unsupported version %d, ignoring .debug_aranges."),
2858 objfile_name (objfile),
2859 plongest (entry_addr - section->buffer), version);
2860 return;
2861 }
2862
2863 const uint64_t debug_info_offset
2864 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2865 addr += offset_size;
2866 const auto per_cu_it
2867 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2868 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2869 {
2870 warning (_("Section .debug_aranges in %s entry at offset %s "
2871 "debug_info_offset %s does not exists, "
2872 "ignoring .debug_aranges."),
2873 objfile_name (objfile),
2874 plongest (entry_addr - section->buffer),
2875 pulongest (debug_info_offset));
2876 return;
2877 }
2878 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2879
2880 const uint8_t address_size = *addr++;
2881 if (address_size < 1 || address_size > 8)
2882 {
2883 warning (_("Section .debug_aranges in %s entry at offset %s "
2884 "address_size %u is invalid, ignoring .debug_aranges."),
2885 objfile_name (objfile),
2886 plongest (entry_addr - section->buffer), address_size);
2887 return;
2888 }
2889
2890 const uint8_t segment_selector_size = *addr++;
2891 if (segment_selector_size != 0)
2892 {
2893 warning (_("Section .debug_aranges in %s entry at offset %s "
2894 "segment_selector_size %u is not supported, "
2895 "ignoring .debug_aranges."),
2896 objfile_name (objfile),
2897 plongest (entry_addr - section->buffer),
2898 segment_selector_size);
2899 return;
2900 }
2901
2902 /* Must pad to an alignment boundary that is twice the address
2903 size. It is undocumented by the DWARF standard but GCC does
2904 use it. */
2905 for (size_t padding = ((-(addr - section->buffer))
2906 & (2 * address_size - 1));
2907 padding > 0; padding--)
2908 if (*addr++ != 0)
2909 {
2910 warning (_("Section .debug_aranges in %s entry at offset %s "
2911 "padding is not zero, ignoring .debug_aranges."),
2912 objfile_name (objfile),
2913 plongest (entry_addr - section->buffer));
2914 return;
2915 }
2916
2917 for (;;)
2918 {
2919 if (addr + 2 * address_size > entry_end)
2920 {
2921 warning (_("Section .debug_aranges in %s entry at offset %s "
2922 "address list is not properly terminated, "
2923 "ignoring .debug_aranges."),
2924 objfile_name (objfile),
2925 plongest (entry_addr - section->buffer));
2926 return;
2927 }
2928 ULONGEST start = extract_unsigned_integer (addr, address_size,
2929 dwarf5_byte_order);
2930 addr += address_size;
2931 ULONGEST length = extract_unsigned_integer (addr, address_size,
2932 dwarf5_byte_order);
2933 addr += address_size;
2934 if (start == 0 && length == 0)
2935 break;
2936 if (start == 0 && !per_bfd->has_section_at_zero)
2937 {
2938 /* Symbol was eliminated due to a COMDAT group. */
2939 continue;
2940 }
2941 ULONGEST end = start + length;
2942 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2943 - baseaddr);
2944 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2945 - baseaddr);
2946 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2947 }
2948 }
2949
2950 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2951 &per_bfd->obstack);
2952 }
2953
2954 /* Find a slot in the mapped index INDEX for the object named NAME.
2955 If NAME is found, set *VEC_OUT to point to the CU vector in the
2956 constant pool and return true. If NAME cannot be found, return
2957 false. */
2958
2959 static bool
2960 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2961 offset_type **vec_out)
2962 {
2963 offset_type hash;
2964 offset_type slot, step;
2965 int (*cmp) (const char *, const char *);
2966
2967 gdb::unique_xmalloc_ptr<char> without_params;
2968 if (current_language->la_language == language_cplus
2969 || current_language->la_language == language_fortran
2970 || current_language->la_language == language_d)
2971 {
2972 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2973 not contain any. */
2974
2975 if (strchr (name, '(') != NULL)
2976 {
2977 without_params = cp_remove_params (name);
2978
2979 if (without_params != NULL)
2980 name = without_params.get ();
2981 }
2982 }
2983
2984 /* Index version 4 did not support case insensitive searches. But the
2985 indices for case insensitive languages are built in lowercase, therefore
2986 simulate our NAME being searched is also lowercased. */
2987 hash = mapped_index_string_hash ((index->version == 4
2988 && case_sensitivity == case_sensitive_off
2989 ? 5 : index->version),
2990 name);
2991
2992 slot = hash & (index->symbol_table.size () - 1);
2993 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2994 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2995
2996 for (;;)
2997 {
2998 const char *str;
2999
3000 const auto &bucket = index->symbol_table[slot];
3001 if (bucket.name == 0 && bucket.vec == 0)
3002 return false;
3003
3004 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3005 if (!cmp (name, str))
3006 {
3007 *vec_out = (offset_type *) (index->constant_pool
3008 + MAYBE_SWAP (bucket.vec));
3009 return true;
3010 }
3011
3012 slot = (slot + step) & (index->symbol_table.size () - 1);
3013 }
3014 }
3015
3016 /* A helper function that reads the .gdb_index from BUFFER and fills
3017 in MAP. FILENAME is the name of the file containing the data;
3018 it is used for error reporting. DEPRECATED_OK is true if it is
3019 ok to use deprecated sections.
3020
3021 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3022 out parameters that are filled in with information about the CU and
3023 TU lists in the section.
3024
3025 Returns true if all went well, false otherwise. */
3026
3027 static bool
3028 read_gdb_index_from_buffer (const char *filename,
3029 bool deprecated_ok,
3030 gdb::array_view<const gdb_byte> buffer,
3031 struct mapped_index *map,
3032 const gdb_byte **cu_list,
3033 offset_type *cu_list_elements,
3034 const gdb_byte **types_list,
3035 offset_type *types_list_elements)
3036 {
3037 const gdb_byte *addr = &buffer[0];
3038
3039 /* Version check. */
3040 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3041 /* Versions earlier than 3 emitted every copy of a psymbol. This
3042 causes the index to behave very poorly for certain requests. Version 3
3043 contained incomplete addrmap. So, it seems better to just ignore such
3044 indices. */
3045 if (version < 4)
3046 {
3047 static int warning_printed = 0;
3048 if (!warning_printed)
3049 {
3050 warning (_("Skipping obsolete .gdb_index section in %s."),
3051 filename);
3052 warning_printed = 1;
3053 }
3054 return 0;
3055 }
3056 /* Index version 4 uses a different hash function than index version
3057 5 and later.
3058
3059 Versions earlier than 6 did not emit psymbols for inlined
3060 functions. Using these files will cause GDB not to be able to
3061 set breakpoints on inlined functions by name, so we ignore these
3062 indices unless the user has done
3063 "set use-deprecated-index-sections on". */
3064 if (version < 6 && !deprecated_ok)
3065 {
3066 static int warning_printed = 0;
3067 if (!warning_printed)
3068 {
3069 warning (_("\
3070 Skipping deprecated .gdb_index section in %s.\n\
3071 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3072 to use the section anyway."),
3073 filename);
3074 warning_printed = 1;
3075 }
3076 return 0;
3077 }
3078 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3079 of the TU (for symbols coming from TUs),
3080 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3081 Plus gold-generated indices can have duplicate entries for global symbols,
3082 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3083 These are just performance bugs, and we can't distinguish gdb-generated
3084 indices from gold-generated ones, so issue no warning here. */
3085
3086 /* Indexes with higher version than the one supported by GDB may be no
3087 longer backward compatible. */
3088 if (version > 8)
3089 return 0;
3090
3091 map->version = version;
3092
3093 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3094
3095 int i = 0;
3096 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3097 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3098 / 8);
3099 ++i;
3100
3101 *types_list = addr + MAYBE_SWAP (metadata[i]);
3102 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3103 - MAYBE_SWAP (metadata[i]))
3104 / 8);
3105 ++i;
3106
3107 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3108 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3109 map->address_table
3110 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3111 ++i;
3112
3113 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3114 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3115 map->symbol_table
3116 = gdb::array_view<mapped_index::symbol_table_slot>
3117 ((mapped_index::symbol_table_slot *) symbol_table,
3118 (mapped_index::symbol_table_slot *) symbol_table_end);
3119
3120 ++i;
3121 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3122
3123 return 1;
3124 }
3125
3126 /* Callback types for dwarf2_read_gdb_index. */
3127
3128 typedef gdb::function_view
3129 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3130 get_gdb_index_contents_ftype;
3131 typedef gdb::function_view
3132 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3133 get_gdb_index_contents_dwz_ftype;
3134
3135 /* Read .gdb_index. If everything went ok, initialize the "quick"
3136 elements of all the CUs and return 1. Otherwise, return 0. */
3137
3138 static int
3139 dwarf2_read_gdb_index
3140 (dwarf2_per_objfile *per_objfile,
3141 get_gdb_index_contents_ftype get_gdb_index_contents,
3142 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3143 {
3144 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3145 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3146 struct dwz_file *dwz;
3147 struct objfile *objfile = per_objfile->objfile;
3148 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3149
3150 gdb::array_view<const gdb_byte> main_index_contents
3151 = get_gdb_index_contents (objfile, per_bfd);
3152
3153 if (main_index_contents.empty ())
3154 return 0;
3155
3156 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3157 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3158 use_deprecated_index_sections,
3159 main_index_contents, map.get (), &cu_list,
3160 &cu_list_elements, &types_list,
3161 &types_list_elements))
3162 return 0;
3163
3164 /* Don't use the index if it's empty. */
3165 if (map->symbol_table.empty ())
3166 return 0;
3167
3168 /* If there is a .dwz file, read it so we can get its CU list as
3169 well. */
3170 dwz = dwarf2_get_dwz_file (per_bfd);
3171 if (dwz != NULL)
3172 {
3173 struct mapped_index dwz_map;
3174 const gdb_byte *dwz_types_ignore;
3175 offset_type dwz_types_elements_ignore;
3176
3177 gdb::array_view<const gdb_byte> dwz_index_content
3178 = get_gdb_index_contents_dwz (objfile, dwz);
3179
3180 if (dwz_index_content.empty ())
3181 return 0;
3182
3183 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3184 1, dwz_index_content, &dwz_map,
3185 &dwz_list, &dwz_list_elements,
3186 &dwz_types_ignore,
3187 &dwz_types_elements_ignore))
3188 {
3189 warning (_("could not read '.gdb_index' section from %s; skipping"),
3190 bfd_get_filename (dwz->dwz_bfd.get ()));
3191 return 0;
3192 }
3193 }
3194
3195 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3196 dwz_list_elements);
3197
3198 if (types_list_elements)
3199 {
3200 /* We can only handle a single .debug_types when we have an
3201 index. */
3202 if (per_bfd->types.size () != 1)
3203 return 0;
3204
3205 dwarf2_section_info *section = &per_bfd->types[0];
3206
3207 create_signatured_type_table_from_index (per_bfd, section, types_list,
3208 types_list_elements);
3209 }
3210
3211 create_addrmap_from_index (per_objfile, map.get ());
3212
3213 per_bfd->index_table = std::move (map);
3214 per_bfd->using_index = 1;
3215 per_bfd->quick_file_names_table =
3216 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3217
3218 return 1;
3219 }
3220
3221 /* die_reader_func for dw2_get_file_names. */
3222
3223 static void
3224 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3225 struct die_info *comp_unit_die)
3226 {
3227 struct dwarf2_cu *cu = reader->cu;
3228 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3229 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3230 struct dwarf2_per_cu_data *lh_cu;
3231 struct attribute *attr;
3232 void **slot;
3233 struct quick_file_names *qfn;
3234
3235 gdb_assert (! this_cu->is_debug_types);
3236
3237 /* Our callers never want to match partial units -- instead they
3238 will match the enclosing full CU. */
3239 if (comp_unit_die->tag == DW_TAG_partial_unit)
3240 {
3241 this_cu->v.quick->no_file_data = 1;
3242 return;
3243 }
3244
3245 lh_cu = this_cu;
3246 slot = NULL;
3247
3248 line_header_up lh;
3249 sect_offset line_offset {};
3250
3251 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3252 if (attr != nullptr && attr->form_is_unsigned ())
3253 {
3254 struct quick_file_names find_entry;
3255
3256 line_offset = (sect_offset) attr->as_unsigned ();
3257
3258 /* We may have already read in this line header (TU line header sharing).
3259 If we have we're done. */
3260 find_entry.hash.dwo_unit = cu->dwo_unit;
3261 find_entry.hash.line_sect_off = line_offset;
3262 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3263 &find_entry, INSERT);
3264 if (*slot != NULL)
3265 {
3266 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3267 return;
3268 }
3269
3270 lh = dwarf_decode_line_header (line_offset, cu);
3271 }
3272 if (lh == NULL)
3273 {
3274 lh_cu->v.quick->no_file_data = 1;
3275 return;
3276 }
3277
3278 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3279 qfn->hash.dwo_unit = cu->dwo_unit;
3280 qfn->hash.line_sect_off = line_offset;
3281 gdb_assert (slot != NULL);
3282 *slot = qfn;
3283
3284 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3285
3286 int offset = 0;
3287 if (strcmp (fnd.name, "<unknown>") != 0)
3288 ++offset;
3289
3290 qfn->num_file_names = offset + lh->file_names_size ();
3291 qfn->file_names =
3292 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3293 qfn->num_file_names);
3294 if (offset != 0)
3295 qfn->file_names[0] = xstrdup (fnd.name);
3296 for (int i = 0; i < lh->file_names_size (); ++i)
3297 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3298 fnd.comp_dir).release ();
3299 qfn->real_names = NULL;
3300
3301 lh_cu->v.quick->file_names = qfn;
3302 }
3303
3304 /* A helper for the "quick" functions which attempts to read the line
3305 table for THIS_CU. */
3306
3307 static struct quick_file_names *
3308 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3309 dwarf2_per_objfile *per_objfile)
3310 {
3311 /* This should never be called for TUs. */
3312 gdb_assert (! this_cu->is_debug_types);
3313 /* Nor type unit groups. */
3314 gdb_assert (! this_cu->type_unit_group_p ());
3315
3316 if (this_cu->v.quick->file_names != NULL)
3317 return this_cu->v.quick->file_names;
3318 /* If we know there is no line data, no point in looking again. */
3319 if (this_cu->v.quick->no_file_data)
3320 return NULL;
3321
3322 cutu_reader reader (this_cu, per_objfile);
3323 if (!reader.dummy_p)
3324 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3325
3326 if (this_cu->v.quick->no_file_data)
3327 return NULL;
3328 return this_cu->v.quick->file_names;
3329 }
3330
3331 /* A helper for the "quick" functions which computes and caches the
3332 real path for a given file name from the line table. */
3333
3334 static const char *
3335 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3336 struct quick_file_names *qfn, int index)
3337 {
3338 if (qfn->real_names == NULL)
3339 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3340 qfn->num_file_names, const char *);
3341
3342 if (qfn->real_names[index] == NULL)
3343 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3344
3345 return qfn->real_names[index];
3346 }
3347
3348 struct symtab *
3349 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3350 {
3351 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3352 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3353 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3354
3355 if (cust == NULL)
3356 return NULL;
3357
3358 return compunit_primary_filetab (cust);
3359 }
3360
3361 /* Traversal function for dw2_forget_cached_source_info. */
3362
3363 static int
3364 dw2_free_cached_file_names (void **slot, void *info)
3365 {
3366 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3367
3368 if (file_data->real_names)
3369 {
3370 int i;
3371
3372 for (i = 0; i < file_data->num_file_names; ++i)
3373 {
3374 xfree ((void*) file_data->real_names[i]);
3375 file_data->real_names[i] = NULL;
3376 }
3377 }
3378
3379 return 1;
3380 }
3381
3382 void
3383 dwarf2_base_index_functions::forget_cached_source_info
3384 (struct objfile *objfile)
3385 {
3386 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3387
3388 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3389 dw2_free_cached_file_names, NULL);
3390 }
3391
3392 /* Helper function for dw2_map_symtabs_matching_filename that expands
3393 the symtabs and calls the iterator. */
3394
3395 static int
3396 dw2_map_expand_apply (struct objfile *objfile,
3397 struct dwarf2_per_cu_data *per_cu,
3398 const char *name, const char *real_path,
3399 gdb::function_view<bool (symtab *)> callback)
3400 {
3401 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3402
3403 /* Don't visit already-expanded CUs. */
3404 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3405 if (per_objfile->symtab_set_p (per_cu))
3406 return 0;
3407
3408 /* This may expand more than one symtab, and we want to iterate over
3409 all of them. */
3410 dw2_instantiate_symtab (per_cu, per_objfile, false);
3411
3412 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3413 last_made, callback);
3414 }
3415
3416 /* Implementation of the map_symtabs_matching_filename method. */
3417
3418 bool
3419 dwarf2_base_index_functions::map_symtabs_matching_filename
3420 (struct objfile *objfile, const char *name, const char *real_path,
3421 gdb::function_view<bool (symtab *)> callback)
3422 {
3423 const char *name_basename = lbasename (name);
3424 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3425
3426 /* The rule is CUs specify all the files, including those used by
3427 any TU, so there's no need to scan TUs here. */
3428
3429 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3430 {
3431 /* We only need to look at symtabs not already expanded. */
3432 if (per_objfile->symtab_set_p (per_cu))
3433 continue;
3434
3435 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3436 if (file_data == NULL)
3437 continue;
3438
3439 for (int j = 0; j < file_data->num_file_names; ++j)
3440 {
3441 const char *this_name = file_data->file_names[j];
3442 const char *this_real_name;
3443
3444 if (compare_filenames_for_search (this_name, name))
3445 {
3446 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3447 callback))
3448 return true;
3449 continue;
3450 }
3451
3452 /* Before we invoke realpath, which can get expensive when many
3453 files are involved, do a quick comparison of the basenames. */
3454 if (! basenames_may_differ
3455 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3456 continue;
3457
3458 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3459 if (compare_filenames_for_search (this_real_name, name))
3460 {
3461 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3462 callback))
3463 return true;
3464 continue;
3465 }
3466
3467 if (real_path != NULL)
3468 {
3469 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3470 gdb_assert (IS_ABSOLUTE_PATH (name));
3471 if (this_real_name != NULL
3472 && FILENAME_CMP (real_path, this_real_name) == 0)
3473 {
3474 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3475 callback))
3476 return true;
3477 continue;
3478 }
3479 }
3480 }
3481 }
3482
3483 return false;
3484 }
3485
3486 /* Struct used to manage iterating over all CUs looking for a symbol. */
3487
3488 struct dw2_symtab_iterator
3489 {
3490 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3491 dwarf2_per_objfile *per_objfile;
3492 /* If set, only look for symbols that match that block. Valid values are
3493 GLOBAL_BLOCK and STATIC_BLOCK. */
3494 gdb::optional<block_enum> block_index;
3495 /* The kind of symbol we're looking for. */
3496 domain_enum domain;
3497 /* The list of CUs from the index entry of the symbol,
3498 or NULL if not found. */
3499 offset_type *vec;
3500 /* The next element in VEC to look at. */
3501 int next;
3502 /* The number of elements in VEC, or zero if there is no match. */
3503 int length;
3504 /* Have we seen a global version of the symbol?
3505 If so we can ignore all further global instances.
3506 This is to work around gold/15646, inefficient gold-generated
3507 indices. */
3508 int global_seen;
3509 };
3510
3511 /* Initialize the index symtab iterator ITER, common part. */
3512
3513 static void
3514 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3515 dwarf2_per_objfile *per_objfile,
3516 gdb::optional<block_enum> block_index,
3517 domain_enum domain)
3518 {
3519 iter->per_objfile = per_objfile;
3520 iter->block_index = block_index;
3521 iter->domain = domain;
3522 iter->next = 0;
3523 iter->global_seen = 0;
3524 iter->vec = NULL;
3525 iter->length = 0;
3526 }
3527
3528 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3529
3530 static void
3531 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3532 dwarf2_per_objfile *per_objfile,
3533 gdb::optional<block_enum> block_index,
3534 domain_enum domain,
3535 const char *name)
3536 {
3537 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3538
3539 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3540 /* index is NULL if OBJF_READNOW. */
3541 if (index == NULL)
3542 return;
3543
3544 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3545 iter->length = MAYBE_SWAP (*iter->vec);
3546 }
3547
3548 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3549
3550 static void
3551 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3552 dwarf2_per_objfile *per_objfile,
3553 gdb::optional<block_enum> block_index,
3554 domain_enum domain, offset_type namei)
3555 {
3556 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3557
3558 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3559 /* index is NULL if OBJF_READNOW. */
3560 if (index == NULL)
3561 return;
3562
3563 gdb_assert (!index->symbol_name_slot_invalid (namei));
3564 const auto &bucket = index->symbol_table[namei];
3565
3566 iter->vec = (offset_type *) (index->constant_pool
3567 + MAYBE_SWAP (bucket.vec));
3568 iter->length = MAYBE_SWAP (*iter->vec);
3569 }
3570
3571 /* Return the next matching CU or NULL if there are no more. */
3572
3573 static struct dwarf2_per_cu_data *
3574 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3575 {
3576 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3577
3578 for ( ; iter->next < iter->length; ++iter->next)
3579 {
3580 offset_type cu_index_and_attrs =
3581 MAYBE_SWAP (iter->vec[iter->next + 1]);
3582 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3583 gdb_index_symbol_kind symbol_kind =
3584 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3585 /* Only check the symbol attributes if they're present.
3586 Indices prior to version 7 don't record them,
3587 and indices >= 7 may elide them for certain symbols
3588 (gold does this). */
3589 int attrs_valid =
3590 (per_objfile->per_bfd->index_table->version >= 7
3591 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3592
3593 /* Don't crash on bad data. */
3594 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3595 + per_objfile->per_bfd->all_type_units.size ()))
3596 {
3597 complaint (_(".gdb_index entry has bad CU index"
3598 " [in module %s]"), objfile_name (per_objfile->objfile));
3599 continue;
3600 }
3601
3602 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3603
3604 /* Skip if already read in. */
3605 if (per_objfile->symtab_set_p (per_cu))
3606 continue;
3607
3608 /* Check static vs global. */
3609 if (attrs_valid)
3610 {
3611 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3612
3613 if (iter->block_index.has_value ())
3614 {
3615 bool want_static = *iter->block_index == STATIC_BLOCK;
3616
3617 if (is_static != want_static)
3618 continue;
3619 }
3620
3621 /* Work around gold/15646. */
3622 if (!is_static
3623 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3624 {
3625 if (iter->global_seen)
3626 continue;
3627
3628 iter->global_seen = 1;
3629 }
3630 }
3631
3632 /* Only check the symbol's kind if it has one. */
3633 if (attrs_valid)
3634 {
3635 switch (iter->domain)
3636 {
3637 case VAR_DOMAIN:
3638 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3639 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3640 /* Some types are also in VAR_DOMAIN. */
3641 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3642 continue;
3643 break;
3644 case STRUCT_DOMAIN:
3645 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3646 continue;
3647 break;
3648 case LABEL_DOMAIN:
3649 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3650 continue;
3651 break;
3652 case MODULE_DOMAIN:
3653 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3654 continue;
3655 break;
3656 default:
3657 break;
3658 }
3659 }
3660
3661 ++iter->next;
3662 return per_cu;
3663 }
3664
3665 return NULL;
3666 }
3667
3668 struct compunit_symtab *
3669 dwarf2_gdb_index::lookup_symbol (struct objfile *objfile,
3670 block_enum block_index,
3671 const char *name, domain_enum domain)
3672 {
3673 struct compunit_symtab *stab_best = NULL;
3674 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3675
3676 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3677
3678 struct dw2_symtab_iterator iter;
3679 struct dwarf2_per_cu_data *per_cu;
3680
3681 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3682
3683 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3684 {
3685 struct symbol *sym, *with_opaque = NULL;
3686 struct compunit_symtab *stab
3687 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3688 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3689 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3690
3691 sym = block_find_symbol (block, name, domain,
3692 block_find_non_opaque_type_preferred,
3693 &with_opaque);
3694
3695 /* Some caution must be observed with overloaded functions
3696 and methods, since the index will not contain any overload
3697 information (but NAME might contain it). */
3698
3699 if (sym != NULL
3700 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3701 return stab;
3702 if (with_opaque != NULL
3703 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3704 stab_best = stab;
3705
3706 /* Keep looking through other CUs. */
3707 }
3708
3709 return stab_best;
3710 }
3711
3712 void
3713 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3714 bool print_bcache)
3715 {
3716 if (print_bcache)
3717 return;
3718
3719 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3720 int total = (per_objfile->per_bfd->all_comp_units.size ()
3721 + per_objfile->per_bfd->all_type_units.size ());
3722 int count = 0;
3723
3724 for (int i = 0; i < total; ++i)
3725 {
3726 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3727
3728 if (!per_objfile->symtab_set_p (per_cu))
3729 ++count;
3730 }
3731 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3732 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3733 }
3734
3735 /* This dumps minimal information about the index.
3736 It is called via "mt print objfiles".
3737 One use is to verify .gdb_index has been loaded by the
3738 gdb.dwarf2/gdb-index.exp testcase. */
3739
3740 void
3741 dwarf2_gdb_index::dump (struct objfile *objfile)
3742 {
3743 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3744
3745 gdb_assert (per_objfile->per_bfd->using_index);
3746 printf_filtered (".gdb_index:");
3747 if (per_objfile->per_bfd->index_table != NULL)
3748 {
3749 printf_filtered (" version %d\n",
3750 per_objfile->per_bfd->index_table->version);
3751 }
3752 else
3753 printf_filtered (" faked for \"readnow\"\n");
3754 printf_filtered ("\n");
3755 }
3756
3757 void
3758 dwarf2_gdb_index::expand_symtabs_for_function (struct objfile *objfile,
3759 const char *func_name)
3760 {
3761 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3762
3763 struct dw2_symtab_iterator iter;
3764 struct dwarf2_per_cu_data *per_cu;
3765
3766 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3767
3768 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3769 dw2_instantiate_symtab (per_cu, per_objfile, false);
3770
3771 }
3772
3773 void
3774 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3775 {
3776 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3777 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3778 + per_objfile->per_bfd->all_type_units.size ());
3779
3780 for (int i = 0; i < total_units; ++i)
3781 {
3782 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3783
3784 /* We don't want to directly expand a partial CU, because if we
3785 read it with the wrong language, then assertion failures can
3786 be triggered later on. See PR symtab/23010. So, tell
3787 dw2_instantiate_symtab to skip partial CUs -- any important
3788 partial CU will be read via DW_TAG_imported_unit anyway. */
3789 dw2_instantiate_symtab (per_cu, per_objfile, true);
3790 }
3791 }
3792
3793 void
3794 dwarf2_base_index_functions::expand_symtabs_with_fullname
3795 (struct objfile *objfile, const char *fullname)
3796 {
3797 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3798
3799 /* We don't need to consider type units here.
3800 This is only called for examining code, e.g. expand_line_sal.
3801 There can be an order of magnitude (or more) more type units
3802 than comp units, and we avoid them if we can. */
3803
3804 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3805 {
3806 /* We only need to look at symtabs not already expanded. */
3807 if (per_objfile->symtab_set_p (per_cu))
3808 continue;
3809
3810 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3811 if (file_data == NULL)
3812 continue;
3813
3814 for (int j = 0; j < file_data->num_file_names; ++j)
3815 {
3816 const char *this_fullname = file_data->file_names[j];
3817
3818 if (filename_cmp (this_fullname, fullname) == 0)
3819 {
3820 dw2_instantiate_symtab (per_cu, per_objfile, false);
3821 break;
3822 }
3823 }
3824 }
3825 }
3826
3827 static bool
3828 dw2_expand_symtabs_matching_symbol
3829 (mapped_index_base &index,
3830 const lookup_name_info &lookup_name_in,
3831 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3832 gdb::function_view<bool (offset_type)> match_callback,
3833 dwarf2_per_objfile *per_objfile);
3834
3835 static bool
3836 dw2_expand_symtabs_matching_one
3837 (dwarf2_per_cu_data *per_cu,
3838 dwarf2_per_objfile *per_objfile,
3839 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3840 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3841
3842 void
3843 dwarf2_gdb_index::map_matching_symbols
3844 (struct objfile *objfile,
3845 const lookup_name_info &name, domain_enum domain,
3846 int global,
3847 gdb::function_view<symbol_found_callback_ftype> callback,
3848 symbol_compare_ftype *ordered_compare)
3849 {
3850 /* Used for Ada. */
3851 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3852
3853 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3854
3855 if (per_objfile->per_bfd->index_table != nullptr)
3856 {
3857 mapped_index &index = *per_objfile->per_bfd->index_table;
3858
3859 const char *match_name = name.ada ().lookup_name ().c_str ();
3860 auto matcher = [&] (const char *symname)
3861 {
3862 if (ordered_compare == nullptr)
3863 return true;
3864 return ordered_compare (symname, match_name) == 0;
3865 };
3866
3867 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3868 [&] (offset_type namei)
3869 {
3870 struct dw2_symtab_iterator iter;
3871 struct dwarf2_per_cu_data *per_cu;
3872
3873 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3874 namei);
3875 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3876 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3877 nullptr);
3878 return true;
3879 }, per_objfile);
3880 }
3881 else
3882 {
3883 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3884 proceed assuming all symtabs have been read in. */
3885 }
3886
3887 for (compunit_symtab *cust : objfile->compunits ())
3888 {
3889 const struct block *block;
3890
3891 if (cust == NULL)
3892 continue;
3893 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3894 if (!iterate_over_symbols_terminated (block, name,
3895 domain, callback))
3896 return;
3897 }
3898 }
3899
3900 /* Starting from a search name, return the string that finds the upper
3901 bound of all strings that start with SEARCH_NAME in a sorted name
3902 list. Returns the empty string to indicate that the upper bound is
3903 the end of the list. */
3904
3905 static std::string
3906 make_sort_after_prefix_name (const char *search_name)
3907 {
3908 /* When looking to complete "func", we find the upper bound of all
3909 symbols that start with "func" by looking for where we'd insert
3910 the closest string that would follow "func" in lexicographical
3911 order. Usually, that's "func"-with-last-character-incremented,
3912 i.e. "fund". Mind non-ASCII characters, though. Usually those
3913 will be UTF-8 multi-byte sequences, but we can't be certain.
3914 Especially mind the 0xff character, which is a valid character in
3915 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3916 rule out compilers allowing it in identifiers. Note that
3917 conveniently, strcmp/strcasecmp are specified to compare
3918 characters interpreted as unsigned char. So what we do is treat
3919 the whole string as a base 256 number composed of a sequence of
3920 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3921 to 0, and carries 1 to the following more-significant position.
3922 If the very first character in SEARCH_NAME ends up incremented
3923 and carries/overflows, then the upper bound is the end of the
3924 list. The string after the empty string is also the empty
3925 string.
3926
3927 Some examples of this operation:
3928
3929 SEARCH_NAME => "+1" RESULT
3930
3931 "abc" => "abd"
3932 "ab\xff" => "ac"
3933 "\xff" "a" "\xff" => "\xff" "b"
3934 "\xff" => ""
3935 "\xff\xff" => ""
3936 "" => ""
3937
3938 Then, with these symbols for example:
3939
3940 func
3941 func1
3942 fund
3943
3944 completing "func" looks for symbols between "func" and
3945 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3946 which finds "func" and "func1", but not "fund".
3947
3948 And with:
3949
3950 funcÿ (Latin1 'ÿ' [0xff])
3951 funcÿ1
3952 fund
3953
3954 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3955 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3956
3957 And with:
3958
3959 ÿÿ (Latin1 'ÿ' [0xff])
3960 ÿÿ1
3961
3962 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3963 the end of the list.
3964 */
3965 std::string after = search_name;
3966 while (!after.empty () && (unsigned char) after.back () == 0xff)
3967 after.pop_back ();
3968 if (!after.empty ())
3969 after.back () = (unsigned char) after.back () + 1;
3970 return after;
3971 }
3972
3973 /* See declaration. */
3974
3975 std::pair<std::vector<name_component>::const_iterator,
3976 std::vector<name_component>::const_iterator>
3977 mapped_index_base::find_name_components_bounds
3978 (const lookup_name_info &lookup_name_without_params, language lang,
3979 dwarf2_per_objfile *per_objfile) const
3980 {
3981 auto *name_cmp
3982 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3983
3984 const char *lang_name
3985 = lookup_name_without_params.language_lookup_name (lang);
3986
3987 /* Comparison function object for lower_bound that matches against a
3988 given symbol name. */
3989 auto lookup_compare_lower = [&] (const name_component &elem,
3990 const char *name)
3991 {
3992 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3993 const char *elem_name = elem_qualified + elem.name_offset;
3994 return name_cmp (elem_name, name) < 0;
3995 };
3996
3997 /* Comparison function object for upper_bound that matches against a
3998 given symbol name. */
3999 auto lookup_compare_upper = [&] (const char *name,
4000 const name_component &elem)
4001 {
4002 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
4003 const char *elem_name = elem_qualified + elem.name_offset;
4004 return name_cmp (name, elem_name) < 0;
4005 };
4006
4007 auto begin = this->name_components.begin ();
4008 auto end = this->name_components.end ();
4009
4010 /* Find the lower bound. */
4011 auto lower = [&] ()
4012 {
4013 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4014 return begin;
4015 else
4016 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4017 } ();
4018
4019 /* Find the upper bound. */
4020 auto upper = [&] ()
4021 {
4022 if (lookup_name_without_params.completion_mode ())
4023 {
4024 /* In completion mode, we want UPPER to point past all
4025 symbols names that have the same prefix. I.e., with
4026 these symbols, and completing "func":
4027
4028 function << lower bound
4029 function1
4030 other_function << upper bound
4031
4032 We find the upper bound by looking for the insertion
4033 point of "func"-with-last-character-incremented,
4034 i.e. "fund". */
4035 std::string after = make_sort_after_prefix_name (lang_name);
4036 if (after.empty ())
4037 return end;
4038 return std::lower_bound (lower, end, after.c_str (),
4039 lookup_compare_lower);
4040 }
4041 else
4042 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4043 } ();
4044
4045 return {lower, upper};
4046 }
4047
4048 /* See declaration. */
4049
4050 void
4051 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4052 {
4053 if (!this->name_components.empty ())
4054 return;
4055
4056 this->name_components_casing = case_sensitivity;
4057 auto *name_cmp
4058 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4059
4060 /* The code below only knows how to break apart components of C++
4061 symbol names (and other languages that use '::' as
4062 namespace/module separator) and Ada symbol names. */
4063 auto count = this->symbol_name_count ();
4064 for (offset_type idx = 0; idx < count; idx++)
4065 {
4066 if (this->symbol_name_slot_invalid (idx))
4067 continue;
4068
4069 const char *name = this->symbol_name_at (idx, per_objfile);
4070
4071 /* Add each name component to the name component table. */
4072 unsigned int previous_len = 0;
4073
4074 if (strstr (name, "::") != nullptr)
4075 {
4076 for (unsigned int current_len = cp_find_first_component (name);
4077 name[current_len] != '\0';
4078 current_len += cp_find_first_component (name + current_len))
4079 {
4080 gdb_assert (name[current_len] == ':');
4081 this->name_components.push_back ({previous_len, idx});
4082 /* Skip the '::'. */
4083 current_len += 2;
4084 previous_len = current_len;
4085 }
4086 }
4087 else
4088 {
4089 /* Handle the Ada encoded (aka mangled) form here. */
4090 for (const char *iter = strstr (name, "__");
4091 iter != nullptr;
4092 iter = strstr (iter, "__"))
4093 {
4094 this->name_components.push_back ({previous_len, idx});
4095 iter += 2;
4096 previous_len = iter - name;
4097 }
4098 }
4099
4100 this->name_components.push_back ({previous_len, idx});
4101 }
4102
4103 /* Sort name_components elements by name. */
4104 auto name_comp_compare = [&] (const name_component &left,
4105 const name_component &right)
4106 {
4107 const char *left_qualified
4108 = this->symbol_name_at (left.idx, per_objfile);
4109 const char *right_qualified
4110 = this->symbol_name_at (right.idx, per_objfile);
4111
4112 const char *left_name = left_qualified + left.name_offset;
4113 const char *right_name = right_qualified + right.name_offset;
4114
4115 return name_cmp (left_name, right_name) < 0;
4116 };
4117
4118 std::sort (this->name_components.begin (),
4119 this->name_components.end (),
4120 name_comp_compare);
4121 }
4122
4123 /* Helper for dw2_expand_symtabs_matching that works with a
4124 mapped_index_base instead of the containing objfile. This is split
4125 to a separate function in order to be able to unit test the
4126 name_components matching using a mock mapped_index_base. For each
4127 symbol name that matches, calls MATCH_CALLBACK, passing it the
4128 symbol's index in the mapped_index_base symbol table. */
4129
4130 static bool
4131 dw2_expand_symtabs_matching_symbol
4132 (mapped_index_base &index,
4133 const lookup_name_info &lookup_name_in,
4134 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4135 gdb::function_view<bool (offset_type)> match_callback,
4136 dwarf2_per_objfile *per_objfile)
4137 {
4138 lookup_name_info lookup_name_without_params
4139 = lookup_name_in.make_ignore_params ();
4140
4141 /* Build the symbol name component sorted vector, if we haven't
4142 yet. */
4143 index.build_name_components (per_objfile);
4144
4145 /* The same symbol may appear more than once in the range though.
4146 E.g., if we're looking for symbols that complete "w", and we have
4147 a symbol named "w1::w2", we'll find the two name components for
4148 that same symbol in the range. To be sure we only call the
4149 callback once per symbol, we first collect the symbol name
4150 indexes that matched in a temporary vector and ignore
4151 duplicates. */
4152 std::vector<offset_type> matches;
4153
4154 struct name_and_matcher
4155 {
4156 symbol_name_matcher_ftype *matcher;
4157 const char *name;
4158
4159 bool operator== (const name_and_matcher &other) const
4160 {
4161 return matcher == other.matcher && strcmp (name, other.name) == 0;
4162 }
4163 };
4164
4165 /* A vector holding all the different symbol name matchers, for all
4166 languages. */
4167 std::vector<name_and_matcher> matchers;
4168
4169 for (int i = 0; i < nr_languages; i++)
4170 {
4171 enum language lang_e = (enum language) i;
4172
4173 const language_defn *lang = language_def (lang_e);
4174 symbol_name_matcher_ftype *name_matcher
4175 = lang->get_symbol_name_matcher (lookup_name_without_params);
4176
4177 name_and_matcher key {
4178 name_matcher,
4179 lookup_name_without_params.language_lookup_name (lang_e)
4180 };
4181
4182 /* Don't insert the same comparison routine more than once.
4183 Note that we do this linear walk. This is not a problem in
4184 practice because the number of supported languages is
4185 low. */
4186 if (std::find (matchers.begin (), matchers.end (), key)
4187 != matchers.end ())
4188 continue;
4189 matchers.push_back (std::move (key));
4190
4191 auto bounds
4192 = index.find_name_components_bounds (lookup_name_without_params,
4193 lang_e, per_objfile);
4194
4195 /* Now for each symbol name in range, check to see if we have a name
4196 match, and if so, call the MATCH_CALLBACK callback. */
4197
4198 for (; bounds.first != bounds.second; ++bounds.first)
4199 {
4200 const char *qualified
4201 = index.symbol_name_at (bounds.first->idx, per_objfile);
4202
4203 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4204 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4205 continue;
4206
4207 matches.push_back (bounds.first->idx);
4208 }
4209 }
4210
4211 std::sort (matches.begin (), matches.end ());
4212
4213 /* Finally call the callback, once per match. */
4214 ULONGEST prev = -1;
4215 bool result = true;
4216 for (offset_type idx : matches)
4217 {
4218 if (prev != idx)
4219 {
4220 if (!match_callback (idx))
4221 {
4222 result = false;
4223 break;
4224 }
4225 prev = idx;
4226 }
4227 }
4228
4229 /* Above we use a type wider than idx's for 'prev', since 0 and
4230 (offset_type)-1 are both possible values. */
4231 static_assert (sizeof (prev) > sizeof (offset_type), "");
4232
4233 return result;
4234 }
4235
4236 #if GDB_SELF_TEST
4237
4238 namespace selftests { namespace dw2_expand_symtabs_matching {
4239
4240 /* A mock .gdb_index/.debug_names-like name index table, enough to
4241 exercise dw2_expand_symtabs_matching_symbol, which works with the
4242 mapped_index_base interface. Builds an index from the symbol list
4243 passed as parameter to the constructor. */
4244 class mock_mapped_index : public mapped_index_base
4245 {
4246 public:
4247 mock_mapped_index (gdb::array_view<const char *> symbols)
4248 : m_symbol_table (symbols)
4249 {}
4250
4251 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4252
4253 /* Return the number of names in the symbol table. */
4254 size_t symbol_name_count () const override
4255 {
4256 return m_symbol_table.size ();
4257 }
4258
4259 /* Get the name of the symbol at IDX in the symbol table. */
4260 const char *symbol_name_at
4261 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4262 {
4263 return m_symbol_table[idx];
4264 }
4265
4266 private:
4267 gdb::array_view<const char *> m_symbol_table;
4268 };
4269
4270 /* Convenience function that converts a NULL pointer to a "<null>"
4271 string, to pass to print routines. */
4272
4273 static const char *
4274 string_or_null (const char *str)
4275 {
4276 return str != NULL ? str : "<null>";
4277 }
4278
4279 /* Check if a lookup_name_info built from
4280 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4281 index. EXPECTED_LIST is the list of expected matches, in expected
4282 matching order. If no match expected, then an empty list is
4283 specified. Returns true on success. On failure prints a warning
4284 indicating the file:line that failed, and returns false. */
4285
4286 static bool
4287 check_match (const char *file, int line,
4288 mock_mapped_index &mock_index,
4289 const char *name, symbol_name_match_type match_type,
4290 bool completion_mode,
4291 std::initializer_list<const char *> expected_list,
4292 dwarf2_per_objfile *per_objfile)
4293 {
4294 lookup_name_info lookup_name (name, match_type, completion_mode);
4295
4296 bool matched = true;
4297
4298 auto mismatch = [&] (const char *expected_str,
4299 const char *got)
4300 {
4301 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4302 "expected=\"%s\", got=\"%s\"\n"),
4303 file, line,
4304 (match_type == symbol_name_match_type::FULL
4305 ? "FULL" : "WILD"),
4306 name, string_or_null (expected_str), string_or_null (got));
4307 matched = false;
4308 };
4309
4310 auto expected_it = expected_list.begin ();
4311 auto expected_end = expected_list.end ();
4312
4313 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4314 nullptr,
4315 [&] (offset_type idx)
4316 {
4317 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4318 const char *expected_str
4319 = expected_it == expected_end ? NULL : *expected_it++;
4320
4321 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4322 mismatch (expected_str, matched_name);
4323 return true;
4324 }, per_objfile);
4325
4326 const char *expected_str
4327 = expected_it == expected_end ? NULL : *expected_it++;
4328 if (expected_str != NULL)
4329 mismatch (expected_str, NULL);
4330
4331 return matched;
4332 }
4333
4334 /* The symbols added to the mock mapped_index for testing (in
4335 canonical form). */
4336 static const char *test_symbols[] = {
4337 "function",
4338 "std::bar",
4339 "std::zfunction",
4340 "std::zfunction2",
4341 "w1::w2",
4342 "ns::foo<char*>",
4343 "ns::foo<int>",
4344 "ns::foo<long>",
4345 "ns2::tmpl<int>::foo2",
4346 "(anonymous namespace)::A::B::C",
4347
4348 /* These are used to check that the increment-last-char in the
4349 matching algorithm for completion doesn't match "t1_fund" when
4350 completing "t1_func". */
4351 "t1_func",
4352 "t1_func1",
4353 "t1_fund",
4354 "t1_fund1",
4355
4356 /* A UTF-8 name with multi-byte sequences to make sure that
4357 cp-name-parser understands this as a single identifier ("função"
4358 is "function" in PT). */
4359 u8"u8função",
4360
4361 /* \377 (0xff) is Latin1 'ÿ'. */
4362 "yfunc\377",
4363
4364 /* \377 (0xff) is Latin1 'ÿ'. */
4365 "\377",
4366 "\377\377123",
4367
4368 /* A name with all sorts of complications. Starts with "z" to make
4369 it easier for the completion tests below. */
4370 #define Z_SYM_NAME \
4371 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4372 "::tuple<(anonymous namespace)::ui*, " \
4373 "std::default_delete<(anonymous namespace)::ui>, void>"
4374
4375 Z_SYM_NAME
4376 };
4377
4378 /* Returns true if the mapped_index_base::find_name_component_bounds
4379 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4380 in completion mode. */
4381
4382 static bool
4383 check_find_bounds_finds (mapped_index_base &index,
4384 const char *search_name,
4385 gdb::array_view<const char *> expected_syms,
4386 dwarf2_per_objfile *per_objfile)
4387 {
4388 lookup_name_info lookup_name (search_name,
4389 symbol_name_match_type::FULL, true);
4390
4391 auto bounds = index.find_name_components_bounds (lookup_name,
4392 language_cplus,
4393 per_objfile);
4394
4395 size_t distance = std::distance (bounds.first, bounds.second);
4396 if (distance != expected_syms.size ())
4397 return false;
4398
4399 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4400 {
4401 auto nc_elem = bounds.first + exp_elem;
4402 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4403 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4404 return false;
4405 }
4406
4407 return true;
4408 }
4409
4410 /* Test the lower-level mapped_index::find_name_component_bounds
4411 method. */
4412
4413 static void
4414 test_mapped_index_find_name_component_bounds ()
4415 {
4416 mock_mapped_index mock_index (test_symbols);
4417
4418 mock_index.build_name_components (NULL /* per_objfile */);
4419
4420 /* Test the lower-level mapped_index::find_name_component_bounds
4421 method in completion mode. */
4422 {
4423 static const char *expected_syms[] = {
4424 "t1_func",
4425 "t1_func1",
4426 };
4427
4428 SELF_CHECK (check_find_bounds_finds
4429 (mock_index, "t1_func", expected_syms,
4430 NULL /* per_objfile */));
4431 }
4432
4433 /* Check that the increment-last-char in the name matching algorithm
4434 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4435 {
4436 static const char *expected_syms1[] = {
4437 "\377",
4438 "\377\377123",
4439 };
4440 SELF_CHECK (check_find_bounds_finds
4441 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4442
4443 static const char *expected_syms2[] = {
4444 "\377\377123",
4445 };
4446 SELF_CHECK (check_find_bounds_finds
4447 (mock_index, "\377\377", expected_syms2,
4448 NULL /* per_objfile */));
4449 }
4450 }
4451
4452 /* Test dw2_expand_symtabs_matching_symbol. */
4453
4454 static void
4455 test_dw2_expand_symtabs_matching_symbol ()
4456 {
4457 mock_mapped_index mock_index (test_symbols);
4458
4459 /* We let all tests run until the end even if some fails, for debug
4460 convenience. */
4461 bool any_mismatch = false;
4462
4463 /* Create the expected symbols list (an initializer_list). Needed
4464 because lists have commas, and we need to pass them to CHECK,
4465 which is a macro. */
4466 #define EXPECT(...) { __VA_ARGS__ }
4467
4468 /* Wrapper for check_match that passes down the current
4469 __FILE__/__LINE__. */
4470 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4471 any_mismatch |= !check_match (__FILE__, __LINE__, \
4472 mock_index, \
4473 NAME, MATCH_TYPE, COMPLETION_MODE, \
4474 EXPECTED_LIST, NULL)
4475
4476 /* Identity checks. */
4477 for (const char *sym : test_symbols)
4478 {
4479 /* Should be able to match all existing symbols. */
4480 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4481 EXPECT (sym));
4482
4483 /* Should be able to match all existing symbols with
4484 parameters. */
4485 std::string with_params = std::string (sym) + "(int)";
4486 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4487 EXPECT (sym));
4488
4489 /* Should be able to match all existing symbols with
4490 parameters and qualifiers. */
4491 with_params = std::string (sym) + " ( int ) const";
4492 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4493 EXPECT (sym));
4494
4495 /* This should really find sym, but cp-name-parser.y doesn't
4496 know about lvalue/rvalue qualifiers yet. */
4497 with_params = std::string (sym) + " ( int ) &&";
4498 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4499 {});
4500 }
4501
4502 /* Check that the name matching algorithm for completion doesn't get
4503 confused with Latin1 'ÿ' / 0xff. */
4504 {
4505 static const char str[] = "\377";
4506 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4507 EXPECT ("\377", "\377\377123"));
4508 }
4509
4510 /* Check that the increment-last-char in the matching algorithm for
4511 completion doesn't match "t1_fund" when completing "t1_func". */
4512 {
4513 static const char str[] = "t1_func";
4514 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4515 EXPECT ("t1_func", "t1_func1"));
4516 }
4517
4518 /* Check that completion mode works at each prefix of the expected
4519 symbol name. */
4520 {
4521 static const char str[] = "function(int)";
4522 size_t len = strlen (str);
4523 std::string lookup;
4524
4525 for (size_t i = 1; i < len; i++)
4526 {
4527 lookup.assign (str, i);
4528 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4529 EXPECT ("function"));
4530 }
4531 }
4532
4533 /* While "w" is a prefix of both components, the match function
4534 should still only be called once. */
4535 {
4536 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4537 EXPECT ("w1::w2"));
4538 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4539 EXPECT ("w1::w2"));
4540 }
4541
4542 /* Same, with a "complicated" symbol. */
4543 {
4544 static const char str[] = Z_SYM_NAME;
4545 size_t len = strlen (str);
4546 std::string lookup;
4547
4548 for (size_t i = 1; i < len; i++)
4549 {
4550 lookup.assign (str, i);
4551 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4552 EXPECT (Z_SYM_NAME));
4553 }
4554 }
4555
4556 /* In FULL mode, an incomplete symbol doesn't match. */
4557 {
4558 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4559 {});
4560 }
4561
4562 /* A complete symbol with parameters matches any overload, since the
4563 index has no overload info. */
4564 {
4565 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4566 EXPECT ("std::zfunction", "std::zfunction2"));
4567 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4568 EXPECT ("std::zfunction", "std::zfunction2"));
4569 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4570 EXPECT ("std::zfunction", "std::zfunction2"));
4571 }
4572
4573 /* Check that whitespace is ignored appropriately. A symbol with a
4574 template argument list. */
4575 {
4576 static const char expected[] = "ns::foo<int>";
4577 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4578 EXPECT (expected));
4579 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4580 EXPECT (expected));
4581 }
4582
4583 /* Check that whitespace is ignored appropriately. A symbol with a
4584 template argument list that includes a pointer. */
4585 {
4586 static const char expected[] = "ns::foo<char*>";
4587 /* Try both completion and non-completion modes. */
4588 static const bool completion_mode[2] = {false, true};
4589 for (size_t i = 0; i < 2; i++)
4590 {
4591 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4592 completion_mode[i], EXPECT (expected));
4593 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4594 completion_mode[i], EXPECT (expected));
4595
4596 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4597 completion_mode[i], EXPECT (expected));
4598 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4599 completion_mode[i], EXPECT (expected));
4600 }
4601 }
4602
4603 {
4604 /* Check method qualifiers are ignored. */
4605 static const char expected[] = "ns::foo<char*>";
4606 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4607 symbol_name_match_type::FULL, true, EXPECT (expected));
4608 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4609 symbol_name_match_type::FULL, true, EXPECT (expected));
4610 CHECK_MATCH ("foo < char * > ( int ) const",
4611 symbol_name_match_type::WILD, true, EXPECT (expected));
4612 CHECK_MATCH ("foo < char * > ( int ) &&",
4613 symbol_name_match_type::WILD, true, EXPECT (expected));
4614 }
4615
4616 /* Test lookup names that don't match anything. */
4617 {
4618 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4619 {});
4620
4621 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4622 {});
4623 }
4624
4625 /* Some wild matching tests, exercising "(anonymous namespace)",
4626 which should not be confused with a parameter list. */
4627 {
4628 static const char *syms[] = {
4629 "A::B::C",
4630 "B::C",
4631 "C",
4632 "A :: B :: C ( int )",
4633 "B :: C ( int )",
4634 "C ( int )",
4635 };
4636
4637 for (const char *s : syms)
4638 {
4639 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4640 EXPECT ("(anonymous namespace)::A::B::C"));
4641 }
4642 }
4643
4644 {
4645 static const char expected[] = "ns2::tmpl<int>::foo2";
4646 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4647 EXPECT (expected));
4648 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4649 EXPECT (expected));
4650 }
4651
4652 SELF_CHECK (!any_mismatch);
4653
4654 #undef EXPECT
4655 #undef CHECK_MATCH
4656 }
4657
4658 static void
4659 run_test ()
4660 {
4661 test_mapped_index_find_name_component_bounds ();
4662 test_dw2_expand_symtabs_matching_symbol ();
4663 }
4664
4665 }} // namespace selftests::dw2_expand_symtabs_matching
4666
4667 #endif /* GDB_SELF_TEST */
4668
4669 /* If FILE_MATCHER is NULL or if PER_CU has
4670 dwarf2_per_cu_quick_data::MARK set (see
4671 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4672 EXPANSION_NOTIFY on it. */
4673
4674 static bool
4675 dw2_expand_symtabs_matching_one
4676 (dwarf2_per_cu_data *per_cu,
4677 dwarf2_per_objfile *per_objfile,
4678 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4679 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4680 {
4681 if (file_matcher == NULL || per_cu->v.quick->mark)
4682 {
4683 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4684
4685 compunit_symtab *symtab
4686 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4687 gdb_assert (symtab != nullptr);
4688
4689 if (expansion_notify != NULL && symtab_was_null)
4690 return expansion_notify (symtab);
4691 }
4692 return true;
4693 }
4694
4695 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4696 matched, to expand corresponding CUs that were marked. IDX is the
4697 index of the symbol name that matched. */
4698
4699 static bool
4700 dw2_expand_marked_cus
4701 (dwarf2_per_objfile *per_objfile, offset_type idx,
4702 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4703 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4704 block_search_flags search_flags,
4705 search_domain kind)
4706 {
4707 offset_type *vec, vec_len, vec_idx;
4708 bool global_seen = false;
4709 mapped_index &index = *per_objfile->per_bfd->index_table;
4710
4711 vec = (offset_type *) (index.constant_pool
4712 + MAYBE_SWAP (index.symbol_table[idx].vec));
4713 vec_len = MAYBE_SWAP (vec[0]);
4714 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4715 {
4716 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4717 /* This value is only valid for index versions >= 7. */
4718 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4719 gdb_index_symbol_kind symbol_kind =
4720 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4721 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4722 /* Only check the symbol attributes if they're present.
4723 Indices prior to version 7 don't record them,
4724 and indices >= 7 may elide them for certain symbols
4725 (gold does this). */
4726 int attrs_valid =
4727 (index.version >= 7
4728 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4729
4730 /* Work around gold/15646. */
4731 if (attrs_valid
4732 && !is_static
4733 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4734 {
4735 if (global_seen)
4736 continue;
4737
4738 global_seen = true;
4739 }
4740
4741 /* Only check the symbol's kind if it has one. */
4742 if (attrs_valid)
4743 {
4744 if (is_static)
4745 {
4746 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4747 continue;
4748 }
4749 else
4750 {
4751 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4752 continue;
4753 }
4754
4755 switch (kind)
4756 {
4757 case VARIABLES_DOMAIN:
4758 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4759 continue;
4760 break;
4761 case FUNCTIONS_DOMAIN:
4762 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4763 continue;
4764 break;
4765 case TYPES_DOMAIN:
4766 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4767 continue;
4768 break;
4769 case MODULES_DOMAIN:
4770 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4771 continue;
4772 break;
4773 default:
4774 break;
4775 }
4776 }
4777
4778 /* Don't crash on bad data. */
4779 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4780 + per_objfile->per_bfd->all_type_units.size ()))
4781 {
4782 complaint (_(".gdb_index entry has bad CU index"
4783 " [in module %s]"), objfile_name (per_objfile->objfile));
4784 continue;
4785 }
4786
4787 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4788 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4789 expansion_notify))
4790 return false;
4791 }
4792
4793 return true;
4794 }
4795
4796 /* If FILE_MATCHER is non-NULL, set all the
4797 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4798 that match FILE_MATCHER. */
4799
4800 static void
4801 dw_expand_symtabs_matching_file_matcher
4802 (dwarf2_per_objfile *per_objfile,
4803 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4804 {
4805 if (file_matcher == NULL)
4806 return;
4807
4808 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4809 htab_eq_pointer,
4810 NULL, xcalloc, xfree));
4811 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4812 htab_eq_pointer,
4813 NULL, xcalloc, xfree));
4814
4815 /* The rule is CUs specify all the files, including those used by
4816 any TU, so there's no need to scan TUs here. */
4817
4818 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4819 {
4820 QUIT;
4821
4822 per_cu->v.quick->mark = 0;
4823
4824 /* We only need to look at symtabs not already expanded. */
4825 if (per_objfile->symtab_set_p (per_cu))
4826 continue;
4827
4828 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4829 if (file_data == NULL)
4830 continue;
4831
4832 if (htab_find (visited_not_found.get (), file_data) != NULL)
4833 continue;
4834 else if (htab_find (visited_found.get (), file_data) != NULL)
4835 {
4836 per_cu->v.quick->mark = 1;
4837 continue;
4838 }
4839
4840 for (int j = 0; j < file_data->num_file_names; ++j)
4841 {
4842 const char *this_real_name;
4843
4844 if (file_matcher (file_data->file_names[j], false))
4845 {
4846 per_cu->v.quick->mark = 1;
4847 break;
4848 }
4849
4850 /* Before we invoke realpath, which can get expensive when many
4851 files are involved, do a quick comparison of the basenames. */
4852 if (!basenames_may_differ
4853 && !file_matcher (lbasename (file_data->file_names[j]),
4854 true))
4855 continue;
4856
4857 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4858 if (file_matcher (this_real_name, false))
4859 {
4860 per_cu->v.quick->mark = 1;
4861 break;
4862 }
4863 }
4864
4865 void **slot = htab_find_slot (per_cu->v.quick->mark
4866 ? visited_found.get ()
4867 : visited_not_found.get (),
4868 file_data, INSERT);
4869 *slot = file_data;
4870 }
4871 }
4872
4873 bool
4874 dwarf2_gdb_index::expand_symtabs_matching
4875 (struct objfile *objfile,
4876 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4877 const lookup_name_info *lookup_name,
4878 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4879 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4880 block_search_flags search_flags,
4881 enum search_domain kind)
4882 {
4883 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4884
4885 /* index_table is NULL if OBJF_READNOW. */
4886 if (!per_objfile->per_bfd->index_table)
4887 return true;
4888
4889 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4890
4891 if (symbol_matcher == NULL && lookup_name == NULL)
4892 {
4893 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4894 {
4895 QUIT;
4896
4897 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4898 file_matcher,
4899 expansion_notify))
4900 return false;
4901 }
4902 return true;
4903 }
4904
4905 mapped_index &index = *per_objfile->per_bfd->index_table;
4906
4907 bool result
4908 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4909 symbol_matcher,
4910 [&] (offset_type idx)
4911 {
4912 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4913 expansion_notify, search_flags, kind))
4914 return false;
4915 return true;
4916 }, per_objfile);
4917
4918 return result;
4919 }
4920
4921 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4922 symtab. */
4923
4924 static struct compunit_symtab *
4925 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4926 CORE_ADDR pc)
4927 {
4928 int i;
4929
4930 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4931 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4932 return cust;
4933
4934 if (cust->includes == NULL)
4935 return NULL;
4936
4937 for (i = 0; cust->includes[i]; ++i)
4938 {
4939 struct compunit_symtab *s = cust->includes[i];
4940
4941 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4942 if (s != NULL)
4943 return s;
4944 }
4945
4946 return NULL;
4947 }
4948
4949 struct compunit_symtab *
4950 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4951 (struct objfile *objfile,
4952 struct bound_minimal_symbol msymbol,
4953 CORE_ADDR pc,
4954 struct obj_section *section,
4955 int warn_if_readin)
4956 {
4957 struct dwarf2_per_cu_data *data;
4958 struct compunit_symtab *result;
4959
4960 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4961 if (per_objfile->per_bfd->index_addrmap == nullptr)
4962 return NULL;
4963
4964 CORE_ADDR baseaddr = objfile->text_section_offset ();
4965 data = ((struct dwarf2_per_cu_data *)
4966 addrmap_find (per_objfile->per_bfd->index_addrmap,
4967 pc - baseaddr));
4968 if (!data)
4969 return NULL;
4970
4971 if (warn_if_readin && per_objfile->symtab_set_p (data))
4972 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4973 paddress (objfile->arch (), pc));
4974
4975 result = recursively_find_pc_sect_compunit_symtab
4976 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4977
4978 gdb_assert (result != NULL);
4979 return result;
4980 }
4981
4982 void
4983 dwarf2_base_index_functions::map_symbol_filenames
4984 (struct objfile *objfile,
4985 gdb::function_view<symbol_filename_ftype> fun,
4986 bool need_fullname)
4987 {
4988 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4989
4990 if (!per_objfile->per_bfd->filenames_cache)
4991 {
4992 per_objfile->per_bfd->filenames_cache.emplace ();
4993
4994 htab_up visited (htab_create_alloc (10,
4995 htab_hash_pointer, htab_eq_pointer,
4996 NULL, xcalloc, xfree));
4997
4998 /* The rule is CUs specify all the files, including those used
4999 by any TU, so there's no need to scan TUs here. We can
5000 ignore file names coming from already-expanded CUs. */
5001
5002 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5003 {
5004 if (per_objfile->symtab_set_p (per_cu))
5005 {
5006 void **slot = htab_find_slot (visited.get (),
5007 per_cu->v.quick->file_names,
5008 INSERT);
5009
5010 *slot = per_cu->v.quick->file_names;
5011 }
5012 }
5013
5014 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5015 {
5016 /* We only need to look at symtabs not already expanded. */
5017 if (per_objfile->symtab_set_p (per_cu))
5018 continue;
5019
5020 quick_file_names *file_data
5021 = dw2_get_file_names (per_cu, per_objfile);
5022 if (file_data == NULL)
5023 continue;
5024
5025 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5026 if (*slot)
5027 {
5028 /* Already visited. */
5029 continue;
5030 }
5031 *slot = file_data;
5032
5033 for (int j = 0; j < file_data->num_file_names; ++j)
5034 {
5035 const char *filename = file_data->file_names[j];
5036 per_objfile->per_bfd->filenames_cache->seen (filename);
5037 }
5038 }
5039 }
5040
5041 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
5042 {
5043 gdb::unique_xmalloc_ptr<char> this_real_name;
5044
5045 if (need_fullname)
5046 this_real_name = gdb_realpath (filename);
5047 fun (filename, this_real_name.get ());
5048 });
5049 }
5050
5051 bool
5052 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
5053 {
5054 return true;
5055 }
5056
5057 /* DWARF-5 debug_names reader. */
5058
5059 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5060 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5061
5062 /* A helper function that reads the .debug_names section in SECTION
5063 and fills in MAP. FILENAME is the name of the file containing the
5064 section; it is used for error reporting.
5065
5066 Returns true if all went well, false otherwise. */
5067
5068 static bool
5069 read_debug_names_from_section (struct objfile *objfile,
5070 const char *filename,
5071 struct dwarf2_section_info *section,
5072 mapped_debug_names &map)
5073 {
5074 if (section->empty ())
5075 return false;
5076
5077 /* Older elfutils strip versions could keep the section in the main
5078 executable while splitting it for the separate debug info file. */
5079 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5080 return false;
5081
5082 section->read (objfile);
5083
5084 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5085
5086 const gdb_byte *addr = section->buffer;
5087
5088 bfd *const abfd = section->get_bfd_owner ();
5089
5090 unsigned int bytes_read;
5091 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5092 addr += bytes_read;
5093
5094 map.dwarf5_is_dwarf64 = bytes_read != 4;
5095 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5096 if (bytes_read + length != section->size)
5097 {
5098 /* There may be multiple per-CU indices. */
5099 warning (_("Section .debug_names in %s length %s does not match "
5100 "section length %s, ignoring .debug_names."),
5101 filename, plongest (bytes_read + length),
5102 pulongest (section->size));
5103 return false;
5104 }
5105
5106 /* The version number. */
5107 uint16_t version = read_2_bytes (abfd, addr);
5108 addr += 2;
5109 if (version != 5)
5110 {
5111 warning (_("Section .debug_names in %s has unsupported version %d, "
5112 "ignoring .debug_names."),
5113 filename, version);
5114 return false;
5115 }
5116
5117 /* Padding. */
5118 uint16_t padding = read_2_bytes (abfd, addr);
5119 addr += 2;
5120 if (padding != 0)
5121 {
5122 warning (_("Section .debug_names in %s has unsupported padding %d, "
5123 "ignoring .debug_names."),
5124 filename, padding);
5125 return false;
5126 }
5127
5128 /* comp_unit_count - The number of CUs in the CU list. */
5129 map.cu_count = read_4_bytes (abfd, addr);
5130 addr += 4;
5131
5132 /* local_type_unit_count - The number of TUs in the local TU
5133 list. */
5134 map.tu_count = read_4_bytes (abfd, addr);
5135 addr += 4;
5136
5137 /* foreign_type_unit_count - The number of TUs in the foreign TU
5138 list. */
5139 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5140 addr += 4;
5141 if (foreign_tu_count != 0)
5142 {
5143 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5144 "ignoring .debug_names."),
5145 filename, static_cast<unsigned long> (foreign_tu_count));
5146 return false;
5147 }
5148
5149 /* bucket_count - The number of hash buckets in the hash lookup
5150 table. */
5151 map.bucket_count = read_4_bytes (abfd, addr);
5152 addr += 4;
5153
5154 /* name_count - The number of unique names in the index. */
5155 map.name_count = read_4_bytes (abfd, addr);
5156 addr += 4;
5157
5158 /* abbrev_table_size - The size in bytes of the abbreviations
5159 table. */
5160 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5161 addr += 4;
5162
5163 /* augmentation_string_size - The size in bytes of the augmentation
5164 string. This value is rounded up to a multiple of 4. */
5165 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5166 addr += 4;
5167 map.augmentation_is_gdb = ((augmentation_string_size
5168 == sizeof (dwarf5_augmentation))
5169 && memcmp (addr, dwarf5_augmentation,
5170 sizeof (dwarf5_augmentation)) == 0);
5171 augmentation_string_size += (-augmentation_string_size) & 3;
5172 addr += augmentation_string_size;
5173
5174 /* List of CUs */
5175 map.cu_table_reordered = addr;
5176 addr += map.cu_count * map.offset_size;
5177
5178 /* List of Local TUs */
5179 map.tu_table_reordered = addr;
5180 addr += map.tu_count * map.offset_size;
5181
5182 /* Hash Lookup Table */
5183 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5184 addr += map.bucket_count * 4;
5185 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5186 addr += map.name_count * 4;
5187
5188 /* Name Table */
5189 map.name_table_string_offs_reordered = addr;
5190 addr += map.name_count * map.offset_size;
5191 map.name_table_entry_offs_reordered = addr;
5192 addr += map.name_count * map.offset_size;
5193
5194 const gdb_byte *abbrev_table_start = addr;
5195 for (;;)
5196 {
5197 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5198 addr += bytes_read;
5199 if (index_num == 0)
5200 break;
5201
5202 const auto insertpair
5203 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5204 if (!insertpair.second)
5205 {
5206 warning (_("Section .debug_names in %s has duplicate index %s, "
5207 "ignoring .debug_names."),
5208 filename, pulongest (index_num));
5209 return false;
5210 }
5211 mapped_debug_names::index_val &indexval = insertpair.first->second;
5212 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5213 addr += bytes_read;
5214
5215 for (;;)
5216 {
5217 mapped_debug_names::index_val::attr attr;
5218 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5219 addr += bytes_read;
5220 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5221 addr += bytes_read;
5222 if (attr.form == DW_FORM_implicit_const)
5223 {
5224 attr.implicit_const = read_signed_leb128 (abfd, addr,
5225 &bytes_read);
5226 addr += bytes_read;
5227 }
5228 if (attr.dw_idx == 0 && attr.form == 0)
5229 break;
5230 indexval.attr_vec.push_back (std::move (attr));
5231 }
5232 }
5233 if (addr != abbrev_table_start + abbrev_table_size)
5234 {
5235 warning (_("Section .debug_names in %s has abbreviation_table "
5236 "of size %s vs. written as %u, ignoring .debug_names."),
5237 filename, plongest (addr - abbrev_table_start),
5238 abbrev_table_size);
5239 return false;
5240 }
5241 map.entry_pool = addr;
5242
5243 return true;
5244 }
5245
5246 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5247 list. */
5248
5249 static void
5250 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5251 const mapped_debug_names &map,
5252 dwarf2_section_info &section,
5253 bool is_dwz)
5254 {
5255 if (!map.augmentation_is_gdb)
5256 {
5257 for (uint32_t i = 0; i < map.cu_count; ++i)
5258 {
5259 sect_offset sect_off
5260 = (sect_offset) (extract_unsigned_integer
5261 (map.cu_table_reordered + i * map.offset_size,
5262 map.offset_size,
5263 map.dwarf5_byte_order));
5264 /* We don't know the length of the CU, because the CU list in a
5265 .debug_names index can be incomplete, so we can't use the start
5266 of the next CU as end of this CU. We create the CUs here with
5267 length 0, and in cutu_reader::cutu_reader we'll fill in the
5268 actual length. */
5269 dwarf2_per_cu_data *per_cu
5270 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5271 sect_off, 0);
5272 per_bfd->all_comp_units.push_back (per_cu);
5273 }
5274 return;
5275 }
5276
5277 sect_offset sect_off_prev;
5278 for (uint32_t i = 0; i <= map.cu_count; ++i)
5279 {
5280 sect_offset sect_off_next;
5281 if (i < map.cu_count)
5282 {
5283 sect_off_next
5284 = (sect_offset) (extract_unsigned_integer
5285 (map.cu_table_reordered + i * map.offset_size,
5286 map.offset_size,
5287 map.dwarf5_byte_order));
5288 }
5289 else
5290 sect_off_next = (sect_offset) section.size;
5291 if (i >= 1)
5292 {
5293 const ULONGEST length = sect_off_next - sect_off_prev;
5294 dwarf2_per_cu_data *per_cu
5295 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5296 sect_off_prev, length);
5297 per_bfd->all_comp_units.push_back (per_cu);
5298 }
5299 sect_off_prev = sect_off_next;
5300 }
5301 }
5302
5303 /* Read the CU list from the mapped index, and use it to create all
5304 the CU objects for this dwarf2_per_objfile. */
5305
5306 static void
5307 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5308 const mapped_debug_names &map,
5309 const mapped_debug_names &dwz_map)
5310 {
5311 gdb_assert (per_bfd->all_comp_units.empty ());
5312 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5313
5314 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5315 false /* is_dwz */);
5316
5317 if (dwz_map.cu_count == 0)
5318 return;
5319
5320 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5321 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5322 true /* is_dwz */);
5323 }
5324
5325 /* Read .debug_names. If everything went ok, initialize the "quick"
5326 elements of all the CUs and return true. Otherwise, return false. */
5327
5328 static bool
5329 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5330 {
5331 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5332 mapped_debug_names dwz_map;
5333 struct objfile *objfile = per_objfile->objfile;
5334 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5335
5336 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5337 &per_bfd->debug_names, *map))
5338 return false;
5339
5340 /* Don't use the index if it's empty. */
5341 if (map->name_count == 0)
5342 return false;
5343
5344 /* If there is a .dwz file, read it so we can get its CU list as
5345 well. */
5346 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5347 if (dwz != NULL)
5348 {
5349 if (!read_debug_names_from_section (objfile,
5350 bfd_get_filename (dwz->dwz_bfd.get ()),
5351 &dwz->debug_names, dwz_map))
5352 {
5353 warning (_("could not read '.debug_names' section from %s; skipping"),
5354 bfd_get_filename (dwz->dwz_bfd.get ()));
5355 return false;
5356 }
5357 }
5358
5359 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5360
5361 if (map->tu_count != 0)
5362 {
5363 /* We can only handle a single .debug_types when we have an
5364 index. */
5365 if (per_bfd->types.size () != 1)
5366 return false;
5367
5368 dwarf2_section_info *section = &per_bfd->types[0];
5369
5370 create_signatured_type_table_from_debug_names
5371 (per_objfile, *map, section, &per_bfd->abbrev);
5372 }
5373
5374 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5375
5376 per_bfd->debug_names_table = std::move (map);
5377 per_bfd->using_index = 1;
5378 per_bfd->quick_file_names_table =
5379 create_quick_file_names_table (per_bfd->all_comp_units.size ());
5380
5381 return true;
5382 }
5383
5384 /* Type used to manage iterating over all CUs looking for a symbol for
5385 .debug_names. */
5386
5387 class dw2_debug_names_iterator
5388 {
5389 public:
5390 dw2_debug_names_iterator (const mapped_debug_names &map,
5391 block_search_flags block_index,
5392 domain_enum domain,
5393 const char *name, dwarf2_per_objfile *per_objfile)
5394 : m_map (map), m_block_index (block_index), m_domain (domain),
5395 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5396 m_per_objfile (per_objfile)
5397 {}
5398
5399 dw2_debug_names_iterator (const mapped_debug_names &map,
5400 search_domain search, uint32_t namei,
5401 dwarf2_per_objfile *per_objfile)
5402 : m_map (map),
5403 m_search (search),
5404 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5405 m_per_objfile (per_objfile)
5406 {}
5407
5408 dw2_debug_names_iterator (const mapped_debug_names &map,
5409 block_search_flags block_index, domain_enum domain,
5410 uint32_t namei, dwarf2_per_objfile *per_objfile)
5411 : m_map (map), m_block_index (block_index), m_domain (domain),
5412 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5413 m_per_objfile (per_objfile)
5414 {}
5415
5416 /* Return the next matching CU or NULL if there are no more. */
5417 dwarf2_per_cu_data *next ();
5418
5419 private:
5420 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5421 const char *name,
5422 dwarf2_per_objfile *per_objfile);
5423 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5424 uint32_t namei,
5425 dwarf2_per_objfile *per_objfile);
5426
5427 /* The internalized form of .debug_names. */
5428 const mapped_debug_names &m_map;
5429
5430 /* Restrict the search to these blocks. */
5431 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
5432 | SEARCH_STATIC_BLOCK);
5433
5434 /* The kind of symbol we're looking for. */
5435 const domain_enum m_domain = UNDEF_DOMAIN;
5436 const search_domain m_search = ALL_DOMAIN;
5437
5438 /* The list of CUs from the index entry of the symbol, or NULL if
5439 not found. */
5440 const gdb_byte *m_addr;
5441
5442 dwarf2_per_objfile *m_per_objfile;
5443 };
5444
5445 const char *
5446 mapped_debug_names::namei_to_name
5447 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5448 {
5449 const ULONGEST namei_string_offs
5450 = extract_unsigned_integer ((name_table_string_offs_reordered
5451 + namei * offset_size),
5452 offset_size,
5453 dwarf5_byte_order);
5454 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5455 }
5456
5457 /* Find a slot in .debug_names for the object named NAME. If NAME is
5458 found, return pointer to its pool data. If NAME cannot be found,
5459 return NULL. */
5460
5461 const gdb_byte *
5462 dw2_debug_names_iterator::find_vec_in_debug_names
5463 (const mapped_debug_names &map, const char *name,
5464 dwarf2_per_objfile *per_objfile)
5465 {
5466 int (*cmp) (const char *, const char *);
5467
5468 gdb::unique_xmalloc_ptr<char> without_params;
5469 if (current_language->la_language == language_cplus
5470 || current_language->la_language == language_fortran
5471 || current_language->la_language == language_d)
5472 {
5473 /* NAME is already canonical. Drop any qualifiers as
5474 .debug_names does not contain any. */
5475
5476 if (strchr (name, '(') != NULL)
5477 {
5478 without_params = cp_remove_params (name);
5479 if (without_params != NULL)
5480 name = without_params.get ();
5481 }
5482 }
5483
5484 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5485
5486 const uint32_t full_hash = dwarf5_djb_hash (name);
5487 uint32_t namei
5488 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5489 (map.bucket_table_reordered
5490 + (full_hash % map.bucket_count)), 4,
5491 map.dwarf5_byte_order);
5492 if (namei == 0)
5493 return NULL;
5494 --namei;
5495 if (namei >= map.name_count)
5496 {
5497 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5498 "[in module %s]"),
5499 namei, map.name_count,
5500 objfile_name (per_objfile->objfile));
5501 return NULL;
5502 }
5503
5504 for (;;)
5505 {
5506 const uint32_t namei_full_hash
5507 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5508 (map.hash_table_reordered + namei), 4,
5509 map.dwarf5_byte_order);
5510 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5511 return NULL;
5512
5513 if (full_hash == namei_full_hash)
5514 {
5515 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5516
5517 #if 0 /* An expensive sanity check. */
5518 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5519 {
5520 complaint (_("Wrong .debug_names hash for string at index %u "
5521 "[in module %s]"),
5522 namei, objfile_name (dwarf2_per_objfile->objfile));
5523 return NULL;
5524 }
5525 #endif
5526
5527 if (cmp (namei_string, name) == 0)
5528 {
5529 const ULONGEST namei_entry_offs
5530 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5531 + namei * map.offset_size),
5532 map.offset_size, map.dwarf5_byte_order);
5533 return map.entry_pool + namei_entry_offs;
5534 }
5535 }
5536
5537 ++namei;
5538 if (namei >= map.name_count)
5539 return NULL;
5540 }
5541 }
5542
5543 const gdb_byte *
5544 dw2_debug_names_iterator::find_vec_in_debug_names
5545 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5546 {
5547 if (namei >= map.name_count)
5548 {
5549 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5550 "[in module %s]"),
5551 namei, map.name_count,
5552 objfile_name (per_objfile->objfile));
5553 return NULL;
5554 }
5555
5556 const ULONGEST namei_entry_offs
5557 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5558 + namei * map.offset_size),
5559 map.offset_size, map.dwarf5_byte_order);
5560 return map.entry_pool + namei_entry_offs;
5561 }
5562
5563 /* See dw2_debug_names_iterator. */
5564
5565 dwarf2_per_cu_data *
5566 dw2_debug_names_iterator::next ()
5567 {
5568 if (m_addr == NULL)
5569 return NULL;
5570
5571 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5572 struct objfile *objfile = m_per_objfile->objfile;
5573 bfd *const abfd = objfile->obfd;
5574
5575 again:
5576
5577 unsigned int bytes_read;
5578 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5579 m_addr += bytes_read;
5580 if (abbrev == 0)
5581 return NULL;
5582
5583 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5584 if (indexval_it == m_map.abbrev_map.cend ())
5585 {
5586 complaint (_("Wrong .debug_names undefined abbrev code %s "
5587 "[in module %s]"),
5588 pulongest (abbrev), objfile_name (objfile));
5589 return NULL;
5590 }
5591 const mapped_debug_names::index_val &indexval = indexval_it->second;
5592 enum class symbol_linkage {
5593 unknown,
5594 static_,
5595 extern_,
5596 } symbol_linkage_ = symbol_linkage::unknown;
5597 dwarf2_per_cu_data *per_cu = NULL;
5598 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5599 {
5600 ULONGEST ull;
5601 switch (attr.form)
5602 {
5603 case DW_FORM_implicit_const:
5604 ull = attr.implicit_const;
5605 break;
5606 case DW_FORM_flag_present:
5607 ull = 1;
5608 break;
5609 case DW_FORM_udata:
5610 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5611 m_addr += bytes_read;
5612 break;
5613 case DW_FORM_ref4:
5614 ull = read_4_bytes (abfd, m_addr);
5615 m_addr += 4;
5616 break;
5617 case DW_FORM_ref8:
5618 ull = read_8_bytes (abfd, m_addr);
5619 m_addr += 8;
5620 break;
5621 case DW_FORM_ref_sig8:
5622 ull = read_8_bytes (abfd, m_addr);
5623 m_addr += 8;
5624 break;
5625 default:
5626 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5627 dwarf_form_name (attr.form),
5628 objfile_name (objfile));
5629 return NULL;
5630 }
5631 switch (attr.dw_idx)
5632 {
5633 case DW_IDX_compile_unit:
5634 /* Don't crash on bad data. */
5635 if (ull >= per_bfd->all_comp_units.size ())
5636 {
5637 complaint (_(".debug_names entry has bad CU index %s"
5638 " [in module %s]"),
5639 pulongest (ull),
5640 objfile_name (objfile));
5641 continue;
5642 }
5643 per_cu = per_bfd->get_cutu (ull);
5644 break;
5645 case DW_IDX_type_unit:
5646 /* Don't crash on bad data. */
5647 if (ull >= per_bfd->all_type_units.size ())
5648 {
5649 complaint (_(".debug_names entry has bad TU index %s"
5650 " [in module %s]"),
5651 pulongest (ull),
5652 objfile_name (objfile));
5653 continue;
5654 }
5655 per_cu = &per_bfd->get_tu (ull)->per_cu;
5656 break;
5657 case DW_IDX_die_offset:
5658 /* In a per-CU index (as opposed to a per-module index), index
5659 entries without CU attribute implicitly refer to the single CU. */
5660 if (per_cu == NULL)
5661 per_cu = per_bfd->get_cu (0);
5662 break;
5663 case DW_IDX_GNU_internal:
5664 if (!m_map.augmentation_is_gdb)
5665 break;
5666 symbol_linkage_ = symbol_linkage::static_;
5667 break;
5668 case DW_IDX_GNU_external:
5669 if (!m_map.augmentation_is_gdb)
5670 break;
5671 symbol_linkage_ = symbol_linkage::extern_;
5672 break;
5673 }
5674 }
5675
5676 /* Skip if already read in. */
5677 if (m_per_objfile->symtab_set_p (per_cu))
5678 goto again;
5679
5680 /* Check static vs global. */
5681 if (symbol_linkage_ != symbol_linkage::unknown)
5682 {
5683 if (symbol_linkage_ == symbol_linkage::static_)
5684 {
5685 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5686 goto again;
5687 }
5688 else
5689 {
5690 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5691 goto again;
5692 }
5693 }
5694
5695 /* Match dw2_symtab_iter_next, symbol_kind
5696 and debug_names::psymbol_tag. */
5697 switch (m_domain)
5698 {
5699 case VAR_DOMAIN:
5700 switch (indexval.dwarf_tag)
5701 {
5702 case DW_TAG_variable:
5703 case DW_TAG_subprogram:
5704 /* Some types are also in VAR_DOMAIN. */
5705 case DW_TAG_typedef:
5706 case DW_TAG_structure_type:
5707 break;
5708 default:
5709 goto again;
5710 }
5711 break;
5712 case STRUCT_DOMAIN:
5713 switch (indexval.dwarf_tag)
5714 {
5715 case DW_TAG_typedef:
5716 case DW_TAG_structure_type:
5717 break;
5718 default:
5719 goto again;
5720 }
5721 break;
5722 case LABEL_DOMAIN:
5723 switch (indexval.dwarf_tag)
5724 {
5725 case 0:
5726 case DW_TAG_variable:
5727 break;
5728 default:
5729 goto again;
5730 }
5731 break;
5732 case MODULE_DOMAIN:
5733 switch (indexval.dwarf_tag)
5734 {
5735 case DW_TAG_module:
5736 break;
5737 default:
5738 goto again;
5739 }
5740 break;
5741 default:
5742 break;
5743 }
5744
5745 /* Match dw2_expand_symtabs_matching, symbol_kind and
5746 debug_names::psymbol_tag. */
5747 switch (m_search)
5748 {
5749 case VARIABLES_DOMAIN:
5750 switch (indexval.dwarf_tag)
5751 {
5752 case DW_TAG_variable:
5753 break;
5754 default:
5755 goto again;
5756 }
5757 break;
5758 case FUNCTIONS_DOMAIN:
5759 switch (indexval.dwarf_tag)
5760 {
5761 case DW_TAG_subprogram:
5762 break;
5763 default:
5764 goto again;
5765 }
5766 break;
5767 case TYPES_DOMAIN:
5768 switch (indexval.dwarf_tag)
5769 {
5770 case DW_TAG_typedef:
5771 case DW_TAG_structure_type:
5772 break;
5773 default:
5774 goto again;
5775 }
5776 break;
5777 case MODULES_DOMAIN:
5778 switch (indexval.dwarf_tag)
5779 {
5780 case DW_TAG_module:
5781 break;
5782 default:
5783 goto again;
5784 }
5785 default:
5786 break;
5787 }
5788
5789 return per_cu;
5790 }
5791
5792 struct compunit_symtab *
5793 dwarf2_debug_names_index::lookup_symbol
5794 (struct objfile *objfile, block_enum block_index,
5795 const char *name, domain_enum domain)
5796 {
5797 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5798
5799 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5800 if (!mapp)
5801 {
5802 /* index is NULL if OBJF_READNOW. */
5803 return NULL;
5804 }
5805 const auto &map = *mapp;
5806
5807 dw2_debug_names_iterator iter (map,
5808 block_index == GLOBAL_BLOCK
5809 ? SEARCH_GLOBAL_BLOCK
5810 : SEARCH_STATIC_BLOCK,
5811 domain, name, per_objfile);
5812
5813 struct compunit_symtab *stab_best = NULL;
5814 struct dwarf2_per_cu_data *per_cu;
5815 while ((per_cu = iter.next ()) != NULL)
5816 {
5817 struct symbol *sym, *with_opaque = NULL;
5818 compunit_symtab *stab
5819 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5820 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5821 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5822
5823 sym = block_find_symbol (block, name, domain,
5824 block_find_non_opaque_type_preferred,
5825 &with_opaque);
5826
5827 /* Some caution must be observed with overloaded functions and
5828 methods, since the index will not contain any overload
5829 information (but NAME might contain it). */
5830
5831 if (sym != NULL
5832 && strcmp_iw (sym->search_name (), name) == 0)
5833 return stab;
5834 if (with_opaque != NULL
5835 && strcmp_iw (with_opaque->search_name (), name) == 0)
5836 stab_best = stab;
5837
5838 /* Keep looking through other CUs. */
5839 }
5840
5841 return stab_best;
5842 }
5843
5844 /* This dumps minimal information about .debug_names. It is called
5845 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5846 uses this to verify that .debug_names has been loaded. */
5847
5848 void
5849 dwarf2_debug_names_index::dump (struct objfile *objfile)
5850 {
5851 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5852
5853 gdb_assert (per_objfile->per_bfd->using_index);
5854 printf_filtered (".debug_names:");
5855 if (per_objfile->per_bfd->debug_names_table)
5856 printf_filtered (" exists\n");
5857 else
5858 printf_filtered (" faked for \"readnow\"\n");
5859 printf_filtered ("\n");
5860 }
5861
5862 void
5863 dwarf2_debug_names_index::expand_symtabs_for_function
5864 (struct objfile *objfile, const char *func_name)
5865 {
5866 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5867
5868 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5869 if (per_objfile->per_bfd->debug_names_table)
5870 {
5871 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5872
5873 dw2_debug_names_iterator iter (map,
5874 (SEARCH_GLOBAL_BLOCK
5875 | SEARCH_STATIC_BLOCK),
5876 VAR_DOMAIN, func_name,
5877 per_objfile);
5878
5879 struct dwarf2_per_cu_data *per_cu;
5880 while ((per_cu = iter.next ()) != NULL)
5881 dw2_instantiate_symtab (per_cu, per_objfile, false);
5882 }
5883 }
5884
5885 void
5886 dwarf2_debug_names_index::map_matching_symbols
5887 (struct objfile *objfile,
5888 const lookup_name_info &name, domain_enum domain,
5889 int global,
5890 gdb::function_view<symbol_found_callback_ftype> callback,
5891 symbol_compare_ftype *ordered_compare)
5892 {
5893 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5894
5895 /* debug_names_table is NULL if OBJF_READNOW. */
5896 if (!per_objfile->per_bfd->debug_names_table)
5897 return;
5898
5899 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5900 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5901 const block_search_flags block_flags
5902 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5903
5904 const char *match_name = name.ada ().lookup_name ().c_str ();
5905 auto matcher = [&] (const char *symname)
5906 {
5907 if (ordered_compare == nullptr)
5908 return true;
5909 return ordered_compare (symname, match_name) == 0;
5910 };
5911
5912 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5913 [&] (offset_type namei)
5914 {
5915 /* The name was matched, now expand corresponding CUs that were
5916 marked. */
5917 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5918 per_objfile);
5919
5920 struct dwarf2_per_cu_data *per_cu;
5921 while ((per_cu = iter.next ()) != NULL)
5922 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5923 nullptr);
5924 return true;
5925 }, per_objfile);
5926
5927 /* It's a shame we couldn't do this inside the
5928 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5929 that have already been expanded. Instead, this loop matches what
5930 the psymtab code does. */
5931 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5932 {
5933 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5934 if (symtab != nullptr)
5935 {
5936 const struct block *block
5937 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5938 if (!iterate_over_symbols_terminated (block, name,
5939 domain, callback))
5940 break;
5941 }
5942 }
5943 }
5944
5945 bool
5946 dwarf2_debug_names_index::expand_symtabs_matching
5947 (struct objfile *objfile,
5948 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5949 const lookup_name_info *lookup_name,
5950 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5951 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5952 block_search_flags search_flags,
5953 enum search_domain kind)
5954 {
5955 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5956
5957 /* debug_names_table is NULL if OBJF_READNOW. */
5958 if (!per_objfile->per_bfd->debug_names_table)
5959 return true;
5960
5961 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5962
5963 if (symbol_matcher == NULL && lookup_name == NULL)
5964 {
5965 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5966 {
5967 QUIT;
5968
5969 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5970 file_matcher,
5971 expansion_notify))
5972 return false;
5973 }
5974 return true;
5975 }
5976
5977 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5978
5979 bool result
5980 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5981 symbol_matcher,
5982 [&] (offset_type namei)
5983 {
5984 /* The name was matched, now expand corresponding CUs that were
5985 marked. */
5986 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5987
5988 struct dwarf2_per_cu_data *per_cu;
5989 while ((per_cu = iter.next ()) != NULL)
5990 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5991 file_matcher,
5992 expansion_notify))
5993 return false;
5994 return true;
5995 }, per_objfile);
5996
5997 return result;
5998 }
5999
6000 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6001 to either a dwarf2_per_bfd or dwz_file object. */
6002
6003 template <typename T>
6004 static gdb::array_view<const gdb_byte>
6005 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6006 {
6007 dwarf2_section_info *section = &section_owner->gdb_index;
6008
6009 if (section->empty ())
6010 return {};
6011
6012 /* Older elfutils strip versions could keep the section in the main
6013 executable while splitting it for the separate debug info file. */
6014 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
6015 return {};
6016
6017 section->read (obj);
6018
6019 /* dwarf2_section_info::size is a bfd_size_type, while
6020 gdb::array_view works with size_t. On 32-bit hosts, with
6021 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6022 is 32-bit. So we need an explicit narrowing conversion here.
6023 This is fine, because it's impossible to allocate or mmap an
6024 array/buffer larger than what size_t can represent. */
6025 return gdb::make_array_view (section->buffer, section->size);
6026 }
6027
6028 /* Lookup the index cache for the contents of the index associated to
6029 DWARF2_OBJ. */
6030
6031 static gdb::array_view<const gdb_byte>
6032 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
6033 {
6034 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6035 if (build_id == nullptr)
6036 return {};
6037
6038 return global_index_cache.lookup_gdb_index (build_id,
6039 &dwarf2_per_bfd->index_cache_res);
6040 }
6041
6042 /* Same as the above, but for DWZ. */
6043
6044 static gdb::array_view<const gdb_byte>
6045 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6046 {
6047 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6048 if (build_id == nullptr)
6049 return {};
6050
6051 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6052 }
6053
6054 /* See dwarf2/public.h. */
6055
6056 void
6057 dwarf2_initialize_objfile (struct objfile *objfile)
6058 {
6059 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6060 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6061
6062 dwarf_read_debug_printf ("called");
6063
6064 /* If we're about to read full symbols, don't bother with the
6065 indices. In this case we also don't care if some other debug
6066 format is making psymtabs, because they are all about to be
6067 expanded anyway. */
6068 if ((objfile->flags & OBJF_READNOW))
6069 {
6070 dwarf_read_debug_printf ("readnow requested");
6071
6072 /* When using READNOW, the using_index flag (set below) indicates that
6073 PER_BFD was already initialized, when we loaded some other objfile. */
6074 if (per_bfd->using_index)
6075 {
6076 dwarf_read_debug_printf ("using_index already set");
6077 per_objfile->resize_symtabs ();
6078 objfile->qf.push_front (make_dwarf_gdb_index ());
6079 return;
6080 }
6081
6082 per_bfd->using_index = 1;
6083 create_all_comp_units (per_objfile);
6084 create_all_type_units (per_objfile);
6085 per_bfd->quick_file_names_table
6086 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6087 per_objfile->resize_symtabs ();
6088
6089 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6090 + per_bfd->all_type_units.size ()); ++i)
6091 {
6092 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6093
6094 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6095 struct dwarf2_per_cu_quick_data);
6096 }
6097
6098 /* Arrange for gdb to see the "quick" functions. However, these
6099 functions will be no-ops because we will have expanded all
6100 symtabs. */
6101 objfile->qf.push_front (make_dwarf_gdb_index ());
6102 return;
6103 }
6104
6105 /* Was a debug names index already read when we processed an objfile sharing
6106 PER_BFD? */
6107 if (per_bfd->debug_names_table != nullptr)
6108 {
6109 dwarf_read_debug_printf ("re-using shared debug names table");
6110 per_objfile->resize_symtabs ();
6111 objfile->qf.push_front (make_dwarf_debug_names ());
6112 return;
6113 }
6114
6115 /* Was a GDB index already read when we processed an objfile sharing
6116 PER_BFD? */
6117 if (per_bfd->index_table != nullptr)
6118 {
6119 dwarf_read_debug_printf ("re-using shared index table");
6120 per_objfile->resize_symtabs ();
6121 objfile->qf.push_front (make_dwarf_gdb_index ());
6122 return;
6123 }
6124
6125 /* There might already be partial symtabs built for this BFD. This happens
6126 when loading the same binary twice with the index-cache enabled. If so,
6127 don't try to read an index. The objfile / per_objfile initialization will
6128 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6129 code path. */
6130 if (per_bfd->partial_symtabs != nullptr)
6131 {
6132 dwarf_read_debug_printf ("re-using shared partial symtabs");
6133 objfile->qf.push_front (make_lazy_dwarf_reader ());
6134 return;
6135 }
6136
6137 if (dwarf2_read_debug_names (per_objfile))
6138 {
6139 dwarf_read_debug_printf ("found debug names");
6140 per_objfile->resize_symtabs ();
6141 objfile->qf.push_front (make_dwarf_debug_names ());
6142 return;
6143 }
6144
6145 if (dwarf2_read_gdb_index (per_objfile,
6146 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6147 get_gdb_index_contents_from_section<dwz_file>))
6148 {
6149 dwarf_read_debug_printf ("found gdb index from file");
6150 per_objfile->resize_symtabs ();
6151 objfile->qf.push_front (make_dwarf_gdb_index ());
6152 return;
6153 }
6154
6155 /* ... otherwise, try to find the index in the index cache. */
6156 if (dwarf2_read_gdb_index (per_objfile,
6157 get_gdb_index_contents_from_cache,
6158 get_gdb_index_contents_from_cache_dwz))
6159 {
6160 dwarf_read_debug_printf ("found gdb index from cache");
6161 global_index_cache.hit ();
6162 per_objfile->resize_symtabs ();
6163 objfile->qf.push_front (make_dwarf_gdb_index ());
6164 return;
6165 }
6166
6167 global_index_cache.miss ();
6168 objfile->qf.push_front (make_lazy_dwarf_reader ());
6169 }
6170
6171 \f
6172
6173 /* Build a partial symbol table. */
6174
6175 void
6176 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
6177 {
6178 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6179 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6180
6181 if (per_bfd->partial_symtabs != nullptr)
6182 {
6183 /* Partial symbols were already read, so now we can simply
6184 attach them. */
6185 if (psf == nullptr)
6186 {
6187 psf = new psymbol_functions (per_bfd->partial_symtabs);
6188 objfile->qf.emplace_front (psf);
6189 }
6190 else
6191 psf->set_partial_symtabs (per_bfd->partial_symtabs);
6192 per_objfile->resize_symtabs ();
6193 return;
6194 }
6195
6196 if (psf == nullptr)
6197 {
6198 psf = new psymbol_functions;
6199 objfile->qf.emplace_front (psf);
6200 }
6201 const std::shared_ptr<psymtab_storage> &partial_symtabs
6202 = psf->get_partial_symtabs ();
6203
6204 /* Set the local reference to partial symtabs, so that we don't try
6205 to read them again if reading another objfile with the same BFD.
6206 If we can't in fact share, this won't make a difference anyway as
6207 the dwarf2_per_bfd object won't be shared. */
6208 per_bfd->partial_symtabs = partial_symtabs;
6209
6210 try
6211 {
6212 /* This isn't really ideal: all the data we allocate on the
6213 objfile's obstack is still uselessly kept around. However,
6214 freeing it seems unsafe. */
6215 psymtab_discarder psymtabs (partial_symtabs.get ());
6216 dwarf2_build_psymtabs_hard (per_objfile);
6217 psymtabs.keep ();
6218
6219 per_objfile->resize_symtabs ();
6220
6221 /* (maybe) store an index in the cache. */
6222 global_index_cache.store (per_objfile);
6223 }
6224 catch (const gdb_exception_error &except)
6225 {
6226 exception_print (gdb_stderr, except);
6227 }
6228 }
6229
6230 /* Find the base address of the compilation unit for range lists and
6231 location lists. It will normally be specified by DW_AT_low_pc.
6232 In DWARF-3 draft 4, the base address could be overridden by
6233 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6234 compilation units with discontinuous ranges. */
6235
6236 static void
6237 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6238 {
6239 struct attribute *attr;
6240
6241 cu->base_address.reset ();
6242
6243 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6244 if (attr != nullptr)
6245 cu->base_address = attr->as_address ();
6246 else
6247 {
6248 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6249 if (attr != nullptr)
6250 cu->base_address = attr->as_address ();
6251 }
6252 }
6253
6254 /* Helper function that returns the proper abbrev section for
6255 THIS_CU. */
6256
6257 static struct dwarf2_section_info *
6258 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6259 {
6260 struct dwarf2_section_info *abbrev;
6261 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6262
6263 if (this_cu->is_dwz)
6264 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
6265 else
6266 abbrev = &per_bfd->abbrev;
6267
6268 return abbrev;
6269 }
6270
6271 /* Fetch the abbreviation table offset from a comp or type unit header. */
6272
6273 static sect_offset
6274 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6275 struct dwarf2_section_info *section,
6276 sect_offset sect_off)
6277 {
6278 bfd *abfd = section->get_bfd_owner ();
6279 const gdb_byte *info_ptr;
6280 unsigned int initial_length_size, offset_size;
6281 uint16_t version;
6282
6283 section->read (per_objfile->objfile);
6284 info_ptr = section->buffer + to_underlying (sect_off);
6285 read_initial_length (abfd, info_ptr, &initial_length_size);
6286 offset_size = initial_length_size == 4 ? 4 : 8;
6287 info_ptr += initial_length_size;
6288
6289 version = read_2_bytes (abfd, info_ptr);
6290 info_ptr += 2;
6291 if (version >= 5)
6292 {
6293 /* Skip unit type and address size. */
6294 info_ptr += 2;
6295 }
6296
6297 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6298 }
6299
6300 /* A partial symtab that is used only for include files. */
6301 struct dwarf2_include_psymtab : public partial_symtab
6302 {
6303 dwarf2_include_psymtab (const char *filename,
6304 psymtab_storage *partial_symtabs,
6305 objfile_per_bfd_storage *objfile_per_bfd)
6306 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
6307 {
6308 }
6309
6310 void read_symtab (struct objfile *objfile) override
6311 {
6312 /* It's an include file, no symbols to read for it.
6313 Everything is in the includer symtab. */
6314
6315 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6316 expansion of the includer psymtab. We use the dependencies[0] field to
6317 model the includer. But if we go the regular route of calling
6318 expand_psymtab here, and having expand_psymtab call expand_dependencies
6319 to expand the includer, we'll only use expand_psymtab on the includer
6320 (making it a non-toplevel psymtab), while if we expand the includer via
6321 another path, we'll use read_symtab (making it a toplevel psymtab).
6322 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6323 psymtab, and trigger read_symtab on the includer here directly. */
6324 includer ()->read_symtab (objfile);
6325 }
6326
6327 void expand_psymtab (struct objfile *objfile) override
6328 {
6329 /* This is not called by read_symtab, and should not be called by any
6330 expand_dependencies. */
6331 gdb_assert (false);
6332 }
6333
6334 bool readin_p (struct objfile *objfile) const override
6335 {
6336 return includer ()->readin_p (objfile);
6337 }
6338
6339 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6340 {
6341 return nullptr;
6342 }
6343
6344 private:
6345 partial_symtab *includer () const
6346 {
6347 /* An include psymtab has exactly one dependency: the psymtab that
6348 includes it. */
6349 gdb_assert (this->number_of_dependencies == 1);
6350 return this->dependencies[0];
6351 }
6352 };
6353
6354 /* Allocate a new partial symtab for file named NAME and mark this new
6355 partial symtab as being an include of PST. */
6356
6357 static void
6358 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
6359 const char *name,
6360 dwarf2_psymtab *pst,
6361 psymtab_storage *partial_symtabs,
6362 objfile_per_bfd_storage *objfile_per_bfd)
6363 {
6364 dwarf2_include_psymtab *subpst
6365 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
6366
6367 if (!IS_ABSOLUTE_PATH (subpst->filename))
6368 subpst->dirname = pst->dirname;
6369
6370 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
6371 subpst->dependencies[0] = pst;
6372 subpst->number_of_dependencies = 1;
6373 }
6374
6375 /* Read the Line Number Program data and extract the list of files
6376 included by the source file represented by PST. Build an include
6377 partial symtab for each of these included files. */
6378
6379 static void
6380 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6381 struct die_info *die,
6382 dwarf2_psymtab *pst)
6383 {
6384 line_header_up lh;
6385 struct attribute *attr;
6386
6387 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6388 if (attr != nullptr && attr->form_is_unsigned ())
6389 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6390 if (lh == NULL)
6391 return; /* No linetable, so no includes. */
6392
6393 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6394 that we pass in the raw text_low here; that is ok because we're
6395 only decoding the line table to make include partial symtabs, and
6396 so the addresses aren't really used. */
6397 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6398 pst->raw_text_low (), 1);
6399 }
6400
6401 static hashval_t
6402 hash_signatured_type (const void *item)
6403 {
6404 const struct signatured_type *sig_type
6405 = (const struct signatured_type *) item;
6406
6407 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6408 return sig_type->signature;
6409 }
6410
6411 static int
6412 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6413 {
6414 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6415 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6416
6417 return lhs->signature == rhs->signature;
6418 }
6419
6420 /* Allocate a hash table for signatured types. */
6421
6422 static htab_up
6423 allocate_signatured_type_table ()
6424 {
6425 return htab_up (htab_create_alloc (41,
6426 hash_signatured_type,
6427 eq_signatured_type,
6428 NULL, xcalloc, xfree));
6429 }
6430
6431 /* A helper function to add a signatured type CU to a table. */
6432
6433 static int
6434 add_signatured_type_cu_to_table (void **slot, void *datum)
6435 {
6436 struct signatured_type *sigt = (struct signatured_type *) *slot;
6437 std::vector<signatured_type *> *all_type_units
6438 = (std::vector<signatured_type *> *) datum;
6439
6440 all_type_units->push_back (sigt);
6441
6442 return 1;
6443 }
6444
6445 /* A helper for create_debug_types_hash_table. Read types from SECTION
6446 and fill them into TYPES_HTAB. It will process only type units,
6447 therefore DW_UT_type. */
6448
6449 static void
6450 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6451 struct dwo_file *dwo_file,
6452 dwarf2_section_info *section, htab_up &types_htab,
6453 rcuh_kind section_kind)
6454 {
6455 struct objfile *objfile = per_objfile->objfile;
6456 struct dwarf2_section_info *abbrev_section;
6457 bfd *abfd;
6458 const gdb_byte *info_ptr, *end_ptr;
6459
6460 abbrev_section = (dwo_file != NULL
6461 ? &dwo_file->sections.abbrev
6462 : &per_objfile->per_bfd->abbrev);
6463
6464 dwarf_read_debug_printf ("Reading %s for %s",
6465 section->get_name (),
6466 abbrev_section->get_file_name ());
6467
6468 section->read (objfile);
6469 info_ptr = section->buffer;
6470
6471 if (info_ptr == NULL)
6472 return;
6473
6474 /* We can't set abfd until now because the section may be empty or
6475 not present, in which case the bfd is unknown. */
6476 abfd = section->get_bfd_owner ();
6477
6478 /* We don't use cutu_reader here because we don't need to read
6479 any dies: the signature is in the header. */
6480
6481 end_ptr = info_ptr + section->size;
6482 while (info_ptr < end_ptr)
6483 {
6484 struct signatured_type *sig_type;
6485 struct dwo_unit *dwo_tu;
6486 void **slot;
6487 const gdb_byte *ptr = info_ptr;
6488 struct comp_unit_head header;
6489 unsigned int length;
6490
6491 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6492
6493 /* Initialize it due to a false compiler warning. */
6494 header.signature = -1;
6495 header.type_cu_offset_in_tu = (cu_offset) -1;
6496
6497 /* We need to read the type's signature in order to build the hash
6498 table, but we don't need anything else just yet. */
6499
6500 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6501 abbrev_section, ptr, section_kind);
6502
6503 length = header.get_length ();
6504
6505 /* Skip dummy type units. */
6506 if (ptr >= info_ptr + length
6507 || peek_abbrev_code (abfd, ptr) == 0
6508 || (header.unit_type != DW_UT_type
6509 && header.unit_type != DW_UT_split_type))
6510 {
6511 info_ptr += length;
6512 continue;
6513 }
6514
6515 if (types_htab == NULL)
6516 {
6517 if (dwo_file)
6518 types_htab = allocate_dwo_unit_table ();
6519 else
6520 types_htab = allocate_signatured_type_table ();
6521 }
6522
6523 if (dwo_file)
6524 {
6525 sig_type = NULL;
6526 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6527 dwo_tu->dwo_file = dwo_file;
6528 dwo_tu->signature = header.signature;
6529 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6530 dwo_tu->section = section;
6531 dwo_tu->sect_off = sect_off;
6532 dwo_tu->length = length;
6533 }
6534 else
6535 {
6536 /* N.B.: type_offset is not usable if this type uses a DWO file.
6537 The real type_offset is in the DWO file. */
6538 dwo_tu = NULL;
6539 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6540 sig_type->signature = header.signature;
6541 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6542 sig_type->per_cu.is_debug_types = 1;
6543 sig_type->per_cu.section = section;
6544 sig_type->per_cu.sect_off = sect_off;
6545 sig_type->per_cu.length = length;
6546 }
6547
6548 slot = htab_find_slot (types_htab.get (),
6549 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6550 INSERT);
6551 gdb_assert (slot != NULL);
6552 if (*slot != NULL)
6553 {
6554 sect_offset dup_sect_off;
6555
6556 if (dwo_file)
6557 {
6558 const struct dwo_unit *dup_tu
6559 = (const struct dwo_unit *) *slot;
6560
6561 dup_sect_off = dup_tu->sect_off;
6562 }
6563 else
6564 {
6565 const struct signatured_type *dup_tu
6566 = (const struct signatured_type *) *slot;
6567
6568 dup_sect_off = dup_tu->per_cu.sect_off;
6569 }
6570
6571 complaint (_("debug type entry at offset %s is duplicate to"
6572 " the entry at offset %s, signature %s"),
6573 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6574 hex_string (header.signature));
6575 }
6576 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6577
6578 dwarf_read_debug_printf_v (" offset %s, signature %s",
6579 sect_offset_str (sect_off),
6580 hex_string (header.signature));
6581
6582 info_ptr += length;
6583 }
6584 }
6585
6586 /* Create the hash table of all entries in the .debug_types
6587 (or .debug_types.dwo) section(s).
6588 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6589 otherwise it is NULL.
6590
6591 The result is a pointer to the hash table or NULL if there are no types.
6592
6593 Note: This function processes DWO files only, not DWP files. */
6594
6595 static void
6596 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6597 struct dwo_file *dwo_file,
6598 gdb::array_view<dwarf2_section_info> type_sections,
6599 htab_up &types_htab)
6600 {
6601 for (dwarf2_section_info &section : type_sections)
6602 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6603 rcuh_kind::TYPE);
6604 }
6605
6606 /* Create the hash table of all entries in the .debug_types section,
6607 and initialize all_type_units.
6608 The result is zero if there is an error (e.g. missing .debug_types section),
6609 otherwise non-zero. */
6610
6611 static int
6612 create_all_type_units (dwarf2_per_objfile *per_objfile)
6613 {
6614 htab_up types_htab;
6615
6616 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6617 types_htab, rcuh_kind::COMPILE);
6618 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6619 types_htab);
6620 if (types_htab == NULL)
6621 {
6622 per_objfile->per_bfd->signatured_types = NULL;
6623 return 0;
6624 }
6625
6626 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6627
6628 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6629 per_objfile->per_bfd->all_type_units.reserve
6630 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6631
6632 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6633 add_signatured_type_cu_to_table,
6634 &per_objfile->per_bfd->all_type_units);
6635
6636 return 1;
6637 }
6638
6639 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6640 If SLOT is non-NULL, it is the entry to use in the hash table.
6641 Otherwise we find one. */
6642
6643 static struct signatured_type *
6644 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6645 {
6646 if (per_objfile->per_bfd->all_type_units.size ()
6647 == per_objfile->per_bfd->all_type_units.capacity ())
6648 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6649
6650 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6651
6652 per_objfile->resize_symtabs ();
6653
6654 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6655 sig_type->signature = sig;
6656 sig_type->per_cu.is_debug_types = 1;
6657 if (per_objfile->per_bfd->using_index)
6658 {
6659 sig_type->per_cu.v.quick =
6660 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6661 struct dwarf2_per_cu_quick_data);
6662 }
6663
6664 if (slot == NULL)
6665 {
6666 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6667 sig_type, INSERT);
6668 }
6669 gdb_assert (*slot == NULL);
6670 *slot = sig_type;
6671 /* The rest of sig_type must be filled in by the caller. */
6672 return sig_type;
6673 }
6674
6675 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6676 Fill in SIG_ENTRY with DWO_ENTRY. */
6677
6678 static void
6679 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6680 struct signatured_type *sig_entry,
6681 struct dwo_unit *dwo_entry)
6682 {
6683 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6684
6685 /* Make sure we're not clobbering something we don't expect to. */
6686 gdb_assert (! sig_entry->per_cu.queued);
6687 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6688 if (per_bfd->using_index)
6689 {
6690 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6691 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6692 }
6693 else
6694 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6695 gdb_assert (sig_entry->signature == dwo_entry->signature);
6696 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6697 gdb_assert (sig_entry->type_unit_group == NULL);
6698 gdb_assert (sig_entry->dwo_unit == NULL);
6699
6700 sig_entry->per_cu.section = dwo_entry->section;
6701 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6702 sig_entry->per_cu.length = dwo_entry->length;
6703 sig_entry->per_cu.reading_dwo_directly = 1;
6704 sig_entry->per_cu.per_bfd = per_bfd;
6705 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6706 sig_entry->dwo_unit = dwo_entry;
6707 }
6708
6709 /* Subroutine of lookup_signatured_type.
6710 If we haven't read the TU yet, create the signatured_type data structure
6711 for a TU to be read in directly from a DWO file, bypassing the stub.
6712 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6713 using .gdb_index, then when reading a CU we want to stay in the DWO file
6714 containing that CU. Otherwise we could end up reading several other DWO
6715 files (due to comdat folding) to process the transitive closure of all the
6716 mentioned TUs, and that can be slow. The current DWO file will have every
6717 type signature that it needs.
6718 We only do this for .gdb_index because in the psymtab case we already have
6719 to read all the DWOs to build the type unit groups. */
6720
6721 static struct signatured_type *
6722 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6723 {
6724 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6725 struct dwo_file *dwo_file;
6726 struct dwo_unit find_dwo_entry, *dwo_entry;
6727 struct signatured_type find_sig_entry, *sig_entry;
6728 void **slot;
6729
6730 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6731
6732 /* If TU skeletons have been removed then we may not have read in any
6733 TUs yet. */
6734 if (per_objfile->per_bfd->signatured_types == NULL)
6735 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6736
6737 /* We only ever need to read in one copy of a signatured type.
6738 Use the global signatured_types array to do our own comdat-folding
6739 of types. If this is the first time we're reading this TU, and
6740 the TU has an entry in .gdb_index, replace the recorded data from
6741 .gdb_index with this TU. */
6742
6743 find_sig_entry.signature = sig;
6744 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6745 &find_sig_entry, INSERT);
6746 sig_entry = (struct signatured_type *) *slot;
6747
6748 /* We can get here with the TU already read, *or* in the process of being
6749 read. Don't reassign the global entry to point to this DWO if that's
6750 the case. Also note that if the TU is already being read, it may not
6751 have come from a DWO, the program may be a mix of Fission-compiled
6752 code and non-Fission-compiled code. */
6753
6754 /* Have we already tried to read this TU?
6755 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6756 needn't exist in the global table yet). */
6757 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6758 return sig_entry;
6759
6760 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6761 dwo_unit of the TU itself. */
6762 dwo_file = cu->dwo_unit->dwo_file;
6763
6764 /* Ok, this is the first time we're reading this TU. */
6765 if (dwo_file->tus == NULL)
6766 return NULL;
6767 find_dwo_entry.signature = sig;
6768 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6769 &find_dwo_entry);
6770 if (dwo_entry == NULL)
6771 return NULL;
6772
6773 /* If the global table doesn't have an entry for this TU, add one. */
6774 if (sig_entry == NULL)
6775 sig_entry = add_type_unit (per_objfile, sig, slot);
6776
6777 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6778 sig_entry->per_cu.tu_read = 1;
6779 return sig_entry;
6780 }
6781
6782 /* Subroutine of lookup_signatured_type.
6783 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6784 then try the DWP file. If the TU stub (skeleton) has been removed then
6785 it won't be in .gdb_index. */
6786
6787 static struct signatured_type *
6788 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6789 {
6790 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6791 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6792 struct dwo_unit *dwo_entry;
6793 struct signatured_type find_sig_entry, *sig_entry;
6794 void **slot;
6795
6796 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6797 gdb_assert (dwp_file != NULL);
6798
6799 /* If TU skeletons have been removed then we may not have read in any
6800 TUs yet. */
6801 if (per_objfile->per_bfd->signatured_types == NULL)
6802 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6803
6804 find_sig_entry.signature = sig;
6805 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6806 &find_sig_entry, INSERT);
6807 sig_entry = (struct signatured_type *) *slot;
6808
6809 /* Have we already tried to read this TU?
6810 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6811 needn't exist in the global table yet). */
6812 if (sig_entry != NULL)
6813 return sig_entry;
6814
6815 if (dwp_file->tus == NULL)
6816 return NULL;
6817 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6818 1 /* is_debug_types */);
6819 if (dwo_entry == NULL)
6820 return NULL;
6821
6822 sig_entry = add_type_unit (per_objfile, sig, slot);
6823 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6824
6825 return sig_entry;
6826 }
6827
6828 /* Lookup a signature based type for DW_FORM_ref_sig8.
6829 Returns NULL if signature SIG is not present in the table.
6830 It is up to the caller to complain about this. */
6831
6832 static struct signatured_type *
6833 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6834 {
6835 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6836
6837 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6838 {
6839 /* We're in a DWO/DWP file, and we're using .gdb_index.
6840 These cases require special processing. */
6841 if (get_dwp_file (per_objfile) == NULL)
6842 return lookup_dwo_signatured_type (cu, sig);
6843 else
6844 return lookup_dwp_signatured_type (cu, sig);
6845 }
6846 else
6847 {
6848 struct signatured_type find_entry, *entry;
6849
6850 if (per_objfile->per_bfd->signatured_types == NULL)
6851 return NULL;
6852 find_entry.signature = sig;
6853 entry = ((struct signatured_type *)
6854 htab_find (per_objfile->per_bfd->signatured_types.get (),
6855 &find_entry));
6856 return entry;
6857 }
6858 }
6859
6860 /* Low level DIE reading support. */
6861
6862 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6863
6864 static void
6865 init_cu_die_reader (struct die_reader_specs *reader,
6866 struct dwarf2_cu *cu,
6867 struct dwarf2_section_info *section,
6868 struct dwo_file *dwo_file,
6869 struct abbrev_table *abbrev_table)
6870 {
6871 gdb_assert (section->readin && section->buffer != NULL);
6872 reader->abfd = section->get_bfd_owner ();
6873 reader->cu = cu;
6874 reader->dwo_file = dwo_file;
6875 reader->die_section = section;
6876 reader->buffer = section->buffer;
6877 reader->buffer_end = section->buffer + section->size;
6878 reader->abbrev_table = abbrev_table;
6879 }
6880
6881 /* Subroutine of cutu_reader to simplify it.
6882 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6883 There's just a lot of work to do, and cutu_reader is big enough
6884 already.
6885
6886 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6887 from it to the DIE in the DWO. If NULL we are skipping the stub.
6888 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6889 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6890 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6891 STUB_COMP_DIR may be non-NULL.
6892 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6893 are filled in with the info of the DIE from the DWO file.
6894 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6895 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6896 kept around for at least as long as *RESULT_READER.
6897
6898 The result is non-zero if a valid (non-dummy) DIE was found. */
6899
6900 static int
6901 read_cutu_die_from_dwo (dwarf2_cu *cu,
6902 struct dwo_unit *dwo_unit,
6903 struct die_info *stub_comp_unit_die,
6904 const char *stub_comp_dir,
6905 struct die_reader_specs *result_reader,
6906 const gdb_byte **result_info_ptr,
6907 struct die_info **result_comp_unit_die,
6908 abbrev_table_up *result_dwo_abbrev_table)
6909 {
6910 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6911 dwarf2_per_cu_data *per_cu = cu->per_cu;
6912 struct objfile *objfile = per_objfile->objfile;
6913 bfd *abfd;
6914 const gdb_byte *begin_info_ptr, *info_ptr;
6915 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6916 int i,num_extra_attrs;
6917 struct dwarf2_section_info *dwo_abbrev_section;
6918 struct die_info *comp_unit_die;
6919
6920 /* At most one of these may be provided. */
6921 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6922
6923 /* These attributes aren't processed until later:
6924 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6925 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6926 referenced later. However, these attributes are found in the stub
6927 which we won't have later. In order to not impose this complication
6928 on the rest of the code, we read them here and copy them to the
6929 DWO CU/TU die. */
6930
6931 stmt_list = NULL;
6932 low_pc = NULL;
6933 high_pc = NULL;
6934 ranges = NULL;
6935 comp_dir = NULL;
6936
6937 if (stub_comp_unit_die != NULL)
6938 {
6939 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6940 DWO file. */
6941 if (!per_cu->is_debug_types)
6942 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6943 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6944 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6945 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6946 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6947
6948 cu->addr_base = stub_comp_unit_die->addr_base ();
6949
6950 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6951 We need the value before we can process DW_AT_ranges values from the
6952 DWO. */
6953 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6954
6955 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6956 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6957 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6958 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6959 section. */
6960 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6961 }
6962 else if (stub_comp_dir != NULL)
6963 {
6964 /* Reconstruct the comp_dir attribute to simplify the code below. */
6965 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6966 comp_dir->name = DW_AT_comp_dir;
6967 comp_dir->form = DW_FORM_string;
6968 comp_dir->set_string_noncanonical (stub_comp_dir);
6969 }
6970
6971 /* Set up for reading the DWO CU/TU. */
6972 cu->dwo_unit = dwo_unit;
6973 dwarf2_section_info *section = dwo_unit->section;
6974 section->read (objfile);
6975 abfd = section->get_bfd_owner ();
6976 begin_info_ptr = info_ptr = (section->buffer
6977 + to_underlying (dwo_unit->sect_off));
6978 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6979
6980 if (per_cu->is_debug_types)
6981 {
6982 signatured_type *sig_type = (struct signatured_type *) per_cu;
6983
6984 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6985 section, dwo_abbrev_section,
6986 info_ptr, rcuh_kind::TYPE);
6987 /* This is not an assert because it can be caused by bad debug info. */
6988 if (sig_type->signature != cu->header.signature)
6989 {
6990 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6991 " TU at offset %s [in module %s]"),
6992 hex_string (sig_type->signature),
6993 hex_string (cu->header.signature),
6994 sect_offset_str (dwo_unit->sect_off),
6995 bfd_get_filename (abfd));
6996 }
6997 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6998 /* For DWOs coming from DWP files, we don't know the CU length
6999 nor the type's offset in the TU until now. */
7000 dwo_unit->length = cu->header.get_length ();
7001 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7002
7003 /* Establish the type offset that can be used to lookup the type.
7004 For DWO files, we don't know it until now. */
7005 sig_type->type_offset_in_section
7006 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7007 }
7008 else
7009 {
7010 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7011 section, dwo_abbrev_section,
7012 info_ptr, rcuh_kind::COMPILE);
7013 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7014 /* For DWOs coming from DWP files, we don't know the CU length
7015 until now. */
7016 dwo_unit->length = cu->header.get_length ();
7017 }
7018
7019 dwo_abbrev_section->read (objfile);
7020 *result_dwo_abbrev_table
7021 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
7022 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7023 result_dwo_abbrev_table->get ());
7024
7025 /* Read in the die, but leave space to copy over the attributes
7026 from the stub. This has the benefit of simplifying the rest of
7027 the code - all the work to maintain the illusion of a single
7028 DW_TAG_{compile,type}_unit DIE is done here. */
7029 num_extra_attrs = ((stmt_list != NULL)
7030 + (low_pc != NULL)
7031 + (high_pc != NULL)
7032 + (ranges != NULL)
7033 + (comp_dir != NULL));
7034 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7035 num_extra_attrs);
7036
7037 /* Copy over the attributes from the stub to the DIE we just read in. */
7038 comp_unit_die = *result_comp_unit_die;
7039 i = comp_unit_die->num_attrs;
7040 if (stmt_list != NULL)
7041 comp_unit_die->attrs[i++] = *stmt_list;
7042 if (low_pc != NULL)
7043 comp_unit_die->attrs[i++] = *low_pc;
7044 if (high_pc != NULL)
7045 comp_unit_die->attrs[i++] = *high_pc;
7046 if (ranges != NULL)
7047 comp_unit_die->attrs[i++] = *ranges;
7048 if (comp_dir != NULL)
7049 comp_unit_die->attrs[i++] = *comp_dir;
7050 comp_unit_die->num_attrs += num_extra_attrs;
7051
7052 if (dwarf_die_debug)
7053 {
7054 fprintf_unfiltered (gdb_stdlog,
7055 "Read die from %s@0x%x of %s:\n",
7056 section->get_name (),
7057 (unsigned) (begin_info_ptr - section->buffer),
7058 bfd_get_filename (abfd));
7059 dump_die (comp_unit_die, dwarf_die_debug);
7060 }
7061
7062 /* Skip dummy compilation units. */
7063 if (info_ptr >= begin_info_ptr + dwo_unit->length
7064 || peek_abbrev_code (abfd, info_ptr) == 0)
7065 return 0;
7066
7067 *result_info_ptr = info_ptr;
7068 return 1;
7069 }
7070
7071 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7072 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7073 signature is part of the header. */
7074 static gdb::optional<ULONGEST>
7075 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7076 {
7077 if (cu->header.version >= 5)
7078 return cu->header.signature;
7079 struct attribute *attr;
7080 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7081 if (attr == nullptr || !attr->form_is_unsigned ())
7082 return gdb::optional<ULONGEST> ();
7083 return attr->as_unsigned ();
7084 }
7085
7086 /* Subroutine of cutu_reader to simplify it.
7087 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7088 Returns NULL if the specified DWO unit cannot be found. */
7089
7090 static struct dwo_unit *
7091 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7092 {
7093 dwarf2_per_cu_data *per_cu = cu->per_cu;
7094 struct dwo_unit *dwo_unit;
7095 const char *comp_dir;
7096
7097 gdb_assert (cu != NULL);
7098
7099 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7100 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7101 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7102
7103 if (per_cu->is_debug_types)
7104 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7105 else
7106 {
7107 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7108
7109 if (!signature.has_value ())
7110 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7111 " [in module %s]"),
7112 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7113
7114 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7115 }
7116
7117 return dwo_unit;
7118 }
7119
7120 /* Subroutine of cutu_reader to simplify it.
7121 See it for a description of the parameters.
7122 Read a TU directly from a DWO file, bypassing the stub. */
7123
7124 void
7125 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7126 dwarf2_per_objfile *per_objfile,
7127 dwarf2_cu *existing_cu)
7128 {
7129 struct signatured_type *sig_type;
7130
7131 /* Verify we can do the following downcast, and that we have the
7132 data we need. */
7133 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7134 sig_type = (struct signatured_type *) this_cu;
7135 gdb_assert (sig_type->dwo_unit != NULL);
7136
7137 dwarf2_cu *cu;
7138
7139 if (existing_cu != nullptr)
7140 {
7141 cu = existing_cu;
7142 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7143 /* There's no need to do the rereading_dwo_cu handling that
7144 cutu_reader does since we don't read the stub. */
7145 }
7146 else
7147 {
7148 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7149 in per_objfile yet. */
7150 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7151 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7152 cu = m_new_cu.get ();
7153 }
7154
7155 /* A future optimization, if needed, would be to use an existing
7156 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7157 could share abbrev tables. */
7158
7159 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7160 NULL /* stub_comp_unit_die */,
7161 sig_type->dwo_unit->dwo_file->comp_dir,
7162 this, &info_ptr,
7163 &comp_unit_die,
7164 &m_dwo_abbrev_table) == 0)
7165 {
7166 /* Dummy die. */
7167 dummy_p = true;
7168 }
7169 }
7170
7171 /* Initialize a CU (or TU) and read its DIEs.
7172 If the CU defers to a DWO file, read the DWO file as well.
7173
7174 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7175 Otherwise the table specified in the comp unit header is read in and used.
7176 This is an optimization for when we already have the abbrev table.
7177
7178 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7179 allocated. */
7180
7181 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7182 dwarf2_per_objfile *per_objfile,
7183 struct abbrev_table *abbrev_table,
7184 dwarf2_cu *existing_cu,
7185 bool skip_partial)
7186 : die_reader_specs {},
7187 m_this_cu (this_cu)
7188 {
7189 struct objfile *objfile = per_objfile->objfile;
7190 struct dwarf2_section_info *section = this_cu->section;
7191 bfd *abfd = section->get_bfd_owner ();
7192 const gdb_byte *begin_info_ptr;
7193 struct signatured_type *sig_type = NULL;
7194 struct dwarf2_section_info *abbrev_section;
7195 /* Non-zero if CU currently points to a DWO file and we need to
7196 reread it. When this happens we need to reread the skeleton die
7197 before we can reread the DWO file (this only applies to CUs, not TUs). */
7198 int rereading_dwo_cu = 0;
7199
7200 if (dwarf_die_debug)
7201 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7202 this_cu->is_debug_types ? "type" : "comp",
7203 sect_offset_str (this_cu->sect_off));
7204
7205 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7206 file (instead of going through the stub), short-circuit all of this. */
7207 if (this_cu->reading_dwo_directly)
7208 {
7209 /* Narrow down the scope of possibilities to have to understand. */
7210 gdb_assert (this_cu->is_debug_types);
7211 gdb_assert (abbrev_table == NULL);
7212 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7213 return;
7214 }
7215
7216 /* This is cheap if the section is already read in. */
7217 section->read (objfile);
7218
7219 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7220
7221 abbrev_section = get_abbrev_section_for_cu (this_cu);
7222
7223 dwarf2_cu *cu;
7224
7225 if (existing_cu != nullptr)
7226 {
7227 cu = existing_cu;
7228 /* If this CU is from a DWO file we need to start over, we need to
7229 refetch the attributes from the skeleton CU.
7230 This could be optimized by retrieving those attributes from when we
7231 were here the first time: the previous comp_unit_die was stored in
7232 comp_unit_obstack. But there's no data yet that we need this
7233 optimization. */
7234 if (cu->dwo_unit != NULL)
7235 rereading_dwo_cu = 1;
7236 }
7237 else
7238 {
7239 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7240 in per_objfile yet. */
7241 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7242 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7243 cu = m_new_cu.get ();
7244 }
7245
7246 /* Get the header. */
7247 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7248 {
7249 /* We already have the header, there's no need to read it in again. */
7250 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7251 }
7252 else
7253 {
7254 if (this_cu->is_debug_types)
7255 {
7256 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7257 section, abbrev_section,
7258 info_ptr, rcuh_kind::TYPE);
7259
7260 /* Since per_cu is the first member of struct signatured_type,
7261 we can go from a pointer to one to a pointer to the other. */
7262 sig_type = (struct signatured_type *) this_cu;
7263 gdb_assert (sig_type->signature == cu->header.signature);
7264 gdb_assert (sig_type->type_offset_in_tu
7265 == cu->header.type_cu_offset_in_tu);
7266 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7267
7268 /* LENGTH has not been set yet for type units if we're
7269 using .gdb_index. */
7270 this_cu->length = cu->header.get_length ();
7271
7272 /* Establish the type offset that can be used to lookup the type. */
7273 sig_type->type_offset_in_section =
7274 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7275
7276 this_cu->dwarf_version = cu->header.version;
7277 }
7278 else
7279 {
7280 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7281 section, abbrev_section,
7282 info_ptr,
7283 rcuh_kind::COMPILE);
7284
7285 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7286 if (this_cu->length == 0)
7287 this_cu->length = cu->header.get_length ();
7288 else
7289 gdb_assert (this_cu->length == cu->header.get_length ());
7290 this_cu->dwarf_version = cu->header.version;
7291 }
7292 }
7293
7294 /* Skip dummy compilation units. */
7295 if (info_ptr >= begin_info_ptr + this_cu->length
7296 || peek_abbrev_code (abfd, info_ptr) == 0)
7297 {
7298 dummy_p = true;
7299 return;
7300 }
7301
7302 /* If we don't have them yet, read the abbrevs for this compilation unit.
7303 And if we need to read them now, make sure they're freed when we're
7304 done. */
7305 if (abbrev_table != NULL)
7306 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7307 else
7308 {
7309 abbrev_section->read (objfile);
7310 m_abbrev_table_holder
7311 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7312 abbrev_table = m_abbrev_table_holder.get ();
7313 }
7314
7315 /* Read the top level CU/TU die. */
7316 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7317 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7318
7319 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7320 {
7321 dummy_p = true;
7322 return;
7323 }
7324
7325 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7326 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7327 table from the DWO file and pass the ownership over to us. It will be
7328 referenced from READER, so we must make sure to free it after we're done
7329 with READER.
7330
7331 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7332 DWO CU, that this test will fail (the attribute will not be present). */
7333 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7334 if (dwo_name != nullptr)
7335 {
7336 struct dwo_unit *dwo_unit;
7337 struct die_info *dwo_comp_unit_die;
7338
7339 if (comp_unit_die->has_children)
7340 {
7341 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7342 " has children (offset %s) [in module %s]"),
7343 sect_offset_str (this_cu->sect_off),
7344 bfd_get_filename (abfd));
7345 }
7346 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7347 if (dwo_unit != NULL)
7348 {
7349 if (read_cutu_die_from_dwo (cu, dwo_unit,
7350 comp_unit_die, NULL,
7351 this, &info_ptr,
7352 &dwo_comp_unit_die,
7353 &m_dwo_abbrev_table) == 0)
7354 {
7355 /* Dummy die. */
7356 dummy_p = true;
7357 return;
7358 }
7359 comp_unit_die = dwo_comp_unit_die;
7360 }
7361 else
7362 {
7363 /* Yikes, we couldn't find the rest of the DIE, we only have
7364 the stub. A complaint has already been logged. There's
7365 not much more we can do except pass on the stub DIE to
7366 die_reader_func. We don't want to throw an error on bad
7367 debug info. */
7368 }
7369 }
7370 }
7371
7372 void
7373 cutu_reader::keep ()
7374 {
7375 /* Done, clean up. */
7376 gdb_assert (!dummy_p);
7377 if (m_new_cu != NULL)
7378 {
7379 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7380 now. */
7381 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7382 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7383 }
7384 }
7385
7386 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7387 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7388 assumed to have already done the lookup to find the DWO file).
7389
7390 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7391 THIS_CU->is_debug_types, but nothing else.
7392
7393 We fill in THIS_CU->length.
7394
7395 THIS_CU->cu is always freed when done.
7396 This is done in order to not leave THIS_CU->cu in a state where we have
7397 to care whether it refers to the "main" CU or the DWO CU.
7398
7399 When parent_cu is passed, it is used to provide a default value for
7400 str_offsets_base and addr_base from the parent. */
7401
7402 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7403 dwarf2_per_objfile *per_objfile,
7404 struct dwarf2_cu *parent_cu,
7405 struct dwo_file *dwo_file)
7406 : die_reader_specs {},
7407 m_this_cu (this_cu)
7408 {
7409 struct objfile *objfile = per_objfile->objfile;
7410 struct dwarf2_section_info *section = this_cu->section;
7411 bfd *abfd = section->get_bfd_owner ();
7412 struct dwarf2_section_info *abbrev_section;
7413 const gdb_byte *begin_info_ptr, *info_ptr;
7414
7415 if (dwarf_die_debug)
7416 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7417 this_cu->is_debug_types ? "type" : "comp",
7418 sect_offset_str (this_cu->sect_off));
7419
7420 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7421
7422 abbrev_section = (dwo_file != NULL
7423 ? &dwo_file->sections.abbrev
7424 : get_abbrev_section_for_cu (this_cu));
7425
7426 /* This is cheap if the section is already read in. */
7427 section->read (objfile);
7428
7429 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7430
7431 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7432 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7433 section, abbrev_section, info_ptr,
7434 (this_cu->is_debug_types
7435 ? rcuh_kind::TYPE
7436 : rcuh_kind::COMPILE));
7437
7438 if (parent_cu != nullptr)
7439 {
7440 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7441 m_new_cu->addr_base = parent_cu->addr_base;
7442 }
7443 this_cu->length = m_new_cu->header.get_length ();
7444
7445 /* Skip dummy compilation units. */
7446 if (info_ptr >= begin_info_ptr + this_cu->length
7447 || peek_abbrev_code (abfd, info_ptr) == 0)
7448 {
7449 dummy_p = true;
7450 return;
7451 }
7452
7453 abbrev_section->read (objfile);
7454 m_abbrev_table_holder
7455 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7456
7457 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7458 m_abbrev_table_holder.get ());
7459 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7460 }
7461
7462 \f
7463 /* Type Unit Groups.
7464
7465 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7466 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7467 so that all types coming from the same compilation (.o file) are grouped
7468 together. A future step could be to put the types in the same symtab as
7469 the CU the types ultimately came from. */
7470
7471 static hashval_t
7472 hash_type_unit_group (const void *item)
7473 {
7474 const struct type_unit_group *tu_group
7475 = (const struct type_unit_group *) item;
7476
7477 return hash_stmt_list_entry (&tu_group->hash);
7478 }
7479
7480 static int
7481 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7482 {
7483 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7484 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7485
7486 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7487 }
7488
7489 /* Allocate a hash table for type unit groups. */
7490
7491 static htab_up
7492 allocate_type_unit_groups_table ()
7493 {
7494 return htab_up (htab_create_alloc (3,
7495 hash_type_unit_group,
7496 eq_type_unit_group,
7497 NULL, xcalloc, xfree));
7498 }
7499
7500 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7501 partial symtabs. We combine several TUs per psymtab to not let the size
7502 of any one psymtab grow too big. */
7503 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7504 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7505
7506 /* Helper routine for get_type_unit_group.
7507 Create the type_unit_group object used to hold one or more TUs. */
7508
7509 static struct type_unit_group *
7510 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7511 {
7512 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7513 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7514 struct dwarf2_per_cu_data *per_cu;
7515 struct type_unit_group *tu_group;
7516
7517 tu_group = OBSTACK_ZALLOC (&per_bfd->obstack, type_unit_group);
7518 per_cu = &tu_group->per_cu;
7519 per_cu->per_bfd = per_bfd;
7520
7521 if (per_bfd->using_index)
7522 {
7523 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7524 struct dwarf2_per_cu_quick_data);
7525 }
7526 else
7527 {
7528 unsigned int line_offset = to_underlying (line_offset_struct);
7529 dwarf2_psymtab *pst;
7530 std::string name;
7531
7532 /* Give the symtab a useful name for debug purposes. */
7533 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7534 name = string_printf ("<type_units_%d>",
7535 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7536 else
7537 name = string_printf ("<type_units_at_0x%x>", line_offset);
7538
7539 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7540 pst->anonymous = true;
7541 }
7542
7543 tu_group->hash.dwo_unit = cu->dwo_unit;
7544 tu_group->hash.line_sect_off = line_offset_struct;
7545
7546 return tu_group;
7547 }
7548
7549 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7550 STMT_LIST is a DW_AT_stmt_list attribute. */
7551
7552 static struct type_unit_group *
7553 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7554 {
7555 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7556 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7557 struct type_unit_group *tu_group;
7558 void **slot;
7559 unsigned int line_offset;
7560 struct type_unit_group type_unit_group_for_lookup;
7561
7562 if (per_objfile->per_bfd->type_unit_groups == NULL)
7563 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7564
7565 /* Do we need to create a new group, or can we use an existing one? */
7566
7567 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7568 {
7569 line_offset = stmt_list->as_unsigned ();
7570 ++tu_stats->nr_symtab_sharers;
7571 }
7572 else
7573 {
7574 /* Ugh, no stmt_list. Rare, but we have to handle it.
7575 We can do various things here like create one group per TU or
7576 spread them over multiple groups to split up the expansion work.
7577 To avoid worst case scenarios (too many groups or too large groups)
7578 we, umm, group them in bunches. */
7579 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7580 | (tu_stats->nr_stmt_less_type_units
7581 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7582 ++tu_stats->nr_stmt_less_type_units;
7583 }
7584
7585 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7586 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7587 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7588 &type_unit_group_for_lookup, INSERT);
7589 if (*slot != NULL)
7590 {
7591 tu_group = (struct type_unit_group *) *slot;
7592 gdb_assert (tu_group != NULL);
7593 }
7594 else
7595 {
7596 sect_offset line_offset_struct = (sect_offset) line_offset;
7597 tu_group = create_type_unit_group (cu, line_offset_struct);
7598 *slot = tu_group;
7599 ++tu_stats->nr_symtabs;
7600 }
7601
7602 return tu_group;
7603 }
7604 \f
7605 /* Partial symbol tables. */
7606
7607 /* Create a psymtab named NAME and assign it to PER_CU.
7608
7609 The caller must fill in the following details:
7610 dirname, textlow, texthigh. */
7611
7612 static dwarf2_psymtab *
7613 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7614 dwarf2_per_objfile *per_objfile,
7615 const char *name)
7616 {
7617 dwarf2_psymtab *pst
7618 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7619 per_objfile->objfile->per_bfd, per_cu);
7620
7621 pst->psymtabs_addrmap_supported = true;
7622
7623 /* This is the glue that links PST into GDB's symbol API. */
7624 per_cu->v.psymtab = pst;
7625
7626 return pst;
7627 }
7628
7629 /* DIE reader function for process_psymtab_comp_unit. */
7630
7631 static void
7632 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7633 const gdb_byte *info_ptr,
7634 struct die_info *comp_unit_die,
7635 enum language pretend_language)
7636 {
7637 struct dwarf2_cu *cu = reader->cu;
7638 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7639 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7640 struct objfile *objfile = per_objfile->objfile;
7641 struct gdbarch *gdbarch = objfile->arch ();
7642 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7643 CORE_ADDR baseaddr;
7644 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7645 dwarf2_psymtab *pst;
7646 enum pc_bounds_kind cu_bounds_kind;
7647 const char *filename;
7648
7649 gdb_assert (! per_cu->is_debug_types);
7650
7651 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7652
7653 /* Allocate a new partial symbol table structure. */
7654 gdb::unique_xmalloc_ptr<char> debug_filename;
7655 static const char artificial[] = "<artificial>";
7656 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7657 if (filename == NULL)
7658 filename = "";
7659 else if (strcmp (filename, artificial) == 0)
7660 {
7661 debug_filename.reset (concat (artificial, "@",
7662 sect_offset_str (per_cu->sect_off),
7663 (char *) NULL));
7664 filename = debug_filename.get ();
7665 }
7666
7667 pst = create_partial_symtab (per_cu, per_objfile, filename);
7668
7669 /* This must be done before calling dwarf2_build_include_psymtabs. */
7670 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7671
7672 baseaddr = objfile->text_section_offset ();
7673
7674 dwarf2_find_base_address (comp_unit_die, cu);
7675
7676 /* Possibly set the default values of LOWPC and HIGHPC from
7677 `DW_AT_ranges'. */
7678 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7679 &best_highpc, cu, pst);
7680 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7681 {
7682 CORE_ADDR low
7683 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7684 - baseaddr);
7685 CORE_ADDR high
7686 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7687 - baseaddr - 1);
7688 /* Store the contiguous range if it is not empty; it can be
7689 empty for CUs with no code. */
7690 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7691 low, high, pst);
7692 }
7693
7694 /* Check if comp unit has_children.
7695 If so, read the rest of the partial symbols from this comp unit.
7696 If not, there's no more debug_info for this comp unit. */
7697 if (comp_unit_die->has_children)
7698 {
7699 struct partial_die_info *first_die;
7700 CORE_ADDR lowpc, highpc;
7701
7702 lowpc = ((CORE_ADDR) -1);
7703 highpc = ((CORE_ADDR) 0);
7704
7705 first_die = load_partial_dies (reader, info_ptr, 1);
7706
7707 scan_partial_symbols (first_die, &lowpc, &highpc,
7708 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7709
7710 /* If we didn't find a lowpc, set it to highpc to avoid
7711 complaints from `maint check'. */
7712 if (lowpc == ((CORE_ADDR) -1))
7713 lowpc = highpc;
7714
7715 /* If the compilation unit didn't have an explicit address range,
7716 then use the information extracted from its child dies. */
7717 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7718 {
7719 best_lowpc = lowpc;
7720 best_highpc = highpc;
7721 }
7722 }
7723 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7724 best_lowpc + baseaddr)
7725 - baseaddr);
7726 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7727 best_highpc + baseaddr)
7728 - baseaddr);
7729
7730 pst->end ();
7731
7732 if (!cu->per_cu->imported_symtabs_empty ())
7733 {
7734 int i;
7735 int len = cu->per_cu->imported_symtabs_size ();
7736
7737 /* Fill in 'dependencies' here; we fill in 'users' in a
7738 post-pass. */
7739 pst->number_of_dependencies = len;
7740 pst->dependencies
7741 = per_bfd->partial_symtabs->allocate_dependencies (len);
7742 for (i = 0; i < len; ++i)
7743 {
7744 pst->dependencies[i]
7745 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7746 }
7747
7748 cu->per_cu->imported_symtabs_free ();
7749 }
7750
7751 /* Get the list of files included in the current compilation unit,
7752 and build a psymtab for each of them. */
7753 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7754
7755 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7756 ", %d global, %d static syms",
7757 per_cu->is_debug_types ? "type" : "comp",
7758 sect_offset_str (per_cu->sect_off),
7759 paddress (gdbarch, pst->text_low (objfile)),
7760 paddress (gdbarch, pst->text_high (objfile)),
7761 (int) pst->global_psymbols.size (),
7762 (int) pst->static_psymbols.size ());
7763 }
7764
7765 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7766 Process compilation unit THIS_CU for a psymtab. */
7767
7768 static void
7769 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7770 dwarf2_per_objfile *per_objfile,
7771 bool want_partial_unit,
7772 enum language pretend_language)
7773 {
7774 /* If this compilation unit was already read in, free the
7775 cached copy in order to read it in again. This is
7776 necessary because we skipped some symbols when we first
7777 read in the compilation unit (see load_partial_dies).
7778 This problem could be avoided, but the benefit is unclear. */
7779 per_objfile->remove_cu (this_cu);
7780
7781 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7782
7783 switch (reader.comp_unit_die->tag)
7784 {
7785 case DW_TAG_compile_unit:
7786 this_cu->unit_type = DW_UT_compile;
7787 break;
7788 case DW_TAG_partial_unit:
7789 this_cu->unit_type = DW_UT_partial;
7790 break;
7791 case DW_TAG_type_unit:
7792 this_cu->unit_type = DW_UT_type;
7793 break;
7794 default:
7795 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7796 dwarf_tag_name (reader.comp_unit_die->tag),
7797 sect_offset_str (reader.cu->per_cu->sect_off),
7798 objfile_name (per_objfile->objfile));
7799 }
7800
7801 if (reader.dummy_p)
7802 {
7803 /* Nothing. */
7804 }
7805 else if (this_cu->is_debug_types)
7806 build_type_psymtabs_reader (&reader, reader.info_ptr,
7807 reader.comp_unit_die);
7808 else if (want_partial_unit
7809 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7810 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7811 reader.comp_unit_die,
7812 pretend_language);
7813
7814 this_cu->lang = reader.cu->language;
7815
7816 /* Age out any secondary CUs. */
7817 per_objfile->age_comp_units ();
7818 }
7819
7820 /* Reader function for build_type_psymtabs. */
7821
7822 static void
7823 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7824 const gdb_byte *info_ptr,
7825 struct die_info *type_unit_die)
7826 {
7827 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7828 struct dwarf2_cu *cu = reader->cu;
7829 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7830 struct signatured_type *sig_type;
7831 struct type_unit_group *tu_group;
7832 struct attribute *attr;
7833 struct partial_die_info *first_die;
7834 CORE_ADDR lowpc, highpc;
7835 dwarf2_psymtab *pst;
7836
7837 gdb_assert (per_cu->is_debug_types);
7838 sig_type = (struct signatured_type *) per_cu;
7839
7840 if (! type_unit_die->has_children)
7841 return;
7842
7843 attr = type_unit_die->attr (DW_AT_stmt_list);
7844 tu_group = get_type_unit_group (cu, attr);
7845
7846 if (tu_group->tus == nullptr)
7847 tu_group->tus = new std::vector<signatured_type *>;
7848 tu_group->tus->push_back (sig_type);
7849
7850 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7851 pst = create_partial_symtab (per_cu, per_objfile, "");
7852 pst->anonymous = true;
7853
7854 first_die = load_partial_dies (reader, info_ptr, 1);
7855
7856 lowpc = (CORE_ADDR) -1;
7857 highpc = (CORE_ADDR) 0;
7858 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7859
7860 pst->end ();
7861 }
7862
7863 /* Struct used to sort TUs by their abbreviation table offset. */
7864
7865 struct tu_abbrev_offset
7866 {
7867 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7868 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7869 {}
7870
7871 signatured_type *sig_type;
7872 sect_offset abbrev_offset;
7873 };
7874
7875 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7876
7877 static bool
7878 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7879 const struct tu_abbrev_offset &b)
7880 {
7881 return a.abbrev_offset < b.abbrev_offset;
7882 }
7883
7884 /* Efficiently read all the type units.
7885 This does the bulk of the work for build_type_psymtabs.
7886
7887 The efficiency is because we sort TUs by the abbrev table they use and
7888 only read each abbrev table once. In one program there are 200K TUs
7889 sharing 8K abbrev tables.
7890
7891 The main purpose of this function is to support building the
7892 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7893 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7894 can collapse the search space by grouping them by stmt_list.
7895 The savings can be significant, in the same program from above the 200K TUs
7896 share 8K stmt_list tables.
7897
7898 FUNC is expected to call get_type_unit_group, which will create the
7899 struct type_unit_group if necessary and add it to
7900 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7901
7902 static void
7903 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7904 {
7905 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7906 abbrev_table_up abbrev_table;
7907 sect_offset abbrev_offset;
7908
7909 /* It's up to the caller to not call us multiple times. */
7910 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7911
7912 if (per_objfile->per_bfd->all_type_units.empty ())
7913 return;
7914
7915 /* TUs typically share abbrev tables, and there can be way more TUs than
7916 abbrev tables. Sort by abbrev table to reduce the number of times we
7917 read each abbrev table in.
7918 Alternatives are to punt or to maintain a cache of abbrev tables.
7919 This is simpler and efficient enough for now.
7920
7921 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7922 symtab to use). Typically TUs with the same abbrev offset have the same
7923 stmt_list value too so in practice this should work well.
7924
7925 The basic algorithm here is:
7926
7927 sort TUs by abbrev table
7928 for each TU with same abbrev table:
7929 read abbrev table if first user
7930 read TU top level DIE
7931 [IWBN if DWO skeletons had DW_AT_stmt_list]
7932 call FUNC */
7933
7934 dwarf_read_debug_printf ("Building type unit groups ...");
7935
7936 /* Sort in a separate table to maintain the order of all_type_units
7937 for .gdb_index: TU indices directly index all_type_units. */
7938 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7939 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7940
7941 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7942 sorted_by_abbrev.emplace_back
7943 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7944 sig_type->per_cu.sect_off));
7945
7946 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7947 sort_tu_by_abbrev_offset);
7948
7949 abbrev_offset = (sect_offset) ~(unsigned) 0;
7950
7951 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7952 {
7953 /* Switch to the next abbrev table if necessary. */
7954 if (abbrev_table == NULL
7955 || tu.abbrev_offset != abbrev_offset)
7956 {
7957 abbrev_offset = tu.abbrev_offset;
7958 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7959 abbrev_table =
7960 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7961 ++tu_stats->nr_uniq_abbrev_tables;
7962 }
7963
7964 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7965 abbrev_table.get (), nullptr, false);
7966 if (!reader.dummy_p)
7967 build_type_psymtabs_reader (&reader, reader.info_ptr,
7968 reader.comp_unit_die);
7969 }
7970 }
7971
7972 /* Print collected type unit statistics. */
7973
7974 static void
7975 print_tu_stats (dwarf2_per_objfile *per_objfile)
7976 {
7977 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7978
7979 dwarf_read_debug_printf ("Type unit statistics:");
7980 dwarf_read_debug_printf (" %zu TUs",
7981 per_objfile->per_bfd->all_type_units.size ());
7982 dwarf_read_debug_printf (" %d uniq abbrev tables",
7983 tu_stats->nr_uniq_abbrev_tables);
7984 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7985 tu_stats->nr_symtabs);
7986 dwarf_read_debug_printf (" %d symtab sharers",
7987 tu_stats->nr_symtab_sharers);
7988 dwarf_read_debug_printf (" %d type units without a stmt_list",
7989 tu_stats->nr_stmt_less_type_units);
7990 dwarf_read_debug_printf (" %d all_type_units reallocs",
7991 tu_stats->nr_all_type_units_reallocs);
7992 }
7993
7994 /* Traversal function for build_type_psymtabs. */
7995
7996 static int
7997 build_type_psymtab_dependencies (void **slot, void *info)
7998 {
7999 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8000 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
8001 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8002 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8003 dwarf2_psymtab *pst = per_cu->v.psymtab;
8004 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8005 int i;
8006
8007 gdb_assert (len > 0);
8008 gdb_assert (per_cu->type_unit_group_p ());
8009
8010 pst->number_of_dependencies = len;
8011 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
8012 for (i = 0; i < len; ++i)
8013 {
8014 struct signatured_type *iter = tu_group->tus->at (i);
8015 gdb_assert (iter->per_cu.is_debug_types);
8016 pst->dependencies[i] = iter->per_cu.v.psymtab;
8017 iter->type_unit_group = tu_group;
8018 }
8019
8020 delete tu_group->tus;
8021 tu_group->tus = nullptr;
8022
8023 return 1;
8024 }
8025
8026 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8027 Build partial symbol tables for the .debug_types comp-units. */
8028
8029 static void
8030 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
8031 {
8032 if (! create_all_type_units (per_objfile))
8033 return;
8034
8035 build_type_psymtabs_1 (per_objfile);
8036 }
8037
8038 /* Traversal function for process_skeletonless_type_unit.
8039 Read a TU in a DWO file and build partial symbols for it. */
8040
8041 static int
8042 process_skeletonless_type_unit (void **slot, void *info)
8043 {
8044 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8045 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8046 struct signatured_type find_entry, *entry;
8047
8048 /* If this TU doesn't exist in the global table, add it and read it in. */
8049
8050 if (per_objfile->per_bfd->signatured_types == NULL)
8051 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
8052
8053 find_entry.signature = dwo_unit->signature;
8054 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
8055 &find_entry, INSERT);
8056 /* If we've already seen this type there's nothing to do. What's happening
8057 is we're doing our own version of comdat-folding here. */
8058 if (*slot != NULL)
8059 return 1;
8060
8061 /* This does the job that create_all_type_units would have done for
8062 this TU. */
8063 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8064 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
8065 *slot = entry;
8066
8067 /* This does the job that build_type_psymtabs_1 would have done. */
8068 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
8069 if (!reader.dummy_p)
8070 build_type_psymtabs_reader (&reader, reader.info_ptr,
8071 reader.comp_unit_die);
8072
8073 return 1;
8074 }
8075
8076 /* Traversal function for process_skeletonless_type_units. */
8077
8078 static int
8079 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8080 {
8081 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8082
8083 if (dwo_file->tus != NULL)
8084 htab_traverse_noresize (dwo_file->tus.get (),
8085 process_skeletonless_type_unit, info);
8086
8087 return 1;
8088 }
8089
8090 /* Scan all TUs of DWO files, verifying we've processed them.
8091 This is needed in case a TU was emitted without its skeleton.
8092 Note: This can't be done until we know what all the DWO files are. */
8093
8094 static void
8095 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8096 {
8097 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8098 if (get_dwp_file (per_objfile) == NULL
8099 && per_objfile->per_bfd->dwo_files != NULL)
8100 {
8101 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8102 process_dwo_file_for_skeletonless_type_units,
8103 per_objfile);
8104 }
8105 }
8106
8107 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8108
8109 static void
8110 set_partial_user (dwarf2_per_objfile *per_objfile)
8111 {
8112 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8113 {
8114 dwarf2_psymtab *pst = per_cu->v.psymtab;
8115
8116 if (pst == NULL)
8117 continue;
8118
8119 for (int j = 0; j < pst->number_of_dependencies; ++j)
8120 {
8121 /* Set the 'user' field only if it is not already set. */
8122 if (pst->dependencies[j]->user == NULL)
8123 pst->dependencies[j]->user = pst;
8124 }
8125 }
8126 }
8127
8128 /* Build the partial symbol table by doing a quick pass through the
8129 .debug_info and .debug_abbrev sections. */
8130
8131 static void
8132 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8133 {
8134 struct objfile *objfile = per_objfile->objfile;
8135 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
8136
8137 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8138 objfile_name (objfile));
8139
8140 scoped_restore restore_reading_psyms
8141 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
8142
8143 per_bfd->info.read (objfile);
8144
8145 /* Any cached compilation units will be linked by the per-objfile
8146 read_in_chain. Make sure to free them when we're done. */
8147 free_cached_comp_units freer (per_objfile);
8148
8149 build_type_psymtabs (per_objfile);
8150
8151 create_all_comp_units (per_objfile);
8152
8153 /* Create a temporary address map on a temporary obstack. We later
8154 copy this to the final obstack. */
8155 auto_obstack temp_obstack;
8156
8157 scoped_restore save_psymtabs_addrmap
8158 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
8159 addrmap_create_mutable (&temp_obstack));
8160
8161 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
8162 {
8163 if (per_cu->v.psymtab != NULL)
8164 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8165 continue;
8166 process_psymtab_comp_unit (per_cu, per_objfile, false,
8167 language_minimal);
8168 }
8169
8170 /* This has to wait until we read the CUs, we need the list of DWOs. */
8171 process_skeletonless_type_units (per_objfile);
8172
8173 /* Now that all TUs have been processed we can fill in the dependencies. */
8174 if (per_bfd->type_unit_groups != NULL)
8175 {
8176 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
8177 build_type_psymtab_dependencies, per_objfile);
8178 }
8179
8180 if (dwarf_read_debug > 0)
8181 print_tu_stats (per_objfile);
8182
8183 set_partial_user (per_objfile);
8184
8185 per_bfd->partial_symtabs->psymtabs_addrmap
8186 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
8187 per_bfd->partial_symtabs->obstack ());
8188 /* At this point we want to keep the address map. */
8189 save_psymtabs_addrmap.release ();
8190
8191 dwarf_read_debug_printf ("Done building psymtabs of %s",
8192 objfile_name (objfile));
8193 }
8194
8195 /* Load the partial DIEs for a secondary CU into memory.
8196 This is also used when rereading a primary CU with load_all_dies. */
8197
8198 static void
8199 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8200 dwarf2_per_objfile *per_objfile,
8201 dwarf2_cu *existing_cu)
8202 {
8203 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8204
8205 if (!reader.dummy_p)
8206 {
8207 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8208 language_minimal);
8209
8210 /* Check if comp unit has_children.
8211 If so, read the rest of the partial symbols from this comp unit.
8212 If not, there's no more debug_info for this comp unit. */
8213 if (reader.comp_unit_die->has_children)
8214 load_partial_dies (&reader, reader.info_ptr, 0);
8215
8216 reader.keep ();
8217 }
8218 }
8219
8220 static void
8221 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8222 struct dwarf2_section_info *section,
8223 struct dwarf2_section_info *abbrev_section,
8224 unsigned int is_dwz)
8225 {
8226 const gdb_byte *info_ptr;
8227 struct objfile *objfile = per_objfile->objfile;
8228
8229 dwarf_read_debug_printf ("Reading %s for %s",
8230 section->get_name (),
8231 section->get_file_name ());
8232
8233 section->read (objfile);
8234
8235 info_ptr = section->buffer;
8236
8237 while (info_ptr < section->buffer + section->size)
8238 {
8239 struct dwarf2_per_cu_data *this_cu;
8240
8241 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8242
8243 comp_unit_head cu_header;
8244 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8245 abbrev_section, info_ptr,
8246 rcuh_kind::COMPILE);
8247
8248 /* Save the compilation unit for later lookup. */
8249 if (cu_header.unit_type != DW_UT_type)
8250 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8251 else
8252 {
8253 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8254 sig_type->signature = cu_header.signature;
8255 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8256 this_cu = &sig_type->per_cu;
8257 }
8258 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8259 this_cu->sect_off = sect_off;
8260 this_cu->length = cu_header.length + cu_header.initial_length_size;
8261 this_cu->is_dwz = is_dwz;
8262 this_cu->section = section;
8263
8264 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8265
8266 info_ptr = info_ptr + this_cu->length;
8267 }
8268 }
8269
8270 /* Create a list of all compilation units in OBJFILE.
8271 This is only done for -readnow and building partial symtabs. */
8272
8273 static void
8274 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8275 {
8276 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8277 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8278 &per_objfile->per_bfd->abbrev, 0);
8279
8280 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8281 if (dwz != NULL)
8282 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8283 }
8284
8285 /* Process all loaded DIEs for compilation unit CU, starting at
8286 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8287 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8288 DW_AT_ranges). See the comments of add_partial_subprogram on how
8289 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8290
8291 static void
8292 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8293 CORE_ADDR *highpc, int set_addrmap,
8294 struct dwarf2_cu *cu)
8295 {
8296 struct partial_die_info *pdi;
8297
8298 /* Now, march along the PDI's, descending into ones which have
8299 interesting children but skipping the children of the other ones,
8300 until we reach the end of the compilation unit. */
8301
8302 pdi = first_die;
8303
8304 while (pdi != NULL)
8305 {
8306 pdi->fixup (cu);
8307
8308 /* Anonymous namespaces or modules have no name but have interesting
8309 children, so we need to look at them. Ditto for anonymous
8310 enums. */
8311
8312 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8313 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8314 || pdi->tag == DW_TAG_imported_unit
8315 || pdi->tag == DW_TAG_inlined_subroutine)
8316 {
8317 switch (pdi->tag)
8318 {
8319 case DW_TAG_subprogram:
8320 case DW_TAG_inlined_subroutine:
8321 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8322 if (cu->language == language_cplus)
8323 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8324 set_addrmap, cu);
8325 break;
8326 case DW_TAG_constant:
8327 case DW_TAG_variable:
8328 case DW_TAG_typedef:
8329 case DW_TAG_union_type:
8330 if (!pdi->is_declaration
8331 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8332 {
8333 add_partial_symbol (pdi, cu);
8334 }
8335 break;
8336 case DW_TAG_class_type:
8337 case DW_TAG_interface_type:
8338 case DW_TAG_structure_type:
8339 if (!pdi->is_declaration)
8340 {
8341 add_partial_symbol (pdi, cu);
8342 }
8343 if ((cu->language == language_rust
8344 || cu->language == language_cplus) && pdi->has_children)
8345 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8346 set_addrmap, cu);
8347 break;
8348 case DW_TAG_enumeration_type:
8349 if (!pdi->is_declaration)
8350 add_partial_enumeration (pdi, cu);
8351 break;
8352 case DW_TAG_base_type:
8353 case DW_TAG_subrange_type:
8354 /* File scope base type definitions are added to the partial
8355 symbol table. */
8356 add_partial_symbol (pdi, cu);
8357 break;
8358 case DW_TAG_namespace:
8359 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8360 break;
8361 case DW_TAG_module:
8362 if (!pdi->is_declaration)
8363 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8364 break;
8365 case DW_TAG_imported_unit:
8366 {
8367 struct dwarf2_per_cu_data *per_cu;
8368
8369 /* For now we don't handle imported units in type units. */
8370 if (cu->per_cu->is_debug_types)
8371 {
8372 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8373 " supported in type units [in module %s]"),
8374 objfile_name (cu->per_objfile->objfile));
8375 }
8376
8377 per_cu = dwarf2_find_containing_comp_unit
8378 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8379
8380 /* Go read the partial unit, if needed. */
8381 if (per_cu->v.psymtab == NULL)
8382 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8383 cu->language);
8384
8385 cu->per_cu->imported_symtabs_push (per_cu);
8386 }
8387 break;
8388 case DW_TAG_imported_declaration:
8389 add_partial_symbol (pdi, cu);
8390 break;
8391 default:
8392 break;
8393 }
8394 }
8395
8396 /* If the die has a sibling, skip to the sibling. */
8397
8398 pdi = pdi->die_sibling;
8399 }
8400 }
8401
8402 /* Functions used to compute the fully scoped name of a partial DIE.
8403
8404 Normally, this is simple. For C++, the parent DIE's fully scoped
8405 name is concatenated with "::" and the partial DIE's name.
8406 Enumerators are an exception; they use the scope of their parent
8407 enumeration type, i.e. the name of the enumeration type is not
8408 prepended to the enumerator.
8409
8410 There are two complexities. One is DW_AT_specification; in this
8411 case "parent" means the parent of the target of the specification,
8412 instead of the direct parent of the DIE. The other is compilers
8413 which do not emit DW_TAG_namespace; in this case we try to guess
8414 the fully qualified name of structure types from their members'
8415 linkage names. This must be done using the DIE's children rather
8416 than the children of any DW_AT_specification target. We only need
8417 to do this for structures at the top level, i.e. if the target of
8418 any DW_AT_specification (if any; otherwise the DIE itself) does not
8419 have a parent. */
8420
8421 /* Compute the scope prefix associated with PDI's parent, in
8422 compilation unit CU. The result will be allocated on CU's
8423 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8424 field. NULL is returned if no prefix is necessary. */
8425 static const char *
8426 partial_die_parent_scope (struct partial_die_info *pdi,
8427 struct dwarf2_cu *cu)
8428 {
8429 const char *grandparent_scope;
8430 struct partial_die_info *parent, *real_pdi;
8431
8432 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8433 then this means the parent of the specification DIE. */
8434
8435 real_pdi = pdi;
8436 while (real_pdi->has_specification)
8437 {
8438 auto res = find_partial_die (real_pdi->spec_offset,
8439 real_pdi->spec_is_dwz, cu);
8440 real_pdi = res.pdi;
8441 cu = res.cu;
8442 }
8443
8444 parent = real_pdi->die_parent;
8445 if (parent == NULL)
8446 return NULL;
8447
8448 if (parent->scope_set)
8449 return parent->scope;
8450
8451 parent->fixup (cu);
8452
8453 grandparent_scope = partial_die_parent_scope (parent, cu);
8454
8455 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8456 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8457 Work around this problem here. */
8458 if (cu->language == language_cplus
8459 && parent->tag == DW_TAG_namespace
8460 && strcmp (parent->name (cu), "::") == 0
8461 && grandparent_scope == NULL)
8462 {
8463 parent->scope = NULL;
8464 parent->scope_set = 1;
8465 return NULL;
8466 }
8467
8468 /* Nested subroutines in Fortran get a prefix. */
8469 if (pdi->tag == DW_TAG_enumerator)
8470 /* Enumerators should not get the name of the enumeration as a prefix. */
8471 parent->scope = grandparent_scope;
8472 else if (parent->tag == DW_TAG_namespace
8473 || parent->tag == DW_TAG_module
8474 || parent->tag == DW_TAG_structure_type
8475 || parent->tag == DW_TAG_class_type
8476 || parent->tag == DW_TAG_interface_type
8477 || parent->tag == DW_TAG_union_type
8478 || parent->tag == DW_TAG_enumeration_type
8479 || (cu->language == language_fortran
8480 && parent->tag == DW_TAG_subprogram
8481 && pdi->tag == DW_TAG_subprogram))
8482 {
8483 if (grandparent_scope == NULL)
8484 parent->scope = parent->name (cu);
8485 else
8486 parent->scope = typename_concat (&cu->comp_unit_obstack,
8487 grandparent_scope,
8488 parent->name (cu), 0, cu);
8489 }
8490 else
8491 {
8492 /* FIXME drow/2004-04-01: What should we be doing with
8493 function-local names? For partial symbols, we should probably be
8494 ignoring them. */
8495 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8496 dwarf_tag_name (parent->tag),
8497 sect_offset_str (pdi->sect_off));
8498 parent->scope = grandparent_scope;
8499 }
8500
8501 parent->scope_set = 1;
8502 return parent->scope;
8503 }
8504
8505 /* Return the fully scoped name associated with PDI, from compilation unit
8506 CU. The result will be allocated with malloc. */
8507
8508 static gdb::unique_xmalloc_ptr<char>
8509 partial_die_full_name (struct partial_die_info *pdi,
8510 struct dwarf2_cu *cu)
8511 {
8512 const char *parent_scope;
8513
8514 /* If this is a template instantiation, we can not work out the
8515 template arguments from partial DIEs. So, unfortunately, we have
8516 to go through the full DIEs. At least any work we do building
8517 types here will be reused if full symbols are loaded later. */
8518 if (pdi->has_template_arguments)
8519 {
8520 pdi->fixup (cu);
8521
8522 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8523 {
8524 struct die_info *die;
8525 struct attribute attr;
8526 struct dwarf2_cu *ref_cu = cu;
8527
8528 /* DW_FORM_ref_addr is using section offset. */
8529 attr.name = (enum dwarf_attribute) 0;
8530 attr.form = DW_FORM_ref_addr;
8531 attr.u.unsnd = to_underlying (pdi->sect_off);
8532 die = follow_die_ref (NULL, &attr, &ref_cu);
8533
8534 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8535 }
8536 }
8537
8538 parent_scope = partial_die_parent_scope (pdi, cu);
8539 if (parent_scope == NULL)
8540 return NULL;
8541 else
8542 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8543 pdi->name (cu),
8544 0, cu));
8545 }
8546
8547 static void
8548 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8549 {
8550 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8551 struct objfile *objfile = per_objfile->objfile;
8552 struct gdbarch *gdbarch = objfile->arch ();
8553 CORE_ADDR addr = 0;
8554 const char *actual_name = NULL;
8555 CORE_ADDR baseaddr;
8556
8557 baseaddr = objfile->text_section_offset ();
8558
8559 gdb::unique_xmalloc_ptr<char> built_actual_name
8560 = partial_die_full_name (pdi, cu);
8561 if (built_actual_name != NULL)
8562 actual_name = built_actual_name.get ();
8563
8564 if (actual_name == NULL)
8565 actual_name = pdi->name (cu);
8566
8567 partial_symbol psymbol;
8568 memset (&psymbol, 0, sizeof (psymbol));
8569 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8570 psymbol.ginfo.set_section_index (-1);
8571
8572 /* The code below indicates that the psymbol should be installed by
8573 setting this. */
8574 gdb::optional<psymbol_placement> where;
8575
8576 switch (pdi->tag)
8577 {
8578 case DW_TAG_inlined_subroutine:
8579 case DW_TAG_subprogram:
8580 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8581 - baseaddr);
8582 if (pdi->is_external
8583 || cu->language == language_ada
8584 || (cu->language == language_fortran
8585 && pdi->die_parent != NULL
8586 && pdi->die_parent->tag == DW_TAG_subprogram))
8587 {
8588 /* Normally, only "external" DIEs are part of the global scope.
8589 But in Ada and Fortran, we want to be able to access nested
8590 procedures globally. So all Ada and Fortran subprograms are
8591 stored in the global scope. */
8592 where = psymbol_placement::GLOBAL;
8593 }
8594 else
8595 where = psymbol_placement::STATIC;
8596
8597 psymbol.domain = VAR_DOMAIN;
8598 psymbol.aclass = LOC_BLOCK;
8599 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8600 psymbol.ginfo.value.address = addr;
8601
8602 if (pdi->main_subprogram && actual_name != NULL)
8603 set_objfile_main_name (objfile, actual_name, cu->language);
8604 break;
8605 case DW_TAG_constant:
8606 psymbol.domain = VAR_DOMAIN;
8607 psymbol.aclass = LOC_STATIC;
8608 where = (pdi->is_external
8609 ? psymbol_placement::GLOBAL
8610 : psymbol_placement::STATIC);
8611 break;
8612 case DW_TAG_variable:
8613 if (pdi->d.locdesc)
8614 addr = decode_locdesc (pdi->d.locdesc, cu);
8615
8616 if (pdi->d.locdesc
8617 && addr == 0
8618 && !per_objfile->per_bfd->has_section_at_zero)
8619 {
8620 /* A global or static variable may also have been stripped
8621 out by the linker if unused, in which case its address
8622 will be nullified; do not add such variables into partial
8623 symbol table then. */
8624 }
8625 else if (pdi->is_external)
8626 {
8627 /* Global Variable.
8628 Don't enter into the minimal symbol tables as there is
8629 a minimal symbol table entry from the ELF symbols already.
8630 Enter into partial symbol table if it has a location
8631 descriptor or a type.
8632 If the location descriptor is missing, new_symbol will create
8633 a LOC_UNRESOLVED symbol, the address of the variable will then
8634 be determined from the minimal symbol table whenever the variable
8635 is referenced.
8636 The address for the partial symbol table entry is not
8637 used by GDB, but it comes in handy for debugging partial symbol
8638 table building. */
8639
8640 if (pdi->d.locdesc || pdi->has_type)
8641 {
8642 psymbol.domain = VAR_DOMAIN;
8643 psymbol.aclass = LOC_STATIC;
8644 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8645 psymbol.ginfo.value.address = addr;
8646 where = psymbol_placement::GLOBAL;
8647 }
8648 }
8649 else
8650 {
8651 int has_loc = pdi->d.locdesc != NULL;
8652
8653 /* Static Variable. Skip symbols whose value we cannot know (those
8654 without location descriptors or constant values). */
8655 if (!has_loc && !pdi->has_const_value)
8656 return;
8657
8658 psymbol.domain = VAR_DOMAIN;
8659 psymbol.aclass = LOC_STATIC;
8660 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8661 if (has_loc)
8662 psymbol.ginfo.value.address = addr;
8663 where = psymbol_placement::STATIC;
8664 }
8665 break;
8666 case DW_TAG_array_type:
8667 case DW_TAG_typedef:
8668 case DW_TAG_base_type:
8669 case DW_TAG_subrange_type:
8670 psymbol.domain = VAR_DOMAIN;
8671 psymbol.aclass = LOC_TYPEDEF;
8672 where = psymbol_placement::STATIC;
8673 break;
8674 case DW_TAG_imported_declaration:
8675 case DW_TAG_namespace:
8676 psymbol.domain = VAR_DOMAIN;
8677 psymbol.aclass = LOC_TYPEDEF;
8678 where = psymbol_placement::GLOBAL;
8679 break;
8680 case DW_TAG_module:
8681 /* With Fortran 77 there might be a "BLOCK DATA" module
8682 available without any name. If so, we skip the module as it
8683 doesn't bring any value. */
8684 if (actual_name != nullptr)
8685 {
8686 psymbol.domain = MODULE_DOMAIN;
8687 psymbol.aclass = LOC_TYPEDEF;
8688 where = psymbol_placement::GLOBAL;
8689 }
8690 break;
8691 case DW_TAG_class_type:
8692 case DW_TAG_interface_type:
8693 case DW_TAG_structure_type:
8694 case DW_TAG_union_type:
8695 case DW_TAG_enumeration_type:
8696 /* Skip external references. The DWARF standard says in the section
8697 about "Structure, Union, and Class Type Entries": "An incomplete
8698 structure, union or class type is represented by a structure,
8699 union or class entry that does not have a byte size attribute
8700 and that has a DW_AT_declaration attribute." */
8701 if (!pdi->has_byte_size && pdi->is_declaration)
8702 return;
8703
8704 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8705 static vs. global. */
8706 psymbol.domain = STRUCT_DOMAIN;
8707 psymbol.aclass = LOC_TYPEDEF;
8708 where = (cu->language == language_cplus
8709 ? psymbol_placement::GLOBAL
8710 : psymbol_placement::STATIC);
8711 break;
8712 case DW_TAG_enumerator:
8713 psymbol.domain = VAR_DOMAIN;
8714 psymbol.aclass = LOC_CONST;
8715 where = (cu->language == language_cplus
8716 ? psymbol_placement::GLOBAL
8717 : psymbol_placement::STATIC);
8718 break;
8719 default:
8720 break;
8721 }
8722
8723 if (where.has_value ())
8724 {
8725 if (built_actual_name != nullptr)
8726 actual_name = objfile->intern (actual_name);
8727 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8728 psymbol.ginfo.set_linkage_name (actual_name);
8729 else
8730 {
8731 psymbol.ginfo.set_demangled_name (actual_name,
8732 &objfile->objfile_obstack);
8733 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8734 }
8735 cu->per_cu->v.psymtab->add_psymbol
8736 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8737 objfile);
8738 }
8739 }
8740
8741 /* Read a partial die corresponding to a namespace; also, add a symbol
8742 corresponding to that namespace to the symbol table. NAMESPACE is
8743 the name of the enclosing namespace. */
8744
8745 static void
8746 add_partial_namespace (struct partial_die_info *pdi,
8747 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8748 int set_addrmap, struct dwarf2_cu *cu)
8749 {
8750 /* Add a symbol for the namespace. */
8751
8752 add_partial_symbol (pdi, cu);
8753
8754 /* Now scan partial symbols in that namespace. */
8755
8756 if (pdi->has_children)
8757 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8758 }
8759
8760 /* Read a partial die corresponding to a Fortran module. */
8761
8762 static void
8763 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8764 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8765 {
8766 /* Add a symbol for the namespace. */
8767
8768 add_partial_symbol (pdi, cu);
8769
8770 /* Now scan partial symbols in that module. */
8771
8772 if (pdi->has_children)
8773 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8774 }
8775
8776 /* Read a partial die corresponding to a subprogram or an inlined
8777 subprogram and create a partial symbol for that subprogram.
8778 When the CU language allows it, this routine also defines a partial
8779 symbol for each nested subprogram that this subprogram contains.
8780 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8781 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8782
8783 PDI may also be a lexical block, in which case we simply search
8784 recursively for subprograms defined inside that lexical block.
8785 Again, this is only performed when the CU language allows this
8786 type of definitions. */
8787
8788 static void
8789 add_partial_subprogram (struct partial_die_info *pdi,
8790 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8791 int set_addrmap, struct dwarf2_cu *cu)
8792 {
8793 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8794 {
8795 if (pdi->has_pc_info)
8796 {
8797 if (pdi->lowpc < *lowpc)
8798 *lowpc = pdi->lowpc;
8799 if (pdi->highpc > *highpc)
8800 *highpc = pdi->highpc;
8801 if (set_addrmap)
8802 {
8803 struct objfile *objfile = cu->per_objfile->objfile;
8804 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8805 struct gdbarch *gdbarch = objfile->arch ();
8806 CORE_ADDR baseaddr;
8807 CORE_ADDR this_highpc;
8808 CORE_ADDR this_lowpc;
8809
8810 baseaddr = objfile->text_section_offset ();
8811 this_lowpc
8812 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8813 pdi->lowpc + baseaddr)
8814 - baseaddr);
8815 this_highpc
8816 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8817 pdi->highpc + baseaddr)
8818 - baseaddr);
8819 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8820 this_lowpc, this_highpc - 1,
8821 cu->per_cu->v.psymtab);
8822 }
8823 }
8824
8825 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8826 {
8827 if (!pdi->is_declaration)
8828 /* Ignore subprogram DIEs that do not have a name, they are
8829 illegal. Do not emit a complaint at this point, we will
8830 do so when we convert this psymtab into a symtab. */
8831 if (pdi->name (cu))
8832 add_partial_symbol (pdi, cu);
8833 }
8834 }
8835
8836 if (! pdi->has_children)
8837 return;
8838
8839 if (cu->language == language_ada || cu->language == language_fortran)
8840 {
8841 pdi = pdi->die_child;
8842 while (pdi != NULL)
8843 {
8844 pdi->fixup (cu);
8845 if (pdi->tag == DW_TAG_subprogram
8846 || pdi->tag == DW_TAG_inlined_subroutine
8847 || pdi->tag == DW_TAG_lexical_block)
8848 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8849 pdi = pdi->die_sibling;
8850 }
8851 }
8852 }
8853
8854 /* Read a partial die corresponding to an enumeration type. */
8855
8856 static void
8857 add_partial_enumeration (struct partial_die_info *enum_pdi,
8858 struct dwarf2_cu *cu)
8859 {
8860 struct partial_die_info *pdi;
8861
8862 if (enum_pdi->name (cu) != NULL)
8863 add_partial_symbol (enum_pdi, cu);
8864
8865 pdi = enum_pdi->die_child;
8866 while (pdi)
8867 {
8868 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8869 complaint (_("malformed enumerator DIE ignored"));
8870 else
8871 add_partial_symbol (pdi, cu);
8872 pdi = pdi->die_sibling;
8873 }
8874 }
8875
8876 /* Return the initial uleb128 in the die at INFO_PTR. */
8877
8878 static unsigned int
8879 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8880 {
8881 unsigned int bytes_read;
8882
8883 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8884 }
8885
8886 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8887 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8888
8889 Return the corresponding abbrev, or NULL if the number is zero (indicating
8890 an empty DIE). In either case *BYTES_READ will be set to the length of
8891 the initial number. */
8892
8893 static const struct abbrev_info *
8894 peek_die_abbrev (const die_reader_specs &reader,
8895 const gdb_byte *info_ptr, unsigned int *bytes_read)
8896 {
8897 dwarf2_cu *cu = reader.cu;
8898 bfd *abfd = reader.abfd;
8899 unsigned int abbrev_number
8900 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8901
8902 if (abbrev_number == 0)
8903 return NULL;
8904
8905 const abbrev_info *abbrev
8906 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8907 if (!abbrev)
8908 {
8909 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8910 " at offset %s [in module %s]"),
8911 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8912 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8913 }
8914
8915 return abbrev;
8916 }
8917
8918 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8919 Returns a pointer to the end of a series of DIEs, terminated by an empty
8920 DIE. Any children of the skipped DIEs will also be skipped. */
8921
8922 static const gdb_byte *
8923 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8924 {
8925 while (1)
8926 {
8927 unsigned int bytes_read;
8928 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8929 &bytes_read);
8930
8931 if (abbrev == NULL)
8932 return info_ptr + bytes_read;
8933 else
8934 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8935 }
8936 }
8937
8938 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8939 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8940 abbrev corresponding to that skipped uleb128 should be passed in
8941 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8942 children. */
8943
8944 static const gdb_byte *
8945 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8946 const struct abbrev_info *abbrev)
8947 {
8948 unsigned int bytes_read;
8949 struct attribute attr;
8950 bfd *abfd = reader->abfd;
8951 struct dwarf2_cu *cu = reader->cu;
8952 const gdb_byte *buffer = reader->buffer;
8953 const gdb_byte *buffer_end = reader->buffer_end;
8954 unsigned int form, i;
8955
8956 for (i = 0; i < abbrev->num_attrs; i++)
8957 {
8958 /* The only abbrev we care about is DW_AT_sibling. */
8959 if (abbrev->attrs[i].name == DW_AT_sibling)
8960 {
8961 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8962 if (attr.form == DW_FORM_ref_addr)
8963 complaint (_("ignoring absolute DW_AT_sibling"));
8964 else
8965 {
8966 sect_offset off = attr.get_ref_die_offset ();
8967 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8968
8969 if (sibling_ptr < info_ptr)
8970 complaint (_("DW_AT_sibling points backwards"));
8971 else if (sibling_ptr > reader->buffer_end)
8972 reader->die_section->overflow_complaint ();
8973 else
8974 return sibling_ptr;
8975 }
8976 }
8977
8978 /* If it isn't DW_AT_sibling, skip this attribute. */
8979 form = abbrev->attrs[i].form;
8980 skip_attribute:
8981 switch (form)
8982 {
8983 case DW_FORM_ref_addr:
8984 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8985 and later it is offset sized. */
8986 if (cu->header.version == 2)
8987 info_ptr += cu->header.addr_size;
8988 else
8989 info_ptr += cu->header.offset_size;
8990 break;
8991 case DW_FORM_GNU_ref_alt:
8992 info_ptr += cu->header.offset_size;
8993 break;
8994 case DW_FORM_addr:
8995 info_ptr += cu->header.addr_size;
8996 break;
8997 case DW_FORM_data1:
8998 case DW_FORM_ref1:
8999 case DW_FORM_flag:
9000 case DW_FORM_strx1:
9001 info_ptr += 1;
9002 break;
9003 case DW_FORM_flag_present:
9004 case DW_FORM_implicit_const:
9005 break;
9006 case DW_FORM_data2:
9007 case DW_FORM_ref2:
9008 case DW_FORM_strx2:
9009 info_ptr += 2;
9010 break;
9011 case DW_FORM_strx3:
9012 info_ptr += 3;
9013 break;
9014 case DW_FORM_data4:
9015 case DW_FORM_ref4:
9016 case DW_FORM_strx4:
9017 info_ptr += 4;
9018 break;
9019 case DW_FORM_data8:
9020 case DW_FORM_ref8:
9021 case DW_FORM_ref_sig8:
9022 info_ptr += 8;
9023 break;
9024 case DW_FORM_data16:
9025 info_ptr += 16;
9026 break;
9027 case DW_FORM_string:
9028 read_direct_string (abfd, info_ptr, &bytes_read);
9029 info_ptr += bytes_read;
9030 break;
9031 case DW_FORM_sec_offset:
9032 case DW_FORM_strp:
9033 case DW_FORM_GNU_strp_alt:
9034 info_ptr += cu->header.offset_size;
9035 break;
9036 case DW_FORM_exprloc:
9037 case DW_FORM_block:
9038 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9039 info_ptr += bytes_read;
9040 break;
9041 case DW_FORM_block1:
9042 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9043 break;
9044 case DW_FORM_block2:
9045 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9046 break;
9047 case DW_FORM_block4:
9048 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9049 break;
9050 case DW_FORM_addrx:
9051 case DW_FORM_strx:
9052 case DW_FORM_sdata:
9053 case DW_FORM_udata:
9054 case DW_FORM_ref_udata:
9055 case DW_FORM_GNU_addr_index:
9056 case DW_FORM_GNU_str_index:
9057 case DW_FORM_rnglistx:
9058 case DW_FORM_loclistx:
9059 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9060 break;
9061 case DW_FORM_indirect:
9062 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9063 info_ptr += bytes_read;
9064 /* We need to continue parsing from here, so just go back to
9065 the top. */
9066 goto skip_attribute;
9067
9068 default:
9069 error (_("Dwarf Error: Cannot handle %s "
9070 "in DWARF reader [in module %s]"),
9071 dwarf_form_name (form),
9072 bfd_get_filename (abfd));
9073 }
9074 }
9075
9076 if (abbrev->has_children)
9077 return skip_children (reader, info_ptr);
9078 else
9079 return info_ptr;
9080 }
9081
9082 /* Locate ORIG_PDI's sibling.
9083 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9084
9085 static const gdb_byte *
9086 locate_pdi_sibling (const struct die_reader_specs *reader,
9087 struct partial_die_info *orig_pdi,
9088 const gdb_byte *info_ptr)
9089 {
9090 /* Do we know the sibling already? */
9091
9092 if (orig_pdi->sibling)
9093 return orig_pdi->sibling;
9094
9095 /* Are there any children to deal with? */
9096
9097 if (!orig_pdi->has_children)
9098 return info_ptr;
9099
9100 /* Skip the children the long way. */
9101
9102 return skip_children (reader, info_ptr);
9103 }
9104
9105 /* Expand this partial symbol table into a full symbol table. SELF is
9106 not NULL. */
9107
9108 void
9109 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9110 {
9111 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9112
9113 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9114
9115 /* If this psymtab is constructed from a debug-only objfile, the
9116 has_section_at_zero flag will not necessarily be correct. We
9117 can get the correct value for this flag by looking at the data
9118 associated with the (presumably stripped) associated objfile. */
9119 if (objfile->separate_debug_objfile_backlink)
9120 {
9121 dwarf2_per_objfile *per_objfile_backlink
9122 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9123
9124 per_objfile->per_bfd->has_section_at_zero
9125 = per_objfile_backlink->per_bfd->has_section_at_zero;
9126 }
9127
9128 expand_psymtab (objfile);
9129
9130 process_cu_includes (per_objfile);
9131 }
9132 \f
9133 /* Reading in full CUs. */
9134
9135 /* Add PER_CU to the queue. */
9136
9137 static void
9138 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9139 dwarf2_per_objfile *per_objfile,
9140 enum language pretend_language)
9141 {
9142 per_cu->queued = 1;
9143
9144 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9145 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
9146 }
9147
9148 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9149
9150 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9151 dependency.
9152
9153 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9154 DIEs, false otherwise.
9155
9156 Explanation: there is an invariant that if a CU is queued for expansion
9157 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9158 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9159 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9160 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9161 invariant is respected.
9162
9163 The caller is therefore not required to load the CU's DIEs (we return false)
9164 if:
9165
9166 - the CU is already expanded, and therefore does not get enqueued
9167 - the CU gets enqueued for expansion, but its DIEs are already loaded
9168
9169 Note that the caller should not use this function's return value as an
9170 indicator of whether the CU's DIEs are loaded right now, it should check
9171 that by calling `dwarf2_per_objfile::get_cu` instead. */
9172
9173 static int
9174 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9175 dwarf2_per_cu_data *per_cu,
9176 dwarf2_per_objfile *per_objfile,
9177 enum language pretend_language)
9178 {
9179 /* We may arrive here during partial symbol reading, if we need full
9180 DIEs to process an unusual case (e.g. template arguments). Do
9181 not queue PER_CU, just tell our caller to load its DIEs. */
9182 if (per_cu->per_bfd->reading_partial_symbols)
9183 {
9184 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9185
9186 if (cu == NULL || cu->dies == NULL)
9187 return 1;
9188 return 0;
9189 }
9190
9191 /* Mark the dependence relation so that we don't flush PER_CU
9192 too early. */
9193 if (dependent_cu != NULL)
9194 dwarf2_add_dependence (dependent_cu, per_cu);
9195
9196 /* If it's already on the queue, we have nothing to do. */
9197 if (per_cu->queued)
9198 {
9199 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9200 loaded. */
9201 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9202
9203 /* If the CU is queued for expansion, it should not already be
9204 expanded. */
9205 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9206
9207 /* The DIEs are already loaded, the caller doesn't need to do it. */
9208 return 0;
9209 }
9210
9211 bool queued = false;
9212 if (!per_objfile->symtab_set_p (per_cu))
9213 {
9214 /* Add it to the queue. */
9215 queue_comp_unit (per_cu, per_objfile, pretend_language);
9216 queued = true;
9217 }
9218
9219 /* If the compilation unit is already loaded, just mark it as
9220 used. */
9221 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9222 if (cu != nullptr)
9223 cu->last_used = 0;
9224
9225 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9226 and the DIEs are not already loaded. */
9227 return queued && cu == nullptr;
9228 }
9229
9230 /* Process the queue. */
9231
9232 static void
9233 process_queue (dwarf2_per_objfile *per_objfile)
9234 {
9235 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9236 objfile_name (per_objfile->objfile));
9237
9238 /* The queue starts out with one item, but following a DIE reference
9239 may load a new CU, adding it to the end of the queue. */
9240 while (!per_objfile->per_bfd->queue->empty ())
9241 {
9242 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
9243 dwarf2_per_cu_data *per_cu = item.per_cu;
9244
9245 if (!per_objfile->symtab_set_p (per_cu))
9246 {
9247 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9248
9249 /* Skip dummy CUs. */
9250 if (cu != nullptr)
9251 {
9252 unsigned int debug_print_threshold;
9253 char buf[100];
9254
9255 if (per_cu->is_debug_types)
9256 {
9257 struct signatured_type *sig_type =
9258 (struct signatured_type *) per_cu;
9259
9260 sprintf (buf, "TU %s at offset %s",
9261 hex_string (sig_type->signature),
9262 sect_offset_str (per_cu->sect_off));
9263 /* There can be 100s of TUs.
9264 Only print them in verbose mode. */
9265 debug_print_threshold = 2;
9266 }
9267 else
9268 {
9269 sprintf (buf, "CU at offset %s",
9270 sect_offset_str (per_cu->sect_off));
9271 debug_print_threshold = 1;
9272 }
9273
9274 if (dwarf_read_debug >= debug_print_threshold)
9275 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9276
9277 if (per_cu->is_debug_types)
9278 process_full_type_unit (cu, item.pretend_language);
9279 else
9280 process_full_comp_unit (cu, item.pretend_language);
9281
9282 if (dwarf_read_debug >= debug_print_threshold)
9283 dwarf_read_debug_printf ("Done expanding %s", buf);
9284 }
9285 }
9286
9287 per_cu->queued = 0;
9288 per_objfile->per_bfd->queue->pop ();
9289 }
9290
9291 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9292 objfile_name (per_objfile->objfile));
9293 }
9294
9295 /* Read in full symbols for PST, and anything it depends on. */
9296
9297 void
9298 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9299 {
9300 gdb_assert (!readin_p (objfile));
9301
9302 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9303 free_cached_comp_units freer (per_objfile);
9304 expand_dependencies (objfile);
9305
9306 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9307 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9308 }
9309
9310 /* See psympriv.h. */
9311
9312 bool
9313 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9314 {
9315 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9316 return per_objfile->symtab_set_p (per_cu_data);
9317 }
9318
9319 /* See psympriv.h. */
9320
9321 compunit_symtab *
9322 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9323 {
9324 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9325 return per_objfile->get_symtab (per_cu_data);
9326 }
9327
9328 /* Trivial hash function for die_info: the hash value of a DIE
9329 is its offset in .debug_info for this objfile. */
9330
9331 static hashval_t
9332 die_hash (const void *item)
9333 {
9334 const struct die_info *die = (const struct die_info *) item;
9335
9336 return to_underlying (die->sect_off);
9337 }
9338
9339 /* Trivial comparison function for die_info structures: two DIEs
9340 are equal if they have the same offset. */
9341
9342 static int
9343 die_eq (const void *item_lhs, const void *item_rhs)
9344 {
9345 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9346 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9347
9348 return die_lhs->sect_off == die_rhs->sect_off;
9349 }
9350
9351 /* Load the DIEs associated with PER_CU into memory.
9352
9353 In some cases, the caller, while reading partial symbols, will need to load
9354 the full symbols for the CU for some reason. It will already have a
9355 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9356 rather than creating a new one. */
9357
9358 static void
9359 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9360 dwarf2_per_objfile *per_objfile,
9361 dwarf2_cu *existing_cu,
9362 bool skip_partial,
9363 enum language pretend_language)
9364 {
9365 gdb_assert (! this_cu->is_debug_types);
9366
9367 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9368 if (reader.dummy_p)
9369 return;
9370
9371 struct dwarf2_cu *cu = reader.cu;
9372 const gdb_byte *info_ptr = reader.info_ptr;
9373
9374 gdb_assert (cu->die_hash == NULL);
9375 cu->die_hash =
9376 htab_create_alloc_ex (cu->header.length / 12,
9377 die_hash,
9378 die_eq,
9379 NULL,
9380 &cu->comp_unit_obstack,
9381 hashtab_obstack_allocate,
9382 dummy_obstack_deallocate);
9383
9384 if (reader.comp_unit_die->has_children)
9385 reader.comp_unit_die->child
9386 = read_die_and_siblings (&reader, reader.info_ptr,
9387 &info_ptr, reader.comp_unit_die);
9388 cu->dies = reader.comp_unit_die;
9389 /* comp_unit_die is not stored in die_hash, no need. */
9390
9391 /* We try not to read any attributes in this function, because not
9392 all CUs needed for references have been loaded yet, and symbol
9393 table processing isn't initialized. But we have to set the CU language,
9394 or we won't be able to build types correctly.
9395 Similarly, if we do not read the producer, we can not apply
9396 producer-specific interpretation. */
9397 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9398
9399 reader.keep ();
9400 }
9401
9402 /* Add a DIE to the delayed physname list. */
9403
9404 static void
9405 add_to_method_list (struct type *type, int fnfield_index, int index,
9406 const char *name, struct die_info *die,
9407 struct dwarf2_cu *cu)
9408 {
9409 struct delayed_method_info mi;
9410 mi.type = type;
9411 mi.fnfield_index = fnfield_index;
9412 mi.index = index;
9413 mi.name = name;
9414 mi.die = die;
9415 cu->method_list.push_back (mi);
9416 }
9417
9418 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9419 "const" / "volatile". If so, decrements LEN by the length of the
9420 modifier and return true. Otherwise return false. */
9421
9422 template<size_t N>
9423 static bool
9424 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9425 {
9426 size_t mod_len = sizeof (mod) - 1;
9427 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9428 {
9429 len -= mod_len;
9430 return true;
9431 }
9432 return false;
9433 }
9434
9435 /* Compute the physnames of any methods on the CU's method list.
9436
9437 The computation of method physnames is delayed in order to avoid the
9438 (bad) condition that one of the method's formal parameters is of an as yet
9439 incomplete type. */
9440
9441 static void
9442 compute_delayed_physnames (struct dwarf2_cu *cu)
9443 {
9444 /* Only C++ delays computing physnames. */
9445 if (cu->method_list.empty ())
9446 return;
9447 gdb_assert (cu->language == language_cplus);
9448
9449 for (const delayed_method_info &mi : cu->method_list)
9450 {
9451 const char *physname;
9452 struct fn_fieldlist *fn_flp
9453 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9454 physname = dwarf2_physname (mi.name, mi.die, cu);
9455 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9456 = physname ? physname : "";
9457
9458 /* Since there's no tag to indicate whether a method is a
9459 const/volatile overload, extract that information out of the
9460 demangled name. */
9461 if (physname != NULL)
9462 {
9463 size_t len = strlen (physname);
9464
9465 while (1)
9466 {
9467 if (physname[len] == ')') /* shortcut */
9468 break;
9469 else if (check_modifier (physname, len, " const"))
9470 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9471 else if (check_modifier (physname, len, " volatile"))
9472 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9473 else
9474 break;
9475 }
9476 }
9477 }
9478
9479 /* The list is no longer needed. */
9480 cu->method_list.clear ();
9481 }
9482
9483 /* Go objects should be embedded in a DW_TAG_module DIE,
9484 and it's not clear if/how imported objects will appear.
9485 To keep Go support simple until that's worked out,
9486 go back through what we've read and create something usable.
9487 We could do this while processing each DIE, and feels kinda cleaner,
9488 but that way is more invasive.
9489 This is to, for example, allow the user to type "p var" or "b main"
9490 without having to specify the package name, and allow lookups
9491 of module.object to work in contexts that use the expression
9492 parser. */
9493
9494 static void
9495 fixup_go_packaging (struct dwarf2_cu *cu)
9496 {
9497 gdb::unique_xmalloc_ptr<char> package_name;
9498 struct pending *list;
9499 int i;
9500
9501 for (list = *cu->get_builder ()->get_global_symbols ();
9502 list != NULL;
9503 list = list->next)
9504 {
9505 for (i = 0; i < list->nsyms; ++i)
9506 {
9507 struct symbol *sym = list->symbol[i];
9508
9509 if (sym->language () == language_go
9510 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9511 {
9512 gdb::unique_xmalloc_ptr<char> this_package_name
9513 (go_symbol_package_name (sym));
9514
9515 if (this_package_name == NULL)
9516 continue;
9517 if (package_name == NULL)
9518 package_name = std::move (this_package_name);
9519 else
9520 {
9521 struct objfile *objfile = cu->per_objfile->objfile;
9522 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9523 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9524 (symbol_symtab (sym) != NULL
9525 ? symtab_to_filename_for_display
9526 (symbol_symtab (sym))
9527 : objfile_name (objfile)),
9528 this_package_name.get (), package_name.get ());
9529 }
9530 }
9531 }
9532 }
9533
9534 if (package_name != NULL)
9535 {
9536 struct objfile *objfile = cu->per_objfile->objfile;
9537 const char *saved_package_name = objfile->intern (package_name.get ());
9538 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9539 saved_package_name);
9540 struct symbol *sym;
9541
9542 sym = new (&objfile->objfile_obstack) symbol;
9543 sym->set_language (language_go, &objfile->objfile_obstack);
9544 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9545 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9546 e.g., "main" finds the "main" module and not C's main(). */
9547 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9548 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9549 SYMBOL_TYPE (sym) = type;
9550
9551 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9552 }
9553 }
9554
9555 /* Allocate a fully-qualified name consisting of the two parts on the
9556 obstack. */
9557
9558 static const char *
9559 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9560 {
9561 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9562 }
9563
9564 /* A helper that allocates a variant part to attach to a Rust enum
9565 type. OBSTACK is where the results should be allocated. TYPE is
9566 the type we're processing. DISCRIMINANT_INDEX is the index of the
9567 discriminant. It must be the index of one of the fields of TYPE,
9568 or -1 to mean there is no discriminant (univariant enum).
9569 DEFAULT_INDEX is the index of the default field; or -1 if there is
9570 no default. RANGES is indexed by "effective" field number (the
9571 field index, but omitting the discriminant and default fields) and
9572 must hold the discriminant values used by the variants. Note that
9573 RANGES must have a lifetime at least as long as OBSTACK -- either
9574 already allocated on it, or static. */
9575
9576 static void
9577 alloc_rust_variant (struct obstack *obstack, struct type *type,
9578 int discriminant_index, int default_index,
9579 gdb::array_view<discriminant_range> ranges)
9580 {
9581 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9582 gdb_assert (discriminant_index == -1
9583 || (discriminant_index >= 0
9584 && discriminant_index < type->num_fields ()));
9585 gdb_assert (default_index == -1
9586 || (default_index >= 0 && default_index < type->num_fields ()));
9587
9588 /* We have one variant for each non-discriminant field. */
9589 int n_variants = type->num_fields ();
9590 if (discriminant_index != -1)
9591 --n_variants;
9592
9593 variant *variants = new (obstack) variant[n_variants];
9594 int var_idx = 0;
9595 int range_idx = 0;
9596 for (int i = 0; i < type->num_fields (); ++i)
9597 {
9598 if (i == discriminant_index)
9599 continue;
9600
9601 variants[var_idx].first_field = i;
9602 variants[var_idx].last_field = i + 1;
9603
9604 /* The default field does not need a range, but other fields do.
9605 We skipped the discriminant above. */
9606 if (i != default_index)
9607 {
9608 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9609 ++range_idx;
9610 }
9611
9612 ++var_idx;
9613 }
9614
9615 gdb_assert (range_idx == ranges.size ());
9616 gdb_assert (var_idx == n_variants);
9617
9618 variant_part *part = new (obstack) variant_part;
9619 part->discriminant_index = discriminant_index;
9620 /* If there is no discriminant, then whether it is signed is of no
9621 consequence. */
9622 part->is_unsigned
9623 = (discriminant_index == -1
9624 ? false
9625 : type->field (discriminant_index).type ()->is_unsigned ());
9626 part->variants = gdb::array_view<variant> (variants, n_variants);
9627
9628 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9629 gdb::array_view<variant_part> *prop_value
9630 = new (storage) gdb::array_view<variant_part> (part, 1);
9631
9632 struct dynamic_prop prop;
9633 prop.set_variant_parts (prop_value);
9634
9635 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9636 }
9637
9638 /* Some versions of rustc emitted enums in an unusual way.
9639
9640 Ordinary enums were emitted as unions. The first element of each
9641 structure in the union was named "RUST$ENUM$DISR". This element
9642 held the discriminant.
9643
9644 These versions of Rust also implemented the "non-zero"
9645 optimization. When the enum had two values, and one is empty and
9646 the other holds a pointer that cannot be zero, the pointer is used
9647 as the discriminant, with a zero value meaning the empty variant.
9648 Here, the union's first member is of the form
9649 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9650 where the fieldnos are the indices of the fields that should be
9651 traversed in order to find the field (which may be several fields deep)
9652 and the variantname is the name of the variant of the case when the
9653 field is zero.
9654
9655 This function recognizes whether TYPE is of one of these forms,
9656 and, if so, smashes it to be a variant type. */
9657
9658 static void
9659 quirk_rust_enum (struct type *type, struct objfile *objfile)
9660 {
9661 gdb_assert (type->code () == TYPE_CODE_UNION);
9662
9663 /* We don't need to deal with empty enums. */
9664 if (type->num_fields () == 0)
9665 return;
9666
9667 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9668 if (type->num_fields () == 1
9669 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9670 {
9671 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9672
9673 /* Decode the field name to find the offset of the
9674 discriminant. */
9675 ULONGEST bit_offset = 0;
9676 struct type *field_type = type->field (0).type ();
9677 while (name[0] >= '0' && name[0] <= '9')
9678 {
9679 char *tail;
9680 unsigned long index = strtoul (name, &tail, 10);
9681 name = tail;
9682 if (*name != '$'
9683 || index >= field_type->num_fields ()
9684 || (TYPE_FIELD_LOC_KIND (field_type, index)
9685 != FIELD_LOC_KIND_BITPOS))
9686 {
9687 complaint (_("Could not parse Rust enum encoding string \"%s\""
9688 "[in module %s]"),
9689 TYPE_FIELD_NAME (type, 0),
9690 objfile_name (objfile));
9691 return;
9692 }
9693 ++name;
9694
9695 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9696 field_type = field_type->field (index).type ();
9697 }
9698
9699 /* Smash this type to be a structure type. We have to do this
9700 because the type has already been recorded. */
9701 type->set_code (TYPE_CODE_STRUCT);
9702 type->set_num_fields (3);
9703 /* Save the field we care about. */
9704 struct field saved_field = type->field (0);
9705 type->set_fields
9706 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9707
9708 /* Put the discriminant at index 0. */
9709 type->field (0).set_type (field_type);
9710 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9711 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9712 SET_FIELD_BITPOS (type->field (0), bit_offset);
9713
9714 /* The order of fields doesn't really matter, so put the real
9715 field at index 1 and the data-less field at index 2. */
9716 type->field (1) = saved_field;
9717 TYPE_FIELD_NAME (type, 1)
9718 = rust_last_path_segment (type->field (1).type ()->name ());
9719 type->field (1).type ()->set_name
9720 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9721 TYPE_FIELD_NAME (type, 1)));
9722
9723 const char *dataless_name
9724 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9725 name);
9726 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9727 dataless_name);
9728 type->field (2).set_type (dataless_type);
9729 /* NAME points into the original discriminant name, which
9730 already has the correct lifetime. */
9731 TYPE_FIELD_NAME (type, 2) = name;
9732 SET_FIELD_BITPOS (type->field (2), 0);
9733
9734 /* Indicate that this is a variant type. */
9735 static discriminant_range ranges[1] = { { 0, 0 } };
9736 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9737 }
9738 /* A union with a single anonymous field is probably an old-style
9739 univariant enum. */
9740 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9741 {
9742 /* Smash this type to be a structure type. We have to do this
9743 because the type has already been recorded. */
9744 type->set_code (TYPE_CODE_STRUCT);
9745
9746 struct type *field_type = type->field (0).type ();
9747 const char *variant_name
9748 = rust_last_path_segment (field_type->name ());
9749 TYPE_FIELD_NAME (type, 0) = variant_name;
9750 field_type->set_name
9751 (rust_fully_qualify (&objfile->objfile_obstack,
9752 type->name (), variant_name));
9753
9754 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9755 }
9756 else
9757 {
9758 struct type *disr_type = nullptr;
9759 for (int i = 0; i < type->num_fields (); ++i)
9760 {
9761 disr_type = type->field (i).type ();
9762
9763 if (disr_type->code () != TYPE_CODE_STRUCT)
9764 {
9765 /* All fields of a true enum will be structs. */
9766 return;
9767 }
9768 else if (disr_type->num_fields () == 0)
9769 {
9770 /* Could be data-less variant, so keep going. */
9771 disr_type = nullptr;
9772 }
9773 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9774 "RUST$ENUM$DISR") != 0)
9775 {
9776 /* Not a Rust enum. */
9777 return;
9778 }
9779 else
9780 {
9781 /* Found one. */
9782 break;
9783 }
9784 }
9785
9786 /* If we got here without a discriminant, then it's probably
9787 just a union. */
9788 if (disr_type == nullptr)
9789 return;
9790
9791 /* Smash this type to be a structure type. We have to do this
9792 because the type has already been recorded. */
9793 type->set_code (TYPE_CODE_STRUCT);
9794
9795 /* Make space for the discriminant field. */
9796 struct field *disr_field = &disr_type->field (0);
9797 field *new_fields
9798 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9799 * sizeof (struct field)));
9800 memcpy (new_fields + 1, type->fields (),
9801 type->num_fields () * sizeof (struct field));
9802 type->set_fields (new_fields);
9803 type->set_num_fields (type->num_fields () + 1);
9804
9805 /* Install the discriminant at index 0 in the union. */
9806 type->field (0) = *disr_field;
9807 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9808 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9809
9810 /* We need a way to find the correct discriminant given a
9811 variant name. For convenience we build a map here. */
9812 struct type *enum_type = disr_field->type ();
9813 std::unordered_map<std::string, ULONGEST> discriminant_map;
9814 for (int i = 0; i < enum_type->num_fields (); ++i)
9815 {
9816 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9817 {
9818 const char *name
9819 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9820 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9821 }
9822 }
9823
9824 int n_fields = type->num_fields ();
9825 /* We don't need a range entry for the discriminant, but we do
9826 need one for every other field, as there is no default
9827 variant. */
9828 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9829 discriminant_range,
9830 n_fields - 1);
9831 /* Skip the discriminant here. */
9832 for (int i = 1; i < n_fields; ++i)
9833 {
9834 /* Find the final word in the name of this variant's type.
9835 That name can be used to look up the correct
9836 discriminant. */
9837 const char *variant_name
9838 = rust_last_path_segment (type->field (i).type ()->name ());
9839
9840 auto iter = discriminant_map.find (variant_name);
9841 if (iter != discriminant_map.end ())
9842 {
9843 ranges[i - 1].low = iter->second;
9844 ranges[i - 1].high = iter->second;
9845 }
9846
9847 /* In Rust, each element should have the size of the
9848 enclosing enum. */
9849 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9850
9851 /* Remove the discriminant field, if it exists. */
9852 struct type *sub_type = type->field (i).type ();
9853 if (sub_type->num_fields () > 0)
9854 {
9855 sub_type->set_num_fields (sub_type->num_fields () - 1);
9856 sub_type->set_fields (sub_type->fields () + 1);
9857 }
9858 TYPE_FIELD_NAME (type, i) = variant_name;
9859 sub_type->set_name
9860 (rust_fully_qualify (&objfile->objfile_obstack,
9861 type->name (), variant_name));
9862 }
9863
9864 /* Indicate that this is a variant type. */
9865 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9866 gdb::array_view<discriminant_range> (ranges,
9867 n_fields - 1));
9868 }
9869 }
9870
9871 /* Rewrite some Rust unions to be structures with variants parts. */
9872
9873 static void
9874 rust_union_quirks (struct dwarf2_cu *cu)
9875 {
9876 gdb_assert (cu->language == language_rust);
9877 for (type *type_ : cu->rust_unions)
9878 quirk_rust_enum (type_, cu->per_objfile->objfile);
9879 /* We don't need this any more. */
9880 cu->rust_unions.clear ();
9881 }
9882
9883 /* See read.h. */
9884
9885 type_unit_group_unshareable *
9886 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9887 {
9888 auto iter = this->m_type_units.find (tu_group);
9889 if (iter != this->m_type_units.end ())
9890 return iter->second.get ();
9891
9892 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9893 type_unit_group_unshareable *result = uniq.get ();
9894 this->m_type_units[tu_group] = std::move (uniq);
9895 return result;
9896 }
9897
9898 struct type *
9899 dwarf2_per_objfile::get_type_for_signatured_type
9900 (signatured_type *sig_type) const
9901 {
9902 auto iter = this->m_type_map.find (sig_type);
9903 if (iter == this->m_type_map.end ())
9904 return nullptr;
9905
9906 return iter->second;
9907 }
9908
9909 void dwarf2_per_objfile::set_type_for_signatured_type
9910 (signatured_type *sig_type, struct type *type)
9911 {
9912 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9913
9914 this->m_type_map[sig_type] = type;
9915 }
9916
9917 /* A helper function for computing the list of all symbol tables
9918 included by PER_CU. */
9919
9920 static void
9921 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9922 htab_t all_children, htab_t all_type_symtabs,
9923 dwarf2_per_cu_data *per_cu,
9924 dwarf2_per_objfile *per_objfile,
9925 struct compunit_symtab *immediate_parent)
9926 {
9927 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9928 if (*slot != NULL)
9929 {
9930 /* This inclusion and its children have been processed. */
9931 return;
9932 }
9933
9934 *slot = per_cu;
9935
9936 /* Only add a CU if it has a symbol table. */
9937 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9938 if (cust != NULL)
9939 {
9940 /* If this is a type unit only add its symbol table if we haven't
9941 seen it yet (type unit per_cu's can share symtabs). */
9942 if (per_cu->is_debug_types)
9943 {
9944 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9945 if (*slot == NULL)
9946 {
9947 *slot = cust;
9948 result->push_back (cust);
9949 if (cust->user == NULL)
9950 cust->user = immediate_parent;
9951 }
9952 }
9953 else
9954 {
9955 result->push_back (cust);
9956 if (cust->user == NULL)
9957 cust->user = immediate_parent;
9958 }
9959 }
9960
9961 if (!per_cu->imported_symtabs_empty ())
9962 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9963 {
9964 recursively_compute_inclusions (result, all_children,
9965 all_type_symtabs, ptr, per_objfile,
9966 cust);
9967 }
9968 }
9969
9970 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9971 PER_CU. */
9972
9973 static void
9974 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9975 dwarf2_per_objfile *per_objfile)
9976 {
9977 gdb_assert (! per_cu->is_debug_types);
9978
9979 if (!per_cu->imported_symtabs_empty ())
9980 {
9981 int len;
9982 std::vector<compunit_symtab *> result_symtabs;
9983 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9984
9985 /* If we don't have a symtab, we can just skip this case. */
9986 if (cust == NULL)
9987 return;
9988
9989 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9990 htab_eq_pointer,
9991 NULL, xcalloc, xfree));
9992 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9993 htab_eq_pointer,
9994 NULL, xcalloc, xfree));
9995
9996 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9997 {
9998 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9999 all_type_symtabs.get (), ptr,
10000 per_objfile, cust);
10001 }
10002
10003 /* Now we have a transitive closure of all the included symtabs. */
10004 len = result_symtabs.size ();
10005 cust->includes
10006 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
10007 struct compunit_symtab *, len + 1);
10008 memcpy (cust->includes, result_symtabs.data (),
10009 len * sizeof (compunit_symtab *));
10010 cust->includes[len] = NULL;
10011 }
10012 }
10013
10014 /* Compute the 'includes' field for the symtabs of all the CUs we just
10015 read. */
10016
10017 static void
10018 process_cu_includes (dwarf2_per_objfile *per_objfile)
10019 {
10020 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
10021 {
10022 if (! iter->is_debug_types)
10023 compute_compunit_symtab_includes (iter, per_objfile);
10024 }
10025
10026 per_objfile->per_bfd->just_read_cus.clear ();
10027 }
10028
10029 /* Generate full symbol information for CU, whose DIEs have
10030 already been loaded into memory. */
10031
10032 static void
10033 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10034 {
10035 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10036 struct objfile *objfile = per_objfile->objfile;
10037 struct gdbarch *gdbarch = objfile->arch ();
10038 CORE_ADDR lowpc, highpc;
10039 struct compunit_symtab *cust;
10040 CORE_ADDR baseaddr;
10041 struct block *static_block;
10042 CORE_ADDR addr;
10043
10044 baseaddr = objfile->text_section_offset ();
10045
10046 /* Clear the list here in case something was left over. */
10047 cu->method_list.clear ();
10048
10049 cu->language = pretend_language;
10050 cu->language_defn = language_def (cu->language);
10051
10052 dwarf2_find_base_address (cu->dies, cu);
10053
10054 /* Before we start reading the top-level DIE, ensure it has a valid tag
10055 type. */
10056 switch (cu->dies->tag)
10057 {
10058 case DW_TAG_compile_unit:
10059 case DW_TAG_partial_unit:
10060 case DW_TAG_type_unit:
10061 break;
10062 default:
10063 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
10064 dwarf_tag_name (cu->dies->tag),
10065 sect_offset_str (cu->per_cu->sect_off),
10066 objfile_name (per_objfile->objfile));
10067 }
10068
10069 /* Do line number decoding in read_file_scope () */
10070 process_die (cu->dies, cu);
10071
10072 /* For now fudge the Go package. */
10073 if (cu->language == language_go)
10074 fixup_go_packaging (cu);
10075
10076 /* Now that we have processed all the DIEs in the CU, all the types
10077 should be complete, and it should now be safe to compute all of the
10078 physnames. */
10079 compute_delayed_physnames (cu);
10080
10081 if (cu->language == language_rust)
10082 rust_union_quirks (cu);
10083
10084 /* Some compilers don't define a DW_AT_high_pc attribute for the
10085 compilation unit. If the DW_AT_high_pc is missing, synthesize
10086 it, by scanning the DIE's below the compilation unit. */
10087 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10088
10089 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10090 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10091
10092 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10093 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10094 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10095 addrmap to help ensure it has an accurate map of pc values belonging to
10096 this comp unit. */
10097 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10098
10099 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10100 SECT_OFF_TEXT (objfile),
10101 0);
10102
10103 if (cust != NULL)
10104 {
10105 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10106
10107 /* Set symtab language to language from DW_AT_language. If the
10108 compilation is from a C file generated by language preprocessors, do
10109 not set the language if it was already deduced by start_subfile. */
10110 if (!(cu->language == language_c
10111 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10112 COMPUNIT_FILETABS (cust)->language = cu->language;
10113
10114 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10115 produce DW_AT_location with location lists but it can be possibly
10116 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10117 there were bugs in prologue debug info, fixed later in GCC-4.5
10118 by "unwind info for epilogues" patch (which is not directly related).
10119
10120 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10121 needed, it would be wrong due to missing DW_AT_producer there.
10122
10123 Still one can confuse GDB by using non-standard GCC compilation
10124 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10125 */
10126 if (cu->has_loclist && gcc_4_minor >= 5)
10127 cust->locations_valid = 1;
10128
10129 if (gcc_4_minor >= 5)
10130 cust->epilogue_unwind_valid = 1;
10131
10132 cust->call_site_htab = cu->call_site_htab;
10133 }
10134
10135 per_objfile->set_symtab (cu->per_cu, cust);
10136
10137 /* Push it for inclusion processing later. */
10138 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10139
10140 /* Not needed any more. */
10141 cu->reset_builder ();
10142 }
10143
10144 /* Generate full symbol information for type unit CU, whose DIEs have
10145 already been loaded into memory. */
10146
10147 static void
10148 process_full_type_unit (dwarf2_cu *cu,
10149 enum language pretend_language)
10150 {
10151 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10152 struct objfile *objfile = per_objfile->objfile;
10153 struct compunit_symtab *cust;
10154 struct signatured_type *sig_type;
10155
10156 gdb_assert (cu->per_cu->is_debug_types);
10157 sig_type = (struct signatured_type *) cu->per_cu;
10158
10159 /* Clear the list here in case something was left over. */
10160 cu->method_list.clear ();
10161
10162 cu->language = pretend_language;
10163 cu->language_defn = language_def (cu->language);
10164
10165 /* The symbol tables are set up in read_type_unit_scope. */
10166 process_die (cu->dies, cu);
10167
10168 /* For now fudge the Go package. */
10169 if (cu->language == language_go)
10170 fixup_go_packaging (cu);
10171
10172 /* Now that we have processed all the DIEs in the CU, all the types
10173 should be complete, and it should now be safe to compute all of the
10174 physnames. */
10175 compute_delayed_physnames (cu);
10176
10177 if (cu->language == language_rust)
10178 rust_union_quirks (cu);
10179
10180 /* TUs share symbol tables.
10181 If this is the first TU to use this symtab, complete the construction
10182 of it with end_expandable_symtab. Otherwise, complete the addition of
10183 this TU's symbols to the existing symtab. */
10184 type_unit_group_unshareable *tug_unshare =
10185 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10186 if (tug_unshare->compunit_symtab == NULL)
10187 {
10188 buildsym_compunit *builder = cu->get_builder ();
10189 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10190 tug_unshare->compunit_symtab = cust;
10191
10192 if (cust != NULL)
10193 {
10194 /* Set symtab language to language from DW_AT_language. If the
10195 compilation is from a C file generated by language preprocessors,
10196 do not set the language if it was already deduced by
10197 start_subfile. */
10198 if (!(cu->language == language_c
10199 && COMPUNIT_FILETABS (cust)->language != language_c))
10200 COMPUNIT_FILETABS (cust)->language = cu->language;
10201 }
10202 }
10203 else
10204 {
10205 cu->get_builder ()->augment_type_symtab ();
10206 cust = tug_unshare->compunit_symtab;
10207 }
10208
10209 per_objfile->set_symtab (cu->per_cu, cust);
10210
10211 /* Not needed any more. */
10212 cu->reset_builder ();
10213 }
10214
10215 /* Process an imported unit DIE. */
10216
10217 static void
10218 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10219 {
10220 struct attribute *attr;
10221
10222 /* For now we don't handle imported units in type units. */
10223 if (cu->per_cu->is_debug_types)
10224 {
10225 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10226 " supported in type units [in module %s]"),
10227 objfile_name (cu->per_objfile->objfile));
10228 }
10229
10230 attr = dwarf2_attr (die, DW_AT_import, cu);
10231 if (attr != NULL)
10232 {
10233 sect_offset sect_off = attr->get_ref_die_offset ();
10234 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10235 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10236 dwarf2_per_cu_data *per_cu
10237 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10238
10239 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10240 into another compilation unit, at root level. Regard this as a hint,
10241 and ignore it. */
10242 if (die->parent && die->parent->parent == NULL
10243 && per_cu->unit_type == DW_UT_compile
10244 && per_cu->lang == language_cplus)
10245 return;
10246
10247 /* If necessary, add it to the queue and load its DIEs. */
10248 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10249 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10250 false, cu->language);
10251
10252 cu->per_cu->imported_symtabs_push (per_cu);
10253 }
10254 }
10255
10256 /* RAII object that represents a process_die scope: i.e.,
10257 starts/finishes processing a DIE. */
10258 class process_die_scope
10259 {
10260 public:
10261 process_die_scope (die_info *die, dwarf2_cu *cu)
10262 : m_die (die), m_cu (cu)
10263 {
10264 /* We should only be processing DIEs not already in process. */
10265 gdb_assert (!m_die->in_process);
10266 m_die->in_process = true;
10267 }
10268
10269 ~process_die_scope ()
10270 {
10271 m_die->in_process = false;
10272
10273 /* If we're done processing the DIE for the CU that owns the line
10274 header, we don't need the line header anymore. */
10275 if (m_cu->line_header_die_owner == m_die)
10276 {
10277 delete m_cu->line_header;
10278 m_cu->line_header = NULL;
10279 m_cu->line_header_die_owner = NULL;
10280 }
10281 }
10282
10283 private:
10284 die_info *m_die;
10285 dwarf2_cu *m_cu;
10286 };
10287
10288 /* Process a die and its children. */
10289
10290 static void
10291 process_die (struct die_info *die, struct dwarf2_cu *cu)
10292 {
10293 process_die_scope scope (die, cu);
10294
10295 switch (die->tag)
10296 {
10297 case DW_TAG_padding:
10298 break;
10299 case DW_TAG_compile_unit:
10300 case DW_TAG_partial_unit:
10301 read_file_scope (die, cu);
10302 break;
10303 case DW_TAG_type_unit:
10304 read_type_unit_scope (die, cu);
10305 break;
10306 case DW_TAG_subprogram:
10307 /* Nested subprograms in Fortran get a prefix. */
10308 if (cu->language == language_fortran
10309 && die->parent != NULL
10310 && die->parent->tag == DW_TAG_subprogram)
10311 cu->processing_has_namespace_info = true;
10312 /* Fall through. */
10313 case DW_TAG_inlined_subroutine:
10314 read_func_scope (die, cu);
10315 break;
10316 case DW_TAG_lexical_block:
10317 case DW_TAG_try_block:
10318 case DW_TAG_catch_block:
10319 read_lexical_block_scope (die, cu);
10320 break;
10321 case DW_TAG_call_site:
10322 case DW_TAG_GNU_call_site:
10323 read_call_site_scope (die, cu);
10324 break;
10325 case DW_TAG_class_type:
10326 case DW_TAG_interface_type:
10327 case DW_TAG_structure_type:
10328 case DW_TAG_union_type:
10329 process_structure_scope (die, cu);
10330 break;
10331 case DW_TAG_enumeration_type:
10332 process_enumeration_scope (die, cu);
10333 break;
10334
10335 /* These dies have a type, but processing them does not create
10336 a symbol or recurse to process the children. Therefore we can
10337 read them on-demand through read_type_die. */
10338 case DW_TAG_subroutine_type:
10339 case DW_TAG_set_type:
10340 case DW_TAG_pointer_type:
10341 case DW_TAG_ptr_to_member_type:
10342 case DW_TAG_reference_type:
10343 case DW_TAG_rvalue_reference_type:
10344 case DW_TAG_string_type:
10345 break;
10346
10347 case DW_TAG_array_type:
10348 /* We only need to handle this case for Ada -- in other
10349 languages, it's normal for the compiler to emit a typedef
10350 instead. */
10351 if (cu->language != language_ada)
10352 break;
10353 /* FALLTHROUGH */
10354 case DW_TAG_base_type:
10355 case DW_TAG_subrange_type:
10356 case DW_TAG_typedef:
10357 /* Add a typedef symbol for the type definition, if it has a
10358 DW_AT_name. */
10359 new_symbol (die, read_type_die (die, cu), cu);
10360 break;
10361 case DW_TAG_common_block:
10362 read_common_block (die, cu);
10363 break;
10364 case DW_TAG_common_inclusion:
10365 break;
10366 case DW_TAG_namespace:
10367 cu->processing_has_namespace_info = true;
10368 read_namespace (die, cu);
10369 break;
10370 case DW_TAG_module:
10371 cu->processing_has_namespace_info = true;
10372 read_module (die, cu);
10373 break;
10374 case DW_TAG_imported_declaration:
10375 cu->processing_has_namespace_info = true;
10376 if (read_namespace_alias (die, cu))
10377 break;
10378 /* The declaration is not a global namespace alias. */
10379 /* Fall through. */
10380 case DW_TAG_imported_module:
10381 cu->processing_has_namespace_info = true;
10382 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10383 || cu->language != language_fortran))
10384 complaint (_("Tag '%s' has unexpected children"),
10385 dwarf_tag_name (die->tag));
10386 read_import_statement (die, cu);
10387 break;
10388
10389 case DW_TAG_imported_unit:
10390 process_imported_unit_die (die, cu);
10391 break;
10392
10393 case DW_TAG_variable:
10394 read_variable (die, cu);
10395 break;
10396
10397 default:
10398 new_symbol (die, NULL, cu);
10399 break;
10400 }
10401 }
10402 \f
10403 /* DWARF name computation. */
10404
10405 /* A helper function for dwarf2_compute_name which determines whether DIE
10406 needs to have the name of the scope prepended to the name listed in the
10407 die. */
10408
10409 static int
10410 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10411 {
10412 struct attribute *attr;
10413
10414 switch (die->tag)
10415 {
10416 case DW_TAG_namespace:
10417 case DW_TAG_typedef:
10418 case DW_TAG_class_type:
10419 case DW_TAG_interface_type:
10420 case DW_TAG_structure_type:
10421 case DW_TAG_union_type:
10422 case DW_TAG_enumeration_type:
10423 case DW_TAG_enumerator:
10424 case DW_TAG_subprogram:
10425 case DW_TAG_inlined_subroutine:
10426 case DW_TAG_member:
10427 case DW_TAG_imported_declaration:
10428 return 1;
10429
10430 case DW_TAG_variable:
10431 case DW_TAG_constant:
10432 /* We only need to prefix "globally" visible variables. These include
10433 any variable marked with DW_AT_external or any variable that
10434 lives in a namespace. [Variables in anonymous namespaces
10435 require prefixing, but they are not DW_AT_external.] */
10436
10437 if (dwarf2_attr (die, DW_AT_specification, cu))
10438 {
10439 struct dwarf2_cu *spec_cu = cu;
10440
10441 return die_needs_namespace (die_specification (die, &spec_cu),
10442 spec_cu);
10443 }
10444
10445 attr = dwarf2_attr (die, DW_AT_external, cu);
10446 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10447 && die->parent->tag != DW_TAG_module)
10448 return 0;
10449 /* A variable in a lexical block of some kind does not need a
10450 namespace, even though in C++ such variables may be external
10451 and have a mangled name. */
10452 if (die->parent->tag == DW_TAG_lexical_block
10453 || die->parent->tag == DW_TAG_try_block
10454 || die->parent->tag == DW_TAG_catch_block
10455 || die->parent->tag == DW_TAG_subprogram)
10456 return 0;
10457 return 1;
10458
10459 default:
10460 return 0;
10461 }
10462 }
10463
10464 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10465 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10466 defined for the given DIE. */
10467
10468 static struct attribute *
10469 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10470 {
10471 struct attribute *attr;
10472
10473 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10474 if (attr == NULL)
10475 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10476
10477 return attr;
10478 }
10479
10480 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10481 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10482 defined for the given DIE. */
10483
10484 static const char *
10485 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10486 {
10487 const char *linkage_name;
10488
10489 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10490 if (linkage_name == NULL)
10491 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10492
10493 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10494 See https://github.com/rust-lang/rust/issues/32925. */
10495 if (cu->language == language_rust && linkage_name != NULL
10496 && strchr (linkage_name, '{') != NULL)
10497 linkage_name = NULL;
10498
10499 return linkage_name;
10500 }
10501
10502 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10503 compute the physname for the object, which include a method's:
10504 - formal parameters (C++),
10505 - receiver type (Go),
10506
10507 The term "physname" is a bit confusing.
10508 For C++, for example, it is the demangled name.
10509 For Go, for example, it's the mangled name.
10510
10511 For Ada, return the DIE's linkage name rather than the fully qualified
10512 name. PHYSNAME is ignored..
10513
10514 The result is allocated on the objfile->per_bfd's obstack and
10515 canonicalized. */
10516
10517 static const char *
10518 dwarf2_compute_name (const char *name,
10519 struct die_info *die, struct dwarf2_cu *cu,
10520 int physname)
10521 {
10522 struct objfile *objfile = cu->per_objfile->objfile;
10523
10524 if (name == NULL)
10525 name = dwarf2_name (die, cu);
10526
10527 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10528 but otherwise compute it by typename_concat inside GDB.
10529 FIXME: Actually this is not really true, or at least not always true.
10530 It's all very confusing. compute_and_set_names doesn't try to demangle
10531 Fortran names because there is no mangling standard. So new_symbol
10532 will set the demangled name to the result of dwarf2_full_name, and it is
10533 the demangled name that GDB uses if it exists. */
10534 if (cu->language == language_ada
10535 || (cu->language == language_fortran && physname))
10536 {
10537 /* For Ada unit, we prefer the linkage name over the name, as
10538 the former contains the exported name, which the user expects
10539 to be able to reference. Ideally, we want the user to be able
10540 to reference this entity using either natural or linkage name,
10541 but we haven't started looking at this enhancement yet. */
10542 const char *linkage_name = dw2_linkage_name (die, cu);
10543
10544 if (linkage_name != NULL)
10545 return linkage_name;
10546 }
10547
10548 /* These are the only languages we know how to qualify names in. */
10549 if (name != NULL
10550 && (cu->language == language_cplus
10551 || cu->language == language_fortran || cu->language == language_d
10552 || cu->language == language_rust))
10553 {
10554 if (die_needs_namespace (die, cu))
10555 {
10556 const char *prefix;
10557 const char *canonical_name = NULL;
10558
10559 string_file buf;
10560
10561 prefix = determine_prefix (die, cu);
10562 if (*prefix != '\0')
10563 {
10564 gdb::unique_xmalloc_ptr<char> prefixed_name
10565 (typename_concat (NULL, prefix, name, physname, cu));
10566
10567 buf.puts (prefixed_name.get ());
10568 }
10569 else
10570 buf.puts (name);
10571
10572 /* Template parameters may be specified in the DIE's DW_AT_name, or
10573 as children with DW_TAG_template_type_param or
10574 DW_TAG_value_type_param. If the latter, add them to the name
10575 here. If the name already has template parameters, then
10576 skip this step; some versions of GCC emit both, and
10577 it is more efficient to use the pre-computed name.
10578
10579 Something to keep in mind about this process: it is very
10580 unlikely, or in some cases downright impossible, to produce
10581 something that will match the mangled name of a function.
10582 If the definition of the function has the same debug info,
10583 we should be able to match up with it anyway. But fallbacks
10584 using the minimal symbol, for instance to find a method
10585 implemented in a stripped copy of libstdc++, will not work.
10586 If we do not have debug info for the definition, we will have to
10587 match them up some other way.
10588
10589 When we do name matching there is a related problem with function
10590 templates; two instantiated function templates are allowed to
10591 differ only by their return types, which we do not add here. */
10592
10593 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10594 {
10595 struct attribute *attr;
10596 struct die_info *child;
10597 int first = 1;
10598 const language_defn *cplus_lang = language_def (cu->language);
10599
10600 die->building_fullname = 1;
10601
10602 for (child = die->child; child != NULL; child = child->sibling)
10603 {
10604 struct type *type;
10605 LONGEST value;
10606 const gdb_byte *bytes;
10607 struct dwarf2_locexpr_baton *baton;
10608 struct value *v;
10609
10610 if (child->tag != DW_TAG_template_type_param
10611 && child->tag != DW_TAG_template_value_param)
10612 continue;
10613
10614 if (first)
10615 {
10616 buf.puts ("<");
10617 first = 0;
10618 }
10619 else
10620 buf.puts (", ");
10621
10622 attr = dwarf2_attr (child, DW_AT_type, cu);
10623 if (attr == NULL)
10624 {
10625 complaint (_("template parameter missing DW_AT_type"));
10626 buf.puts ("UNKNOWN_TYPE");
10627 continue;
10628 }
10629 type = die_type (child, cu);
10630
10631 if (child->tag == DW_TAG_template_type_param)
10632 {
10633 cplus_lang->print_type (type, "", &buf, -1, 0,
10634 &type_print_raw_options);
10635 continue;
10636 }
10637
10638 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10639 if (attr == NULL)
10640 {
10641 complaint (_("template parameter missing "
10642 "DW_AT_const_value"));
10643 buf.puts ("UNKNOWN_VALUE");
10644 continue;
10645 }
10646
10647 dwarf2_const_value_attr (attr, type, name,
10648 &cu->comp_unit_obstack, cu,
10649 &value, &bytes, &baton);
10650
10651 if (type->has_no_signedness ())
10652 /* GDB prints characters as NUMBER 'CHAR'. If that's
10653 changed, this can use value_print instead. */
10654 cplus_lang->printchar (value, type, &buf);
10655 else
10656 {
10657 struct value_print_options opts;
10658
10659 if (baton != NULL)
10660 v = dwarf2_evaluate_loc_desc (type, NULL,
10661 baton->data,
10662 baton->size,
10663 baton->per_cu,
10664 baton->per_objfile);
10665 else if (bytes != NULL)
10666 {
10667 v = allocate_value (type);
10668 memcpy (value_contents_writeable (v), bytes,
10669 TYPE_LENGTH (type));
10670 }
10671 else
10672 v = value_from_longest (type, value);
10673
10674 /* Specify decimal so that we do not depend on
10675 the radix. */
10676 get_formatted_print_options (&opts, 'd');
10677 opts.raw = 1;
10678 value_print (v, &buf, &opts);
10679 release_value (v);
10680 }
10681 }
10682
10683 die->building_fullname = 0;
10684
10685 if (!first)
10686 {
10687 /* Close the argument list, with a space if necessary
10688 (nested templates). */
10689 if (!buf.empty () && buf.string ().back () == '>')
10690 buf.puts (" >");
10691 else
10692 buf.puts (">");
10693 }
10694 }
10695
10696 /* For C++ methods, append formal parameter type
10697 information, if PHYSNAME. */
10698
10699 if (physname && die->tag == DW_TAG_subprogram
10700 && cu->language == language_cplus)
10701 {
10702 struct type *type = read_type_die (die, cu);
10703
10704 c_type_print_args (type, &buf, 1, cu->language,
10705 &type_print_raw_options);
10706
10707 if (cu->language == language_cplus)
10708 {
10709 /* Assume that an artificial first parameter is
10710 "this", but do not crash if it is not. RealView
10711 marks unnamed (and thus unused) parameters as
10712 artificial; there is no way to differentiate
10713 the two cases. */
10714 if (type->num_fields () > 0
10715 && TYPE_FIELD_ARTIFICIAL (type, 0)
10716 && type->field (0).type ()->code () == TYPE_CODE_PTR
10717 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10718 buf.puts (" const");
10719 }
10720 }
10721
10722 const std::string &intermediate_name = buf.string ();
10723
10724 if (cu->language == language_cplus)
10725 canonical_name
10726 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10727 objfile);
10728
10729 /* If we only computed INTERMEDIATE_NAME, or if
10730 INTERMEDIATE_NAME is already canonical, then we need to
10731 intern it. */
10732 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10733 name = objfile->intern (intermediate_name);
10734 else
10735 name = canonical_name;
10736 }
10737 }
10738
10739 return name;
10740 }
10741
10742 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10743 If scope qualifiers are appropriate they will be added. The result
10744 will be allocated on the storage_obstack, or NULL if the DIE does
10745 not have a name. NAME may either be from a previous call to
10746 dwarf2_name or NULL.
10747
10748 The output string will be canonicalized (if C++). */
10749
10750 static const char *
10751 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10752 {
10753 return dwarf2_compute_name (name, die, cu, 0);
10754 }
10755
10756 /* Construct a physname for the given DIE in CU. NAME may either be
10757 from a previous call to dwarf2_name or NULL. The result will be
10758 allocated on the objfile_objstack or NULL if the DIE does not have a
10759 name.
10760
10761 The output string will be canonicalized (if C++). */
10762
10763 static const char *
10764 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10765 {
10766 struct objfile *objfile = cu->per_objfile->objfile;
10767 const char *retval, *mangled = NULL, *canon = NULL;
10768 int need_copy = 1;
10769
10770 /* In this case dwarf2_compute_name is just a shortcut not building anything
10771 on its own. */
10772 if (!die_needs_namespace (die, cu))
10773 return dwarf2_compute_name (name, die, cu, 1);
10774
10775 if (cu->language != language_rust)
10776 mangled = dw2_linkage_name (die, cu);
10777
10778 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10779 has computed. */
10780 gdb::unique_xmalloc_ptr<char> demangled;
10781 if (mangled != NULL)
10782 {
10783
10784 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10785 {
10786 /* Do nothing (do not demangle the symbol name). */
10787 }
10788 else
10789 {
10790 /* Use DMGL_RET_DROP for C++ template functions to suppress
10791 their return type. It is easier for GDB users to search
10792 for such functions as `name(params)' than `long name(params)'.
10793 In such case the minimal symbol names do not match the full
10794 symbol names but for template functions there is never a need
10795 to look up their definition from their declaration so
10796 the only disadvantage remains the minimal symbol variant
10797 `long name(params)' does not have the proper inferior type. */
10798 demangled.reset (gdb_demangle (mangled,
10799 (DMGL_PARAMS | DMGL_ANSI
10800 | DMGL_RET_DROP)));
10801 }
10802 if (demangled)
10803 canon = demangled.get ();
10804 else
10805 {
10806 canon = mangled;
10807 need_copy = 0;
10808 }
10809 }
10810
10811 if (canon == NULL || check_physname)
10812 {
10813 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10814
10815 if (canon != NULL && strcmp (physname, canon) != 0)
10816 {
10817 /* It may not mean a bug in GDB. The compiler could also
10818 compute DW_AT_linkage_name incorrectly. But in such case
10819 GDB would need to be bug-to-bug compatible. */
10820
10821 complaint (_("Computed physname <%s> does not match demangled <%s> "
10822 "(from linkage <%s>) - DIE at %s [in module %s]"),
10823 physname, canon, mangled, sect_offset_str (die->sect_off),
10824 objfile_name (objfile));
10825
10826 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10827 is available here - over computed PHYSNAME. It is safer
10828 against both buggy GDB and buggy compilers. */
10829
10830 retval = canon;
10831 }
10832 else
10833 {
10834 retval = physname;
10835 need_copy = 0;
10836 }
10837 }
10838 else
10839 retval = canon;
10840
10841 if (need_copy)
10842 retval = objfile->intern (retval);
10843
10844 return retval;
10845 }
10846
10847 /* Inspect DIE in CU for a namespace alias. If one exists, record
10848 a new symbol for it.
10849
10850 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10851
10852 static int
10853 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10854 {
10855 struct attribute *attr;
10856
10857 /* If the die does not have a name, this is not a namespace
10858 alias. */
10859 attr = dwarf2_attr (die, DW_AT_name, cu);
10860 if (attr != NULL)
10861 {
10862 int num;
10863 struct die_info *d = die;
10864 struct dwarf2_cu *imported_cu = cu;
10865
10866 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10867 keep inspecting DIEs until we hit the underlying import. */
10868 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10869 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10870 {
10871 attr = dwarf2_attr (d, DW_AT_import, cu);
10872 if (attr == NULL)
10873 break;
10874
10875 d = follow_die_ref (d, attr, &imported_cu);
10876 if (d->tag != DW_TAG_imported_declaration)
10877 break;
10878 }
10879
10880 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10881 {
10882 complaint (_("DIE at %s has too many recursively imported "
10883 "declarations"), sect_offset_str (d->sect_off));
10884 return 0;
10885 }
10886
10887 if (attr != NULL)
10888 {
10889 struct type *type;
10890 sect_offset sect_off = attr->get_ref_die_offset ();
10891
10892 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10893 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10894 {
10895 /* This declaration is a global namespace alias. Add
10896 a symbol for it whose type is the aliased namespace. */
10897 new_symbol (die, type, cu);
10898 return 1;
10899 }
10900 }
10901 }
10902
10903 return 0;
10904 }
10905
10906 /* Return the using directives repository (global or local?) to use in the
10907 current context for CU.
10908
10909 For Ada, imported declarations can materialize renamings, which *may* be
10910 global. However it is impossible (for now?) in DWARF to distinguish
10911 "external" imported declarations and "static" ones. As all imported
10912 declarations seem to be static in all other languages, make them all CU-wide
10913 global only in Ada. */
10914
10915 static struct using_direct **
10916 using_directives (struct dwarf2_cu *cu)
10917 {
10918 if (cu->language == language_ada
10919 && cu->get_builder ()->outermost_context_p ())
10920 return cu->get_builder ()->get_global_using_directives ();
10921 else
10922 return cu->get_builder ()->get_local_using_directives ();
10923 }
10924
10925 /* Read the import statement specified by the given die and record it. */
10926
10927 static void
10928 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10929 {
10930 struct objfile *objfile = cu->per_objfile->objfile;
10931 struct attribute *import_attr;
10932 struct die_info *imported_die, *child_die;
10933 struct dwarf2_cu *imported_cu;
10934 const char *imported_name;
10935 const char *imported_name_prefix;
10936 const char *canonical_name;
10937 const char *import_alias;
10938 const char *imported_declaration = NULL;
10939 const char *import_prefix;
10940 std::vector<const char *> excludes;
10941
10942 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10943 if (import_attr == NULL)
10944 {
10945 complaint (_("Tag '%s' has no DW_AT_import"),
10946 dwarf_tag_name (die->tag));
10947 return;
10948 }
10949
10950 imported_cu = cu;
10951 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10952 imported_name = dwarf2_name (imported_die, imported_cu);
10953 if (imported_name == NULL)
10954 {
10955 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10956
10957 The import in the following code:
10958 namespace A
10959 {
10960 typedef int B;
10961 }
10962
10963 int main ()
10964 {
10965 using A::B;
10966 B b;
10967 return b;
10968 }
10969
10970 ...
10971 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10972 <52> DW_AT_decl_file : 1
10973 <53> DW_AT_decl_line : 6
10974 <54> DW_AT_import : <0x75>
10975 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10976 <59> DW_AT_name : B
10977 <5b> DW_AT_decl_file : 1
10978 <5c> DW_AT_decl_line : 2
10979 <5d> DW_AT_type : <0x6e>
10980 ...
10981 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10982 <76> DW_AT_byte_size : 4
10983 <77> DW_AT_encoding : 5 (signed)
10984
10985 imports the wrong die ( 0x75 instead of 0x58 ).
10986 This case will be ignored until the gcc bug is fixed. */
10987 return;
10988 }
10989
10990 /* Figure out the local name after import. */
10991 import_alias = dwarf2_name (die, cu);
10992
10993 /* Figure out where the statement is being imported to. */
10994 import_prefix = determine_prefix (die, cu);
10995
10996 /* Figure out what the scope of the imported die is and prepend it
10997 to the name of the imported die. */
10998 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10999
11000 if (imported_die->tag != DW_TAG_namespace
11001 && imported_die->tag != DW_TAG_module)
11002 {
11003 imported_declaration = imported_name;
11004 canonical_name = imported_name_prefix;
11005 }
11006 else if (strlen (imported_name_prefix) > 0)
11007 canonical_name = obconcat (&objfile->objfile_obstack,
11008 imported_name_prefix,
11009 (cu->language == language_d ? "." : "::"),
11010 imported_name, (char *) NULL);
11011 else
11012 canonical_name = imported_name;
11013
11014 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11015 for (child_die = die->child; child_die && child_die->tag;
11016 child_die = child_die->sibling)
11017 {
11018 /* DWARF-4: A Fortran use statement with a “rename list” may be
11019 represented by an imported module entry with an import attribute
11020 referring to the module and owned entries corresponding to those
11021 entities that are renamed as part of being imported. */
11022
11023 if (child_die->tag != DW_TAG_imported_declaration)
11024 {
11025 complaint (_("child DW_TAG_imported_declaration expected "
11026 "- DIE at %s [in module %s]"),
11027 sect_offset_str (child_die->sect_off),
11028 objfile_name (objfile));
11029 continue;
11030 }
11031
11032 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11033 if (import_attr == NULL)
11034 {
11035 complaint (_("Tag '%s' has no DW_AT_import"),
11036 dwarf_tag_name (child_die->tag));
11037 continue;
11038 }
11039
11040 imported_cu = cu;
11041 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11042 &imported_cu);
11043 imported_name = dwarf2_name (imported_die, imported_cu);
11044 if (imported_name == NULL)
11045 {
11046 complaint (_("child DW_TAG_imported_declaration has unknown "
11047 "imported name - DIE at %s [in module %s]"),
11048 sect_offset_str (child_die->sect_off),
11049 objfile_name (objfile));
11050 continue;
11051 }
11052
11053 excludes.push_back (imported_name);
11054
11055 process_die (child_die, cu);
11056 }
11057
11058 add_using_directive (using_directives (cu),
11059 import_prefix,
11060 canonical_name,
11061 import_alias,
11062 imported_declaration,
11063 excludes,
11064 0,
11065 &objfile->objfile_obstack);
11066 }
11067
11068 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11069 types, but gives them a size of zero. Starting with version 14,
11070 ICC is compatible with GCC. */
11071
11072 static bool
11073 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11074 {
11075 if (!cu->checked_producer)
11076 check_producer (cu);
11077
11078 return cu->producer_is_icc_lt_14;
11079 }
11080
11081 /* ICC generates a DW_AT_type for C void functions. This was observed on
11082 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11083 which says that void functions should not have a DW_AT_type. */
11084
11085 static bool
11086 producer_is_icc (struct dwarf2_cu *cu)
11087 {
11088 if (!cu->checked_producer)
11089 check_producer (cu);
11090
11091 return cu->producer_is_icc;
11092 }
11093
11094 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11095 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11096 this, it was first present in GCC release 4.3.0. */
11097
11098 static bool
11099 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11100 {
11101 if (!cu->checked_producer)
11102 check_producer (cu);
11103
11104 return cu->producer_is_gcc_lt_4_3;
11105 }
11106
11107 static file_and_directory
11108 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11109 {
11110 file_and_directory res;
11111
11112 /* Find the filename. Do not use dwarf2_name here, since the filename
11113 is not a source language identifier. */
11114 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11115 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11116
11117 if (res.comp_dir == NULL
11118 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11119 && IS_ABSOLUTE_PATH (res.name))
11120 {
11121 res.comp_dir_storage = ldirname (res.name);
11122 if (!res.comp_dir_storage.empty ())
11123 res.comp_dir = res.comp_dir_storage.c_str ();
11124 }
11125 if (res.comp_dir != NULL)
11126 {
11127 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11128 directory, get rid of it. */
11129 const char *cp = strchr (res.comp_dir, ':');
11130
11131 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11132 res.comp_dir = cp + 1;
11133 }
11134
11135 if (res.name == NULL)
11136 res.name = "<unknown>";
11137
11138 return res;
11139 }
11140
11141 /* Handle DW_AT_stmt_list for a compilation unit.
11142 DIE is the DW_TAG_compile_unit die for CU.
11143 COMP_DIR is the compilation directory. LOWPC is passed to
11144 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11145
11146 static void
11147 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11148 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11149 {
11150 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11151 struct attribute *attr;
11152 struct line_header line_header_local;
11153 hashval_t line_header_local_hash;
11154 void **slot;
11155 int decode_mapping;
11156
11157 gdb_assert (! cu->per_cu->is_debug_types);
11158
11159 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11160 if (attr == NULL || !attr->form_is_unsigned ())
11161 return;
11162
11163 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11164
11165 /* The line header hash table is only created if needed (it exists to
11166 prevent redundant reading of the line table for partial_units).
11167 If we're given a partial_unit, we'll need it. If we're given a
11168 compile_unit, then use the line header hash table if it's already
11169 created, but don't create one just yet. */
11170
11171 if (per_objfile->line_header_hash == NULL
11172 && die->tag == DW_TAG_partial_unit)
11173 {
11174 per_objfile->line_header_hash
11175 .reset (htab_create_alloc (127, line_header_hash_voidp,
11176 line_header_eq_voidp,
11177 free_line_header_voidp,
11178 xcalloc, xfree));
11179 }
11180
11181 line_header_local.sect_off = line_offset;
11182 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11183 line_header_local_hash = line_header_hash (&line_header_local);
11184 if (per_objfile->line_header_hash != NULL)
11185 {
11186 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11187 &line_header_local,
11188 line_header_local_hash, NO_INSERT);
11189
11190 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11191 is not present in *SLOT (since if there is something in *SLOT then
11192 it will be for a partial_unit). */
11193 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11194 {
11195 gdb_assert (*slot != NULL);
11196 cu->line_header = (struct line_header *) *slot;
11197 return;
11198 }
11199 }
11200
11201 /* dwarf_decode_line_header does not yet provide sufficient information.
11202 We always have to call also dwarf_decode_lines for it. */
11203 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11204 if (lh == NULL)
11205 return;
11206
11207 cu->line_header = lh.release ();
11208 cu->line_header_die_owner = die;
11209
11210 if (per_objfile->line_header_hash == NULL)
11211 slot = NULL;
11212 else
11213 {
11214 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11215 &line_header_local,
11216 line_header_local_hash, INSERT);
11217 gdb_assert (slot != NULL);
11218 }
11219 if (slot != NULL && *slot == NULL)
11220 {
11221 /* This newly decoded line number information unit will be owned
11222 by line_header_hash hash table. */
11223 *slot = cu->line_header;
11224 cu->line_header_die_owner = NULL;
11225 }
11226 else
11227 {
11228 /* We cannot free any current entry in (*slot) as that struct line_header
11229 may be already used by multiple CUs. Create only temporary decoded
11230 line_header for this CU - it may happen at most once for each line
11231 number information unit. And if we're not using line_header_hash
11232 then this is what we want as well. */
11233 gdb_assert (die->tag != DW_TAG_partial_unit);
11234 }
11235 decode_mapping = (die->tag != DW_TAG_partial_unit);
11236 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11237 decode_mapping);
11238
11239 }
11240
11241 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11242
11243 static void
11244 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11245 {
11246 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11247 struct objfile *objfile = per_objfile->objfile;
11248 struct gdbarch *gdbarch = objfile->arch ();
11249 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11250 CORE_ADDR highpc = ((CORE_ADDR) 0);
11251 struct attribute *attr;
11252 struct die_info *child_die;
11253 CORE_ADDR baseaddr;
11254
11255 prepare_one_comp_unit (cu, die, cu->language);
11256 baseaddr = objfile->text_section_offset ();
11257
11258 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11259
11260 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11261 from finish_block. */
11262 if (lowpc == ((CORE_ADDR) -1))
11263 lowpc = highpc;
11264 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11265
11266 file_and_directory fnd = find_file_and_directory (die, cu);
11267
11268 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11269 standardised yet. As a workaround for the language detection we fall
11270 back to the DW_AT_producer string. */
11271 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11272 cu->language = language_opencl;
11273
11274 /* Similar hack for Go. */
11275 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11276 set_cu_language (DW_LANG_Go, cu);
11277
11278 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11279
11280 /* Decode line number information if present. We do this before
11281 processing child DIEs, so that the line header table is available
11282 for DW_AT_decl_file. */
11283 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11284
11285 /* Process all dies in compilation unit. */
11286 if (die->child != NULL)
11287 {
11288 child_die = die->child;
11289 while (child_die && child_die->tag)
11290 {
11291 process_die (child_die, cu);
11292 child_die = child_die->sibling;
11293 }
11294 }
11295
11296 /* Decode macro information, if present. Dwarf 2 macro information
11297 refers to information in the line number info statement program
11298 header, so we can only read it if we've read the header
11299 successfully. */
11300 attr = dwarf2_attr (die, DW_AT_macros, cu);
11301 if (attr == NULL)
11302 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11303 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11304 {
11305 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11306 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11307
11308 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11309 }
11310 else
11311 {
11312 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11313 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11314 {
11315 unsigned int macro_offset = attr->as_unsigned ();
11316
11317 dwarf_decode_macros (cu, macro_offset, 0);
11318 }
11319 }
11320 }
11321
11322 void
11323 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11324 {
11325 struct type_unit_group *tu_group;
11326 int first_time;
11327 struct attribute *attr;
11328 unsigned int i;
11329 struct signatured_type *sig_type;
11330
11331 gdb_assert (per_cu->is_debug_types);
11332 sig_type = (struct signatured_type *) per_cu;
11333
11334 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11335
11336 /* If we're using .gdb_index (includes -readnow) then
11337 per_cu->type_unit_group may not have been set up yet. */
11338 if (sig_type->type_unit_group == NULL)
11339 sig_type->type_unit_group = get_type_unit_group (this, attr);
11340 tu_group = sig_type->type_unit_group;
11341
11342 /* If we've already processed this stmt_list there's no real need to
11343 do it again, we could fake it and just recreate the part we need
11344 (file name,index -> symtab mapping). If data shows this optimization
11345 is useful we can do it then. */
11346 type_unit_group_unshareable *tug_unshare
11347 = per_objfile->get_type_unit_group_unshareable (tu_group);
11348 first_time = tug_unshare->compunit_symtab == NULL;
11349
11350 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11351 debug info. */
11352 line_header_up lh;
11353 if (attr != NULL && attr->form_is_unsigned ())
11354 {
11355 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11356 lh = dwarf_decode_line_header (line_offset, this);
11357 }
11358 if (lh == NULL)
11359 {
11360 if (first_time)
11361 start_symtab ("", NULL, 0);
11362 else
11363 {
11364 gdb_assert (tug_unshare->symtabs == NULL);
11365 gdb_assert (m_builder == nullptr);
11366 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11367 m_builder.reset (new struct buildsym_compunit
11368 (COMPUNIT_OBJFILE (cust), "",
11369 COMPUNIT_DIRNAME (cust),
11370 compunit_language (cust),
11371 0, cust));
11372 list_in_scope = get_builder ()->get_file_symbols ();
11373 }
11374 return;
11375 }
11376
11377 line_header = lh.release ();
11378 line_header_die_owner = die;
11379
11380 if (first_time)
11381 {
11382 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11383
11384 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11385 still initializing it, and our caller (a few levels up)
11386 process_full_type_unit still needs to know if this is the first
11387 time. */
11388
11389 tug_unshare->symtabs
11390 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11391 struct symtab *, line_header->file_names_size ());
11392
11393 auto &file_names = line_header->file_names ();
11394 for (i = 0; i < file_names.size (); ++i)
11395 {
11396 file_entry &fe = file_names[i];
11397 dwarf2_start_subfile (this, fe.name,
11398 fe.include_dir (line_header));
11399 buildsym_compunit *b = get_builder ();
11400 if (b->get_current_subfile ()->symtab == NULL)
11401 {
11402 /* NOTE: start_subfile will recognize when it's been
11403 passed a file it has already seen. So we can't
11404 assume there's a simple mapping from
11405 cu->line_header->file_names to subfiles, plus
11406 cu->line_header->file_names may contain dups. */
11407 b->get_current_subfile ()->symtab
11408 = allocate_symtab (cust, b->get_current_subfile ()->name);
11409 }
11410
11411 fe.symtab = b->get_current_subfile ()->symtab;
11412 tug_unshare->symtabs[i] = fe.symtab;
11413 }
11414 }
11415 else
11416 {
11417 gdb_assert (m_builder == nullptr);
11418 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11419 m_builder.reset (new struct buildsym_compunit
11420 (COMPUNIT_OBJFILE (cust), "",
11421 COMPUNIT_DIRNAME (cust),
11422 compunit_language (cust),
11423 0, cust));
11424 list_in_scope = get_builder ()->get_file_symbols ();
11425
11426 auto &file_names = line_header->file_names ();
11427 for (i = 0; i < file_names.size (); ++i)
11428 {
11429 file_entry &fe = file_names[i];
11430 fe.symtab = tug_unshare->symtabs[i];
11431 }
11432 }
11433
11434 /* The main symtab is allocated last. Type units don't have DW_AT_name
11435 so they don't have a "real" (so to speak) symtab anyway.
11436 There is later code that will assign the main symtab to all symbols
11437 that don't have one. We need to handle the case of a symbol with a
11438 missing symtab (DW_AT_decl_file) anyway. */
11439 }
11440
11441 /* Process DW_TAG_type_unit.
11442 For TUs we want to skip the first top level sibling if it's not the
11443 actual type being defined by this TU. In this case the first top
11444 level sibling is there to provide context only. */
11445
11446 static void
11447 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11448 {
11449 struct die_info *child_die;
11450
11451 prepare_one_comp_unit (cu, die, language_minimal);
11452
11453 /* Initialize (or reinitialize) the machinery for building symtabs.
11454 We do this before processing child DIEs, so that the line header table
11455 is available for DW_AT_decl_file. */
11456 cu->setup_type_unit_groups (die);
11457
11458 if (die->child != NULL)
11459 {
11460 child_die = die->child;
11461 while (child_die && child_die->tag)
11462 {
11463 process_die (child_die, cu);
11464 child_die = child_die->sibling;
11465 }
11466 }
11467 }
11468 \f
11469 /* DWO/DWP files.
11470
11471 http://gcc.gnu.org/wiki/DebugFission
11472 http://gcc.gnu.org/wiki/DebugFissionDWP
11473
11474 To simplify handling of both DWO files ("object" files with the DWARF info)
11475 and DWP files (a file with the DWOs packaged up into one file), we treat
11476 DWP files as having a collection of virtual DWO files. */
11477
11478 static hashval_t
11479 hash_dwo_file (const void *item)
11480 {
11481 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11482 hashval_t hash;
11483
11484 hash = htab_hash_string (dwo_file->dwo_name);
11485 if (dwo_file->comp_dir != NULL)
11486 hash += htab_hash_string (dwo_file->comp_dir);
11487 return hash;
11488 }
11489
11490 static int
11491 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11492 {
11493 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11494 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11495
11496 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11497 return 0;
11498 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11499 return lhs->comp_dir == rhs->comp_dir;
11500 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11501 }
11502
11503 /* Allocate a hash table for DWO files. */
11504
11505 static htab_up
11506 allocate_dwo_file_hash_table ()
11507 {
11508 auto delete_dwo_file = [] (void *item)
11509 {
11510 struct dwo_file *dwo_file = (struct dwo_file *) item;
11511
11512 delete dwo_file;
11513 };
11514
11515 return htab_up (htab_create_alloc (41,
11516 hash_dwo_file,
11517 eq_dwo_file,
11518 delete_dwo_file,
11519 xcalloc, xfree));
11520 }
11521
11522 /* Lookup DWO file DWO_NAME. */
11523
11524 static void **
11525 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11526 const char *dwo_name,
11527 const char *comp_dir)
11528 {
11529 struct dwo_file find_entry;
11530 void **slot;
11531
11532 if (per_objfile->per_bfd->dwo_files == NULL)
11533 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11534
11535 find_entry.dwo_name = dwo_name;
11536 find_entry.comp_dir = comp_dir;
11537 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11538 INSERT);
11539
11540 return slot;
11541 }
11542
11543 static hashval_t
11544 hash_dwo_unit (const void *item)
11545 {
11546 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11547
11548 /* This drops the top 32 bits of the id, but is ok for a hash. */
11549 return dwo_unit->signature;
11550 }
11551
11552 static int
11553 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11554 {
11555 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11556 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11557
11558 /* The signature is assumed to be unique within the DWO file.
11559 So while object file CU dwo_id's always have the value zero,
11560 that's OK, assuming each object file DWO file has only one CU,
11561 and that's the rule for now. */
11562 return lhs->signature == rhs->signature;
11563 }
11564
11565 /* Allocate a hash table for DWO CUs,TUs.
11566 There is one of these tables for each of CUs,TUs for each DWO file. */
11567
11568 static htab_up
11569 allocate_dwo_unit_table ()
11570 {
11571 /* Start out with a pretty small number.
11572 Generally DWO files contain only one CU and maybe some TUs. */
11573 return htab_up (htab_create_alloc (3,
11574 hash_dwo_unit,
11575 eq_dwo_unit,
11576 NULL, xcalloc, xfree));
11577 }
11578
11579 /* die_reader_func for create_dwo_cu. */
11580
11581 static void
11582 create_dwo_cu_reader (const struct die_reader_specs *reader,
11583 const gdb_byte *info_ptr,
11584 struct die_info *comp_unit_die,
11585 struct dwo_file *dwo_file,
11586 struct dwo_unit *dwo_unit)
11587 {
11588 struct dwarf2_cu *cu = reader->cu;
11589 sect_offset sect_off = cu->per_cu->sect_off;
11590 struct dwarf2_section_info *section = cu->per_cu->section;
11591
11592 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11593 if (!signature.has_value ())
11594 {
11595 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11596 " its dwo_id [in module %s]"),
11597 sect_offset_str (sect_off), dwo_file->dwo_name);
11598 return;
11599 }
11600
11601 dwo_unit->dwo_file = dwo_file;
11602 dwo_unit->signature = *signature;
11603 dwo_unit->section = section;
11604 dwo_unit->sect_off = sect_off;
11605 dwo_unit->length = cu->per_cu->length;
11606
11607 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11608 sect_offset_str (sect_off),
11609 hex_string (dwo_unit->signature));
11610 }
11611
11612 /* Create the dwo_units for the CUs in a DWO_FILE.
11613 Note: This function processes DWO files only, not DWP files. */
11614
11615 static void
11616 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11617 dwarf2_cu *cu, struct dwo_file &dwo_file,
11618 dwarf2_section_info &section, htab_up &cus_htab)
11619 {
11620 struct objfile *objfile = per_objfile->objfile;
11621 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11622 const gdb_byte *info_ptr, *end_ptr;
11623
11624 section.read (objfile);
11625 info_ptr = section.buffer;
11626
11627 if (info_ptr == NULL)
11628 return;
11629
11630 dwarf_read_debug_printf ("Reading %s for %s:",
11631 section.get_name (),
11632 section.get_file_name ());
11633
11634 end_ptr = info_ptr + section.size;
11635 while (info_ptr < end_ptr)
11636 {
11637 struct dwarf2_per_cu_data per_cu;
11638 struct dwo_unit read_unit {};
11639 struct dwo_unit *dwo_unit;
11640 void **slot;
11641 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11642
11643 memset (&per_cu, 0, sizeof (per_cu));
11644 per_cu.per_bfd = per_bfd;
11645 per_cu.is_debug_types = 0;
11646 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11647 per_cu.section = &section;
11648
11649 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11650 if (!reader.dummy_p)
11651 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11652 &dwo_file, &read_unit);
11653 info_ptr += per_cu.length;
11654
11655 // If the unit could not be parsed, skip it.
11656 if (read_unit.dwo_file == NULL)
11657 continue;
11658
11659 if (cus_htab == NULL)
11660 cus_htab = allocate_dwo_unit_table ();
11661
11662 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11663 struct dwo_unit);
11664 *dwo_unit = read_unit;
11665 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11666 gdb_assert (slot != NULL);
11667 if (*slot != NULL)
11668 {
11669 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11670 sect_offset dup_sect_off = dup_cu->sect_off;
11671
11672 complaint (_("debug cu entry at offset %s is duplicate to"
11673 " the entry at offset %s, signature %s"),
11674 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11675 hex_string (dwo_unit->signature));
11676 }
11677 *slot = (void *)dwo_unit;
11678 }
11679 }
11680
11681 /* DWP file .debug_{cu,tu}_index section format:
11682 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11683 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11684
11685 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11686 officially standard DWP format was published with DWARF v5 and is called
11687 Version 5. There are no versions 3 or 4.
11688
11689 DWP Version 1:
11690
11691 Both index sections have the same format, and serve to map a 64-bit
11692 signature to a set of section numbers. Each section begins with a header,
11693 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11694 indexes, and a pool of 32-bit section numbers. The index sections will be
11695 aligned at 8-byte boundaries in the file.
11696
11697 The index section header consists of:
11698
11699 V, 32 bit version number
11700 -, 32 bits unused
11701 N, 32 bit number of compilation units or type units in the index
11702 M, 32 bit number of slots in the hash table
11703
11704 Numbers are recorded using the byte order of the application binary.
11705
11706 The hash table begins at offset 16 in the section, and consists of an array
11707 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11708 order of the application binary). Unused slots in the hash table are 0.
11709 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11710
11711 The parallel table begins immediately after the hash table
11712 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11713 array of 32-bit indexes (using the byte order of the application binary),
11714 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11715 table contains a 32-bit index into the pool of section numbers. For unused
11716 hash table slots, the corresponding entry in the parallel table will be 0.
11717
11718 The pool of section numbers begins immediately following the hash table
11719 (at offset 16 + 12 * M from the beginning of the section). The pool of
11720 section numbers consists of an array of 32-bit words (using the byte order
11721 of the application binary). Each item in the array is indexed starting
11722 from 0. The hash table entry provides the index of the first section
11723 number in the set. Additional section numbers in the set follow, and the
11724 set is terminated by a 0 entry (section number 0 is not used in ELF).
11725
11726 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11727 section must be the first entry in the set, and the .debug_abbrev.dwo must
11728 be the second entry. Other members of the set may follow in any order.
11729
11730 ---
11731
11732 DWP Versions 2 and 5:
11733
11734 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11735 and the entries in the index tables are now offsets into these sections.
11736 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11737 section.
11738
11739 Index Section Contents:
11740 Header
11741 Hash Table of Signatures dwp_hash_table.hash_table
11742 Parallel Table of Indices dwp_hash_table.unit_table
11743 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11744 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11745
11746 The index section header consists of:
11747
11748 V, 32 bit version number
11749 L, 32 bit number of columns in the table of section offsets
11750 N, 32 bit number of compilation units or type units in the index
11751 M, 32 bit number of slots in the hash table
11752
11753 Numbers are recorded using the byte order of the application binary.
11754
11755 The hash table has the same format as version 1.
11756 The parallel table of indices has the same format as version 1,
11757 except that the entries are origin-1 indices into the table of sections
11758 offsets and the table of section sizes.
11759
11760 The table of offsets begins immediately following the parallel table
11761 (at offset 16 + 12 * M from the beginning of the section). The table is
11762 a two-dimensional array of 32-bit words (using the byte order of the
11763 application binary), with L columns and N+1 rows, in row-major order.
11764 Each row in the array is indexed starting from 0. The first row provides
11765 a key to the remaining rows: each column in this row provides an identifier
11766 for a debug section, and the offsets in the same column of subsequent rows
11767 refer to that section. The section identifiers for Version 2 are:
11768
11769 DW_SECT_INFO 1 .debug_info.dwo
11770 DW_SECT_TYPES 2 .debug_types.dwo
11771 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11772 DW_SECT_LINE 4 .debug_line.dwo
11773 DW_SECT_LOC 5 .debug_loc.dwo
11774 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11775 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11776 DW_SECT_MACRO 8 .debug_macro.dwo
11777
11778 The section identifiers for Version 5 are:
11779
11780 DW_SECT_INFO_V5 1 .debug_info.dwo
11781 DW_SECT_RESERVED_V5 2 --
11782 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11783 DW_SECT_LINE_V5 4 .debug_line.dwo
11784 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11785 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11786 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11787 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11788
11789 The offsets provided by the CU and TU index sections are the base offsets
11790 for the contributions made by each CU or TU to the corresponding section
11791 in the package file. Each CU and TU header contains an abbrev_offset
11792 field, used to find the abbreviations table for that CU or TU within the
11793 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11794 be interpreted as relative to the base offset given in the index section.
11795 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11796 should be interpreted as relative to the base offset for .debug_line.dwo,
11797 and offsets into other debug sections obtained from DWARF attributes should
11798 also be interpreted as relative to the corresponding base offset.
11799
11800 The table of sizes begins immediately following the table of offsets.
11801 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11802 with L columns and N rows, in row-major order. Each row in the array is
11803 indexed starting from 1 (row 0 is shared by the two tables).
11804
11805 ---
11806
11807 Hash table lookup is handled the same in version 1 and 2:
11808
11809 We assume that N and M will not exceed 2^32 - 1.
11810 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11811
11812 Given a 64-bit compilation unit signature or a type signature S, an entry
11813 in the hash table is located as follows:
11814
11815 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11816 the low-order k bits all set to 1.
11817
11818 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11819
11820 3) If the hash table entry at index H matches the signature, use that
11821 entry. If the hash table entry at index H is unused (all zeroes),
11822 terminate the search: the signature is not present in the table.
11823
11824 4) Let H = (H + H') modulo M. Repeat at Step 3.
11825
11826 Because M > N and H' and M are relatively prime, the search is guaranteed
11827 to stop at an unused slot or find the match. */
11828
11829 /* Create a hash table to map DWO IDs to their CU/TU entry in
11830 .debug_{info,types}.dwo in DWP_FILE.
11831 Returns NULL if there isn't one.
11832 Note: This function processes DWP files only, not DWO files. */
11833
11834 static struct dwp_hash_table *
11835 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11836 struct dwp_file *dwp_file, int is_debug_types)
11837 {
11838 struct objfile *objfile = per_objfile->objfile;
11839 bfd *dbfd = dwp_file->dbfd.get ();
11840 const gdb_byte *index_ptr, *index_end;
11841 struct dwarf2_section_info *index;
11842 uint32_t version, nr_columns, nr_units, nr_slots;
11843 struct dwp_hash_table *htab;
11844
11845 if (is_debug_types)
11846 index = &dwp_file->sections.tu_index;
11847 else
11848 index = &dwp_file->sections.cu_index;
11849
11850 if (index->empty ())
11851 return NULL;
11852 index->read (objfile);
11853
11854 index_ptr = index->buffer;
11855 index_end = index_ptr + index->size;
11856
11857 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11858 For now it's safe to just read 4 bytes (particularly as it's difficult to
11859 tell if you're dealing with Version 5 before you've read the version). */
11860 version = read_4_bytes (dbfd, index_ptr);
11861 index_ptr += 4;
11862 if (version == 2 || version == 5)
11863 nr_columns = read_4_bytes (dbfd, index_ptr);
11864 else
11865 nr_columns = 0;
11866 index_ptr += 4;
11867 nr_units = read_4_bytes (dbfd, index_ptr);
11868 index_ptr += 4;
11869 nr_slots = read_4_bytes (dbfd, index_ptr);
11870 index_ptr += 4;
11871
11872 if (version != 1 && version != 2 && version != 5)
11873 {
11874 error (_("Dwarf Error: unsupported DWP file version (%s)"
11875 " [in module %s]"),
11876 pulongest (version), dwp_file->name);
11877 }
11878 if (nr_slots != (nr_slots & -nr_slots))
11879 {
11880 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11881 " is not power of 2 [in module %s]"),
11882 pulongest (nr_slots), dwp_file->name);
11883 }
11884
11885 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11886 htab->version = version;
11887 htab->nr_columns = nr_columns;
11888 htab->nr_units = nr_units;
11889 htab->nr_slots = nr_slots;
11890 htab->hash_table = index_ptr;
11891 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11892
11893 /* Exit early if the table is empty. */
11894 if (nr_slots == 0 || nr_units == 0
11895 || (version == 2 && nr_columns == 0)
11896 || (version == 5 && nr_columns == 0))
11897 {
11898 /* All must be zero. */
11899 if (nr_slots != 0 || nr_units != 0
11900 || (version == 2 && nr_columns != 0)
11901 || (version == 5 && nr_columns != 0))
11902 {
11903 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11904 " all zero [in modules %s]"),
11905 dwp_file->name);
11906 }
11907 return htab;
11908 }
11909
11910 if (version == 1)
11911 {
11912 htab->section_pool.v1.indices =
11913 htab->unit_table + sizeof (uint32_t) * nr_slots;
11914 /* It's harder to decide whether the section is too small in v1.
11915 V1 is deprecated anyway so we punt. */
11916 }
11917 else if (version == 2)
11918 {
11919 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11920 int *ids = htab->section_pool.v2.section_ids;
11921 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11922 /* Reverse map for error checking. */
11923 int ids_seen[DW_SECT_MAX + 1];
11924 int i;
11925
11926 if (nr_columns < 2)
11927 {
11928 error (_("Dwarf Error: bad DWP hash table, too few columns"
11929 " in section table [in module %s]"),
11930 dwp_file->name);
11931 }
11932 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11933 {
11934 error (_("Dwarf Error: bad DWP hash table, too many columns"
11935 " in section table [in module %s]"),
11936 dwp_file->name);
11937 }
11938 memset (ids, 255, sizeof_ids);
11939 memset (ids_seen, 255, sizeof (ids_seen));
11940 for (i = 0; i < nr_columns; ++i)
11941 {
11942 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11943
11944 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11945 {
11946 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11947 " in section table [in module %s]"),
11948 id, dwp_file->name);
11949 }
11950 if (ids_seen[id] != -1)
11951 {
11952 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11953 " id %d in section table [in module %s]"),
11954 id, dwp_file->name);
11955 }
11956 ids_seen[id] = i;
11957 ids[i] = id;
11958 }
11959 /* Must have exactly one info or types section. */
11960 if (((ids_seen[DW_SECT_INFO] != -1)
11961 + (ids_seen[DW_SECT_TYPES] != -1))
11962 != 1)
11963 {
11964 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11965 " DWO info/types section [in module %s]"),
11966 dwp_file->name);
11967 }
11968 /* Must have an abbrev section. */
11969 if (ids_seen[DW_SECT_ABBREV] == -1)
11970 {
11971 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11972 " section [in module %s]"),
11973 dwp_file->name);
11974 }
11975 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11976 htab->section_pool.v2.sizes =
11977 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11978 * nr_units * nr_columns);
11979 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11980 * nr_units * nr_columns))
11981 > index_end)
11982 {
11983 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11984 " [in module %s]"),
11985 dwp_file->name);
11986 }
11987 }
11988 else /* version == 5 */
11989 {
11990 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11991 int *ids = htab->section_pool.v5.section_ids;
11992 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11993 /* Reverse map for error checking. */
11994 int ids_seen[DW_SECT_MAX_V5 + 1];
11995
11996 if (nr_columns < 2)
11997 {
11998 error (_("Dwarf Error: bad DWP hash table, too few columns"
11999 " in section table [in module %s]"),
12000 dwp_file->name);
12001 }
12002 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
12003 {
12004 error (_("Dwarf Error: bad DWP hash table, too many columns"
12005 " in section table [in module %s]"),
12006 dwp_file->name);
12007 }
12008 memset (ids, 255, sizeof_ids);
12009 memset (ids_seen, 255, sizeof (ids_seen));
12010 for (int i = 0; i < nr_columns; ++i)
12011 {
12012 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12013
12014 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
12015 {
12016 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12017 " in section table [in module %s]"),
12018 id, dwp_file->name);
12019 }
12020 if (ids_seen[id] != -1)
12021 {
12022 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12023 " id %d in section table [in module %s]"),
12024 id, dwp_file->name);
12025 }
12026 ids_seen[id] = i;
12027 ids[i] = id;
12028 }
12029 /* Must have seen an info section. */
12030 if (ids_seen[DW_SECT_INFO_V5] == -1)
12031 {
12032 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12033 " DWO info/types section [in module %s]"),
12034 dwp_file->name);
12035 }
12036 /* Must have an abbrev section. */
12037 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
12038 {
12039 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12040 " section [in module %s]"),
12041 dwp_file->name);
12042 }
12043 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12044 htab->section_pool.v5.sizes
12045 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
12046 * nr_units * nr_columns);
12047 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
12048 * nr_units * nr_columns))
12049 > index_end)
12050 {
12051 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12052 " [in module %s]"),
12053 dwp_file->name);
12054 }
12055 }
12056
12057 return htab;
12058 }
12059
12060 /* Update SECTIONS with the data from SECTP.
12061
12062 This function is like the other "locate" section routines, but in
12063 this context the sections to read comes from the DWP V1 hash table,
12064 not the full ELF section table.
12065
12066 The result is non-zero for success, or zero if an error was found. */
12067
12068 static int
12069 locate_v1_virtual_dwo_sections (asection *sectp,
12070 struct virtual_v1_dwo_sections *sections)
12071 {
12072 const struct dwop_section_names *names = &dwop_section_names;
12073
12074 if (names->abbrev_dwo.matches (sectp->name))
12075 {
12076 /* There can be only one. */
12077 if (sections->abbrev.s.section != NULL)
12078 return 0;
12079 sections->abbrev.s.section = sectp;
12080 sections->abbrev.size = bfd_section_size (sectp);
12081 }
12082 else if (names->info_dwo.matches (sectp->name)
12083 || names->types_dwo.matches (sectp->name))
12084 {
12085 /* There can be only one. */
12086 if (sections->info_or_types.s.section != NULL)
12087 return 0;
12088 sections->info_or_types.s.section = sectp;
12089 sections->info_or_types.size = bfd_section_size (sectp);
12090 }
12091 else if (names->line_dwo.matches (sectp->name))
12092 {
12093 /* There can be only one. */
12094 if (sections->line.s.section != NULL)
12095 return 0;
12096 sections->line.s.section = sectp;
12097 sections->line.size = bfd_section_size (sectp);
12098 }
12099 else if (names->loc_dwo.matches (sectp->name))
12100 {
12101 /* There can be only one. */
12102 if (sections->loc.s.section != NULL)
12103 return 0;
12104 sections->loc.s.section = sectp;
12105 sections->loc.size = bfd_section_size (sectp);
12106 }
12107 else if (names->macinfo_dwo.matches (sectp->name))
12108 {
12109 /* There can be only one. */
12110 if (sections->macinfo.s.section != NULL)
12111 return 0;
12112 sections->macinfo.s.section = sectp;
12113 sections->macinfo.size = bfd_section_size (sectp);
12114 }
12115 else if (names->macro_dwo.matches (sectp->name))
12116 {
12117 /* There can be only one. */
12118 if (sections->macro.s.section != NULL)
12119 return 0;
12120 sections->macro.s.section = sectp;
12121 sections->macro.size = bfd_section_size (sectp);
12122 }
12123 else if (names->str_offsets_dwo.matches (sectp->name))
12124 {
12125 /* There can be only one. */
12126 if (sections->str_offsets.s.section != NULL)
12127 return 0;
12128 sections->str_offsets.s.section = sectp;
12129 sections->str_offsets.size = bfd_section_size (sectp);
12130 }
12131 else
12132 {
12133 /* No other kind of section is valid. */
12134 return 0;
12135 }
12136
12137 return 1;
12138 }
12139
12140 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12141 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12142 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12143 This is for DWP version 1 files. */
12144
12145 static struct dwo_unit *
12146 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12147 struct dwp_file *dwp_file,
12148 uint32_t unit_index,
12149 const char *comp_dir,
12150 ULONGEST signature, int is_debug_types)
12151 {
12152 const struct dwp_hash_table *dwp_htab =
12153 is_debug_types ? dwp_file->tus : dwp_file->cus;
12154 bfd *dbfd = dwp_file->dbfd.get ();
12155 const char *kind = is_debug_types ? "TU" : "CU";
12156 struct dwo_file *dwo_file;
12157 struct dwo_unit *dwo_unit;
12158 struct virtual_v1_dwo_sections sections;
12159 void **dwo_file_slot;
12160 int i;
12161
12162 gdb_assert (dwp_file->version == 1);
12163
12164 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12165 kind, pulongest (unit_index), hex_string (signature),
12166 dwp_file->name);
12167
12168 /* Fetch the sections of this DWO unit.
12169 Put a limit on the number of sections we look for so that bad data
12170 doesn't cause us to loop forever. */
12171
12172 #define MAX_NR_V1_DWO_SECTIONS \
12173 (1 /* .debug_info or .debug_types */ \
12174 + 1 /* .debug_abbrev */ \
12175 + 1 /* .debug_line */ \
12176 + 1 /* .debug_loc */ \
12177 + 1 /* .debug_str_offsets */ \
12178 + 1 /* .debug_macro or .debug_macinfo */ \
12179 + 1 /* trailing zero */)
12180
12181 memset (&sections, 0, sizeof (sections));
12182
12183 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12184 {
12185 asection *sectp;
12186 uint32_t section_nr =
12187 read_4_bytes (dbfd,
12188 dwp_htab->section_pool.v1.indices
12189 + (unit_index + i) * sizeof (uint32_t));
12190
12191 if (section_nr == 0)
12192 break;
12193 if (section_nr >= dwp_file->num_sections)
12194 {
12195 error (_("Dwarf Error: bad DWP hash table, section number too large"
12196 " [in module %s]"),
12197 dwp_file->name);
12198 }
12199
12200 sectp = dwp_file->elf_sections[section_nr];
12201 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12202 {
12203 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12204 " [in module %s]"),
12205 dwp_file->name);
12206 }
12207 }
12208
12209 if (i < 2
12210 || sections.info_or_types.empty ()
12211 || sections.abbrev.empty ())
12212 {
12213 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12214 " [in module %s]"),
12215 dwp_file->name);
12216 }
12217 if (i == MAX_NR_V1_DWO_SECTIONS)
12218 {
12219 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12220 " [in module %s]"),
12221 dwp_file->name);
12222 }
12223
12224 /* It's easier for the rest of the code if we fake a struct dwo_file and
12225 have dwo_unit "live" in that. At least for now.
12226
12227 The DWP file can be made up of a random collection of CUs and TUs.
12228 However, for each CU + set of TUs that came from the same original DWO
12229 file, we can combine them back into a virtual DWO file to save space
12230 (fewer struct dwo_file objects to allocate). Remember that for really
12231 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12232
12233 std::string virtual_dwo_name =
12234 string_printf ("virtual-dwo/%d-%d-%d-%d",
12235 sections.abbrev.get_id (),
12236 sections.line.get_id (),
12237 sections.loc.get_id (),
12238 sections.str_offsets.get_id ());
12239 /* Can we use an existing virtual DWO file? */
12240 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12241 comp_dir);
12242 /* Create one if necessary. */
12243 if (*dwo_file_slot == NULL)
12244 {
12245 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12246 virtual_dwo_name.c_str ());
12247
12248 dwo_file = new struct dwo_file;
12249 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12250 dwo_file->comp_dir = comp_dir;
12251 dwo_file->sections.abbrev = sections.abbrev;
12252 dwo_file->sections.line = sections.line;
12253 dwo_file->sections.loc = sections.loc;
12254 dwo_file->sections.macinfo = sections.macinfo;
12255 dwo_file->sections.macro = sections.macro;
12256 dwo_file->sections.str_offsets = sections.str_offsets;
12257 /* The "str" section is global to the entire DWP file. */
12258 dwo_file->sections.str = dwp_file->sections.str;
12259 /* The info or types section is assigned below to dwo_unit,
12260 there's no need to record it in dwo_file.
12261 Also, we can't simply record type sections in dwo_file because
12262 we record a pointer into the vector in dwo_unit. As we collect more
12263 types we'll grow the vector and eventually have to reallocate space
12264 for it, invalidating all copies of pointers into the previous
12265 contents. */
12266 *dwo_file_slot = dwo_file;
12267 }
12268 else
12269 {
12270 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12271 virtual_dwo_name.c_str ());
12272
12273 dwo_file = (struct dwo_file *) *dwo_file_slot;
12274 }
12275
12276 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12277 dwo_unit->dwo_file = dwo_file;
12278 dwo_unit->signature = signature;
12279 dwo_unit->section =
12280 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12281 *dwo_unit->section = sections.info_or_types;
12282 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12283
12284 return dwo_unit;
12285 }
12286
12287 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12288 simplify them. Given a pointer to the containing section SECTION, and
12289 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12290 virtual section of just that piece. */
12291
12292 static struct dwarf2_section_info
12293 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12294 struct dwarf2_section_info *section,
12295 bfd_size_type offset, bfd_size_type size)
12296 {
12297 struct dwarf2_section_info result;
12298 asection *sectp;
12299
12300 gdb_assert (section != NULL);
12301 gdb_assert (!section->is_virtual);
12302
12303 memset (&result, 0, sizeof (result));
12304 result.s.containing_section = section;
12305 result.is_virtual = true;
12306
12307 if (size == 0)
12308 return result;
12309
12310 sectp = section->get_bfd_section ();
12311
12312 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12313 bounds of the real section. This is a pretty-rare event, so just
12314 flag an error (easier) instead of a warning and trying to cope. */
12315 if (sectp == NULL
12316 || offset + size > bfd_section_size (sectp))
12317 {
12318 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12319 " in section %s [in module %s]"),
12320 sectp ? bfd_section_name (sectp) : "<unknown>",
12321 objfile_name (per_objfile->objfile));
12322 }
12323
12324 result.virtual_offset = offset;
12325 result.size = size;
12326 return result;
12327 }
12328
12329 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12330 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12331 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12332 This is for DWP version 2 files. */
12333
12334 static struct dwo_unit *
12335 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12336 struct dwp_file *dwp_file,
12337 uint32_t unit_index,
12338 const char *comp_dir,
12339 ULONGEST signature, int is_debug_types)
12340 {
12341 const struct dwp_hash_table *dwp_htab =
12342 is_debug_types ? dwp_file->tus : dwp_file->cus;
12343 bfd *dbfd = dwp_file->dbfd.get ();
12344 const char *kind = is_debug_types ? "TU" : "CU";
12345 struct dwo_file *dwo_file;
12346 struct dwo_unit *dwo_unit;
12347 struct virtual_v2_or_v5_dwo_sections sections;
12348 void **dwo_file_slot;
12349 int i;
12350
12351 gdb_assert (dwp_file->version == 2);
12352
12353 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12354 kind, pulongest (unit_index), hex_string (signature),
12355 dwp_file->name);
12356
12357 /* Fetch the section offsets of this DWO unit. */
12358
12359 memset (&sections, 0, sizeof (sections));
12360
12361 for (i = 0; i < dwp_htab->nr_columns; ++i)
12362 {
12363 uint32_t offset = read_4_bytes (dbfd,
12364 dwp_htab->section_pool.v2.offsets
12365 + (((unit_index - 1) * dwp_htab->nr_columns
12366 + i)
12367 * sizeof (uint32_t)));
12368 uint32_t size = read_4_bytes (dbfd,
12369 dwp_htab->section_pool.v2.sizes
12370 + (((unit_index - 1) * dwp_htab->nr_columns
12371 + i)
12372 * sizeof (uint32_t)));
12373
12374 switch (dwp_htab->section_pool.v2.section_ids[i])
12375 {
12376 case DW_SECT_INFO:
12377 case DW_SECT_TYPES:
12378 sections.info_or_types_offset = offset;
12379 sections.info_or_types_size = size;
12380 break;
12381 case DW_SECT_ABBREV:
12382 sections.abbrev_offset = offset;
12383 sections.abbrev_size = size;
12384 break;
12385 case DW_SECT_LINE:
12386 sections.line_offset = offset;
12387 sections.line_size = size;
12388 break;
12389 case DW_SECT_LOC:
12390 sections.loc_offset = offset;
12391 sections.loc_size = size;
12392 break;
12393 case DW_SECT_STR_OFFSETS:
12394 sections.str_offsets_offset = offset;
12395 sections.str_offsets_size = size;
12396 break;
12397 case DW_SECT_MACINFO:
12398 sections.macinfo_offset = offset;
12399 sections.macinfo_size = size;
12400 break;
12401 case DW_SECT_MACRO:
12402 sections.macro_offset = offset;
12403 sections.macro_size = size;
12404 break;
12405 }
12406 }
12407
12408 /* It's easier for the rest of the code if we fake a struct dwo_file and
12409 have dwo_unit "live" in that. At least for now.
12410
12411 The DWP file can be made up of a random collection of CUs and TUs.
12412 However, for each CU + set of TUs that came from the same original DWO
12413 file, we can combine them back into a virtual DWO file to save space
12414 (fewer struct dwo_file objects to allocate). Remember that for really
12415 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12416
12417 std::string virtual_dwo_name =
12418 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12419 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12420 (long) (sections.line_size ? sections.line_offset : 0),
12421 (long) (sections.loc_size ? sections.loc_offset : 0),
12422 (long) (sections.str_offsets_size
12423 ? sections.str_offsets_offset : 0));
12424 /* Can we use an existing virtual DWO file? */
12425 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12426 comp_dir);
12427 /* Create one if necessary. */
12428 if (*dwo_file_slot == NULL)
12429 {
12430 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12431 virtual_dwo_name.c_str ());
12432
12433 dwo_file = new struct dwo_file;
12434 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12435 dwo_file->comp_dir = comp_dir;
12436 dwo_file->sections.abbrev =
12437 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12438 sections.abbrev_offset,
12439 sections.abbrev_size);
12440 dwo_file->sections.line =
12441 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12442 sections.line_offset,
12443 sections.line_size);
12444 dwo_file->sections.loc =
12445 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12446 sections.loc_offset, sections.loc_size);
12447 dwo_file->sections.macinfo =
12448 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12449 sections.macinfo_offset,
12450 sections.macinfo_size);
12451 dwo_file->sections.macro =
12452 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12453 sections.macro_offset,
12454 sections.macro_size);
12455 dwo_file->sections.str_offsets =
12456 create_dwp_v2_or_v5_section (per_objfile,
12457 &dwp_file->sections.str_offsets,
12458 sections.str_offsets_offset,
12459 sections.str_offsets_size);
12460 /* The "str" section is global to the entire DWP file. */
12461 dwo_file->sections.str = dwp_file->sections.str;
12462 /* The info or types section is assigned below to dwo_unit,
12463 there's no need to record it in dwo_file.
12464 Also, we can't simply record type sections in dwo_file because
12465 we record a pointer into the vector in dwo_unit. As we collect more
12466 types we'll grow the vector and eventually have to reallocate space
12467 for it, invalidating all copies of pointers into the previous
12468 contents. */
12469 *dwo_file_slot = dwo_file;
12470 }
12471 else
12472 {
12473 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12474 virtual_dwo_name.c_str ());
12475
12476 dwo_file = (struct dwo_file *) *dwo_file_slot;
12477 }
12478
12479 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12480 dwo_unit->dwo_file = dwo_file;
12481 dwo_unit->signature = signature;
12482 dwo_unit->section =
12483 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12484 *dwo_unit->section = create_dwp_v2_or_v5_section
12485 (per_objfile,
12486 is_debug_types
12487 ? &dwp_file->sections.types
12488 : &dwp_file->sections.info,
12489 sections.info_or_types_offset,
12490 sections.info_or_types_size);
12491 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12492
12493 return dwo_unit;
12494 }
12495
12496 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12497 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12498 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12499 This is for DWP version 5 files. */
12500
12501 static struct dwo_unit *
12502 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12503 struct dwp_file *dwp_file,
12504 uint32_t unit_index,
12505 const char *comp_dir,
12506 ULONGEST signature, int is_debug_types)
12507 {
12508 const struct dwp_hash_table *dwp_htab
12509 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12510 bfd *dbfd = dwp_file->dbfd.get ();
12511 const char *kind = is_debug_types ? "TU" : "CU";
12512 struct dwo_file *dwo_file;
12513 struct dwo_unit *dwo_unit;
12514 struct virtual_v2_or_v5_dwo_sections sections {};
12515 void **dwo_file_slot;
12516
12517 gdb_assert (dwp_file->version == 5);
12518
12519 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12520 kind, pulongest (unit_index), hex_string (signature),
12521 dwp_file->name);
12522
12523 /* Fetch the section offsets of this DWO unit. */
12524
12525 /* memset (&sections, 0, sizeof (sections)); */
12526
12527 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12528 {
12529 uint32_t offset = read_4_bytes (dbfd,
12530 dwp_htab->section_pool.v5.offsets
12531 + (((unit_index - 1)
12532 * dwp_htab->nr_columns
12533 + i)
12534 * sizeof (uint32_t)));
12535 uint32_t size = read_4_bytes (dbfd,
12536 dwp_htab->section_pool.v5.sizes
12537 + (((unit_index - 1) * dwp_htab->nr_columns
12538 + i)
12539 * sizeof (uint32_t)));
12540
12541 switch (dwp_htab->section_pool.v5.section_ids[i])
12542 {
12543 case DW_SECT_ABBREV_V5:
12544 sections.abbrev_offset = offset;
12545 sections.abbrev_size = size;
12546 break;
12547 case DW_SECT_INFO_V5:
12548 sections.info_or_types_offset = offset;
12549 sections.info_or_types_size = size;
12550 break;
12551 case DW_SECT_LINE_V5:
12552 sections.line_offset = offset;
12553 sections.line_size = size;
12554 break;
12555 case DW_SECT_LOCLISTS_V5:
12556 sections.loclists_offset = offset;
12557 sections.loclists_size = size;
12558 break;
12559 case DW_SECT_MACRO_V5:
12560 sections.macro_offset = offset;
12561 sections.macro_size = size;
12562 break;
12563 case DW_SECT_RNGLISTS_V5:
12564 sections.rnglists_offset = offset;
12565 sections.rnglists_size = size;
12566 break;
12567 case DW_SECT_STR_OFFSETS_V5:
12568 sections.str_offsets_offset = offset;
12569 sections.str_offsets_size = size;
12570 break;
12571 case DW_SECT_RESERVED_V5:
12572 default:
12573 break;
12574 }
12575 }
12576
12577 /* It's easier for the rest of the code if we fake a struct dwo_file and
12578 have dwo_unit "live" in that. At least for now.
12579
12580 The DWP file can be made up of a random collection of CUs and TUs.
12581 However, for each CU + set of TUs that came from the same original DWO
12582 file, we can combine them back into a virtual DWO file to save space
12583 (fewer struct dwo_file objects to allocate). Remember that for really
12584 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12585
12586 std::string virtual_dwo_name =
12587 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12588 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12589 (long) (sections.line_size ? sections.line_offset : 0),
12590 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12591 (long) (sections.str_offsets_size
12592 ? sections.str_offsets_offset : 0),
12593 (long) (sections.macro_size ? sections.macro_offset : 0),
12594 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12595 /* Can we use an existing virtual DWO file? */
12596 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12597 virtual_dwo_name.c_str (),
12598 comp_dir);
12599 /* Create one if necessary. */
12600 if (*dwo_file_slot == NULL)
12601 {
12602 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12603 virtual_dwo_name.c_str ());
12604
12605 dwo_file = new struct dwo_file;
12606 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12607 dwo_file->comp_dir = comp_dir;
12608 dwo_file->sections.abbrev =
12609 create_dwp_v2_or_v5_section (per_objfile,
12610 &dwp_file->sections.abbrev,
12611 sections.abbrev_offset,
12612 sections.abbrev_size);
12613 dwo_file->sections.line =
12614 create_dwp_v2_or_v5_section (per_objfile,
12615 &dwp_file->sections.line,
12616 sections.line_offset, sections.line_size);
12617 dwo_file->sections.macro =
12618 create_dwp_v2_or_v5_section (per_objfile,
12619 &dwp_file->sections.macro,
12620 sections.macro_offset,
12621 sections.macro_size);
12622 dwo_file->sections.loclists =
12623 create_dwp_v2_or_v5_section (per_objfile,
12624 &dwp_file->sections.loclists,
12625 sections.loclists_offset,
12626 sections.loclists_size);
12627 dwo_file->sections.rnglists =
12628 create_dwp_v2_or_v5_section (per_objfile,
12629 &dwp_file->sections.rnglists,
12630 sections.rnglists_offset,
12631 sections.rnglists_size);
12632 dwo_file->sections.str_offsets =
12633 create_dwp_v2_or_v5_section (per_objfile,
12634 &dwp_file->sections.str_offsets,
12635 sections.str_offsets_offset,
12636 sections.str_offsets_size);
12637 /* The "str" section is global to the entire DWP file. */
12638 dwo_file->sections.str = dwp_file->sections.str;
12639 /* The info or types section is assigned below to dwo_unit,
12640 there's no need to record it in dwo_file.
12641 Also, we can't simply record type sections in dwo_file because
12642 we record a pointer into the vector in dwo_unit. As we collect more
12643 types we'll grow the vector and eventually have to reallocate space
12644 for it, invalidating all copies of pointers into the previous
12645 contents. */
12646 *dwo_file_slot = dwo_file;
12647 }
12648 else
12649 {
12650 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12651 virtual_dwo_name.c_str ());
12652
12653 dwo_file = (struct dwo_file *) *dwo_file_slot;
12654 }
12655
12656 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12657 dwo_unit->dwo_file = dwo_file;
12658 dwo_unit->signature = signature;
12659 dwo_unit->section
12660 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12661 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12662 &dwp_file->sections.info,
12663 sections.info_or_types_offset,
12664 sections.info_or_types_size);
12665 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12666
12667 return dwo_unit;
12668 }
12669
12670 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12671 Returns NULL if the signature isn't found. */
12672
12673 static struct dwo_unit *
12674 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12675 struct dwp_file *dwp_file, const char *comp_dir,
12676 ULONGEST signature, int is_debug_types)
12677 {
12678 const struct dwp_hash_table *dwp_htab =
12679 is_debug_types ? dwp_file->tus : dwp_file->cus;
12680 bfd *dbfd = dwp_file->dbfd.get ();
12681 uint32_t mask = dwp_htab->nr_slots - 1;
12682 uint32_t hash = signature & mask;
12683 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12684 unsigned int i;
12685 void **slot;
12686 struct dwo_unit find_dwo_cu;
12687
12688 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12689 find_dwo_cu.signature = signature;
12690 slot = htab_find_slot (is_debug_types
12691 ? dwp_file->loaded_tus.get ()
12692 : dwp_file->loaded_cus.get (),
12693 &find_dwo_cu, INSERT);
12694
12695 if (*slot != NULL)
12696 return (struct dwo_unit *) *slot;
12697
12698 /* Use a for loop so that we don't loop forever on bad debug info. */
12699 for (i = 0; i < dwp_htab->nr_slots; ++i)
12700 {
12701 ULONGEST signature_in_table;
12702
12703 signature_in_table =
12704 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12705 if (signature_in_table == signature)
12706 {
12707 uint32_t unit_index =
12708 read_4_bytes (dbfd,
12709 dwp_htab->unit_table + hash * sizeof (uint32_t));
12710
12711 if (dwp_file->version == 1)
12712 {
12713 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12714 unit_index, comp_dir,
12715 signature, is_debug_types);
12716 }
12717 else if (dwp_file->version == 2)
12718 {
12719 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12720 unit_index, comp_dir,
12721 signature, is_debug_types);
12722 }
12723 else /* version == 5 */
12724 {
12725 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12726 unit_index, comp_dir,
12727 signature, is_debug_types);
12728 }
12729 return (struct dwo_unit *) *slot;
12730 }
12731 if (signature_in_table == 0)
12732 return NULL;
12733 hash = (hash + hash2) & mask;
12734 }
12735
12736 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12737 " [in module %s]"),
12738 dwp_file->name);
12739 }
12740
12741 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12742 Open the file specified by FILE_NAME and hand it off to BFD for
12743 preliminary analysis. Return a newly initialized bfd *, which
12744 includes a canonicalized copy of FILE_NAME.
12745 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12746 SEARCH_CWD is true if the current directory is to be searched.
12747 It will be searched before debug-file-directory.
12748 If successful, the file is added to the bfd include table of the
12749 objfile's bfd (see gdb_bfd_record_inclusion).
12750 If unable to find/open the file, return NULL.
12751 NOTE: This function is derived from symfile_bfd_open. */
12752
12753 static gdb_bfd_ref_ptr
12754 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12755 const char *file_name, int is_dwp, int search_cwd)
12756 {
12757 int desc;
12758 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12759 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12760 to debug_file_directory. */
12761 const char *search_path;
12762 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12763
12764 gdb::unique_xmalloc_ptr<char> search_path_holder;
12765 if (search_cwd)
12766 {
12767 if (*debug_file_directory != '\0')
12768 {
12769 search_path_holder.reset (concat (".", dirname_separator_string,
12770 debug_file_directory,
12771 (char *) NULL));
12772 search_path = search_path_holder.get ();
12773 }
12774 else
12775 search_path = ".";
12776 }
12777 else
12778 search_path = debug_file_directory;
12779
12780 /* Add the path for the executable binary to the list of search paths. */
12781 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12782 search_path_holder.reset (concat (objfile_dir.c_str (),
12783 dirname_separator_string,
12784 search_path, nullptr));
12785 search_path = search_path_holder.get ();
12786
12787 openp_flags flags = OPF_RETURN_REALPATH;
12788 if (is_dwp)
12789 flags |= OPF_SEARCH_IN_PATH;
12790
12791 gdb::unique_xmalloc_ptr<char> absolute_name;
12792 desc = openp (search_path, flags, file_name,
12793 O_RDONLY | O_BINARY, &absolute_name);
12794 if (desc < 0)
12795 return NULL;
12796
12797 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12798 gnutarget, desc));
12799 if (sym_bfd == NULL)
12800 return NULL;
12801 bfd_set_cacheable (sym_bfd.get (), 1);
12802
12803 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12804 return NULL;
12805
12806 /* Success. Record the bfd as having been included by the objfile's bfd.
12807 This is important because things like demangled_names_hash lives in the
12808 objfile's per_bfd space and may have references to things like symbol
12809 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12810 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12811
12812 return sym_bfd;
12813 }
12814
12815 /* Try to open DWO file FILE_NAME.
12816 COMP_DIR is the DW_AT_comp_dir attribute.
12817 The result is the bfd handle of the file.
12818 If there is a problem finding or opening the file, return NULL.
12819 Upon success, the canonicalized path of the file is stored in the bfd,
12820 same as symfile_bfd_open. */
12821
12822 static gdb_bfd_ref_ptr
12823 open_dwo_file (dwarf2_per_objfile *per_objfile,
12824 const char *file_name, const char *comp_dir)
12825 {
12826 if (IS_ABSOLUTE_PATH (file_name))
12827 return try_open_dwop_file (per_objfile, file_name,
12828 0 /*is_dwp*/, 0 /*search_cwd*/);
12829
12830 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12831
12832 if (comp_dir != NULL)
12833 {
12834 gdb::unique_xmalloc_ptr<char> path_to_try
12835 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12836
12837 /* NOTE: If comp_dir is a relative path, this will also try the
12838 search path, which seems useful. */
12839 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12840 0 /*is_dwp*/,
12841 1 /*search_cwd*/));
12842 if (abfd != NULL)
12843 return abfd;
12844 }
12845
12846 /* That didn't work, try debug-file-directory, which, despite its name,
12847 is a list of paths. */
12848
12849 if (*debug_file_directory == '\0')
12850 return NULL;
12851
12852 return try_open_dwop_file (per_objfile, file_name,
12853 0 /*is_dwp*/, 1 /*search_cwd*/);
12854 }
12855
12856 /* This function is mapped across the sections and remembers the offset and
12857 size of each of the DWO debugging sections we are interested in. */
12858
12859 static void
12860 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12861 dwo_sections *dwo_sections)
12862 {
12863 const struct dwop_section_names *names = &dwop_section_names;
12864
12865 if (names->abbrev_dwo.matches (sectp->name))
12866 {
12867 dwo_sections->abbrev.s.section = sectp;
12868 dwo_sections->abbrev.size = bfd_section_size (sectp);
12869 }
12870 else if (names->info_dwo.matches (sectp->name))
12871 {
12872 dwo_sections->info.s.section = sectp;
12873 dwo_sections->info.size = bfd_section_size (sectp);
12874 }
12875 else if (names->line_dwo.matches (sectp->name))
12876 {
12877 dwo_sections->line.s.section = sectp;
12878 dwo_sections->line.size = bfd_section_size (sectp);
12879 }
12880 else if (names->loc_dwo.matches (sectp->name))
12881 {
12882 dwo_sections->loc.s.section = sectp;
12883 dwo_sections->loc.size = bfd_section_size (sectp);
12884 }
12885 else if (names->loclists_dwo.matches (sectp->name))
12886 {
12887 dwo_sections->loclists.s.section = sectp;
12888 dwo_sections->loclists.size = bfd_section_size (sectp);
12889 }
12890 else if (names->macinfo_dwo.matches (sectp->name))
12891 {
12892 dwo_sections->macinfo.s.section = sectp;
12893 dwo_sections->macinfo.size = bfd_section_size (sectp);
12894 }
12895 else if (names->macro_dwo.matches (sectp->name))
12896 {
12897 dwo_sections->macro.s.section = sectp;
12898 dwo_sections->macro.size = bfd_section_size (sectp);
12899 }
12900 else if (names->rnglists_dwo.matches (sectp->name))
12901 {
12902 dwo_sections->rnglists.s.section = sectp;
12903 dwo_sections->rnglists.size = bfd_section_size (sectp);
12904 }
12905 else if (names->str_dwo.matches (sectp->name))
12906 {
12907 dwo_sections->str.s.section = sectp;
12908 dwo_sections->str.size = bfd_section_size (sectp);
12909 }
12910 else if (names->str_offsets_dwo.matches (sectp->name))
12911 {
12912 dwo_sections->str_offsets.s.section = sectp;
12913 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12914 }
12915 else if (names->types_dwo.matches (sectp->name))
12916 {
12917 struct dwarf2_section_info type_section;
12918
12919 memset (&type_section, 0, sizeof (type_section));
12920 type_section.s.section = sectp;
12921 type_section.size = bfd_section_size (sectp);
12922 dwo_sections->types.push_back (type_section);
12923 }
12924 }
12925
12926 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12927 by PER_CU. This is for the non-DWP case.
12928 The result is NULL if DWO_NAME can't be found. */
12929
12930 static struct dwo_file *
12931 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12932 const char *comp_dir)
12933 {
12934 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12935
12936 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12937 if (dbfd == NULL)
12938 {
12939 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12940
12941 return NULL;
12942 }
12943
12944 dwo_file_up dwo_file (new struct dwo_file);
12945 dwo_file->dwo_name = dwo_name;
12946 dwo_file->comp_dir = comp_dir;
12947 dwo_file->dbfd = std::move (dbfd);
12948
12949 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12950 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12951 &dwo_file->sections);
12952
12953 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12954 dwo_file->cus);
12955
12956 if (cu->per_cu->dwarf_version < 5)
12957 {
12958 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12959 dwo_file->sections.types, dwo_file->tus);
12960 }
12961 else
12962 {
12963 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12964 &dwo_file->sections.info, dwo_file->tus,
12965 rcuh_kind::COMPILE);
12966 }
12967
12968 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12969
12970 return dwo_file.release ();
12971 }
12972
12973 /* This function is mapped across the sections and remembers the offset and
12974 size of each of the DWP debugging sections common to version 1 and 2 that
12975 we are interested in. */
12976
12977 static void
12978 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12979 dwp_file *dwp_file)
12980 {
12981 const struct dwop_section_names *names = &dwop_section_names;
12982 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12983
12984 /* Record the ELF section number for later lookup: this is what the
12985 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12986 gdb_assert (elf_section_nr < dwp_file->num_sections);
12987 dwp_file->elf_sections[elf_section_nr] = sectp;
12988
12989 /* Look for specific sections that we need. */
12990 if (names->str_dwo.matches (sectp->name))
12991 {
12992 dwp_file->sections.str.s.section = sectp;
12993 dwp_file->sections.str.size = bfd_section_size (sectp);
12994 }
12995 else if (names->cu_index.matches (sectp->name))
12996 {
12997 dwp_file->sections.cu_index.s.section = sectp;
12998 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12999 }
13000 else if (names->tu_index.matches (sectp->name))
13001 {
13002 dwp_file->sections.tu_index.s.section = sectp;
13003 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13004 }
13005 }
13006
13007 /* This function is mapped across the sections and remembers the offset and
13008 size of each of the DWP version 2 debugging sections that we are interested
13009 in. This is split into a separate function because we don't know if we
13010 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13011
13012 static void
13013 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13014 {
13015 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13016 const struct dwop_section_names *names = &dwop_section_names;
13017 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13018
13019 /* Record the ELF section number for later lookup: this is what the
13020 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13021 gdb_assert (elf_section_nr < dwp_file->num_sections);
13022 dwp_file->elf_sections[elf_section_nr] = sectp;
13023
13024 /* Look for specific sections that we need. */
13025 if (names->abbrev_dwo.matches (sectp->name))
13026 {
13027 dwp_file->sections.abbrev.s.section = sectp;
13028 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13029 }
13030 else if (names->info_dwo.matches (sectp->name))
13031 {
13032 dwp_file->sections.info.s.section = sectp;
13033 dwp_file->sections.info.size = bfd_section_size (sectp);
13034 }
13035 else if (names->line_dwo.matches (sectp->name))
13036 {
13037 dwp_file->sections.line.s.section = sectp;
13038 dwp_file->sections.line.size = bfd_section_size (sectp);
13039 }
13040 else if (names->loc_dwo.matches (sectp->name))
13041 {
13042 dwp_file->sections.loc.s.section = sectp;
13043 dwp_file->sections.loc.size = bfd_section_size (sectp);
13044 }
13045 else if (names->macinfo_dwo.matches (sectp->name))
13046 {
13047 dwp_file->sections.macinfo.s.section = sectp;
13048 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13049 }
13050 else if (names->macro_dwo.matches (sectp->name))
13051 {
13052 dwp_file->sections.macro.s.section = sectp;
13053 dwp_file->sections.macro.size = bfd_section_size (sectp);
13054 }
13055 else if (names->str_offsets_dwo.matches (sectp->name))
13056 {
13057 dwp_file->sections.str_offsets.s.section = sectp;
13058 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13059 }
13060 else if (names->types_dwo.matches (sectp->name))
13061 {
13062 dwp_file->sections.types.s.section = sectp;
13063 dwp_file->sections.types.size = bfd_section_size (sectp);
13064 }
13065 }
13066
13067 /* This function is mapped across the sections and remembers the offset and
13068 size of each of the DWP version 5 debugging sections that we are interested
13069 in. This is split into a separate function because we don't know if we
13070 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13071
13072 static void
13073 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13074 {
13075 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13076 const struct dwop_section_names *names = &dwop_section_names;
13077 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13078
13079 /* Record the ELF section number for later lookup: this is what the
13080 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13081 gdb_assert (elf_section_nr < dwp_file->num_sections);
13082 dwp_file->elf_sections[elf_section_nr] = sectp;
13083
13084 /* Look for specific sections that we need. */
13085 if (names->abbrev_dwo.matches (sectp->name))
13086 {
13087 dwp_file->sections.abbrev.s.section = sectp;
13088 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13089 }
13090 else if (names->info_dwo.matches (sectp->name))
13091 {
13092 dwp_file->sections.info.s.section = sectp;
13093 dwp_file->sections.info.size = bfd_section_size (sectp);
13094 }
13095 else if (names->line_dwo.matches (sectp->name))
13096 {
13097 dwp_file->sections.line.s.section = sectp;
13098 dwp_file->sections.line.size = bfd_section_size (sectp);
13099 }
13100 else if (names->loclists_dwo.matches (sectp->name))
13101 {
13102 dwp_file->sections.loclists.s.section = sectp;
13103 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13104 }
13105 else if (names->macro_dwo.matches (sectp->name))
13106 {
13107 dwp_file->sections.macro.s.section = sectp;
13108 dwp_file->sections.macro.size = bfd_section_size (sectp);
13109 }
13110 else if (names->rnglists_dwo.matches (sectp->name))
13111 {
13112 dwp_file->sections.rnglists.s.section = sectp;
13113 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13114 }
13115 else if (names->str_offsets_dwo.matches (sectp->name))
13116 {
13117 dwp_file->sections.str_offsets.s.section = sectp;
13118 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13119 }
13120 }
13121
13122 /* Hash function for dwp_file loaded CUs/TUs. */
13123
13124 static hashval_t
13125 hash_dwp_loaded_cutus (const void *item)
13126 {
13127 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13128
13129 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13130 return dwo_unit->signature;
13131 }
13132
13133 /* Equality function for dwp_file loaded CUs/TUs. */
13134
13135 static int
13136 eq_dwp_loaded_cutus (const void *a, const void *b)
13137 {
13138 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13139 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13140
13141 return dua->signature == dub->signature;
13142 }
13143
13144 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13145
13146 static htab_up
13147 allocate_dwp_loaded_cutus_table ()
13148 {
13149 return htab_up (htab_create_alloc (3,
13150 hash_dwp_loaded_cutus,
13151 eq_dwp_loaded_cutus,
13152 NULL, xcalloc, xfree));
13153 }
13154
13155 /* Try to open DWP file FILE_NAME.
13156 The result is the bfd handle of the file.
13157 If there is a problem finding or opening the file, return NULL.
13158 Upon success, the canonicalized path of the file is stored in the bfd,
13159 same as symfile_bfd_open. */
13160
13161 static gdb_bfd_ref_ptr
13162 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13163 {
13164 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13165 1 /*is_dwp*/,
13166 1 /*search_cwd*/));
13167 if (abfd != NULL)
13168 return abfd;
13169
13170 /* Work around upstream bug 15652.
13171 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13172 [Whether that's a "bug" is debatable, but it is getting in our way.]
13173 We have no real idea where the dwp file is, because gdb's realpath-ing
13174 of the executable's path may have discarded the needed info.
13175 [IWBN if the dwp file name was recorded in the executable, akin to
13176 .gnu_debuglink, but that doesn't exist yet.]
13177 Strip the directory from FILE_NAME and search again. */
13178 if (*debug_file_directory != '\0')
13179 {
13180 /* Don't implicitly search the current directory here.
13181 If the user wants to search "." to handle this case,
13182 it must be added to debug-file-directory. */
13183 return try_open_dwop_file (per_objfile, lbasename (file_name),
13184 1 /*is_dwp*/,
13185 0 /*search_cwd*/);
13186 }
13187
13188 return NULL;
13189 }
13190
13191 /* Initialize the use of the DWP file for the current objfile.
13192 By convention the name of the DWP file is ${objfile}.dwp.
13193 The result is NULL if it can't be found. */
13194
13195 static std::unique_ptr<struct dwp_file>
13196 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13197 {
13198 struct objfile *objfile = per_objfile->objfile;
13199
13200 /* Try to find first .dwp for the binary file before any symbolic links
13201 resolving. */
13202
13203 /* If the objfile is a debug file, find the name of the real binary
13204 file and get the name of dwp file from there. */
13205 std::string dwp_name;
13206 if (objfile->separate_debug_objfile_backlink != NULL)
13207 {
13208 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13209 const char *backlink_basename = lbasename (backlink->original_name);
13210
13211 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13212 }
13213 else
13214 dwp_name = objfile->original_name;
13215
13216 dwp_name += ".dwp";
13217
13218 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13219 if (dbfd == NULL
13220 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13221 {
13222 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13223 dwp_name = objfile_name (objfile);
13224 dwp_name += ".dwp";
13225 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13226 }
13227
13228 if (dbfd == NULL)
13229 {
13230 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13231
13232 return std::unique_ptr<dwp_file> ();
13233 }
13234
13235 const char *name = bfd_get_filename (dbfd.get ());
13236 std::unique_ptr<struct dwp_file> dwp_file
13237 (new struct dwp_file (name, std::move (dbfd)));
13238
13239 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13240 dwp_file->elf_sections =
13241 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13242 dwp_file->num_sections, asection *);
13243
13244 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13245 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13246 dwp_file.get ());
13247
13248 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13249
13250 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13251
13252 /* The DWP file version is stored in the hash table. Oh well. */
13253 if (dwp_file->cus && dwp_file->tus
13254 && dwp_file->cus->version != dwp_file->tus->version)
13255 {
13256 /* Technically speaking, we should try to limp along, but this is
13257 pretty bizarre. We use pulongest here because that's the established
13258 portability solution (e.g, we cannot use %u for uint32_t). */
13259 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13260 " TU version %s [in DWP file %s]"),
13261 pulongest (dwp_file->cus->version),
13262 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13263 }
13264
13265 if (dwp_file->cus)
13266 dwp_file->version = dwp_file->cus->version;
13267 else if (dwp_file->tus)
13268 dwp_file->version = dwp_file->tus->version;
13269 else
13270 dwp_file->version = 2;
13271
13272 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13273 {
13274 if (dwp_file->version == 2)
13275 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13276 dwp_file.get ());
13277 else
13278 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13279 dwp_file.get ());
13280 }
13281
13282 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13283 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13284
13285 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13286 dwarf_read_debug_printf (" %s CUs, %s TUs",
13287 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13288 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13289
13290 return dwp_file;
13291 }
13292
13293 /* Wrapper around open_and_init_dwp_file, only open it once. */
13294
13295 static struct dwp_file *
13296 get_dwp_file (dwarf2_per_objfile *per_objfile)
13297 {
13298 if (!per_objfile->per_bfd->dwp_checked)
13299 {
13300 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13301 per_objfile->per_bfd->dwp_checked = 1;
13302 }
13303 return per_objfile->per_bfd->dwp_file.get ();
13304 }
13305
13306 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13307 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13308 or in the DWP file for the objfile, referenced by THIS_UNIT.
13309 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13310 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13311
13312 This is called, for example, when wanting to read a variable with a
13313 complex location. Therefore we don't want to do file i/o for every call.
13314 Therefore we don't want to look for a DWO file on every call.
13315 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13316 then we check if we've already seen DWO_NAME, and only THEN do we check
13317 for a DWO file.
13318
13319 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13320 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13321
13322 static struct dwo_unit *
13323 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13324 ULONGEST signature, int is_debug_types)
13325 {
13326 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13327 struct objfile *objfile = per_objfile->objfile;
13328 const char *kind = is_debug_types ? "TU" : "CU";
13329 void **dwo_file_slot;
13330 struct dwo_file *dwo_file;
13331 struct dwp_file *dwp_file;
13332
13333 /* First see if there's a DWP file.
13334 If we have a DWP file but didn't find the DWO inside it, don't
13335 look for the original DWO file. It makes gdb behave differently
13336 depending on whether one is debugging in the build tree. */
13337
13338 dwp_file = get_dwp_file (per_objfile);
13339 if (dwp_file != NULL)
13340 {
13341 const struct dwp_hash_table *dwp_htab =
13342 is_debug_types ? dwp_file->tus : dwp_file->cus;
13343
13344 if (dwp_htab != NULL)
13345 {
13346 struct dwo_unit *dwo_cutu =
13347 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13348 is_debug_types);
13349
13350 if (dwo_cutu != NULL)
13351 {
13352 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13353 kind, hex_string (signature),
13354 host_address_to_string (dwo_cutu));
13355
13356 return dwo_cutu;
13357 }
13358 }
13359 }
13360 else
13361 {
13362 /* No DWP file, look for the DWO file. */
13363
13364 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13365 if (*dwo_file_slot == NULL)
13366 {
13367 /* Read in the file and build a table of the CUs/TUs it contains. */
13368 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13369 }
13370 /* NOTE: This will be NULL if unable to open the file. */
13371 dwo_file = (struct dwo_file *) *dwo_file_slot;
13372
13373 if (dwo_file != NULL)
13374 {
13375 struct dwo_unit *dwo_cutu = NULL;
13376
13377 if (is_debug_types && dwo_file->tus)
13378 {
13379 struct dwo_unit find_dwo_cutu;
13380
13381 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13382 find_dwo_cutu.signature = signature;
13383 dwo_cutu
13384 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13385 &find_dwo_cutu);
13386 }
13387 else if (!is_debug_types && dwo_file->cus)
13388 {
13389 struct dwo_unit find_dwo_cutu;
13390
13391 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13392 find_dwo_cutu.signature = signature;
13393 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13394 &find_dwo_cutu);
13395 }
13396
13397 if (dwo_cutu != NULL)
13398 {
13399 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13400 kind, dwo_name, hex_string (signature),
13401 host_address_to_string (dwo_cutu));
13402
13403 return dwo_cutu;
13404 }
13405 }
13406 }
13407
13408 /* We didn't find it. This could mean a dwo_id mismatch, or
13409 someone deleted the DWO/DWP file, or the search path isn't set up
13410 correctly to find the file. */
13411
13412 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13413 kind, dwo_name, hex_string (signature));
13414
13415 /* This is a warning and not a complaint because it can be caused by
13416 pilot error (e.g., user accidentally deleting the DWO). */
13417 {
13418 /* Print the name of the DWP file if we looked there, helps the user
13419 better diagnose the problem. */
13420 std::string dwp_text;
13421
13422 if (dwp_file != NULL)
13423 dwp_text = string_printf (" [in DWP file %s]",
13424 lbasename (dwp_file->name));
13425
13426 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13427 " [in module %s]"),
13428 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13429 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13430 }
13431 return NULL;
13432 }
13433
13434 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13435 See lookup_dwo_cutu_unit for details. */
13436
13437 static struct dwo_unit *
13438 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13439 ULONGEST signature)
13440 {
13441 gdb_assert (!cu->per_cu->is_debug_types);
13442
13443 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13444 }
13445
13446 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13447 See lookup_dwo_cutu_unit for details. */
13448
13449 static struct dwo_unit *
13450 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13451 {
13452 gdb_assert (cu->per_cu->is_debug_types);
13453
13454 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13455
13456 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13457 }
13458
13459 /* Traversal function for queue_and_load_all_dwo_tus. */
13460
13461 static int
13462 queue_and_load_dwo_tu (void **slot, void *info)
13463 {
13464 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13465 dwarf2_cu *cu = (dwarf2_cu *) info;
13466 ULONGEST signature = dwo_unit->signature;
13467 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13468
13469 if (sig_type != NULL)
13470 {
13471 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13472
13473 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13474 a real dependency of PER_CU on SIG_TYPE. That is detected later
13475 while processing PER_CU. */
13476 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13477 load_full_type_unit (sig_cu, cu->per_objfile);
13478 cu->per_cu->imported_symtabs_push (sig_cu);
13479 }
13480
13481 return 1;
13482 }
13483
13484 /* Queue all TUs contained in the DWO of CU to be read in.
13485 The DWO may have the only definition of the type, though it may not be
13486 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13487 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13488
13489 static void
13490 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13491 {
13492 struct dwo_unit *dwo_unit;
13493 struct dwo_file *dwo_file;
13494
13495 gdb_assert (cu != nullptr);
13496 gdb_assert (!cu->per_cu->is_debug_types);
13497 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13498
13499 dwo_unit = cu->dwo_unit;
13500 gdb_assert (dwo_unit != NULL);
13501
13502 dwo_file = dwo_unit->dwo_file;
13503 if (dwo_file->tus != NULL)
13504 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13505 }
13506
13507 /* Read in various DIEs. */
13508
13509 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13510 Inherit only the children of the DW_AT_abstract_origin DIE not being
13511 already referenced by DW_AT_abstract_origin from the children of the
13512 current DIE. */
13513
13514 static void
13515 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13516 {
13517 struct die_info *child_die;
13518 sect_offset *offsetp;
13519 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13520 struct die_info *origin_die;
13521 /* Iterator of the ORIGIN_DIE children. */
13522 struct die_info *origin_child_die;
13523 struct attribute *attr;
13524 struct dwarf2_cu *origin_cu;
13525 struct pending **origin_previous_list_in_scope;
13526
13527 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13528 if (!attr)
13529 return;
13530
13531 /* Note that following die references may follow to a die in a
13532 different cu. */
13533
13534 origin_cu = cu;
13535 origin_die = follow_die_ref (die, attr, &origin_cu);
13536
13537 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13538 symbols in. */
13539 origin_previous_list_in_scope = origin_cu->list_in_scope;
13540 origin_cu->list_in_scope = cu->list_in_scope;
13541
13542 if (die->tag != origin_die->tag
13543 && !(die->tag == DW_TAG_inlined_subroutine
13544 && origin_die->tag == DW_TAG_subprogram))
13545 complaint (_("DIE %s and its abstract origin %s have different tags"),
13546 sect_offset_str (die->sect_off),
13547 sect_offset_str (origin_die->sect_off));
13548
13549 /* Find if the concrete and abstract trees are structurally the
13550 same. This is a shallow traversal and it is not bullet-proof;
13551 the compiler can trick the debugger into believing that the trees
13552 are isomorphic, whereas they actually are not. However, the
13553 likelyhood of this happening is pretty low, and a full-fledged
13554 check would be an overkill. */
13555 bool are_isomorphic = true;
13556 die_info *concrete_child = die->child;
13557 die_info *abstract_child = origin_die->child;
13558 while (concrete_child != nullptr || abstract_child != nullptr)
13559 {
13560 if (concrete_child == nullptr
13561 || abstract_child == nullptr
13562 || concrete_child->tag != abstract_child->tag)
13563 {
13564 are_isomorphic = false;
13565 break;
13566 }
13567
13568 concrete_child = concrete_child->sibling;
13569 abstract_child = abstract_child->sibling;
13570 }
13571
13572 /* Walk the origin's children in parallel to the concrete children.
13573 This helps match an origin child in case the debug info misses
13574 DW_AT_abstract_origin attributes. Keep in mind that the abstract
13575 origin tree may not have the same tree structure as the concrete
13576 DIE, though. */
13577 die_info *corresponding_abstract_child
13578 = are_isomorphic ? origin_die->child : nullptr;
13579
13580 std::vector<sect_offset> offsets;
13581
13582 for (child_die = die->child;
13583 child_die && child_die->tag;
13584 child_die = child_die->sibling)
13585 {
13586 struct die_info *child_origin_die;
13587 struct dwarf2_cu *child_origin_cu;
13588
13589 /* We are trying to process concrete instance entries:
13590 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13591 it's not relevant to our analysis here. i.e. detecting DIEs that are
13592 present in the abstract instance but not referenced in the concrete
13593 one. */
13594 if (child_die->tag == DW_TAG_call_site
13595 || child_die->tag == DW_TAG_GNU_call_site)
13596 {
13597 if (are_isomorphic)
13598 corresponding_abstract_child
13599 = corresponding_abstract_child->sibling;
13600 continue;
13601 }
13602
13603 /* For each CHILD_DIE, find the corresponding child of
13604 ORIGIN_DIE. If there is more than one layer of
13605 DW_AT_abstract_origin, follow them all; there shouldn't be,
13606 but GCC versions at least through 4.4 generate this (GCC PR
13607 40573). */
13608 child_origin_die = child_die;
13609 child_origin_cu = cu;
13610 while (1)
13611 {
13612 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13613 child_origin_cu);
13614 if (attr == NULL)
13615 break;
13616 child_origin_die = follow_die_ref (child_origin_die, attr,
13617 &child_origin_cu);
13618 }
13619
13620 /* If missing DW_AT_abstract_origin, try the corresponding child
13621 of the origin. Clang emits such lexical scopes. */
13622 if (child_origin_die == child_die
13623 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13624 && are_isomorphic
13625 && child_die->tag == DW_TAG_lexical_block)
13626 child_origin_die = corresponding_abstract_child;
13627
13628 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13629 counterpart may exist. */
13630 if (child_origin_die != child_die)
13631 {
13632 if (child_die->tag != child_origin_die->tag
13633 && !(child_die->tag == DW_TAG_inlined_subroutine
13634 && child_origin_die->tag == DW_TAG_subprogram))
13635 complaint (_("Child DIE %s and its abstract origin %s have "
13636 "different tags"),
13637 sect_offset_str (child_die->sect_off),
13638 sect_offset_str (child_origin_die->sect_off));
13639 if (child_origin_die->parent != origin_die)
13640 complaint (_("Child DIE %s and its abstract origin %s have "
13641 "different parents"),
13642 sect_offset_str (child_die->sect_off),
13643 sect_offset_str (child_origin_die->sect_off));
13644 else
13645 offsets.push_back (child_origin_die->sect_off);
13646 }
13647
13648 if (are_isomorphic)
13649 corresponding_abstract_child = corresponding_abstract_child->sibling;
13650 }
13651 std::sort (offsets.begin (), offsets.end ());
13652 sect_offset *offsets_end = offsets.data () + offsets.size ();
13653 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13654 if (offsetp[-1] == *offsetp)
13655 complaint (_("Multiple children of DIE %s refer "
13656 "to DIE %s as their abstract origin"),
13657 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13658
13659 offsetp = offsets.data ();
13660 origin_child_die = origin_die->child;
13661 while (origin_child_die && origin_child_die->tag)
13662 {
13663 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13664 while (offsetp < offsets_end
13665 && *offsetp < origin_child_die->sect_off)
13666 offsetp++;
13667 if (offsetp >= offsets_end
13668 || *offsetp > origin_child_die->sect_off)
13669 {
13670 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13671 Check whether we're already processing ORIGIN_CHILD_DIE.
13672 This can happen with mutually referenced abstract_origins.
13673 PR 16581. */
13674 if (!origin_child_die->in_process)
13675 process_die (origin_child_die, origin_cu);
13676 }
13677 origin_child_die = origin_child_die->sibling;
13678 }
13679 origin_cu->list_in_scope = origin_previous_list_in_scope;
13680
13681 if (cu != origin_cu)
13682 compute_delayed_physnames (origin_cu);
13683 }
13684
13685 static void
13686 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13687 {
13688 struct objfile *objfile = cu->per_objfile->objfile;
13689 struct gdbarch *gdbarch = objfile->arch ();
13690 struct context_stack *newobj;
13691 CORE_ADDR lowpc;
13692 CORE_ADDR highpc;
13693 struct die_info *child_die;
13694 struct attribute *attr, *call_line, *call_file;
13695 const char *name;
13696 CORE_ADDR baseaddr;
13697 struct block *block;
13698 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13699 std::vector<struct symbol *> template_args;
13700 struct template_symbol *templ_func = NULL;
13701
13702 if (inlined_func)
13703 {
13704 /* If we do not have call site information, we can't show the
13705 caller of this inlined function. That's too confusing, so
13706 only use the scope for local variables. */
13707 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13708 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13709 if (call_line == NULL || call_file == NULL)
13710 {
13711 read_lexical_block_scope (die, cu);
13712 return;
13713 }
13714 }
13715
13716 baseaddr = objfile->text_section_offset ();
13717
13718 name = dwarf2_name (die, cu);
13719
13720 /* Ignore functions with missing or empty names. These are actually
13721 illegal according to the DWARF standard. */
13722 if (name == NULL)
13723 {
13724 complaint (_("missing name for subprogram DIE at %s"),
13725 sect_offset_str (die->sect_off));
13726 return;
13727 }
13728
13729 /* Ignore functions with missing or invalid low and high pc attributes. */
13730 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13731 <= PC_BOUNDS_INVALID)
13732 {
13733 attr = dwarf2_attr (die, DW_AT_external, cu);
13734 if (attr == nullptr || !attr->as_boolean ())
13735 complaint (_("cannot get low and high bounds "
13736 "for subprogram DIE at %s"),
13737 sect_offset_str (die->sect_off));
13738 return;
13739 }
13740
13741 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13742 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13743
13744 /* If we have any template arguments, then we must allocate a
13745 different sort of symbol. */
13746 for (child_die = die->child; child_die; child_die = child_die->sibling)
13747 {
13748 if (child_die->tag == DW_TAG_template_type_param
13749 || child_die->tag == DW_TAG_template_value_param)
13750 {
13751 templ_func = new (&objfile->objfile_obstack) template_symbol;
13752 templ_func->subclass = SYMBOL_TEMPLATE;
13753 break;
13754 }
13755 }
13756
13757 gdb_assert (cu->get_builder () != nullptr);
13758 newobj = cu->get_builder ()->push_context (0, lowpc);
13759 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13760 (struct symbol *) templ_func);
13761
13762 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13763 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13764 cu->language);
13765
13766 /* If there is a location expression for DW_AT_frame_base, record
13767 it. */
13768 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13769 if (attr != nullptr)
13770 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13771
13772 /* If there is a location for the static link, record it. */
13773 newobj->static_link = NULL;
13774 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13775 if (attr != nullptr)
13776 {
13777 newobj->static_link
13778 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13779 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13780 cu->addr_type ());
13781 }
13782
13783 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13784
13785 if (die->child != NULL)
13786 {
13787 child_die = die->child;
13788 while (child_die && child_die->tag)
13789 {
13790 if (child_die->tag == DW_TAG_template_type_param
13791 || child_die->tag == DW_TAG_template_value_param)
13792 {
13793 struct symbol *arg = new_symbol (child_die, NULL, cu);
13794
13795 if (arg != NULL)
13796 template_args.push_back (arg);
13797 }
13798 else
13799 process_die (child_die, cu);
13800 child_die = child_die->sibling;
13801 }
13802 }
13803
13804 inherit_abstract_dies (die, cu);
13805
13806 /* If we have a DW_AT_specification, we might need to import using
13807 directives from the context of the specification DIE. See the
13808 comment in determine_prefix. */
13809 if (cu->language == language_cplus
13810 && dwarf2_attr (die, DW_AT_specification, cu))
13811 {
13812 struct dwarf2_cu *spec_cu = cu;
13813 struct die_info *spec_die = die_specification (die, &spec_cu);
13814
13815 while (spec_die)
13816 {
13817 child_die = spec_die->child;
13818 while (child_die && child_die->tag)
13819 {
13820 if (child_die->tag == DW_TAG_imported_module)
13821 process_die (child_die, spec_cu);
13822 child_die = child_die->sibling;
13823 }
13824
13825 /* In some cases, GCC generates specification DIEs that
13826 themselves contain DW_AT_specification attributes. */
13827 spec_die = die_specification (spec_die, &spec_cu);
13828 }
13829 }
13830
13831 struct context_stack cstk = cu->get_builder ()->pop_context ();
13832 /* Make a block for the local symbols within. */
13833 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13834 cstk.static_link, lowpc, highpc);
13835
13836 /* For C++, set the block's scope. */
13837 if ((cu->language == language_cplus
13838 || cu->language == language_fortran
13839 || cu->language == language_d
13840 || cu->language == language_rust)
13841 && cu->processing_has_namespace_info)
13842 block_set_scope (block, determine_prefix (die, cu),
13843 &objfile->objfile_obstack);
13844
13845 /* If we have address ranges, record them. */
13846 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13847
13848 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13849
13850 /* Attach template arguments to function. */
13851 if (!template_args.empty ())
13852 {
13853 gdb_assert (templ_func != NULL);
13854
13855 templ_func->n_template_arguments = template_args.size ();
13856 templ_func->template_arguments
13857 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13858 templ_func->n_template_arguments);
13859 memcpy (templ_func->template_arguments,
13860 template_args.data (),
13861 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13862
13863 /* Make sure that the symtab is set on the new symbols. Even
13864 though they don't appear in this symtab directly, other parts
13865 of gdb assume that symbols do, and this is reasonably
13866 true. */
13867 for (symbol *sym : template_args)
13868 symbol_set_symtab (sym, symbol_symtab (templ_func));
13869 }
13870
13871 /* In C++, we can have functions nested inside functions (e.g., when
13872 a function declares a class that has methods). This means that
13873 when we finish processing a function scope, we may need to go
13874 back to building a containing block's symbol lists. */
13875 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13876 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13877
13878 /* If we've finished processing a top-level function, subsequent
13879 symbols go in the file symbol list. */
13880 if (cu->get_builder ()->outermost_context_p ())
13881 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13882 }
13883
13884 /* Process all the DIES contained within a lexical block scope. Start
13885 a new scope, process the dies, and then close the scope. */
13886
13887 static void
13888 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13889 {
13890 struct objfile *objfile = cu->per_objfile->objfile;
13891 struct gdbarch *gdbarch = objfile->arch ();
13892 CORE_ADDR lowpc, highpc;
13893 struct die_info *child_die;
13894 CORE_ADDR baseaddr;
13895
13896 baseaddr = objfile->text_section_offset ();
13897
13898 /* Ignore blocks with missing or invalid low and high pc attributes. */
13899 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13900 as multiple lexical blocks? Handling children in a sane way would
13901 be nasty. Might be easier to properly extend generic blocks to
13902 describe ranges. */
13903 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13904 {
13905 case PC_BOUNDS_NOT_PRESENT:
13906 /* DW_TAG_lexical_block has no attributes, process its children as if
13907 there was no wrapping by that DW_TAG_lexical_block.
13908 GCC does no longer produces such DWARF since GCC r224161. */
13909 for (child_die = die->child;
13910 child_die != NULL && child_die->tag;
13911 child_die = child_die->sibling)
13912 {
13913 /* We might already be processing this DIE. This can happen
13914 in an unusual circumstance -- where a subroutine A
13915 appears lexically in another subroutine B, but A actually
13916 inlines B. The recursion is broken here, rather than in
13917 inherit_abstract_dies, because it seems better to simply
13918 drop concrete children here. */
13919 if (!child_die->in_process)
13920 process_die (child_die, cu);
13921 }
13922 return;
13923 case PC_BOUNDS_INVALID:
13924 return;
13925 }
13926 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13927 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13928
13929 cu->get_builder ()->push_context (0, lowpc);
13930 if (die->child != NULL)
13931 {
13932 child_die = die->child;
13933 while (child_die && child_die->tag)
13934 {
13935 process_die (child_die, cu);
13936 child_die = child_die->sibling;
13937 }
13938 }
13939 inherit_abstract_dies (die, cu);
13940 struct context_stack cstk = cu->get_builder ()->pop_context ();
13941
13942 if (*cu->get_builder ()->get_local_symbols () != NULL
13943 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13944 {
13945 struct block *block
13946 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13947 cstk.start_addr, highpc);
13948
13949 /* Note that recording ranges after traversing children, as we
13950 do here, means that recording a parent's ranges entails
13951 walking across all its children's ranges as they appear in
13952 the address map, which is quadratic behavior.
13953
13954 It would be nicer to record the parent's ranges before
13955 traversing its children, simply overriding whatever you find
13956 there. But since we don't even decide whether to create a
13957 block until after we've traversed its children, that's hard
13958 to do. */
13959 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13960 }
13961 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13962 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13963 }
13964
13965 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13966
13967 static void
13968 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13969 {
13970 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13971 struct objfile *objfile = per_objfile->objfile;
13972 struct gdbarch *gdbarch = objfile->arch ();
13973 CORE_ADDR pc, baseaddr;
13974 struct attribute *attr;
13975 struct call_site *call_site, call_site_local;
13976 void **slot;
13977 int nparams;
13978 struct die_info *child_die;
13979
13980 baseaddr = objfile->text_section_offset ();
13981
13982 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13983 if (attr == NULL)
13984 {
13985 /* This was a pre-DWARF-5 GNU extension alias
13986 for DW_AT_call_return_pc. */
13987 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13988 }
13989 if (!attr)
13990 {
13991 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13992 "DIE %s [in module %s]"),
13993 sect_offset_str (die->sect_off), objfile_name (objfile));
13994 return;
13995 }
13996 pc = attr->as_address () + baseaddr;
13997 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13998
13999 if (cu->call_site_htab == NULL)
14000 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14001 NULL, &objfile->objfile_obstack,
14002 hashtab_obstack_allocate, NULL);
14003 call_site_local.pc = pc;
14004 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14005 if (*slot != NULL)
14006 {
14007 complaint (_("Duplicate PC %s for DW_TAG_call_site "
14008 "DIE %s [in module %s]"),
14009 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14010 objfile_name (objfile));
14011 return;
14012 }
14013
14014 /* Count parameters at the caller. */
14015
14016 nparams = 0;
14017 for (child_die = die->child; child_die && child_die->tag;
14018 child_die = child_die->sibling)
14019 {
14020 if (child_die->tag != DW_TAG_call_site_parameter
14021 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14022 {
14023 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
14024 "DW_TAG_call_site child DIE %s [in module %s]"),
14025 child_die->tag, sect_offset_str (child_die->sect_off),
14026 objfile_name (objfile));
14027 continue;
14028 }
14029
14030 nparams++;
14031 }
14032
14033 call_site
14034 = ((struct call_site *)
14035 obstack_alloc (&objfile->objfile_obstack,
14036 sizeof (*call_site)
14037 + (sizeof (*call_site->parameter) * (nparams - 1))));
14038 *slot = call_site;
14039 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14040 call_site->pc = pc;
14041
14042 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14043 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14044 {
14045 struct die_info *func_die;
14046
14047 /* Skip also over DW_TAG_inlined_subroutine. */
14048 for (func_die = die->parent;
14049 func_die && func_die->tag != DW_TAG_subprogram
14050 && func_die->tag != DW_TAG_subroutine_type;
14051 func_die = func_die->parent);
14052
14053 /* DW_AT_call_all_calls is a superset
14054 of DW_AT_call_all_tail_calls. */
14055 if (func_die
14056 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14057 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14058 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14059 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14060 {
14061 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14062 not complete. But keep CALL_SITE for look ups via call_site_htab,
14063 both the initial caller containing the real return address PC and
14064 the final callee containing the current PC of a chain of tail
14065 calls do not need to have the tail call list complete. But any
14066 function candidate for a virtual tail call frame searched via
14067 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14068 determined unambiguously. */
14069 }
14070 else
14071 {
14072 struct type *func_type = NULL;
14073
14074 if (func_die)
14075 func_type = get_die_type (func_die, cu);
14076 if (func_type != NULL)
14077 {
14078 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
14079
14080 /* Enlist this call site to the function. */
14081 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14082 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14083 }
14084 else
14085 complaint (_("Cannot find function owning DW_TAG_call_site "
14086 "DIE %s [in module %s]"),
14087 sect_offset_str (die->sect_off), objfile_name (objfile));
14088 }
14089 }
14090
14091 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14092 if (attr == NULL)
14093 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14094 if (attr == NULL)
14095 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14096 if (attr == NULL)
14097 {
14098 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14099 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14100 }
14101 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14102 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
14103 /* Keep NULL DWARF_BLOCK. */;
14104 else if (attr->form_is_block ())
14105 {
14106 struct dwarf2_locexpr_baton *dlbaton;
14107 struct dwarf_block *block = attr->as_block ();
14108
14109 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14110 dlbaton->data = block->data;
14111 dlbaton->size = block->size;
14112 dlbaton->per_objfile = per_objfile;
14113 dlbaton->per_cu = cu->per_cu;
14114
14115 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14116 }
14117 else if (attr->form_is_ref ())
14118 {
14119 struct dwarf2_cu *target_cu = cu;
14120 struct die_info *target_die;
14121
14122 target_die = follow_die_ref (die, attr, &target_cu);
14123 gdb_assert (target_cu->per_objfile->objfile == objfile);
14124 if (die_is_declaration (target_die, target_cu))
14125 {
14126 const char *target_physname;
14127
14128 /* Prefer the mangled name; otherwise compute the demangled one. */
14129 target_physname = dw2_linkage_name (target_die, target_cu);
14130 if (target_physname == NULL)
14131 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14132 if (target_physname == NULL)
14133 complaint (_("DW_AT_call_target target DIE has invalid "
14134 "physname, for referencing DIE %s [in module %s]"),
14135 sect_offset_str (die->sect_off), objfile_name (objfile));
14136 else
14137 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14138 }
14139 else
14140 {
14141 CORE_ADDR lowpc;
14142
14143 /* DW_AT_entry_pc should be preferred. */
14144 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14145 <= PC_BOUNDS_INVALID)
14146 complaint (_("DW_AT_call_target target DIE has invalid "
14147 "low pc, for referencing DIE %s [in module %s]"),
14148 sect_offset_str (die->sect_off), objfile_name (objfile));
14149 else
14150 {
14151 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14152 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14153 }
14154 }
14155 }
14156 else
14157 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14158 "block nor reference, for DIE %s [in module %s]"),
14159 sect_offset_str (die->sect_off), objfile_name (objfile));
14160
14161 call_site->per_cu = cu->per_cu;
14162 call_site->per_objfile = per_objfile;
14163
14164 for (child_die = die->child;
14165 child_die && child_die->tag;
14166 child_die = child_die->sibling)
14167 {
14168 struct call_site_parameter *parameter;
14169 struct attribute *loc, *origin;
14170
14171 if (child_die->tag != DW_TAG_call_site_parameter
14172 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14173 {
14174 /* Already printed the complaint above. */
14175 continue;
14176 }
14177
14178 gdb_assert (call_site->parameter_count < nparams);
14179 parameter = &call_site->parameter[call_site->parameter_count];
14180
14181 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14182 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14183 register is contained in DW_AT_call_value. */
14184
14185 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14186 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14187 if (origin == NULL)
14188 {
14189 /* This was a pre-DWARF-5 GNU extension alias
14190 for DW_AT_call_parameter. */
14191 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14192 }
14193 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14194 {
14195 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14196
14197 sect_offset sect_off = origin->get_ref_die_offset ();
14198 if (!cu->header.offset_in_cu_p (sect_off))
14199 {
14200 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14201 binding can be done only inside one CU. Such referenced DIE
14202 therefore cannot be even moved to DW_TAG_partial_unit. */
14203 complaint (_("DW_AT_call_parameter offset is not in CU for "
14204 "DW_TAG_call_site child DIE %s [in module %s]"),
14205 sect_offset_str (child_die->sect_off),
14206 objfile_name (objfile));
14207 continue;
14208 }
14209 parameter->u.param_cu_off
14210 = (cu_offset) (sect_off - cu->header.sect_off);
14211 }
14212 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14213 {
14214 complaint (_("No DW_FORM_block* DW_AT_location for "
14215 "DW_TAG_call_site child DIE %s [in module %s]"),
14216 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14217 continue;
14218 }
14219 else
14220 {
14221 struct dwarf_block *block = loc->as_block ();
14222
14223 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14224 (block->data, &block->data[block->size]);
14225 if (parameter->u.dwarf_reg != -1)
14226 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14227 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14228 &block->data[block->size],
14229 &parameter->u.fb_offset))
14230 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14231 else
14232 {
14233 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14234 "for DW_FORM_block* DW_AT_location is supported for "
14235 "DW_TAG_call_site child DIE %s "
14236 "[in module %s]"),
14237 sect_offset_str (child_die->sect_off),
14238 objfile_name (objfile));
14239 continue;
14240 }
14241 }
14242
14243 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14244 if (attr == NULL)
14245 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14246 if (attr == NULL || !attr->form_is_block ())
14247 {
14248 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14249 "DW_TAG_call_site child DIE %s [in module %s]"),
14250 sect_offset_str (child_die->sect_off),
14251 objfile_name (objfile));
14252 continue;
14253 }
14254
14255 struct dwarf_block *block = attr->as_block ();
14256 parameter->value = block->data;
14257 parameter->value_size = block->size;
14258
14259 /* Parameters are not pre-cleared by memset above. */
14260 parameter->data_value = NULL;
14261 parameter->data_value_size = 0;
14262 call_site->parameter_count++;
14263
14264 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14265 if (attr == NULL)
14266 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14267 if (attr != nullptr)
14268 {
14269 if (!attr->form_is_block ())
14270 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14271 "DW_TAG_call_site child DIE %s [in module %s]"),
14272 sect_offset_str (child_die->sect_off),
14273 objfile_name (objfile));
14274 else
14275 {
14276 block = attr->as_block ();
14277 parameter->data_value = block->data;
14278 parameter->data_value_size = block->size;
14279 }
14280 }
14281 }
14282 }
14283
14284 /* Helper function for read_variable. If DIE represents a virtual
14285 table, then return the type of the concrete object that is
14286 associated with the virtual table. Otherwise, return NULL. */
14287
14288 static struct type *
14289 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14290 {
14291 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14292 if (attr == NULL)
14293 return NULL;
14294
14295 /* Find the type DIE. */
14296 struct die_info *type_die = NULL;
14297 struct dwarf2_cu *type_cu = cu;
14298
14299 if (attr->form_is_ref ())
14300 type_die = follow_die_ref (die, attr, &type_cu);
14301 if (type_die == NULL)
14302 return NULL;
14303
14304 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14305 return NULL;
14306 return die_containing_type (type_die, type_cu);
14307 }
14308
14309 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14310
14311 static void
14312 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14313 {
14314 struct rust_vtable_symbol *storage = NULL;
14315
14316 if (cu->language == language_rust)
14317 {
14318 struct type *containing_type = rust_containing_type (die, cu);
14319
14320 if (containing_type != NULL)
14321 {
14322 struct objfile *objfile = cu->per_objfile->objfile;
14323
14324 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14325 storage->concrete_type = containing_type;
14326 storage->subclass = SYMBOL_RUST_VTABLE;
14327 }
14328 }
14329
14330 struct symbol *res = new_symbol (die, NULL, cu, storage);
14331 struct attribute *abstract_origin
14332 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14333 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14334 if (res == NULL && loc && abstract_origin)
14335 {
14336 /* We have a variable without a name, but with a location and an abstract
14337 origin. This may be a concrete instance of an abstract variable
14338 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14339 later. */
14340 struct dwarf2_cu *origin_cu = cu;
14341 struct die_info *origin_die
14342 = follow_die_ref (die, abstract_origin, &origin_cu);
14343 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14344 per_objfile->per_bfd->abstract_to_concrete
14345 [origin_die->sect_off].push_back (die->sect_off);
14346 }
14347 }
14348
14349 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14350 reading .debug_rnglists.
14351 Callback's type should be:
14352 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14353 Return true if the attributes are present and valid, otherwise,
14354 return false. */
14355
14356 template <typename Callback>
14357 static bool
14358 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14359 dwarf_tag tag, Callback &&callback)
14360 {
14361 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14362 struct objfile *objfile = per_objfile->objfile;
14363 bfd *obfd = objfile->obfd;
14364 /* Base address selection entry. */
14365 gdb::optional<CORE_ADDR> base;
14366 const gdb_byte *buffer;
14367 CORE_ADDR baseaddr;
14368 bool overflow = false;
14369 ULONGEST addr_index;
14370 struct dwarf2_section_info *rnglists_section;
14371
14372 base = cu->base_address;
14373 rnglists_section = cu_debug_rnglists_section (cu, tag);
14374 rnglists_section->read (objfile);
14375
14376 if (offset >= rnglists_section->size)
14377 {
14378 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14379 offset);
14380 return false;
14381 }
14382 buffer = rnglists_section->buffer + offset;
14383
14384 baseaddr = objfile->text_section_offset ();
14385
14386 while (1)
14387 {
14388 /* Initialize it due to a false compiler warning. */
14389 CORE_ADDR range_beginning = 0, range_end = 0;
14390 const gdb_byte *buf_end = (rnglists_section->buffer
14391 + rnglists_section->size);
14392 unsigned int bytes_read;
14393
14394 if (buffer == buf_end)
14395 {
14396 overflow = true;
14397 break;
14398 }
14399 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14400 switch (rlet)
14401 {
14402 case DW_RLE_end_of_list:
14403 break;
14404 case DW_RLE_base_address:
14405 if (buffer + cu->header.addr_size > buf_end)
14406 {
14407 overflow = true;
14408 break;
14409 }
14410 base = cu->header.read_address (obfd, buffer, &bytes_read);
14411 buffer += bytes_read;
14412 break;
14413 case DW_RLE_base_addressx:
14414 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14415 buffer += bytes_read;
14416 base = read_addr_index (cu, addr_index);
14417 break;
14418 case DW_RLE_start_length:
14419 if (buffer + cu->header.addr_size > buf_end)
14420 {
14421 overflow = true;
14422 break;
14423 }
14424 range_beginning = cu->header.read_address (obfd, buffer,
14425 &bytes_read);
14426 buffer += bytes_read;
14427 range_end = (range_beginning
14428 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14429 buffer += bytes_read;
14430 if (buffer > buf_end)
14431 {
14432 overflow = true;
14433 break;
14434 }
14435 break;
14436 case DW_RLE_startx_length:
14437 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14438 buffer += bytes_read;
14439 range_beginning = read_addr_index (cu, addr_index);
14440 if (buffer > buf_end)
14441 {
14442 overflow = true;
14443 break;
14444 }
14445 range_end = (range_beginning
14446 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14447 buffer += bytes_read;
14448 break;
14449 case DW_RLE_offset_pair:
14450 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14451 buffer += bytes_read;
14452 if (buffer > buf_end)
14453 {
14454 overflow = true;
14455 break;
14456 }
14457 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14458 buffer += bytes_read;
14459 if (buffer > buf_end)
14460 {
14461 overflow = true;
14462 break;
14463 }
14464 break;
14465 case DW_RLE_start_end:
14466 if (buffer + 2 * cu->header.addr_size > buf_end)
14467 {
14468 overflow = true;
14469 break;
14470 }
14471 range_beginning = cu->header.read_address (obfd, buffer,
14472 &bytes_read);
14473 buffer += bytes_read;
14474 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14475 buffer += bytes_read;
14476 break;
14477 case DW_RLE_startx_endx:
14478 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14479 buffer += bytes_read;
14480 range_beginning = read_addr_index (cu, addr_index);
14481 if (buffer > buf_end)
14482 {
14483 overflow = true;
14484 break;
14485 }
14486 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14487 buffer += bytes_read;
14488 range_end = read_addr_index (cu, addr_index);
14489 break;
14490 default:
14491 complaint (_("Invalid .debug_rnglists data (no base address)"));
14492 return false;
14493 }
14494 if (rlet == DW_RLE_end_of_list || overflow)
14495 break;
14496 if (rlet == DW_RLE_base_address)
14497 continue;
14498
14499 if (range_beginning > range_end)
14500 {
14501 /* Inverted range entries are invalid. */
14502 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14503 return false;
14504 }
14505
14506 /* Empty range entries have no effect. */
14507 if (range_beginning == range_end)
14508 continue;
14509
14510 /* Only DW_RLE_offset_pair needs the base address added. */
14511 if (rlet == DW_RLE_offset_pair)
14512 {
14513 if (!base.has_value ())
14514 {
14515 /* We have no valid base address for the DW_RLE_offset_pair. */
14516 complaint (_("Invalid .debug_rnglists data (no base address for "
14517 "DW_RLE_offset_pair)"));
14518 return false;
14519 }
14520
14521 range_beginning += *base;
14522 range_end += *base;
14523 }
14524
14525 /* A not-uncommon case of bad debug info.
14526 Don't pollute the addrmap with bad data. */
14527 if (range_beginning + baseaddr == 0
14528 && !per_objfile->per_bfd->has_section_at_zero)
14529 {
14530 complaint (_(".debug_rnglists entry has start address of zero"
14531 " [in module %s]"), objfile_name (objfile));
14532 continue;
14533 }
14534
14535 callback (range_beginning, range_end);
14536 }
14537
14538 if (overflow)
14539 {
14540 complaint (_("Offset %d is not terminated "
14541 "for DW_AT_ranges attribute"),
14542 offset);
14543 return false;
14544 }
14545
14546 return true;
14547 }
14548
14549 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14550 Callback's type should be:
14551 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14552 Return 1 if the attributes are present and valid, otherwise, return 0. */
14553
14554 template <typename Callback>
14555 static int
14556 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14557 Callback &&callback)
14558 {
14559 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14560 struct objfile *objfile = per_objfile->objfile;
14561 struct comp_unit_head *cu_header = &cu->header;
14562 bfd *obfd = objfile->obfd;
14563 unsigned int addr_size = cu_header->addr_size;
14564 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14565 /* Base address selection entry. */
14566 gdb::optional<CORE_ADDR> base;
14567 unsigned int dummy;
14568 const gdb_byte *buffer;
14569 CORE_ADDR baseaddr;
14570
14571 if (cu_header->version >= 5)
14572 return dwarf2_rnglists_process (offset, cu, tag, callback);
14573
14574 base = cu->base_address;
14575
14576 per_objfile->per_bfd->ranges.read (objfile);
14577 if (offset >= per_objfile->per_bfd->ranges.size)
14578 {
14579 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14580 offset);
14581 return 0;
14582 }
14583 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14584
14585 baseaddr = objfile->text_section_offset ();
14586
14587 while (1)
14588 {
14589 CORE_ADDR range_beginning, range_end;
14590
14591 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14592 buffer += addr_size;
14593 range_end = cu->header.read_address (obfd, buffer, &dummy);
14594 buffer += addr_size;
14595 offset += 2 * addr_size;
14596
14597 /* An end of list marker is a pair of zero addresses. */
14598 if (range_beginning == 0 && range_end == 0)
14599 /* Found the end of list entry. */
14600 break;
14601
14602 /* Each base address selection entry is a pair of 2 values.
14603 The first is the largest possible address, the second is
14604 the base address. Check for a base address here. */
14605 if ((range_beginning & mask) == mask)
14606 {
14607 /* If we found the largest possible address, then we already
14608 have the base address in range_end. */
14609 base = range_end;
14610 continue;
14611 }
14612
14613 if (!base.has_value ())
14614 {
14615 /* We have no valid base address for the ranges
14616 data. */
14617 complaint (_("Invalid .debug_ranges data (no base address)"));
14618 return 0;
14619 }
14620
14621 if (range_beginning > range_end)
14622 {
14623 /* Inverted range entries are invalid. */
14624 complaint (_("Invalid .debug_ranges data (inverted range)"));
14625 return 0;
14626 }
14627
14628 /* Empty range entries have no effect. */
14629 if (range_beginning == range_end)
14630 continue;
14631
14632 range_beginning += *base;
14633 range_end += *base;
14634
14635 /* A not-uncommon case of bad debug info.
14636 Don't pollute the addrmap with bad data. */
14637 if (range_beginning + baseaddr == 0
14638 && !per_objfile->per_bfd->has_section_at_zero)
14639 {
14640 complaint (_(".debug_ranges entry has start address of zero"
14641 " [in module %s]"), objfile_name (objfile));
14642 continue;
14643 }
14644
14645 callback (range_beginning, range_end);
14646 }
14647
14648 return 1;
14649 }
14650
14651 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14652 Return 1 if the attributes are present and valid, otherwise, return 0.
14653 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14654
14655 static int
14656 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14657 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14658 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14659 {
14660 struct objfile *objfile = cu->per_objfile->objfile;
14661 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14662 struct gdbarch *gdbarch = objfile->arch ();
14663 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14664 int low_set = 0;
14665 CORE_ADDR low = 0;
14666 CORE_ADDR high = 0;
14667 int retval;
14668
14669 retval = dwarf2_ranges_process (offset, cu, tag,
14670 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14671 {
14672 if (ranges_pst != NULL)
14673 {
14674 CORE_ADDR lowpc;
14675 CORE_ADDR highpc;
14676
14677 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14678 range_beginning + baseaddr)
14679 - baseaddr);
14680 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14681 range_end + baseaddr)
14682 - baseaddr);
14683 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14684 lowpc, highpc - 1, ranges_pst);
14685 }
14686
14687 /* FIXME: This is recording everything as a low-high
14688 segment of consecutive addresses. We should have a
14689 data structure for discontiguous block ranges
14690 instead. */
14691 if (! low_set)
14692 {
14693 low = range_beginning;
14694 high = range_end;
14695 low_set = 1;
14696 }
14697 else
14698 {
14699 if (range_beginning < low)
14700 low = range_beginning;
14701 if (range_end > high)
14702 high = range_end;
14703 }
14704 });
14705 if (!retval)
14706 return 0;
14707
14708 if (! low_set)
14709 /* If the first entry is an end-of-list marker, the range
14710 describes an empty scope, i.e. no instructions. */
14711 return 0;
14712
14713 if (low_return)
14714 *low_return = low;
14715 if (high_return)
14716 *high_return = high;
14717 return 1;
14718 }
14719
14720 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14721 definition for the return value. *LOWPC and *HIGHPC are set iff
14722 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14723
14724 static enum pc_bounds_kind
14725 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14726 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14727 dwarf2_psymtab *pst)
14728 {
14729 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14730 struct attribute *attr;
14731 struct attribute *attr_high;
14732 CORE_ADDR low = 0;
14733 CORE_ADDR high = 0;
14734 enum pc_bounds_kind ret;
14735
14736 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14737 if (attr_high)
14738 {
14739 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14740 if (attr != nullptr)
14741 {
14742 low = attr->as_address ();
14743 high = attr_high->as_address ();
14744 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14745 high += low;
14746 }
14747 else
14748 /* Found high w/o low attribute. */
14749 return PC_BOUNDS_INVALID;
14750
14751 /* Found consecutive range of addresses. */
14752 ret = PC_BOUNDS_HIGH_LOW;
14753 }
14754 else
14755 {
14756 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14757 if (attr != nullptr && attr->form_is_unsigned ())
14758 {
14759 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14760 on DWARF version). */
14761 ULONGEST ranges_offset = attr->as_unsigned ();
14762
14763 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14764 this value. */
14765 if (die->tag != DW_TAG_compile_unit)
14766 ranges_offset += cu->gnu_ranges_base;
14767
14768 /* Value of the DW_AT_ranges attribute is the offset in the
14769 .debug_ranges section. */
14770 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14771 die->tag))
14772 return PC_BOUNDS_INVALID;
14773 /* Found discontinuous range of addresses. */
14774 ret = PC_BOUNDS_RANGES;
14775 }
14776 else
14777 return PC_BOUNDS_NOT_PRESENT;
14778 }
14779
14780 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14781 if (high <= low)
14782 return PC_BOUNDS_INVALID;
14783
14784 /* When using the GNU linker, .gnu.linkonce. sections are used to
14785 eliminate duplicate copies of functions and vtables and such.
14786 The linker will arbitrarily choose one and discard the others.
14787 The AT_*_pc values for such functions refer to local labels in
14788 these sections. If the section from that file was discarded, the
14789 labels are not in the output, so the relocs get a value of 0.
14790 If this is a discarded function, mark the pc bounds as invalid,
14791 so that GDB will ignore it. */
14792 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14793 return PC_BOUNDS_INVALID;
14794
14795 *lowpc = low;
14796 if (highpc)
14797 *highpc = high;
14798 return ret;
14799 }
14800
14801 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14802 its low and high PC addresses. Do nothing if these addresses could not
14803 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14804 and HIGHPC to the high address if greater than HIGHPC. */
14805
14806 static void
14807 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14808 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14809 struct dwarf2_cu *cu)
14810 {
14811 CORE_ADDR low, high;
14812 struct die_info *child = die->child;
14813
14814 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14815 {
14816 *lowpc = std::min (*lowpc, low);
14817 *highpc = std::max (*highpc, high);
14818 }
14819
14820 /* If the language does not allow nested subprograms (either inside
14821 subprograms or lexical blocks), we're done. */
14822 if (cu->language != language_ada)
14823 return;
14824
14825 /* Check all the children of the given DIE. If it contains nested
14826 subprograms, then check their pc bounds. Likewise, we need to
14827 check lexical blocks as well, as they may also contain subprogram
14828 definitions. */
14829 while (child && child->tag)
14830 {
14831 if (child->tag == DW_TAG_subprogram
14832 || child->tag == DW_TAG_lexical_block)
14833 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14834 child = child->sibling;
14835 }
14836 }
14837
14838 /* Get the low and high pc's represented by the scope DIE, and store
14839 them in *LOWPC and *HIGHPC. If the correct values can't be
14840 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14841
14842 static void
14843 get_scope_pc_bounds (struct die_info *die,
14844 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14845 struct dwarf2_cu *cu)
14846 {
14847 CORE_ADDR best_low = (CORE_ADDR) -1;
14848 CORE_ADDR best_high = (CORE_ADDR) 0;
14849 CORE_ADDR current_low, current_high;
14850
14851 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14852 >= PC_BOUNDS_RANGES)
14853 {
14854 best_low = current_low;
14855 best_high = current_high;
14856 }
14857 else
14858 {
14859 struct die_info *child = die->child;
14860
14861 while (child && child->tag)
14862 {
14863 switch (child->tag) {
14864 case DW_TAG_subprogram:
14865 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14866 break;
14867 case DW_TAG_namespace:
14868 case DW_TAG_module:
14869 /* FIXME: carlton/2004-01-16: Should we do this for
14870 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14871 that current GCC's always emit the DIEs corresponding
14872 to definitions of methods of classes as children of a
14873 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14874 the DIEs giving the declarations, which could be
14875 anywhere). But I don't see any reason why the
14876 standards says that they have to be there. */
14877 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14878
14879 if (current_low != ((CORE_ADDR) -1))
14880 {
14881 best_low = std::min (best_low, current_low);
14882 best_high = std::max (best_high, current_high);
14883 }
14884 break;
14885 default:
14886 /* Ignore. */
14887 break;
14888 }
14889
14890 child = child->sibling;
14891 }
14892 }
14893
14894 *lowpc = best_low;
14895 *highpc = best_high;
14896 }
14897
14898 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14899 in DIE. */
14900
14901 static void
14902 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14903 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14904 {
14905 struct objfile *objfile = cu->per_objfile->objfile;
14906 struct gdbarch *gdbarch = objfile->arch ();
14907 struct attribute *attr;
14908 struct attribute *attr_high;
14909
14910 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14911 if (attr_high)
14912 {
14913 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14914 if (attr != nullptr)
14915 {
14916 CORE_ADDR low = attr->as_address ();
14917 CORE_ADDR high = attr_high->as_address ();
14918
14919 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14920 high += low;
14921
14922 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14923 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14924 cu->get_builder ()->record_block_range (block, low, high - 1);
14925 }
14926 }
14927
14928 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14929 if (attr != nullptr && attr->form_is_unsigned ())
14930 {
14931 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14932 on DWARF version). */
14933 ULONGEST ranges_offset = attr->as_unsigned ();
14934
14935 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14936 this value. */
14937 if (die->tag != DW_TAG_compile_unit)
14938 ranges_offset += cu->gnu_ranges_base;
14939
14940 std::vector<blockrange> blockvec;
14941 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14942 [&] (CORE_ADDR start, CORE_ADDR end)
14943 {
14944 start += baseaddr;
14945 end += baseaddr;
14946 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14947 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14948 cu->get_builder ()->record_block_range (block, start, end - 1);
14949 blockvec.emplace_back (start, end);
14950 });
14951
14952 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14953 }
14954 }
14955
14956 /* Check whether the producer field indicates either of GCC < 4.6, or the
14957 Intel C/C++ compiler, and cache the result in CU. */
14958
14959 static void
14960 check_producer (struct dwarf2_cu *cu)
14961 {
14962 int major, minor;
14963
14964 if (cu->producer == NULL)
14965 {
14966 /* For unknown compilers expect their behavior is DWARF version
14967 compliant.
14968
14969 GCC started to support .debug_types sections by -gdwarf-4 since
14970 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14971 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14972 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14973 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14974 }
14975 else if (producer_is_gcc (cu->producer, &major, &minor))
14976 {
14977 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14978 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14979 }
14980 else if (producer_is_icc (cu->producer, &major, &minor))
14981 {
14982 cu->producer_is_icc = true;
14983 cu->producer_is_icc_lt_14 = major < 14;
14984 }
14985 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14986 cu->producer_is_codewarrior = true;
14987 else
14988 {
14989 /* For other non-GCC compilers, expect their behavior is DWARF version
14990 compliant. */
14991 }
14992
14993 cu->checked_producer = true;
14994 }
14995
14996 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14997 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14998 during 4.6.0 experimental. */
14999
15000 static bool
15001 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15002 {
15003 if (!cu->checked_producer)
15004 check_producer (cu);
15005
15006 return cu->producer_is_gxx_lt_4_6;
15007 }
15008
15009
15010 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
15011 with incorrect is_stmt attributes. */
15012
15013 static bool
15014 producer_is_codewarrior (struct dwarf2_cu *cu)
15015 {
15016 if (!cu->checked_producer)
15017 check_producer (cu);
15018
15019 return cu->producer_is_codewarrior;
15020 }
15021
15022 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
15023 If that attribute is not available, return the appropriate
15024 default. */
15025
15026 static enum dwarf_access_attribute
15027 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15028 {
15029 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15030 if (attr != nullptr)
15031 {
15032 LONGEST value = attr->constant_value (-1);
15033 if (value == DW_ACCESS_public
15034 || value == DW_ACCESS_protected
15035 || value == DW_ACCESS_private)
15036 return (dwarf_access_attribute) value;
15037 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
15038 plongest (value));
15039 }
15040
15041 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15042 {
15043 /* The default DWARF 2 accessibility for members is public, the default
15044 accessibility for inheritance is private. */
15045
15046 if (die->tag != DW_TAG_inheritance)
15047 return DW_ACCESS_public;
15048 else
15049 return DW_ACCESS_private;
15050 }
15051 else
15052 {
15053 /* DWARF 3+ defines the default accessibility a different way. The same
15054 rules apply now for DW_TAG_inheritance as for the members and it only
15055 depends on the container kind. */
15056
15057 if (die->parent->tag == DW_TAG_class_type)
15058 return DW_ACCESS_private;
15059 else
15060 return DW_ACCESS_public;
15061 }
15062 }
15063
15064 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15065 offset. If the attribute was not found return 0, otherwise return
15066 1. If it was found but could not properly be handled, set *OFFSET
15067 to 0. */
15068
15069 static int
15070 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15071 LONGEST *offset)
15072 {
15073 struct attribute *attr;
15074
15075 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15076 if (attr != NULL)
15077 {
15078 *offset = 0;
15079
15080 /* Note that we do not check for a section offset first here.
15081 This is because DW_AT_data_member_location is new in DWARF 4,
15082 so if we see it, we can assume that a constant form is really
15083 a constant and not a section offset. */
15084 if (attr->form_is_constant ())
15085 *offset = attr->constant_value (0);
15086 else if (attr->form_is_section_offset ())
15087 dwarf2_complex_location_expr_complaint ();
15088 else if (attr->form_is_block ())
15089 *offset = decode_locdesc (attr->as_block (), cu);
15090 else
15091 dwarf2_complex_location_expr_complaint ();
15092
15093 return 1;
15094 }
15095
15096 return 0;
15097 }
15098
15099 /* Look for DW_AT_data_member_location and store the results in FIELD. */
15100
15101 static void
15102 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15103 struct field *field)
15104 {
15105 struct attribute *attr;
15106
15107 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15108 if (attr != NULL)
15109 {
15110 if (attr->form_is_constant ())
15111 {
15112 LONGEST offset = attr->constant_value (0);
15113 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15114 }
15115 else if (attr->form_is_section_offset ())
15116 dwarf2_complex_location_expr_complaint ();
15117 else if (attr->form_is_block ())
15118 {
15119 bool handled;
15120 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
15121 if (handled)
15122 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15123 else
15124 {
15125 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15126 struct objfile *objfile = per_objfile->objfile;
15127 struct dwarf2_locexpr_baton *dlbaton
15128 = XOBNEW (&objfile->objfile_obstack,
15129 struct dwarf2_locexpr_baton);
15130 dlbaton->data = attr->as_block ()->data;
15131 dlbaton->size = attr->as_block ()->size;
15132 /* When using this baton, we want to compute the address
15133 of the field, not the value. This is why
15134 is_reference is set to false here. */
15135 dlbaton->is_reference = false;
15136 dlbaton->per_objfile = per_objfile;
15137 dlbaton->per_cu = cu->per_cu;
15138
15139 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15140 }
15141 }
15142 else
15143 dwarf2_complex_location_expr_complaint ();
15144 }
15145 }
15146
15147 /* Add an aggregate field to the field list. */
15148
15149 static void
15150 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15151 struct dwarf2_cu *cu)
15152 {
15153 struct objfile *objfile = cu->per_objfile->objfile;
15154 struct gdbarch *gdbarch = objfile->arch ();
15155 struct nextfield *new_field;
15156 struct attribute *attr;
15157 struct field *fp;
15158 const char *fieldname = "";
15159
15160 if (die->tag == DW_TAG_inheritance)
15161 {
15162 fip->baseclasses.emplace_back ();
15163 new_field = &fip->baseclasses.back ();
15164 }
15165 else
15166 {
15167 fip->fields.emplace_back ();
15168 new_field = &fip->fields.back ();
15169 }
15170
15171 new_field->offset = die->sect_off;
15172
15173 new_field->accessibility = dwarf2_access_attribute (die, cu);
15174 if (new_field->accessibility != DW_ACCESS_public)
15175 fip->non_public_fields = true;
15176
15177 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15178 if (attr != nullptr)
15179 new_field->virtuality = attr->as_virtuality ();
15180 else
15181 new_field->virtuality = DW_VIRTUALITY_none;
15182
15183 fp = &new_field->field;
15184
15185 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15186 {
15187 /* Data member other than a C++ static data member. */
15188
15189 /* Get type of field. */
15190 fp->set_type (die_type (die, cu));
15191
15192 SET_FIELD_BITPOS (*fp, 0);
15193
15194 /* Get bit size of field (zero if none). */
15195 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15196 if (attr != nullptr)
15197 {
15198 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15199 }
15200 else
15201 {
15202 FIELD_BITSIZE (*fp) = 0;
15203 }
15204
15205 /* Get bit offset of field. */
15206 handle_data_member_location (die, cu, fp);
15207 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15208 if (attr != nullptr && attr->form_is_constant ())
15209 {
15210 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15211 {
15212 /* For big endian bits, the DW_AT_bit_offset gives the
15213 additional bit offset from the MSB of the containing
15214 anonymous object to the MSB of the field. We don't
15215 have to do anything special since we don't need to
15216 know the size of the anonymous object. */
15217 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15218 + attr->constant_value (0)));
15219 }
15220 else
15221 {
15222 /* For little endian bits, compute the bit offset to the
15223 MSB of the anonymous object, subtract off the number of
15224 bits from the MSB of the field to the MSB of the
15225 object, and then subtract off the number of bits of
15226 the field itself. The result is the bit offset of
15227 the LSB of the field. */
15228 int anonymous_size;
15229 int bit_offset = attr->constant_value (0);
15230
15231 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15232 if (attr != nullptr && attr->form_is_constant ())
15233 {
15234 /* The size of the anonymous object containing
15235 the bit field is explicit, so use the
15236 indicated size (in bytes). */
15237 anonymous_size = attr->constant_value (0);
15238 }
15239 else
15240 {
15241 /* The size of the anonymous object containing
15242 the bit field must be inferred from the type
15243 attribute of the data member containing the
15244 bit field. */
15245 anonymous_size = TYPE_LENGTH (fp->type ());
15246 }
15247 SET_FIELD_BITPOS (*fp,
15248 (FIELD_BITPOS (*fp)
15249 + anonymous_size * bits_per_byte
15250 - bit_offset - FIELD_BITSIZE (*fp)));
15251 }
15252 }
15253 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15254 if (attr != NULL)
15255 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15256 + attr->constant_value (0)));
15257
15258 /* Get name of field. */
15259 fieldname = dwarf2_name (die, cu);
15260 if (fieldname == NULL)
15261 fieldname = "";
15262
15263 /* The name is already allocated along with this objfile, so we don't
15264 need to duplicate it for the type. */
15265 fp->name = fieldname;
15266
15267 /* Change accessibility for artificial fields (e.g. virtual table
15268 pointer or virtual base class pointer) to private. */
15269 if (dwarf2_attr (die, DW_AT_artificial, cu))
15270 {
15271 FIELD_ARTIFICIAL (*fp) = 1;
15272 new_field->accessibility = DW_ACCESS_private;
15273 fip->non_public_fields = true;
15274 }
15275 }
15276 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15277 {
15278 /* C++ static member. */
15279
15280 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15281 is a declaration, but all versions of G++ as of this writing
15282 (so through at least 3.2.1) incorrectly generate
15283 DW_TAG_variable tags. */
15284
15285 const char *physname;
15286
15287 /* Get name of field. */
15288 fieldname = dwarf2_name (die, cu);
15289 if (fieldname == NULL)
15290 return;
15291
15292 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15293 if (attr
15294 /* Only create a symbol if this is an external value.
15295 new_symbol checks this and puts the value in the global symbol
15296 table, which we want. If it is not external, new_symbol
15297 will try to put the value in cu->list_in_scope which is wrong. */
15298 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15299 {
15300 /* A static const member, not much different than an enum as far as
15301 we're concerned, except that we can support more types. */
15302 new_symbol (die, NULL, cu);
15303 }
15304
15305 /* Get physical name. */
15306 physname = dwarf2_physname (fieldname, die, cu);
15307
15308 /* The name is already allocated along with this objfile, so we don't
15309 need to duplicate it for the type. */
15310 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15311 fp->set_type (die_type (die, cu));
15312 FIELD_NAME (*fp) = fieldname;
15313 }
15314 else if (die->tag == DW_TAG_inheritance)
15315 {
15316 /* C++ base class field. */
15317 handle_data_member_location (die, cu, fp);
15318 FIELD_BITSIZE (*fp) = 0;
15319 fp->set_type (die_type (die, cu));
15320 FIELD_NAME (*fp) = fp->type ()->name ();
15321 }
15322 else
15323 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15324 }
15325
15326 /* Can the type given by DIE define another type? */
15327
15328 static bool
15329 type_can_define_types (const struct die_info *die)
15330 {
15331 switch (die->tag)
15332 {
15333 case DW_TAG_typedef:
15334 case DW_TAG_class_type:
15335 case DW_TAG_structure_type:
15336 case DW_TAG_union_type:
15337 case DW_TAG_enumeration_type:
15338 return true;
15339
15340 default:
15341 return false;
15342 }
15343 }
15344
15345 /* Add a type definition defined in the scope of the FIP's class. */
15346
15347 static void
15348 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15349 struct dwarf2_cu *cu)
15350 {
15351 struct decl_field fp;
15352 memset (&fp, 0, sizeof (fp));
15353
15354 gdb_assert (type_can_define_types (die));
15355
15356 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15357 fp.name = dwarf2_name (die, cu);
15358 fp.type = read_type_die (die, cu);
15359
15360 /* Save accessibility. */
15361 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15362 switch (accessibility)
15363 {
15364 case DW_ACCESS_public:
15365 /* The assumed value if neither private nor protected. */
15366 break;
15367 case DW_ACCESS_private:
15368 fp.is_private = 1;
15369 break;
15370 case DW_ACCESS_protected:
15371 fp.is_protected = 1;
15372 break;
15373 }
15374
15375 if (die->tag == DW_TAG_typedef)
15376 fip->typedef_field_list.push_back (fp);
15377 else
15378 fip->nested_types_list.push_back (fp);
15379 }
15380
15381 /* A convenience typedef that's used when finding the discriminant
15382 field for a variant part. */
15383 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15384 offset_map_type;
15385
15386 /* Compute the discriminant range for a given variant. OBSTACK is
15387 where the results will be stored. VARIANT is the variant to
15388 process. IS_UNSIGNED indicates whether the discriminant is signed
15389 or unsigned. */
15390
15391 static const gdb::array_view<discriminant_range>
15392 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15393 bool is_unsigned)
15394 {
15395 std::vector<discriminant_range> ranges;
15396
15397 if (variant.default_branch)
15398 return {};
15399
15400 if (variant.discr_list_data == nullptr)
15401 {
15402 discriminant_range r
15403 = {variant.discriminant_value, variant.discriminant_value};
15404 ranges.push_back (r);
15405 }
15406 else
15407 {
15408 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15409 variant.discr_list_data->size);
15410 while (!data.empty ())
15411 {
15412 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15413 {
15414 complaint (_("invalid discriminant marker: %d"), data[0]);
15415 break;
15416 }
15417 bool is_range = data[0] == DW_DSC_range;
15418 data = data.slice (1);
15419
15420 ULONGEST low, high;
15421 unsigned int bytes_read;
15422
15423 if (data.empty ())
15424 {
15425 complaint (_("DW_AT_discr_list missing low value"));
15426 break;
15427 }
15428 if (is_unsigned)
15429 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15430 else
15431 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15432 &bytes_read);
15433 data = data.slice (bytes_read);
15434
15435 if (is_range)
15436 {
15437 if (data.empty ())
15438 {
15439 complaint (_("DW_AT_discr_list missing high value"));
15440 break;
15441 }
15442 if (is_unsigned)
15443 high = read_unsigned_leb128 (nullptr, data.data (),
15444 &bytes_read);
15445 else
15446 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15447 &bytes_read);
15448 data = data.slice (bytes_read);
15449 }
15450 else
15451 high = low;
15452
15453 ranges.push_back ({ low, high });
15454 }
15455 }
15456
15457 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15458 ranges.size ());
15459 std::copy (ranges.begin (), ranges.end (), result);
15460 return gdb::array_view<discriminant_range> (result, ranges.size ());
15461 }
15462
15463 static const gdb::array_view<variant_part> create_variant_parts
15464 (struct obstack *obstack,
15465 const offset_map_type &offset_map,
15466 struct field_info *fi,
15467 const std::vector<variant_part_builder> &variant_parts);
15468
15469 /* Fill in a "struct variant" for a given variant field. RESULT is
15470 the variant to fill in. OBSTACK is where any needed allocations
15471 will be done. OFFSET_MAP holds the mapping from section offsets to
15472 fields for the type. FI describes the fields of the type we're
15473 processing. FIELD is the variant field we're converting. */
15474
15475 static void
15476 create_one_variant (variant &result, struct obstack *obstack,
15477 const offset_map_type &offset_map,
15478 struct field_info *fi, const variant_field &field)
15479 {
15480 result.discriminants = convert_variant_range (obstack, field, false);
15481 result.first_field = field.first_field + fi->baseclasses.size ();
15482 result.last_field = field.last_field + fi->baseclasses.size ();
15483 result.parts = create_variant_parts (obstack, offset_map, fi,
15484 field.variant_parts);
15485 }
15486
15487 /* Fill in a "struct variant_part" for a given variant part. RESULT
15488 is the variant part to fill in. OBSTACK is where any needed
15489 allocations will be done. OFFSET_MAP holds the mapping from
15490 section offsets to fields for the type. FI describes the fields of
15491 the type we're processing. BUILDER is the variant part to be
15492 converted. */
15493
15494 static void
15495 create_one_variant_part (variant_part &result,
15496 struct obstack *obstack,
15497 const offset_map_type &offset_map,
15498 struct field_info *fi,
15499 const variant_part_builder &builder)
15500 {
15501 auto iter = offset_map.find (builder.discriminant_offset);
15502 if (iter == offset_map.end ())
15503 {
15504 result.discriminant_index = -1;
15505 /* Doesn't matter. */
15506 result.is_unsigned = false;
15507 }
15508 else
15509 {
15510 result.discriminant_index = iter->second;
15511 result.is_unsigned
15512 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15513 }
15514
15515 size_t n = builder.variants.size ();
15516 variant *output = new (obstack) variant[n];
15517 for (size_t i = 0; i < n; ++i)
15518 create_one_variant (output[i], obstack, offset_map, fi,
15519 builder.variants[i]);
15520
15521 result.variants = gdb::array_view<variant> (output, n);
15522 }
15523
15524 /* Create a vector of variant parts that can be attached to a type.
15525 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15526 holds the mapping from section offsets to fields for the type. FI
15527 describes the fields of the type we're processing. VARIANT_PARTS
15528 is the vector to convert. */
15529
15530 static const gdb::array_view<variant_part>
15531 create_variant_parts (struct obstack *obstack,
15532 const offset_map_type &offset_map,
15533 struct field_info *fi,
15534 const std::vector<variant_part_builder> &variant_parts)
15535 {
15536 if (variant_parts.empty ())
15537 return {};
15538
15539 size_t n = variant_parts.size ();
15540 variant_part *result = new (obstack) variant_part[n];
15541 for (size_t i = 0; i < n; ++i)
15542 create_one_variant_part (result[i], obstack, offset_map, fi,
15543 variant_parts[i]);
15544
15545 return gdb::array_view<variant_part> (result, n);
15546 }
15547
15548 /* Compute the variant part vector for FIP, attaching it to TYPE when
15549 done. */
15550
15551 static void
15552 add_variant_property (struct field_info *fip, struct type *type,
15553 struct dwarf2_cu *cu)
15554 {
15555 /* Map section offsets of fields to their field index. Note the
15556 field index here does not take the number of baseclasses into
15557 account. */
15558 offset_map_type offset_map;
15559 for (int i = 0; i < fip->fields.size (); ++i)
15560 offset_map[fip->fields[i].offset] = i;
15561
15562 struct objfile *objfile = cu->per_objfile->objfile;
15563 gdb::array_view<variant_part> parts
15564 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15565 fip->variant_parts);
15566
15567 struct dynamic_prop prop;
15568 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15569 obstack_copy (&objfile->objfile_obstack, &parts,
15570 sizeof (parts)));
15571
15572 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15573 }
15574
15575 /* Create the vector of fields, and attach it to the type. */
15576
15577 static void
15578 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15579 struct dwarf2_cu *cu)
15580 {
15581 int nfields = fip->nfields ();
15582
15583 /* Record the field count, allocate space for the array of fields,
15584 and create blank accessibility bitfields if necessary. */
15585 type->set_num_fields (nfields);
15586 type->set_fields
15587 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15588
15589 if (fip->non_public_fields && cu->language != language_ada)
15590 {
15591 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15592
15593 TYPE_FIELD_PRIVATE_BITS (type) =
15594 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15595 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15596
15597 TYPE_FIELD_PROTECTED_BITS (type) =
15598 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15599 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15600
15601 TYPE_FIELD_IGNORE_BITS (type) =
15602 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15603 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15604 }
15605
15606 /* If the type has baseclasses, allocate and clear a bit vector for
15607 TYPE_FIELD_VIRTUAL_BITS. */
15608 if (!fip->baseclasses.empty () && cu->language != language_ada)
15609 {
15610 int num_bytes = B_BYTES (fip->baseclasses.size ());
15611 unsigned char *pointer;
15612
15613 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15614 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15615 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15616 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15617 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15618 }
15619
15620 if (!fip->variant_parts.empty ())
15621 add_variant_property (fip, type, cu);
15622
15623 /* Copy the saved-up fields into the field vector. */
15624 for (int i = 0; i < nfields; ++i)
15625 {
15626 struct nextfield &field
15627 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15628 : fip->fields[i - fip->baseclasses.size ()]);
15629
15630 type->field (i) = field.field;
15631 switch (field.accessibility)
15632 {
15633 case DW_ACCESS_private:
15634 if (cu->language != language_ada)
15635 SET_TYPE_FIELD_PRIVATE (type, i);
15636 break;
15637
15638 case DW_ACCESS_protected:
15639 if (cu->language != language_ada)
15640 SET_TYPE_FIELD_PROTECTED (type, i);
15641 break;
15642
15643 case DW_ACCESS_public:
15644 break;
15645
15646 default:
15647 /* Unknown accessibility. Complain and treat it as public. */
15648 {
15649 complaint (_("unsupported accessibility %d"),
15650 field.accessibility);
15651 }
15652 break;
15653 }
15654 if (i < fip->baseclasses.size ())
15655 {
15656 switch (field.virtuality)
15657 {
15658 case DW_VIRTUALITY_virtual:
15659 case DW_VIRTUALITY_pure_virtual:
15660 if (cu->language == language_ada)
15661 error (_("unexpected virtuality in component of Ada type"));
15662 SET_TYPE_FIELD_VIRTUAL (type, i);
15663 break;
15664 }
15665 }
15666 }
15667 }
15668
15669 /* Return true if this member function is a constructor, false
15670 otherwise. */
15671
15672 static int
15673 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15674 {
15675 const char *fieldname;
15676 const char *type_name;
15677 int len;
15678
15679 if (die->parent == NULL)
15680 return 0;
15681
15682 if (die->parent->tag != DW_TAG_structure_type
15683 && die->parent->tag != DW_TAG_union_type
15684 && die->parent->tag != DW_TAG_class_type)
15685 return 0;
15686
15687 fieldname = dwarf2_name (die, cu);
15688 type_name = dwarf2_name (die->parent, cu);
15689 if (fieldname == NULL || type_name == NULL)
15690 return 0;
15691
15692 len = strlen (fieldname);
15693 return (strncmp (fieldname, type_name, len) == 0
15694 && (type_name[len] == '\0' || type_name[len] == '<'));
15695 }
15696
15697 /* Add a member function to the proper fieldlist. */
15698
15699 static void
15700 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15701 struct type *type, struct dwarf2_cu *cu)
15702 {
15703 struct objfile *objfile = cu->per_objfile->objfile;
15704 struct attribute *attr;
15705 int i;
15706 struct fnfieldlist *flp = nullptr;
15707 struct fn_field *fnp;
15708 const char *fieldname;
15709 struct type *this_type;
15710
15711 if (cu->language == language_ada)
15712 error (_("unexpected member function in Ada type"));
15713
15714 /* Get name of member function. */
15715 fieldname = dwarf2_name (die, cu);
15716 if (fieldname == NULL)
15717 return;
15718
15719 /* Look up member function name in fieldlist. */
15720 for (i = 0; i < fip->fnfieldlists.size (); i++)
15721 {
15722 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15723 {
15724 flp = &fip->fnfieldlists[i];
15725 break;
15726 }
15727 }
15728
15729 /* Create a new fnfieldlist if necessary. */
15730 if (flp == nullptr)
15731 {
15732 fip->fnfieldlists.emplace_back ();
15733 flp = &fip->fnfieldlists.back ();
15734 flp->name = fieldname;
15735 i = fip->fnfieldlists.size () - 1;
15736 }
15737
15738 /* Create a new member function field and add it to the vector of
15739 fnfieldlists. */
15740 flp->fnfields.emplace_back ();
15741 fnp = &flp->fnfields.back ();
15742
15743 /* Delay processing of the physname until later. */
15744 if (cu->language == language_cplus)
15745 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15746 die, cu);
15747 else
15748 {
15749 const char *physname = dwarf2_physname (fieldname, die, cu);
15750 fnp->physname = physname ? physname : "";
15751 }
15752
15753 fnp->type = alloc_type (objfile);
15754 this_type = read_type_die (die, cu);
15755 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15756 {
15757 int nparams = this_type->num_fields ();
15758
15759 /* TYPE is the domain of this method, and THIS_TYPE is the type
15760 of the method itself (TYPE_CODE_METHOD). */
15761 smash_to_method_type (fnp->type, type,
15762 TYPE_TARGET_TYPE (this_type),
15763 this_type->fields (),
15764 this_type->num_fields (),
15765 this_type->has_varargs ());
15766
15767 /* Handle static member functions.
15768 Dwarf2 has no clean way to discern C++ static and non-static
15769 member functions. G++ helps GDB by marking the first
15770 parameter for non-static member functions (which is the this
15771 pointer) as artificial. We obtain this information from
15772 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15773 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15774 fnp->voffset = VOFFSET_STATIC;
15775 }
15776 else
15777 complaint (_("member function type missing for '%s'"),
15778 dwarf2_full_name (fieldname, die, cu));
15779
15780 /* Get fcontext from DW_AT_containing_type if present. */
15781 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15782 fnp->fcontext = die_containing_type (die, cu);
15783
15784 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15785 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15786
15787 /* Get accessibility. */
15788 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15789 switch (accessibility)
15790 {
15791 case DW_ACCESS_private:
15792 fnp->is_private = 1;
15793 break;
15794 case DW_ACCESS_protected:
15795 fnp->is_protected = 1;
15796 break;
15797 }
15798
15799 /* Check for artificial methods. */
15800 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15801 if (attr && attr->as_boolean ())
15802 fnp->is_artificial = 1;
15803
15804 /* Check for defaulted methods. */
15805 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15806 if (attr != nullptr)
15807 fnp->defaulted = attr->defaulted ();
15808
15809 /* Check for deleted methods. */
15810 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15811 if (attr != nullptr && attr->as_boolean ())
15812 fnp->is_deleted = 1;
15813
15814 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15815
15816 /* Get index in virtual function table if it is a virtual member
15817 function. For older versions of GCC, this is an offset in the
15818 appropriate virtual table, as specified by DW_AT_containing_type.
15819 For everyone else, it is an expression to be evaluated relative
15820 to the object address. */
15821
15822 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15823 if (attr != nullptr)
15824 {
15825 if (attr->form_is_block () && attr->as_block ()->size > 0)
15826 {
15827 struct dwarf_block *block = attr->as_block ();
15828
15829 if (block->data[0] == DW_OP_constu)
15830 {
15831 /* Old-style GCC. */
15832 fnp->voffset = decode_locdesc (block, cu) + 2;
15833 }
15834 else if (block->data[0] == DW_OP_deref
15835 || (block->size > 1
15836 && block->data[0] == DW_OP_deref_size
15837 && block->data[1] == cu->header.addr_size))
15838 {
15839 fnp->voffset = decode_locdesc (block, cu);
15840 if ((fnp->voffset % cu->header.addr_size) != 0)
15841 dwarf2_complex_location_expr_complaint ();
15842 else
15843 fnp->voffset /= cu->header.addr_size;
15844 fnp->voffset += 2;
15845 }
15846 else
15847 dwarf2_complex_location_expr_complaint ();
15848
15849 if (!fnp->fcontext)
15850 {
15851 /* If there is no `this' field and no DW_AT_containing_type,
15852 we cannot actually find a base class context for the
15853 vtable! */
15854 if (this_type->num_fields () == 0
15855 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15856 {
15857 complaint (_("cannot determine context for virtual member "
15858 "function \"%s\" (offset %s)"),
15859 fieldname, sect_offset_str (die->sect_off));
15860 }
15861 else
15862 {
15863 fnp->fcontext
15864 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15865 }
15866 }
15867 }
15868 else if (attr->form_is_section_offset ())
15869 {
15870 dwarf2_complex_location_expr_complaint ();
15871 }
15872 else
15873 {
15874 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15875 fieldname);
15876 }
15877 }
15878 else
15879 {
15880 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15881 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15882 {
15883 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15884 complaint (_("Member function \"%s\" (offset %s) is virtual "
15885 "but the vtable offset is not specified"),
15886 fieldname, sect_offset_str (die->sect_off));
15887 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15888 TYPE_CPLUS_DYNAMIC (type) = 1;
15889 }
15890 }
15891 }
15892
15893 /* Create the vector of member function fields, and attach it to the type. */
15894
15895 static void
15896 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15897 struct dwarf2_cu *cu)
15898 {
15899 if (cu->language == language_ada)
15900 error (_("unexpected member functions in Ada type"));
15901
15902 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15903 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15904 TYPE_ALLOC (type,
15905 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15906
15907 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15908 {
15909 struct fnfieldlist &nf = fip->fnfieldlists[i];
15910 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15911
15912 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15913 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15914 fn_flp->fn_fields = (struct fn_field *)
15915 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15916
15917 for (int k = 0; k < nf.fnfields.size (); ++k)
15918 fn_flp->fn_fields[k] = nf.fnfields[k];
15919 }
15920
15921 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15922 }
15923
15924 /* Returns non-zero if NAME is the name of a vtable member in CU's
15925 language, zero otherwise. */
15926 static int
15927 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15928 {
15929 static const char vptr[] = "_vptr";
15930
15931 /* Look for the C++ form of the vtable. */
15932 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15933 return 1;
15934
15935 return 0;
15936 }
15937
15938 /* GCC outputs unnamed structures that are really pointers to member
15939 functions, with the ABI-specified layout. If TYPE describes
15940 such a structure, smash it into a member function type.
15941
15942 GCC shouldn't do this; it should just output pointer to member DIEs.
15943 This is GCC PR debug/28767. */
15944
15945 static void
15946 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15947 {
15948 struct type *pfn_type, *self_type, *new_type;
15949
15950 /* Check for a structure with no name and two children. */
15951 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15952 return;
15953
15954 /* Check for __pfn and __delta members. */
15955 if (TYPE_FIELD_NAME (type, 0) == NULL
15956 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15957 || TYPE_FIELD_NAME (type, 1) == NULL
15958 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15959 return;
15960
15961 /* Find the type of the method. */
15962 pfn_type = type->field (0).type ();
15963 if (pfn_type == NULL
15964 || pfn_type->code () != TYPE_CODE_PTR
15965 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15966 return;
15967
15968 /* Look for the "this" argument. */
15969 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15970 if (pfn_type->num_fields () == 0
15971 /* || pfn_type->field (0).type () == NULL */
15972 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15973 return;
15974
15975 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15976 new_type = alloc_type (objfile);
15977 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15978 pfn_type->fields (), pfn_type->num_fields (),
15979 pfn_type->has_varargs ());
15980 smash_to_methodptr_type (type, new_type);
15981 }
15982
15983 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15984 requires rewriting, then copy it and return the updated copy.
15985 Otherwise return nullptr. */
15986
15987 static struct type *
15988 rewrite_array_type (struct type *type)
15989 {
15990 if (type->code () != TYPE_CODE_ARRAY)
15991 return nullptr;
15992
15993 struct type *index_type = type->index_type ();
15994 range_bounds *current_bounds = index_type->bounds ();
15995
15996 /* Handle multi-dimensional arrays. */
15997 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15998 if (new_target == nullptr)
15999 {
16000 /* Maybe we don't need to rewrite this array. */
16001 if (current_bounds->low.kind () == PROP_CONST
16002 && current_bounds->high.kind () == PROP_CONST)
16003 return nullptr;
16004 }
16005
16006 /* Either the target type was rewritten, or the bounds have to be
16007 updated. Either way we want to copy the type and update
16008 everything. */
16009 struct type *copy = copy_type (type);
16010 int nfields = copy->num_fields ();
16011 field *new_fields
16012 = ((struct field *) TYPE_ZALLOC (copy,
16013 nfields * sizeof (struct field)));
16014 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
16015 copy->set_fields (new_fields);
16016 if (new_target != nullptr)
16017 TYPE_TARGET_TYPE (copy) = new_target;
16018
16019 struct type *index_copy = copy_type (index_type);
16020 range_bounds *bounds
16021 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
16022 sizeof (range_bounds));
16023 *bounds = *current_bounds;
16024 bounds->low.set_const_val (1);
16025 bounds->high.set_const_val (0);
16026 index_copy->set_bounds (bounds);
16027 copy->set_index_type (index_copy);
16028
16029 return copy;
16030 }
16031
16032 /* While some versions of GCC will generate complicated DWARF for an
16033 array (see quirk_ada_thick_pointer), more recent versions were
16034 modified to emit an explicit thick pointer structure. However, in
16035 this case, the array still has DWARF expressions for its ranges,
16036 and these must be ignored. */
16037
16038 static void
16039 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
16040 struct type *type)
16041 {
16042 gdb_assert (cu->language == language_ada);
16043
16044 /* Check for a structure with two children. */
16045 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
16046 return;
16047
16048 /* Check for P_ARRAY and P_BOUNDS members. */
16049 if (TYPE_FIELD_NAME (type, 0) == NULL
16050 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
16051 || TYPE_FIELD_NAME (type, 1) == NULL
16052 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
16053 return;
16054
16055 /* Make sure we're looking at a pointer to an array. */
16056 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
16057 return;
16058
16059 /* The Ada code already knows how to handle these types, so all that
16060 we need to do is turn the bounds into static bounds. However, we
16061 don't want to rewrite existing array or index types in-place,
16062 because those may be referenced in other contexts where this
16063 rewriting is undesirable. */
16064 struct type *new_ary_type
16065 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
16066 if (new_ary_type != nullptr)
16067 type->field (0).set_type (lookup_pointer_type (new_ary_type));
16068 }
16069
16070 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
16071 appropriate error checking and issuing complaints if there is a
16072 problem. */
16073
16074 static ULONGEST
16075 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
16076 {
16077 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
16078
16079 if (attr == nullptr)
16080 return 0;
16081
16082 if (!attr->form_is_constant ())
16083 {
16084 complaint (_("DW_AT_alignment must have constant form"
16085 " - DIE at %s [in module %s]"),
16086 sect_offset_str (die->sect_off),
16087 objfile_name (cu->per_objfile->objfile));
16088 return 0;
16089 }
16090
16091 LONGEST val = attr->constant_value (0);
16092 if (val < 0)
16093 {
16094 complaint (_("DW_AT_alignment value must not be negative"
16095 " - DIE at %s [in module %s]"),
16096 sect_offset_str (die->sect_off),
16097 objfile_name (cu->per_objfile->objfile));
16098 return 0;
16099 }
16100 ULONGEST align = val;
16101
16102 if (align == 0)
16103 {
16104 complaint (_("DW_AT_alignment value must not be zero"
16105 " - DIE at %s [in module %s]"),
16106 sect_offset_str (die->sect_off),
16107 objfile_name (cu->per_objfile->objfile));
16108 return 0;
16109 }
16110 if ((align & (align - 1)) != 0)
16111 {
16112 complaint (_("DW_AT_alignment value must be a power of 2"
16113 " - DIE at %s [in module %s]"),
16114 sect_offset_str (die->sect_off),
16115 objfile_name (cu->per_objfile->objfile));
16116 return 0;
16117 }
16118
16119 return align;
16120 }
16121
16122 /* If the DIE has a DW_AT_alignment attribute, use its value to set
16123 the alignment for TYPE. */
16124
16125 static void
16126 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16127 struct type *type)
16128 {
16129 if (!set_type_align (type, get_alignment (cu, die)))
16130 complaint (_("DW_AT_alignment value too large"
16131 " - DIE at %s [in module %s]"),
16132 sect_offset_str (die->sect_off),
16133 objfile_name (cu->per_objfile->objfile));
16134 }
16135
16136 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16137 constant for a type, according to DWARF5 spec, Table 5.5. */
16138
16139 static bool
16140 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16141 {
16142 switch (value)
16143 {
16144 case DW_CC_normal:
16145 case DW_CC_pass_by_reference:
16146 case DW_CC_pass_by_value:
16147 return true;
16148
16149 default:
16150 complaint (_("unrecognized DW_AT_calling_convention value "
16151 "(%s) for a type"), pulongest (value));
16152 return false;
16153 }
16154 }
16155
16156 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16157 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16158 also according to GNU-specific values (see include/dwarf2.h). */
16159
16160 static bool
16161 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16162 {
16163 switch (value)
16164 {
16165 case DW_CC_normal:
16166 case DW_CC_program:
16167 case DW_CC_nocall:
16168 return true;
16169
16170 case DW_CC_GNU_renesas_sh:
16171 case DW_CC_GNU_borland_fastcall_i386:
16172 case DW_CC_GDB_IBM_OpenCL:
16173 return true;
16174
16175 default:
16176 complaint (_("unrecognized DW_AT_calling_convention value "
16177 "(%s) for a subroutine"), pulongest (value));
16178 return false;
16179 }
16180 }
16181
16182 /* Called when we find the DIE that starts a structure or union scope
16183 (definition) to create a type for the structure or union. Fill in
16184 the type's name and general properties; the members will not be
16185 processed until process_structure_scope. A symbol table entry for
16186 the type will also not be done until process_structure_scope (assuming
16187 the type has a name).
16188
16189 NOTE: we need to call these functions regardless of whether or not the
16190 DIE has a DW_AT_name attribute, since it might be an anonymous
16191 structure or union. This gets the type entered into our set of
16192 user defined types. */
16193
16194 static struct type *
16195 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16196 {
16197 struct objfile *objfile = cu->per_objfile->objfile;
16198 struct type *type;
16199 struct attribute *attr;
16200 const char *name;
16201
16202 /* If the definition of this type lives in .debug_types, read that type.
16203 Don't follow DW_AT_specification though, that will take us back up
16204 the chain and we want to go down. */
16205 attr = die->attr (DW_AT_signature);
16206 if (attr != nullptr)
16207 {
16208 type = get_DW_AT_signature_type (die, attr, cu);
16209
16210 /* The type's CU may not be the same as CU.
16211 Ensure TYPE is recorded with CU in die_type_hash. */
16212 return set_die_type (die, type, cu);
16213 }
16214
16215 type = alloc_type (objfile);
16216 INIT_CPLUS_SPECIFIC (type);
16217
16218 name = dwarf2_name (die, cu);
16219 if (name != NULL)
16220 {
16221 if (cu->language == language_cplus
16222 || cu->language == language_d
16223 || cu->language == language_rust)
16224 {
16225 const char *full_name = dwarf2_full_name (name, die, cu);
16226
16227 /* dwarf2_full_name might have already finished building the DIE's
16228 type. If so, there is no need to continue. */
16229 if (get_die_type (die, cu) != NULL)
16230 return get_die_type (die, cu);
16231
16232 type->set_name (full_name);
16233 }
16234 else
16235 {
16236 /* The name is already allocated along with this objfile, so
16237 we don't need to duplicate it for the type. */
16238 type->set_name (name);
16239 }
16240 }
16241
16242 if (die->tag == DW_TAG_structure_type)
16243 {
16244 type->set_code (TYPE_CODE_STRUCT);
16245 }
16246 else if (die->tag == DW_TAG_union_type)
16247 {
16248 type->set_code (TYPE_CODE_UNION);
16249 }
16250 else
16251 {
16252 type->set_code (TYPE_CODE_STRUCT);
16253 }
16254
16255 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16256 type->set_is_declared_class (true);
16257
16258 /* Store the calling convention in the type if it's available in
16259 the die. Otherwise the calling convention remains set to
16260 the default value DW_CC_normal. */
16261 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16262 if (attr != nullptr
16263 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16264 {
16265 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16266 TYPE_CPLUS_CALLING_CONVENTION (type)
16267 = (enum dwarf_calling_convention) (attr->constant_value (0));
16268 }
16269
16270 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16271 if (attr != nullptr)
16272 {
16273 if (attr->form_is_constant ())
16274 TYPE_LENGTH (type) = attr->constant_value (0);
16275 else
16276 {
16277 struct dynamic_prop prop;
16278 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16279 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16280 TYPE_LENGTH (type) = 0;
16281 }
16282 }
16283 else
16284 {
16285 TYPE_LENGTH (type) = 0;
16286 }
16287
16288 maybe_set_alignment (cu, die, type);
16289
16290 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16291 {
16292 /* ICC<14 does not output the required DW_AT_declaration on
16293 incomplete types, but gives them a size of zero. */
16294 type->set_is_stub (true);
16295 }
16296 else
16297 type->set_stub_is_supported (true);
16298
16299 if (die_is_declaration (die, cu))
16300 type->set_is_stub (true);
16301 else if (attr == NULL && die->child == NULL
16302 && producer_is_realview (cu->producer))
16303 /* RealView does not output the required DW_AT_declaration
16304 on incomplete types. */
16305 type->set_is_stub (true);
16306
16307 /* We need to add the type field to the die immediately so we don't
16308 infinitely recurse when dealing with pointers to the structure
16309 type within the structure itself. */
16310 set_die_type (die, type, cu);
16311
16312 /* set_die_type should be already done. */
16313 set_descriptive_type (type, die, cu);
16314
16315 return type;
16316 }
16317
16318 static void handle_struct_member_die
16319 (struct die_info *child_die,
16320 struct type *type,
16321 struct field_info *fi,
16322 std::vector<struct symbol *> *template_args,
16323 struct dwarf2_cu *cu);
16324
16325 /* A helper for handle_struct_member_die that handles
16326 DW_TAG_variant_part. */
16327
16328 static void
16329 handle_variant_part (struct die_info *die, struct type *type,
16330 struct field_info *fi,
16331 std::vector<struct symbol *> *template_args,
16332 struct dwarf2_cu *cu)
16333 {
16334 variant_part_builder *new_part;
16335 if (fi->current_variant_part == nullptr)
16336 {
16337 fi->variant_parts.emplace_back ();
16338 new_part = &fi->variant_parts.back ();
16339 }
16340 else if (!fi->current_variant_part->processing_variant)
16341 {
16342 complaint (_("nested DW_TAG_variant_part seen "
16343 "- DIE at %s [in module %s]"),
16344 sect_offset_str (die->sect_off),
16345 objfile_name (cu->per_objfile->objfile));
16346 return;
16347 }
16348 else
16349 {
16350 variant_field &current = fi->current_variant_part->variants.back ();
16351 current.variant_parts.emplace_back ();
16352 new_part = &current.variant_parts.back ();
16353 }
16354
16355 /* When we recurse, we want callees to add to this new variant
16356 part. */
16357 scoped_restore save_current_variant_part
16358 = make_scoped_restore (&fi->current_variant_part, new_part);
16359
16360 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16361 if (discr == NULL)
16362 {
16363 /* It's a univariant form, an extension we support. */
16364 }
16365 else if (discr->form_is_ref ())
16366 {
16367 struct dwarf2_cu *target_cu = cu;
16368 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16369
16370 new_part->discriminant_offset = target_die->sect_off;
16371 }
16372 else
16373 {
16374 complaint (_("DW_AT_discr does not have DIE reference form"
16375 " - DIE at %s [in module %s]"),
16376 sect_offset_str (die->sect_off),
16377 objfile_name (cu->per_objfile->objfile));
16378 }
16379
16380 for (die_info *child_die = die->child;
16381 child_die != NULL;
16382 child_die = child_die->sibling)
16383 handle_struct_member_die (child_die, type, fi, template_args, cu);
16384 }
16385
16386 /* A helper for handle_struct_member_die that handles
16387 DW_TAG_variant. */
16388
16389 static void
16390 handle_variant (struct die_info *die, struct type *type,
16391 struct field_info *fi,
16392 std::vector<struct symbol *> *template_args,
16393 struct dwarf2_cu *cu)
16394 {
16395 if (fi->current_variant_part == nullptr)
16396 {
16397 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16398 "- DIE at %s [in module %s]"),
16399 sect_offset_str (die->sect_off),
16400 objfile_name (cu->per_objfile->objfile));
16401 return;
16402 }
16403 if (fi->current_variant_part->processing_variant)
16404 {
16405 complaint (_("nested DW_TAG_variant seen "
16406 "- DIE at %s [in module %s]"),
16407 sect_offset_str (die->sect_off),
16408 objfile_name (cu->per_objfile->objfile));
16409 return;
16410 }
16411
16412 scoped_restore save_processing_variant
16413 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16414 true);
16415
16416 fi->current_variant_part->variants.emplace_back ();
16417 variant_field &variant = fi->current_variant_part->variants.back ();
16418 variant.first_field = fi->fields.size ();
16419
16420 /* In a variant we want to get the discriminant and also add a
16421 field for our sole member child. */
16422 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16423 if (discr == nullptr || !discr->form_is_constant ())
16424 {
16425 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16426 if (discr == nullptr || discr->as_block ()->size == 0)
16427 variant.default_branch = true;
16428 else
16429 variant.discr_list_data = discr->as_block ();
16430 }
16431 else
16432 variant.discriminant_value = discr->constant_value (0);
16433
16434 for (die_info *variant_child = die->child;
16435 variant_child != NULL;
16436 variant_child = variant_child->sibling)
16437 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16438
16439 variant.last_field = fi->fields.size ();
16440 }
16441
16442 /* A helper for process_structure_scope that handles a single member
16443 DIE. */
16444
16445 static void
16446 handle_struct_member_die (struct die_info *child_die, struct type *type,
16447 struct field_info *fi,
16448 std::vector<struct symbol *> *template_args,
16449 struct dwarf2_cu *cu)
16450 {
16451 if (child_die->tag == DW_TAG_member
16452 || child_die->tag == DW_TAG_variable)
16453 {
16454 /* NOTE: carlton/2002-11-05: A C++ static data member
16455 should be a DW_TAG_member that is a declaration, but
16456 all versions of G++ as of this writing (so through at
16457 least 3.2.1) incorrectly generate DW_TAG_variable
16458 tags for them instead. */
16459 dwarf2_add_field (fi, child_die, cu);
16460 }
16461 else if (child_die->tag == DW_TAG_subprogram)
16462 {
16463 /* Rust doesn't have member functions in the C++ sense.
16464 However, it does emit ordinary functions as children
16465 of a struct DIE. */
16466 if (cu->language == language_rust)
16467 read_func_scope (child_die, cu);
16468 else
16469 {
16470 /* C++ member function. */
16471 dwarf2_add_member_fn (fi, child_die, type, cu);
16472 }
16473 }
16474 else if (child_die->tag == DW_TAG_inheritance)
16475 {
16476 /* C++ base class field. */
16477 dwarf2_add_field (fi, child_die, cu);
16478 }
16479 else if (type_can_define_types (child_die))
16480 dwarf2_add_type_defn (fi, child_die, cu);
16481 else if (child_die->tag == DW_TAG_template_type_param
16482 || child_die->tag == DW_TAG_template_value_param)
16483 {
16484 struct symbol *arg = new_symbol (child_die, NULL, cu);
16485
16486 if (arg != NULL)
16487 template_args->push_back (arg);
16488 }
16489 else if (child_die->tag == DW_TAG_variant_part)
16490 handle_variant_part (child_die, type, fi, template_args, cu);
16491 else if (child_die->tag == DW_TAG_variant)
16492 handle_variant (child_die, type, fi, template_args, cu);
16493 }
16494
16495 /* Finish creating a structure or union type, including filling in
16496 its members and creating a symbol for it. */
16497
16498 static void
16499 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16500 {
16501 struct objfile *objfile = cu->per_objfile->objfile;
16502 struct die_info *child_die;
16503 struct type *type;
16504
16505 type = get_die_type (die, cu);
16506 if (type == NULL)
16507 type = read_structure_type (die, cu);
16508
16509 bool has_template_parameters = false;
16510 if (die->child != NULL && ! die_is_declaration (die, cu))
16511 {
16512 struct field_info fi;
16513 std::vector<struct symbol *> template_args;
16514
16515 child_die = die->child;
16516
16517 while (child_die && child_die->tag)
16518 {
16519 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16520 child_die = child_die->sibling;
16521 }
16522
16523 /* Attach template arguments to type. */
16524 if (!template_args.empty ())
16525 {
16526 has_template_parameters = true;
16527 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16528 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16529 TYPE_TEMPLATE_ARGUMENTS (type)
16530 = XOBNEWVEC (&objfile->objfile_obstack,
16531 struct symbol *,
16532 TYPE_N_TEMPLATE_ARGUMENTS (type));
16533 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16534 template_args.data (),
16535 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16536 * sizeof (struct symbol *)));
16537 }
16538
16539 /* Attach fields and member functions to the type. */
16540 if (fi.nfields () > 0)
16541 dwarf2_attach_fields_to_type (&fi, type, cu);
16542 if (!fi.fnfieldlists.empty ())
16543 {
16544 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16545
16546 /* Get the type which refers to the base class (possibly this
16547 class itself) which contains the vtable pointer for the current
16548 class from the DW_AT_containing_type attribute. This use of
16549 DW_AT_containing_type is a GNU extension. */
16550
16551 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16552 {
16553 struct type *t = die_containing_type (die, cu);
16554
16555 set_type_vptr_basetype (type, t);
16556 if (type == t)
16557 {
16558 int i;
16559
16560 /* Our own class provides vtbl ptr. */
16561 for (i = t->num_fields () - 1;
16562 i >= TYPE_N_BASECLASSES (t);
16563 --i)
16564 {
16565 const char *fieldname = TYPE_FIELD_NAME (t, i);
16566
16567 if (is_vtable_name (fieldname, cu))
16568 {
16569 set_type_vptr_fieldno (type, i);
16570 break;
16571 }
16572 }
16573
16574 /* Complain if virtual function table field not found. */
16575 if (i < TYPE_N_BASECLASSES (t))
16576 complaint (_("virtual function table pointer "
16577 "not found when defining class '%s'"),
16578 type->name () ? type->name () : "");
16579 }
16580 else
16581 {
16582 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16583 }
16584 }
16585 else if (cu->producer
16586 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16587 {
16588 /* The IBM XLC compiler does not provide direct indication
16589 of the containing type, but the vtable pointer is
16590 always named __vfp. */
16591
16592 int i;
16593
16594 for (i = type->num_fields () - 1;
16595 i >= TYPE_N_BASECLASSES (type);
16596 --i)
16597 {
16598 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16599 {
16600 set_type_vptr_fieldno (type, i);
16601 set_type_vptr_basetype (type, type);
16602 break;
16603 }
16604 }
16605 }
16606 }
16607
16608 /* Copy fi.typedef_field_list linked list elements content into the
16609 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16610 if (!fi.typedef_field_list.empty ())
16611 {
16612 int count = fi.typedef_field_list.size ();
16613
16614 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16615 TYPE_TYPEDEF_FIELD_ARRAY (type)
16616 = ((struct decl_field *)
16617 TYPE_ALLOC (type,
16618 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16619 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16620
16621 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16622 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16623 }
16624
16625 /* Copy fi.nested_types_list linked list elements content into the
16626 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16627 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16628 {
16629 int count = fi.nested_types_list.size ();
16630
16631 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16632 TYPE_NESTED_TYPES_ARRAY (type)
16633 = ((struct decl_field *)
16634 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16635 TYPE_NESTED_TYPES_COUNT (type) = count;
16636
16637 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16638 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16639 }
16640 }
16641
16642 quirk_gcc_member_function_pointer (type, objfile);
16643 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16644 cu->rust_unions.push_back (type);
16645 else if (cu->language == language_ada)
16646 quirk_ada_thick_pointer_struct (die, cu, type);
16647
16648 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16649 snapshots) has been known to create a die giving a declaration
16650 for a class that has, as a child, a die giving a definition for a
16651 nested class. So we have to process our children even if the
16652 current die is a declaration. Normally, of course, a declaration
16653 won't have any children at all. */
16654
16655 child_die = die->child;
16656
16657 while (child_die != NULL && child_die->tag)
16658 {
16659 if (child_die->tag == DW_TAG_member
16660 || child_die->tag == DW_TAG_variable
16661 || child_die->tag == DW_TAG_inheritance
16662 || child_die->tag == DW_TAG_template_value_param
16663 || child_die->tag == DW_TAG_template_type_param)
16664 {
16665 /* Do nothing. */
16666 }
16667 else
16668 process_die (child_die, cu);
16669
16670 child_die = child_die->sibling;
16671 }
16672
16673 /* Do not consider external references. According to the DWARF standard,
16674 these DIEs are identified by the fact that they have no byte_size
16675 attribute, and a declaration attribute. */
16676 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16677 || !die_is_declaration (die, cu)
16678 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16679 {
16680 struct symbol *sym = new_symbol (die, type, cu);
16681
16682 if (has_template_parameters)
16683 {
16684 struct symtab *symtab;
16685 if (sym != nullptr)
16686 symtab = symbol_symtab (sym);
16687 else if (cu->line_header != nullptr)
16688 {
16689 /* Any related symtab will do. */
16690 symtab
16691 = cu->line_header->file_names ()[0].symtab;
16692 }
16693 else
16694 {
16695 symtab = nullptr;
16696 complaint (_("could not find suitable "
16697 "symtab for template parameter"
16698 " - DIE at %s [in module %s]"),
16699 sect_offset_str (die->sect_off),
16700 objfile_name (objfile));
16701 }
16702
16703 if (symtab != nullptr)
16704 {
16705 /* Make sure that the symtab is set on the new symbols.
16706 Even though they don't appear in this symtab directly,
16707 other parts of gdb assume that symbols do, and this is
16708 reasonably true. */
16709 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16710 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16711 }
16712 }
16713 }
16714 }
16715
16716 /* Assuming DIE is an enumeration type, and TYPE is its associated
16717 type, update TYPE using some information only available in DIE's
16718 children. In particular, the fields are computed. */
16719
16720 static void
16721 update_enumeration_type_from_children (struct die_info *die,
16722 struct type *type,
16723 struct dwarf2_cu *cu)
16724 {
16725 struct die_info *child_die;
16726 int unsigned_enum = 1;
16727 int flag_enum = 1;
16728
16729 auto_obstack obstack;
16730 std::vector<struct field> fields;
16731
16732 for (child_die = die->child;
16733 child_die != NULL && child_die->tag;
16734 child_die = child_die->sibling)
16735 {
16736 struct attribute *attr;
16737 LONGEST value;
16738 const gdb_byte *bytes;
16739 struct dwarf2_locexpr_baton *baton;
16740 const char *name;
16741
16742 if (child_die->tag != DW_TAG_enumerator)
16743 continue;
16744
16745 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16746 if (attr == NULL)
16747 continue;
16748
16749 name = dwarf2_name (child_die, cu);
16750 if (name == NULL)
16751 name = "<anonymous enumerator>";
16752
16753 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16754 &value, &bytes, &baton);
16755 if (value < 0)
16756 {
16757 unsigned_enum = 0;
16758 flag_enum = 0;
16759 }
16760 else
16761 {
16762 if (count_one_bits_ll (value) >= 2)
16763 flag_enum = 0;
16764 }
16765
16766 fields.emplace_back ();
16767 struct field &field = fields.back ();
16768 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16769 SET_FIELD_ENUMVAL (field, value);
16770 }
16771
16772 if (!fields.empty ())
16773 {
16774 type->set_num_fields (fields.size ());
16775 type->set_fields
16776 ((struct field *)
16777 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16778 memcpy (type->fields (), fields.data (),
16779 sizeof (struct field) * fields.size ());
16780 }
16781
16782 if (unsigned_enum)
16783 type->set_is_unsigned (true);
16784
16785 if (flag_enum)
16786 type->set_is_flag_enum (true);
16787 }
16788
16789 /* Given a DW_AT_enumeration_type die, set its type. We do not
16790 complete the type's fields yet, or create any symbols. */
16791
16792 static struct type *
16793 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16794 {
16795 struct objfile *objfile = cu->per_objfile->objfile;
16796 struct type *type;
16797 struct attribute *attr;
16798 const char *name;
16799
16800 /* If the definition of this type lives in .debug_types, read that type.
16801 Don't follow DW_AT_specification though, that will take us back up
16802 the chain and we want to go down. */
16803 attr = die->attr (DW_AT_signature);
16804 if (attr != nullptr)
16805 {
16806 type = get_DW_AT_signature_type (die, attr, cu);
16807
16808 /* The type's CU may not be the same as CU.
16809 Ensure TYPE is recorded with CU in die_type_hash. */
16810 return set_die_type (die, type, cu);
16811 }
16812
16813 type = alloc_type (objfile);
16814
16815 type->set_code (TYPE_CODE_ENUM);
16816 name = dwarf2_full_name (NULL, die, cu);
16817 if (name != NULL)
16818 type->set_name (name);
16819
16820 attr = dwarf2_attr (die, DW_AT_type, cu);
16821 if (attr != NULL)
16822 {
16823 struct type *underlying_type = die_type (die, cu);
16824
16825 TYPE_TARGET_TYPE (type) = underlying_type;
16826 }
16827
16828 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16829 if (attr != nullptr)
16830 {
16831 TYPE_LENGTH (type) = attr->constant_value (0);
16832 }
16833 else
16834 {
16835 TYPE_LENGTH (type) = 0;
16836 }
16837
16838 maybe_set_alignment (cu, die, type);
16839
16840 /* The enumeration DIE can be incomplete. In Ada, any type can be
16841 declared as private in the package spec, and then defined only
16842 inside the package body. Such types are known as Taft Amendment
16843 Types. When another package uses such a type, an incomplete DIE
16844 may be generated by the compiler. */
16845 if (die_is_declaration (die, cu))
16846 type->set_is_stub (true);
16847
16848 /* If this type has an underlying type that is not a stub, then we
16849 may use its attributes. We always use the "unsigned" attribute
16850 in this situation, because ordinarily we guess whether the type
16851 is unsigned -- but the guess can be wrong and the underlying type
16852 can tell us the reality. However, we defer to a local size
16853 attribute if one exists, because this lets the compiler override
16854 the underlying type if needed. */
16855 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16856 {
16857 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16858 underlying_type = check_typedef (underlying_type);
16859
16860 type->set_is_unsigned (underlying_type->is_unsigned ());
16861
16862 if (TYPE_LENGTH (type) == 0)
16863 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16864
16865 if (TYPE_RAW_ALIGN (type) == 0
16866 && TYPE_RAW_ALIGN (underlying_type) != 0)
16867 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16868 }
16869
16870 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16871
16872 set_die_type (die, type, cu);
16873
16874 /* Finish the creation of this type by using the enum's children.
16875 Note that, as usual, this must come after set_die_type to avoid
16876 infinite recursion when trying to compute the names of the
16877 enumerators. */
16878 update_enumeration_type_from_children (die, type, cu);
16879
16880 return type;
16881 }
16882
16883 /* Given a pointer to a die which begins an enumeration, process all
16884 the dies that define the members of the enumeration, and create the
16885 symbol for the enumeration type.
16886
16887 NOTE: We reverse the order of the element list. */
16888
16889 static void
16890 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16891 {
16892 struct type *this_type;
16893
16894 this_type = get_die_type (die, cu);
16895 if (this_type == NULL)
16896 this_type = read_enumeration_type (die, cu);
16897
16898 if (die->child != NULL)
16899 {
16900 struct die_info *child_die;
16901 const char *name;
16902
16903 child_die = die->child;
16904 while (child_die && child_die->tag)
16905 {
16906 if (child_die->tag != DW_TAG_enumerator)
16907 {
16908 process_die (child_die, cu);
16909 }
16910 else
16911 {
16912 name = dwarf2_name (child_die, cu);
16913 if (name)
16914 new_symbol (child_die, this_type, cu);
16915 }
16916
16917 child_die = child_die->sibling;
16918 }
16919 }
16920
16921 /* If we are reading an enum from a .debug_types unit, and the enum
16922 is a declaration, and the enum is not the signatured type in the
16923 unit, then we do not want to add a symbol for it. Adding a
16924 symbol would in some cases obscure the true definition of the
16925 enum, giving users an incomplete type when the definition is
16926 actually available. Note that we do not want to do this for all
16927 enums which are just declarations, because C++0x allows forward
16928 enum declarations. */
16929 if (cu->per_cu->is_debug_types
16930 && die_is_declaration (die, cu))
16931 {
16932 struct signatured_type *sig_type;
16933
16934 sig_type = (struct signatured_type *) cu->per_cu;
16935 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16936 if (sig_type->type_offset_in_section != die->sect_off)
16937 return;
16938 }
16939
16940 new_symbol (die, this_type, cu);
16941 }
16942
16943 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16944 expression for an index type and finds the corresponding field
16945 offset in the hidden "P_BOUNDS" structure. Returns true on success
16946 and updates *FIELD, false if it fails to recognize an
16947 expression. */
16948
16949 static bool
16950 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16951 int *bounds_offset, struct field *field,
16952 struct dwarf2_cu *cu)
16953 {
16954 struct attribute *attr = dwarf2_attr (die, name, cu);
16955 if (attr == nullptr || !attr->form_is_block ())
16956 return false;
16957
16958 const struct dwarf_block *block = attr->as_block ();
16959 const gdb_byte *start = block->data;
16960 const gdb_byte *end = block->data + block->size;
16961
16962 /* The expression to recognize generally looks like:
16963
16964 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16965 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16966
16967 However, the second "plus_uconst" may be missing:
16968
16969 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16970 DW_OP_deref_size: 4)
16971
16972 This happens when the field is at the start of the structure.
16973
16974 Also, the final deref may not be sized:
16975
16976 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16977 DW_OP_deref)
16978
16979 This happens when the size of the index type happens to be the
16980 same as the architecture's word size. This can occur with or
16981 without the second plus_uconst. */
16982
16983 if (end - start < 2)
16984 return false;
16985 if (*start++ != DW_OP_push_object_address)
16986 return false;
16987 if (*start++ != DW_OP_plus_uconst)
16988 return false;
16989
16990 uint64_t this_bound_off;
16991 start = gdb_read_uleb128 (start, end, &this_bound_off);
16992 if (start == nullptr || (int) this_bound_off != this_bound_off)
16993 return false;
16994 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16995 is consistent among all bounds. */
16996 if (*bounds_offset == -1)
16997 *bounds_offset = this_bound_off;
16998 else if (*bounds_offset != this_bound_off)
16999 return false;
17000
17001 if (start == end || *start++ != DW_OP_deref)
17002 return false;
17003
17004 int offset = 0;
17005 if (start ==end)
17006 return false;
17007 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
17008 {
17009 /* This means an offset of 0. */
17010 }
17011 else if (*start++ != DW_OP_plus_uconst)
17012 return false;
17013 else
17014 {
17015 /* The size is the parameter to DW_OP_plus_uconst. */
17016 uint64_t val;
17017 start = gdb_read_uleb128 (start, end, &val);
17018 if (start == nullptr)
17019 return false;
17020 if ((int) val != val)
17021 return false;
17022 offset = val;
17023 }
17024
17025 if (start == end)
17026 return false;
17027
17028 uint64_t size;
17029 if (*start == DW_OP_deref_size)
17030 {
17031 start = gdb_read_uleb128 (start + 1, end, &size);
17032 if (start == nullptr)
17033 return false;
17034 }
17035 else if (*start == DW_OP_deref)
17036 {
17037 size = cu->header.addr_size;
17038 ++start;
17039 }
17040 else
17041 return false;
17042
17043 SET_FIELD_BITPOS (*field, 8 * offset);
17044 if (size != TYPE_LENGTH (field->type ()))
17045 FIELD_BITSIZE (*field) = 8 * size;
17046
17047 return true;
17048 }
17049
17050 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
17051 some kinds of Ada arrays:
17052
17053 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
17054 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
17055 <11e0> DW_AT_data_location: 2 byte block: 97 6
17056 (DW_OP_push_object_address; DW_OP_deref)
17057 <11e3> DW_AT_type : <0x1173>
17058 <11e7> DW_AT_sibling : <0x1201>
17059 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
17060 <11ec> DW_AT_type : <0x1206>
17061 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
17062 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17063 DW_OP_deref_size: 4)
17064 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
17065 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17066 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
17067
17068 This actually represents a "thick pointer", which is a structure
17069 with two elements: one that is a pointer to the array data, and one
17070 that is a pointer to another structure; this second structure holds
17071 the array bounds.
17072
17073 This returns a new type on success, or nullptr if this didn't
17074 recognize the type. */
17075
17076 static struct type *
17077 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
17078 struct type *type)
17079 {
17080 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
17081 /* So far we've only seen this with block form. */
17082 if (attr == nullptr || !attr->form_is_block ())
17083 return nullptr;
17084
17085 /* Note that this will fail if the structure layout is changed by
17086 the compiler. However, we have no good way to recognize some
17087 other layout, because we don't know what expression the compiler
17088 might choose to emit should this happen. */
17089 struct dwarf_block *blk = attr->as_block ();
17090 if (blk->size != 2
17091 || blk->data[0] != DW_OP_push_object_address
17092 || blk->data[1] != DW_OP_deref)
17093 return nullptr;
17094
17095 int bounds_offset = -1;
17096 int max_align = -1;
17097 std::vector<struct field> range_fields;
17098 for (struct die_info *child_die = die->child;
17099 child_die;
17100 child_die = child_die->sibling)
17101 {
17102 if (child_die->tag == DW_TAG_subrange_type)
17103 {
17104 struct type *underlying = read_subrange_index_type (child_die, cu);
17105
17106 int this_align = type_align (underlying);
17107 if (this_align > max_align)
17108 max_align = this_align;
17109
17110 range_fields.emplace_back ();
17111 range_fields.emplace_back ();
17112
17113 struct field &lower = range_fields[range_fields.size () - 2];
17114 struct field &upper = range_fields[range_fields.size () - 1];
17115
17116 lower.set_type (underlying);
17117 FIELD_ARTIFICIAL (lower) = 1;
17118
17119 upper.set_type (underlying);
17120 FIELD_ARTIFICIAL (upper) = 1;
17121
17122 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17123 &bounds_offset, &lower, cu)
17124 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17125 &bounds_offset, &upper, cu))
17126 return nullptr;
17127 }
17128 }
17129
17130 /* This shouldn't really happen, but double-check that we found
17131 where the bounds are stored. */
17132 if (bounds_offset == -1)
17133 return nullptr;
17134
17135 struct objfile *objfile = cu->per_objfile->objfile;
17136 for (int i = 0; i < range_fields.size (); i += 2)
17137 {
17138 char name[20];
17139
17140 /* Set the name of each field in the bounds. */
17141 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17142 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17143 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17144 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17145 }
17146
17147 struct type *bounds = alloc_type (objfile);
17148 bounds->set_code (TYPE_CODE_STRUCT);
17149
17150 bounds->set_num_fields (range_fields.size ());
17151 bounds->set_fields
17152 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17153 * sizeof (struct field))));
17154 memcpy (bounds->fields (), range_fields.data (),
17155 bounds->num_fields () * sizeof (struct field));
17156
17157 int last_fieldno = range_fields.size () - 1;
17158 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17159 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17160 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17161
17162 /* Rewrite the existing array type in place. Specifically, we
17163 remove any dynamic properties we might have read, and we replace
17164 the index types. */
17165 struct type *iter = type;
17166 for (int i = 0; i < range_fields.size (); i += 2)
17167 {
17168 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17169 iter->main_type->dyn_prop_list = nullptr;
17170 iter->set_index_type
17171 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17172 iter = TYPE_TARGET_TYPE (iter);
17173 }
17174
17175 struct type *result = alloc_type (objfile);
17176 result->set_code (TYPE_CODE_STRUCT);
17177
17178 result->set_num_fields (2);
17179 result->set_fields
17180 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17181 * sizeof (struct field))));
17182
17183 /* The names are chosen to coincide with what the compiler does with
17184 -fgnat-encodings=all, which the Ada code in gdb already
17185 understands. */
17186 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17187 result->field (0).set_type (lookup_pointer_type (type));
17188
17189 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17190 result->field (1).set_type (lookup_pointer_type (bounds));
17191 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17192
17193 result->set_name (type->name ());
17194 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17195 + TYPE_LENGTH (result->field (1).type ()));
17196
17197 return result;
17198 }
17199
17200 /* Extract all information from a DW_TAG_array_type DIE and put it in
17201 the DIE's type field. For now, this only handles one dimensional
17202 arrays. */
17203
17204 static struct type *
17205 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17206 {
17207 struct objfile *objfile = cu->per_objfile->objfile;
17208 struct die_info *child_die;
17209 struct type *type;
17210 struct type *element_type, *range_type, *index_type;
17211 struct attribute *attr;
17212 const char *name;
17213 struct dynamic_prop *byte_stride_prop = NULL;
17214 unsigned int bit_stride = 0;
17215
17216 element_type = die_type (die, cu);
17217
17218 /* The die_type call above may have already set the type for this DIE. */
17219 type = get_die_type (die, cu);
17220 if (type)
17221 return type;
17222
17223 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17224 if (attr != NULL)
17225 {
17226 int stride_ok;
17227 struct type *prop_type = cu->addr_sized_int_type (false);
17228
17229 byte_stride_prop
17230 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17231 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17232 prop_type);
17233 if (!stride_ok)
17234 {
17235 complaint (_("unable to read array DW_AT_byte_stride "
17236 " - DIE at %s [in module %s]"),
17237 sect_offset_str (die->sect_off),
17238 objfile_name (cu->per_objfile->objfile));
17239 /* Ignore this attribute. We will likely not be able to print
17240 arrays of this type correctly, but there is little we can do
17241 to help if we cannot read the attribute's value. */
17242 byte_stride_prop = NULL;
17243 }
17244 }
17245
17246 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17247 if (attr != NULL)
17248 bit_stride = attr->constant_value (0);
17249
17250 /* Irix 6.2 native cc creates array types without children for
17251 arrays with unspecified length. */
17252 if (die->child == NULL)
17253 {
17254 index_type = objfile_type (objfile)->builtin_int;
17255 range_type = create_static_range_type (NULL, index_type, 0, -1);
17256 type = create_array_type_with_stride (NULL, element_type, range_type,
17257 byte_stride_prop, bit_stride);
17258 return set_die_type (die, type, cu);
17259 }
17260
17261 std::vector<struct type *> range_types;
17262 child_die = die->child;
17263 while (child_die && child_die->tag)
17264 {
17265 if (child_die->tag == DW_TAG_subrange_type)
17266 {
17267 struct type *child_type = read_type_die (child_die, cu);
17268
17269 if (child_type != NULL)
17270 {
17271 /* The range type was succesfully read. Save it for the
17272 array type creation. */
17273 range_types.push_back (child_type);
17274 }
17275 }
17276 child_die = child_die->sibling;
17277 }
17278
17279 if (range_types.empty ())
17280 {
17281 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17282 sect_offset_str (die->sect_off),
17283 objfile_name (cu->per_objfile->objfile));
17284 return NULL;
17285 }
17286
17287 /* Dwarf2 dimensions are output from left to right, create the
17288 necessary array types in backwards order. */
17289
17290 type = element_type;
17291
17292 if (read_array_order (die, cu) == DW_ORD_col_major)
17293 {
17294 int i = 0;
17295
17296 while (i < range_types.size ())
17297 {
17298 type = create_array_type_with_stride (NULL, type, range_types[i++],
17299 byte_stride_prop, bit_stride);
17300 bit_stride = 0;
17301 byte_stride_prop = nullptr;
17302 }
17303 }
17304 else
17305 {
17306 size_t ndim = range_types.size ();
17307 while (ndim-- > 0)
17308 {
17309 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17310 byte_stride_prop, bit_stride);
17311 bit_stride = 0;
17312 byte_stride_prop = nullptr;
17313 }
17314 }
17315
17316 gdb_assert (type != element_type);
17317
17318 /* Understand Dwarf2 support for vector types (like they occur on
17319 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17320 array type. This is not part of the Dwarf2/3 standard yet, but a
17321 custom vendor extension. The main difference between a regular
17322 array and the vector variant is that vectors are passed by value
17323 to functions. */
17324 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17325 if (attr != nullptr)
17326 make_vector_type (type);
17327
17328 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17329 implementation may choose to implement triple vectors using this
17330 attribute. */
17331 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17332 if (attr != nullptr && attr->form_is_unsigned ())
17333 {
17334 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17335 TYPE_LENGTH (type) = attr->as_unsigned ();
17336 else
17337 complaint (_("DW_AT_byte_size for array type smaller "
17338 "than the total size of elements"));
17339 }
17340
17341 name = dwarf2_name (die, cu);
17342 if (name)
17343 type->set_name (name);
17344
17345 maybe_set_alignment (cu, die, type);
17346
17347 struct type *replacement_type = nullptr;
17348 if (cu->language == language_ada)
17349 {
17350 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17351 if (replacement_type != nullptr)
17352 type = replacement_type;
17353 }
17354
17355 /* Install the type in the die. */
17356 set_die_type (die, type, cu, replacement_type != nullptr);
17357
17358 /* set_die_type should be already done. */
17359 set_descriptive_type (type, die, cu);
17360
17361 return type;
17362 }
17363
17364 static enum dwarf_array_dim_ordering
17365 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17366 {
17367 struct attribute *attr;
17368
17369 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17370
17371 if (attr != nullptr)
17372 {
17373 LONGEST val = attr->constant_value (-1);
17374 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17375 return (enum dwarf_array_dim_ordering) val;
17376 }
17377
17378 /* GNU F77 is a special case, as at 08/2004 array type info is the
17379 opposite order to the dwarf2 specification, but data is still
17380 laid out as per normal fortran.
17381
17382 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17383 version checking. */
17384
17385 if (cu->language == language_fortran
17386 && cu->producer && strstr (cu->producer, "GNU F77"))
17387 {
17388 return DW_ORD_row_major;
17389 }
17390
17391 switch (cu->language_defn->array_ordering ())
17392 {
17393 case array_column_major:
17394 return DW_ORD_col_major;
17395 case array_row_major:
17396 default:
17397 return DW_ORD_row_major;
17398 };
17399 }
17400
17401 /* Extract all information from a DW_TAG_set_type DIE and put it in
17402 the DIE's type field. */
17403
17404 static struct type *
17405 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17406 {
17407 struct type *domain_type, *set_type;
17408 struct attribute *attr;
17409
17410 domain_type = die_type (die, cu);
17411
17412 /* The die_type call above may have already set the type for this DIE. */
17413 set_type = get_die_type (die, cu);
17414 if (set_type)
17415 return set_type;
17416
17417 set_type = create_set_type (NULL, domain_type);
17418
17419 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17420 if (attr != nullptr && attr->form_is_unsigned ())
17421 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17422
17423 maybe_set_alignment (cu, die, set_type);
17424
17425 return set_die_type (die, set_type, cu);
17426 }
17427
17428 /* A helper for read_common_block that creates a locexpr baton.
17429 SYM is the symbol which we are marking as computed.
17430 COMMON_DIE is the DIE for the common block.
17431 COMMON_LOC is the location expression attribute for the common
17432 block itself.
17433 MEMBER_LOC is the location expression attribute for the particular
17434 member of the common block that we are processing.
17435 CU is the CU from which the above come. */
17436
17437 static void
17438 mark_common_block_symbol_computed (struct symbol *sym,
17439 struct die_info *common_die,
17440 struct attribute *common_loc,
17441 struct attribute *member_loc,
17442 struct dwarf2_cu *cu)
17443 {
17444 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17445 struct objfile *objfile = per_objfile->objfile;
17446 struct dwarf2_locexpr_baton *baton;
17447 gdb_byte *ptr;
17448 unsigned int cu_off;
17449 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17450 LONGEST offset = 0;
17451
17452 gdb_assert (common_loc && member_loc);
17453 gdb_assert (common_loc->form_is_block ());
17454 gdb_assert (member_loc->form_is_block ()
17455 || member_loc->form_is_constant ());
17456
17457 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17458 baton->per_objfile = per_objfile;
17459 baton->per_cu = cu->per_cu;
17460 gdb_assert (baton->per_cu);
17461
17462 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17463
17464 if (member_loc->form_is_constant ())
17465 {
17466 offset = member_loc->constant_value (0);
17467 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17468 }
17469 else
17470 baton->size += member_loc->as_block ()->size;
17471
17472 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17473 baton->data = ptr;
17474
17475 *ptr++ = DW_OP_call4;
17476 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17477 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17478 ptr += 4;
17479
17480 if (member_loc->form_is_constant ())
17481 {
17482 *ptr++ = DW_OP_addr;
17483 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17484 ptr += cu->header.addr_size;
17485 }
17486 else
17487 {
17488 /* We have to copy the data here, because DW_OP_call4 will only
17489 use a DW_AT_location attribute. */
17490 struct dwarf_block *block = member_loc->as_block ();
17491 memcpy (ptr, block->data, block->size);
17492 ptr += block->size;
17493 }
17494
17495 *ptr++ = DW_OP_plus;
17496 gdb_assert (ptr - baton->data == baton->size);
17497
17498 SYMBOL_LOCATION_BATON (sym) = baton;
17499 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17500 }
17501
17502 /* Create appropriate locally-scoped variables for all the
17503 DW_TAG_common_block entries. Also create a struct common_block
17504 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17505 is used to separate the common blocks name namespace from regular
17506 variable names. */
17507
17508 static void
17509 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17510 {
17511 struct attribute *attr;
17512
17513 attr = dwarf2_attr (die, DW_AT_location, cu);
17514 if (attr != nullptr)
17515 {
17516 /* Support the .debug_loc offsets. */
17517 if (attr->form_is_block ())
17518 {
17519 /* Ok. */
17520 }
17521 else if (attr->form_is_section_offset ())
17522 {
17523 dwarf2_complex_location_expr_complaint ();
17524 attr = NULL;
17525 }
17526 else
17527 {
17528 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17529 "common block member");
17530 attr = NULL;
17531 }
17532 }
17533
17534 if (die->child != NULL)
17535 {
17536 struct objfile *objfile = cu->per_objfile->objfile;
17537 struct die_info *child_die;
17538 size_t n_entries = 0, size;
17539 struct common_block *common_block;
17540 struct symbol *sym;
17541
17542 for (child_die = die->child;
17543 child_die && child_die->tag;
17544 child_die = child_die->sibling)
17545 ++n_entries;
17546
17547 size = (sizeof (struct common_block)
17548 + (n_entries - 1) * sizeof (struct symbol *));
17549 common_block
17550 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17551 size);
17552 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17553 common_block->n_entries = 0;
17554
17555 for (child_die = die->child;
17556 child_die && child_die->tag;
17557 child_die = child_die->sibling)
17558 {
17559 /* Create the symbol in the DW_TAG_common_block block in the current
17560 symbol scope. */
17561 sym = new_symbol (child_die, NULL, cu);
17562 if (sym != NULL)
17563 {
17564 struct attribute *member_loc;
17565
17566 common_block->contents[common_block->n_entries++] = sym;
17567
17568 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17569 cu);
17570 if (member_loc)
17571 {
17572 /* GDB has handled this for a long time, but it is
17573 not specified by DWARF. It seems to have been
17574 emitted by gfortran at least as recently as:
17575 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17576 complaint (_("Variable in common block has "
17577 "DW_AT_data_member_location "
17578 "- DIE at %s [in module %s]"),
17579 sect_offset_str (child_die->sect_off),
17580 objfile_name (objfile));
17581
17582 if (member_loc->form_is_section_offset ())
17583 dwarf2_complex_location_expr_complaint ();
17584 else if (member_loc->form_is_constant ()
17585 || member_loc->form_is_block ())
17586 {
17587 if (attr != nullptr)
17588 mark_common_block_symbol_computed (sym, die, attr,
17589 member_loc, cu);
17590 }
17591 else
17592 dwarf2_complex_location_expr_complaint ();
17593 }
17594 }
17595 }
17596
17597 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17598 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17599 }
17600 }
17601
17602 /* Create a type for a C++ namespace. */
17603
17604 static struct type *
17605 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17606 {
17607 struct objfile *objfile = cu->per_objfile->objfile;
17608 const char *previous_prefix, *name;
17609 int is_anonymous;
17610 struct type *type;
17611
17612 /* For extensions, reuse the type of the original namespace. */
17613 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17614 {
17615 struct die_info *ext_die;
17616 struct dwarf2_cu *ext_cu = cu;
17617
17618 ext_die = dwarf2_extension (die, &ext_cu);
17619 type = read_type_die (ext_die, ext_cu);
17620
17621 /* EXT_CU may not be the same as CU.
17622 Ensure TYPE is recorded with CU in die_type_hash. */
17623 return set_die_type (die, type, cu);
17624 }
17625
17626 name = namespace_name (die, &is_anonymous, cu);
17627
17628 /* Now build the name of the current namespace. */
17629
17630 previous_prefix = determine_prefix (die, cu);
17631 if (previous_prefix[0] != '\0')
17632 name = typename_concat (&objfile->objfile_obstack,
17633 previous_prefix, name, 0, cu);
17634
17635 /* Create the type. */
17636 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17637
17638 return set_die_type (die, type, cu);
17639 }
17640
17641 /* Read a namespace scope. */
17642
17643 static void
17644 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17645 {
17646 struct objfile *objfile = cu->per_objfile->objfile;
17647 int is_anonymous;
17648
17649 /* Add a symbol associated to this if we haven't seen the namespace
17650 before. Also, add a using directive if it's an anonymous
17651 namespace. */
17652
17653 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17654 {
17655 struct type *type;
17656
17657 type = read_type_die (die, cu);
17658 new_symbol (die, type, cu);
17659
17660 namespace_name (die, &is_anonymous, cu);
17661 if (is_anonymous)
17662 {
17663 const char *previous_prefix = determine_prefix (die, cu);
17664
17665 std::vector<const char *> excludes;
17666 add_using_directive (using_directives (cu),
17667 previous_prefix, type->name (), NULL,
17668 NULL, excludes, 0, &objfile->objfile_obstack);
17669 }
17670 }
17671
17672 if (die->child != NULL)
17673 {
17674 struct die_info *child_die = die->child;
17675
17676 while (child_die && child_die->tag)
17677 {
17678 process_die (child_die, cu);
17679 child_die = child_die->sibling;
17680 }
17681 }
17682 }
17683
17684 /* Read a Fortran module as type. This DIE can be only a declaration used for
17685 imported module. Still we need that type as local Fortran "use ... only"
17686 declaration imports depend on the created type in determine_prefix. */
17687
17688 static struct type *
17689 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17690 {
17691 struct objfile *objfile = cu->per_objfile->objfile;
17692 const char *module_name;
17693 struct type *type;
17694
17695 module_name = dwarf2_name (die, cu);
17696 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17697
17698 return set_die_type (die, type, cu);
17699 }
17700
17701 /* Read a Fortran module. */
17702
17703 static void
17704 read_module (struct die_info *die, struct dwarf2_cu *cu)
17705 {
17706 struct die_info *child_die = die->child;
17707 struct type *type;
17708
17709 type = read_type_die (die, cu);
17710 new_symbol (die, type, cu);
17711
17712 while (child_die && child_die->tag)
17713 {
17714 process_die (child_die, cu);
17715 child_die = child_die->sibling;
17716 }
17717 }
17718
17719 /* Return the name of the namespace represented by DIE. Set
17720 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17721 namespace. */
17722
17723 static const char *
17724 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17725 {
17726 struct die_info *current_die;
17727 const char *name = NULL;
17728
17729 /* Loop through the extensions until we find a name. */
17730
17731 for (current_die = die;
17732 current_die != NULL;
17733 current_die = dwarf2_extension (die, &cu))
17734 {
17735 /* We don't use dwarf2_name here so that we can detect the absence
17736 of a name -> anonymous namespace. */
17737 name = dwarf2_string_attr (die, DW_AT_name, cu);
17738
17739 if (name != NULL)
17740 break;
17741 }
17742
17743 /* Is it an anonymous namespace? */
17744
17745 *is_anonymous = (name == NULL);
17746 if (*is_anonymous)
17747 name = CP_ANONYMOUS_NAMESPACE_STR;
17748
17749 return name;
17750 }
17751
17752 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17753 the user defined type vector. */
17754
17755 static struct type *
17756 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17757 {
17758 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17759 struct comp_unit_head *cu_header = &cu->header;
17760 struct type *type;
17761 struct attribute *attr_byte_size;
17762 struct attribute *attr_address_class;
17763 int byte_size, addr_class;
17764 struct type *target_type;
17765
17766 target_type = die_type (die, cu);
17767
17768 /* The die_type call above may have already set the type for this DIE. */
17769 type = get_die_type (die, cu);
17770 if (type)
17771 return type;
17772
17773 type = lookup_pointer_type (target_type);
17774
17775 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17776 if (attr_byte_size)
17777 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17778 else
17779 byte_size = cu_header->addr_size;
17780
17781 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17782 if (attr_address_class)
17783 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17784 else
17785 addr_class = DW_ADDR_none;
17786
17787 ULONGEST alignment = get_alignment (cu, die);
17788
17789 /* If the pointer size, alignment, or address class is different
17790 than the default, create a type variant marked as such and set
17791 the length accordingly. */
17792 if (TYPE_LENGTH (type) != byte_size
17793 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17794 && alignment != TYPE_RAW_ALIGN (type))
17795 || addr_class != DW_ADDR_none)
17796 {
17797 if (gdbarch_address_class_type_flags_p (gdbarch))
17798 {
17799 type_instance_flags type_flags
17800 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17801 addr_class);
17802 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17803 == 0);
17804 type = make_type_with_address_space (type, type_flags);
17805 }
17806 else if (TYPE_LENGTH (type) != byte_size)
17807 {
17808 complaint (_("invalid pointer size %d"), byte_size);
17809 }
17810 else if (TYPE_RAW_ALIGN (type) != alignment)
17811 {
17812 complaint (_("Invalid DW_AT_alignment"
17813 " - DIE at %s [in module %s]"),
17814 sect_offset_str (die->sect_off),
17815 objfile_name (cu->per_objfile->objfile));
17816 }
17817 else
17818 {
17819 /* Should we also complain about unhandled address classes? */
17820 }
17821 }
17822
17823 TYPE_LENGTH (type) = byte_size;
17824 set_type_align (type, alignment);
17825 return set_die_type (die, type, cu);
17826 }
17827
17828 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17829 the user defined type vector. */
17830
17831 static struct type *
17832 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17833 {
17834 struct type *type;
17835 struct type *to_type;
17836 struct type *domain;
17837
17838 to_type = die_type (die, cu);
17839 domain = die_containing_type (die, cu);
17840
17841 /* The calls above may have already set the type for this DIE. */
17842 type = get_die_type (die, cu);
17843 if (type)
17844 return type;
17845
17846 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17847 type = lookup_methodptr_type (to_type);
17848 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17849 {
17850 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17851
17852 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17853 to_type->fields (), to_type->num_fields (),
17854 to_type->has_varargs ());
17855 type = lookup_methodptr_type (new_type);
17856 }
17857 else
17858 type = lookup_memberptr_type (to_type, domain);
17859
17860 return set_die_type (die, type, cu);
17861 }
17862
17863 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17864 the user defined type vector. */
17865
17866 static struct type *
17867 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17868 enum type_code refcode)
17869 {
17870 struct comp_unit_head *cu_header = &cu->header;
17871 struct type *type, *target_type;
17872 struct attribute *attr;
17873
17874 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17875
17876 target_type = die_type (die, cu);
17877
17878 /* The die_type call above may have already set the type for this DIE. */
17879 type = get_die_type (die, cu);
17880 if (type)
17881 return type;
17882
17883 type = lookup_reference_type (target_type, refcode);
17884 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17885 if (attr != nullptr)
17886 {
17887 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17888 }
17889 else
17890 {
17891 TYPE_LENGTH (type) = cu_header->addr_size;
17892 }
17893 maybe_set_alignment (cu, die, type);
17894 return set_die_type (die, type, cu);
17895 }
17896
17897 /* Add the given cv-qualifiers to the element type of the array. GCC
17898 outputs DWARF type qualifiers that apply to an array, not the
17899 element type. But GDB relies on the array element type to carry
17900 the cv-qualifiers. This mimics section 6.7.3 of the C99
17901 specification. */
17902
17903 static struct type *
17904 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17905 struct type *base_type, int cnst, int voltl)
17906 {
17907 struct type *el_type, *inner_array;
17908
17909 base_type = copy_type (base_type);
17910 inner_array = base_type;
17911
17912 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17913 {
17914 TYPE_TARGET_TYPE (inner_array) =
17915 copy_type (TYPE_TARGET_TYPE (inner_array));
17916 inner_array = TYPE_TARGET_TYPE (inner_array);
17917 }
17918
17919 el_type = TYPE_TARGET_TYPE (inner_array);
17920 cnst |= TYPE_CONST (el_type);
17921 voltl |= TYPE_VOLATILE (el_type);
17922 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17923
17924 return set_die_type (die, base_type, cu);
17925 }
17926
17927 static struct type *
17928 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17929 {
17930 struct type *base_type, *cv_type;
17931
17932 base_type = die_type (die, cu);
17933
17934 /* The die_type call above may have already set the type for this DIE. */
17935 cv_type = get_die_type (die, cu);
17936 if (cv_type)
17937 return cv_type;
17938
17939 /* In case the const qualifier is applied to an array type, the element type
17940 is so qualified, not the array type (section 6.7.3 of C99). */
17941 if (base_type->code () == TYPE_CODE_ARRAY)
17942 return add_array_cv_type (die, cu, base_type, 1, 0);
17943
17944 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17945 return set_die_type (die, cv_type, cu);
17946 }
17947
17948 static struct type *
17949 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17950 {
17951 struct type *base_type, *cv_type;
17952
17953 base_type = die_type (die, cu);
17954
17955 /* The die_type call above may have already set the type for this DIE. */
17956 cv_type = get_die_type (die, cu);
17957 if (cv_type)
17958 return cv_type;
17959
17960 /* In case the volatile qualifier is applied to an array type, the
17961 element type is so qualified, not the array type (section 6.7.3
17962 of C99). */
17963 if (base_type->code () == TYPE_CODE_ARRAY)
17964 return add_array_cv_type (die, cu, base_type, 0, 1);
17965
17966 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17967 return set_die_type (die, cv_type, cu);
17968 }
17969
17970 /* Handle DW_TAG_restrict_type. */
17971
17972 static struct type *
17973 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17974 {
17975 struct type *base_type, *cv_type;
17976
17977 base_type = die_type (die, cu);
17978
17979 /* The die_type call above may have already set the type for this DIE. */
17980 cv_type = get_die_type (die, cu);
17981 if (cv_type)
17982 return cv_type;
17983
17984 cv_type = make_restrict_type (base_type);
17985 return set_die_type (die, cv_type, cu);
17986 }
17987
17988 /* Handle DW_TAG_atomic_type. */
17989
17990 static struct type *
17991 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17992 {
17993 struct type *base_type, *cv_type;
17994
17995 base_type = die_type (die, cu);
17996
17997 /* The die_type call above may have already set the type for this DIE. */
17998 cv_type = get_die_type (die, cu);
17999 if (cv_type)
18000 return cv_type;
18001
18002 cv_type = make_atomic_type (base_type);
18003 return set_die_type (die, cv_type, cu);
18004 }
18005
18006 /* Extract all information from a DW_TAG_string_type DIE and add to
18007 the user defined type vector. It isn't really a user defined type,
18008 but it behaves like one, with other DIE's using an AT_user_def_type
18009 attribute to reference it. */
18010
18011 static struct type *
18012 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
18013 {
18014 struct objfile *objfile = cu->per_objfile->objfile;
18015 struct gdbarch *gdbarch = objfile->arch ();
18016 struct type *type, *range_type, *index_type, *char_type;
18017 struct attribute *attr;
18018 struct dynamic_prop prop;
18019 bool length_is_constant = true;
18020 LONGEST length;
18021
18022 /* There are a couple of places where bit sizes might be made use of
18023 when parsing a DW_TAG_string_type, however, no producer that we know
18024 of make use of these. Handling bit sizes that are a multiple of the
18025 byte size is easy enough, but what about other bit sizes? Lets deal
18026 with that problem when we have to. Warn about these attributes being
18027 unsupported, then parse the type and ignore them like we always
18028 have. */
18029 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
18030 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
18031 {
18032 static bool warning_printed = false;
18033 if (!warning_printed)
18034 {
18035 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
18036 "currently supported on DW_TAG_string_type."));
18037 warning_printed = true;
18038 }
18039 }
18040
18041 attr = dwarf2_attr (die, DW_AT_string_length, cu);
18042 if (attr != nullptr && !attr->form_is_constant ())
18043 {
18044 /* The string length describes the location at which the length of
18045 the string can be found. The size of the length field can be
18046 specified with one of the attributes below. */
18047 struct type *prop_type;
18048 struct attribute *len
18049 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
18050 if (len == nullptr)
18051 len = dwarf2_attr (die, DW_AT_byte_size, cu);
18052 if (len != nullptr && len->form_is_constant ())
18053 {
18054 /* Pass 0 as the default as we know this attribute is constant
18055 and the default value will not be returned. */
18056 LONGEST sz = len->constant_value (0);
18057 prop_type = cu->per_objfile->int_type (sz, true);
18058 }
18059 else
18060 {
18061 /* If the size is not specified then we assume it is the size of
18062 an address on this target. */
18063 prop_type = cu->addr_sized_int_type (true);
18064 }
18065
18066 /* Convert the attribute into a dynamic property. */
18067 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
18068 length = 1;
18069 else
18070 length_is_constant = false;
18071 }
18072 else if (attr != nullptr)
18073 {
18074 /* This DW_AT_string_length just contains the length with no
18075 indirection. There's no need to create a dynamic property in this
18076 case. Pass 0 for the default value as we know it will not be
18077 returned in this case. */
18078 length = attr->constant_value (0);
18079 }
18080 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
18081 {
18082 /* We don't currently support non-constant byte sizes for strings. */
18083 length = attr->constant_value (1);
18084 }
18085 else
18086 {
18087 /* Use 1 as a fallback length if we have nothing else. */
18088 length = 1;
18089 }
18090
18091 index_type = objfile_type (objfile)->builtin_int;
18092 if (length_is_constant)
18093 range_type = create_static_range_type (NULL, index_type, 1, length);
18094 else
18095 {
18096 struct dynamic_prop low_bound;
18097
18098 low_bound.set_const_val (1);
18099 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
18100 }
18101 char_type = language_string_char_type (cu->language_defn, gdbarch);
18102 type = create_string_type (NULL, char_type, range_type);
18103
18104 return set_die_type (die, type, cu);
18105 }
18106
18107 /* Assuming that DIE corresponds to a function, returns nonzero
18108 if the function is prototyped. */
18109
18110 static int
18111 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18112 {
18113 struct attribute *attr;
18114
18115 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
18116 if (attr && attr->as_boolean ())
18117 return 1;
18118
18119 /* The DWARF standard implies that the DW_AT_prototyped attribute
18120 is only meaningful for C, but the concept also extends to other
18121 languages that allow unprototyped functions (Eg: Objective C).
18122 For all other languages, assume that functions are always
18123 prototyped. */
18124 if (cu->language != language_c
18125 && cu->language != language_objc
18126 && cu->language != language_opencl)
18127 return 1;
18128
18129 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18130 prototyped and unprototyped functions; default to prototyped,
18131 since that is more common in modern code (and RealView warns
18132 about unprototyped functions). */
18133 if (producer_is_realview (cu->producer))
18134 return 1;
18135
18136 return 0;
18137 }
18138
18139 /* Handle DIES due to C code like:
18140
18141 struct foo
18142 {
18143 int (*funcp)(int a, long l);
18144 int b;
18145 };
18146
18147 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18148
18149 static struct type *
18150 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18151 {
18152 struct objfile *objfile = cu->per_objfile->objfile;
18153 struct type *type; /* Type that this function returns. */
18154 struct type *ftype; /* Function that returns above type. */
18155 struct attribute *attr;
18156
18157 type = die_type (die, cu);
18158
18159 /* The die_type call above may have already set the type for this DIE. */
18160 ftype = get_die_type (die, cu);
18161 if (ftype)
18162 return ftype;
18163
18164 ftype = lookup_function_type (type);
18165
18166 if (prototyped_function_p (die, cu))
18167 ftype->set_is_prototyped (true);
18168
18169 /* Store the calling convention in the type if it's available in
18170 the subroutine die. Otherwise set the calling convention to
18171 the default value DW_CC_normal. */
18172 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18173 if (attr != nullptr
18174 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18175 TYPE_CALLING_CONVENTION (ftype)
18176 = (enum dwarf_calling_convention) attr->constant_value (0);
18177 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18178 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18179 else
18180 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18181
18182 /* Record whether the function returns normally to its caller or not
18183 if the DWARF producer set that information. */
18184 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18185 if (attr && attr->as_boolean ())
18186 TYPE_NO_RETURN (ftype) = 1;
18187
18188 /* We need to add the subroutine type to the die immediately so
18189 we don't infinitely recurse when dealing with parameters
18190 declared as the same subroutine type. */
18191 set_die_type (die, ftype, cu);
18192
18193 if (die->child != NULL)
18194 {
18195 struct type *void_type = objfile_type (objfile)->builtin_void;
18196 struct die_info *child_die;
18197 int nparams, iparams;
18198
18199 /* Count the number of parameters.
18200 FIXME: GDB currently ignores vararg functions, but knows about
18201 vararg member functions. */
18202 nparams = 0;
18203 child_die = die->child;
18204 while (child_die && child_die->tag)
18205 {
18206 if (child_die->tag == DW_TAG_formal_parameter)
18207 nparams++;
18208 else if (child_die->tag == DW_TAG_unspecified_parameters)
18209 ftype->set_has_varargs (true);
18210
18211 child_die = child_die->sibling;
18212 }
18213
18214 /* Allocate storage for parameters and fill them in. */
18215 ftype->set_num_fields (nparams);
18216 ftype->set_fields
18217 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18218
18219 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18220 even if we error out during the parameters reading below. */
18221 for (iparams = 0; iparams < nparams; iparams++)
18222 ftype->field (iparams).set_type (void_type);
18223
18224 iparams = 0;
18225 child_die = die->child;
18226 while (child_die && child_die->tag)
18227 {
18228 if (child_die->tag == DW_TAG_formal_parameter)
18229 {
18230 struct type *arg_type;
18231
18232 /* DWARF version 2 has no clean way to discern C++
18233 static and non-static member functions. G++ helps
18234 GDB by marking the first parameter for non-static
18235 member functions (which is the this pointer) as
18236 artificial. We pass this information to
18237 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18238
18239 DWARF version 3 added DW_AT_object_pointer, which GCC
18240 4.5 does not yet generate. */
18241 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18242 if (attr != nullptr)
18243 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18244 else
18245 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18246 arg_type = die_type (child_die, cu);
18247
18248 /* RealView does not mark THIS as const, which the testsuite
18249 expects. GCC marks THIS as const in method definitions,
18250 but not in the class specifications (GCC PR 43053). */
18251 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18252 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18253 {
18254 int is_this = 0;
18255 struct dwarf2_cu *arg_cu = cu;
18256 const char *name = dwarf2_name (child_die, cu);
18257
18258 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18259 if (attr != nullptr)
18260 {
18261 /* If the compiler emits this, use it. */
18262 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18263 is_this = 1;
18264 }
18265 else if (name && strcmp (name, "this") == 0)
18266 /* Function definitions will have the argument names. */
18267 is_this = 1;
18268 else if (name == NULL && iparams == 0)
18269 /* Declarations may not have the names, so like
18270 elsewhere in GDB, assume an artificial first
18271 argument is "this". */
18272 is_this = 1;
18273
18274 if (is_this)
18275 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18276 arg_type, 0);
18277 }
18278
18279 ftype->field (iparams).set_type (arg_type);
18280 iparams++;
18281 }
18282 child_die = child_die->sibling;
18283 }
18284 }
18285
18286 return ftype;
18287 }
18288
18289 static struct type *
18290 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18291 {
18292 struct objfile *objfile = cu->per_objfile->objfile;
18293 const char *name = NULL;
18294 struct type *this_type, *target_type;
18295
18296 name = dwarf2_full_name (NULL, die, cu);
18297 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18298 this_type->set_target_is_stub (true);
18299 set_die_type (die, this_type, cu);
18300 target_type = die_type (die, cu);
18301 if (target_type != this_type)
18302 TYPE_TARGET_TYPE (this_type) = target_type;
18303 else
18304 {
18305 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18306 spec and cause infinite loops in GDB. */
18307 complaint (_("Self-referential DW_TAG_typedef "
18308 "- DIE at %s [in module %s]"),
18309 sect_offset_str (die->sect_off), objfile_name (objfile));
18310 TYPE_TARGET_TYPE (this_type) = NULL;
18311 }
18312 if (name == NULL)
18313 {
18314 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18315 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18316 Handle these by just returning the target type, rather than
18317 constructing an anonymous typedef type and trying to handle this
18318 elsewhere. */
18319 set_die_type (die, target_type, cu);
18320 return target_type;
18321 }
18322 return this_type;
18323 }
18324
18325 /* Helper for get_dwarf2_rational_constant that computes the value of
18326 a given gmp_mpz given an attribute. */
18327
18328 static void
18329 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18330 {
18331 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18332 location expression that pushes an implicit value. */
18333 if (attr->form == DW_FORM_exprloc)
18334 {
18335 dwarf_block *blk = attr->as_block ();
18336 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18337 {
18338 uint64_t len;
18339 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18340 blk->data + blk->size,
18341 &len);
18342 if (ptr - blk->data + len <= blk->size)
18343 {
18344 mpz_import (value->val, len,
18345 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18346 1, 0, 0, ptr);
18347 return;
18348 }
18349 }
18350
18351 /* On failure set it to 1. */
18352 *value = gdb_mpz (1);
18353 }
18354 else if (attr->form_is_block ())
18355 {
18356 dwarf_block *blk = attr->as_block ();
18357 mpz_import (value->val, blk->size,
18358 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18359 1, 0, 0, blk->data);
18360 }
18361 else
18362 *value = gdb_mpz (attr->constant_value (1));
18363 }
18364
18365 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18366 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18367
18368 If the numerator and/or numerator attribute is missing,
18369 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18370 untouched. */
18371
18372 static void
18373 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18374 gdb_mpz *numerator, gdb_mpz *denominator)
18375 {
18376 struct attribute *num_attr, *denom_attr;
18377
18378 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18379 if (num_attr == nullptr)
18380 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18381 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18382
18383 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18384 if (denom_attr == nullptr)
18385 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18386 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18387
18388 if (num_attr == nullptr || denom_attr == nullptr)
18389 return;
18390
18391 get_mpz (cu, numerator, num_attr);
18392 get_mpz (cu, denominator, denom_attr);
18393 }
18394
18395 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18396 rational constant, rather than a signed one.
18397
18398 If the rational constant has a negative value, a complaint
18399 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18400
18401 static void
18402 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18403 struct dwarf2_cu *cu,
18404 gdb_mpz *numerator,
18405 gdb_mpz *denominator)
18406 {
18407 gdb_mpz num (1);
18408 gdb_mpz denom (1);
18409
18410 get_dwarf2_rational_constant (die, cu, &num, &denom);
18411 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18412 {
18413 mpz_neg (num.val, num.val);
18414 mpz_neg (denom.val, denom.val);
18415 }
18416 else if (mpz_sgn (num.val) == -1)
18417 {
18418 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18419 " in DIE at %s"),
18420 sect_offset_str (die->sect_off));
18421 return;
18422 }
18423 else if (mpz_sgn (denom.val) == -1)
18424 {
18425 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18426 " in DIE at %s"),
18427 sect_offset_str (die->sect_off));
18428 return;
18429 }
18430
18431 *numerator = std::move (num);
18432 *denominator = std::move (denom);
18433 }
18434
18435 /* Assuming that ENCODING is a string whose contents starting at the
18436 K'th character is "_nn" where "nn" is a decimal number, scan that
18437 number and set RESULT to the value. K is updated to point to the
18438 character immediately following the number.
18439
18440 If the string does not conform to the format described above, false
18441 is returned, and K may or may not be changed. */
18442
18443 static bool
18444 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18445 {
18446 /* The next character should be an underscore ('_') followed
18447 by a digit. */
18448 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18449 return false;
18450
18451 /* Skip the underscore. */
18452 k++;
18453 int start = k;
18454
18455 /* Determine the number of digits for our number. */
18456 while (isdigit (encoding[k]))
18457 k++;
18458 if (k == start)
18459 return false;
18460
18461 std::string copy (&encoding[start], k - start);
18462 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18463 return false;
18464
18465 return true;
18466 }
18467
18468 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18469 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18470 DENOM, update OFFSET, and return true on success. Return false on
18471 failure. */
18472
18473 static bool
18474 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18475 gdb_mpz *num, gdb_mpz *denom)
18476 {
18477 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18478 return false;
18479 return ada_get_gnat_encoded_number (encoding, offset, denom);
18480 }
18481
18482 /* Assuming DIE corresponds to a fixed point type, finish the creation
18483 of the corresponding TYPE by setting its type-specific data. CU is
18484 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18485 encodings. It is nullptr if the GNAT encoding should be
18486 ignored. */
18487
18488 static void
18489 finish_fixed_point_type (struct type *type, const char *suffix,
18490 struct die_info *die, struct dwarf2_cu *cu)
18491 {
18492 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18493 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18494
18495 /* If GNAT encodings are preferred, don't examine the
18496 attributes. */
18497 struct attribute *attr = nullptr;
18498 if (suffix == nullptr)
18499 {
18500 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18501 if (attr == nullptr)
18502 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18503 if (attr == nullptr)
18504 attr = dwarf2_attr (die, DW_AT_small, cu);
18505 }
18506
18507 /* Numerator and denominator of our fixed-point type's scaling factor.
18508 The default is a scaling factor of 1, which we use as a fallback
18509 when we are not able to decode it (problem with the debugging info,
18510 unsupported forms, bug in GDB, etc...). Using that as the default
18511 allows us to at least print the unscaled value, which might still
18512 be useful to a user. */
18513 gdb_mpz scale_num (1);
18514 gdb_mpz scale_denom (1);
18515
18516 if (attr == nullptr)
18517 {
18518 int offset = 0;
18519 if (suffix != nullptr
18520 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18521 &scale_denom)
18522 /* The number might be encoded as _nn_dd_nn_dd, where the
18523 second ratio is the 'small value. In this situation, we
18524 want the second value. */
18525 && (suffix[offset] != '_'
18526 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18527 &scale_denom)))
18528 {
18529 /* Found it. */
18530 }
18531 else
18532 {
18533 /* Scaling factor not found. Assume a scaling factor of 1,
18534 and hope for the best. At least the user will be able to
18535 see the encoded value. */
18536 scale_num = 1;
18537 scale_denom = 1;
18538 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18539 sect_offset_str (die->sect_off));
18540 }
18541 }
18542 else if (attr->name == DW_AT_binary_scale)
18543 {
18544 LONGEST scale_exp = attr->constant_value (0);
18545 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18546
18547 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18548 }
18549 else if (attr->name == DW_AT_decimal_scale)
18550 {
18551 LONGEST scale_exp = attr->constant_value (0);
18552 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18553
18554 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18555 }
18556 else if (attr->name == DW_AT_small)
18557 {
18558 struct die_info *scale_die;
18559 struct dwarf2_cu *scale_cu = cu;
18560
18561 scale_die = follow_die_ref (die, attr, &scale_cu);
18562 if (scale_die->tag == DW_TAG_constant)
18563 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18564 &scale_num, &scale_denom);
18565 else
18566 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18567 " (DIE at %s)"),
18568 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18569 }
18570 else
18571 {
18572 complaint (_("unsupported scale attribute %s for fixed-point type"
18573 " (DIE at %s)"),
18574 dwarf_attr_name (attr->name),
18575 sect_offset_str (die->sect_off));
18576 }
18577
18578 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18579 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18580 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18581 mpq_canonicalize (scaling_factor.val);
18582 }
18583
18584 /* The gnat-encoding suffix for fixed point. */
18585
18586 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18587
18588 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18589 "XF" suffix of the name. The text after this is what encodes the
18590 'small and 'delta information. Otherwise, return nullptr. */
18591
18592 static const char *
18593 gnat_encoded_fixed_point_type_info (const char *name)
18594 {
18595 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18596 }
18597
18598 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18599 (which may be different from NAME) to the architecture back-end to allow
18600 it to guess the correct format if necessary. */
18601
18602 static struct type *
18603 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18604 const char *name_hint, enum bfd_endian byte_order)
18605 {
18606 struct gdbarch *gdbarch = objfile->arch ();
18607 const struct floatformat **format;
18608 struct type *type;
18609
18610 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18611 if (format)
18612 type = init_float_type (objfile, bits, name, format, byte_order);
18613 else
18614 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18615
18616 return type;
18617 }
18618
18619 /* Allocate an integer type of size BITS and name NAME. */
18620
18621 static struct type *
18622 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18623 int bits, int unsigned_p, const char *name)
18624 {
18625 struct type *type;
18626
18627 /* Versions of Intel's C Compiler generate an integer type called "void"
18628 instead of using DW_TAG_unspecified_type. This has been seen on
18629 at least versions 14, 17, and 18. */
18630 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18631 && strcmp (name, "void") == 0)
18632 type = objfile_type (objfile)->builtin_void;
18633 else
18634 type = init_integer_type (objfile, bits, unsigned_p, name);
18635
18636 return type;
18637 }
18638
18639 /* Return true if DIE has a DW_AT_small attribute whose value is
18640 a constant rational, where both the numerator and denominator
18641 are equal to zero.
18642
18643 CU is the DIE's Compilation Unit. */
18644
18645 static bool
18646 has_zero_over_zero_small_attribute (struct die_info *die,
18647 struct dwarf2_cu *cu)
18648 {
18649 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18650 if (attr == nullptr)
18651 return false;
18652
18653 struct dwarf2_cu *scale_cu = cu;
18654 struct die_info *scale_die
18655 = follow_die_ref (die, attr, &scale_cu);
18656
18657 if (scale_die->tag != DW_TAG_constant)
18658 return false;
18659
18660 gdb_mpz num (1), denom (1);
18661 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18662 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18663 }
18664
18665 /* Initialise and return a floating point type of size BITS suitable for
18666 use as a component of a complex number. The NAME_HINT is passed through
18667 when initialising the floating point type and is the name of the complex
18668 type.
18669
18670 As DWARF doesn't currently provide an explicit name for the components
18671 of a complex number, but it can be helpful to have these components
18672 named, we try to select a suitable name based on the size of the
18673 component. */
18674 static struct type *
18675 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18676 struct objfile *objfile,
18677 int bits, const char *name_hint,
18678 enum bfd_endian byte_order)
18679 {
18680 gdbarch *gdbarch = objfile->arch ();
18681 struct type *tt = nullptr;
18682
18683 /* Try to find a suitable floating point builtin type of size BITS.
18684 We're going to use the name of this type as the name for the complex
18685 target type that we are about to create. */
18686 switch (cu->language)
18687 {
18688 case language_fortran:
18689 switch (bits)
18690 {
18691 case 32:
18692 tt = builtin_f_type (gdbarch)->builtin_real;
18693 break;
18694 case 64:
18695 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18696 break;
18697 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18698 case 128:
18699 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18700 break;
18701 }
18702 break;
18703 default:
18704 switch (bits)
18705 {
18706 case 32:
18707 tt = builtin_type (gdbarch)->builtin_float;
18708 break;
18709 case 64:
18710 tt = builtin_type (gdbarch)->builtin_double;
18711 break;
18712 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18713 case 128:
18714 tt = builtin_type (gdbarch)->builtin_long_double;
18715 break;
18716 }
18717 break;
18718 }
18719
18720 /* If the type we found doesn't match the size we were looking for, then
18721 pretend we didn't find a type at all, the complex target type we
18722 create will then be nameless. */
18723 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18724 tt = nullptr;
18725
18726 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18727 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18728 }
18729
18730 /* Find a representation of a given base type and install
18731 it in the TYPE field of the die. */
18732
18733 static struct type *
18734 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18735 {
18736 struct objfile *objfile = cu->per_objfile->objfile;
18737 struct type *type;
18738 struct attribute *attr;
18739 int encoding = 0, bits = 0;
18740 const char *name;
18741 gdbarch *arch;
18742
18743 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18744 if (attr != nullptr && attr->form_is_constant ())
18745 encoding = attr->constant_value (0);
18746 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18747 if (attr != nullptr)
18748 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18749 name = dwarf2_name (die, cu);
18750 if (!name)
18751 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18752
18753 arch = objfile->arch ();
18754 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18755
18756 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18757 if (attr != nullptr && attr->form_is_constant ())
18758 {
18759 int endianity = attr->constant_value (0);
18760
18761 switch (endianity)
18762 {
18763 case DW_END_big:
18764 byte_order = BFD_ENDIAN_BIG;
18765 break;
18766 case DW_END_little:
18767 byte_order = BFD_ENDIAN_LITTLE;
18768 break;
18769 default:
18770 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18771 break;
18772 }
18773 }
18774
18775 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18776 && cu->language == language_ada
18777 && has_zero_over_zero_small_attribute (die, cu))
18778 {
18779 /* brobecker/2018-02-24: This is a fixed point type for which
18780 the scaling factor is represented as fraction whose value
18781 does not make sense (zero divided by zero), so we should
18782 normally never see these. However, there is a small category
18783 of fixed point types for which GNAT is unable to provide
18784 the scaling factor via the standard DWARF mechanisms, and
18785 for which the info is provided via the GNAT encodings instead.
18786 This is likely what this DIE is about. */
18787 encoding = (encoding == DW_ATE_signed_fixed
18788 ? DW_ATE_signed
18789 : DW_ATE_unsigned);
18790 }
18791
18792 /* With GNAT encodings, fixed-point information will be encoded in
18793 the type name. Note that this can also occur with the above
18794 zero-over-zero case, which is why this is a separate "if" rather
18795 than an "else if". */
18796 const char *gnat_encoding_suffix = nullptr;
18797 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18798 && cu->language == language_ada
18799 && name != nullptr)
18800 {
18801 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18802 if (gnat_encoding_suffix != nullptr)
18803 {
18804 gdb_assert (startswith (gnat_encoding_suffix,
18805 GNAT_FIXED_POINT_SUFFIX));
18806 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18807 name, gnat_encoding_suffix - name);
18808 /* Use -1 here so that SUFFIX points at the "_" after the
18809 "XF". */
18810 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18811
18812 encoding = (encoding == DW_ATE_signed
18813 ? DW_ATE_signed_fixed
18814 : DW_ATE_unsigned_fixed);
18815 }
18816 }
18817
18818 switch (encoding)
18819 {
18820 case DW_ATE_address:
18821 /* Turn DW_ATE_address into a void * pointer. */
18822 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18823 type = init_pointer_type (objfile, bits, name, type);
18824 break;
18825 case DW_ATE_boolean:
18826 type = init_boolean_type (objfile, bits, 1, name);
18827 break;
18828 case DW_ATE_complex_float:
18829 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18830 byte_order);
18831 if (type->code () == TYPE_CODE_ERROR)
18832 {
18833 if (name == nullptr)
18834 {
18835 struct obstack *obstack
18836 = &cu->per_objfile->objfile->objfile_obstack;
18837 name = obconcat (obstack, "_Complex ", type->name (),
18838 nullptr);
18839 }
18840 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18841 }
18842 else
18843 type = init_complex_type (name, type);
18844 break;
18845 case DW_ATE_decimal_float:
18846 type = init_decfloat_type (objfile, bits, name);
18847 break;
18848 case DW_ATE_float:
18849 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18850 break;
18851 case DW_ATE_signed:
18852 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18853 break;
18854 case DW_ATE_unsigned:
18855 if (cu->language == language_fortran
18856 && name
18857 && startswith (name, "character("))
18858 type = init_character_type (objfile, bits, 1, name);
18859 else
18860 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18861 break;
18862 case DW_ATE_signed_char:
18863 if (cu->language == language_ada || cu->language == language_m2
18864 || cu->language == language_pascal
18865 || cu->language == language_fortran)
18866 type = init_character_type (objfile, bits, 0, name);
18867 else
18868 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18869 break;
18870 case DW_ATE_unsigned_char:
18871 if (cu->language == language_ada || cu->language == language_m2
18872 || cu->language == language_pascal
18873 || cu->language == language_fortran
18874 || cu->language == language_rust)
18875 type = init_character_type (objfile, bits, 1, name);
18876 else
18877 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18878 break;
18879 case DW_ATE_UTF:
18880 {
18881 if (bits == 16)
18882 type = builtin_type (arch)->builtin_char16;
18883 else if (bits == 32)
18884 type = builtin_type (arch)->builtin_char32;
18885 else
18886 {
18887 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18888 bits);
18889 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18890 }
18891 return set_die_type (die, type, cu);
18892 }
18893 break;
18894 case DW_ATE_signed_fixed:
18895 type = init_fixed_point_type (objfile, bits, 0, name);
18896 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18897 break;
18898 case DW_ATE_unsigned_fixed:
18899 type = init_fixed_point_type (objfile, bits, 1, name);
18900 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18901 break;
18902
18903 default:
18904 complaint (_("unsupported DW_AT_encoding: '%s'"),
18905 dwarf_type_encoding_name (encoding));
18906 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18907 break;
18908 }
18909
18910 if (name && strcmp (name, "char") == 0)
18911 type->set_has_no_signedness (true);
18912
18913 maybe_set_alignment (cu, die, type);
18914
18915 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18916
18917 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18918 {
18919 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18920 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18921 {
18922 unsigned real_bit_size = attr->as_unsigned ();
18923 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18924 /* Only use the attributes if they make sense together. */
18925 if (attr == nullptr
18926 || (attr->as_unsigned () + real_bit_size
18927 <= 8 * TYPE_LENGTH (type)))
18928 {
18929 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18930 = real_bit_size;
18931 if (attr != nullptr)
18932 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18933 = attr->as_unsigned ();
18934 }
18935 }
18936 }
18937
18938 return set_die_type (die, type, cu);
18939 }
18940
18941 /* Parse dwarf attribute if it's a block, reference or constant and put the
18942 resulting value of the attribute into struct bound_prop.
18943 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18944
18945 static int
18946 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18947 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18948 struct type *default_type)
18949 {
18950 struct dwarf2_property_baton *baton;
18951 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18952 struct objfile *objfile = per_objfile->objfile;
18953 struct obstack *obstack = &objfile->objfile_obstack;
18954
18955 gdb_assert (default_type != NULL);
18956
18957 if (attr == NULL || prop == NULL)
18958 return 0;
18959
18960 if (attr->form_is_block ())
18961 {
18962 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18963 baton->property_type = default_type;
18964 baton->locexpr.per_cu = cu->per_cu;
18965 baton->locexpr.per_objfile = per_objfile;
18966
18967 struct dwarf_block *block = attr->as_block ();
18968 baton->locexpr.size = block->size;
18969 baton->locexpr.data = block->data;
18970 switch (attr->name)
18971 {
18972 case DW_AT_string_length:
18973 baton->locexpr.is_reference = true;
18974 break;
18975 default:
18976 baton->locexpr.is_reference = false;
18977 break;
18978 }
18979
18980 prop->set_locexpr (baton);
18981 gdb_assert (prop->baton () != NULL);
18982 }
18983 else if (attr->form_is_ref ())
18984 {
18985 struct dwarf2_cu *target_cu = cu;
18986 struct die_info *target_die;
18987 struct attribute *target_attr;
18988
18989 target_die = follow_die_ref (die, attr, &target_cu);
18990 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18991 if (target_attr == NULL)
18992 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18993 target_cu);
18994 if (target_attr == NULL)
18995 return 0;
18996
18997 switch (target_attr->name)
18998 {
18999 case DW_AT_location:
19000 if (target_attr->form_is_section_offset ())
19001 {
19002 baton = XOBNEW (obstack, struct dwarf2_property_baton);
19003 baton->property_type = die_type (target_die, target_cu);
19004 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
19005 prop->set_loclist (baton);
19006 gdb_assert (prop->baton () != NULL);
19007 }
19008 else if (target_attr->form_is_block ())
19009 {
19010 baton = XOBNEW (obstack, struct dwarf2_property_baton);
19011 baton->property_type = die_type (target_die, target_cu);
19012 baton->locexpr.per_cu = cu->per_cu;
19013 baton->locexpr.per_objfile = per_objfile;
19014 struct dwarf_block *block = target_attr->as_block ();
19015 baton->locexpr.size = block->size;
19016 baton->locexpr.data = block->data;
19017 baton->locexpr.is_reference = true;
19018 prop->set_locexpr (baton);
19019 gdb_assert (prop->baton () != NULL);
19020 }
19021 else
19022 {
19023 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19024 "dynamic property");
19025 return 0;
19026 }
19027 break;
19028 case DW_AT_data_member_location:
19029 {
19030 LONGEST offset;
19031
19032 if (!handle_data_member_location (target_die, target_cu,
19033 &offset))
19034 return 0;
19035
19036 baton = XOBNEW (obstack, struct dwarf2_property_baton);
19037 baton->property_type = read_type_die (target_die->parent,
19038 target_cu);
19039 baton->offset_info.offset = offset;
19040 baton->offset_info.type = die_type (target_die, target_cu);
19041 prop->set_addr_offset (baton);
19042 break;
19043 }
19044 }
19045 }
19046 else if (attr->form_is_constant ())
19047 prop->set_const_val (attr->constant_value (0));
19048 else
19049 {
19050 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
19051 dwarf2_name (die, cu));
19052 return 0;
19053 }
19054
19055 return 1;
19056 }
19057
19058 /* See read.h. */
19059
19060 struct type *
19061 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
19062 {
19063 struct type *int_type;
19064
19065 /* Helper macro to examine the various builtin types. */
19066 #define TRY_TYPE(F) \
19067 int_type = (unsigned_p \
19068 ? objfile_type (objfile)->builtin_unsigned_ ## F \
19069 : objfile_type (objfile)->builtin_ ## F); \
19070 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
19071 return int_type
19072
19073 TRY_TYPE (char);
19074 TRY_TYPE (short);
19075 TRY_TYPE (int);
19076 TRY_TYPE (long);
19077 TRY_TYPE (long_long);
19078
19079 #undef TRY_TYPE
19080
19081 gdb_assert_not_reached ("unable to find suitable integer type");
19082 }
19083
19084 /* See read.h. */
19085
19086 struct type *
19087 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
19088 {
19089 int addr_size = this->per_cu->addr_size ();
19090 return this->per_objfile->int_type (addr_size, unsigned_p);
19091 }
19092
19093 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
19094 present (which is valid) then compute the default type based on the
19095 compilation units address size. */
19096
19097 static struct type *
19098 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
19099 {
19100 struct type *index_type = die_type (die, cu);
19101
19102 /* Dwarf-2 specifications explicitly allows to create subrange types
19103 without specifying a base type.
19104 In that case, the base type must be set to the type of
19105 the lower bound, upper bound or count, in that order, if any of these
19106 three attributes references an object that has a type.
19107 If no base type is found, the Dwarf-2 specifications say that
19108 a signed integer type of size equal to the size of an address should
19109 be used.
19110 For the following C code: `extern char gdb_int [];'
19111 GCC produces an empty range DIE.
19112 FIXME: muller/2010-05-28: Possible references to object for low bound,
19113 high bound or count are not yet handled by this code. */
19114 if (index_type->code () == TYPE_CODE_VOID)
19115 index_type = cu->addr_sized_int_type (false);
19116
19117 return index_type;
19118 }
19119
19120 /* Read the given DW_AT_subrange DIE. */
19121
19122 static struct type *
19123 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
19124 {
19125 struct type *base_type, *orig_base_type;
19126 struct type *range_type;
19127 struct attribute *attr;
19128 struct dynamic_prop low, high;
19129 int low_default_is_valid;
19130 int high_bound_is_count = 0;
19131 const char *name;
19132 ULONGEST negative_mask;
19133
19134 orig_base_type = read_subrange_index_type (die, cu);
19135
19136 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
19137 whereas the real type might be. So, we use ORIG_BASE_TYPE when
19138 creating the range type, but we use the result of check_typedef
19139 when examining properties of the type. */
19140 base_type = check_typedef (orig_base_type);
19141
19142 /* The die_type call above may have already set the type for this DIE. */
19143 range_type = get_die_type (die, cu);
19144 if (range_type)
19145 return range_type;
19146
19147 high.set_const_val (0);
19148
19149 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19150 omitting DW_AT_lower_bound. */
19151 switch (cu->language)
19152 {
19153 case language_c:
19154 case language_cplus:
19155 low.set_const_val (0);
19156 low_default_is_valid = 1;
19157 break;
19158 case language_fortran:
19159 low.set_const_val (1);
19160 low_default_is_valid = 1;
19161 break;
19162 case language_d:
19163 case language_objc:
19164 case language_rust:
19165 low.set_const_val (0);
19166 low_default_is_valid = (cu->header.version >= 4);
19167 break;
19168 case language_ada:
19169 case language_m2:
19170 case language_pascal:
19171 low.set_const_val (1);
19172 low_default_is_valid = (cu->header.version >= 4);
19173 break;
19174 default:
19175 low.set_const_val (0);
19176 low_default_is_valid = 0;
19177 break;
19178 }
19179
19180 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
19181 if (attr != nullptr)
19182 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
19183 else if (!low_default_is_valid)
19184 complaint (_("Missing DW_AT_lower_bound "
19185 "- DIE at %s [in module %s]"),
19186 sect_offset_str (die->sect_off),
19187 objfile_name (cu->per_objfile->objfile));
19188
19189 struct attribute *attr_ub, *attr_count;
19190 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
19191 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19192 {
19193 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
19194 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19195 {
19196 /* If bounds are constant do the final calculation here. */
19197 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19198 high.set_const_val (low.const_val () + high.const_val () - 1);
19199 else
19200 high_bound_is_count = 1;
19201 }
19202 else
19203 {
19204 if (attr_ub != NULL)
19205 complaint (_("Unresolved DW_AT_upper_bound "
19206 "- DIE at %s [in module %s]"),
19207 sect_offset_str (die->sect_off),
19208 objfile_name (cu->per_objfile->objfile));
19209 if (attr_count != NULL)
19210 complaint (_("Unresolved DW_AT_count "
19211 "- DIE at %s [in module %s]"),
19212 sect_offset_str (die->sect_off),
19213 objfile_name (cu->per_objfile->objfile));
19214 }
19215 }
19216
19217 LONGEST bias = 0;
19218 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
19219 if (bias_attr != nullptr && bias_attr->form_is_constant ())
19220 bias = bias_attr->constant_value (0);
19221
19222 /* Normally, the DWARF producers are expected to use a signed
19223 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19224 But this is unfortunately not always the case, as witnessed
19225 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19226 is used instead. To work around that ambiguity, we treat
19227 the bounds as signed, and thus sign-extend their values, when
19228 the base type is signed. */
19229 negative_mask =
19230 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19231 if (low.kind () == PROP_CONST
19232 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19233 low.set_const_val (low.const_val () | negative_mask);
19234 if (high.kind () == PROP_CONST
19235 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19236 high.set_const_val (high.const_val () | negative_mask);
19237
19238 /* Check for bit and byte strides. */
19239 struct dynamic_prop byte_stride_prop;
19240 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19241 if (attr_byte_stride != nullptr)
19242 {
19243 struct type *prop_type = cu->addr_sized_int_type (false);
19244 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19245 prop_type);
19246 }
19247
19248 struct dynamic_prop bit_stride_prop;
19249 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19250 if (attr_bit_stride != nullptr)
19251 {
19252 /* It only makes sense to have either a bit or byte stride. */
19253 if (attr_byte_stride != nullptr)
19254 {
19255 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19256 "- DIE at %s [in module %s]"),
19257 sect_offset_str (die->sect_off),
19258 objfile_name (cu->per_objfile->objfile));
19259 attr_bit_stride = nullptr;
19260 }
19261 else
19262 {
19263 struct type *prop_type = cu->addr_sized_int_type (false);
19264 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19265 prop_type);
19266 }
19267 }
19268
19269 if (attr_byte_stride != nullptr
19270 || attr_bit_stride != nullptr)
19271 {
19272 bool byte_stride_p = (attr_byte_stride != nullptr);
19273 struct dynamic_prop *stride
19274 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19275
19276 range_type
19277 = create_range_type_with_stride (NULL, orig_base_type, &low,
19278 &high, bias, stride, byte_stride_p);
19279 }
19280 else
19281 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19282
19283 if (high_bound_is_count)
19284 range_type->bounds ()->flag_upper_bound_is_count = 1;
19285
19286 /* Ada expects an empty array on no boundary attributes. */
19287 if (attr == NULL && cu->language != language_ada)
19288 range_type->bounds ()->high.set_undefined ();
19289
19290 name = dwarf2_name (die, cu);
19291 if (name)
19292 range_type->set_name (name);
19293
19294 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19295 if (attr != nullptr)
19296 TYPE_LENGTH (range_type) = attr->constant_value (0);
19297
19298 maybe_set_alignment (cu, die, range_type);
19299
19300 set_die_type (die, range_type, cu);
19301
19302 /* set_die_type should be already done. */
19303 set_descriptive_type (range_type, die, cu);
19304
19305 return range_type;
19306 }
19307
19308 static struct type *
19309 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19310 {
19311 struct type *type;
19312
19313 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19314 type->set_name (dwarf2_name (die, cu));
19315
19316 /* In Ada, an unspecified type is typically used when the description
19317 of the type is deferred to a different unit. When encountering
19318 such a type, we treat it as a stub, and try to resolve it later on,
19319 when needed. */
19320 if (cu->language == language_ada)
19321 type->set_is_stub (true);
19322
19323 return set_die_type (die, type, cu);
19324 }
19325
19326 /* Read a single die and all its descendents. Set the die's sibling
19327 field to NULL; set other fields in the die correctly, and set all
19328 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19329 location of the info_ptr after reading all of those dies. PARENT
19330 is the parent of the die in question. */
19331
19332 static struct die_info *
19333 read_die_and_children (const struct die_reader_specs *reader,
19334 const gdb_byte *info_ptr,
19335 const gdb_byte **new_info_ptr,
19336 struct die_info *parent)
19337 {
19338 struct die_info *die;
19339 const gdb_byte *cur_ptr;
19340
19341 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19342 if (die == NULL)
19343 {
19344 *new_info_ptr = cur_ptr;
19345 return NULL;
19346 }
19347 store_in_ref_table (die, reader->cu);
19348
19349 if (die->has_children)
19350 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19351 else
19352 {
19353 die->child = NULL;
19354 *new_info_ptr = cur_ptr;
19355 }
19356
19357 die->sibling = NULL;
19358 die->parent = parent;
19359 return die;
19360 }
19361
19362 /* Read a die, all of its descendents, and all of its siblings; set
19363 all of the fields of all of the dies correctly. Arguments are as
19364 in read_die_and_children. */
19365
19366 static struct die_info *
19367 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19368 const gdb_byte *info_ptr,
19369 const gdb_byte **new_info_ptr,
19370 struct die_info *parent)
19371 {
19372 struct die_info *first_die, *last_sibling;
19373 const gdb_byte *cur_ptr;
19374
19375 cur_ptr = info_ptr;
19376 first_die = last_sibling = NULL;
19377
19378 while (1)
19379 {
19380 struct die_info *die
19381 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19382
19383 if (die == NULL)
19384 {
19385 *new_info_ptr = cur_ptr;
19386 return first_die;
19387 }
19388
19389 if (!first_die)
19390 first_die = die;
19391 else
19392 last_sibling->sibling = die;
19393
19394 last_sibling = die;
19395 }
19396 }
19397
19398 /* Read a die, all of its descendents, and all of its siblings; set
19399 all of the fields of all of the dies correctly. Arguments are as
19400 in read_die_and_children.
19401 This the main entry point for reading a DIE and all its children. */
19402
19403 static struct die_info *
19404 read_die_and_siblings (const struct die_reader_specs *reader,
19405 const gdb_byte *info_ptr,
19406 const gdb_byte **new_info_ptr,
19407 struct die_info *parent)
19408 {
19409 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19410 new_info_ptr, parent);
19411
19412 if (dwarf_die_debug)
19413 {
19414 fprintf_unfiltered (gdb_stdlog,
19415 "Read die from %s@0x%x of %s:\n",
19416 reader->die_section->get_name (),
19417 (unsigned) (info_ptr - reader->die_section->buffer),
19418 bfd_get_filename (reader->abfd));
19419 dump_die (die, dwarf_die_debug);
19420 }
19421
19422 return die;
19423 }
19424
19425 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19426 attributes.
19427 The caller is responsible for filling in the extra attributes
19428 and updating (*DIEP)->num_attrs.
19429 Set DIEP to point to a newly allocated die with its information,
19430 except for its child, sibling, and parent fields. */
19431
19432 static const gdb_byte *
19433 read_full_die_1 (const struct die_reader_specs *reader,
19434 struct die_info **diep, const gdb_byte *info_ptr,
19435 int num_extra_attrs)
19436 {
19437 unsigned int abbrev_number, bytes_read, i;
19438 const struct abbrev_info *abbrev;
19439 struct die_info *die;
19440 struct dwarf2_cu *cu = reader->cu;
19441 bfd *abfd = reader->abfd;
19442
19443 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19444 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19445 info_ptr += bytes_read;
19446 if (!abbrev_number)
19447 {
19448 *diep = NULL;
19449 return info_ptr;
19450 }
19451
19452 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19453 if (!abbrev)
19454 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19455 abbrev_number,
19456 bfd_get_filename (abfd));
19457
19458 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19459 die->sect_off = sect_off;
19460 die->tag = abbrev->tag;
19461 die->abbrev = abbrev_number;
19462 die->has_children = abbrev->has_children;
19463
19464 /* Make the result usable.
19465 The caller needs to update num_attrs after adding the extra
19466 attributes. */
19467 die->num_attrs = abbrev->num_attrs;
19468
19469 bool any_need_reprocess = false;
19470 for (i = 0; i < abbrev->num_attrs; ++i)
19471 {
19472 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19473 info_ptr);
19474 if (die->attrs[i].requires_reprocessing_p ())
19475 any_need_reprocess = true;
19476 }
19477
19478 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19479 if (attr != nullptr && attr->form_is_unsigned ())
19480 cu->str_offsets_base = attr->as_unsigned ();
19481
19482 attr = die->attr (DW_AT_loclists_base);
19483 if (attr != nullptr)
19484 cu->loclist_base = attr->as_unsigned ();
19485
19486 auto maybe_addr_base = die->addr_base ();
19487 if (maybe_addr_base.has_value ())
19488 cu->addr_base = *maybe_addr_base;
19489
19490 attr = die->attr (DW_AT_rnglists_base);
19491 if (attr != nullptr)
19492 cu->rnglists_base = attr->as_unsigned ();
19493
19494 if (any_need_reprocess)
19495 {
19496 for (i = 0; i < abbrev->num_attrs; ++i)
19497 {
19498 if (die->attrs[i].requires_reprocessing_p ())
19499 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19500 }
19501 }
19502 *diep = die;
19503 return info_ptr;
19504 }
19505
19506 /* Read a die and all its attributes.
19507 Set DIEP to point to a newly allocated die with its information,
19508 except for its child, sibling, and parent fields. */
19509
19510 static const gdb_byte *
19511 read_full_die (const struct die_reader_specs *reader,
19512 struct die_info **diep, const gdb_byte *info_ptr)
19513 {
19514 const gdb_byte *result;
19515
19516 result = read_full_die_1 (reader, diep, info_ptr, 0);
19517
19518 if (dwarf_die_debug)
19519 {
19520 fprintf_unfiltered (gdb_stdlog,
19521 "Read die from %s@0x%x of %s:\n",
19522 reader->die_section->get_name (),
19523 (unsigned) (info_ptr - reader->die_section->buffer),
19524 bfd_get_filename (reader->abfd));
19525 dump_die (*diep, dwarf_die_debug);
19526 }
19527
19528 return result;
19529 }
19530 \f
19531
19532 /* Returns nonzero if TAG represents a type that we might generate a partial
19533 symbol for. */
19534
19535 static int
19536 is_type_tag_for_partial (int tag, enum language lang)
19537 {
19538 switch (tag)
19539 {
19540 #if 0
19541 /* Some types that would be reasonable to generate partial symbols for,
19542 that we don't at present. Note that normally this does not
19543 matter, mainly because C compilers don't give names to these
19544 types, but instead emit DW_TAG_typedef. */
19545 case DW_TAG_file_type:
19546 case DW_TAG_ptr_to_member_type:
19547 case DW_TAG_set_type:
19548 case DW_TAG_string_type:
19549 case DW_TAG_subroutine_type:
19550 #endif
19551
19552 /* GNAT may emit an array with a name, but no typedef, so we
19553 need to make a symbol in this case. */
19554 case DW_TAG_array_type:
19555 return lang == language_ada;
19556
19557 case DW_TAG_base_type:
19558 case DW_TAG_class_type:
19559 case DW_TAG_interface_type:
19560 case DW_TAG_enumeration_type:
19561 case DW_TAG_structure_type:
19562 case DW_TAG_subrange_type:
19563 case DW_TAG_typedef:
19564 case DW_TAG_union_type:
19565 return 1;
19566 default:
19567 return 0;
19568 }
19569 }
19570
19571 /* Load all DIEs that are interesting for partial symbols into memory. */
19572
19573 static struct partial_die_info *
19574 load_partial_dies (const struct die_reader_specs *reader,
19575 const gdb_byte *info_ptr, int building_psymtab)
19576 {
19577 struct dwarf2_cu *cu = reader->cu;
19578 struct objfile *objfile = cu->per_objfile->objfile;
19579 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19580 unsigned int bytes_read;
19581 unsigned int load_all = 0;
19582 int nesting_level = 1;
19583
19584 parent_die = NULL;
19585 last_die = NULL;
19586
19587 gdb_assert (cu->per_cu != NULL);
19588 if (cu->per_cu->load_all_dies)
19589 load_all = 1;
19590
19591 cu->partial_dies
19592 = htab_create_alloc_ex (cu->header.length / 12,
19593 partial_die_hash,
19594 partial_die_eq,
19595 NULL,
19596 &cu->comp_unit_obstack,
19597 hashtab_obstack_allocate,
19598 dummy_obstack_deallocate);
19599
19600 while (1)
19601 {
19602 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19603 &bytes_read);
19604
19605 /* A NULL abbrev means the end of a series of children. */
19606 if (abbrev == NULL)
19607 {
19608 if (--nesting_level == 0)
19609 return first_die;
19610
19611 info_ptr += bytes_read;
19612 last_die = parent_die;
19613 parent_die = parent_die->die_parent;
19614 continue;
19615 }
19616
19617 /* Check for template arguments. We never save these; if
19618 they're seen, we just mark the parent, and go on our way. */
19619 if (parent_die != NULL
19620 && cu->language == language_cplus
19621 && (abbrev->tag == DW_TAG_template_type_param
19622 || abbrev->tag == DW_TAG_template_value_param))
19623 {
19624 parent_die->has_template_arguments = 1;
19625
19626 if (!load_all)
19627 {
19628 /* We don't need a partial DIE for the template argument. */
19629 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19630 continue;
19631 }
19632 }
19633
19634 /* We only recurse into c++ subprograms looking for template arguments.
19635 Skip their other children. */
19636 if (!load_all
19637 && cu->language == language_cplus
19638 && parent_die != NULL
19639 && parent_die->tag == DW_TAG_subprogram
19640 && abbrev->tag != DW_TAG_inlined_subroutine)
19641 {
19642 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19643 continue;
19644 }
19645
19646 /* Check whether this DIE is interesting enough to save. Normally
19647 we would not be interested in members here, but there may be
19648 later variables referencing them via DW_AT_specification (for
19649 static members). */
19650 if (!load_all
19651 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19652 && abbrev->tag != DW_TAG_constant
19653 && abbrev->tag != DW_TAG_enumerator
19654 && abbrev->tag != DW_TAG_subprogram
19655 && abbrev->tag != DW_TAG_inlined_subroutine
19656 && abbrev->tag != DW_TAG_lexical_block
19657 && abbrev->tag != DW_TAG_variable
19658 && abbrev->tag != DW_TAG_namespace
19659 && abbrev->tag != DW_TAG_module
19660 && abbrev->tag != DW_TAG_member
19661 && abbrev->tag != DW_TAG_imported_unit
19662 && abbrev->tag != DW_TAG_imported_declaration)
19663 {
19664 /* Otherwise we skip to the next sibling, if any. */
19665 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19666 continue;
19667 }
19668
19669 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19670 abbrev);
19671
19672 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19673
19674 /* This two-pass algorithm for processing partial symbols has a
19675 high cost in cache pressure. Thus, handle some simple cases
19676 here which cover the majority of C partial symbols. DIEs
19677 which neither have specification tags in them, nor could have
19678 specification tags elsewhere pointing at them, can simply be
19679 processed and discarded.
19680
19681 This segment is also optional; scan_partial_symbols and
19682 add_partial_symbol will handle these DIEs if we chain
19683 them in normally. When compilers which do not emit large
19684 quantities of duplicate debug information are more common,
19685 this code can probably be removed. */
19686
19687 /* Any complete simple types at the top level (pretty much all
19688 of them, for a language without namespaces), can be processed
19689 directly. */
19690 if (parent_die == NULL
19691 && pdi.has_specification == 0
19692 && pdi.is_declaration == 0
19693 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19694 || pdi.tag == DW_TAG_base_type
19695 || pdi.tag == DW_TAG_array_type
19696 || pdi.tag == DW_TAG_subrange_type))
19697 {
19698 if (building_psymtab && pdi.raw_name != NULL)
19699 add_partial_symbol (&pdi, cu);
19700
19701 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19702 continue;
19703 }
19704
19705 /* The exception for DW_TAG_typedef with has_children above is
19706 a workaround of GCC PR debug/47510. In the case of this complaint
19707 type_name_or_error will error on such types later.
19708
19709 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19710 it could not find the child DIEs referenced later, this is checked
19711 above. In correct DWARF DW_TAG_typedef should have no children. */
19712
19713 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19714 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19715 "- DIE at %s [in module %s]"),
19716 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19717
19718 /* If we're at the second level, and we're an enumerator, and
19719 our parent has no specification (meaning possibly lives in a
19720 namespace elsewhere), then we can add the partial symbol now
19721 instead of queueing it. */
19722 if (pdi.tag == DW_TAG_enumerator
19723 && parent_die != NULL
19724 && parent_die->die_parent == NULL
19725 && parent_die->tag == DW_TAG_enumeration_type
19726 && parent_die->has_specification == 0)
19727 {
19728 if (pdi.raw_name == NULL)
19729 complaint (_("malformed enumerator DIE ignored"));
19730 else if (building_psymtab)
19731 add_partial_symbol (&pdi, cu);
19732
19733 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19734 continue;
19735 }
19736
19737 struct partial_die_info *part_die
19738 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19739
19740 /* We'll save this DIE so link it in. */
19741 part_die->die_parent = parent_die;
19742 part_die->die_sibling = NULL;
19743 part_die->die_child = NULL;
19744
19745 if (last_die && last_die == parent_die)
19746 last_die->die_child = part_die;
19747 else if (last_die)
19748 last_die->die_sibling = part_die;
19749
19750 last_die = part_die;
19751
19752 if (first_die == NULL)
19753 first_die = part_die;
19754
19755 /* Maybe add the DIE to the hash table. Not all DIEs that we
19756 find interesting need to be in the hash table, because we
19757 also have the parent/sibling/child chains; only those that we
19758 might refer to by offset later during partial symbol reading.
19759
19760 For now this means things that might have be the target of a
19761 DW_AT_specification, DW_AT_abstract_origin, or
19762 DW_AT_extension. DW_AT_extension will refer only to
19763 namespaces; DW_AT_abstract_origin refers to functions (and
19764 many things under the function DIE, but we do not recurse
19765 into function DIEs during partial symbol reading) and
19766 possibly variables as well; DW_AT_specification refers to
19767 declarations. Declarations ought to have the DW_AT_declaration
19768 flag. It happens that GCC forgets to put it in sometimes, but
19769 only for functions, not for types.
19770
19771 Adding more things than necessary to the hash table is harmless
19772 except for the performance cost. Adding too few will result in
19773 wasted time in find_partial_die, when we reread the compilation
19774 unit with load_all_dies set. */
19775
19776 if (load_all
19777 || abbrev->tag == DW_TAG_constant
19778 || abbrev->tag == DW_TAG_subprogram
19779 || abbrev->tag == DW_TAG_variable
19780 || abbrev->tag == DW_TAG_namespace
19781 || part_die->is_declaration)
19782 {
19783 void **slot;
19784
19785 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19786 to_underlying (part_die->sect_off),
19787 INSERT);
19788 *slot = part_die;
19789 }
19790
19791 /* For some DIEs we want to follow their children (if any). For C
19792 we have no reason to follow the children of structures; for other
19793 languages we have to, so that we can get at method physnames
19794 to infer fully qualified class names, for DW_AT_specification,
19795 and for C++ template arguments. For C++, we also look one level
19796 inside functions to find template arguments (if the name of the
19797 function does not already contain the template arguments).
19798
19799 For Ada and Fortran, we need to scan the children of subprograms
19800 and lexical blocks as well because these languages allow the
19801 definition of nested entities that could be interesting for the
19802 debugger, such as nested subprograms for instance. */
19803 if (last_die->has_children
19804 && (load_all
19805 || last_die->tag == DW_TAG_namespace
19806 || last_die->tag == DW_TAG_module
19807 || last_die->tag == DW_TAG_enumeration_type
19808 || (cu->language == language_cplus
19809 && last_die->tag == DW_TAG_subprogram
19810 && (last_die->raw_name == NULL
19811 || strchr (last_die->raw_name, '<') == NULL))
19812 || (cu->language != language_c
19813 && (last_die->tag == DW_TAG_class_type
19814 || last_die->tag == DW_TAG_interface_type
19815 || last_die->tag == DW_TAG_structure_type
19816 || last_die->tag == DW_TAG_union_type))
19817 || ((cu->language == language_ada
19818 || cu->language == language_fortran)
19819 && (last_die->tag == DW_TAG_subprogram
19820 || last_die->tag == DW_TAG_lexical_block))))
19821 {
19822 nesting_level++;
19823 parent_die = last_die;
19824 continue;
19825 }
19826
19827 /* Otherwise we skip to the next sibling, if any. */
19828 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19829
19830 /* Back to the top, do it again. */
19831 }
19832 }
19833
19834 partial_die_info::partial_die_info (sect_offset sect_off_,
19835 const struct abbrev_info *abbrev)
19836 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19837 {
19838 }
19839
19840 /* See class definition. */
19841
19842 const char *
19843 partial_die_info::name (dwarf2_cu *cu)
19844 {
19845 if (!canonical_name && raw_name != nullptr)
19846 {
19847 struct objfile *objfile = cu->per_objfile->objfile;
19848 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19849 canonical_name = 1;
19850 }
19851
19852 return raw_name;
19853 }
19854
19855 /* Read a minimal amount of information into the minimal die structure.
19856 INFO_PTR should point just after the initial uleb128 of a DIE. */
19857
19858 const gdb_byte *
19859 partial_die_info::read (const struct die_reader_specs *reader,
19860 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19861 {
19862 struct dwarf2_cu *cu = reader->cu;
19863 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19864 unsigned int i;
19865 int has_low_pc_attr = 0;
19866 int has_high_pc_attr = 0;
19867 int high_pc_relative = 0;
19868
19869 for (i = 0; i < abbrev.num_attrs; ++i)
19870 {
19871 attribute attr;
19872 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19873 /* String and address offsets that need to do the reprocessing have
19874 already been read at this point, so there is no need to wait until
19875 the loop terminates to do the reprocessing. */
19876 if (attr.requires_reprocessing_p ())
19877 read_attribute_reprocess (reader, &attr, tag);
19878 /* Store the data if it is of an attribute we want to keep in a
19879 partial symbol table. */
19880 switch (attr.name)
19881 {
19882 case DW_AT_name:
19883 switch (tag)
19884 {
19885 case DW_TAG_compile_unit:
19886 case DW_TAG_partial_unit:
19887 case DW_TAG_type_unit:
19888 /* Compilation units have a DW_AT_name that is a filename, not
19889 a source language identifier. */
19890 case DW_TAG_enumeration_type:
19891 case DW_TAG_enumerator:
19892 /* These tags always have simple identifiers already; no need
19893 to canonicalize them. */
19894 canonical_name = 1;
19895 raw_name = attr.as_string ();
19896 break;
19897 default:
19898 canonical_name = 0;
19899 raw_name = attr.as_string ();
19900 break;
19901 }
19902 break;
19903 case DW_AT_linkage_name:
19904 case DW_AT_MIPS_linkage_name:
19905 /* Note that both forms of linkage name might appear. We
19906 assume they will be the same, and we only store the last
19907 one we see. */
19908 linkage_name = attr.as_string ();
19909 break;
19910 case DW_AT_low_pc:
19911 has_low_pc_attr = 1;
19912 lowpc = attr.as_address ();
19913 break;
19914 case DW_AT_high_pc:
19915 has_high_pc_attr = 1;
19916 highpc = attr.as_address ();
19917 if (cu->header.version >= 4 && attr.form_is_constant ())
19918 high_pc_relative = 1;
19919 break;
19920 case DW_AT_location:
19921 /* Support the .debug_loc offsets. */
19922 if (attr.form_is_block ())
19923 {
19924 d.locdesc = attr.as_block ();
19925 }
19926 else if (attr.form_is_section_offset ())
19927 {
19928 dwarf2_complex_location_expr_complaint ();
19929 }
19930 else
19931 {
19932 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19933 "partial symbol information");
19934 }
19935 break;
19936 case DW_AT_external:
19937 is_external = attr.as_boolean ();
19938 break;
19939 case DW_AT_declaration:
19940 is_declaration = attr.as_boolean ();
19941 break;
19942 case DW_AT_type:
19943 has_type = 1;
19944 break;
19945 case DW_AT_abstract_origin:
19946 case DW_AT_specification:
19947 case DW_AT_extension:
19948 has_specification = 1;
19949 spec_offset = attr.get_ref_die_offset ();
19950 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19951 || cu->per_cu->is_dwz);
19952 break;
19953 case DW_AT_sibling:
19954 /* Ignore absolute siblings, they might point outside of
19955 the current compile unit. */
19956 if (attr.form == DW_FORM_ref_addr)
19957 complaint (_("ignoring absolute DW_AT_sibling"));
19958 else
19959 {
19960 const gdb_byte *buffer = reader->buffer;
19961 sect_offset off = attr.get_ref_die_offset ();
19962 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19963
19964 if (sibling_ptr < info_ptr)
19965 complaint (_("DW_AT_sibling points backwards"));
19966 else if (sibling_ptr > reader->buffer_end)
19967 reader->die_section->overflow_complaint ();
19968 else
19969 sibling = sibling_ptr;
19970 }
19971 break;
19972 case DW_AT_byte_size:
19973 has_byte_size = 1;
19974 break;
19975 case DW_AT_const_value:
19976 has_const_value = 1;
19977 break;
19978 case DW_AT_calling_convention:
19979 /* DWARF doesn't provide a way to identify a program's source-level
19980 entry point. DW_AT_calling_convention attributes are only meant
19981 to describe functions' calling conventions.
19982
19983 However, because it's a necessary piece of information in
19984 Fortran, and before DWARF 4 DW_CC_program was the only
19985 piece of debugging information whose definition refers to
19986 a 'main program' at all, several compilers marked Fortran
19987 main programs with DW_CC_program --- even when those
19988 functions use the standard calling conventions.
19989
19990 Although DWARF now specifies a way to provide this
19991 information, we support this practice for backward
19992 compatibility. */
19993 if (attr.constant_value (0) == DW_CC_program
19994 && cu->language == language_fortran)
19995 main_subprogram = 1;
19996 break;
19997 case DW_AT_inline:
19998 {
19999 LONGEST value = attr.constant_value (-1);
20000 if (value == DW_INL_inlined
20001 || value == DW_INL_declared_inlined)
20002 may_be_inlined = 1;
20003 }
20004 break;
20005
20006 case DW_AT_import:
20007 if (tag == DW_TAG_imported_unit)
20008 {
20009 d.sect_off = attr.get_ref_die_offset ();
20010 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
20011 || cu->per_cu->is_dwz);
20012 }
20013 break;
20014
20015 case DW_AT_main_subprogram:
20016 main_subprogram = attr.as_boolean ();
20017 break;
20018
20019 case DW_AT_ranges:
20020 {
20021 /* Offset in the .debug_ranges or .debug_rnglist section (depending
20022 on DWARF version). */
20023 ULONGEST ranges_offset = attr.as_unsigned ();
20024
20025 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
20026 this value. */
20027 if (tag != DW_TAG_compile_unit)
20028 ranges_offset += cu->gnu_ranges_base;
20029
20030 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
20031 nullptr, tag))
20032 has_pc_info = 1;
20033 }
20034 break;
20035
20036 default:
20037 break;
20038 }
20039 }
20040
20041 /* For Ada, if both the name and the linkage name appear, we prefer
20042 the latter. This lets "catch exception" work better, regardless
20043 of the order in which the name and linkage name were emitted.
20044 Really, though, this is just a workaround for the fact that gdb
20045 doesn't store both the name and the linkage name. */
20046 if (cu->language == language_ada && linkage_name != nullptr)
20047 raw_name = linkage_name;
20048
20049 if (high_pc_relative)
20050 highpc += lowpc;
20051
20052 if (has_low_pc_attr && has_high_pc_attr)
20053 {
20054 /* When using the GNU linker, .gnu.linkonce. sections are used to
20055 eliminate duplicate copies of functions and vtables and such.
20056 The linker will arbitrarily choose one and discard the others.
20057 The AT_*_pc values for such functions refer to local labels in
20058 these sections. If the section from that file was discarded, the
20059 labels are not in the output, so the relocs get a value of 0.
20060 If this is a discarded function, mark the pc bounds as invalid,
20061 so that GDB will ignore it. */
20062 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
20063 {
20064 struct objfile *objfile = per_objfile->objfile;
20065 struct gdbarch *gdbarch = objfile->arch ();
20066
20067 complaint (_("DW_AT_low_pc %s is zero "
20068 "for DIE at %s [in module %s]"),
20069 paddress (gdbarch, lowpc),
20070 sect_offset_str (sect_off),
20071 objfile_name (objfile));
20072 }
20073 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
20074 else if (lowpc >= highpc)
20075 {
20076 struct objfile *objfile = per_objfile->objfile;
20077 struct gdbarch *gdbarch = objfile->arch ();
20078
20079 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
20080 "for DIE at %s [in module %s]"),
20081 paddress (gdbarch, lowpc),
20082 paddress (gdbarch, highpc),
20083 sect_offset_str (sect_off),
20084 objfile_name (objfile));
20085 }
20086 else
20087 has_pc_info = 1;
20088 }
20089
20090 return info_ptr;
20091 }
20092
20093 /* Find a cached partial DIE at OFFSET in CU. */
20094
20095 struct partial_die_info *
20096 dwarf2_cu::find_partial_die (sect_offset sect_off)
20097 {
20098 struct partial_die_info *lookup_die = NULL;
20099 struct partial_die_info part_die (sect_off);
20100
20101 lookup_die = ((struct partial_die_info *)
20102 htab_find_with_hash (partial_dies, &part_die,
20103 to_underlying (sect_off)));
20104
20105 return lookup_die;
20106 }
20107
20108 /* Find a partial DIE at OFFSET, which may or may not be in CU,
20109 except in the case of .debug_types DIEs which do not reference
20110 outside their CU (they do however referencing other types via
20111 DW_FORM_ref_sig8). */
20112
20113 static const struct cu_partial_die_info
20114 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
20115 {
20116 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20117 struct objfile *objfile = per_objfile->objfile;
20118 struct partial_die_info *pd = NULL;
20119
20120 if (offset_in_dwz == cu->per_cu->is_dwz
20121 && cu->header.offset_in_cu_p (sect_off))
20122 {
20123 pd = cu->find_partial_die (sect_off);
20124 if (pd != NULL)
20125 return { cu, pd };
20126 /* We missed recording what we needed.
20127 Load all dies and try again. */
20128 }
20129 else
20130 {
20131 /* TUs don't reference other CUs/TUs (except via type signatures). */
20132 if (cu->per_cu->is_debug_types)
20133 {
20134 error (_("Dwarf Error: Type Unit at offset %s contains"
20135 " external reference to offset %s [in module %s].\n"),
20136 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
20137 bfd_get_filename (objfile->obfd));
20138 }
20139 dwarf2_per_cu_data *per_cu
20140 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20141 per_objfile);
20142
20143 cu = per_objfile->get_cu (per_cu);
20144 if (cu == NULL || cu->partial_dies == NULL)
20145 load_partial_comp_unit (per_cu, per_objfile, nullptr);
20146
20147 cu = per_objfile->get_cu (per_cu);
20148
20149 cu->last_used = 0;
20150 pd = cu->find_partial_die (sect_off);
20151 }
20152
20153 /* If we didn't find it, and not all dies have been loaded,
20154 load them all and try again. */
20155
20156 if (pd == NULL && cu->per_cu->load_all_dies == 0)
20157 {
20158 cu->per_cu->load_all_dies = 1;
20159
20160 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20161 THIS_CU->cu may already be in use. So we can't just free it and
20162 replace its DIEs with the ones we read in. Instead, we leave those
20163 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20164 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20165 set. */
20166 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
20167
20168 pd = cu->find_partial_die (sect_off);
20169 }
20170
20171 if (pd == NULL)
20172 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
20173 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
20174 return { cu, pd };
20175 }
20176
20177 /* See if we can figure out if the class lives in a namespace. We do
20178 this by looking for a member function; its demangled name will
20179 contain namespace info, if there is any. */
20180
20181 static void
20182 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20183 struct dwarf2_cu *cu)
20184 {
20185 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20186 what template types look like, because the demangler
20187 frequently doesn't give the same name as the debug info. We
20188 could fix this by only using the demangled name to get the
20189 prefix (but see comment in read_structure_type). */
20190
20191 struct partial_die_info *real_pdi;
20192 struct partial_die_info *child_pdi;
20193
20194 /* If this DIE (this DIE's specification, if any) has a parent, then
20195 we should not do this. We'll prepend the parent's fully qualified
20196 name when we create the partial symbol. */
20197
20198 real_pdi = struct_pdi;
20199 while (real_pdi->has_specification)
20200 {
20201 auto res = find_partial_die (real_pdi->spec_offset,
20202 real_pdi->spec_is_dwz, cu);
20203 real_pdi = res.pdi;
20204 cu = res.cu;
20205 }
20206
20207 if (real_pdi->die_parent != NULL)
20208 return;
20209
20210 for (child_pdi = struct_pdi->die_child;
20211 child_pdi != NULL;
20212 child_pdi = child_pdi->die_sibling)
20213 {
20214 if (child_pdi->tag == DW_TAG_subprogram
20215 && child_pdi->linkage_name != NULL)
20216 {
20217 gdb::unique_xmalloc_ptr<char> actual_class_name
20218 (cu->language_defn->class_name_from_physname
20219 (child_pdi->linkage_name));
20220 if (actual_class_name != NULL)
20221 {
20222 struct objfile *objfile = cu->per_objfile->objfile;
20223 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20224 struct_pdi->canonical_name = 1;
20225 }
20226 break;
20227 }
20228 }
20229 }
20230
20231 /* Return true if a DIE with TAG may have the DW_AT_const_value
20232 attribute. */
20233
20234 static bool
20235 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20236 {
20237 switch (tag)
20238 {
20239 case DW_TAG_constant:
20240 case DW_TAG_enumerator:
20241 case DW_TAG_formal_parameter:
20242 case DW_TAG_template_value_param:
20243 case DW_TAG_variable:
20244 return true;
20245 }
20246
20247 return false;
20248 }
20249
20250 void
20251 partial_die_info::fixup (struct dwarf2_cu *cu)
20252 {
20253 /* Once we've fixed up a die, there's no point in doing so again.
20254 This also avoids a memory leak if we were to call
20255 guess_partial_die_structure_name multiple times. */
20256 if (fixup_called)
20257 return;
20258
20259 /* If we found a reference attribute and the DIE has no name, try
20260 to find a name in the referred to DIE. */
20261
20262 if (raw_name == NULL && has_specification)
20263 {
20264 struct partial_die_info *spec_die;
20265
20266 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20267 spec_die = res.pdi;
20268 cu = res.cu;
20269
20270 spec_die->fixup (cu);
20271
20272 if (spec_die->raw_name)
20273 {
20274 raw_name = spec_die->raw_name;
20275 canonical_name = spec_die->canonical_name;
20276
20277 /* Copy DW_AT_external attribute if it is set. */
20278 if (spec_die->is_external)
20279 is_external = spec_die->is_external;
20280 }
20281 }
20282
20283 if (!has_const_value && has_specification
20284 && can_have_DW_AT_const_value_p (tag))
20285 {
20286 struct partial_die_info *spec_die;
20287
20288 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20289 spec_die = res.pdi;
20290 cu = res.cu;
20291
20292 spec_die->fixup (cu);
20293
20294 if (spec_die->has_const_value)
20295 {
20296 /* Copy DW_AT_const_value attribute if it is set. */
20297 has_const_value = spec_die->has_const_value;
20298 }
20299 }
20300
20301 /* Set default names for some unnamed DIEs. */
20302
20303 if (raw_name == NULL && tag == DW_TAG_namespace)
20304 {
20305 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20306 canonical_name = 1;
20307 }
20308
20309 /* If there is no parent die to provide a namespace, and there are
20310 children, see if we can determine the namespace from their linkage
20311 name. */
20312 if (cu->language == language_cplus
20313 && !cu->per_objfile->per_bfd->types.empty ()
20314 && die_parent == NULL
20315 && has_children
20316 && (tag == DW_TAG_class_type
20317 || tag == DW_TAG_structure_type
20318 || tag == DW_TAG_union_type))
20319 guess_partial_die_structure_name (this, cu);
20320
20321 /* GCC might emit a nameless struct or union that has a linkage
20322 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20323 if (raw_name == NULL
20324 && (tag == DW_TAG_class_type
20325 || tag == DW_TAG_interface_type
20326 || tag == DW_TAG_structure_type
20327 || tag == DW_TAG_union_type)
20328 && linkage_name != NULL)
20329 {
20330 gdb::unique_xmalloc_ptr<char> demangled
20331 (gdb_demangle (linkage_name, DMGL_TYPES));
20332 if (demangled != nullptr)
20333 {
20334 const char *base;
20335
20336 /* Strip any leading namespaces/classes, keep only the base name.
20337 DW_AT_name for named DIEs does not contain the prefixes. */
20338 base = strrchr (demangled.get (), ':');
20339 if (base && base > demangled.get () && base[-1] == ':')
20340 base++;
20341 else
20342 base = demangled.get ();
20343
20344 struct objfile *objfile = cu->per_objfile->objfile;
20345 raw_name = objfile->intern (base);
20346 canonical_name = 1;
20347 }
20348 }
20349
20350 fixup_called = 1;
20351 }
20352
20353 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20354 contents from the given SECTION in the HEADER.
20355
20356 HEADER_OFFSET is the offset of the header in the section. */
20357 static void
20358 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20359 struct dwarf2_section_info *section,
20360 sect_offset header_offset)
20361 {
20362 unsigned int bytes_read;
20363 bfd *abfd = section->get_bfd_owner ();
20364 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20365
20366 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20367 info_ptr += bytes_read;
20368
20369 header->version = read_2_bytes (abfd, info_ptr);
20370 info_ptr += 2;
20371
20372 header->addr_size = read_1_byte (abfd, info_ptr);
20373 info_ptr += 1;
20374
20375 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20376 info_ptr += 1;
20377
20378 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20379 }
20380
20381 /* Return the DW_AT_loclists_base value for the CU. */
20382 static ULONGEST
20383 lookup_loclist_base (struct dwarf2_cu *cu)
20384 {
20385 /* For the .dwo unit, the loclist_base points to the first offset following
20386 the header. The header consists of the following entities-
20387 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20388 bit format)
20389 2. version (2 bytes)
20390 3. address size (1 byte)
20391 4. segment selector size (1 byte)
20392 5. offset entry count (4 bytes)
20393 These sizes are derived as per the DWARFv5 standard. */
20394 if (cu->dwo_unit != nullptr)
20395 {
20396 if (cu->header.initial_length_size == 4)
20397 return LOCLIST_HEADER_SIZE32;
20398 return LOCLIST_HEADER_SIZE64;
20399 }
20400 return cu->loclist_base;
20401 }
20402
20403 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20404 array of offsets in the .debug_loclists section. */
20405
20406 static sect_offset
20407 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20408 {
20409 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20410 struct objfile *objfile = per_objfile->objfile;
20411 bfd *abfd = objfile->obfd;
20412 ULONGEST loclist_header_size =
20413 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20414 : LOCLIST_HEADER_SIZE64);
20415 ULONGEST loclist_base = lookup_loclist_base (cu);
20416
20417 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20418 ULONGEST start_offset =
20419 loclist_base + loclist_index * cu->header.offset_size;
20420
20421 /* Get loclists section. */
20422 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20423
20424 /* Read the loclists section content. */
20425 section->read (objfile);
20426 if (section->buffer == NULL)
20427 error (_("DW_FORM_loclistx used without .debug_loclists "
20428 "section [in module %s]"), objfile_name (objfile));
20429
20430 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20431 so if loclist_base is smaller than the header size, we have a problem. */
20432 if (loclist_base < loclist_header_size)
20433 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20434 objfile_name (objfile));
20435
20436 /* Read the header of the loclists contribution. */
20437 struct loclists_rnglists_header header;
20438 read_loclists_rnglists_header (&header, section,
20439 (sect_offset) (loclist_base - loclist_header_size));
20440
20441 /* Verify the loclist index is valid. */
20442 if (loclist_index >= header.offset_entry_count)
20443 error (_("DW_FORM_loclistx pointing outside of "
20444 ".debug_loclists offset array [in module %s]"),
20445 objfile_name (objfile));
20446
20447 /* Validate that reading won't go beyond the end of the section. */
20448 if (start_offset + cu->header.offset_size > section->size)
20449 error (_("Reading DW_FORM_loclistx index beyond end of"
20450 ".debug_loclists section [in module %s]"),
20451 objfile_name (objfile));
20452
20453 const gdb_byte *info_ptr = section->buffer + start_offset;
20454
20455 if (cu->header.offset_size == 4)
20456 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20457 else
20458 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20459 }
20460
20461 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20462 array of offsets in the .debug_rnglists section. */
20463
20464 static sect_offset
20465 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20466 dwarf_tag tag)
20467 {
20468 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20469 struct objfile *objfile = dwarf2_per_objfile->objfile;
20470 bfd *abfd = objfile->obfd;
20471 ULONGEST rnglist_header_size =
20472 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20473 : RNGLIST_HEADER_SIZE64);
20474
20475 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20476 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20477 doesn't apply. */
20478 ULONGEST rnglist_base =
20479 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20480
20481 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20482 ULONGEST start_offset =
20483 rnglist_base + rnglist_index * cu->header.offset_size;
20484
20485 /* Get rnglists section. */
20486 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20487
20488 /* Read the rnglists section content. */
20489 section->read (objfile);
20490 if (section->buffer == nullptr)
20491 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20492 "[in module %s]"),
20493 objfile_name (objfile));
20494
20495 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20496 so if rnglist_base is smaller than the header size, we have a problem. */
20497 if (rnglist_base < rnglist_header_size)
20498 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20499 objfile_name (objfile));
20500
20501 /* Read the header of the rnglists contribution. */
20502 struct loclists_rnglists_header header;
20503 read_loclists_rnglists_header (&header, section,
20504 (sect_offset) (rnglist_base - rnglist_header_size));
20505
20506 /* Verify the rnglist index is valid. */
20507 if (rnglist_index >= header.offset_entry_count)
20508 error (_("DW_FORM_rnglistx index pointing outside of "
20509 ".debug_rnglists offset array [in module %s]"),
20510 objfile_name (objfile));
20511
20512 /* Validate that reading won't go beyond the end of the section. */
20513 if (start_offset + cu->header.offset_size > section->size)
20514 error (_("Reading DW_FORM_rnglistx index beyond end of"
20515 ".debug_rnglists section [in module %s]"),
20516 objfile_name (objfile));
20517
20518 const gdb_byte *info_ptr = section->buffer + start_offset;
20519
20520 if (cu->header.offset_size == 4)
20521 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20522 else
20523 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20524 }
20525
20526 /* Process the attributes that had to be skipped in the first round. These
20527 attributes are the ones that need str_offsets_base or addr_base attributes.
20528 They could not have been processed in the first round, because at the time
20529 the values of str_offsets_base or addr_base may not have been known. */
20530 static void
20531 read_attribute_reprocess (const struct die_reader_specs *reader,
20532 struct attribute *attr, dwarf_tag tag)
20533 {
20534 struct dwarf2_cu *cu = reader->cu;
20535 switch (attr->form)
20536 {
20537 case DW_FORM_addrx:
20538 case DW_FORM_GNU_addr_index:
20539 attr->set_address (read_addr_index (cu,
20540 attr->as_unsigned_reprocess ()));
20541 break;
20542 case DW_FORM_loclistx:
20543 {
20544 sect_offset loclists_sect_off
20545 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20546
20547 attr->set_unsigned (to_underlying (loclists_sect_off));
20548 }
20549 break;
20550 case DW_FORM_rnglistx:
20551 {
20552 sect_offset rnglists_sect_off
20553 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20554
20555 attr->set_unsigned (to_underlying (rnglists_sect_off));
20556 }
20557 break;
20558 case DW_FORM_strx:
20559 case DW_FORM_strx1:
20560 case DW_FORM_strx2:
20561 case DW_FORM_strx3:
20562 case DW_FORM_strx4:
20563 case DW_FORM_GNU_str_index:
20564 {
20565 unsigned int str_index = attr->as_unsigned_reprocess ();
20566 gdb_assert (!attr->canonical_string_p ());
20567 if (reader->dwo_file != NULL)
20568 attr->set_string_noncanonical (read_dwo_str_index (reader,
20569 str_index));
20570 else
20571 attr->set_string_noncanonical (read_stub_str_index (cu,
20572 str_index));
20573 break;
20574 }
20575 default:
20576 gdb_assert_not_reached (_("Unexpected DWARF form."));
20577 }
20578 }
20579
20580 /* Read an attribute value described by an attribute form. */
20581
20582 static const gdb_byte *
20583 read_attribute_value (const struct die_reader_specs *reader,
20584 struct attribute *attr, unsigned form,
20585 LONGEST implicit_const, const gdb_byte *info_ptr)
20586 {
20587 struct dwarf2_cu *cu = reader->cu;
20588 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20589 struct objfile *objfile = per_objfile->objfile;
20590 bfd *abfd = reader->abfd;
20591 struct comp_unit_head *cu_header = &cu->header;
20592 unsigned int bytes_read;
20593 struct dwarf_block *blk;
20594
20595 attr->form = (enum dwarf_form) form;
20596 switch (form)
20597 {
20598 case DW_FORM_ref_addr:
20599 if (cu_header->version == 2)
20600 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20601 &bytes_read));
20602 else
20603 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20604 &bytes_read));
20605 info_ptr += bytes_read;
20606 break;
20607 case DW_FORM_GNU_ref_alt:
20608 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20609 &bytes_read));
20610 info_ptr += bytes_read;
20611 break;
20612 case DW_FORM_addr:
20613 {
20614 struct gdbarch *gdbarch = objfile->arch ();
20615 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20616 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20617 attr->set_address (addr);
20618 info_ptr += bytes_read;
20619 }
20620 break;
20621 case DW_FORM_block2:
20622 blk = dwarf_alloc_block (cu);
20623 blk->size = read_2_bytes (abfd, info_ptr);
20624 info_ptr += 2;
20625 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20626 info_ptr += blk->size;
20627 attr->set_block (blk);
20628 break;
20629 case DW_FORM_block4:
20630 blk = dwarf_alloc_block (cu);
20631 blk->size = read_4_bytes (abfd, info_ptr);
20632 info_ptr += 4;
20633 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20634 info_ptr += blk->size;
20635 attr->set_block (blk);
20636 break;
20637 case DW_FORM_data2:
20638 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20639 info_ptr += 2;
20640 break;
20641 case DW_FORM_data4:
20642 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20643 info_ptr += 4;
20644 break;
20645 case DW_FORM_data8:
20646 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20647 info_ptr += 8;
20648 break;
20649 case DW_FORM_data16:
20650 blk = dwarf_alloc_block (cu);
20651 blk->size = 16;
20652 blk->data = read_n_bytes (abfd, info_ptr, 16);
20653 info_ptr += 16;
20654 attr->set_block (blk);
20655 break;
20656 case DW_FORM_sec_offset:
20657 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20658 &bytes_read));
20659 info_ptr += bytes_read;
20660 break;
20661 case DW_FORM_loclistx:
20662 {
20663 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20664 &bytes_read));
20665 info_ptr += bytes_read;
20666 }
20667 break;
20668 case DW_FORM_string:
20669 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20670 &bytes_read));
20671 info_ptr += bytes_read;
20672 break;
20673 case DW_FORM_strp:
20674 if (!cu->per_cu->is_dwz)
20675 {
20676 attr->set_string_noncanonical
20677 (read_indirect_string (per_objfile,
20678 abfd, info_ptr, cu_header,
20679 &bytes_read));
20680 info_ptr += bytes_read;
20681 break;
20682 }
20683 /* FALLTHROUGH */
20684 case DW_FORM_line_strp:
20685 if (!cu->per_cu->is_dwz)
20686 {
20687 attr->set_string_noncanonical
20688 (per_objfile->read_line_string (info_ptr, cu_header,
20689 &bytes_read));
20690 info_ptr += bytes_read;
20691 break;
20692 }
20693 /* FALLTHROUGH */
20694 case DW_FORM_GNU_strp_alt:
20695 {
20696 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20697 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20698 &bytes_read);
20699
20700 attr->set_string_noncanonical
20701 (dwz->read_string (objfile, str_offset));
20702 info_ptr += bytes_read;
20703 }
20704 break;
20705 case DW_FORM_exprloc:
20706 case DW_FORM_block:
20707 blk = dwarf_alloc_block (cu);
20708 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20709 info_ptr += bytes_read;
20710 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20711 info_ptr += blk->size;
20712 attr->set_block (blk);
20713 break;
20714 case DW_FORM_block1:
20715 blk = dwarf_alloc_block (cu);
20716 blk->size = read_1_byte (abfd, info_ptr);
20717 info_ptr += 1;
20718 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20719 info_ptr += blk->size;
20720 attr->set_block (blk);
20721 break;
20722 case DW_FORM_data1:
20723 case DW_FORM_flag:
20724 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20725 info_ptr += 1;
20726 break;
20727 case DW_FORM_flag_present:
20728 attr->set_unsigned (1);
20729 break;
20730 case DW_FORM_sdata:
20731 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20732 info_ptr += bytes_read;
20733 break;
20734 case DW_FORM_rnglistx:
20735 {
20736 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20737 &bytes_read));
20738 info_ptr += bytes_read;
20739 }
20740 break;
20741 case DW_FORM_udata:
20742 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20743 info_ptr += bytes_read;
20744 break;
20745 case DW_FORM_ref1:
20746 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20747 + read_1_byte (abfd, info_ptr)));
20748 info_ptr += 1;
20749 break;
20750 case DW_FORM_ref2:
20751 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20752 + read_2_bytes (abfd, info_ptr)));
20753 info_ptr += 2;
20754 break;
20755 case DW_FORM_ref4:
20756 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20757 + read_4_bytes (abfd, info_ptr)));
20758 info_ptr += 4;
20759 break;
20760 case DW_FORM_ref8:
20761 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20762 + read_8_bytes (abfd, info_ptr)));
20763 info_ptr += 8;
20764 break;
20765 case DW_FORM_ref_sig8:
20766 attr->set_signature (read_8_bytes (abfd, info_ptr));
20767 info_ptr += 8;
20768 break;
20769 case DW_FORM_ref_udata:
20770 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20771 + read_unsigned_leb128 (abfd, info_ptr,
20772 &bytes_read)));
20773 info_ptr += bytes_read;
20774 break;
20775 case DW_FORM_indirect:
20776 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20777 info_ptr += bytes_read;
20778 if (form == DW_FORM_implicit_const)
20779 {
20780 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20781 info_ptr += bytes_read;
20782 }
20783 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20784 info_ptr);
20785 break;
20786 case DW_FORM_implicit_const:
20787 attr->set_signed (implicit_const);
20788 break;
20789 case DW_FORM_addrx:
20790 case DW_FORM_GNU_addr_index:
20791 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20792 &bytes_read));
20793 info_ptr += bytes_read;
20794 break;
20795 case DW_FORM_strx:
20796 case DW_FORM_strx1:
20797 case DW_FORM_strx2:
20798 case DW_FORM_strx3:
20799 case DW_FORM_strx4:
20800 case DW_FORM_GNU_str_index:
20801 {
20802 ULONGEST str_index;
20803 if (form == DW_FORM_strx1)
20804 {
20805 str_index = read_1_byte (abfd, info_ptr);
20806 info_ptr += 1;
20807 }
20808 else if (form == DW_FORM_strx2)
20809 {
20810 str_index = read_2_bytes (abfd, info_ptr);
20811 info_ptr += 2;
20812 }
20813 else if (form == DW_FORM_strx3)
20814 {
20815 str_index = read_3_bytes (abfd, info_ptr);
20816 info_ptr += 3;
20817 }
20818 else if (form == DW_FORM_strx4)
20819 {
20820 str_index = read_4_bytes (abfd, info_ptr);
20821 info_ptr += 4;
20822 }
20823 else
20824 {
20825 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20826 info_ptr += bytes_read;
20827 }
20828 attr->set_unsigned_reprocess (str_index);
20829 }
20830 break;
20831 default:
20832 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20833 dwarf_form_name (form),
20834 bfd_get_filename (abfd));
20835 }
20836
20837 /* Super hack. */
20838 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20839 attr->form = DW_FORM_GNU_ref_alt;
20840
20841 /* We have seen instances where the compiler tried to emit a byte
20842 size attribute of -1 which ended up being encoded as an unsigned
20843 0xffffffff. Although 0xffffffff is technically a valid size value,
20844 an object of this size seems pretty unlikely so we can relatively
20845 safely treat these cases as if the size attribute was invalid and
20846 treat them as zero by default. */
20847 if (attr->name == DW_AT_byte_size
20848 && form == DW_FORM_data4
20849 && attr->as_unsigned () >= 0xffffffff)
20850 {
20851 complaint
20852 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20853 hex_string (attr->as_unsigned ()));
20854 attr->set_unsigned (0);
20855 }
20856
20857 return info_ptr;
20858 }
20859
20860 /* Read an attribute described by an abbreviated attribute. */
20861
20862 static const gdb_byte *
20863 read_attribute (const struct die_reader_specs *reader,
20864 struct attribute *attr, const struct attr_abbrev *abbrev,
20865 const gdb_byte *info_ptr)
20866 {
20867 attr->name = abbrev->name;
20868 attr->string_is_canonical = 0;
20869 attr->requires_reprocessing = 0;
20870 return read_attribute_value (reader, attr, abbrev->form,
20871 abbrev->implicit_const, info_ptr);
20872 }
20873
20874 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20875
20876 static const char *
20877 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20878 LONGEST str_offset)
20879 {
20880 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20881 str_offset, "DW_FORM_strp");
20882 }
20883
20884 /* Return pointer to string at .debug_str offset as read from BUF.
20885 BUF is assumed to be in a compilation unit described by CU_HEADER.
20886 Return *BYTES_READ_PTR count of bytes read from BUF. */
20887
20888 static const char *
20889 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20890 const gdb_byte *buf,
20891 const struct comp_unit_head *cu_header,
20892 unsigned int *bytes_read_ptr)
20893 {
20894 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20895
20896 return read_indirect_string_at_offset (per_objfile, str_offset);
20897 }
20898
20899 /* See read.h. */
20900
20901 const char *
20902 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20903 const struct comp_unit_head *cu_header,
20904 unsigned int *bytes_read_ptr)
20905 {
20906 bfd *abfd = objfile->obfd;
20907 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20908
20909 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20910 }
20911
20912 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20913 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20914 ADDR_SIZE is the size of addresses from the CU header. */
20915
20916 static CORE_ADDR
20917 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20918 gdb::optional<ULONGEST> addr_base, int addr_size)
20919 {
20920 struct objfile *objfile = per_objfile->objfile;
20921 bfd *abfd = objfile->obfd;
20922 const gdb_byte *info_ptr;
20923 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20924
20925 per_objfile->per_bfd->addr.read (objfile);
20926 if (per_objfile->per_bfd->addr.buffer == NULL)
20927 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20928 objfile_name (objfile));
20929 if (addr_base_or_zero + addr_index * addr_size
20930 >= per_objfile->per_bfd->addr.size)
20931 error (_("DW_FORM_addr_index pointing outside of "
20932 ".debug_addr section [in module %s]"),
20933 objfile_name (objfile));
20934 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20935 + addr_index * addr_size);
20936 if (addr_size == 4)
20937 return bfd_get_32 (abfd, info_ptr);
20938 else
20939 return bfd_get_64 (abfd, info_ptr);
20940 }
20941
20942 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20943
20944 static CORE_ADDR
20945 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20946 {
20947 return read_addr_index_1 (cu->per_objfile, addr_index,
20948 cu->addr_base, cu->header.addr_size);
20949 }
20950
20951 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20952
20953 static CORE_ADDR
20954 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20955 unsigned int *bytes_read)
20956 {
20957 bfd *abfd = cu->per_objfile->objfile->obfd;
20958 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20959
20960 return read_addr_index (cu, addr_index);
20961 }
20962
20963 /* See read.h. */
20964
20965 CORE_ADDR
20966 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20967 dwarf2_per_objfile *per_objfile,
20968 unsigned int addr_index)
20969 {
20970 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20971 gdb::optional<ULONGEST> addr_base;
20972 int addr_size;
20973
20974 /* We need addr_base and addr_size.
20975 If we don't have PER_CU->cu, we have to get it.
20976 Nasty, but the alternative is storing the needed info in PER_CU,
20977 which at this point doesn't seem justified: it's not clear how frequently
20978 it would get used and it would increase the size of every PER_CU.
20979 Entry points like dwarf2_per_cu_addr_size do a similar thing
20980 so we're not in uncharted territory here.
20981 Alas we need to be a bit more complicated as addr_base is contained
20982 in the DIE.
20983
20984 We don't need to read the entire CU(/TU).
20985 We just need the header and top level die.
20986
20987 IWBN to use the aging mechanism to let us lazily later discard the CU.
20988 For now we skip this optimization. */
20989
20990 if (cu != NULL)
20991 {
20992 addr_base = cu->addr_base;
20993 addr_size = cu->header.addr_size;
20994 }
20995 else
20996 {
20997 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20998 addr_base = reader.cu->addr_base;
20999 addr_size = reader.cu->header.addr_size;
21000 }
21001
21002 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
21003 }
21004
21005 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
21006 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
21007 DWO file. */
21008
21009 static const char *
21010 read_str_index (struct dwarf2_cu *cu,
21011 struct dwarf2_section_info *str_section,
21012 struct dwarf2_section_info *str_offsets_section,
21013 ULONGEST str_offsets_base, ULONGEST str_index)
21014 {
21015 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21016 struct objfile *objfile = per_objfile->objfile;
21017 const char *objf_name = objfile_name (objfile);
21018 bfd *abfd = objfile->obfd;
21019 const gdb_byte *info_ptr;
21020 ULONGEST str_offset;
21021 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
21022
21023 str_section->read (objfile);
21024 str_offsets_section->read (objfile);
21025 if (str_section->buffer == NULL)
21026 error (_("%s used without %s section"
21027 " in CU at offset %s [in module %s]"),
21028 form_name, str_section->get_name (),
21029 sect_offset_str (cu->header.sect_off), objf_name);
21030 if (str_offsets_section->buffer == NULL)
21031 error (_("%s used without %s section"
21032 " in CU at offset %s [in module %s]"),
21033 form_name, str_section->get_name (),
21034 sect_offset_str (cu->header.sect_off), objf_name);
21035 info_ptr = (str_offsets_section->buffer
21036 + str_offsets_base
21037 + str_index * cu->header.offset_size);
21038 if (cu->header.offset_size == 4)
21039 str_offset = bfd_get_32 (abfd, info_ptr);
21040 else
21041 str_offset = bfd_get_64 (abfd, info_ptr);
21042 if (str_offset >= str_section->size)
21043 error (_("Offset from %s pointing outside of"
21044 " .debug_str.dwo section in CU at offset %s [in module %s]"),
21045 form_name, sect_offset_str (cu->header.sect_off), objf_name);
21046 return (const char *) (str_section->buffer + str_offset);
21047 }
21048
21049 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
21050
21051 static const char *
21052 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
21053 {
21054 ULONGEST str_offsets_base = reader->cu->header.version >= 5
21055 ? reader->cu->header.addr_size : 0;
21056 return read_str_index (reader->cu,
21057 &reader->dwo_file->sections.str,
21058 &reader->dwo_file->sections.str_offsets,
21059 str_offsets_base, str_index);
21060 }
21061
21062 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
21063
21064 static const char *
21065 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
21066 {
21067 struct objfile *objfile = cu->per_objfile->objfile;
21068 const char *objf_name = objfile_name (objfile);
21069 static const char form_name[] = "DW_FORM_GNU_str_index";
21070 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
21071
21072 if (!cu->str_offsets_base.has_value ())
21073 error (_("%s used in Fission stub without %s"
21074 " in CU at offset 0x%lx [in module %s]"),
21075 form_name, str_offsets_attr_name,
21076 (long) cu->header.offset_size, objf_name);
21077
21078 return read_str_index (cu,
21079 &cu->per_objfile->per_bfd->str,
21080 &cu->per_objfile->per_bfd->str_offsets,
21081 *cu->str_offsets_base, str_index);
21082 }
21083
21084 /* Return the length of an LEB128 number in BUF. */
21085
21086 static int
21087 leb128_size (const gdb_byte *buf)
21088 {
21089 const gdb_byte *begin = buf;
21090 gdb_byte byte;
21091
21092 while (1)
21093 {
21094 byte = *buf++;
21095 if ((byte & 128) == 0)
21096 return buf - begin;
21097 }
21098 }
21099
21100 static void
21101 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
21102 {
21103 switch (lang)
21104 {
21105 case DW_LANG_C89:
21106 case DW_LANG_C99:
21107 case DW_LANG_C11:
21108 case DW_LANG_C:
21109 case DW_LANG_UPC:
21110 cu->language = language_c;
21111 break;
21112 case DW_LANG_Java:
21113 case DW_LANG_C_plus_plus:
21114 case DW_LANG_C_plus_plus_11:
21115 case DW_LANG_C_plus_plus_14:
21116 cu->language = language_cplus;
21117 break;
21118 case DW_LANG_D:
21119 cu->language = language_d;
21120 break;
21121 case DW_LANG_Fortran77:
21122 case DW_LANG_Fortran90:
21123 case DW_LANG_Fortran95:
21124 case DW_LANG_Fortran03:
21125 case DW_LANG_Fortran08:
21126 cu->language = language_fortran;
21127 break;
21128 case DW_LANG_Go:
21129 cu->language = language_go;
21130 break;
21131 case DW_LANG_Mips_Assembler:
21132 cu->language = language_asm;
21133 break;
21134 case DW_LANG_Ada83:
21135 case DW_LANG_Ada95:
21136 cu->language = language_ada;
21137 break;
21138 case DW_LANG_Modula2:
21139 cu->language = language_m2;
21140 break;
21141 case DW_LANG_Pascal83:
21142 cu->language = language_pascal;
21143 break;
21144 case DW_LANG_ObjC:
21145 cu->language = language_objc;
21146 break;
21147 case DW_LANG_Rust:
21148 case DW_LANG_Rust_old:
21149 cu->language = language_rust;
21150 break;
21151 case DW_LANG_Cobol74:
21152 case DW_LANG_Cobol85:
21153 default:
21154 cu->language = language_minimal;
21155 break;
21156 }
21157 cu->language_defn = language_def (cu->language);
21158 }
21159
21160 /* Return the named attribute or NULL if not there. */
21161
21162 static struct attribute *
21163 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21164 {
21165 for (;;)
21166 {
21167 unsigned int i;
21168 struct attribute *spec = NULL;
21169
21170 for (i = 0; i < die->num_attrs; ++i)
21171 {
21172 if (die->attrs[i].name == name)
21173 return &die->attrs[i];
21174 if (die->attrs[i].name == DW_AT_specification
21175 || die->attrs[i].name == DW_AT_abstract_origin)
21176 spec = &die->attrs[i];
21177 }
21178
21179 if (!spec)
21180 break;
21181
21182 die = follow_die_ref (die, spec, &cu);
21183 }
21184
21185 return NULL;
21186 }
21187
21188 /* Return the string associated with a string-typed attribute, or NULL if it
21189 is either not found or is of an incorrect type. */
21190
21191 static const char *
21192 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21193 {
21194 struct attribute *attr;
21195 const char *str = NULL;
21196
21197 attr = dwarf2_attr (die, name, cu);
21198
21199 if (attr != NULL)
21200 {
21201 str = attr->as_string ();
21202 if (str == nullptr)
21203 complaint (_("string type expected for attribute %s for "
21204 "DIE at %s in module %s"),
21205 dwarf_attr_name (name), sect_offset_str (die->sect_off),
21206 objfile_name (cu->per_objfile->objfile));
21207 }
21208
21209 return str;
21210 }
21211
21212 /* Return the dwo name or NULL if not present. If present, it is in either
21213 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
21214 static const char *
21215 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21216 {
21217 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21218 if (dwo_name == nullptr)
21219 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21220 return dwo_name;
21221 }
21222
21223 /* Return non-zero iff the attribute NAME is defined for the given DIE,
21224 and holds a non-zero value. This function should only be used for
21225 DW_FORM_flag or DW_FORM_flag_present attributes. */
21226
21227 static int
21228 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21229 {
21230 struct attribute *attr = dwarf2_attr (die, name, cu);
21231
21232 return attr != nullptr && attr->as_boolean ();
21233 }
21234
21235 static int
21236 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
21237 {
21238 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21239 which value is non-zero. However, we have to be careful with
21240 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21241 (via dwarf2_flag_true_p) follows this attribute. So we may
21242 end up accidently finding a declaration attribute that belongs
21243 to a different DIE referenced by the specification attribute,
21244 even though the given DIE does not have a declaration attribute. */
21245 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21246 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
21247 }
21248
21249 /* Return the die giving the specification for DIE, if there is
21250 one. *SPEC_CU is the CU containing DIE on input, and the CU
21251 containing the return value on output. If there is no
21252 specification, but there is an abstract origin, that is
21253 returned. */
21254
21255 static struct die_info *
21256 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
21257 {
21258 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21259 *spec_cu);
21260
21261 if (spec_attr == NULL)
21262 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21263
21264 if (spec_attr == NULL)
21265 return NULL;
21266 else
21267 return follow_die_ref (die, spec_attr, spec_cu);
21268 }
21269
21270 /* Stub for free_line_header to match void * callback types. */
21271
21272 static void
21273 free_line_header_voidp (void *arg)
21274 {
21275 struct line_header *lh = (struct line_header *) arg;
21276
21277 delete lh;
21278 }
21279
21280 /* A convenience function to find the proper .debug_line section for a CU. */
21281
21282 static struct dwarf2_section_info *
21283 get_debug_line_section (struct dwarf2_cu *cu)
21284 {
21285 struct dwarf2_section_info *section;
21286 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21287
21288 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21289 DWO file. */
21290 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21291 section = &cu->dwo_unit->dwo_file->sections.line;
21292 else if (cu->per_cu->is_dwz)
21293 {
21294 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
21295
21296 section = &dwz->line;
21297 }
21298 else
21299 section = &per_objfile->per_bfd->line;
21300
21301 return section;
21302 }
21303
21304 /* Read the statement program header starting at OFFSET in
21305 .debug_line, or .debug_line.dwo. Return a pointer
21306 to a struct line_header, allocated using xmalloc.
21307 Returns NULL if there is a problem reading the header, e.g., if it
21308 has a version we don't understand.
21309
21310 NOTE: the strings in the include directory and file name tables of
21311 the returned object point into the dwarf line section buffer,
21312 and must not be freed. */
21313
21314 static line_header_up
21315 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21316 {
21317 struct dwarf2_section_info *section;
21318 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21319
21320 section = get_debug_line_section (cu);
21321 section->read (per_objfile->objfile);
21322 if (section->buffer == NULL)
21323 {
21324 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21325 complaint (_("missing .debug_line.dwo section"));
21326 else
21327 complaint (_("missing .debug_line section"));
21328 return 0;
21329 }
21330
21331 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21332 per_objfile, section, &cu->header);
21333 }
21334
21335 /* Subroutine of dwarf_decode_lines to simplify it.
21336 Return the file name of the psymtab for the given file_entry.
21337 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21338 If space for the result is malloc'd, *NAME_HOLDER will be set.
21339 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21340
21341 static const char *
21342 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21343 const dwarf2_psymtab *pst,
21344 const char *comp_dir,
21345 gdb::unique_xmalloc_ptr<char> *name_holder)
21346 {
21347 const char *include_name = fe.name;
21348 const char *include_name_to_compare = include_name;
21349 const char *pst_filename;
21350 int file_is_pst;
21351
21352 const char *dir_name = fe.include_dir (lh);
21353
21354 gdb::unique_xmalloc_ptr<char> hold_compare;
21355 if (!IS_ABSOLUTE_PATH (include_name)
21356 && (dir_name != NULL || comp_dir != NULL))
21357 {
21358 /* Avoid creating a duplicate psymtab for PST.
21359 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21360 Before we do the comparison, however, we need to account
21361 for DIR_NAME and COMP_DIR.
21362 First prepend dir_name (if non-NULL). If we still don't
21363 have an absolute path prepend comp_dir (if non-NULL).
21364 However, the directory we record in the include-file's
21365 psymtab does not contain COMP_DIR (to match the
21366 corresponding symtab(s)).
21367
21368 Example:
21369
21370 bash$ cd /tmp
21371 bash$ gcc -g ./hello.c
21372 include_name = "hello.c"
21373 dir_name = "."
21374 DW_AT_comp_dir = comp_dir = "/tmp"
21375 DW_AT_name = "./hello.c"
21376
21377 */
21378
21379 if (dir_name != NULL)
21380 {
21381 name_holder->reset (concat (dir_name, SLASH_STRING,
21382 include_name, (char *) NULL));
21383 include_name = name_holder->get ();
21384 include_name_to_compare = include_name;
21385 }
21386 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21387 {
21388 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21389 include_name, (char *) NULL));
21390 include_name_to_compare = hold_compare.get ();
21391 }
21392 }
21393
21394 pst_filename = pst->filename;
21395 gdb::unique_xmalloc_ptr<char> copied_name;
21396 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21397 {
21398 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21399 pst_filename, (char *) NULL));
21400 pst_filename = copied_name.get ();
21401 }
21402
21403 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21404
21405 if (file_is_pst)
21406 return NULL;
21407 return include_name;
21408 }
21409
21410 /* State machine to track the state of the line number program. */
21411
21412 class lnp_state_machine
21413 {
21414 public:
21415 /* Initialize a machine state for the start of a line number
21416 program. */
21417 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21418 bool record_lines_p);
21419
21420 file_entry *current_file ()
21421 {
21422 /* lh->file_names is 0-based, but the file name numbers in the
21423 statement program are 1-based. */
21424 return m_line_header->file_name_at (m_file);
21425 }
21426
21427 /* Record the line in the state machine. END_SEQUENCE is true if
21428 we're processing the end of a sequence. */
21429 void record_line (bool end_sequence);
21430
21431 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21432 nop-out rest of the lines in this sequence. */
21433 void check_line_address (struct dwarf2_cu *cu,
21434 const gdb_byte *line_ptr,
21435 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21436
21437 void handle_set_discriminator (unsigned int discriminator)
21438 {
21439 m_discriminator = discriminator;
21440 m_line_has_non_zero_discriminator |= discriminator != 0;
21441 }
21442
21443 /* Handle DW_LNE_set_address. */
21444 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21445 {
21446 m_op_index = 0;
21447 address += baseaddr;
21448 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21449 }
21450
21451 /* Handle DW_LNS_advance_pc. */
21452 void handle_advance_pc (CORE_ADDR adjust);
21453
21454 /* Handle a special opcode. */
21455 void handle_special_opcode (unsigned char op_code);
21456
21457 /* Handle DW_LNS_advance_line. */
21458 void handle_advance_line (int line_delta)
21459 {
21460 advance_line (line_delta);
21461 }
21462
21463 /* Handle DW_LNS_set_file. */
21464 void handle_set_file (file_name_index file);
21465
21466 /* Handle DW_LNS_negate_stmt. */
21467 void handle_negate_stmt ()
21468 {
21469 m_is_stmt = !m_is_stmt;
21470 }
21471
21472 /* Handle DW_LNS_const_add_pc. */
21473 void handle_const_add_pc ();
21474
21475 /* Handle DW_LNS_fixed_advance_pc. */
21476 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21477 {
21478 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21479 m_op_index = 0;
21480 }
21481
21482 /* Handle DW_LNS_copy. */
21483 void handle_copy ()
21484 {
21485 record_line (false);
21486 m_discriminator = 0;
21487 }
21488
21489 /* Handle DW_LNE_end_sequence. */
21490 void handle_end_sequence ()
21491 {
21492 m_currently_recording_lines = true;
21493 }
21494
21495 private:
21496 /* Advance the line by LINE_DELTA. */
21497 void advance_line (int line_delta)
21498 {
21499 m_line += line_delta;
21500
21501 if (line_delta != 0)
21502 m_line_has_non_zero_discriminator = m_discriminator != 0;
21503 }
21504
21505 struct dwarf2_cu *m_cu;
21506
21507 gdbarch *m_gdbarch;
21508
21509 /* True if we're recording lines.
21510 Otherwise we're building partial symtabs and are just interested in
21511 finding include files mentioned by the line number program. */
21512 bool m_record_lines_p;
21513
21514 /* The line number header. */
21515 line_header *m_line_header;
21516
21517 /* These are part of the standard DWARF line number state machine,
21518 and initialized according to the DWARF spec. */
21519
21520 unsigned char m_op_index = 0;
21521 /* The line table index of the current file. */
21522 file_name_index m_file = 1;
21523 unsigned int m_line = 1;
21524
21525 /* These are initialized in the constructor. */
21526
21527 CORE_ADDR m_address;
21528 bool m_is_stmt;
21529 unsigned int m_discriminator;
21530
21531 /* Additional bits of state we need to track. */
21532
21533 /* The last file that we called dwarf2_start_subfile for.
21534 This is only used for TLLs. */
21535 unsigned int m_last_file = 0;
21536 /* The last file a line number was recorded for. */
21537 struct subfile *m_last_subfile = NULL;
21538
21539 /* The address of the last line entry. */
21540 CORE_ADDR m_last_address;
21541
21542 /* Set to true when a previous line at the same address (using
21543 m_last_address) had m_is_stmt true. This is reset to false when a
21544 line entry at a new address (m_address different to m_last_address) is
21545 processed. */
21546 bool m_stmt_at_address = false;
21547
21548 /* When true, record the lines we decode. */
21549 bool m_currently_recording_lines = false;
21550
21551 /* The last line number that was recorded, used to coalesce
21552 consecutive entries for the same line. This can happen, for
21553 example, when discriminators are present. PR 17276. */
21554 unsigned int m_last_line = 0;
21555 bool m_line_has_non_zero_discriminator = false;
21556 };
21557
21558 void
21559 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21560 {
21561 CORE_ADDR addr_adj = (((m_op_index + adjust)
21562 / m_line_header->maximum_ops_per_instruction)
21563 * m_line_header->minimum_instruction_length);
21564 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21565 m_op_index = ((m_op_index + adjust)
21566 % m_line_header->maximum_ops_per_instruction);
21567 }
21568
21569 void
21570 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21571 {
21572 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21573 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21574 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21575 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21576 / m_line_header->maximum_ops_per_instruction)
21577 * m_line_header->minimum_instruction_length);
21578 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21579 m_op_index = ((m_op_index + adj_opcode_d)
21580 % m_line_header->maximum_ops_per_instruction);
21581
21582 int line_delta = m_line_header->line_base + adj_opcode_r;
21583 advance_line (line_delta);
21584 record_line (false);
21585 m_discriminator = 0;
21586 }
21587
21588 void
21589 lnp_state_machine::handle_set_file (file_name_index file)
21590 {
21591 m_file = file;
21592
21593 const file_entry *fe = current_file ();
21594 if (fe == NULL)
21595 dwarf2_debug_line_missing_file_complaint ();
21596 else if (m_record_lines_p)
21597 {
21598 const char *dir = fe->include_dir (m_line_header);
21599
21600 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21601 m_line_has_non_zero_discriminator = m_discriminator != 0;
21602 dwarf2_start_subfile (m_cu, fe->name, dir);
21603 }
21604 }
21605
21606 void
21607 lnp_state_machine::handle_const_add_pc ()
21608 {
21609 CORE_ADDR adjust
21610 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21611
21612 CORE_ADDR addr_adj
21613 = (((m_op_index + adjust)
21614 / m_line_header->maximum_ops_per_instruction)
21615 * m_line_header->minimum_instruction_length);
21616
21617 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21618 m_op_index = ((m_op_index + adjust)
21619 % m_line_header->maximum_ops_per_instruction);
21620 }
21621
21622 /* Return non-zero if we should add LINE to the line number table.
21623 LINE is the line to add, LAST_LINE is the last line that was added,
21624 LAST_SUBFILE is the subfile for LAST_LINE.
21625 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21626 had a non-zero discriminator.
21627
21628 We have to be careful in the presence of discriminators.
21629 E.g., for this line:
21630
21631 for (i = 0; i < 100000; i++);
21632
21633 clang can emit four line number entries for that one line,
21634 each with a different discriminator.
21635 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21636
21637 However, we want gdb to coalesce all four entries into one.
21638 Otherwise the user could stepi into the middle of the line and
21639 gdb would get confused about whether the pc really was in the
21640 middle of the line.
21641
21642 Things are further complicated by the fact that two consecutive
21643 line number entries for the same line is a heuristic used by gcc
21644 to denote the end of the prologue. So we can't just discard duplicate
21645 entries, we have to be selective about it. The heuristic we use is
21646 that we only collapse consecutive entries for the same line if at least
21647 one of those entries has a non-zero discriminator. PR 17276.
21648
21649 Note: Addresses in the line number state machine can never go backwards
21650 within one sequence, thus this coalescing is ok. */
21651
21652 static int
21653 dwarf_record_line_p (struct dwarf2_cu *cu,
21654 unsigned int line, unsigned int last_line,
21655 int line_has_non_zero_discriminator,
21656 struct subfile *last_subfile)
21657 {
21658 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21659 return 1;
21660 if (line != last_line)
21661 return 1;
21662 /* Same line for the same file that we've seen already.
21663 As a last check, for pr 17276, only record the line if the line
21664 has never had a non-zero discriminator. */
21665 if (!line_has_non_zero_discriminator)
21666 return 1;
21667 return 0;
21668 }
21669
21670 /* Use the CU's builder to record line number LINE beginning at
21671 address ADDRESS in the line table of subfile SUBFILE. */
21672
21673 static void
21674 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21675 unsigned int line, CORE_ADDR address, bool is_stmt,
21676 struct dwarf2_cu *cu)
21677 {
21678 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21679
21680 if (dwarf_line_debug)
21681 {
21682 fprintf_unfiltered (gdb_stdlog,
21683 "Recording line %u, file %s, address %s\n",
21684 line, lbasename (subfile->name),
21685 paddress (gdbarch, address));
21686 }
21687
21688 if (cu != nullptr)
21689 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21690 }
21691
21692 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21693 Mark the end of a set of line number records.
21694 The arguments are the same as for dwarf_record_line_1.
21695 If SUBFILE is NULL the request is ignored. */
21696
21697 static void
21698 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21699 CORE_ADDR address, struct dwarf2_cu *cu)
21700 {
21701 if (subfile == NULL)
21702 return;
21703
21704 if (dwarf_line_debug)
21705 {
21706 fprintf_unfiltered (gdb_stdlog,
21707 "Finishing current line, file %s, address %s\n",
21708 lbasename (subfile->name),
21709 paddress (gdbarch, address));
21710 }
21711
21712 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21713 }
21714
21715 void
21716 lnp_state_machine::record_line (bool end_sequence)
21717 {
21718 if (dwarf_line_debug)
21719 {
21720 fprintf_unfiltered (gdb_stdlog,
21721 "Processing actual line %u: file %u,"
21722 " address %s, is_stmt %u, discrim %u%s\n",
21723 m_line, m_file,
21724 paddress (m_gdbarch, m_address),
21725 m_is_stmt, m_discriminator,
21726 (end_sequence ? "\t(end sequence)" : ""));
21727 }
21728
21729 file_entry *fe = current_file ();
21730
21731 if (fe == NULL)
21732 dwarf2_debug_line_missing_file_complaint ();
21733 /* For now we ignore lines not starting on an instruction boundary.
21734 But not when processing end_sequence for compatibility with the
21735 previous version of the code. */
21736 else if (m_op_index == 0 || end_sequence)
21737 {
21738 fe->included_p = 1;
21739 if (m_record_lines_p)
21740 {
21741 /* When we switch files we insert an end maker in the first file,
21742 switch to the second file and add a new line entry. The
21743 problem is that the end marker inserted in the first file will
21744 discard any previous line entries at the same address. If the
21745 line entries in the first file are marked as is-stmt, while
21746 the new line in the second file is non-stmt, then this means
21747 the end marker will discard is-stmt lines so we can have a
21748 non-stmt line. This means that there are less addresses at
21749 which the user can insert a breakpoint.
21750
21751 To improve this we track the last address in m_last_address,
21752 and whether we have seen an is-stmt at this address. Then
21753 when switching files, if we have seen a stmt at the current
21754 address, and we are switching to create a non-stmt line, then
21755 discard the new line. */
21756 bool file_changed
21757 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21758 bool ignore_this_line
21759 = ((file_changed && !end_sequence && m_last_address == m_address
21760 && !m_is_stmt && m_stmt_at_address)
21761 || (!end_sequence && m_line == 0));
21762
21763 if ((file_changed && !ignore_this_line) || end_sequence)
21764 {
21765 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21766 m_currently_recording_lines ? m_cu : nullptr);
21767 }
21768
21769 if (!end_sequence && !ignore_this_line)
21770 {
21771 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21772
21773 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21774 m_line_has_non_zero_discriminator,
21775 m_last_subfile))
21776 {
21777 buildsym_compunit *builder = m_cu->get_builder ();
21778 dwarf_record_line_1 (m_gdbarch,
21779 builder->get_current_subfile (),
21780 m_line, m_address, is_stmt,
21781 m_currently_recording_lines ? m_cu : nullptr);
21782 }
21783 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21784 m_last_line = m_line;
21785 }
21786 }
21787 }
21788
21789 /* Track whether we have seen any m_is_stmt true at m_address in case we
21790 have multiple line table entries all at m_address. */
21791 if (m_last_address != m_address)
21792 {
21793 m_stmt_at_address = false;
21794 m_last_address = m_address;
21795 }
21796 m_stmt_at_address |= m_is_stmt;
21797 }
21798
21799 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21800 line_header *lh, bool record_lines_p)
21801 {
21802 m_cu = cu;
21803 m_gdbarch = arch;
21804 m_record_lines_p = record_lines_p;
21805 m_line_header = lh;
21806
21807 m_currently_recording_lines = true;
21808
21809 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21810 was a line entry for it so that the backend has a chance to adjust it
21811 and also record it in case it needs it. This is currently used by MIPS
21812 code, cf. `mips_adjust_dwarf2_line'. */
21813 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21814 m_is_stmt = lh->default_is_stmt;
21815 m_discriminator = 0;
21816
21817 m_last_address = m_address;
21818 m_stmt_at_address = false;
21819 }
21820
21821 void
21822 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21823 const gdb_byte *line_ptr,
21824 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21825 {
21826 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21827 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21828 located at 0x0. In this case, additionally check that if
21829 ADDRESS < UNRELOCATED_LOWPC. */
21830
21831 if ((address == 0 && address < unrelocated_lowpc)
21832 || address == (CORE_ADDR) -1)
21833 {
21834 /* This line table is for a function which has been
21835 GCd by the linker. Ignore it. PR gdb/12528 */
21836
21837 struct objfile *objfile = cu->per_objfile->objfile;
21838 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21839
21840 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21841 line_offset, objfile_name (objfile));
21842 m_currently_recording_lines = false;
21843 /* Note: m_currently_recording_lines is left as false until we see
21844 DW_LNE_end_sequence. */
21845 }
21846 }
21847
21848 /* Subroutine of dwarf_decode_lines to simplify it.
21849 Process the line number information in LH.
21850 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21851 program in order to set included_p for every referenced header. */
21852
21853 static void
21854 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21855 const int decode_for_pst_p, CORE_ADDR lowpc)
21856 {
21857 const gdb_byte *line_ptr, *extended_end;
21858 const gdb_byte *line_end;
21859 unsigned int bytes_read, extended_len;
21860 unsigned char op_code, extended_op;
21861 CORE_ADDR baseaddr;
21862 struct objfile *objfile = cu->per_objfile->objfile;
21863 bfd *abfd = objfile->obfd;
21864 struct gdbarch *gdbarch = objfile->arch ();
21865 /* True if we're recording line info (as opposed to building partial
21866 symtabs and just interested in finding include files mentioned by
21867 the line number program). */
21868 bool record_lines_p = !decode_for_pst_p;
21869
21870 baseaddr = objfile->text_section_offset ();
21871
21872 line_ptr = lh->statement_program_start;
21873 line_end = lh->statement_program_end;
21874
21875 /* Read the statement sequences until there's nothing left. */
21876 while (line_ptr < line_end)
21877 {
21878 /* The DWARF line number program state machine. Reset the state
21879 machine at the start of each sequence. */
21880 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21881 bool end_sequence = false;
21882
21883 if (record_lines_p)
21884 {
21885 /* Start a subfile for the current file of the state
21886 machine. */
21887 const file_entry *fe = state_machine.current_file ();
21888
21889 if (fe != NULL)
21890 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21891 }
21892
21893 /* Decode the table. */
21894 while (line_ptr < line_end && !end_sequence)
21895 {
21896 op_code = read_1_byte (abfd, line_ptr);
21897 line_ptr += 1;
21898
21899 if (op_code >= lh->opcode_base)
21900 {
21901 /* Special opcode. */
21902 state_machine.handle_special_opcode (op_code);
21903 }
21904 else switch (op_code)
21905 {
21906 case DW_LNS_extended_op:
21907 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21908 &bytes_read);
21909 line_ptr += bytes_read;
21910 extended_end = line_ptr + extended_len;
21911 extended_op = read_1_byte (abfd, line_ptr);
21912 line_ptr += 1;
21913 if (DW_LNE_lo_user <= extended_op
21914 && extended_op <= DW_LNE_hi_user)
21915 {
21916 /* Vendor extension, ignore. */
21917 line_ptr = extended_end;
21918 break;
21919 }
21920 switch (extended_op)
21921 {
21922 case DW_LNE_end_sequence:
21923 state_machine.handle_end_sequence ();
21924 end_sequence = true;
21925 break;
21926 case DW_LNE_set_address:
21927 {
21928 CORE_ADDR address
21929 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21930 line_ptr += bytes_read;
21931
21932 state_machine.check_line_address (cu, line_ptr,
21933 lowpc - baseaddr, address);
21934 state_machine.handle_set_address (baseaddr, address);
21935 }
21936 break;
21937 case DW_LNE_define_file:
21938 {
21939 const char *cur_file;
21940 unsigned int mod_time, length;
21941 dir_index dindex;
21942
21943 cur_file = read_direct_string (abfd, line_ptr,
21944 &bytes_read);
21945 line_ptr += bytes_read;
21946 dindex = (dir_index)
21947 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21948 line_ptr += bytes_read;
21949 mod_time =
21950 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21951 line_ptr += bytes_read;
21952 length =
21953 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21954 line_ptr += bytes_read;
21955 lh->add_file_name (cur_file, dindex, mod_time, length);
21956 }
21957 break;
21958 case DW_LNE_set_discriminator:
21959 {
21960 /* The discriminator is not interesting to the
21961 debugger; just ignore it. We still need to
21962 check its value though:
21963 if there are consecutive entries for the same
21964 (non-prologue) line we want to coalesce them.
21965 PR 17276. */
21966 unsigned int discr
21967 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21968 line_ptr += bytes_read;
21969
21970 state_machine.handle_set_discriminator (discr);
21971 }
21972 break;
21973 default:
21974 complaint (_("mangled .debug_line section"));
21975 return;
21976 }
21977 /* Make sure that we parsed the extended op correctly. If e.g.
21978 we expected a different address size than the producer used,
21979 we may have read the wrong number of bytes. */
21980 if (line_ptr != extended_end)
21981 {
21982 complaint (_("mangled .debug_line section"));
21983 return;
21984 }
21985 break;
21986 case DW_LNS_copy:
21987 state_machine.handle_copy ();
21988 break;
21989 case DW_LNS_advance_pc:
21990 {
21991 CORE_ADDR adjust
21992 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21993 line_ptr += bytes_read;
21994
21995 state_machine.handle_advance_pc (adjust);
21996 }
21997 break;
21998 case DW_LNS_advance_line:
21999 {
22000 int line_delta
22001 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
22002 line_ptr += bytes_read;
22003
22004 state_machine.handle_advance_line (line_delta);
22005 }
22006 break;
22007 case DW_LNS_set_file:
22008 {
22009 file_name_index file
22010 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
22011 &bytes_read);
22012 line_ptr += bytes_read;
22013
22014 state_machine.handle_set_file (file);
22015 }
22016 break;
22017 case DW_LNS_set_column:
22018 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
22019 line_ptr += bytes_read;
22020 break;
22021 case DW_LNS_negate_stmt:
22022 state_machine.handle_negate_stmt ();
22023 break;
22024 case DW_LNS_set_basic_block:
22025 break;
22026 /* Add to the address register of the state machine the
22027 address increment value corresponding to special opcode
22028 255. I.e., this value is scaled by the minimum
22029 instruction length since special opcode 255 would have
22030 scaled the increment. */
22031 case DW_LNS_const_add_pc:
22032 state_machine.handle_const_add_pc ();
22033 break;
22034 case DW_LNS_fixed_advance_pc:
22035 {
22036 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
22037 line_ptr += 2;
22038
22039 state_machine.handle_fixed_advance_pc (addr_adj);
22040 }
22041 break;
22042 default:
22043 {
22044 /* Unknown standard opcode, ignore it. */
22045 int i;
22046
22047 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
22048 {
22049 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
22050 line_ptr += bytes_read;
22051 }
22052 }
22053 }
22054 }
22055
22056 if (!end_sequence)
22057 dwarf2_debug_line_missing_end_sequence_complaint ();
22058
22059 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
22060 in which case we still finish recording the last line). */
22061 state_machine.record_line (true);
22062 }
22063 }
22064
22065 /* Decode the Line Number Program (LNP) for the given line_header
22066 structure and CU. The actual information extracted and the type
22067 of structures created from the LNP depends on the value of PST.
22068
22069 1. If PST is NULL, then this procedure uses the data from the program
22070 to create all necessary symbol tables, and their linetables.
22071
22072 2. If PST is not NULL, this procedure reads the program to determine
22073 the list of files included by the unit represented by PST, and
22074 builds all the associated partial symbol tables.
22075
22076 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
22077 It is used for relative paths in the line table.
22078 NOTE: When processing partial symtabs (pst != NULL),
22079 comp_dir == pst->dirname.
22080
22081 NOTE: It is important that psymtabs have the same file name (via strcmp)
22082 as the corresponding symtab. Since COMP_DIR is not used in the name of the
22083 symtab we don't use it in the name of the psymtabs we create.
22084 E.g. expand_line_sal requires this when finding psymtabs to expand.
22085 A good testcase for this is mb-inline.exp.
22086
22087 LOWPC is the lowest address in CU (or 0 if not known).
22088
22089 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
22090 for its PC<->lines mapping information. Otherwise only the filename
22091 table is read in. */
22092
22093 static void
22094 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
22095 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
22096 CORE_ADDR lowpc, int decode_mapping)
22097 {
22098 struct objfile *objfile = cu->per_objfile->objfile;
22099 const int decode_for_pst_p = (pst != NULL);
22100
22101 if (decode_mapping)
22102 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
22103
22104 if (decode_for_pst_p)
22105 {
22106 /* Now that we're done scanning the Line Header Program, we can
22107 create the psymtab of each included file. */
22108 for (auto &file_entry : lh->file_names ())
22109 if (file_entry.included_p == 1)
22110 {
22111 gdb::unique_xmalloc_ptr<char> name_holder;
22112 const char *include_name =
22113 psymtab_include_file_name (lh, file_entry, pst,
22114 comp_dir, &name_holder);
22115 if (include_name != NULL)
22116 dwarf2_create_include_psymtab
22117 (cu->per_objfile->per_bfd, include_name, pst,
22118 cu->per_objfile->per_bfd->partial_symtabs.get (),
22119 objfile->per_bfd);
22120 }
22121 }
22122 else
22123 {
22124 /* Make sure a symtab is created for every file, even files
22125 which contain only variables (i.e. no code with associated
22126 line numbers). */
22127 buildsym_compunit *builder = cu->get_builder ();
22128 struct compunit_symtab *cust = builder->get_compunit_symtab ();
22129
22130 for (auto &fe : lh->file_names ())
22131 {
22132 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
22133 if (builder->get_current_subfile ()->symtab == NULL)
22134 {
22135 builder->get_current_subfile ()->symtab
22136 = allocate_symtab (cust,
22137 builder->get_current_subfile ()->name);
22138 }
22139 fe.symtab = builder->get_current_subfile ()->symtab;
22140 }
22141 }
22142 }
22143
22144 /* Start a subfile for DWARF. FILENAME is the name of the file and
22145 DIRNAME the name of the source directory which contains FILENAME
22146 or NULL if not known.
22147 This routine tries to keep line numbers from identical absolute and
22148 relative file names in a common subfile.
22149
22150 Using the `list' example from the GDB testsuite, which resides in
22151 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22152 of /srcdir/list0.c yields the following debugging information for list0.c:
22153
22154 DW_AT_name: /srcdir/list0.c
22155 DW_AT_comp_dir: /compdir
22156 files.files[0].name: list0.h
22157 files.files[0].dir: /srcdir
22158 files.files[1].name: list0.c
22159 files.files[1].dir: /srcdir
22160
22161 The line number information for list0.c has to end up in a single
22162 subfile, so that `break /srcdir/list0.c:1' works as expected.
22163 start_subfile will ensure that this happens provided that we pass the
22164 concatenation of files.files[1].dir and files.files[1].name as the
22165 subfile's name. */
22166
22167 static void
22168 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22169 const char *dirname)
22170 {
22171 gdb::unique_xmalloc_ptr<char> copy;
22172
22173 /* In order not to lose the line information directory,
22174 we concatenate it to the filename when it makes sense.
22175 Note that the Dwarf3 standard says (speaking of filenames in line
22176 information): ``The directory index is ignored for file names
22177 that represent full path names''. Thus ignoring dirname in the
22178 `else' branch below isn't an issue. */
22179
22180 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
22181 {
22182 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22183 filename = copy.get ();
22184 }
22185
22186 cu->get_builder ()->start_subfile (filename);
22187 }
22188
22189 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22190 buildsym_compunit constructor. */
22191
22192 struct compunit_symtab *
22193 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22194 CORE_ADDR low_pc)
22195 {
22196 gdb_assert (m_builder == nullptr);
22197
22198 m_builder.reset (new struct buildsym_compunit
22199 (this->per_objfile->objfile,
22200 name, comp_dir, language, low_pc));
22201
22202 list_in_scope = get_builder ()->get_file_symbols ();
22203
22204 get_builder ()->record_debugformat ("DWARF 2");
22205 get_builder ()->record_producer (producer);
22206
22207 processing_has_namespace_info = false;
22208
22209 return get_builder ()->get_compunit_symtab ();
22210 }
22211
22212 static void
22213 var_decode_location (struct attribute *attr, struct symbol *sym,
22214 struct dwarf2_cu *cu)
22215 {
22216 struct objfile *objfile = cu->per_objfile->objfile;
22217 struct comp_unit_head *cu_header = &cu->header;
22218
22219 /* NOTE drow/2003-01-30: There used to be a comment and some special
22220 code here to turn a symbol with DW_AT_external and a
22221 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22222 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22223 with some versions of binutils) where shared libraries could have
22224 relocations against symbols in their debug information - the
22225 minimal symbol would have the right address, but the debug info
22226 would not. It's no longer necessary, because we will explicitly
22227 apply relocations when we read in the debug information now. */
22228
22229 /* A DW_AT_location attribute with no contents indicates that a
22230 variable has been optimized away. */
22231 if (attr->form_is_block () && attr->as_block ()->size == 0)
22232 {
22233 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22234 return;
22235 }
22236
22237 /* Handle one degenerate form of location expression specially, to
22238 preserve GDB's previous behavior when section offsets are
22239 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22240 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
22241
22242 if (attr->form_is_block ())
22243 {
22244 struct dwarf_block *block = attr->as_block ();
22245
22246 if ((block->data[0] == DW_OP_addr
22247 && block->size == 1 + cu_header->addr_size)
22248 || ((block->data[0] == DW_OP_GNU_addr_index
22249 || block->data[0] == DW_OP_addrx)
22250 && (block->size
22251 == 1 + leb128_size (&block->data[1]))))
22252 {
22253 unsigned int dummy;
22254
22255 if (block->data[0] == DW_OP_addr)
22256 SET_SYMBOL_VALUE_ADDRESS
22257 (sym, cu->header.read_address (objfile->obfd,
22258 block->data + 1,
22259 &dummy));
22260 else
22261 SET_SYMBOL_VALUE_ADDRESS
22262 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22263 &dummy));
22264 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22265 fixup_symbol_section (sym, objfile);
22266 SET_SYMBOL_VALUE_ADDRESS
22267 (sym,
22268 SYMBOL_VALUE_ADDRESS (sym)
22269 + objfile->section_offsets[sym->section_index ()]);
22270 return;
22271 }
22272 }
22273
22274 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22275 expression evaluator, and use LOC_COMPUTED only when necessary
22276 (i.e. when the value of a register or memory location is
22277 referenced, or a thread-local block, etc.). Then again, it might
22278 not be worthwhile. I'm assuming that it isn't unless performance
22279 or memory numbers show me otherwise. */
22280
22281 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22282
22283 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22284 cu->has_loclist = true;
22285 }
22286
22287 /* Given a pointer to a DWARF information entry, figure out if we need
22288 to make a symbol table entry for it, and if so, create a new entry
22289 and return a pointer to it.
22290 If TYPE is NULL, determine symbol type from the die, otherwise
22291 used the passed type.
22292 If SPACE is not NULL, use it to hold the new symbol. If it is
22293 NULL, allocate a new symbol on the objfile's obstack. */
22294
22295 static struct symbol *
22296 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22297 struct symbol *space)
22298 {
22299 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22300 struct objfile *objfile = per_objfile->objfile;
22301 struct gdbarch *gdbarch = objfile->arch ();
22302 struct symbol *sym = NULL;
22303 const char *name;
22304 struct attribute *attr = NULL;
22305 struct attribute *attr2 = NULL;
22306 CORE_ADDR baseaddr;
22307 struct pending **list_to_add = NULL;
22308
22309 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22310
22311 baseaddr = objfile->text_section_offset ();
22312
22313 name = dwarf2_name (die, cu);
22314 if (name)
22315 {
22316 int suppress_add = 0;
22317
22318 if (space)
22319 sym = space;
22320 else
22321 sym = new (&objfile->objfile_obstack) symbol;
22322 OBJSTAT (objfile, n_syms++);
22323
22324 /* Cache this symbol's name and the name's demangled form (if any). */
22325 sym->set_language (cu->language, &objfile->objfile_obstack);
22326 /* Fortran does not have mangling standard and the mangling does differ
22327 between gfortran, iFort etc. */
22328 const char *physname
22329 = (cu->language == language_fortran
22330 ? dwarf2_full_name (name, die, cu)
22331 : dwarf2_physname (name, die, cu));
22332 const char *linkagename = dw2_linkage_name (die, cu);
22333
22334 if (linkagename == nullptr || cu->language == language_ada)
22335 sym->set_linkage_name (physname);
22336 else
22337 {
22338 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22339 sym->set_linkage_name (linkagename);
22340 }
22341
22342 /* Default assumptions.
22343 Use the passed type or decode it from the die. */
22344 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22345 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22346 if (type != NULL)
22347 SYMBOL_TYPE (sym) = type;
22348 else
22349 SYMBOL_TYPE (sym) = die_type (die, cu);
22350 attr = dwarf2_attr (die,
22351 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22352 cu);
22353 if (attr != nullptr)
22354 SYMBOL_LINE (sym) = attr->constant_value (0);
22355
22356 attr = dwarf2_attr (die,
22357 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22358 cu);
22359 if (attr != nullptr && attr->is_nonnegative ())
22360 {
22361 file_name_index file_index
22362 = (file_name_index) attr->as_nonnegative ();
22363 struct file_entry *fe;
22364
22365 if (cu->line_header != NULL)
22366 fe = cu->line_header->file_name_at (file_index);
22367 else
22368 fe = NULL;
22369
22370 if (fe == NULL)
22371 complaint (_("file index out of range"));
22372 else
22373 symbol_set_symtab (sym, fe->symtab);
22374 }
22375
22376 switch (die->tag)
22377 {
22378 case DW_TAG_label:
22379 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22380 if (attr != nullptr)
22381 {
22382 CORE_ADDR addr;
22383
22384 addr = attr->as_address ();
22385 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22386 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22387 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22388 }
22389 else
22390 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22391 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22392 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22393 add_symbol_to_list (sym, cu->list_in_scope);
22394 break;
22395 case DW_TAG_subprogram:
22396 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22397 finish_block. */
22398 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22399 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22400 if ((attr2 != nullptr && attr2->as_boolean ())
22401 || cu->language == language_ada
22402 || cu->language == language_fortran)
22403 {
22404 /* Subprograms marked external are stored as a global symbol.
22405 Ada and Fortran subprograms, whether marked external or
22406 not, are always stored as a global symbol, because we want
22407 to be able to access them globally. For instance, we want
22408 to be able to break on a nested subprogram without having
22409 to specify the context. */
22410 list_to_add = cu->get_builder ()->get_global_symbols ();
22411 }
22412 else
22413 {
22414 list_to_add = cu->list_in_scope;
22415 }
22416 break;
22417 case DW_TAG_inlined_subroutine:
22418 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22419 finish_block. */
22420 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22421 SYMBOL_INLINED (sym) = 1;
22422 list_to_add = cu->list_in_scope;
22423 break;
22424 case DW_TAG_template_value_param:
22425 suppress_add = 1;
22426 /* Fall through. */
22427 case DW_TAG_constant:
22428 case DW_TAG_variable:
22429 case DW_TAG_member:
22430 /* Compilation with minimal debug info may result in
22431 variables with missing type entries. Change the
22432 misleading `void' type to something sensible. */
22433 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22434 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22435
22436 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22437 /* In the case of DW_TAG_member, we should only be called for
22438 static const members. */
22439 if (die->tag == DW_TAG_member)
22440 {
22441 /* dwarf2_add_field uses die_is_declaration,
22442 so we do the same. */
22443 gdb_assert (die_is_declaration (die, cu));
22444 gdb_assert (attr);
22445 }
22446 if (attr != nullptr)
22447 {
22448 dwarf2_const_value (attr, sym, cu);
22449 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22450 if (!suppress_add)
22451 {
22452 if (attr2 != nullptr && attr2->as_boolean ())
22453 list_to_add = cu->get_builder ()->get_global_symbols ();
22454 else
22455 list_to_add = cu->list_in_scope;
22456 }
22457 break;
22458 }
22459 attr = dwarf2_attr (die, DW_AT_location, cu);
22460 if (attr != nullptr)
22461 {
22462 var_decode_location (attr, sym, cu);
22463 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22464
22465 /* Fortran explicitly imports any global symbols to the local
22466 scope by DW_TAG_common_block. */
22467 if (cu->language == language_fortran && die->parent
22468 && die->parent->tag == DW_TAG_common_block)
22469 attr2 = NULL;
22470
22471 if (SYMBOL_CLASS (sym) == LOC_STATIC
22472 && SYMBOL_VALUE_ADDRESS (sym) == 0
22473 && !per_objfile->per_bfd->has_section_at_zero)
22474 {
22475 /* When a static variable is eliminated by the linker,
22476 the corresponding debug information is not stripped
22477 out, but the variable address is set to null;
22478 do not add such variables into symbol table. */
22479 }
22480 else if (attr2 != nullptr && attr2->as_boolean ())
22481 {
22482 if (SYMBOL_CLASS (sym) == LOC_STATIC
22483 && (objfile->flags & OBJF_MAINLINE) == 0
22484 && per_objfile->per_bfd->can_copy)
22485 {
22486 /* A global static variable might be subject to
22487 copy relocation. We first check for a local
22488 minsym, though, because maybe the symbol was
22489 marked hidden, in which case this would not
22490 apply. */
22491 bound_minimal_symbol found
22492 = (lookup_minimal_symbol_linkage
22493 (sym->linkage_name (), objfile));
22494 if (found.minsym != nullptr)
22495 sym->maybe_copied = 1;
22496 }
22497
22498 /* A variable with DW_AT_external is never static,
22499 but it may be block-scoped. */
22500 list_to_add
22501 = ((cu->list_in_scope
22502 == cu->get_builder ()->get_file_symbols ())
22503 ? cu->get_builder ()->get_global_symbols ()
22504 : cu->list_in_scope);
22505 }
22506 else
22507 list_to_add = cu->list_in_scope;
22508 }
22509 else
22510 {
22511 /* We do not know the address of this symbol.
22512 If it is an external symbol and we have type information
22513 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22514 The address of the variable will then be determined from
22515 the minimal symbol table whenever the variable is
22516 referenced. */
22517 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22518
22519 /* Fortran explicitly imports any global symbols to the local
22520 scope by DW_TAG_common_block. */
22521 if (cu->language == language_fortran && die->parent
22522 && die->parent->tag == DW_TAG_common_block)
22523 {
22524 /* SYMBOL_CLASS doesn't matter here because
22525 read_common_block is going to reset it. */
22526 if (!suppress_add)
22527 list_to_add = cu->list_in_scope;
22528 }
22529 else if (attr2 != nullptr && attr2->as_boolean ()
22530 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22531 {
22532 /* A variable with DW_AT_external is never static, but it
22533 may be block-scoped. */
22534 list_to_add
22535 = ((cu->list_in_scope
22536 == cu->get_builder ()->get_file_symbols ())
22537 ? cu->get_builder ()->get_global_symbols ()
22538 : cu->list_in_scope);
22539
22540 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22541 }
22542 else if (!die_is_declaration (die, cu))
22543 {
22544 /* Use the default LOC_OPTIMIZED_OUT class. */
22545 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22546 if (!suppress_add)
22547 list_to_add = cu->list_in_scope;
22548 }
22549 }
22550 break;
22551 case DW_TAG_formal_parameter:
22552 {
22553 /* If we are inside a function, mark this as an argument. If
22554 not, we might be looking at an argument to an inlined function
22555 when we do not have enough information to show inlined frames;
22556 pretend it's a local variable in that case so that the user can
22557 still see it. */
22558 struct context_stack *curr
22559 = cu->get_builder ()->get_current_context_stack ();
22560 if (curr != nullptr && curr->name != nullptr)
22561 SYMBOL_IS_ARGUMENT (sym) = 1;
22562 attr = dwarf2_attr (die, DW_AT_location, cu);
22563 if (attr != nullptr)
22564 {
22565 var_decode_location (attr, sym, cu);
22566 }
22567 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22568 if (attr != nullptr)
22569 {
22570 dwarf2_const_value (attr, sym, cu);
22571 }
22572
22573 list_to_add = cu->list_in_scope;
22574 }
22575 break;
22576 case DW_TAG_unspecified_parameters:
22577 /* From varargs functions; gdb doesn't seem to have any
22578 interest in this information, so just ignore it for now.
22579 (FIXME?) */
22580 break;
22581 case DW_TAG_template_type_param:
22582 suppress_add = 1;
22583 /* Fall through. */
22584 case DW_TAG_class_type:
22585 case DW_TAG_interface_type:
22586 case DW_TAG_structure_type:
22587 case DW_TAG_union_type:
22588 case DW_TAG_set_type:
22589 case DW_TAG_enumeration_type:
22590 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22591 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22592
22593 {
22594 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22595 really ever be static objects: otherwise, if you try
22596 to, say, break of a class's method and you're in a file
22597 which doesn't mention that class, it won't work unless
22598 the check for all static symbols in lookup_symbol_aux
22599 saves you. See the OtherFileClass tests in
22600 gdb.c++/namespace.exp. */
22601
22602 if (!suppress_add)
22603 {
22604 buildsym_compunit *builder = cu->get_builder ();
22605 list_to_add
22606 = (cu->list_in_scope == builder->get_file_symbols ()
22607 && cu->language == language_cplus
22608 ? builder->get_global_symbols ()
22609 : cu->list_in_scope);
22610
22611 /* The semantics of C++ state that "struct foo {
22612 ... }" also defines a typedef for "foo". */
22613 if (cu->language == language_cplus
22614 || cu->language == language_ada
22615 || cu->language == language_d
22616 || cu->language == language_rust)
22617 {
22618 /* The symbol's name is already allocated along
22619 with this objfile, so we don't need to
22620 duplicate it for the type. */
22621 if (SYMBOL_TYPE (sym)->name () == 0)
22622 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22623 }
22624 }
22625 }
22626 break;
22627 case DW_TAG_typedef:
22628 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22629 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22630 list_to_add = cu->list_in_scope;
22631 break;
22632 case DW_TAG_array_type:
22633 case DW_TAG_base_type:
22634 case DW_TAG_subrange_type:
22635 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22636 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22637 list_to_add = cu->list_in_scope;
22638 break;
22639 case DW_TAG_enumerator:
22640 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22641 if (attr != nullptr)
22642 {
22643 dwarf2_const_value (attr, sym, cu);
22644 }
22645 {
22646 /* NOTE: carlton/2003-11-10: See comment above in the
22647 DW_TAG_class_type, etc. block. */
22648
22649 list_to_add
22650 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22651 && cu->language == language_cplus
22652 ? cu->get_builder ()->get_global_symbols ()
22653 : cu->list_in_scope);
22654 }
22655 break;
22656 case DW_TAG_imported_declaration:
22657 case DW_TAG_namespace:
22658 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22659 list_to_add = cu->get_builder ()->get_global_symbols ();
22660 break;
22661 case DW_TAG_module:
22662 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22663 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22664 list_to_add = cu->get_builder ()->get_global_symbols ();
22665 break;
22666 case DW_TAG_common_block:
22667 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22668 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22669 add_symbol_to_list (sym, cu->list_in_scope);
22670 break;
22671 default:
22672 /* Not a tag we recognize. Hopefully we aren't processing
22673 trash data, but since we must specifically ignore things
22674 we don't recognize, there is nothing else we should do at
22675 this point. */
22676 complaint (_("unsupported tag: '%s'"),
22677 dwarf_tag_name (die->tag));
22678 break;
22679 }
22680
22681 if (suppress_add)
22682 {
22683 sym->hash_next = objfile->template_symbols;
22684 objfile->template_symbols = sym;
22685 list_to_add = NULL;
22686 }
22687
22688 if (list_to_add != NULL)
22689 add_symbol_to_list (sym, list_to_add);
22690
22691 /* For the benefit of old versions of GCC, check for anonymous
22692 namespaces based on the demangled name. */
22693 if (!cu->processing_has_namespace_info
22694 && cu->language == language_cplus)
22695 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22696 }
22697 return (sym);
22698 }
22699
22700 /* Given an attr with a DW_FORM_dataN value in host byte order,
22701 zero-extend it as appropriate for the symbol's type. The DWARF
22702 standard (v4) is not entirely clear about the meaning of using
22703 DW_FORM_dataN for a constant with a signed type, where the type is
22704 wider than the data. The conclusion of a discussion on the DWARF
22705 list was that this is unspecified. We choose to always zero-extend
22706 because that is the interpretation long in use by GCC. */
22707
22708 static gdb_byte *
22709 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22710 struct dwarf2_cu *cu, LONGEST *value, int bits)
22711 {
22712 struct objfile *objfile = cu->per_objfile->objfile;
22713 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22714 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22715 LONGEST l = attr->constant_value (0);
22716
22717 if (bits < sizeof (*value) * 8)
22718 {
22719 l &= ((LONGEST) 1 << bits) - 1;
22720 *value = l;
22721 }
22722 else if (bits == sizeof (*value) * 8)
22723 *value = l;
22724 else
22725 {
22726 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22727 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22728 return bytes;
22729 }
22730
22731 return NULL;
22732 }
22733
22734 /* Read a constant value from an attribute. Either set *VALUE, or if
22735 the value does not fit in *VALUE, set *BYTES - either already
22736 allocated on the objfile obstack, or newly allocated on OBSTACK,
22737 or, set *BATON, if we translated the constant to a location
22738 expression. */
22739
22740 static void
22741 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22742 const char *name, struct obstack *obstack,
22743 struct dwarf2_cu *cu,
22744 LONGEST *value, const gdb_byte **bytes,
22745 struct dwarf2_locexpr_baton **baton)
22746 {
22747 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22748 struct objfile *objfile = per_objfile->objfile;
22749 struct comp_unit_head *cu_header = &cu->header;
22750 struct dwarf_block *blk;
22751 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22752 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22753
22754 *value = 0;
22755 *bytes = NULL;
22756 *baton = NULL;
22757
22758 switch (attr->form)
22759 {
22760 case DW_FORM_addr:
22761 case DW_FORM_addrx:
22762 case DW_FORM_GNU_addr_index:
22763 {
22764 gdb_byte *data;
22765
22766 if (TYPE_LENGTH (type) != cu_header->addr_size)
22767 dwarf2_const_value_length_mismatch_complaint (name,
22768 cu_header->addr_size,
22769 TYPE_LENGTH (type));
22770 /* Symbols of this form are reasonably rare, so we just
22771 piggyback on the existing location code rather than writing
22772 a new implementation of symbol_computed_ops. */
22773 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22774 (*baton)->per_objfile = per_objfile;
22775 (*baton)->per_cu = cu->per_cu;
22776 gdb_assert ((*baton)->per_cu);
22777
22778 (*baton)->size = 2 + cu_header->addr_size;
22779 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22780 (*baton)->data = data;
22781
22782 data[0] = DW_OP_addr;
22783 store_unsigned_integer (&data[1], cu_header->addr_size,
22784 byte_order, attr->as_address ());
22785 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22786 }
22787 break;
22788 case DW_FORM_string:
22789 case DW_FORM_strp:
22790 case DW_FORM_strx:
22791 case DW_FORM_GNU_str_index:
22792 case DW_FORM_GNU_strp_alt:
22793 /* The string is already allocated on the objfile obstack, point
22794 directly to it. */
22795 *bytes = (const gdb_byte *) attr->as_string ();
22796 break;
22797 case DW_FORM_block1:
22798 case DW_FORM_block2:
22799 case DW_FORM_block4:
22800 case DW_FORM_block:
22801 case DW_FORM_exprloc:
22802 case DW_FORM_data16:
22803 blk = attr->as_block ();
22804 if (TYPE_LENGTH (type) != blk->size)
22805 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22806 TYPE_LENGTH (type));
22807 *bytes = blk->data;
22808 break;
22809
22810 /* The DW_AT_const_value attributes are supposed to carry the
22811 symbol's value "represented as it would be on the target
22812 architecture." By the time we get here, it's already been
22813 converted to host endianness, so we just need to sign- or
22814 zero-extend it as appropriate. */
22815 case DW_FORM_data1:
22816 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22817 break;
22818 case DW_FORM_data2:
22819 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22820 break;
22821 case DW_FORM_data4:
22822 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22823 break;
22824 case DW_FORM_data8:
22825 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22826 break;
22827
22828 case DW_FORM_sdata:
22829 case DW_FORM_implicit_const:
22830 *value = attr->as_signed ();
22831 break;
22832
22833 case DW_FORM_udata:
22834 *value = attr->as_unsigned ();
22835 break;
22836
22837 default:
22838 complaint (_("unsupported const value attribute form: '%s'"),
22839 dwarf_form_name (attr->form));
22840 *value = 0;
22841 break;
22842 }
22843 }
22844
22845
22846 /* Copy constant value from an attribute to a symbol. */
22847
22848 static void
22849 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22850 struct dwarf2_cu *cu)
22851 {
22852 struct objfile *objfile = cu->per_objfile->objfile;
22853 LONGEST value;
22854 const gdb_byte *bytes;
22855 struct dwarf2_locexpr_baton *baton;
22856
22857 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22858 sym->print_name (),
22859 &objfile->objfile_obstack, cu,
22860 &value, &bytes, &baton);
22861
22862 if (baton != NULL)
22863 {
22864 SYMBOL_LOCATION_BATON (sym) = baton;
22865 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22866 }
22867 else if (bytes != NULL)
22868 {
22869 SYMBOL_VALUE_BYTES (sym) = bytes;
22870 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22871 }
22872 else
22873 {
22874 SYMBOL_VALUE (sym) = value;
22875 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22876 }
22877 }
22878
22879 /* Return the type of the die in question using its DW_AT_type attribute. */
22880
22881 static struct type *
22882 die_type (struct die_info *die, struct dwarf2_cu *cu)
22883 {
22884 struct attribute *type_attr;
22885
22886 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22887 if (!type_attr)
22888 {
22889 struct objfile *objfile = cu->per_objfile->objfile;
22890 /* A missing DW_AT_type represents a void type. */
22891 return objfile_type (objfile)->builtin_void;
22892 }
22893
22894 return lookup_die_type (die, type_attr, cu);
22895 }
22896
22897 /* True iff CU's producer generates GNAT Ada auxiliary information
22898 that allows to find parallel types through that information instead
22899 of having to do expensive parallel lookups by type name. */
22900
22901 static int
22902 need_gnat_info (struct dwarf2_cu *cu)
22903 {
22904 /* Assume that the Ada compiler was GNAT, which always produces
22905 the auxiliary information. */
22906 return (cu->language == language_ada);
22907 }
22908
22909 /* Return the auxiliary type of the die in question using its
22910 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22911 attribute is not present. */
22912
22913 static struct type *
22914 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22915 {
22916 struct attribute *type_attr;
22917
22918 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22919 if (!type_attr)
22920 return NULL;
22921
22922 return lookup_die_type (die, type_attr, cu);
22923 }
22924
22925 /* If DIE has a descriptive_type attribute, then set the TYPE's
22926 descriptive type accordingly. */
22927
22928 static void
22929 set_descriptive_type (struct type *type, struct die_info *die,
22930 struct dwarf2_cu *cu)
22931 {
22932 struct type *descriptive_type = die_descriptive_type (die, cu);
22933
22934 if (descriptive_type)
22935 {
22936 ALLOCATE_GNAT_AUX_TYPE (type);
22937 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22938 }
22939 }
22940
22941 /* Return the containing type of the die in question using its
22942 DW_AT_containing_type attribute. */
22943
22944 static struct type *
22945 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22946 {
22947 struct attribute *type_attr;
22948 struct objfile *objfile = cu->per_objfile->objfile;
22949
22950 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22951 if (!type_attr)
22952 error (_("Dwarf Error: Problem turning containing type into gdb type "
22953 "[in module %s]"), objfile_name (objfile));
22954
22955 return lookup_die_type (die, type_attr, cu);
22956 }
22957
22958 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22959
22960 static struct type *
22961 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22962 {
22963 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22964 struct objfile *objfile = per_objfile->objfile;
22965 char *saved;
22966
22967 std::string message
22968 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22969 objfile_name (objfile),
22970 sect_offset_str (cu->header.sect_off),
22971 sect_offset_str (die->sect_off));
22972 saved = obstack_strdup (&objfile->objfile_obstack, message);
22973
22974 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22975 }
22976
22977 /* Look up the type of DIE in CU using its type attribute ATTR.
22978 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22979 DW_AT_containing_type.
22980 If there is no type substitute an error marker. */
22981
22982 static struct type *
22983 lookup_die_type (struct die_info *die, const struct attribute *attr,
22984 struct dwarf2_cu *cu)
22985 {
22986 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22987 struct objfile *objfile = per_objfile->objfile;
22988 struct type *this_type;
22989
22990 gdb_assert (attr->name == DW_AT_type
22991 || attr->name == DW_AT_GNAT_descriptive_type
22992 || attr->name == DW_AT_containing_type);
22993
22994 /* First see if we have it cached. */
22995
22996 if (attr->form == DW_FORM_GNU_ref_alt)
22997 {
22998 struct dwarf2_per_cu_data *per_cu;
22999 sect_offset sect_off = attr->get_ref_die_offset ();
23000
23001 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
23002 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
23003 }
23004 else if (attr->form_is_ref ())
23005 {
23006 sect_offset sect_off = attr->get_ref_die_offset ();
23007
23008 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
23009 }
23010 else if (attr->form == DW_FORM_ref_sig8)
23011 {
23012 ULONGEST signature = attr->as_signature ();
23013
23014 return get_signatured_type (die, signature, cu);
23015 }
23016 else
23017 {
23018 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
23019 " at %s [in module %s]"),
23020 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
23021 objfile_name (objfile));
23022 return build_error_marker_type (cu, die);
23023 }
23024
23025 /* If not cached we need to read it in. */
23026
23027 if (this_type == NULL)
23028 {
23029 struct die_info *type_die = NULL;
23030 struct dwarf2_cu *type_cu = cu;
23031
23032 if (attr->form_is_ref ())
23033 type_die = follow_die_ref (die, attr, &type_cu);
23034 if (type_die == NULL)
23035 return build_error_marker_type (cu, die);
23036 /* If we find the type now, it's probably because the type came
23037 from an inter-CU reference and the type's CU got expanded before
23038 ours. */
23039 this_type = read_type_die (type_die, type_cu);
23040 }
23041
23042 /* If we still don't have a type use an error marker. */
23043
23044 if (this_type == NULL)
23045 return build_error_marker_type (cu, die);
23046
23047 return this_type;
23048 }
23049
23050 /* Return the type in DIE, CU.
23051 Returns NULL for invalid types.
23052
23053 This first does a lookup in die_type_hash,
23054 and only reads the die in if necessary.
23055
23056 NOTE: This can be called when reading in partial or full symbols. */
23057
23058 static struct type *
23059 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
23060 {
23061 struct type *this_type;
23062
23063 this_type = get_die_type (die, cu);
23064 if (this_type)
23065 return this_type;
23066
23067 return read_type_die_1 (die, cu);
23068 }
23069
23070 /* Read the type in DIE, CU.
23071 Returns NULL for invalid types. */
23072
23073 static struct type *
23074 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
23075 {
23076 struct type *this_type = NULL;
23077
23078 switch (die->tag)
23079 {
23080 case DW_TAG_class_type:
23081 case DW_TAG_interface_type:
23082 case DW_TAG_structure_type:
23083 case DW_TAG_union_type:
23084 this_type = read_structure_type (die, cu);
23085 break;
23086 case DW_TAG_enumeration_type:
23087 this_type = read_enumeration_type (die, cu);
23088 break;
23089 case DW_TAG_subprogram:
23090 case DW_TAG_subroutine_type:
23091 case DW_TAG_inlined_subroutine:
23092 this_type = read_subroutine_type (die, cu);
23093 break;
23094 case DW_TAG_array_type:
23095 this_type = read_array_type (die, cu);
23096 break;
23097 case DW_TAG_set_type:
23098 this_type = read_set_type (die, cu);
23099 break;
23100 case DW_TAG_pointer_type:
23101 this_type = read_tag_pointer_type (die, cu);
23102 break;
23103 case DW_TAG_ptr_to_member_type:
23104 this_type = read_tag_ptr_to_member_type (die, cu);
23105 break;
23106 case DW_TAG_reference_type:
23107 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
23108 break;
23109 case DW_TAG_rvalue_reference_type:
23110 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
23111 break;
23112 case DW_TAG_const_type:
23113 this_type = read_tag_const_type (die, cu);
23114 break;
23115 case DW_TAG_volatile_type:
23116 this_type = read_tag_volatile_type (die, cu);
23117 break;
23118 case DW_TAG_restrict_type:
23119 this_type = read_tag_restrict_type (die, cu);
23120 break;
23121 case DW_TAG_string_type:
23122 this_type = read_tag_string_type (die, cu);
23123 break;
23124 case DW_TAG_typedef:
23125 this_type = read_typedef (die, cu);
23126 break;
23127 case DW_TAG_subrange_type:
23128 this_type = read_subrange_type (die, cu);
23129 break;
23130 case DW_TAG_base_type:
23131 this_type = read_base_type (die, cu);
23132 break;
23133 case DW_TAG_unspecified_type:
23134 this_type = read_unspecified_type (die, cu);
23135 break;
23136 case DW_TAG_namespace:
23137 this_type = read_namespace_type (die, cu);
23138 break;
23139 case DW_TAG_module:
23140 this_type = read_module_type (die, cu);
23141 break;
23142 case DW_TAG_atomic_type:
23143 this_type = read_tag_atomic_type (die, cu);
23144 break;
23145 default:
23146 complaint (_("unexpected tag in read_type_die: '%s'"),
23147 dwarf_tag_name (die->tag));
23148 break;
23149 }
23150
23151 return this_type;
23152 }
23153
23154 /* See if we can figure out if the class lives in a namespace. We do
23155 this by looking for a member function; its demangled name will
23156 contain namespace info, if there is any.
23157 Return the computed name or NULL.
23158 Space for the result is allocated on the objfile's obstack.
23159 This is the full-die version of guess_partial_die_structure_name.
23160 In this case we know DIE has no useful parent. */
23161
23162 static const char *
23163 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23164 {
23165 struct die_info *spec_die;
23166 struct dwarf2_cu *spec_cu;
23167 struct die_info *child;
23168 struct objfile *objfile = cu->per_objfile->objfile;
23169
23170 spec_cu = cu;
23171 spec_die = die_specification (die, &spec_cu);
23172 if (spec_die != NULL)
23173 {
23174 die = spec_die;
23175 cu = spec_cu;
23176 }
23177
23178 for (child = die->child;
23179 child != NULL;
23180 child = child->sibling)
23181 {
23182 if (child->tag == DW_TAG_subprogram)
23183 {
23184 const char *linkage_name = dw2_linkage_name (child, cu);
23185
23186 if (linkage_name != NULL)
23187 {
23188 gdb::unique_xmalloc_ptr<char> actual_name
23189 (cu->language_defn->class_name_from_physname (linkage_name));
23190 const char *name = NULL;
23191
23192 if (actual_name != NULL)
23193 {
23194 const char *die_name = dwarf2_name (die, cu);
23195
23196 if (die_name != NULL
23197 && strcmp (die_name, actual_name.get ()) != 0)
23198 {
23199 /* Strip off the class name from the full name.
23200 We want the prefix. */
23201 int die_name_len = strlen (die_name);
23202 int actual_name_len = strlen (actual_name.get ());
23203 const char *ptr = actual_name.get ();
23204
23205 /* Test for '::' as a sanity check. */
23206 if (actual_name_len > die_name_len + 2
23207 && ptr[actual_name_len - die_name_len - 1] == ':')
23208 name = obstack_strndup (
23209 &objfile->per_bfd->storage_obstack,
23210 ptr, actual_name_len - die_name_len - 2);
23211 }
23212 }
23213 return name;
23214 }
23215 }
23216 }
23217
23218 return NULL;
23219 }
23220
23221 /* GCC might emit a nameless typedef that has a linkage name. Determine the
23222 prefix part in such case. See
23223 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23224
23225 static const char *
23226 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23227 {
23228 struct attribute *attr;
23229 const char *base;
23230
23231 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23232 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23233 return NULL;
23234
23235 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
23236 return NULL;
23237
23238 attr = dw2_linkage_name_attr (die, cu);
23239 const char *attr_name = attr->as_string ();
23240 if (attr == NULL || attr_name == NULL)
23241 return NULL;
23242
23243 /* dwarf2_name had to be already called. */
23244 gdb_assert (attr->canonical_string_p ());
23245
23246 /* Strip the base name, keep any leading namespaces/classes. */
23247 base = strrchr (attr_name, ':');
23248 if (base == NULL || base == attr_name || base[-1] != ':')
23249 return "";
23250
23251 struct objfile *objfile = cu->per_objfile->objfile;
23252 return obstack_strndup (&objfile->per_bfd->storage_obstack,
23253 attr_name,
23254 &base[-1] - attr_name);
23255 }
23256
23257 /* Return the name of the namespace/class that DIE is defined within,
23258 or "" if we can't tell. The caller should not xfree the result.
23259
23260 For example, if we're within the method foo() in the following
23261 code:
23262
23263 namespace N {
23264 class C {
23265 void foo () {
23266 }
23267 };
23268 }
23269
23270 then determine_prefix on foo's die will return "N::C". */
23271
23272 static const char *
23273 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23274 {
23275 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23276 struct die_info *parent, *spec_die;
23277 struct dwarf2_cu *spec_cu;
23278 struct type *parent_type;
23279 const char *retval;
23280
23281 if (cu->language != language_cplus
23282 && cu->language != language_fortran && cu->language != language_d
23283 && cu->language != language_rust)
23284 return "";
23285
23286 retval = anonymous_struct_prefix (die, cu);
23287 if (retval)
23288 return retval;
23289
23290 /* We have to be careful in the presence of DW_AT_specification.
23291 For example, with GCC 3.4, given the code
23292
23293 namespace N {
23294 void foo() {
23295 // Definition of N::foo.
23296 }
23297 }
23298
23299 then we'll have a tree of DIEs like this:
23300
23301 1: DW_TAG_compile_unit
23302 2: DW_TAG_namespace // N
23303 3: DW_TAG_subprogram // declaration of N::foo
23304 4: DW_TAG_subprogram // definition of N::foo
23305 DW_AT_specification // refers to die #3
23306
23307 Thus, when processing die #4, we have to pretend that we're in
23308 the context of its DW_AT_specification, namely the contex of die
23309 #3. */
23310 spec_cu = cu;
23311 spec_die = die_specification (die, &spec_cu);
23312 if (spec_die == NULL)
23313 parent = die->parent;
23314 else
23315 {
23316 parent = spec_die->parent;
23317 cu = spec_cu;
23318 }
23319
23320 if (parent == NULL)
23321 return "";
23322 else if (parent->building_fullname)
23323 {
23324 const char *name;
23325 const char *parent_name;
23326
23327 /* It has been seen on RealView 2.2 built binaries,
23328 DW_TAG_template_type_param types actually _defined_ as
23329 children of the parent class:
23330
23331 enum E {};
23332 template class <class Enum> Class{};
23333 Class<enum E> class_e;
23334
23335 1: DW_TAG_class_type (Class)
23336 2: DW_TAG_enumeration_type (E)
23337 3: DW_TAG_enumerator (enum1:0)
23338 3: DW_TAG_enumerator (enum2:1)
23339 ...
23340 2: DW_TAG_template_type_param
23341 DW_AT_type DW_FORM_ref_udata (E)
23342
23343 Besides being broken debug info, it can put GDB into an
23344 infinite loop. Consider:
23345
23346 When we're building the full name for Class<E>, we'll start
23347 at Class, and go look over its template type parameters,
23348 finding E. We'll then try to build the full name of E, and
23349 reach here. We're now trying to build the full name of E,
23350 and look over the parent DIE for containing scope. In the
23351 broken case, if we followed the parent DIE of E, we'd again
23352 find Class, and once again go look at its template type
23353 arguments, etc., etc. Simply don't consider such parent die
23354 as source-level parent of this die (it can't be, the language
23355 doesn't allow it), and break the loop here. */
23356 name = dwarf2_name (die, cu);
23357 parent_name = dwarf2_name (parent, cu);
23358 complaint (_("template param type '%s' defined within parent '%s'"),
23359 name ? name : "<unknown>",
23360 parent_name ? parent_name : "<unknown>");
23361 return "";
23362 }
23363 else
23364 switch (parent->tag)
23365 {
23366 case DW_TAG_namespace:
23367 parent_type = read_type_die (parent, cu);
23368 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23369 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23370 Work around this problem here. */
23371 if (cu->language == language_cplus
23372 && strcmp (parent_type->name (), "::") == 0)
23373 return "";
23374 /* We give a name to even anonymous namespaces. */
23375 return parent_type->name ();
23376 case DW_TAG_class_type:
23377 case DW_TAG_interface_type:
23378 case DW_TAG_structure_type:
23379 case DW_TAG_union_type:
23380 case DW_TAG_module:
23381 parent_type = read_type_die (parent, cu);
23382 if (parent_type->name () != NULL)
23383 return parent_type->name ();
23384 else
23385 /* An anonymous structure is only allowed non-static data
23386 members; no typedefs, no member functions, et cetera.
23387 So it does not need a prefix. */
23388 return "";
23389 case DW_TAG_compile_unit:
23390 case DW_TAG_partial_unit:
23391 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23392 if (cu->language == language_cplus
23393 && !per_objfile->per_bfd->types.empty ()
23394 && die->child != NULL
23395 && (die->tag == DW_TAG_class_type
23396 || die->tag == DW_TAG_structure_type
23397 || die->tag == DW_TAG_union_type))
23398 {
23399 const char *name = guess_full_die_structure_name (die, cu);
23400 if (name != NULL)
23401 return name;
23402 }
23403 return "";
23404 case DW_TAG_subprogram:
23405 /* Nested subroutines in Fortran get a prefix with the name
23406 of the parent's subroutine. */
23407 if (cu->language == language_fortran)
23408 {
23409 if ((die->tag == DW_TAG_subprogram)
23410 && (dwarf2_name (parent, cu) != NULL))
23411 return dwarf2_name (parent, cu);
23412 }
23413 return determine_prefix (parent, cu);
23414 case DW_TAG_enumeration_type:
23415 parent_type = read_type_die (parent, cu);
23416 if (parent_type->is_declared_class ())
23417 {
23418 if (parent_type->name () != NULL)
23419 return parent_type->name ();
23420 return "";
23421 }
23422 /* Fall through. */
23423 default:
23424 return determine_prefix (parent, cu);
23425 }
23426 }
23427
23428 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23429 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23430 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23431 an obconcat, otherwise allocate storage for the result. The CU argument is
23432 used to determine the language and hence, the appropriate separator. */
23433
23434 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23435
23436 static char *
23437 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23438 int physname, struct dwarf2_cu *cu)
23439 {
23440 const char *lead = "";
23441 const char *sep;
23442
23443 if (suffix == NULL || suffix[0] == '\0'
23444 || prefix == NULL || prefix[0] == '\0')
23445 sep = "";
23446 else if (cu->language == language_d)
23447 {
23448 /* For D, the 'main' function could be defined in any module, but it
23449 should never be prefixed. */
23450 if (strcmp (suffix, "D main") == 0)
23451 {
23452 prefix = "";
23453 sep = "";
23454 }
23455 else
23456 sep = ".";
23457 }
23458 else if (cu->language == language_fortran && physname)
23459 {
23460 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23461 DW_AT_MIPS_linkage_name is preferred and used instead. */
23462
23463 lead = "__";
23464 sep = "_MOD_";
23465 }
23466 else
23467 sep = "::";
23468
23469 if (prefix == NULL)
23470 prefix = "";
23471 if (suffix == NULL)
23472 suffix = "";
23473
23474 if (obs == NULL)
23475 {
23476 char *retval
23477 = ((char *)
23478 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23479
23480 strcpy (retval, lead);
23481 strcat (retval, prefix);
23482 strcat (retval, sep);
23483 strcat (retval, suffix);
23484 return retval;
23485 }
23486 else
23487 {
23488 /* We have an obstack. */
23489 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23490 }
23491 }
23492
23493 /* Get name of a die, return NULL if not found. */
23494
23495 static const char *
23496 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23497 struct objfile *objfile)
23498 {
23499 if (name && cu->language == language_cplus)
23500 {
23501 gdb::unique_xmalloc_ptr<char> canon_name
23502 = cp_canonicalize_string (name);
23503
23504 if (canon_name != nullptr)
23505 name = objfile->intern (canon_name.get ());
23506 }
23507
23508 return name;
23509 }
23510
23511 /* Get name of a die, return NULL if not found.
23512 Anonymous namespaces are converted to their magic string. */
23513
23514 static const char *
23515 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23516 {
23517 struct attribute *attr;
23518 struct objfile *objfile = cu->per_objfile->objfile;
23519
23520 attr = dwarf2_attr (die, DW_AT_name, cu);
23521 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23522 if (attr_name == nullptr
23523 && die->tag != DW_TAG_namespace
23524 && die->tag != DW_TAG_class_type
23525 && die->tag != DW_TAG_interface_type
23526 && die->tag != DW_TAG_structure_type
23527 && die->tag != DW_TAG_union_type)
23528 return NULL;
23529
23530 switch (die->tag)
23531 {
23532 case DW_TAG_compile_unit:
23533 case DW_TAG_partial_unit:
23534 /* Compilation units have a DW_AT_name that is a filename, not
23535 a source language identifier. */
23536 case DW_TAG_enumeration_type:
23537 case DW_TAG_enumerator:
23538 /* These tags always have simple identifiers already; no need
23539 to canonicalize them. */
23540 return attr_name;
23541
23542 case DW_TAG_namespace:
23543 if (attr_name != nullptr)
23544 return attr_name;
23545 return CP_ANONYMOUS_NAMESPACE_STR;
23546
23547 case DW_TAG_class_type:
23548 case DW_TAG_interface_type:
23549 case DW_TAG_structure_type:
23550 case DW_TAG_union_type:
23551 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23552 structures or unions. These were of the form "._%d" in GCC 4.1,
23553 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23554 and GCC 4.4. We work around this problem by ignoring these. */
23555 if (attr_name != nullptr
23556 && (startswith (attr_name, "._")
23557 || startswith (attr_name, "<anonymous")))
23558 return NULL;
23559
23560 /* GCC might emit a nameless typedef that has a linkage name. See
23561 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23562 if (!attr || attr_name == NULL)
23563 {
23564 attr = dw2_linkage_name_attr (die, cu);
23565 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23566 if (attr == NULL || attr_name == NULL)
23567 return NULL;
23568
23569 /* Avoid demangling attr_name the second time on a second
23570 call for the same DIE. */
23571 if (!attr->canonical_string_p ())
23572 {
23573 gdb::unique_xmalloc_ptr<char> demangled
23574 (gdb_demangle (attr_name, DMGL_TYPES));
23575 if (demangled == nullptr)
23576 return nullptr;
23577
23578 attr->set_string_canonical (objfile->intern (demangled.get ()));
23579 attr_name = attr->as_string ();
23580 }
23581
23582 /* Strip any leading namespaces/classes, keep only the
23583 base name. DW_AT_name for named DIEs does not
23584 contain the prefixes. */
23585 const char *base = strrchr (attr_name, ':');
23586 if (base && base > attr_name && base[-1] == ':')
23587 return &base[1];
23588 else
23589 return attr_name;
23590 }
23591 break;
23592
23593 default:
23594 break;
23595 }
23596
23597 if (!attr->canonical_string_p ())
23598 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23599 objfile));
23600 return attr->as_string ();
23601 }
23602
23603 /* Return the die that this die in an extension of, or NULL if there
23604 is none. *EXT_CU is the CU containing DIE on input, and the CU
23605 containing the return value on output. */
23606
23607 static struct die_info *
23608 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23609 {
23610 struct attribute *attr;
23611
23612 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23613 if (attr == NULL)
23614 return NULL;
23615
23616 return follow_die_ref (die, attr, ext_cu);
23617 }
23618
23619 static void
23620 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23621 {
23622 unsigned int i;
23623
23624 print_spaces (indent, f);
23625 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23626 dwarf_tag_name (die->tag), die->abbrev,
23627 sect_offset_str (die->sect_off));
23628
23629 if (die->parent != NULL)
23630 {
23631 print_spaces (indent, f);
23632 fprintf_unfiltered (f, " parent at offset: %s\n",
23633 sect_offset_str (die->parent->sect_off));
23634 }
23635
23636 print_spaces (indent, f);
23637 fprintf_unfiltered (f, " has children: %s\n",
23638 dwarf_bool_name (die->child != NULL));
23639
23640 print_spaces (indent, f);
23641 fprintf_unfiltered (f, " attributes:\n");
23642
23643 for (i = 0; i < die->num_attrs; ++i)
23644 {
23645 print_spaces (indent, f);
23646 fprintf_unfiltered (f, " %s (%s) ",
23647 dwarf_attr_name (die->attrs[i].name),
23648 dwarf_form_name (die->attrs[i].form));
23649
23650 switch (die->attrs[i].form)
23651 {
23652 case DW_FORM_addr:
23653 case DW_FORM_addrx:
23654 case DW_FORM_GNU_addr_index:
23655 fprintf_unfiltered (f, "address: ");
23656 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23657 break;
23658 case DW_FORM_block2:
23659 case DW_FORM_block4:
23660 case DW_FORM_block:
23661 case DW_FORM_block1:
23662 fprintf_unfiltered (f, "block: size %s",
23663 pulongest (die->attrs[i].as_block ()->size));
23664 break;
23665 case DW_FORM_exprloc:
23666 fprintf_unfiltered (f, "expression: size %s",
23667 pulongest (die->attrs[i].as_block ()->size));
23668 break;
23669 case DW_FORM_data16:
23670 fprintf_unfiltered (f, "constant of 16 bytes");
23671 break;
23672 case DW_FORM_ref_addr:
23673 fprintf_unfiltered (f, "ref address: ");
23674 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23675 break;
23676 case DW_FORM_GNU_ref_alt:
23677 fprintf_unfiltered (f, "alt ref address: ");
23678 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23679 break;
23680 case DW_FORM_ref1:
23681 case DW_FORM_ref2:
23682 case DW_FORM_ref4:
23683 case DW_FORM_ref8:
23684 case DW_FORM_ref_udata:
23685 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23686 (long) (die->attrs[i].as_unsigned ()));
23687 break;
23688 case DW_FORM_data1:
23689 case DW_FORM_data2:
23690 case DW_FORM_data4:
23691 case DW_FORM_data8:
23692 case DW_FORM_udata:
23693 fprintf_unfiltered (f, "constant: %s",
23694 pulongest (die->attrs[i].as_unsigned ()));
23695 break;
23696 case DW_FORM_sec_offset:
23697 fprintf_unfiltered (f, "section offset: %s",
23698 pulongest (die->attrs[i].as_unsigned ()));
23699 break;
23700 case DW_FORM_ref_sig8:
23701 fprintf_unfiltered (f, "signature: %s",
23702 hex_string (die->attrs[i].as_signature ()));
23703 break;
23704 case DW_FORM_string:
23705 case DW_FORM_strp:
23706 case DW_FORM_line_strp:
23707 case DW_FORM_strx:
23708 case DW_FORM_GNU_str_index:
23709 case DW_FORM_GNU_strp_alt:
23710 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23711 die->attrs[i].as_string ()
23712 ? die->attrs[i].as_string () : "",
23713 die->attrs[i].canonical_string_p () ? "is" : "not");
23714 break;
23715 case DW_FORM_flag:
23716 if (die->attrs[i].as_boolean ())
23717 fprintf_unfiltered (f, "flag: TRUE");
23718 else
23719 fprintf_unfiltered (f, "flag: FALSE");
23720 break;
23721 case DW_FORM_flag_present:
23722 fprintf_unfiltered (f, "flag: TRUE");
23723 break;
23724 case DW_FORM_indirect:
23725 /* The reader will have reduced the indirect form to
23726 the "base form" so this form should not occur. */
23727 fprintf_unfiltered (f,
23728 "unexpected attribute form: DW_FORM_indirect");
23729 break;
23730 case DW_FORM_sdata:
23731 case DW_FORM_implicit_const:
23732 fprintf_unfiltered (f, "constant: %s",
23733 plongest (die->attrs[i].as_signed ()));
23734 break;
23735 default:
23736 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23737 die->attrs[i].form);
23738 break;
23739 }
23740 fprintf_unfiltered (f, "\n");
23741 }
23742 }
23743
23744 static void
23745 dump_die_for_error (struct die_info *die)
23746 {
23747 dump_die_shallow (gdb_stderr, 0, die);
23748 }
23749
23750 static void
23751 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23752 {
23753 int indent = level * 4;
23754
23755 gdb_assert (die != NULL);
23756
23757 if (level >= max_level)
23758 return;
23759
23760 dump_die_shallow (f, indent, die);
23761
23762 if (die->child != NULL)
23763 {
23764 print_spaces (indent, f);
23765 fprintf_unfiltered (f, " Children:");
23766 if (level + 1 < max_level)
23767 {
23768 fprintf_unfiltered (f, "\n");
23769 dump_die_1 (f, level + 1, max_level, die->child);
23770 }
23771 else
23772 {
23773 fprintf_unfiltered (f,
23774 " [not printed, max nesting level reached]\n");
23775 }
23776 }
23777
23778 if (die->sibling != NULL && level > 0)
23779 {
23780 dump_die_1 (f, level, max_level, die->sibling);
23781 }
23782 }
23783
23784 /* This is called from the pdie macro in gdbinit.in.
23785 It's not static so gcc will keep a copy callable from gdb. */
23786
23787 void
23788 dump_die (struct die_info *die, int max_level)
23789 {
23790 dump_die_1 (gdb_stdlog, 0, max_level, die);
23791 }
23792
23793 static void
23794 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23795 {
23796 void **slot;
23797
23798 slot = htab_find_slot_with_hash (cu->die_hash, die,
23799 to_underlying (die->sect_off),
23800 INSERT);
23801
23802 *slot = die;
23803 }
23804
23805 /* Follow reference or signature attribute ATTR of SRC_DIE.
23806 On entry *REF_CU is the CU of SRC_DIE.
23807 On exit *REF_CU is the CU of the result. */
23808
23809 static struct die_info *
23810 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23811 struct dwarf2_cu **ref_cu)
23812 {
23813 struct die_info *die;
23814
23815 if (attr->form_is_ref ())
23816 die = follow_die_ref (src_die, attr, ref_cu);
23817 else if (attr->form == DW_FORM_ref_sig8)
23818 die = follow_die_sig (src_die, attr, ref_cu);
23819 else
23820 {
23821 dump_die_for_error (src_die);
23822 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23823 objfile_name ((*ref_cu)->per_objfile->objfile));
23824 }
23825
23826 return die;
23827 }
23828
23829 /* Follow reference OFFSET.
23830 On entry *REF_CU is the CU of the source die referencing OFFSET.
23831 On exit *REF_CU is the CU of the result.
23832 Returns NULL if OFFSET is invalid. */
23833
23834 static struct die_info *
23835 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23836 struct dwarf2_cu **ref_cu)
23837 {
23838 struct die_info temp_die;
23839 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23840 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23841
23842 gdb_assert (cu->per_cu != NULL);
23843
23844 target_cu = cu;
23845
23846 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23847 "source CU contains target offset: %d",
23848 sect_offset_str (cu->per_cu->sect_off),
23849 sect_offset_str (sect_off),
23850 cu->header.offset_in_cu_p (sect_off));
23851
23852 if (cu->per_cu->is_debug_types)
23853 {
23854 /* .debug_types CUs cannot reference anything outside their CU.
23855 If they need to, they have to reference a signatured type via
23856 DW_FORM_ref_sig8. */
23857 if (!cu->header.offset_in_cu_p (sect_off))
23858 return NULL;
23859 }
23860 else if (offset_in_dwz != cu->per_cu->is_dwz
23861 || !cu->header.offset_in_cu_p (sect_off))
23862 {
23863 struct dwarf2_per_cu_data *per_cu;
23864
23865 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23866 per_objfile);
23867
23868 dwarf_read_debug_printf_v ("target CU offset: %s, "
23869 "target CU DIEs loaded: %d",
23870 sect_offset_str (per_cu->sect_off),
23871 per_objfile->get_cu (per_cu) != nullptr);
23872
23873 /* If necessary, add it to the queue and load its DIEs.
23874
23875 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23876 it doesn't mean they are currently loaded. Since we require them
23877 to be loaded, we must check for ourselves. */
23878 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23879 || per_objfile->get_cu (per_cu) == nullptr)
23880 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23881 false, cu->language);
23882
23883 target_cu = per_objfile->get_cu (per_cu);
23884 gdb_assert (target_cu != nullptr);
23885 }
23886 else if (cu->dies == NULL)
23887 {
23888 /* We're loading full DIEs during partial symbol reading. */
23889 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23890 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23891 language_minimal);
23892 }
23893
23894 *ref_cu = target_cu;
23895 temp_die.sect_off = sect_off;
23896
23897 if (target_cu != cu)
23898 target_cu->ancestor = cu;
23899
23900 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23901 &temp_die,
23902 to_underlying (sect_off));
23903 }
23904
23905 /* Follow reference attribute ATTR of SRC_DIE.
23906 On entry *REF_CU is the CU of SRC_DIE.
23907 On exit *REF_CU is the CU of the result. */
23908
23909 static struct die_info *
23910 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23911 struct dwarf2_cu **ref_cu)
23912 {
23913 sect_offset sect_off = attr->get_ref_die_offset ();
23914 struct dwarf2_cu *cu = *ref_cu;
23915 struct die_info *die;
23916
23917 die = follow_die_offset (sect_off,
23918 (attr->form == DW_FORM_GNU_ref_alt
23919 || cu->per_cu->is_dwz),
23920 ref_cu);
23921 if (!die)
23922 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23923 "at %s [in module %s]"),
23924 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23925 objfile_name (cu->per_objfile->objfile));
23926
23927 return die;
23928 }
23929
23930 /* See read.h. */
23931
23932 struct dwarf2_locexpr_baton
23933 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23934 dwarf2_per_cu_data *per_cu,
23935 dwarf2_per_objfile *per_objfile,
23936 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23937 bool resolve_abstract_p)
23938 {
23939 struct die_info *die;
23940 struct attribute *attr;
23941 struct dwarf2_locexpr_baton retval;
23942 struct objfile *objfile = per_objfile->objfile;
23943
23944 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23945 if (cu == nullptr)
23946 cu = load_cu (per_cu, per_objfile, false);
23947
23948 if (cu == nullptr)
23949 {
23950 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23951 Instead just throw an error, not much else we can do. */
23952 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23953 sect_offset_str (sect_off), objfile_name (objfile));
23954 }
23955
23956 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23957 if (!die)
23958 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23959 sect_offset_str (sect_off), objfile_name (objfile));
23960
23961 attr = dwarf2_attr (die, DW_AT_location, cu);
23962 if (!attr && resolve_abstract_p
23963 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23964 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23965 {
23966 CORE_ADDR pc = get_frame_pc ();
23967 CORE_ADDR baseaddr = objfile->text_section_offset ();
23968 struct gdbarch *gdbarch = objfile->arch ();
23969
23970 for (const auto &cand_off
23971 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23972 {
23973 struct dwarf2_cu *cand_cu = cu;
23974 struct die_info *cand
23975 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23976 if (!cand
23977 || !cand->parent
23978 || cand->parent->tag != DW_TAG_subprogram)
23979 continue;
23980
23981 CORE_ADDR pc_low, pc_high;
23982 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23983 if (pc_low == ((CORE_ADDR) -1))
23984 continue;
23985 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23986 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23987 if (!(pc_low <= pc && pc < pc_high))
23988 continue;
23989
23990 die = cand;
23991 attr = dwarf2_attr (die, DW_AT_location, cu);
23992 break;
23993 }
23994 }
23995
23996 if (!attr)
23997 {
23998 /* DWARF: "If there is no such attribute, then there is no effect.".
23999 DATA is ignored if SIZE is 0. */
24000
24001 retval.data = NULL;
24002 retval.size = 0;
24003 }
24004 else if (attr->form_is_section_offset ())
24005 {
24006 struct dwarf2_loclist_baton loclist_baton;
24007 CORE_ADDR pc = get_frame_pc ();
24008 size_t size;
24009
24010 fill_in_loclist_baton (cu, &loclist_baton, attr);
24011
24012 retval.data = dwarf2_find_location_expression (&loclist_baton,
24013 &size, pc);
24014 retval.size = size;
24015 }
24016 else
24017 {
24018 if (!attr->form_is_block ())
24019 error (_("Dwarf Error: DIE at %s referenced in module %s "
24020 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
24021 sect_offset_str (sect_off), objfile_name (objfile));
24022
24023 struct dwarf_block *block = attr->as_block ();
24024 retval.data = block->data;
24025 retval.size = block->size;
24026 }
24027 retval.per_objfile = per_objfile;
24028 retval.per_cu = cu->per_cu;
24029
24030 per_objfile->age_comp_units ();
24031
24032 return retval;
24033 }
24034
24035 /* See read.h. */
24036
24037 struct dwarf2_locexpr_baton
24038 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
24039 dwarf2_per_cu_data *per_cu,
24040 dwarf2_per_objfile *per_objfile,
24041 gdb::function_view<CORE_ADDR ()> get_frame_pc)
24042 {
24043 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
24044
24045 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
24046 get_frame_pc);
24047 }
24048
24049 /* Write a constant of a given type as target-ordered bytes into
24050 OBSTACK. */
24051
24052 static const gdb_byte *
24053 write_constant_as_bytes (struct obstack *obstack,
24054 enum bfd_endian byte_order,
24055 struct type *type,
24056 ULONGEST value,
24057 LONGEST *len)
24058 {
24059 gdb_byte *result;
24060
24061 *len = TYPE_LENGTH (type);
24062 result = (gdb_byte *) obstack_alloc (obstack, *len);
24063 store_unsigned_integer (result, *len, byte_order, value);
24064
24065 return result;
24066 }
24067
24068 /* See read.h. */
24069
24070 const gdb_byte *
24071 dwarf2_fetch_constant_bytes (sect_offset sect_off,
24072 dwarf2_per_cu_data *per_cu,
24073 dwarf2_per_objfile *per_objfile,
24074 obstack *obstack,
24075 LONGEST *len)
24076 {
24077 struct die_info *die;
24078 struct attribute *attr;
24079 const gdb_byte *result = NULL;
24080 struct type *type;
24081 LONGEST value;
24082 enum bfd_endian byte_order;
24083 struct objfile *objfile = per_objfile->objfile;
24084
24085 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24086 if (cu == nullptr)
24087 cu = load_cu (per_cu, per_objfile, false);
24088
24089 if (cu == nullptr)
24090 {
24091 /* We shouldn't get here for a dummy CU, but don't crash on the user.
24092 Instead just throw an error, not much else we can do. */
24093 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
24094 sect_offset_str (sect_off), objfile_name (objfile));
24095 }
24096
24097 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24098 if (!die)
24099 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
24100 sect_offset_str (sect_off), objfile_name (objfile));
24101
24102 attr = dwarf2_attr (die, DW_AT_const_value, cu);
24103 if (attr == NULL)
24104 return NULL;
24105
24106 byte_order = (bfd_big_endian (objfile->obfd)
24107 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
24108
24109 switch (attr->form)
24110 {
24111 case DW_FORM_addr:
24112 case DW_FORM_addrx:
24113 case DW_FORM_GNU_addr_index:
24114 {
24115 gdb_byte *tem;
24116
24117 *len = cu->header.addr_size;
24118 tem = (gdb_byte *) obstack_alloc (obstack, *len);
24119 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
24120 result = tem;
24121 }
24122 break;
24123 case DW_FORM_string:
24124 case DW_FORM_strp:
24125 case DW_FORM_strx:
24126 case DW_FORM_GNU_str_index:
24127 case DW_FORM_GNU_strp_alt:
24128 /* The string is already allocated on the objfile obstack, point
24129 directly to it. */
24130 {
24131 const char *attr_name = attr->as_string ();
24132 result = (const gdb_byte *) attr_name;
24133 *len = strlen (attr_name);
24134 }
24135 break;
24136 case DW_FORM_block1:
24137 case DW_FORM_block2:
24138 case DW_FORM_block4:
24139 case DW_FORM_block:
24140 case DW_FORM_exprloc:
24141 case DW_FORM_data16:
24142 {
24143 struct dwarf_block *block = attr->as_block ();
24144 result = block->data;
24145 *len = block->size;
24146 }
24147 break;
24148
24149 /* The DW_AT_const_value attributes are supposed to carry the
24150 symbol's value "represented as it would be on the target
24151 architecture." By the time we get here, it's already been
24152 converted to host endianness, so we just need to sign- or
24153 zero-extend it as appropriate. */
24154 case DW_FORM_data1:
24155 type = die_type (die, cu);
24156 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24157 if (result == NULL)
24158 result = write_constant_as_bytes (obstack, byte_order,
24159 type, value, len);
24160 break;
24161 case DW_FORM_data2:
24162 type = die_type (die, cu);
24163 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24164 if (result == NULL)
24165 result = write_constant_as_bytes (obstack, byte_order,
24166 type, value, len);
24167 break;
24168 case DW_FORM_data4:
24169 type = die_type (die, cu);
24170 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24171 if (result == NULL)
24172 result = write_constant_as_bytes (obstack, byte_order,
24173 type, value, len);
24174 break;
24175 case DW_FORM_data8:
24176 type = die_type (die, cu);
24177 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24178 if (result == NULL)
24179 result = write_constant_as_bytes (obstack, byte_order,
24180 type, value, len);
24181 break;
24182
24183 case DW_FORM_sdata:
24184 case DW_FORM_implicit_const:
24185 type = die_type (die, cu);
24186 result = write_constant_as_bytes (obstack, byte_order,
24187 type, attr->as_signed (), len);
24188 break;
24189
24190 case DW_FORM_udata:
24191 type = die_type (die, cu);
24192 result = write_constant_as_bytes (obstack, byte_order,
24193 type, attr->as_unsigned (), len);
24194 break;
24195
24196 default:
24197 complaint (_("unsupported const value attribute form: '%s'"),
24198 dwarf_form_name (attr->form));
24199 break;
24200 }
24201
24202 return result;
24203 }
24204
24205 /* See read.h. */
24206
24207 struct type *
24208 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
24209 dwarf2_per_cu_data *per_cu,
24210 dwarf2_per_objfile *per_objfile)
24211 {
24212 struct die_info *die;
24213
24214 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24215 if (cu == nullptr)
24216 cu = load_cu (per_cu, per_objfile, false);
24217
24218 if (cu == nullptr)
24219 return nullptr;
24220
24221 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24222 if (!die)
24223 return NULL;
24224
24225 return die_type (die, cu);
24226 }
24227
24228 /* See read.h. */
24229
24230 struct type *
24231 dwarf2_get_die_type (cu_offset die_offset,
24232 dwarf2_per_cu_data *per_cu,
24233 dwarf2_per_objfile *per_objfile)
24234 {
24235 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
24236 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
24237 }
24238
24239 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
24240 On entry *REF_CU is the CU of SRC_DIE.
24241 On exit *REF_CU is the CU of the result.
24242 Returns NULL if the referenced DIE isn't found. */
24243
24244 static struct die_info *
24245 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24246 struct dwarf2_cu **ref_cu)
24247 {
24248 struct die_info temp_die;
24249 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
24250 struct die_info *die;
24251 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
24252
24253
24254 /* While it might be nice to assert sig_type->type == NULL here,
24255 we can get here for DW_AT_imported_declaration where we need
24256 the DIE not the type. */
24257
24258 /* If necessary, add it to the queue and load its DIEs.
24259
24260 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24261 it doesn't mean they are currently loaded. Since we require them
24262 to be loaded, we must check for ourselves. */
24263 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
24264 language_minimal)
24265 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
24266 read_signatured_type (sig_type, per_objfile);
24267
24268 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
24269 gdb_assert (sig_cu != NULL);
24270 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24271 temp_die.sect_off = sig_type->type_offset_in_section;
24272 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24273 to_underlying (temp_die.sect_off));
24274 if (die)
24275 {
24276 /* For .gdb_index version 7 keep track of included TUs.
24277 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24278 if (per_objfile->per_bfd->index_table != NULL
24279 && per_objfile->per_bfd->index_table->version <= 7)
24280 {
24281 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24282 }
24283
24284 *ref_cu = sig_cu;
24285 if (sig_cu != cu)
24286 sig_cu->ancestor = cu;
24287
24288 return die;
24289 }
24290
24291 return NULL;
24292 }
24293
24294 /* Follow signatured type referenced by ATTR in SRC_DIE.
24295 On entry *REF_CU is the CU of SRC_DIE.
24296 On exit *REF_CU is the CU of the result.
24297 The result is the DIE of the type.
24298 If the referenced type cannot be found an error is thrown. */
24299
24300 static struct die_info *
24301 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24302 struct dwarf2_cu **ref_cu)
24303 {
24304 ULONGEST signature = attr->as_signature ();
24305 struct signatured_type *sig_type;
24306 struct die_info *die;
24307
24308 gdb_assert (attr->form == DW_FORM_ref_sig8);
24309
24310 sig_type = lookup_signatured_type (*ref_cu, signature);
24311 /* sig_type will be NULL if the signatured type is missing from
24312 the debug info. */
24313 if (sig_type == NULL)
24314 {
24315 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24316 " from DIE at %s [in module %s]"),
24317 hex_string (signature), sect_offset_str (src_die->sect_off),
24318 objfile_name ((*ref_cu)->per_objfile->objfile));
24319 }
24320
24321 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24322 if (die == NULL)
24323 {
24324 dump_die_for_error (src_die);
24325 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24326 " from DIE at %s [in module %s]"),
24327 hex_string (signature), sect_offset_str (src_die->sect_off),
24328 objfile_name ((*ref_cu)->per_objfile->objfile));
24329 }
24330
24331 return die;
24332 }
24333
24334 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24335 reading in and processing the type unit if necessary. */
24336
24337 static struct type *
24338 get_signatured_type (struct die_info *die, ULONGEST signature,
24339 struct dwarf2_cu *cu)
24340 {
24341 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24342 struct signatured_type *sig_type;
24343 struct dwarf2_cu *type_cu;
24344 struct die_info *type_die;
24345 struct type *type;
24346
24347 sig_type = lookup_signatured_type (cu, signature);
24348 /* sig_type will be NULL if the signatured type is missing from
24349 the debug info. */
24350 if (sig_type == NULL)
24351 {
24352 complaint (_("Dwarf Error: Cannot find 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 return build_error_marker_type (cu, die);
24357 }
24358
24359 /* If we already know the type we're done. */
24360 type = per_objfile->get_type_for_signatured_type (sig_type);
24361 if (type != nullptr)
24362 return type;
24363
24364 type_cu = cu;
24365 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24366 if (type_die != NULL)
24367 {
24368 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24369 is created. This is important, for example, because for c++ classes
24370 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24371 type = read_type_die (type_die, type_cu);
24372 if (type == NULL)
24373 {
24374 complaint (_("Dwarf Error: Cannot build signatured type %s"
24375 " referenced from DIE at %s [in module %s]"),
24376 hex_string (signature), sect_offset_str (die->sect_off),
24377 objfile_name (per_objfile->objfile));
24378 type = build_error_marker_type (cu, die);
24379 }
24380 }
24381 else
24382 {
24383 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24384 " from DIE at %s [in module %s]"),
24385 hex_string (signature), sect_offset_str (die->sect_off),
24386 objfile_name (per_objfile->objfile));
24387 type = build_error_marker_type (cu, die);
24388 }
24389
24390 per_objfile->set_type_for_signatured_type (sig_type, type);
24391
24392 return type;
24393 }
24394
24395 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24396 reading in and processing the type unit if necessary. */
24397
24398 static struct type *
24399 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24400 struct dwarf2_cu *cu) /* ARI: editCase function */
24401 {
24402 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24403 if (attr->form_is_ref ())
24404 {
24405 struct dwarf2_cu *type_cu = cu;
24406 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24407
24408 return read_type_die (type_die, type_cu);
24409 }
24410 else if (attr->form == DW_FORM_ref_sig8)
24411 {
24412 return get_signatured_type (die, attr->as_signature (), cu);
24413 }
24414 else
24415 {
24416 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24417
24418 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24419 " at %s [in module %s]"),
24420 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24421 objfile_name (per_objfile->objfile));
24422 return build_error_marker_type (cu, die);
24423 }
24424 }
24425
24426 /* Load the DIEs associated with type unit PER_CU into memory. */
24427
24428 static void
24429 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24430 dwarf2_per_objfile *per_objfile)
24431 {
24432 struct signatured_type *sig_type;
24433
24434 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24435 gdb_assert (! per_cu->type_unit_group_p ());
24436
24437 /* We have the per_cu, but we need the signatured_type.
24438 Fortunately this is an easy translation. */
24439 gdb_assert (per_cu->is_debug_types);
24440 sig_type = (struct signatured_type *) per_cu;
24441
24442 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24443
24444 read_signatured_type (sig_type, per_objfile);
24445
24446 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24447 }
24448
24449 /* Read in a signatured type and build its CU and DIEs.
24450 If the type is a stub for the real type in a DWO file,
24451 read in the real type from the DWO file as well. */
24452
24453 static void
24454 read_signatured_type (signatured_type *sig_type,
24455 dwarf2_per_objfile *per_objfile)
24456 {
24457 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24458
24459 gdb_assert (per_cu->is_debug_types);
24460 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24461
24462 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24463
24464 if (!reader.dummy_p)
24465 {
24466 struct dwarf2_cu *cu = reader.cu;
24467 const gdb_byte *info_ptr = reader.info_ptr;
24468
24469 gdb_assert (cu->die_hash == NULL);
24470 cu->die_hash =
24471 htab_create_alloc_ex (cu->header.length / 12,
24472 die_hash,
24473 die_eq,
24474 NULL,
24475 &cu->comp_unit_obstack,
24476 hashtab_obstack_allocate,
24477 dummy_obstack_deallocate);
24478
24479 if (reader.comp_unit_die->has_children)
24480 reader.comp_unit_die->child
24481 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24482 reader.comp_unit_die);
24483 cu->dies = reader.comp_unit_die;
24484 /* comp_unit_die is not stored in die_hash, no need. */
24485
24486 /* We try not to read any attributes in this function, because
24487 not all CUs needed for references have been loaded yet, and
24488 symbol table processing isn't initialized. But we have to
24489 set the CU language, or we won't be able to build types
24490 correctly. Similarly, if we do not read the producer, we can
24491 not apply producer-specific interpretation. */
24492 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24493
24494 reader.keep ();
24495 }
24496
24497 sig_type->per_cu.tu_read = 1;
24498 }
24499
24500 /* Decode simple location descriptions.
24501 Given a pointer to a dwarf block that defines a location, compute
24502 the location and return the value. If COMPUTED is non-null, it is
24503 set to true to indicate that decoding was successful, and false
24504 otherwise. If COMPUTED is null, then this function may emit a
24505 complaint. */
24506
24507 static CORE_ADDR
24508 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24509 {
24510 struct objfile *objfile = cu->per_objfile->objfile;
24511 size_t i;
24512 size_t size = blk->size;
24513 const gdb_byte *data = blk->data;
24514 CORE_ADDR stack[64];
24515 int stacki;
24516 unsigned int bytes_read, unsnd;
24517 gdb_byte op;
24518
24519 if (computed != nullptr)
24520 *computed = false;
24521
24522 i = 0;
24523 stacki = 0;
24524 stack[stacki] = 0;
24525 stack[++stacki] = 0;
24526
24527 while (i < size)
24528 {
24529 op = data[i++];
24530 switch (op)
24531 {
24532 case DW_OP_lit0:
24533 case DW_OP_lit1:
24534 case DW_OP_lit2:
24535 case DW_OP_lit3:
24536 case DW_OP_lit4:
24537 case DW_OP_lit5:
24538 case DW_OP_lit6:
24539 case DW_OP_lit7:
24540 case DW_OP_lit8:
24541 case DW_OP_lit9:
24542 case DW_OP_lit10:
24543 case DW_OP_lit11:
24544 case DW_OP_lit12:
24545 case DW_OP_lit13:
24546 case DW_OP_lit14:
24547 case DW_OP_lit15:
24548 case DW_OP_lit16:
24549 case DW_OP_lit17:
24550 case DW_OP_lit18:
24551 case DW_OP_lit19:
24552 case DW_OP_lit20:
24553 case DW_OP_lit21:
24554 case DW_OP_lit22:
24555 case DW_OP_lit23:
24556 case DW_OP_lit24:
24557 case DW_OP_lit25:
24558 case DW_OP_lit26:
24559 case DW_OP_lit27:
24560 case DW_OP_lit28:
24561 case DW_OP_lit29:
24562 case DW_OP_lit30:
24563 case DW_OP_lit31:
24564 stack[++stacki] = op - DW_OP_lit0;
24565 break;
24566
24567 case DW_OP_reg0:
24568 case DW_OP_reg1:
24569 case DW_OP_reg2:
24570 case DW_OP_reg3:
24571 case DW_OP_reg4:
24572 case DW_OP_reg5:
24573 case DW_OP_reg6:
24574 case DW_OP_reg7:
24575 case DW_OP_reg8:
24576 case DW_OP_reg9:
24577 case DW_OP_reg10:
24578 case DW_OP_reg11:
24579 case DW_OP_reg12:
24580 case DW_OP_reg13:
24581 case DW_OP_reg14:
24582 case DW_OP_reg15:
24583 case DW_OP_reg16:
24584 case DW_OP_reg17:
24585 case DW_OP_reg18:
24586 case DW_OP_reg19:
24587 case DW_OP_reg20:
24588 case DW_OP_reg21:
24589 case DW_OP_reg22:
24590 case DW_OP_reg23:
24591 case DW_OP_reg24:
24592 case DW_OP_reg25:
24593 case DW_OP_reg26:
24594 case DW_OP_reg27:
24595 case DW_OP_reg28:
24596 case DW_OP_reg29:
24597 case DW_OP_reg30:
24598 case DW_OP_reg31:
24599 stack[++stacki] = op - DW_OP_reg0;
24600 if (i < size)
24601 {
24602 if (computed == nullptr)
24603 dwarf2_complex_location_expr_complaint ();
24604 else
24605 return 0;
24606 }
24607 break;
24608
24609 case DW_OP_regx:
24610 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24611 i += bytes_read;
24612 stack[++stacki] = unsnd;
24613 if (i < size)
24614 {
24615 if (computed == nullptr)
24616 dwarf2_complex_location_expr_complaint ();
24617 else
24618 return 0;
24619 }
24620 break;
24621
24622 case DW_OP_addr:
24623 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24624 &bytes_read);
24625 i += bytes_read;
24626 break;
24627
24628 case DW_OP_const1u:
24629 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24630 i += 1;
24631 break;
24632
24633 case DW_OP_const1s:
24634 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24635 i += 1;
24636 break;
24637
24638 case DW_OP_const2u:
24639 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24640 i += 2;
24641 break;
24642
24643 case DW_OP_const2s:
24644 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24645 i += 2;
24646 break;
24647
24648 case DW_OP_const4u:
24649 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24650 i += 4;
24651 break;
24652
24653 case DW_OP_const4s:
24654 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24655 i += 4;
24656 break;
24657
24658 case DW_OP_const8u:
24659 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24660 i += 8;
24661 break;
24662
24663 case DW_OP_constu:
24664 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24665 &bytes_read);
24666 i += bytes_read;
24667 break;
24668
24669 case DW_OP_consts:
24670 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24671 i += bytes_read;
24672 break;
24673
24674 case DW_OP_dup:
24675 stack[stacki + 1] = stack[stacki];
24676 stacki++;
24677 break;
24678
24679 case DW_OP_plus:
24680 stack[stacki - 1] += stack[stacki];
24681 stacki--;
24682 break;
24683
24684 case DW_OP_plus_uconst:
24685 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24686 &bytes_read);
24687 i += bytes_read;
24688 break;
24689
24690 case DW_OP_minus:
24691 stack[stacki - 1] -= stack[stacki];
24692 stacki--;
24693 break;
24694
24695 case DW_OP_deref:
24696 /* If we're not the last op, then we definitely can't encode
24697 this using GDB's address_class enum. This is valid for partial
24698 global symbols, although the variable's address will be bogus
24699 in the psymtab. */
24700 if (i < size)
24701 {
24702 if (computed == nullptr)
24703 dwarf2_complex_location_expr_complaint ();
24704 else
24705 return 0;
24706 }
24707 break;
24708
24709 case DW_OP_GNU_push_tls_address:
24710 case DW_OP_form_tls_address:
24711 /* The top of the stack has the offset from the beginning
24712 of the thread control block at which the variable is located. */
24713 /* Nothing should follow this operator, so the top of stack would
24714 be returned. */
24715 /* This is valid for partial global symbols, but the variable's
24716 address will be bogus in the psymtab. Make it always at least
24717 non-zero to not look as a variable garbage collected by linker
24718 which have DW_OP_addr 0. */
24719 if (i < size)
24720 {
24721 if (computed == nullptr)
24722 dwarf2_complex_location_expr_complaint ();
24723 else
24724 return 0;
24725 }
24726 stack[stacki]++;
24727 break;
24728
24729 case DW_OP_GNU_uninit:
24730 if (computed != nullptr)
24731 return 0;
24732 break;
24733
24734 case DW_OP_addrx:
24735 case DW_OP_GNU_addr_index:
24736 case DW_OP_GNU_const_index:
24737 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24738 &bytes_read);
24739 i += bytes_read;
24740 break;
24741
24742 default:
24743 if (computed == nullptr)
24744 {
24745 const char *name = get_DW_OP_name (op);
24746
24747 if (name)
24748 complaint (_("unsupported stack op: '%s'"),
24749 name);
24750 else
24751 complaint (_("unsupported stack op: '%02x'"),
24752 op);
24753 }
24754
24755 return (stack[stacki]);
24756 }
24757
24758 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24759 outside of the allocated space. Also enforce minimum>0. */
24760 if (stacki >= ARRAY_SIZE (stack) - 1)
24761 {
24762 if (computed == nullptr)
24763 complaint (_("location description stack overflow"));
24764 return 0;
24765 }
24766
24767 if (stacki <= 0)
24768 {
24769 if (computed == nullptr)
24770 complaint (_("location description stack underflow"));
24771 return 0;
24772 }
24773 }
24774
24775 if (computed != nullptr)
24776 *computed = true;
24777 return (stack[stacki]);
24778 }
24779
24780 /* memory allocation interface */
24781
24782 static struct dwarf_block *
24783 dwarf_alloc_block (struct dwarf2_cu *cu)
24784 {
24785 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24786 }
24787
24788 static struct die_info *
24789 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24790 {
24791 struct die_info *die;
24792 size_t size = sizeof (struct die_info);
24793
24794 if (num_attrs > 1)
24795 size += (num_attrs - 1) * sizeof (struct attribute);
24796
24797 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24798 memset (die, 0, sizeof (struct die_info));
24799 return (die);
24800 }
24801
24802 \f
24803
24804 /* Macro support. */
24805
24806 /* An overload of dwarf_decode_macros that finds the correct section
24807 and ensures it is read in before calling the other overload. */
24808
24809 static void
24810 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24811 int section_is_gnu)
24812 {
24813 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24814 struct objfile *objfile = per_objfile->objfile;
24815 const struct line_header *lh = cu->line_header;
24816 unsigned int offset_size = cu->header.offset_size;
24817 struct dwarf2_section_info *section;
24818 const char *section_name;
24819
24820 if (cu->dwo_unit != nullptr)
24821 {
24822 if (section_is_gnu)
24823 {
24824 section = &cu->dwo_unit->dwo_file->sections.macro;
24825 section_name = ".debug_macro.dwo";
24826 }
24827 else
24828 {
24829 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24830 section_name = ".debug_macinfo.dwo";
24831 }
24832 }
24833 else
24834 {
24835 if (section_is_gnu)
24836 {
24837 section = &per_objfile->per_bfd->macro;
24838 section_name = ".debug_macro";
24839 }
24840 else
24841 {
24842 section = &per_objfile->per_bfd->macinfo;
24843 section_name = ".debug_macinfo";
24844 }
24845 }
24846
24847 section->read (objfile);
24848 if (section->buffer == nullptr)
24849 {
24850 complaint (_("missing %s section"), section_name);
24851 return;
24852 }
24853
24854 buildsym_compunit *builder = cu->get_builder ();
24855
24856 struct dwarf2_section_info *str_offsets_section;
24857 struct dwarf2_section_info *str_section;
24858 ULONGEST str_offsets_base;
24859
24860 if (cu->dwo_unit != nullptr)
24861 {
24862 str_offsets_section = &cu->dwo_unit->dwo_file
24863 ->sections.str_offsets;
24864 str_section = &cu->dwo_unit->dwo_file->sections.str;
24865 str_offsets_base = cu->header.addr_size;
24866 }
24867 else
24868 {
24869 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24870 str_section = &per_objfile->per_bfd->str;
24871 str_offsets_base = *cu->str_offsets_base;
24872 }
24873
24874 dwarf_decode_macros (per_objfile, builder, section, lh,
24875 offset_size, offset, str_section, str_offsets_section,
24876 str_offsets_base, section_is_gnu);
24877 }
24878
24879 /* Return the .debug_loc section to use for CU.
24880 For DWO files use .debug_loc.dwo. */
24881
24882 static struct dwarf2_section_info *
24883 cu_debug_loc_section (struct dwarf2_cu *cu)
24884 {
24885 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24886
24887 if (cu->dwo_unit)
24888 {
24889 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24890
24891 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24892 }
24893 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24894 : &per_objfile->per_bfd->loc);
24895 }
24896
24897 /* Return the .debug_rnglists section to use for CU. */
24898 static struct dwarf2_section_info *
24899 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24900 {
24901 if (cu->header.version < 5)
24902 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24903 cu->header.version);
24904 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24905
24906 /* Make sure we read the .debug_rnglists section from the file that
24907 contains the DW_AT_ranges attribute we are reading. Normally that
24908 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24909 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24910 program. */
24911 if (cu->dwo_unit != nullptr
24912 && tag != DW_TAG_compile_unit
24913 && tag != DW_TAG_skeleton_unit)
24914 {
24915 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24916
24917 if (sections->rnglists.size > 0)
24918 return &sections->rnglists;
24919 else
24920 error (_(".debug_rnglists section is missing from .dwo file."));
24921 }
24922 return &dwarf2_per_objfile->per_bfd->rnglists;
24923 }
24924
24925 /* A helper function that fills in a dwarf2_loclist_baton. */
24926
24927 static void
24928 fill_in_loclist_baton (struct dwarf2_cu *cu,
24929 struct dwarf2_loclist_baton *baton,
24930 const struct attribute *attr)
24931 {
24932 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24933 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24934
24935 section->read (per_objfile->objfile);
24936
24937 baton->per_objfile = per_objfile;
24938 baton->per_cu = cu->per_cu;
24939 gdb_assert (baton->per_cu);
24940 /* We don't know how long the location list is, but make sure we
24941 don't run off the edge of the section. */
24942 baton->size = section->size - attr->as_unsigned ();
24943 baton->data = section->buffer + attr->as_unsigned ();
24944 if (cu->base_address.has_value ())
24945 baton->base_address = *cu->base_address;
24946 else
24947 baton->base_address = 0;
24948 baton->from_dwo = cu->dwo_unit != NULL;
24949 }
24950
24951 static void
24952 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24953 struct dwarf2_cu *cu, int is_block)
24954 {
24955 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24956 struct objfile *objfile = per_objfile->objfile;
24957 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24958
24959 if (attr->form_is_section_offset ()
24960 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24961 the section. If so, fall through to the complaint in the
24962 other branch. */
24963 && attr->as_unsigned () < section->get_size (objfile))
24964 {
24965 struct dwarf2_loclist_baton *baton;
24966
24967 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24968
24969 fill_in_loclist_baton (cu, baton, attr);
24970
24971 if (!cu->base_address.has_value ())
24972 complaint (_("Location list used without "
24973 "specifying the CU base address."));
24974
24975 SYMBOL_ACLASS_INDEX (sym) = (is_block
24976 ? dwarf2_loclist_block_index
24977 : dwarf2_loclist_index);
24978 SYMBOL_LOCATION_BATON (sym) = baton;
24979 }
24980 else
24981 {
24982 struct dwarf2_locexpr_baton *baton;
24983
24984 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24985 baton->per_objfile = per_objfile;
24986 baton->per_cu = cu->per_cu;
24987 gdb_assert (baton->per_cu);
24988
24989 if (attr->form_is_block ())
24990 {
24991 /* Note that we're just copying the block's data pointer
24992 here, not the actual data. We're still pointing into the
24993 info_buffer for SYM's objfile; right now we never release
24994 that buffer, but when we do clean up properly this may
24995 need to change. */
24996 struct dwarf_block *block = attr->as_block ();
24997 baton->size = block->size;
24998 baton->data = block->data;
24999 }
25000 else
25001 {
25002 dwarf2_invalid_attrib_class_complaint ("location description",
25003 sym->natural_name ());
25004 baton->size = 0;
25005 }
25006
25007 SYMBOL_ACLASS_INDEX (sym) = (is_block
25008 ? dwarf2_locexpr_block_index
25009 : dwarf2_locexpr_index);
25010 SYMBOL_LOCATION_BATON (sym) = baton;
25011 }
25012 }
25013
25014 /* See read.h. */
25015
25016 const comp_unit_head *
25017 dwarf2_per_cu_data::get_header () const
25018 {
25019 if (!m_header_read_in)
25020 {
25021 const gdb_byte *info_ptr
25022 = this->section->buffer + to_underlying (this->sect_off);
25023
25024 memset (&m_header, 0, sizeof (m_header));
25025
25026 read_comp_unit_head (&m_header, info_ptr, this->section,
25027 rcuh_kind::COMPILE);
25028
25029 m_header_read_in = true;
25030 }
25031
25032 return &m_header;
25033 }
25034
25035 /* See read.h. */
25036
25037 int
25038 dwarf2_per_cu_data::addr_size () const
25039 {
25040 return this->get_header ()->addr_size;
25041 }
25042
25043 /* See read.h. */
25044
25045 int
25046 dwarf2_per_cu_data::offset_size () const
25047 {
25048 return this->get_header ()->offset_size;
25049 }
25050
25051 /* See read.h. */
25052
25053 int
25054 dwarf2_per_cu_data::ref_addr_size () const
25055 {
25056 const comp_unit_head *header = this->get_header ();
25057
25058 if (header->version == 2)
25059 return header->addr_size;
25060 else
25061 return header->offset_size;
25062 }
25063
25064 /* See read.h. */
25065
25066 struct type *
25067 dwarf2_cu::addr_type () const
25068 {
25069 struct objfile *objfile = this->per_objfile->objfile;
25070 struct type *void_type = objfile_type (objfile)->builtin_void;
25071 struct type *addr_type = lookup_pointer_type (void_type);
25072 int addr_size = this->per_cu->addr_size ();
25073
25074 if (TYPE_LENGTH (addr_type) == addr_size)
25075 return addr_type;
25076
25077 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
25078 return addr_type;
25079 }
25080
25081 /* A helper function for dwarf2_find_containing_comp_unit that returns
25082 the index of the result, and that searches a vector. It will
25083 return a result even if the offset in question does not actually
25084 occur in any CU. This is separate so that it can be unit
25085 tested. */
25086
25087 static int
25088 dwarf2_find_containing_comp_unit
25089 (sect_offset sect_off,
25090 unsigned int offset_in_dwz,
25091 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
25092 {
25093 int low, high;
25094
25095 low = 0;
25096 high = all_comp_units.size () - 1;
25097 while (high > low)
25098 {
25099 struct dwarf2_per_cu_data *mid_cu;
25100 int mid = low + (high - low) / 2;
25101
25102 mid_cu = all_comp_units[mid];
25103 if (mid_cu->is_dwz > offset_in_dwz
25104 || (mid_cu->is_dwz == offset_in_dwz
25105 && mid_cu->sect_off + mid_cu->length > sect_off))
25106 high = mid;
25107 else
25108 low = mid + 1;
25109 }
25110 gdb_assert (low == high);
25111 return low;
25112 }
25113
25114 /* Locate the .debug_info compilation unit from CU's objfile which contains
25115 the DIE at OFFSET. Raises an error on failure. */
25116
25117 static struct dwarf2_per_cu_data *
25118 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25119 unsigned int offset_in_dwz,
25120 dwarf2_per_objfile *per_objfile)
25121 {
25122 int low = dwarf2_find_containing_comp_unit
25123 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
25124 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
25125
25126 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25127 {
25128 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25129 error (_("Dwarf Error: could not find partial DIE containing "
25130 "offset %s [in module %s]"),
25131 sect_offset_str (sect_off),
25132 bfd_get_filename (per_objfile->objfile->obfd));
25133
25134 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
25135 <= sect_off);
25136 return per_objfile->per_bfd->all_comp_units[low-1];
25137 }
25138 else
25139 {
25140 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
25141 && sect_off >= this_cu->sect_off + this_cu->length)
25142 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25143 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25144 return this_cu;
25145 }
25146 }
25147
25148 #if GDB_SELF_TEST
25149
25150 namespace selftests {
25151 namespace find_containing_comp_unit {
25152
25153 static void
25154 run_test ()
25155 {
25156 struct dwarf2_per_cu_data one {};
25157 struct dwarf2_per_cu_data two {};
25158 struct dwarf2_per_cu_data three {};
25159 struct dwarf2_per_cu_data four {};
25160
25161 one.length = 5;
25162 two.sect_off = sect_offset (one.length);
25163 two.length = 7;
25164
25165 three.length = 5;
25166 three.is_dwz = 1;
25167 four.sect_off = sect_offset (three.length);
25168 four.length = 7;
25169 four.is_dwz = 1;
25170
25171 std::vector<dwarf2_per_cu_data *> units;
25172 units.push_back (&one);
25173 units.push_back (&two);
25174 units.push_back (&three);
25175 units.push_back (&four);
25176
25177 int result;
25178
25179 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25180 SELF_CHECK (units[result] == &one);
25181 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25182 SELF_CHECK (units[result] == &one);
25183 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25184 SELF_CHECK (units[result] == &two);
25185
25186 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25187 SELF_CHECK (units[result] == &three);
25188 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25189 SELF_CHECK (units[result] == &three);
25190 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25191 SELF_CHECK (units[result] == &four);
25192 }
25193
25194 }
25195 }
25196
25197 #endif /* GDB_SELF_TEST */
25198
25199 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
25200
25201 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25202 dwarf2_per_objfile *per_objfile)
25203 : per_cu (per_cu),
25204 per_objfile (per_objfile),
25205 mark (false),
25206 has_loclist (false),
25207 checked_producer (false),
25208 producer_is_gxx_lt_4_6 (false),
25209 producer_is_gcc_lt_4_3 (false),
25210 producer_is_icc (false),
25211 producer_is_icc_lt_14 (false),
25212 producer_is_codewarrior (false),
25213 processing_has_namespace_info (false)
25214 {
25215 }
25216
25217 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25218
25219 static void
25220 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25221 enum language pretend_language)
25222 {
25223 struct attribute *attr;
25224
25225 /* Set the language we're debugging. */
25226 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25227 if (attr != nullptr)
25228 set_cu_language (attr->constant_value (0), cu);
25229 else
25230 {
25231 cu->language = pretend_language;
25232 cu->language_defn = language_def (cu->language);
25233 }
25234
25235 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25236 }
25237
25238 /* See read.h. */
25239
25240 dwarf2_cu *
25241 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
25242 {
25243 auto it = m_dwarf2_cus.find (per_cu);
25244 if (it == m_dwarf2_cus.end ())
25245 return nullptr;
25246
25247 return it->second;
25248 }
25249
25250 /* See read.h. */
25251
25252 void
25253 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25254 {
25255 gdb_assert (this->get_cu (per_cu) == nullptr);
25256
25257 m_dwarf2_cus[per_cu] = cu;
25258 }
25259
25260 /* See read.h. */
25261
25262 void
25263 dwarf2_per_objfile::age_comp_units ()
25264 {
25265 dwarf_read_debug_printf_v ("running");
25266
25267 /* This is not expected to be called in the middle of CU expansion. There is
25268 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25269 loaded in memory. Calling age_comp_units while the queue is in use could
25270 make us free the DIEs for a CU that is in the queue and therefore break
25271 that invariant. */
25272 gdb_assert (!this->per_bfd->queue.has_value ());
25273
25274 /* Start by clearing all marks. */
25275 for (auto pair : m_dwarf2_cus)
25276 pair.second->mark = false;
25277
25278 /* Traverse all CUs, mark them and their dependencies if used recently
25279 enough. */
25280 for (auto pair : m_dwarf2_cus)
25281 {
25282 dwarf2_cu *cu = pair.second;
25283
25284 cu->last_used++;
25285 if (cu->last_used <= dwarf_max_cache_age)
25286 dwarf2_mark (cu);
25287 }
25288
25289 /* Delete all CUs still not marked. */
25290 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
25291 {
25292 dwarf2_cu *cu = it->second;
25293
25294 if (!cu->mark)
25295 {
25296 dwarf_read_debug_printf_v ("deleting old CU %s",
25297 sect_offset_str (cu->per_cu->sect_off));
25298 delete cu;
25299 it = m_dwarf2_cus.erase (it);
25300 }
25301 else
25302 it++;
25303 }
25304 }
25305
25306 /* See read.h. */
25307
25308 void
25309 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25310 {
25311 auto it = m_dwarf2_cus.find (per_cu);
25312 if (it == m_dwarf2_cus.end ())
25313 return;
25314
25315 delete it->second;
25316
25317 m_dwarf2_cus.erase (it);
25318 }
25319
25320 dwarf2_per_objfile::~dwarf2_per_objfile ()
25321 {
25322 remove_all_cus ();
25323 }
25324
25325 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25326 We store these in a hash table separate from the DIEs, and preserve them
25327 when the DIEs are flushed out of cache.
25328
25329 The CU "per_cu" pointer is needed because offset alone is not enough to
25330 uniquely identify the type. A file may have multiple .debug_types sections,
25331 or the type may come from a DWO file. Furthermore, while it's more logical
25332 to use per_cu->section+offset, with Fission the section with the data is in
25333 the DWO file but we don't know that section at the point we need it.
25334 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25335 because we can enter the lookup routine, get_die_type_at_offset, from
25336 outside this file, and thus won't necessarily have PER_CU->cu.
25337 Fortunately, PER_CU is stable for the life of the objfile. */
25338
25339 struct dwarf2_per_cu_offset_and_type
25340 {
25341 const struct dwarf2_per_cu_data *per_cu;
25342 sect_offset sect_off;
25343 struct type *type;
25344 };
25345
25346 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25347
25348 static hashval_t
25349 per_cu_offset_and_type_hash (const void *item)
25350 {
25351 const struct dwarf2_per_cu_offset_and_type *ofs
25352 = (const struct dwarf2_per_cu_offset_and_type *) item;
25353
25354 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25355 }
25356
25357 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25358
25359 static int
25360 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25361 {
25362 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25363 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25364 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25365 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25366
25367 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25368 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25369 }
25370
25371 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25372 table if necessary. For convenience, return TYPE.
25373
25374 The DIEs reading must have careful ordering to:
25375 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25376 reading current DIE.
25377 * Not trying to dereference contents of still incompletely read in types
25378 while reading in other DIEs.
25379 * Enable referencing still incompletely read in types just by a pointer to
25380 the type without accessing its fields.
25381
25382 Therefore caller should follow these rules:
25383 * Try to fetch any prerequisite types we may need to build this DIE type
25384 before building the type and calling set_die_type.
25385 * After building type call set_die_type for current DIE as soon as
25386 possible before fetching more types to complete the current type.
25387 * Make the type as complete as possible before fetching more types. */
25388
25389 static struct type *
25390 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25391 bool skip_data_location)
25392 {
25393 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25394 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25395 struct objfile *objfile = per_objfile->objfile;
25396 struct attribute *attr;
25397 struct dynamic_prop prop;
25398
25399 /* For Ada types, make sure that the gnat-specific data is always
25400 initialized (if not already set). There are a few types where
25401 we should not be doing so, because the type-specific area is
25402 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25403 where the type-specific area is used to store the floatformat).
25404 But this is not a problem, because the gnat-specific information
25405 is actually not needed for these types. */
25406 if (need_gnat_info (cu)
25407 && type->code () != TYPE_CODE_FUNC
25408 && type->code () != TYPE_CODE_FLT
25409 && type->code () != TYPE_CODE_METHODPTR
25410 && type->code () != TYPE_CODE_MEMBERPTR
25411 && type->code () != TYPE_CODE_METHOD
25412 && type->code () != TYPE_CODE_FIXED_POINT
25413 && !HAVE_GNAT_AUX_INFO (type))
25414 INIT_GNAT_SPECIFIC (type);
25415
25416 /* Read DW_AT_allocated and set in type. */
25417 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25418 if (attr != NULL)
25419 {
25420 struct type *prop_type = cu->addr_sized_int_type (false);
25421 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25422 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25423 }
25424
25425 /* Read DW_AT_associated and set in type. */
25426 attr = dwarf2_attr (die, DW_AT_associated, cu);
25427 if (attr != NULL)
25428 {
25429 struct type *prop_type = cu->addr_sized_int_type (false);
25430 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25431 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25432 }
25433
25434 /* Read DW_AT_data_location and set in type. */
25435 if (!skip_data_location)
25436 {
25437 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25438 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25439 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25440 }
25441
25442 if (per_objfile->die_type_hash == NULL)
25443 per_objfile->die_type_hash
25444 = htab_up (htab_create_alloc (127,
25445 per_cu_offset_and_type_hash,
25446 per_cu_offset_and_type_eq,
25447 NULL, xcalloc, xfree));
25448
25449 ofs.per_cu = cu->per_cu;
25450 ofs.sect_off = die->sect_off;
25451 ofs.type = type;
25452 slot = (struct dwarf2_per_cu_offset_and_type **)
25453 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25454 if (*slot)
25455 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25456 sect_offset_str (die->sect_off));
25457 *slot = XOBNEW (&objfile->objfile_obstack,
25458 struct dwarf2_per_cu_offset_and_type);
25459 **slot = ofs;
25460 return type;
25461 }
25462
25463 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25464 or return NULL if the die does not have a saved type. */
25465
25466 static struct type *
25467 get_die_type_at_offset (sect_offset sect_off,
25468 dwarf2_per_cu_data *per_cu,
25469 dwarf2_per_objfile *per_objfile)
25470 {
25471 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25472
25473 if (per_objfile->die_type_hash == NULL)
25474 return NULL;
25475
25476 ofs.per_cu = per_cu;
25477 ofs.sect_off = sect_off;
25478 slot = ((struct dwarf2_per_cu_offset_and_type *)
25479 htab_find (per_objfile->die_type_hash.get (), &ofs));
25480 if (slot)
25481 return slot->type;
25482 else
25483 return NULL;
25484 }
25485
25486 /* Look up the type for DIE in CU in die_type_hash,
25487 or return NULL if DIE does not have a saved type. */
25488
25489 static struct type *
25490 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25491 {
25492 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25493 }
25494
25495 /* Add a dependence relationship from CU to REF_PER_CU. */
25496
25497 static void
25498 dwarf2_add_dependence (struct dwarf2_cu *cu,
25499 struct dwarf2_per_cu_data *ref_per_cu)
25500 {
25501 void **slot;
25502
25503 if (cu->dependencies == NULL)
25504 cu->dependencies
25505 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25506 NULL, &cu->comp_unit_obstack,
25507 hashtab_obstack_allocate,
25508 dummy_obstack_deallocate);
25509
25510 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25511 if (*slot == NULL)
25512 *slot = ref_per_cu;
25513 }
25514
25515 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25516 Set the mark field in every compilation unit in the
25517 cache that we must keep because we are keeping CU.
25518
25519 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25520
25521 static int
25522 dwarf2_mark_helper (void **slot, void *data)
25523 {
25524 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25525 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25526 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25527
25528 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25529 reading of the chain. As such dependencies remain valid it is not much
25530 useful to track and undo them during QUIT cleanups. */
25531 if (cu == nullptr)
25532 return 1;
25533
25534 if (cu->mark)
25535 return 1;
25536
25537 cu->mark = true;
25538
25539 if (cu->dependencies != nullptr)
25540 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25541
25542 return 1;
25543 }
25544
25545 /* Set the mark field in CU and in every other compilation unit in the
25546 cache that we must keep because we are keeping CU. */
25547
25548 static void
25549 dwarf2_mark (struct dwarf2_cu *cu)
25550 {
25551 if (cu->mark)
25552 return;
25553
25554 cu->mark = true;
25555
25556 if (cu->dependencies != nullptr)
25557 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25558 }
25559
25560 /* Trivial hash function for partial_die_info: the hash value of a DIE
25561 is its offset in .debug_info for this objfile. */
25562
25563 static hashval_t
25564 partial_die_hash (const void *item)
25565 {
25566 const struct partial_die_info *part_die
25567 = (const struct partial_die_info *) item;
25568
25569 return to_underlying (part_die->sect_off);
25570 }
25571
25572 /* Trivial comparison function for partial_die_info structures: two DIEs
25573 are equal if they have the same offset. */
25574
25575 static int
25576 partial_die_eq (const void *item_lhs, const void *item_rhs)
25577 {
25578 const struct partial_die_info *part_die_lhs
25579 = (const struct partial_die_info *) item_lhs;
25580 const struct partial_die_info *part_die_rhs
25581 = (const struct partial_die_info *) item_rhs;
25582
25583 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25584 }
25585
25586 struct cmd_list_element *set_dwarf_cmdlist;
25587 struct cmd_list_element *show_dwarf_cmdlist;
25588
25589 static void
25590 show_check_physname (struct ui_file *file, int from_tty,
25591 struct cmd_list_element *c, const char *value)
25592 {
25593 fprintf_filtered (file,
25594 _("Whether to check \"physname\" is %s.\n"),
25595 value);
25596 }
25597
25598 void _initialize_dwarf2_read ();
25599 void
25600 _initialize_dwarf2_read ()
25601 {
25602 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25603 Set DWARF specific variables.\n\
25604 Configure DWARF variables such as the cache size."),
25605 &set_dwarf_cmdlist, "maintenance set dwarf ",
25606 0/*allow-unknown*/, &maintenance_set_cmdlist);
25607
25608 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25609 Show DWARF specific variables.\n\
25610 Show DWARF variables such as the cache size."),
25611 &show_dwarf_cmdlist, "maintenance show dwarf ",
25612 0/*allow-unknown*/, &maintenance_show_cmdlist);
25613
25614 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25615 &dwarf_max_cache_age, _("\
25616 Set the upper bound on the age of cached DWARF compilation units."), _("\
25617 Show the upper bound on the age of cached DWARF compilation units."), _("\
25618 A higher limit means that cached compilation units will be stored\n\
25619 in memory longer, and more total memory will be used. Zero disables\n\
25620 caching, which can slow down startup."),
25621 NULL,
25622 show_dwarf_max_cache_age,
25623 &set_dwarf_cmdlist,
25624 &show_dwarf_cmdlist);
25625
25626 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25627 Set debugging of the DWARF reader."), _("\
25628 Show debugging of the DWARF reader."), _("\
25629 When enabled (non-zero), debugging messages are printed during DWARF\n\
25630 reading and symtab expansion. A value of 1 (one) provides basic\n\
25631 information. A value greater than 1 provides more verbose information."),
25632 NULL,
25633 NULL,
25634 &setdebuglist, &showdebuglist);
25635
25636 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25637 Set debugging of the DWARF DIE reader."), _("\
25638 Show debugging of the DWARF DIE reader."), _("\
25639 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25640 The value is the maximum depth to print."),
25641 NULL,
25642 NULL,
25643 &setdebuglist, &showdebuglist);
25644
25645 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25646 Set debugging of the dwarf line reader."), _("\
25647 Show debugging of the dwarf line reader."), _("\
25648 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25649 A value of 1 (one) provides basic information.\n\
25650 A value greater than 1 provides more verbose information."),
25651 NULL,
25652 NULL,
25653 &setdebuglist, &showdebuglist);
25654
25655 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25656 Set cross-checking of \"physname\" code against demangler."), _("\
25657 Show cross-checking of \"physname\" code against demangler."), _("\
25658 When enabled, GDB's internal \"physname\" code is checked against\n\
25659 the demangler."),
25660 NULL, show_check_physname,
25661 &setdebuglist, &showdebuglist);
25662
25663 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25664 no_class, &use_deprecated_index_sections, _("\
25665 Set whether to use deprecated gdb_index sections."), _("\
25666 Show whether to use deprecated gdb_index sections."), _("\
25667 When enabled, deprecated .gdb_index sections are used anyway.\n\
25668 Normally they are ignored either because of a missing feature or\n\
25669 performance issue.\n\
25670 Warning: This option must be enabled before gdb reads the file."),
25671 NULL,
25672 NULL,
25673 &setlist, &showlist);
25674
25675 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25676 &dwarf2_locexpr_funcs);
25677 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25678 &dwarf2_loclist_funcs);
25679
25680 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25681 &dwarf2_block_frame_base_locexpr_funcs);
25682 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25683 &dwarf2_block_frame_base_loclist_funcs);
25684
25685 #if GDB_SELF_TEST
25686 selftests::register_test ("dw2_expand_symtabs_matching",
25687 selftests::dw2_expand_symtabs_matching::run_test);
25688 selftests::register_test ("dwarf2_find_containing_comp_unit",
25689 selftests::find_containing_comp_unit::run_test);
25690 #endif
25691 }